blob: 7153e3ca3630a2219d09dfca487cc7dcafa00a55 [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#ifndef __UBOOT__
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35#include <linux/module.h>
36#include <linux/delay.h>
37#include <linux/errno.h>
38#include <linux/err.h>
39#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/nand_bch.h>
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
48#include <linux/leds.h>
49#include <linux/io.h>
50#include <linux/mtd/partitions.h>
51#else
52#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Wolfgang Denk932394a2005-08-17 12:55:25 +020053#include <common.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020054#include <malloc.h>
55#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010056#include <linux/err.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000057#include <linux/compat.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020058#include <linux/mtd/mtd.h>
59#include <linux/mtd/nand.h>
60#include <linux/mtd/nand_ecc.h>
Christian Hitz4c6de852011-10-12 09:31:59 +020061#include <linux/mtd/nand_bch.h>
Stefan Roese10bb62d2009-04-24 15:58:33 +020062#ifdef CONFIG_MTD_PARTITIONS
63#include <linux/mtd/partitions.h>
64#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020065#include <asm/io.h>
66#include <asm/errno.h>
67
Peter Tyser8da60122009-02-04 13:47:22 -060068/*
69 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
70 * a flash. NAND flash is initialized prior to interrupts so standard timers
71 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
72 * which is greater than (max NAND reset time / NAND status read time).
73 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
74 */
75#ifndef CONFIG_SYS_NAND_RESET_CNT
76#define CONFIG_SYS_NAND_RESET_CNT 200000
77#endif
78
Heiko Schocherff94bc42014-06-24 10:10:04 +020079static bool is_module_text_address(unsigned long addr) {return 0;}
80#endif
81
Wolfgang Denk932394a2005-08-17 12:55:25 +020082/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010083static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020084 .eccbytes = 3,
85 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010086 .oobfree = {
87 {.offset = 3,
88 .length = 2},
89 {.offset = 6,
Christian Hitz90e3f392011-10-12 09:32:01 +020090 .length = 2} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020091};
92
William Juulcfa460a2007-10-31 13:53:06 +010093static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020094 .eccbytes = 6,
95 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010096 .oobfree = {
97 {.offset = 8,
Christian Hitz90e3f392011-10-12 09:32:01 +020098 . length = 8} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020099};
100
William Juulcfa460a2007-10-31 13:53:06 +0100101static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200102 .eccbytes = 24,
103 .eccpos = {
William Juulcfa460a2007-10-31 13:53:06 +0100104 40, 41, 42, 43, 44, 45, 46, 47,
105 48, 49, 50, 51, 52, 53, 54, 55,
106 56, 57, 58, 59, 60, 61, 62, 63},
107 .oobfree = {
108 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +0200109 .length = 38} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200110};
111
William Juulcfa460a2007-10-31 13:53:06 +0100112static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200113 .eccbytes = 48,
114 .eccpos = {
Christian Hitz90e3f392011-10-12 09:32:01 +0200115 80, 81, 82, 83, 84, 85, 86, 87,
116 88, 89, 90, 91, 92, 93, 94, 95,
117 96, 97, 98, 99, 100, 101, 102, 103,
William Juulcfa460a2007-10-31 13:53:06 +0100118 104, 105, 106, 107, 108, 109, 110, 111,
119 112, 113, 114, 115, 116, 117, 118, 119,
120 120, 121, 122, 123, 124, 125, 126, 127},
121 .oobfree = {
122 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +0200123 .length = 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200124};
125
Heiko Schocherff94bc42014-06-24 10:10:04 +0200126static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juulcfa460a2007-10-31 13:53:06 +0100127
128static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
129 struct mtd_oob_ops *ops);
130
Heiko Schocherff94bc42014-06-24 10:10:04 +0200131/*
132 * For devices which display every fart in the system on a separate LED. Is
133 * compiled away when LED support is disabled.
134 */
135DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200136
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200137static int check_offs_len(struct mtd_info *mtd,
138 loff_t ofs, uint64_t len)
139{
140 struct nand_chip *chip = mtd->priv;
141 int ret = 0;
142
143 /* Start address must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200144 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
145 pr_debug("%s: unaligned address\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200146 ret = -EINVAL;
147 }
148
149 /* Length must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200150 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
151 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200152 ret = -EINVAL;
153 }
154
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200155 return ret;
156}
157
Wolfgang Denk932394a2005-08-17 12:55:25 +0200158/**
159 * nand_release_device - [GENERIC] release chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000160 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200161 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200162 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200163 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200164static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100165{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200166 struct nand_chip *chip = mtd->priv;
167
Heiko Schocherff94bc42014-06-24 10:10:04 +0200168#ifndef __UBOOT__
169 /* Release the controller and the chip */
170 spin_lock(&chip->controller->lock);
171 chip->controller->active = NULL;
172 chip->state = FL_READY;
173 wake_up(&chip->controller->wq);
174 spin_unlock(&chip->controller->lock);
175#else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200176 /* De-select the NAND device */
177 chip->select_chip(mtd, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200178#endif
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100179}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200180
181/**
182 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000183 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200184 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200185 * Default read function for 8bit buswidth
Wolfgang Denk932394a2005-08-17 12:55:25 +0200186 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200187#ifndef __UBOOT__
188static uint8_t nand_read_byte(struct mtd_info *mtd)
189#else
Simon Schwarz82645f82011-10-31 06:34:44 +0000190uint8_t nand_read_byte(struct mtd_info *mtd)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200191#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200192{
William Juulcfa460a2007-10-31 13:53:06 +0100193 struct nand_chip *chip = mtd->priv;
194 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200195}
196
197/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200198 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000199 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
200 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200201 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000202 * Default read function for 16bit buswidth with endianness conversion.
203 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200204 */
William Juulcfa460a2007-10-31 13:53:06 +0100205static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200206{
William Juulcfa460a2007-10-31 13:53:06 +0100207 struct nand_chip *chip = mtd->priv;
208 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200209}
210
211/**
212 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000213 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200214 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000215 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200216 */
217static u16 nand_read_word(struct mtd_info *mtd)
218{
William Juulcfa460a2007-10-31 13:53:06 +0100219 struct nand_chip *chip = mtd->priv;
220 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200221}
222
223/**
224 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapindfe64e22013-01-14 03:46:50 +0000225 * @mtd: MTD device structure
226 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200227 *
228 * Default select function for 1 chip devices.
229 */
William Juulcfa460a2007-10-31 13:53:06 +0100230static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200231{
William Juulcfa460a2007-10-31 13:53:06 +0100232 struct nand_chip *chip = mtd->priv;
233
234 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200235 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100236 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200237 break;
238 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200239 break;
240
241 default:
242 BUG();
243 }
244}
245
246/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200247 * nand_write_byte - [DEFAULT] write single byte to chip
248 * @mtd: MTD device structure
249 * @byte: value to write
250 *
251 * Default function to write a byte to I/O[7:0]
252 */
253static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
254{
255 struct nand_chip *chip = mtd->priv;
256
257 chip->write_buf(mtd, &byte, 1);
258}
259
260/**
261 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
262 * @mtd: MTD device structure
263 * @byte: value to write
264 *
265 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
266 */
267static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
268{
269 struct nand_chip *chip = mtd->priv;
270 uint16_t word = byte;
271
272 /*
273 * It's not entirely clear what should happen to I/O[15:8] when writing
274 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
275 *
276 * When the host supports a 16-bit bus width, only data is
277 * transferred at the 16-bit width. All address and command line
278 * transfers shall use only the lower 8-bits of the data bus. During
279 * command transfers, the host may place any value on the upper
280 * 8-bits of the data bus. During address transfers, the host shall
281 * set the upper 8-bits of the data bus to 00h.
282 *
283 * One user of the write_byte callback is nand_onfi_set_features. The
284 * four parameters are specified to be written to I/O[7:0], but this is
285 * neither an address nor a command transfer. Let's assume a 0 on the
286 * upper I/O lines is OK.
287 */
288 chip->write_buf(mtd, (uint8_t *)&word, 2);
289}
290
291#if defined(__UBOOT__) && !defined(CONFIG_BLACKFIN)
292static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
293{
294 int i;
295
296 for (i = 0; i < len; i++)
297 writeb(buf[i], addr);
298}
299static void ioread8_rep(void *addr, uint8_t *buf, int len)
300{
301 int i;
302
303 for (i = 0; i < len; i++)
304 buf[i] = readb(addr);
305}
306
307static void ioread16_rep(void *addr, void *buf, int len)
308{
309 int i;
310 u16 *p = (u16 *) buf;
311 len >>= 1;
312
313 for (i = 0; i < len; i++)
314 p[i] = readw(addr);
315}
316
317static void iowrite16_rep(void *addr, void *buf, int len)
318{
319 int i;
320 u16 *p = (u16 *) buf;
321 len >>= 1;
322
323 for (i = 0; i < len; i++)
324 writew(p[i], addr);
325}
326#endif
327
328/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200329 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000330 * @mtd: MTD device structure
331 * @buf: data buffer
332 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200333 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000334 * Default write function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200335 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200336#ifndef __UBOOT__
337static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
338#else
Simon Schwarz82645f82011-10-31 06:34:44 +0000339void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200340#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200341{
William Juulcfa460a2007-10-31 13:53:06 +0100342 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200343
Heiko Schocherff94bc42014-06-24 10:10:04 +0200344 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200345}
346
347/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200348 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000349 * @mtd: MTD device structure
350 * @buf: buffer to store date
351 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200352 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000353 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200354 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200355#ifndef __UBOOT__
356static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
357#else
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400358void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200359#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200360{
William Juulcfa460a2007-10-31 13:53:06 +0100361 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200362
Heiko Schocherff94bc42014-06-24 10:10:04 +0200363 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200364}
365
Heiko Schocherff94bc42014-06-24 10:10:04 +0200366#ifdef __UBOOT__
367#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200368/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200369 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000370 * @mtd: MTD device structure
371 * @buf: buffer containing the data to compare
372 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200373 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000374 * Default verify function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200375 */
William Juulcfa460a2007-10-31 13:53:06 +0100376static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200377{
378 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100379 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200380
William Juulcfa460a2007-10-31 13:53:06 +0100381 for (i = 0; i < len; i++)
382 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200383 return -EFAULT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200384 return 0;
385}
386
387/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200388 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000389 * @mtd: MTD device structure
390 * @buf: buffer containing the data to compare
391 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200392 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000393 * Default verify function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200394 */
William Juulcfa460a2007-10-31 13:53:06 +0100395static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200396{
397 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100398 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200399 u16 *p = (u16 *) buf;
400 len >>= 1;
401
William Juulcfa460a2007-10-31 13:53:06 +0100402 for (i = 0; i < len; i++)
403 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200404 return -EFAULT;
405
406 return 0;
407}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200408#endif
409#endif
410
411/**
412 * nand_write_buf16 - [DEFAULT] write buffer to chip
413 * @mtd: MTD device structure
414 * @buf: data buffer
415 * @len: number of bytes to write
416 *
417 * Default write function for 16bit buswidth.
418 */
419#ifndef __UBOOT__
420static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
421#else
422void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
423#endif
424{
425 struct nand_chip *chip = mtd->priv;
426 u16 *p = (u16 *) buf;
427
428 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
429}
430
431/**
432 * nand_read_buf16 - [DEFAULT] read chip data into buffer
433 * @mtd: MTD device structure
434 * @buf: buffer to store date
435 * @len: number of bytes to read
436 *
437 * Default read function for 16bit buswidth.
438 */
439#ifndef __UBOOT__
440static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
441#else
442void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
443#endif
444{
445 struct nand_chip *chip = mtd->priv;
446 u16 *p = (u16 *) buf;
447
448 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
449}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200450
451/**
452 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000453 * @mtd: MTD device structure
454 * @ofs: offset from device start
455 * @getchip: 0, if the chip is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200456 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200457 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200458 */
459static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
460{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000461 int page, chipnr, res = 0, i = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100462 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200463 u16 bad;
464
Sergey Lapindfe64e22013-01-14 03:46:50 +0000465 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200466 ofs += mtd->erasesize - mtd->writesize;
467
William Juulcfa460a2007-10-31 13:53:06 +0100468 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200469
Wolfgang Denk932394a2005-08-17 12:55:25 +0200470 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100471 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200472
Heiko Schocherff94bc42014-06-24 10:10:04 +0200473 nand_get_device(mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200474
475 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100476 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200477 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200478
Sergey Lapindfe64e22013-01-14 03:46:50 +0000479 do {
480 if (chip->options & NAND_BUSWIDTH_16) {
481 chip->cmdfunc(mtd, NAND_CMD_READOOB,
482 chip->badblockpos & 0xFE, page);
483 bad = cpu_to_le16(chip->read_word(mtd));
484 if (chip->badblockpos & 0x1)
485 bad >>= 8;
486 else
487 bad &= 0xFF;
488 } else {
489 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
490 page);
491 bad = chip->read_byte(mtd);
492 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200493
Sergey Lapindfe64e22013-01-14 03:46:50 +0000494 if (likely(chip->badblockbits == 8))
495 res = bad != 0xFF;
496 else
497 res = hweight8(bad) < chip->badblockbits;
498 ofs += mtd->writesize;
499 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
500 i++;
501 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200502
Heiko Schocherff94bc42014-06-24 10:10:04 +0200503 if (getchip) {
504 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200505 nand_release_device(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200506 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200507
Wolfgang Denk932394a2005-08-17 12:55:25 +0200508 return res;
509}
510
511/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200512 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapindfe64e22013-01-14 03:46:50 +0000513 * @mtd: MTD device structure
514 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200515 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000516 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherff94bc42014-06-24 10:10:04 +0200517 * specific driver. It provides the details for writing a bad block marker to a
518 * block.
519 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200520static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
521{
William Juulcfa460a2007-10-31 13:53:06 +0100522 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200523 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +0100524 uint8_t buf[2] = { 0, 0 };
Heiko Schocherff94bc42014-06-24 10:10:04 +0200525 int ret = 0, res, i = 0;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200526
Heiko Schocherff94bc42014-06-24 10:10:04 +0200527 ops.datbuf = NULL;
528 ops.oobbuf = buf;
529 ops.ooboffs = chip->badblockpos;
530 if (chip->options & NAND_BUSWIDTH_16) {
531 ops.ooboffs &= ~0x01;
532 ops.len = ops.ooblen = 2;
533 } else {
534 ops.len = ops.ooblen = 1;
535 }
536 ops.mode = MTD_OPS_PLACE_OOB;
537
538 /* Write to first/last page(s) if necessary */
539 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
540 ofs += mtd->erasesize - mtd->writesize;
541 do {
542 res = nand_do_write_oob(mtd, ofs, &ops);
543 if (!ret)
544 ret = res;
545
546 i++;
547 ofs += mtd->writesize;
548 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
549
550 return ret;
551}
552
553/**
554 * nand_block_markbad_lowlevel - mark a block bad
555 * @mtd: MTD device structure
556 * @ofs: offset from device start
557 *
558 * This function performs the generic NAND bad block marking steps (i.e., bad
559 * block table(s) and/or marker(s)). We only allow the hardware driver to
560 * specify how to write bad block markers to OOB (chip->block_markbad).
561 *
562 * We try operations in the following order:
563 * (1) erase the affected block, to allow OOB marker to be written cleanly
564 * (2) write bad block marker to OOB area of affected block (unless flag
565 * NAND_BBT_NO_OOB_BBM is present)
566 * (3) update the BBT
567 * Note that we retain the first error encountered in (2) or (3), finish the
568 * procedures, and dump the error in the end.
569*/
570static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
571{
572 struct nand_chip *chip = mtd->priv;
573 int res, ret = 0;
574
575 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000576 struct erase_info einfo;
577
578 /* Attempt erase before marking OOB */
579 memset(&einfo, 0, sizeof(einfo));
580 einfo.mtd = mtd;
581 einfo.addr = ofs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200582 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000583 nand_erase_nand(mtd, &einfo, 0);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200584
Heiko Schocherff94bc42014-06-24 10:10:04 +0200585 /* Write bad block marker to OOB */
586 nand_get_device(mtd, FL_WRITING);
587 ret = chip->block_markbad(mtd, ofs);
Scott Woodc45912d2008-10-24 16:20:43 -0500588 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100589 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000590
Heiko Schocherff94bc42014-06-24 10:10:04 +0200591 /* Mark block bad in BBT */
592 if (chip->bbt) {
593 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000594 if (!ret)
595 ret = res;
596 }
597
William Juulcfa460a2007-10-31 13:53:06 +0100598 if (!ret)
599 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500600
William Juulcfa460a2007-10-31 13:53:06 +0100601 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200602}
603
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200604/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200605 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapindfe64e22013-01-14 03:46:50 +0000606 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200607 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000608 * Check, if the device is write protected. The function expects, that the
609 * device is already selected.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200610 */
William Juulcfa460a2007-10-31 13:53:06 +0100611static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200612{
William Juulcfa460a2007-10-31 13:53:06 +0100613 struct nand_chip *chip = mtd->priv;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200614
Sergey Lapindfe64e22013-01-14 03:46:50 +0000615 /* Broken xD cards report WP despite being writable */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200616 if (chip->options & NAND_BROKEN_XD)
617 return 0;
618
Wolfgang Denk932394a2005-08-17 12:55:25 +0200619 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100620 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
621 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200622}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100623
Wolfgang Denk932394a2005-08-17 12:55:25 +0200624/**
625 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Sergey Lapindfe64e22013-01-14 03:46:50 +0000626 * @mtd: MTD device structure
627 * @ofs: offset from device start
628 * @getchip: 0, if the chip is already selected
629 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +0200630 *
631 * Check, if the block is bad. Either by reading the bad block table or
632 * calling of the scan function.
633 */
William Juulcfa460a2007-10-31 13:53:06 +0100634static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
635 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200636{
William Juulcfa460a2007-10-31 13:53:06 +0100637 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200638
William Juulcfa460a2007-10-31 13:53:06 +0100639 if (!chip->bbt)
640 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200641
Wolfgang Denk932394a2005-08-17 12:55:25 +0200642 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100643 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200644}
645
Heiko Schocherff94bc42014-06-24 10:10:04 +0200646#ifndef __UBOOT__
647/**
648 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
649 * @mtd: MTD device structure
650 * @timeo: Timeout
651 *
652 * Helper function for nand_wait_ready used when needing to wait in interrupt
653 * context.
654 */
655static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
656{
657 struct nand_chip *chip = mtd->priv;
658 int i;
659
660 /* Wait for the device to get ready */
661 for (i = 0; i < timeo; i++) {
662 if (chip->dev_ready(mtd))
663 break;
664 touch_softlockup_watchdog();
665 mdelay(1);
666 }
667}
668#endif
669
Sergey Lapindfe64e22013-01-14 03:46:50 +0000670/* Wait for the ready pin, after a command. The timeout is caught later. */
William Juulcfa460a2007-10-31 13:53:06 +0100671void nand_wait_ready(struct mtd_info *mtd)
672{
673 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200674#ifndef __UBOOT__
675 unsigned long timeo = jiffies + msecs_to_jiffies(20);
676
677 /* 400ms timeout */
678 if (in_interrupt() || oops_in_progress)
679 return panic_nand_wait_ready(mtd, 400);
680
681 led_trigger_event(nand_led_trigger, LED_FULL);
682 /* Wait until command is processed or timeout occurs */
683 do {
684 if (chip->dev_ready(mtd))
685 break;
686 touch_softlockup_watchdog();
687 } while (time_before(jiffies, timeo));
688 led_trigger_event(nand_led_trigger, LED_OFF);
689#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200690 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000691 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100692
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000693 time_start = get_timer(0);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000694 /* Wait until command is processed or timeout occurs */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000695 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100696 if (chip->dev_ready)
697 if (chip->dev_ready(mtd))
698 break;
699 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200700#endif
William Juulcfa460a2007-10-31 13:53:06 +0100701}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200702EXPORT_SYMBOL_GPL(nand_wait_ready);
William Juulcfa460a2007-10-31 13:53:06 +0100703
Wolfgang Denk932394a2005-08-17 12:55:25 +0200704/**
705 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000706 * @mtd: MTD device structure
707 * @command: the command to be sent
708 * @column: the column address for this command, -1 if none
709 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200710 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000711 * Send command to NAND device. This function is used for small page devices
Heiko Schocherff94bc42014-06-24 10:10:04 +0200712 * (512 Bytes per page).
Wolfgang Denk932394a2005-08-17 12:55:25 +0200713 */
William Juulcfa460a2007-10-31 13:53:06 +0100714static void nand_command(struct mtd_info *mtd, unsigned int command,
715 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200716{
William Juulcfa460a2007-10-31 13:53:06 +0100717 register struct nand_chip *chip = mtd->priv;
718 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyser8da60122009-02-04 13:47:22 -0600719 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200720
Sergey Lapindfe64e22013-01-14 03:46:50 +0000721 /* Write out the command to the device */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200722 if (command == NAND_CMD_SEQIN) {
723 int readcmd;
724
William Juulcfa460a2007-10-31 13:53:06 +0100725 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200726 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100727 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200728 readcmd = NAND_CMD_READOOB;
729 } else if (column < 256) {
730 /* First 256 bytes --> READ0 */
731 readcmd = NAND_CMD_READ0;
732 } else {
733 column -= 256;
734 readcmd = NAND_CMD_READ1;
735 }
William Juulcfa460a2007-10-31 13:53:06 +0100736 chip->cmd_ctrl(mtd, readcmd, ctrl);
737 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200738 }
William Juulcfa460a2007-10-31 13:53:06 +0100739 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200740
Sergey Lapindfe64e22013-01-14 03:46:50 +0000741 /* Address cycle, when necessary */
William Juulcfa460a2007-10-31 13:53:06 +0100742 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
743 /* Serially input address */
744 if (column != -1) {
745 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200746 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530747 !nand_opcode_8bits(command))
William Juulcfa460a2007-10-31 13:53:06 +0100748 column >>= 1;
749 chip->cmd_ctrl(mtd, column, ctrl);
750 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200751 }
William Juulcfa460a2007-10-31 13:53:06 +0100752 if (page_addr != -1) {
753 chip->cmd_ctrl(mtd, page_addr, ctrl);
754 ctrl &= ~NAND_CTRL_CHANGE;
755 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
756 /* One more address cycle for devices > 32MiB */
757 if (chip->chipsize > (32 << 20))
758 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
759 }
760 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200761
762 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000763 * Program and erase have their own busy handlers status and sequential
764 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100765 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200766 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200767
Wolfgang Denk932394a2005-08-17 12:55:25 +0200768 case NAND_CMD_PAGEPROG:
769 case NAND_CMD_ERASE1:
770 case NAND_CMD_ERASE2:
771 case NAND_CMD_SEQIN:
772 case NAND_CMD_STATUS:
773 return;
774
775 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100776 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200777 break;
William Juulcfa460a2007-10-31 13:53:06 +0100778 udelay(chip->chip_delay);
779 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
780 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
781 chip->cmd_ctrl(mtd,
782 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600783 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
784 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200785 return;
786
William Juulcfa460a2007-10-31 13:53:06 +0100787 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200788 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200789 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200790 * If we don't have access to the busy pin, we apply the given
791 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100792 */
793 if (!chip->dev_ready) {
794 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200795 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200796 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200797 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000798 /*
799 * Apply this short delay always to ensure that we do wait tWB in
800 * any case on any machine.
801 */
William Juulcfa460a2007-10-31 13:53:06 +0100802 ndelay(100);
803
804 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200805}
806
807/**
808 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000809 * @mtd: MTD device structure
810 * @command: the command to be sent
811 * @column: the column address for this command, -1 if none
812 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200813 *
William Juulcfa460a2007-10-31 13:53:06 +0100814 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-01-14 03:46:50 +0000815 * devices. We don't have the separate regions as we have in the small page
816 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200817 */
William Juulcfa460a2007-10-31 13:53:06 +0100818static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
819 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200820{
William Juulcfa460a2007-10-31 13:53:06 +0100821 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600822 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200823
824 /* Emulate NAND_CMD_READOOB */
825 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100826 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200827 command = NAND_CMD_READ0;
828 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200829
William Juulcfa460a2007-10-31 13:53:06 +0100830 /* Command latch cycle */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200831 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200832
833 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100834 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200835
836 /* Serially input address */
837 if (column != -1) {
838 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200839 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530840 !nand_opcode_8bits(command))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200841 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100842 chip->cmd_ctrl(mtd, column, ctrl);
843 ctrl &= ~NAND_CTRL_CHANGE;
844 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200845 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200846 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100847 chip->cmd_ctrl(mtd, page_addr, ctrl);
848 chip->cmd_ctrl(mtd, page_addr >> 8,
849 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200850 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100851 if (chip->chipsize > (128 << 20))
852 chip->cmd_ctrl(mtd, page_addr >> 16,
853 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200854 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200855 }
William Juulcfa460a2007-10-31 13:53:06 +0100856 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200857
858 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000859 * Program and erase have their own busy handlers status, sequential
860 * in, and deplete1 need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100861 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200862 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200863
Wolfgang Denk932394a2005-08-17 12:55:25 +0200864 case NAND_CMD_CACHEDPROG:
865 case NAND_CMD_PAGEPROG:
866 case NAND_CMD_ERASE1:
867 case NAND_CMD_ERASE2:
868 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100869 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200870 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100871 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200872
873 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100874 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200875 break;
William Juulcfa460a2007-10-31 13:53:06 +0100876 udelay(chip->chip_delay);
877 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
878 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
879 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
880 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600881 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
882 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100883 return;
884
885 case NAND_CMD_RNDOUT:
886 /* No ready / busy check necessary */
887 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
888 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
889 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
890 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200891 return;
892
893 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100894 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
895 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
896 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
897 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200898
William Juulcfa460a2007-10-31 13:53:06 +0100899 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200900 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200901 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200902 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000903 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100904 */
905 if (!chip->dev_ready) {
906 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200907 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200908 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200909 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200910
Sergey Lapindfe64e22013-01-14 03:46:50 +0000911 /*
912 * Apply this short delay always to ensure that we do wait tWB in
913 * any case on any machine.
914 */
William Juulcfa460a2007-10-31 13:53:06 +0100915 ndelay(100);
916
917 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200918}
919
920/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200921 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000922 * @chip: the nand chip descriptor
923 * @mtd: MTD device structure
924 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200925 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200926 * Used when in panic, no locks are taken.
927 */
928static void panic_nand_get_device(struct nand_chip *chip,
929 struct mtd_info *mtd, int new_state)
930{
931 /* Hardware controller shared among independent devices */
932 chip->controller->active = chip;
933 chip->state = new_state;
934}
935
936/**
937 * nand_get_device - [GENERIC] Get chip for selected access
938 * @mtd: MTD device structure
939 * @new_state: the state which is requested
940 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200941 * Get the device and lock it for exclusive access
942 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200943static int
Heiko Schocherff94bc42014-06-24 10:10:04 +0200944nand_get_device(struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100945{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200946 struct nand_chip *chip = mtd->priv;
947#ifndef __UBOOT__
948 spinlock_t *lock = &chip->controller->lock;
949 wait_queue_head_t *wq = &chip->controller->wq;
950 DECLARE_WAITQUEUE(wait, current);
951retry:
952 spin_lock(lock);
953
954 /* Hardware controller shared among independent devices */
955 if (!chip->controller->active)
956 chip->controller->active = chip;
957
958 if (chip->controller->active == chip && chip->state == FL_READY) {
959 chip->state = new_state;
960 spin_unlock(lock);
961 return 0;
962 }
963 if (new_state == FL_PM_SUSPENDED) {
964 if (chip->controller->active->state == FL_PM_SUSPENDED) {
965 chip->state = FL_PM_SUSPENDED;
966 spin_unlock(lock);
967 return 0;
968 }
969 }
970 set_current_state(TASK_UNINTERRUPTIBLE);
971 add_wait_queue(wq, &wait);
972 spin_unlock(lock);
973 schedule();
974 remove_wait_queue(wq, &wait);
975 goto retry;
976#else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200977 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100978 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200979#endif
980}
981
982/**
983 * panic_nand_wait - [GENERIC] wait until the command is done
984 * @mtd: MTD device structure
985 * @chip: NAND chip structure
986 * @timeo: timeout
987 *
988 * Wait for command done. This is a helper function for nand_wait used when
989 * we are in interrupt context. May happen when in panic and trying to write
990 * an oops through mtdoops.
991 */
992static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
993 unsigned long timeo)
994{
995 int i;
996 for (i = 0; i < timeo; i++) {
997 if (chip->dev_ready) {
998 if (chip->dev_ready(mtd))
999 break;
1000 } else {
1001 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1002 break;
1003 }
1004 mdelay(1);
1005 }
William Juulcfa460a2007-10-31 13:53:06 +01001006}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001007
1008/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001009 * nand_wait - [DEFAULT] wait until the command is done
1010 * @mtd: MTD device structure
1011 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02001012 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001013 * Wait for command done. This applies to erase and program only. Erase can
1014 * take up to 400ms and program up to 20ms according to general NAND and
1015 * SmartMedia specs.
William Juulcfa460a2007-10-31 13:53:06 +01001016 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001017static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001018{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001019
Heiko Schocherff94bc42014-06-24 10:10:04 +02001020 int status, state = chip->state;
1021 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001022
Heiko Schocherff94bc42014-06-24 10:10:04 +02001023 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001024
Heiko Schocherff94bc42014-06-24 10:10:04 +02001025 /*
1026 * Apply this short delay always to ensure that we do wait tWB in any
1027 * case on any machine.
1028 */
1029 ndelay(100);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001030
Heiko Schocherff94bc42014-06-24 10:10:04 +02001031 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1032
1033#ifndef __UBOOT__
1034 if (in_interrupt() || oops_in_progress)
1035 panic_nand_wait(mtd, chip, timeo);
1036 else {
1037 timeo = jiffies + msecs_to_jiffies(timeo);
1038 while (time_before(jiffies, timeo)) {
1039 if (chip->dev_ready) {
1040 if (chip->dev_ready(mtd))
1041 break;
1042 } else {
1043 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1044 break;
1045 }
1046 cond_resched();
Stefan Roese15784862006-11-27 17:22:19 +01001047 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001048 }
1049#else
1050 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
1051 u32 time_start;
1052
1053 time_start = get_timer(0);
1054 while (get_timer(time_start) < timer) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001055 if (chip->dev_ready) {
1056 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001057 break;
1058 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001059 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001060 break;
1061 }
1062 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001063#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001064#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer7a8fc362010-11-18 03:14:26 +00001065 time_start = get_timer(0);
1066 while (get_timer(time_start) < 10)
1067 ;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001068#endif /* PPCHAMELON_NAND_TIMER_HACK */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001069 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +01001070
Heiko Schocherff94bc42014-06-24 10:10:04 +02001071 status = (int)chip->read_byte(mtd);
1072 /* This can happen if in case of timeout or buggy dev_ready */
1073 WARN_ON(!(status & NAND_STATUS_READY));
1074 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001075}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001076
Heiko Schocherff94bc42014-06-24 10:10:04 +02001077#ifndef __UBOOT__
1078/**
1079 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1080 * @mtd: mtd info
1081 * @ofs: offset to start unlock from
1082 * @len: length to unlock
1083 * @invert: when = 0, unlock the range of blocks within the lower and
1084 * upper boundary address
1085 * when = 1, unlock the range of blocks outside the boundaries
1086 * of the lower and upper boundary address
1087 *
1088 * Returs unlock status.
1089 */
1090static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1091 uint64_t len, int invert)
1092{
1093 int ret = 0;
1094 int status, page;
1095 struct nand_chip *chip = mtd->priv;
1096
1097 /* Submit address of first page to unlock */
1098 page = ofs >> chip->page_shift;
1099 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1100
1101 /* Submit address of last page to unlock */
1102 page = (ofs + len) >> chip->page_shift;
1103 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1104 (page | invert) & chip->pagemask);
1105
1106 /* Call wait ready function */
1107 status = chip->waitfunc(mtd, chip);
1108 /* See if device thinks it succeeded */
1109 if (status & NAND_STATUS_FAIL) {
1110 pr_debug("%s: error status = 0x%08x\n",
1111 __func__, status);
1112 ret = -EIO;
1113 }
1114
1115 return ret;
1116}
1117
1118/**
1119 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1120 * @mtd: mtd info
1121 * @ofs: offset to start unlock from
1122 * @len: length to unlock
1123 *
1124 * Returns unlock status.
1125 */
1126int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1127{
1128 int ret = 0;
1129 int chipnr;
1130 struct nand_chip *chip = mtd->priv;
1131
1132 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1133 __func__, (unsigned long long)ofs, len);
1134
1135 if (check_offs_len(mtd, ofs, len))
1136 ret = -EINVAL;
1137
1138 /* Align to last block address if size addresses end of the device */
1139 if (ofs + len == mtd->size)
1140 len -= mtd->erasesize;
1141
1142 nand_get_device(mtd, FL_UNLOCKING);
1143
1144 /* Shift to get chip number */
1145 chipnr = ofs >> chip->chip_shift;
1146
1147 chip->select_chip(mtd, chipnr);
1148
1149 /* Check, if it is write protected */
1150 if (nand_check_wp(mtd)) {
1151 pr_debug("%s: device is write protected!\n",
1152 __func__);
1153 ret = -EIO;
1154 goto out;
1155 }
1156
1157 ret = __nand_unlock(mtd, ofs, len, 0);
1158
1159out:
1160 chip->select_chip(mtd, -1);
1161 nand_release_device(mtd);
1162
1163 return ret;
1164}
1165EXPORT_SYMBOL(nand_unlock);
1166
1167/**
1168 * nand_lock - [REPLACEABLE] locks all blocks present in the device
1169 * @mtd: mtd info
1170 * @ofs: offset to start unlock from
1171 * @len: length to unlock
1172 *
1173 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1174 * have this feature, but it allows only to lock all blocks, not for specified
1175 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1176 * now.
1177 *
1178 * Returns lock status.
1179 */
1180int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1181{
1182 int ret = 0;
1183 int chipnr, status, page;
1184 struct nand_chip *chip = mtd->priv;
1185
1186 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1187 __func__, (unsigned long long)ofs, len);
1188
1189 if (check_offs_len(mtd, ofs, len))
1190 ret = -EINVAL;
1191
1192 nand_get_device(mtd, FL_LOCKING);
1193
1194 /* Shift to get chip number */
1195 chipnr = ofs >> chip->chip_shift;
1196
1197 chip->select_chip(mtd, chipnr);
1198
1199 /* Check, if it is write protected */
1200 if (nand_check_wp(mtd)) {
1201 pr_debug("%s: device is write protected!\n",
1202 __func__);
1203 status = MTD_ERASE_FAILED;
1204 ret = -EIO;
1205 goto out;
1206 }
1207
1208 /* Submit address of first page to lock */
1209 page = ofs >> chip->page_shift;
1210 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1211
1212 /* Call wait ready function */
1213 status = chip->waitfunc(mtd, chip);
1214 /* See if device thinks it succeeded */
1215 if (status & NAND_STATUS_FAIL) {
1216 pr_debug("%s: error status = 0x%08x\n",
1217 __func__, status);
1218 ret = -EIO;
1219 goto out;
1220 }
1221
1222 ret = __nand_unlock(mtd, ofs, len, 0x1);
1223
1224out:
1225 chip->select_chip(mtd, -1);
1226 nand_release_device(mtd);
1227
1228 return ret;
1229}
1230EXPORT_SYMBOL(nand_lock);
1231#endif
1232
Wolfgang Denk932394a2005-08-17 12:55:25 +02001233/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001234 * nand_read_page_raw - [INTERN] read raw page data without ecc
1235 * @mtd: mtd info structure
1236 * @chip: nand chip info structure
1237 * @buf: buffer to store read data
1238 * @oob_required: caller requires OOB data read to chip->oob_poi
1239 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001240 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001241 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001242 */
William Juulcfa460a2007-10-31 13:53:06 +01001243static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001244 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001245{
William Juulcfa460a2007-10-31 13:53:06 +01001246 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001247 if (oob_required)
1248 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01001249 return 0;
1250}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001251
William Juulcfa460a2007-10-31 13:53:06 +01001252/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001253 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1254 * @mtd: mtd info structure
1255 * @chip: nand chip info structure
1256 * @buf: buffer to store read data
1257 * @oob_required: caller requires OOB data read to chip->oob_poi
1258 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001259 *
1260 * We need a special oob layout and handling even when OOB isn't used.
1261 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001262static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001263 struct nand_chip *chip, uint8_t *buf,
1264 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -05001265{
1266 int eccsize = chip->ecc.size;
1267 int eccbytes = chip->ecc.bytes;
1268 uint8_t *oob = chip->oob_poi;
1269 int steps, size;
1270
1271 for (steps = chip->ecc.steps; steps > 0; steps--) {
1272 chip->read_buf(mtd, buf, eccsize);
1273 buf += eccsize;
1274
1275 if (chip->ecc.prepad) {
1276 chip->read_buf(mtd, oob, chip->ecc.prepad);
1277 oob += chip->ecc.prepad;
1278 }
1279
1280 chip->read_buf(mtd, oob, eccbytes);
1281 oob += eccbytes;
1282
1283 if (chip->ecc.postpad) {
1284 chip->read_buf(mtd, oob, chip->ecc.postpad);
1285 oob += chip->ecc.postpad;
1286 }
1287 }
1288
1289 size = mtd->oobsize - (oob - chip->oob_poi);
1290 if (size)
1291 chip->read_buf(mtd, oob, size);
1292
1293 return 0;
1294}
1295
1296/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001297 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1298 * @mtd: mtd info structure
1299 * @chip: nand chip info structure
1300 * @buf: buffer to store read data
1301 * @oob_required: caller requires OOB data read to chip->oob_poi
1302 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001303 */
1304static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001305 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001306{
1307 int i, eccsize = chip->ecc.size;
1308 int eccbytes = chip->ecc.bytes;
1309 int eccsteps = chip->ecc.steps;
1310 uint8_t *p = buf;
1311 uint8_t *ecc_calc = chip->buffers->ecccalc;
1312 uint8_t *ecc_code = chip->buffers->ecccode;
1313 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001314 unsigned int max_bitflips = 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001315
Sergey Lapindfe64e22013-01-14 03:46:50 +00001316 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001317
William Juulcfa460a2007-10-31 13:53:06 +01001318 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1319 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001320
William Juulcfa460a2007-10-31 13:53:06 +01001321 for (i = 0; i < chip->ecc.total; i++)
1322 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001323
William Juulcfa460a2007-10-31 13:53:06 +01001324 eccsteps = chip->ecc.steps;
1325 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001326
William Juulcfa460a2007-10-31 13:53:06 +01001327 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1328 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001329
William Juulcfa460a2007-10-31 13:53:06 +01001330 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001331 if (stat < 0) {
Scott Woodc45912d2008-10-24 16:20:43 -05001332 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001333 } else {
Scott Woodc45912d2008-10-24 16:20:43 -05001334 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001335 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1336 }
Scott Woodc45912d2008-10-24 16:20:43 -05001337 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001338 return max_bitflips;
Scott Woodc45912d2008-10-24 16:20:43 -05001339}
1340
1341/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001342 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapindfe64e22013-01-14 03:46:50 +00001343 * @mtd: mtd info structure
1344 * @chip: nand chip info structure
1345 * @data_offs: offset of requested data within the page
1346 * @readlen: data length
1347 * @bufpoi: buffer to store read data
Heiko Schocher4e67c572014-07-15 16:08:43 +02001348 * @page: page number to read
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,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001351 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1352 int page)
Scott Woodc45912d2008-10-24 16:20:43 -05001353{
1354 int start_step, end_step, num_steps;
1355 uint32_t *eccpos = chip->ecc.layout->eccpos;
1356 uint8_t *p;
1357 int data_col_addr, i, gaps = 0;
1358 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1359 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001360 int index;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001361 unsigned int max_bitflips = 0;
Scott Woodc45912d2008-10-24 16:20:43 -05001362
Sergey Lapindfe64e22013-01-14 03:46:50 +00001363 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -05001364 start_step = data_offs / chip->ecc.size;
1365 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1366 num_steps = end_step - start_step + 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001367 index = start_step * chip->ecc.bytes;
Scott Woodc45912d2008-10-24 16:20:43 -05001368
Sergey Lapindfe64e22013-01-14 03:46:50 +00001369 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -05001370 datafrag_len = num_steps * chip->ecc.size;
1371 eccfrag_len = num_steps * chip->ecc.bytes;
1372
1373 data_col_addr = start_step * chip->ecc.size;
1374 /* If we read not a page aligned data */
1375 if (data_col_addr != 0)
1376 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1377
1378 p = bufpoi + data_col_addr;
1379 chip->read_buf(mtd, p, datafrag_len);
1380
Sergey Lapindfe64e22013-01-14 03:46:50 +00001381 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -05001382 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1383 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1384
Sergey Lapindfe64e22013-01-14 03:46:50 +00001385 /*
1386 * The performance is faster if we position offsets according to
1387 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1388 */
Scott Woodc45912d2008-10-24 16:20:43 -05001389 for (i = 0; i < eccfrag_len - 1; i++) {
1390 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1391 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1392 gaps = 1;
1393 break;
1394 }
1395 }
1396 if (gaps) {
1397 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1398 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1399 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001400 /*
1401 * Send the command to read the particular ECC bytes take care
1402 * about buswidth alignment in read_buf.
1403 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001404 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,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001728 col, bytes, bufpoi,
1729 page);
William Juulcfa460a2007-10-31 13:53:06 +01001730 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001731 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001732 oob_required, page);
1733 if (ret < 0) {
1734 if (!aligned)
1735 /* Invalidate page cache */
1736 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001737 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001738 }
William Juulcfa460a2007-10-31 13:53:06 +01001739
Paul Burton40462e52013-09-04 15:16:56 +01001740 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1741
William Juulcfa460a2007-10-31 13:53:06 +01001742 /* Transfer not aligned data */
1743 if (!aligned) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001744 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001745 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton40462e52013-09-04 15:16:56 +01001746 (ops->mode != MTD_OPS_RAW)) {
Scott Woodc45912d2008-10-24 16:20:43 -05001747 chip->pagebuf = realpage;
Paul Burton40462e52013-09-04 15:16:56 +01001748 chip->pagebuf_bitflips = ret;
1749 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001750 /* Invalidate page cache */
1751 chip->pagebuf = -1;
Paul Burton40462e52013-09-04 15:16:56 +01001752 }
William Juulcfa460a2007-10-31 13:53:06 +01001753 memcpy(buf, chip->buffers->databuf + col, bytes);
1754 }
1755
William Juulcfa460a2007-10-31 13:53:06 +01001756 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001757 int toread = min(oobreadlen, max_oobsize);
1758
1759 if (toread) {
1760 oob = nand_transfer_oob(chip,
1761 oob, ops, toread);
1762 oobreadlen -= toread;
1763 }
William Juulcfa460a2007-10-31 13:53:06 +01001764 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001765
1766 if (chip->options & NAND_NEED_READRDY) {
1767 /* Apply delay or wait for ready/busy pin */
1768 if (!chip->dev_ready)
1769 udelay(chip->chip_delay);
1770 else
1771 nand_wait_ready(mtd);
1772 }
1773
1774 if (mtd->ecc_stats.failed - ecc_failures) {
1775 if (retry_mode + 1 < chip->read_retries) {
1776 retry_mode++;
1777 ret = nand_setup_read_retry(mtd,
1778 retry_mode);
1779 if (ret < 0)
1780 break;
1781
1782 /* Reset failures; retry */
1783 mtd->ecc_stats.failed = ecc_failures;
1784 goto read_retry;
1785 } else {
1786 /* No more retry modes; real failure */
1787 ecc_fail = true;
1788 }
1789 }
1790
1791 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001792 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001793 memcpy(buf, chip->buffers->databuf + col, bytes);
1794 buf += bytes;
Paul Burton40462e52013-09-04 15:16:56 +01001795 max_bitflips = max_t(unsigned int, max_bitflips,
1796 chip->pagebuf_bitflips);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001797 }
1798
William Juulcfa460a2007-10-31 13:53:06 +01001799 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001800
Heiko Schocherff94bc42014-06-24 10:10:04 +02001801 /* Reset to retry mode 0 */
1802 if (retry_mode) {
1803 ret = nand_setup_read_retry(mtd, 0);
1804 if (ret < 0)
1805 break;
1806 retry_mode = 0;
1807 }
1808
William Juulcfa460a2007-10-31 13:53:06 +01001809 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001810 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001811
Sergey Lapindfe64e22013-01-14 03:46:50 +00001812 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001813 col = 0;
1814 /* Increment page address */
1815 realpage++;
1816
William Juulcfa460a2007-10-31 13:53:06 +01001817 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001818 /* Check, if we cross a chip boundary */
1819 if (!page) {
1820 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001821 chip->select_chip(mtd, -1);
1822 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001823 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001824 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001825 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001826
William Juulcfa460a2007-10-31 13:53:06 +01001827 ops->retlen = ops->len - (size_t) readlen;
1828 if (oob)
1829 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001830
Heiko Schocherff94bc42014-06-24 10:10:04 +02001831 if (ret < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001832 return ret;
1833
Heiko Schocherff94bc42014-06-24 10:10:04 +02001834 if (ecc_fail)
William Juulcfa460a2007-10-31 13:53:06 +01001835 return -EBADMSG;
1836
Paul Burton40462e52013-09-04 15:16:56 +01001837 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001838}
1839
1840/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001841 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001842 * @mtd: MTD device structure
1843 * @from: offset to read from
1844 * @len: number of bytes to read
1845 * @retlen: pointer to variable to store the number of read bytes
1846 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001847 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001848 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001849 */
William Juulcfa460a2007-10-31 13:53:06 +01001850static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1851 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001852{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001853 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001854 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001855
Heiko Schocherff94bc42014-06-24 10:10:04 +02001856 nand_get_device(mtd, FL_READING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001857 ops.len = len;
1858 ops.datbuf = buf;
1859 ops.oobbuf = NULL;
1860 ops.mode = MTD_OPS_PLACE_OOB;
1861 ret = nand_do_read_ops(mtd, from, &ops);
1862 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001863 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001864 return ret;
1865}
1866
William Juulcfa460a2007-10-31 13:53:06 +01001867/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001868 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1869 * @mtd: mtd info structure
1870 * @chip: nand chip info structure
1871 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001872 */
1873static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001874 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001875{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001876 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001877 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001878 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001879}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001880
1881/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001882 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001883 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001884 * @mtd: mtd info structure
1885 * @chip: nand chip info structure
1886 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001887 */
1888static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001889 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001890{
1891 uint8_t *buf = chip->oob_poi;
1892 int length = mtd->oobsize;
1893 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1894 int eccsize = chip->ecc.size;
1895 uint8_t *bufpoi = buf;
1896 int i, toread, sndrnd = 0, pos;
1897
1898 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1899 for (i = 0; i < chip->ecc.steps; i++) {
1900 if (sndrnd) {
1901 pos = eccsize + i * (eccsize + chunk);
1902 if (mtd->writesize > 512)
1903 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1904 else
1905 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1906 } else
1907 sndrnd = 1;
1908 toread = min_t(int, length, chunk);
1909 chip->read_buf(mtd, bufpoi, toread);
1910 bufpoi += toread;
1911 length -= toread;
1912 }
1913 if (length > 0)
1914 chip->read_buf(mtd, bufpoi, length);
1915
Sergey Lapindfe64e22013-01-14 03:46:50 +00001916 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001917}
1918
1919/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001920 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1921 * @mtd: mtd info structure
1922 * @chip: nand chip info structure
1923 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001924 */
1925static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1926 int page)
1927{
1928 int status = 0;
1929 const uint8_t *buf = chip->oob_poi;
1930 int length = mtd->oobsize;
1931
1932 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1933 chip->write_buf(mtd, buf, length);
1934 /* Send command to program the OOB data */
1935 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1936
1937 status = chip->waitfunc(mtd, chip);
1938
1939 return status & NAND_STATUS_FAIL ? -EIO : 0;
1940}
1941
1942/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001943 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1944 * with syndrome - only for large page flash
1945 * @mtd: mtd info structure
1946 * @chip: nand chip info structure
1947 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001948 */
1949static int nand_write_oob_syndrome(struct mtd_info *mtd,
1950 struct nand_chip *chip, int page)
1951{
1952 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1953 int eccsize = chip->ecc.size, length = mtd->oobsize;
1954 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1955 const uint8_t *bufpoi = chip->oob_poi;
1956
1957 /*
1958 * data-ecc-data-ecc ... ecc-oob
1959 * or
1960 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1961 */
1962 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1963 pos = steps * (eccsize + chunk);
1964 steps = 0;
1965 } else
1966 pos = eccsize;
1967
1968 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1969 for (i = 0; i < steps; i++) {
1970 if (sndcmd) {
1971 if (mtd->writesize <= 512) {
1972 uint32_t fill = 0xFFFFFFFF;
1973
1974 len = eccsize;
1975 while (len > 0) {
1976 int num = min_t(int, len, 4);
1977 chip->write_buf(mtd, (uint8_t *)&fill,
1978 num);
1979 len -= num;
1980 }
1981 } else {
1982 pos = eccsize + i * (eccsize + chunk);
1983 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1984 }
1985 } else
1986 sndcmd = 1;
1987 len = min_t(int, length, chunk);
1988 chip->write_buf(mtd, bufpoi, len);
1989 bufpoi += len;
1990 length -= len;
1991 }
1992 if (length > 0)
1993 chip->write_buf(mtd, bufpoi, length);
1994
1995 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1996 status = chip->waitfunc(mtd, chip);
1997
1998 return status & NAND_STATUS_FAIL ? -EIO : 0;
1999}
2000
2001/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002002 * nand_do_read_oob - [INTERN] NAND read out-of-band
2003 * @mtd: MTD device structure
2004 * @from: offset to read from
2005 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01002006 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002007 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01002008 */
2009static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2010 struct mtd_oob_ops *ops)
2011{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002012 int page, realpage, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002013 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002014 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01002015 int readlen = ops->ooblen;
2016 int len;
2017 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002018 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002019
Heiko Schocherff94bc42014-06-24 10:10:04 +02002020 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02002021 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01002022
Sergey Lapindfe64e22013-01-14 03:46:50 +00002023 stats = mtd->ecc_stats;
2024
2025 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002026 len = chip->ecc.layout->oobavail;
2027 else
2028 len = mtd->oobsize;
2029
2030 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002031 pr_debug("%s: attempt to start read outside oob\n",
2032 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002033 return -EINVAL;
2034 }
2035
2036 /* Do not allow reads past end of device */
2037 if (unlikely(from >= mtd->size ||
2038 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2039 (from >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002040 pr_debug("%s: attempt to read beyond end of device\n",
2041 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002042 return -EINVAL;
2043 }
2044
2045 chipnr = (int)(from >> chip->chip_shift);
2046 chip->select_chip(mtd, chipnr);
2047
2048 /* Shift to get page */
2049 realpage = (int)(from >> chip->page_shift);
2050 page = realpage & chip->pagemask;
2051
Christian Hitz90e3f392011-10-12 09:32:01 +02002052 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002053 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002054
Sergey Lapindfe64e22013-01-14 03:46:50 +00002055 if (ops->mode == MTD_OPS_RAW)
2056 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2057 else
2058 ret = chip->ecc.read_oob(mtd, chip, page);
2059
2060 if (ret < 0)
2061 break;
William Juulcfa460a2007-10-31 13:53:06 +01002062
2063 len = min(len, readlen);
2064 buf = nand_transfer_oob(chip, buf, ops, len);
2065
Heiko Schocherff94bc42014-06-24 10:10:04 +02002066 if (chip->options & NAND_NEED_READRDY) {
2067 /* Apply delay or wait for ready/busy pin */
2068 if (!chip->dev_ready)
2069 udelay(chip->chip_delay);
2070 else
2071 nand_wait_ready(mtd);
2072 }
2073
William Juulcfa460a2007-10-31 13:53:06 +01002074 readlen -= len;
2075 if (!readlen)
2076 break;
2077
2078 /* Increment page address */
2079 realpage++;
2080
2081 page = realpage & chip->pagemask;
2082 /* Check, if we cross a chip boundary */
2083 if (!page) {
2084 chipnr++;
2085 chip->select_chip(mtd, -1);
2086 chip->select_chip(mtd, chipnr);
2087 }
William Juulcfa460a2007-10-31 13:53:06 +01002088 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002089 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002090
Sergey Lapindfe64e22013-01-14 03:46:50 +00002091 ops->oobretlen = ops->ooblen - readlen;
2092
2093 if (ret < 0)
2094 return ret;
2095
2096 if (mtd->ecc_stats.failed - stats.failed)
2097 return -EBADMSG;
2098
2099 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002100}
2101
2102/**
2103 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002104 * @mtd: MTD device structure
2105 * @from: offset to read from
2106 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002107 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002108 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01002109 */
2110static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2111 struct mtd_oob_ops *ops)
2112{
William Juulcfa460a2007-10-31 13:53:06 +01002113 int ret = -ENOTSUPP;
2114
2115 ops->retlen = 0;
2116
2117 /* Do not allow reads past end of device */
2118 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002119 pr_debug("%s: attempt to read beyond end of device\n",
2120 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002121 return -EINVAL;
2122 }
2123
Heiko Schocherff94bc42014-06-24 10:10:04 +02002124 nand_get_device(mtd, FL_READING);
William Juulcfa460a2007-10-31 13:53:06 +01002125
Christian Hitz90e3f392011-10-12 09:32:01 +02002126 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002127 case MTD_OPS_PLACE_OOB:
2128 case MTD_OPS_AUTO_OOB:
2129 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002130 break;
2131
2132 default:
2133 goto out;
2134 }
2135
2136 if (!ops->datbuf)
2137 ret = nand_do_read_oob(mtd, from, ops);
2138 else
2139 ret = nand_do_read_ops(mtd, from, ops);
2140
Christian Hitz90e3f392011-10-12 09:32:01 +02002141out:
William Juulcfa460a2007-10-31 13:53:06 +01002142 nand_release_device(mtd);
2143 return ret;
2144}
2145
2146
2147/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002148 * nand_write_page_raw - [INTERN] raw page write function
2149 * @mtd: mtd info structure
2150 * @chip: nand chip info structure
2151 * @buf: data buffer
2152 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05002153 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002154 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01002155 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002156static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2157 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002158{
2159 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002160 if (oob_required)
2161 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2162
2163 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002164}
2165
2166/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002167 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2168 * @mtd: mtd info structure
2169 * @chip: nand chip info structure
2170 * @buf: data buffer
2171 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05002172 *
2173 * We need a special oob layout and handling even when ECC isn't checked.
2174 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002175static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02002176 struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00002177 const uint8_t *buf, int oob_required)
David Brownell7e866612009-11-07 16:27:01 -05002178{
2179 int eccsize = chip->ecc.size;
2180 int eccbytes = chip->ecc.bytes;
2181 uint8_t *oob = chip->oob_poi;
2182 int steps, size;
2183
2184 for (steps = chip->ecc.steps; steps > 0; steps--) {
2185 chip->write_buf(mtd, buf, eccsize);
2186 buf += eccsize;
2187
2188 if (chip->ecc.prepad) {
2189 chip->write_buf(mtd, oob, chip->ecc.prepad);
2190 oob += chip->ecc.prepad;
2191 }
2192
Heiko Schocher4e67c572014-07-15 16:08:43 +02002193 chip->write_buf(mtd, oob, eccbytes);
David Brownell7e866612009-11-07 16:27:01 -05002194 oob += eccbytes;
2195
2196 if (chip->ecc.postpad) {
2197 chip->write_buf(mtd, oob, chip->ecc.postpad);
2198 oob += chip->ecc.postpad;
2199 }
2200 }
2201
2202 size = mtd->oobsize - (oob - chip->oob_poi);
2203 if (size)
2204 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002205
2206 return 0;
David Brownell7e866612009-11-07 16:27:01 -05002207}
2208/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002209 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2210 * @mtd: mtd info structure
2211 * @chip: nand chip info structure
2212 * @buf: data buffer
2213 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002214 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002215static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2216 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002217{
2218 int i, eccsize = chip->ecc.size;
2219 int eccbytes = chip->ecc.bytes;
2220 int eccsteps = chip->ecc.steps;
2221 uint8_t *ecc_calc = chip->buffers->ecccalc;
2222 const uint8_t *p = buf;
2223 uint32_t *eccpos = chip->ecc.layout->eccpos;
2224
Sergey Lapindfe64e22013-01-14 03:46:50 +00002225 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01002226 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2227 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2228
2229 for (i = 0; i < chip->ecc.total; i++)
2230 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2231
Sergey Lapindfe64e22013-01-14 03:46:50 +00002232 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
William Juulcfa460a2007-10-31 13:53:06 +01002233}
2234
2235/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002236 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2237 * @mtd: mtd info structure
2238 * @chip: nand chip info structure
2239 * @buf: data buffer
2240 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002241 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002242static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2243 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002244{
2245 int i, eccsize = chip->ecc.size;
2246 int eccbytes = chip->ecc.bytes;
2247 int eccsteps = chip->ecc.steps;
2248 uint8_t *ecc_calc = chip->buffers->ecccalc;
2249 const uint8_t *p = buf;
2250 uint32_t *eccpos = chip->ecc.layout->eccpos;
2251
2252 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2253 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2254 chip->write_buf(mtd, p, eccsize);
2255 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2256 }
2257
2258 for (i = 0; i < chip->ecc.total; i++)
2259 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2260
2261 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002262
2263 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002264}
2265
Heiko Schocherff94bc42014-06-24 10:10:04 +02002266
2267/**
2268 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2269 * @mtd: mtd info structure
2270 * @chip: nand chip info structure
2271 * @offset: column address of subpage within the page
2272 * @data_len: data length
2273 * @buf: data buffer
2274 * @oob_required: must write chip->oob_poi to OOB
2275 */
2276static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2277 struct nand_chip *chip, uint32_t offset,
2278 uint32_t data_len, const uint8_t *buf,
2279 int oob_required)
2280{
2281 uint8_t *oob_buf = chip->oob_poi;
2282 uint8_t *ecc_calc = chip->buffers->ecccalc;
2283 int ecc_size = chip->ecc.size;
2284 int ecc_bytes = chip->ecc.bytes;
2285 int ecc_steps = chip->ecc.steps;
2286 uint32_t *eccpos = chip->ecc.layout->eccpos;
2287 uint32_t start_step = offset / ecc_size;
2288 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2289 int oob_bytes = mtd->oobsize / ecc_steps;
2290 int step, i;
2291
2292 for (step = 0; step < ecc_steps; step++) {
2293 /* configure controller for WRITE access */
2294 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2295
2296 /* write data (untouched subpages already masked by 0xFF) */
2297 chip->write_buf(mtd, buf, ecc_size);
2298
2299 /* mask ECC of un-touched subpages by padding 0xFF */
2300 if ((step < start_step) || (step > end_step))
2301 memset(ecc_calc, 0xff, ecc_bytes);
2302 else
2303 chip->ecc.calculate(mtd, buf, ecc_calc);
2304
2305 /* mask OOB of un-touched subpages by padding 0xFF */
2306 /* if oob_required, preserve OOB metadata of written subpage */
2307 if (!oob_required || (step < start_step) || (step > end_step))
2308 memset(oob_buf, 0xff, oob_bytes);
2309
2310 buf += ecc_size;
2311 ecc_calc += ecc_bytes;
2312 oob_buf += oob_bytes;
2313 }
2314
2315 /* copy calculated ECC for whole page to chip->buffer->oob */
2316 /* this include masked-value(0xFF) for unwritten subpages */
2317 ecc_calc = chip->buffers->ecccalc;
2318 for (i = 0; i < chip->ecc.total; i++)
2319 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2320
2321 /* write OOB buffer to NAND device */
2322 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2323
2324 return 0;
2325}
2326
2327
William Juulcfa460a2007-10-31 13:53:06 +01002328/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002329 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2330 * @mtd: mtd info structure
2331 * @chip: nand chip info structure
2332 * @buf: data buffer
2333 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002334 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002335 * The hw generator calculates the error syndrome automatically. Therefore we
2336 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01002337 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002338static int nand_write_page_syndrome(struct mtd_info *mtd,
2339 struct nand_chip *chip,
2340 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002341{
2342 int i, eccsize = chip->ecc.size;
2343 int eccbytes = chip->ecc.bytes;
2344 int eccsteps = chip->ecc.steps;
2345 const uint8_t *p = buf;
2346 uint8_t *oob = chip->oob_poi;
2347
2348 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2349
2350 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2351 chip->write_buf(mtd, p, eccsize);
2352
2353 if (chip->ecc.prepad) {
2354 chip->write_buf(mtd, oob, chip->ecc.prepad);
2355 oob += chip->ecc.prepad;
2356 }
2357
2358 chip->ecc.calculate(mtd, p, oob);
2359 chip->write_buf(mtd, oob, eccbytes);
2360 oob += eccbytes;
2361
2362 if (chip->ecc.postpad) {
2363 chip->write_buf(mtd, oob, chip->ecc.postpad);
2364 oob += chip->ecc.postpad;
2365 }
2366 }
2367
2368 /* Calculate remaining oob bytes */
2369 i = mtd->oobsize - (oob - chip->oob_poi);
2370 if (i)
2371 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002372
2373 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002374}
2375
2376/**
2377 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00002378 * @mtd: MTD device structure
2379 * @chip: NAND chip descriptor
Heiko Schocherff94bc42014-06-24 10:10:04 +02002380 * @offset: address offset within the page
2381 * @data_len: length of actual data to be written
Sergey Lapindfe64e22013-01-14 03:46:50 +00002382 * @buf: the data to write
2383 * @oob_required: must write chip->oob_poi to OOB
2384 * @page: page number to write
2385 * @cached: cached programming
2386 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01002387 */
2388static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02002389 uint32_t offset, int data_len, const uint8_t *buf,
2390 int oob_required, int page, int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01002391{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002392 int status, subpage;
2393
2394 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2395 chip->ecc.write_subpage)
2396 subpage = offset || (data_len < mtd->writesize);
2397 else
2398 subpage = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002399
2400 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2401
2402 if (unlikely(raw))
Heiko Schocherff94bc42014-06-24 10:10:04 +02002403 status = chip->ecc.write_page_raw(mtd, chip, buf,
2404 oob_required);
2405 else if (subpage)
2406 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2407 buf, oob_required);
William Juulcfa460a2007-10-31 13:53:06 +01002408 else
Sergey Lapindfe64e22013-01-14 03:46:50 +00002409 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2410
2411 if (status < 0)
2412 return status;
William Juulcfa460a2007-10-31 13:53:06 +01002413
2414 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002415 * Cached progamming disabled for now. Not sure if it's worth the
2416 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01002417 */
2418 cached = 0;
2419
Heiko Schocherff94bc42014-06-24 10:10:04 +02002420 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
William Juulcfa460a2007-10-31 13:53:06 +01002421
2422 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2423 status = chip->waitfunc(mtd, chip);
2424 /*
2425 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00002426 * available.
William Juulcfa460a2007-10-31 13:53:06 +01002427 */
2428 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2429 status = chip->errstat(mtd, chip, FL_WRITING, status,
2430 page);
2431
2432 if (status & NAND_STATUS_FAIL)
2433 return -EIO;
2434 } else {
2435 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2436 status = chip->waitfunc(mtd, chip);
2437 }
2438
Heiko Schocherff94bc42014-06-24 10:10:04 +02002439
2440#ifdef __UBOOT__
2441#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
William Juulcfa460a2007-10-31 13:53:06 +01002442 /* Send command to read back the data */
2443 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2444
2445 if (chip->verify_buf(mtd, buf, mtd->writesize))
2446 return -EIO;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002447
2448 /* Make sure the next page prog is preceded by a status read */
2449 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002450#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +02002451#endif
2452
William Juulcfa460a2007-10-31 13:53:06 +01002453 return 0;
2454}
2455
2456/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002457 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2458 * @mtd: MTD device structure
2459 * @oob: oob data buffer
2460 * @len: oob data write length
2461 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01002462 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002463static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2464 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01002465{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002466 struct nand_chip *chip = mtd->priv;
2467
2468 /*
2469 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2470 * data from a previous OOB read.
2471 */
2472 memset(chip->oob_poi, 0xff, mtd->oobsize);
2473
Christian Hitz90e3f392011-10-12 09:32:01 +02002474 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01002475
Sergey Lapindfe64e22013-01-14 03:46:50 +00002476 case MTD_OPS_PLACE_OOB:
2477 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002478 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2479 return oob + len;
2480
Sergey Lapindfe64e22013-01-14 03:46:50 +00002481 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01002482 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2483 uint32_t boffs = 0, woffs = ops->ooboffs;
2484 size_t bytes = 0;
2485
Christian Hitz90e3f392011-10-12 09:32:01 +02002486 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002487 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01002488 if (unlikely(woffs)) {
2489 if (woffs >= free->length) {
2490 woffs -= free->length;
2491 continue;
2492 }
2493 boffs = free->offset + woffs;
2494 bytes = min_t(size_t, len,
2495 (free->length - woffs));
2496 woffs = 0;
2497 } else {
2498 bytes = min_t(size_t, len, free->length);
2499 boffs = free->offset;
2500 }
2501 memcpy(chip->oob_poi + boffs, oob, bytes);
2502 oob += bytes;
2503 }
2504 return oob;
2505 }
2506 default:
2507 BUG();
2508 }
2509 return NULL;
2510}
2511
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002512#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01002513
2514/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002515 * nand_do_write_ops - [INTERN] NAND write with ECC
2516 * @mtd: MTD device structure
2517 * @to: offset to write to
2518 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01002519 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002520 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002521 */
2522static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2523 struct mtd_oob_ops *ops)
2524{
2525 int chipnr, realpage, page, blockmask, column;
2526 struct nand_chip *chip = mtd->priv;
2527 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002528
2529 uint32_t oobwritelen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002530 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002531 mtd->oobavail : mtd->oobsize;
2532
William Juulcfa460a2007-10-31 13:53:06 +01002533 uint8_t *oob = ops->oobbuf;
2534 uint8_t *buf = ops->datbuf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002535 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002536 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002537
2538 ops->retlen = 0;
2539 if (!writelen)
2540 return 0;
2541
Heiko Schocherff94bc42014-06-24 10:10:04 +02002542#ifndef __UBOOT__
2543 /* Reject writes, which are not page aligned */
2544 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2545#else
2546 /* Reject writes, which are not page aligned */
2547 if (NOTALIGNED(to)) {
2548#endif
2549 pr_notice("%s: attempt to write non page aligned data\n",
2550 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002551 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002552 }
2553
2554 column = to & (mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +01002555
2556 chipnr = (int)(to >> chip->chip_shift);
2557 chip->select_chip(mtd, chipnr);
2558
2559 /* Check, if it is write protected */
2560 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002561 ret = -EIO;
2562 goto err_out;
William Juulcfa460a2007-10-31 13:53:06 +01002563 }
2564
2565 realpage = (int)(to >> chip->page_shift);
2566 page = realpage & chip->pagemask;
2567 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2568
2569 /* Invalidate the page cache, when we write to the cached page */
2570 if (to <= (chip->pagebuf << chip->page_shift) &&
2571 (chip->pagebuf << chip->page_shift) < (to + ops->len))
2572 chip->pagebuf = -1;
2573
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002574 /* Don't allow multipage oob writes with offset */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002575 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2576 ret = -EINVAL;
2577 goto err_out;
2578 }
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002579
Christian Hitz90e3f392011-10-12 09:32:01 +02002580 while (1) {
William Juulcfa460a2007-10-31 13:53:06 +01002581 int bytes = mtd->writesize;
2582 int cached = writelen > bytes && page != blockmask;
2583 uint8_t *wbuf = buf;
2584
Heiko Schocherff94bc42014-06-24 10:10:04 +02002585 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00002586 /* Partial page write? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002587 if (unlikely(column || writelen < (mtd->writesize - 1))) {
William Juulcfa460a2007-10-31 13:53:06 +01002588 cached = 0;
2589 bytes = min_t(int, bytes - column, (int) writelen);
2590 chip->pagebuf = -1;
2591 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2592 memcpy(&chip->buffers->databuf[column], buf, bytes);
2593 wbuf = chip->buffers->databuf;
2594 }
2595
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002596 if (unlikely(oob)) {
2597 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002598 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002599 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002600 } else {
2601 /* We still need to erase leftover OOB data */
2602 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002603 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002604 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2605 oob_required, page, cached,
2606 (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002607 if (ret)
2608 break;
2609
2610 writelen -= bytes;
2611 if (!writelen)
2612 break;
2613
2614 column = 0;
2615 buf += bytes;
2616 realpage++;
2617
2618 page = realpage & chip->pagemask;
2619 /* Check, if we cross a chip boundary */
2620 if (!page) {
2621 chipnr++;
2622 chip->select_chip(mtd, -1);
2623 chip->select_chip(mtd, chipnr);
2624 }
2625 }
2626
2627 ops->retlen = ops->len - writelen;
2628 if (unlikely(oob))
2629 ops->oobretlen = ops->ooblen;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002630
2631err_out:
2632 chip->select_chip(mtd, -1);
2633 return ret;
2634}
2635
2636/**
2637 * panic_nand_write - [MTD Interface] NAND write with ECC
2638 * @mtd: MTD device structure
2639 * @to: offset to write to
2640 * @len: number of bytes to write
2641 * @retlen: pointer to variable to store the number of written bytes
2642 * @buf: the data to write
2643 *
2644 * NAND write with ECC. Used when performing writes in interrupt context, this
2645 * may for example be called by mtdoops when writing an oops while in panic.
2646 */
2647static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2648 size_t *retlen, const uint8_t *buf)
2649{
2650 struct nand_chip *chip = mtd->priv;
2651 struct mtd_oob_ops ops;
2652 int ret;
2653
2654 /* Wait for the device to get ready */
2655 panic_nand_wait(mtd, chip, 400);
2656
2657 /* Grab the device */
2658 panic_nand_get_device(chip, mtd, FL_WRITING);
2659
2660 ops.len = len;
2661 ops.datbuf = (uint8_t *)buf;
2662 ops.oobbuf = NULL;
2663 ops.mode = MTD_OPS_PLACE_OOB;
2664
2665 ret = nand_do_write_ops(mtd, to, &ops);
2666
2667 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002668 return ret;
2669}
2670
2671/**
2672 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002673 * @mtd: MTD device structure
2674 * @to: offset to write to
2675 * @len: number of bytes to write
2676 * @retlen: pointer to variable to store the number of written bytes
2677 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002678 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002679 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002680 */
2681static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2682 size_t *retlen, const uint8_t *buf)
2683{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002684 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002685 int ret;
2686
Heiko Schocherff94bc42014-06-24 10:10:04 +02002687 nand_get_device(mtd, FL_WRITING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002688 ops.len = len;
2689 ops.datbuf = (uint8_t *)buf;
2690 ops.oobbuf = NULL;
2691 ops.mode = MTD_OPS_PLACE_OOB;
2692 ret = nand_do_write_ops(mtd, to, &ops);
2693 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002694 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002695 return ret;
2696}
2697
2698/**
2699 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002700 * @mtd: MTD device structure
2701 * @to: offset to write to
2702 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002703 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002704 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002705 */
William Juulcfa460a2007-10-31 13:53:06 +01002706static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2707 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002708{
William Juulcfa460a2007-10-31 13:53:06 +01002709 int chipnr, page, status, len;
2710 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002711
Heiko Schocherff94bc42014-06-24 10:10:04 +02002712 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02002713 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002714
Sergey Lapindfe64e22013-01-14 03:46:50 +00002715 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002716 len = chip->ecc.layout->oobavail;
2717 else
2718 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002719
2720 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002721 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002722 pr_debug("%s: attempt to write past end of page\n",
2723 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002724 return -EINVAL;
2725 }
2726
William Juulcfa460a2007-10-31 13:53:06 +01002727 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002728 pr_debug("%s: attempt to start write outside oob\n",
2729 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002730 return -EINVAL;
2731 }
2732
Christian Hitz90e3f392011-10-12 09:32:01 +02002733 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002734 if (unlikely(to >= mtd->size ||
2735 ops->ooboffs + ops->ooblen >
2736 ((mtd->size >> chip->page_shift) -
2737 (to >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002738 pr_debug("%s: attempt to write beyond end of device\n",
2739 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002740 return -EINVAL;
2741 }
2742
William Juulcfa460a2007-10-31 13:53:06 +01002743 chipnr = (int)(to >> chip->chip_shift);
2744 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002745
William Juulcfa460a2007-10-31 13:53:06 +01002746 /* Shift to get page */
2747 page = (int)(to >> chip->page_shift);
2748
2749 /*
2750 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2751 * of my DiskOnChip 2000 test units) will clear the whole data page too
2752 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2753 * it in the doc2000 driver in August 1999. dwmw2.
2754 */
2755 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002756
2757 /* Check, if it is write protected */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002758 if (nand_check_wp(mtd)) {
2759 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002760 return -EROFS;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002761 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002762
Wolfgang Denk932394a2005-08-17 12:55:25 +02002763 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002764 if (page == chip->pagebuf)
2765 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002766
Sergey Lapindfe64e22013-01-14 03:46:50 +00002767 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2768
2769 if (ops->mode == MTD_OPS_RAW)
2770 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2771 else
2772 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002773
Heiko Schocherff94bc42014-06-24 10:10:04 +02002774 chip->select_chip(mtd, -1);
2775
William Juulcfa460a2007-10-31 13:53:06 +01002776 if (status)
2777 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002778
William Juulcfa460a2007-10-31 13:53:06 +01002779 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002780
William Juulcfa460a2007-10-31 13:53:06 +01002781 return 0;
2782}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002783
William Juulcfa460a2007-10-31 13:53:06 +01002784/**
2785 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002786 * @mtd: MTD device structure
2787 * @to: offset to write to
2788 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002789 */
2790static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2791 struct mtd_oob_ops *ops)
2792{
William Juulcfa460a2007-10-31 13:53:06 +01002793 int ret = -ENOTSUPP;
2794
2795 ops->retlen = 0;
2796
2797 /* Do not allow writes past end of device */
2798 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002799 pr_debug("%s: attempt to write beyond end of device\n",
2800 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002801 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002802 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002803
Heiko Schocherff94bc42014-06-24 10:10:04 +02002804 nand_get_device(mtd, FL_WRITING);
William Juulcfa460a2007-10-31 13:53:06 +01002805
Christian Hitz90e3f392011-10-12 09:32:01 +02002806 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002807 case MTD_OPS_PLACE_OOB:
2808 case MTD_OPS_AUTO_OOB:
2809 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002810 break;
2811
2812 default:
2813 goto out;
2814 }
2815
2816 if (!ops->datbuf)
2817 ret = nand_do_write_oob(mtd, to, ops);
2818 else
2819 ret = nand_do_write_ops(mtd, to, ops);
2820
Christian Hitz90e3f392011-10-12 09:32:01 +02002821out:
William Juulcfa460a2007-10-31 13:53:06 +01002822 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002823 return ret;
2824}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002825
2826/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002827 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2828 * @mtd: MTD device structure
2829 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002830 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002831 * Standard erase command for NAND chips.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002832 */
William Juulcfa460a2007-10-31 13:53:06 +01002833static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002834{
William Juulcfa460a2007-10-31 13:53:06 +01002835 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002836 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002837 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2838 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002839}
2840
2841/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002842 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002843 * @mtd: MTD device structure
2844 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002845 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002846 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002847 */
William Juulcfa460a2007-10-31 13:53:06 +01002848static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002849{
William Juulcfa460a2007-10-31 13:53:06 +01002850 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002851}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002852
Wolfgang Denk932394a2005-08-17 12:55:25 +02002853/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002854 * nand_erase_nand - [INTERN] erase block(s)
2855 * @mtd: MTD device structure
2856 * @instr: erase instruction
2857 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002858 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002859 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002860 */
William Juulcfa460a2007-10-31 13:53:06 +01002861int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2862 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002863{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002864 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002865 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002866 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002867
Heiko Schocherff94bc42014-06-24 10:10:04 +02002868 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2869 __func__, (unsigned long long)instr->addr,
2870 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002871
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002872 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002873 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002874
Wolfgang Denk932394a2005-08-17 12:55:25 +02002875 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002876 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002877
2878 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002879 page = (int)(instr->addr >> chip->page_shift);
2880 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002881
2882 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002883 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002884
Wolfgang Denk932394a2005-08-17 12:55:25 +02002885 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002886 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002887
Wolfgang Denk932394a2005-08-17 12:55:25 +02002888 /* Check, if it is write protected */
2889 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002890 pr_debug("%s: device is write protected!\n",
2891 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002892 instr->state = MTD_ERASE_FAILED;
2893 goto erase_exit;
2894 }
2895
2896 /* Loop through the pages */
2897 len = instr->len;
2898
2899 instr->state = MTD_ERASING;
2900
2901 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002902 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002903
Sergey Lapindfe64e22013-01-14 03:46:50 +00002904 /* Check if we have a bad block, we do not erase bad blocks! */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002905 if (nand_block_checkbad(mtd, ((loff_t) page) <<
William Juulcfa460a2007-10-31 13:53:06 +01002906 chip->page_shift, 0, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002907 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02002908 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002909 instr->state = MTD_ERASE_FAILED;
2910 goto erase_exit;
2911 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002912
William Juulcfa460a2007-10-31 13:53:06 +01002913 /*
2914 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002915 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002916 */
2917 if (page <= chip->pagebuf && chip->pagebuf <
2918 (page + pages_per_block))
2919 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002920
William Juulcfa460a2007-10-31 13:53:06 +01002921 chip->erase_cmd(mtd, page & chip->pagemask);
2922
2923 status = chip->waitfunc(mtd, chip);
2924
2925 /*
2926 * See if operation failed and additional status checks are
2927 * available
2928 */
2929 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2930 status = chip->errstat(mtd, chip, FL_ERASING,
2931 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002932
2933 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002934 if (status & NAND_STATUS_FAIL) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002935 pr_debug("%s: failed erase, page 0x%08x\n",
2936 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002937 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002938 instr->fail_addr =
2939 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002940 goto erase_exit;
2941 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002942
Wolfgang Denk932394a2005-08-17 12:55:25 +02002943 /* Increment page address and decrement length */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002944 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002945 page += pages_per_block;
2946
2947 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002948 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002949 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002950 chip->select_chip(mtd, -1);
2951 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002952 }
2953 }
2954 instr->state = MTD_ERASE_DONE;
2955
Christian Hitz90e3f392011-10-12 09:32:01 +02002956erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002957
2958 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002959
2960 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002961 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002962 nand_release_device(mtd);
2963
Scott Woodc45912d2008-10-24 16:20:43 -05002964 /* Do call back function */
2965 if (!ret)
2966 mtd_erase_callback(instr);
2967
Wolfgang Denk932394a2005-08-17 12:55:25 +02002968 /* Return more or less happy */
2969 return ret;
2970}
2971
2972/**
2973 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002974 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002975 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002976 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002977 */
William Juulcfa460a2007-10-31 13:53:06 +01002978static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002979{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002980 pr_debug("%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002981
2982 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002983 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002984 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002985 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002986}
2987
Wolfgang Denk932394a2005-08-17 12:55:25 +02002988/**
William Juulcfa460a2007-10-31 13:53:06 +01002989 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002990 * @mtd: MTD device structure
2991 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002992 */
William Juulcfa460a2007-10-31 13:53:06 +01002993static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002994{
William Juulcfa460a2007-10-31 13:53:06 +01002995 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002996}
2997
2998/**
William Juulcfa460a2007-10-31 13:53:06 +01002999 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00003000 * @mtd: MTD device structure
3001 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02003002 */
William Juulcfa460a2007-10-31 13:53:06 +01003003static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003004{
Wolfgang Denk932394a2005-08-17 12:55:25 +02003005 int ret;
3006
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003007 ret = nand_block_isbad(mtd, ofs);
3008 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003009 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02003010 if (ret > 0)
3011 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02003012 return ret;
3013 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02003014
Heiko Schocherff94bc42014-06-24 10:10:04 +02003015 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003016}
3017
Heiko Schocherff94bc42014-06-24 10:10:04 +02003018/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00003019 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3020 * @mtd: MTD device structure
3021 * @chip: nand chip info structure
3022 * @addr: feature address.
3023 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01003024 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00003025static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3026 int addr, uint8_t *subfeature_param)
3027{
3028 int status;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003029 int i;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003030
Heiko Schocherff94bc42014-06-24 10:10:04 +02003031#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3032 if (!chip->onfi_version ||
3033 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3034 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00003035 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003036#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003037
3038 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003039 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3040 chip->write_byte(mtd, subfeature_param[i]);
3041
Sergey Lapindfe64e22013-01-14 03:46:50 +00003042 status = chip->waitfunc(mtd, chip);
3043 if (status & NAND_STATUS_FAIL)
3044 return -EIO;
3045 return 0;
3046}
3047
3048/**
3049 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3050 * @mtd: MTD device structure
3051 * @chip: nand chip info structure
3052 * @addr: feature address.
3053 * @subfeature_param: the subfeature parameters, a four bytes array.
3054 */
3055static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3056 int addr, uint8_t *subfeature_param)
3057{
Heiko Schocherff94bc42014-06-24 10:10:04 +02003058 int i;
3059
3060#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3061 if (!chip->onfi_version ||
3062 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3063 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00003064 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003065#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003066
3067 /* clear the sub feature parameters */
3068 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
3069
3070 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003071 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3072 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003073 return 0;
3074}
3075
Heiko Schocherff94bc42014-06-24 10:10:04 +02003076#ifndef __UBOOT__
3077/**
3078 * nand_suspend - [MTD Interface] Suspend the NAND flash
3079 * @mtd: MTD device structure
3080 */
3081static int nand_suspend(struct mtd_info *mtd)
3082{
3083 return nand_get_device(mtd, FL_PM_SUSPENDED);
3084}
3085
3086/**
3087 * nand_resume - [MTD Interface] Resume the NAND flash
3088 * @mtd: MTD device structure
3089 */
3090static void nand_resume(struct mtd_info *mtd)
3091{
3092 struct nand_chip *chip = mtd->priv;
3093
3094 if (chip->state == FL_PM_SUSPENDED)
3095 nand_release_device(mtd);
3096 else
3097 pr_err("%s called for a chip which is not in suspended state\n",
3098 __func__);
3099}
3100#endif
3101
Sergey Lapindfe64e22013-01-14 03:46:50 +00003102/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01003103static void nand_set_defaults(struct nand_chip *chip, int busw)
3104{
3105 /* check for proper chip_delay setup, set 20us if not */
3106 if (!chip->chip_delay)
3107 chip->chip_delay = 20;
3108
3109 /* check, if a user supplied command function given */
3110 if (chip->cmdfunc == NULL)
3111 chip->cmdfunc = nand_command;
3112
3113 /* check, if a user supplied wait function given */
3114 if (chip->waitfunc == NULL)
3115 chip->waitfunc = nand_wait;
3116
3117 if (!chip->select_chip)
3118 chip->select_chip = nand_select_chip;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003119
3120 /* set for ONFI nand */
3121 if (!chip->onfi_set_features)
3122 chip->onfi_set_features = nand_onfi_set_features;
3123 if (!chip->onfi_get_features)
3124 chip->onfi_get_features = nand_onfi_get_features;
3125
3126 /* If called twice, pointers that depend on busw may need to be reset */
3127 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juulcfa460a2007-10-31 13:53:06 +01003128 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3129 if (!chip->read_word)
3130 chip->read_word = nand_read_word;
3131 if (!chip->block_bad)
3132 chip->block_bad = nand_block_bad;
3133 if (!chip->block_markbad)
3134 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003135 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juulcfa460a2007-10-31 13:53:06 +01003136 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003137 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3138 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3139 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juulcfa460a2007-10-31 13:53:06 +01003140 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juulcfa460a2007-10-31 13:53:06 +01003141 if (!chip->scan_bbt)
3142 chip->scan_bbt = nand_default_bbt;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003143#ifdef __UBOOT__
3144#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
3145 if (!chip->verify_buf)
3146 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
3147#endif
3148#endif
3149
3150 if (!chip->controller) {
William Juulcfa460a2007-10-31 13:53:06 +01003151 chip->controller = &chip->hwcontrol;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003152 spin_lock_init(&chip->controller->lock);
3153 init_waitqueue_head(&chip->controller->wq);
3154 }
3155
William Juulcfa460a2007-10-31 13:53:06 +01003156}
3157
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 Schocher4e67c572014-07-15 16:08:43 +02003192#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherff94bc42014-06-24 10:10:04 +02003193/* Parse the Extended Parameter Page. */
3194static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3195 struct nand_chip *chip, struct nand_onfi_params *p)
3196{
3197 struct onfi_ext_param_page *ep;
3198 struct onfi_ext_section *s;
3199 struct onfi_ext_ecc_info *ecc;
3200 uint8_t *cursor;
3201 int ret = -EINVAL;
3202 int len;
3203 int i;
3204
3205 len = le16_to_cpu(p->ext_param_page_length) * 16;
3206 ep = kmalloc(len, GFP_KERNEL);
3207 if (!ep)
3208 return -ENOMEM;
3209
3210 /* Send our own NAND_CMD_PARAM. */
3211 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3212
3213 /* Use the Change Read Column command to skip the ONFI param pages. */
3214 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3215 sizeof(*p) * p->num_of_param_pages , -1);
3216
3217 /* Read out the Extended Parameter Page. */
3218 chip->read_buf(mtd, (uint8_t *)ep, len);
3219 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3220 != le16_to_cpu(ep->crc))) {
3221 pr_debug("fail in the CRC.\n");
3222 goto ext_out;
3223 }
3224
3225 /*
3226 * Check the signature.
3227 * Do not strictly follow the ONFI spec, maybe changed in future.
3228 */
3229#ifndef __UBOOT__
3230 if (strncmp(ep->sig, "EPPS", 4)) {
3231#else
3232 if (strncmp((char *)ep->sig, "EPPS", 4)) {
3233#endif
3234 pr_debug("The signature is invalid.\n");
3235 goto ext_out;
3236 }
3237
3238 /* find the ECC section. */
3239 cursor = (uint8_t *)(ep + 1);
3240 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3241 s = ep->sections + i;
3242 if (s->type == ONFI_SECTION_TYPE_2)
3243 break;
3244 cursor += s->length * 16;
3245 }
3246 if (i == ONFI_EXT_SECTION_MAX) {
3247 pr_debug("We can not find the ECC section.\n");
3248 goto ext_out;
3249 }
3250
3251 /* get the info we want. */
3252 ecc = (struct onfi_ext_ecc_info *)cursor;
3253
3254 if (!ecc->codeword_size) {
3255 pr_debug("Invalid codeword size\n");
3256 goto ext_out;
3257 }
3258
3259 chip->ecc_strength_ds = ecc->ecc_bits;
3260 chip->ecc_step_ds = 1 << ecc->codeword_size;
3261 ret = 0;
3262
3263ext_out:
3264 kfree(ep);
3265 return ret;
3266}
3267
3268static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3269{
3270 struct nand_chip *chip = mtd->priv;
3271 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3272
3273 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3274 feature);
3275}
3276
3277/*
3278 * Configure chip properties from Micron vendor-specific ONFI table
3279 */
3280static void nand_onfi_detect_micron(struct nand_chip *chip,
3281 struct nand_onfi_params *p)
3282{
3283 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3284
3285 if (le16_to_cpu(p->vendor_revision) < 1)
3286 return;
3287
3288 chip->read_retries = micron->read_retry_options;
3289 chip->setup_read_retry = nand_setup_read_retry_micron;
3290}
3291
Florian Fainelli0272c712011-02-25 00:01:34 +00003292/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003293 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00003294 */
Christian Hitz90e3f392011-10-12 09:32:01 +02003295static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003296 int *busw)
3297{
3298 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisb9ae6092014-05-06 00:46:16 +05303299 int i, j;
Florian Fainelli0272c712011-02-25 00:01:34 +00003300 int val;
3301
Sergey Lapindfe64e22013-01-14 03:46:50 +00003302 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00003303 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3304 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3305 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3306 return 0;
3307
Florian Fainelli0272c712011-02-25 00:01:34 +00003308 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3309 for (i = 0; i < 3; i++) {
Brian Norrisb9ae6092014-05-06 00:46:16 +05303310 for (j = 0; j < sizeof(*p); j++)
3311 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003312 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02003313 le16_to_cpu(p->crc)) {
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01003314 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00003315 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02003316 }
William Juulcfa460a2007-10-31 13:53:06 +01003317
Heiko Schocherff94bc42014-06-24 10:10:04 +02003318 if (i == 3) {
3319 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00003320 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003321 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003322
Sergey Lapindfe64e22013-01-14 03:46:50 +00003323 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00003324 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003325 if (val & (1 << 5))
3326 chip->onfi_version = 23;
3327 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00003328 chip->onfi_version = 22;
3329 else if (val & (1 << 3))
3330 chip->onfi_version = 21;
3331 else if (val & (1 << 2))
3332 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003333 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00003334 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003335
3336 if (!chip->onfi_version) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003337 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003338 return 0;
3339 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003340
Christian Hitz5454ddb2011-10-12 09:32:05 +02003341 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3342 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01003343 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00003344 mtd->name = p->model;
William Juulcfa460a2007-10-31 13:53:06 +01003345
Heiko Schocherff94bc42014-06-24 10:10:04 +02003346 mtd->writesize = le32_to_cpu(p->byte_per_page);
3347
3348 /*
3349 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3350 * (don't ask me who thought of this...). MTD assumes that these
3351 * dimensions will be power-of-2, so just truncate the remaining area.
3352 */
3353 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3354 mtd->erasesize *= mtd->writesize;
3355
3356 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3357
3358 /* See erasesize comment */
3359 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3360 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3361 chip->bits_per_cell = p->bits_per_cell;
3362
3363 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3364 *busw = NAND_BUSWIDTH_16;
3365 else
3366 *busw = 0;
3367
3368 if (p->ecc_bits != 0xff) {
3369 chip->ecc_strength_ds = p->ecc_bits;
3370 chip->ecc_step_ds = 512;
3371 } else if (chip->onfi_version >= 21 &&
3372 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3373
3374 /*
3375 * The nand_flash_detect_ext_param_page() uses the
3376 * Change Read Column command which maybe not supported
3377 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3378 * now. We do not replace user supplied command function.
3379 */
3380 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3381 chip->cmdfunc = nand_command_lp;
3382
3383 /* The Extended Parameter Page is supported since ONFI 2.1. */
3384 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3385 pr_warn("Failed to detect ONFI extended param page\n");
3386 } else {
3387 pr_warn("Could not retrieve ONFI ECC requirements\n");
3388 }
3389
3390 if (p->jedec_id == NAND_MFR_MICRON)
3391 nand_onfi_detect_micron(chip, p);
3392
Florian Fainelli0272c712011-02-25 00:01:34 +00003393 return 1;
3394}
3395#else
Heiko Schocherff94bc42014-06-24 10:10:04 +02003396static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003397 int *busw)
3398{
3399 return 0;
3400}
3401#endif
3402
Florian Fainelli0272c712011-02-25 00:01:34 +00003403/*
Heiko Schocher4e67c572014-07-15 16:08:43 +02003404 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3405 */
3406static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3407 int *busw)
3408{
3409 struct nand_jedec_params *p = &chip->jedec_params;
3410 struct jedec_ecc_info *ecc;
3411 int val;
3412 int i, j;
3413
3414 /* Try JEDEC for unknown chip or LP */
3415 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3416 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3417 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3418 chip->read_byte(mtd) != 'C')
3419 return 0;
3420
3421 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3422 for (i = 0; i < 3; i++) {
3423 for (j = 0; j < sizeof(*p); j++)
3424 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3425
3426 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3427 le16_to_cpu(p->crc))
3428 break;
3429 }
3430
3431 if (i == 3) {
3432 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3433 return 0;
3434 }
3435
3436 /* Check version */
3437 val = le16_to_cpu(p->revision);
3438 if (val & (1 << 2))
3439 chip->jedec_version = 10;
3440 else if (val & (1 << 1))
3441 chip->jedec_version = 1; /* vendor specific version */
3442
3443 if (!chip->jedec_version) {
3444 pr_info("unsupported JEDEC version: %d\n", val);
3445 return 0;
3446 }
3447
3448 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3449 sanitize_string(p->model, sizeof(p->model));
3450 if (!mtd->name)
3451 mtd->name = p->model;
3452
3453 mtd->writesize = le32_to_cpu(p->byte_per_page);
3454
3455 /* Please reference to the comment for nand_flash_detect_onfi. */
3456 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3457 mtd->erasesize *= mtd->writesize;
3458
3459 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3460
3461 /* Please reference to the comment for nand_flash_detect_onfi. */
3462 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3463 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3464 chip->bits_per_cell = p->bits_per_cell;
3465
3466 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3467 *busw = NAND_BUSWIDTH_16;
3468 else
3469 *busw = 0;
3470
3471 /* ECC info */
3472 ecc = &p->ecc_info[0];
3473
3474 if (ecc->codeword_size >= 9) {
3475 chip->ecc_strength_ds = ecc->ecc_bits;
3476 chip->ecc_step_ds = 1 << ecc->codeword_size;
3477 } else {
3478 pr_warn("Invalid codeword size\n");
3479 }
3480
3481 return 1;
3482}
3483
3484/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003485 * nand_id_has_period - Check if an ID string has a given wraparound period
3486 * @id_data: the ID string
3487 * @arrlen: the length of the @id_data array
3488 * @period: the period of repitition
3489 *
3490 * Check if an ID string is repeated within a given sequence of bytes at
3491 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherff94bc42014-06-24 10:10:04 +02003492 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapindfe64e22013-01-14 03:46:50 +00003493 * if the repetition has a period of @period; otherwise, returns zero.
3494 */
3495static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3496{
3497 int i, j;
3498 for (i = 0; i < period; i++)
3499 for (j = i + period; j < arrlen; j += period)
3500 if (id_data[i] != id_data[j])
3501 return 0;
3502 return 1;
3503}
3504
3505/*
3506 * nand_id_len - Get the length of an ID string returned by CMD_READID
3507 * @id_data: the ID string
3508 * @arrlen: the length of the @id_data array
3509
3510 * Returns the length of the ID string, according to known wraparound/trailing
3511 * zero patterns. If no pattern exists, returns the length of the array.
3512 */
3513static int nand_id_len(u8 *id_data, int arrlen)
3514{
3515 int last_nonzero, period;
3516
3517 /* Find last non-zero byte */
3518 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3519 if (id_data[last_nonzero])
3520 break;
3521
3522 /* All zeros */
3523 if (last_nonzero < 0)
3524 return 0;
3525
3526 /* Calculate wraparound period */
3527 for (period = 1; period < arrlen; period++)
3528 if (nand_id_has_period(id_data, arrlen, period))
3529 break;
3530
3531 /* There's a repeated pattern */
3532 if (period < arrlen)
3533 return period;
3534
3535 /* There are trailing zeros */
3536 if (last_nonzero < arrlen - 1)
3537 return last_nonzero + 1;
3538
3539 /* No pattern detected */
3540 return arrlen;
3541}
3542
Heiko Schocherff94bc42014-06-24 10:10:04 +02003543/* Extract the bits of per cell from the 3rd byte of the extended ID */
3544static int nand_get_bits_per_cell(u8 cellinfo)
3545{
3546 int bits;
3547
3548 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3549 bits >>= NAND_CI_CELLTYPE_SHIFT;
3550 return bits + 1;
3551}
3552
Sergey Lapindfe64e22013-01-14 03:46:50 +00003553/*
3554 * Many new NAND share similar device ID codes, which represent the size of the
3555 * chip. The rest of the parameters must be decoded according to generic or
3556 * manufacturer-specific "extended ID" decoding patterns.
3557 */
3558static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3559 u8 id_data[8], int *busw)
3560{
3561 int extid, id_len;
3562 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003563 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003564 /* The 4th id byte is the important one */
3565 extid = id_data[3];
3566
3567 id_len = nand_id_len(id_data, 8);
3568
3569 /*
3570 * Field definitions are in the following datasheets:
3571 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3572 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3573 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3574 *
3575 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3576 * ID to decide what to do.
3577 */
3578 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003579 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003580 /* Calc pagesize */
3581 mtd->writesize = 2048 << (extid & 0x03);
3582 extid >>= 2;
3583 /* Calc oobsize */
3584 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3585 case 1:
3586 mtd->oobsize = 128;
3587 break;
3588 case 2:
3589 mtd->oobsize = 218;
3590 break;
3591 case 3:
3592 mtd->oobsize = 400;
3593 break;
3594 case 4:
3595 mtd->oobsize = 436;
3596 break;
3597 case 5:
3598 mtd->oobsize = 512;
3599 break;
3600 case 6:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003601 mtd->oobsize = 640;
3602 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003603 case 7:
3604 default: /* Other cases are "reserved" (unknown) */
3605 mtd->oobsize = 1024;
3606 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003607 }
3608 extid >>= 2;
3609 /* Calc blocksize */
3610 mtd->erasesize = (128 * 1024) <<
3611 (((extid >> 1) & 0x04) | (extid & 0x03));
3612 *busw = 0;
3613 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003614 !nand_is_slc(chip)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003615 unsigned int tmp;
3616
3617 /* Calc pagesize */
3618 mtd->writesize = 2048 << (extid & 0x03);
3619 extid >>= 2;
3620 /* Calc oobsize */
3621 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3622 case 0:
3623 mtd->oobsize = 128;
3624 break;
3625 case 1:
3626 mtd->oobsize = 224;
3627 break;
3628 case 2:
3629 mtd->oobsize = 448;
3630 break;
3631 case 3:
3632 mtd->oobsize = 64;
3633 break;
3634 case 4:
3635 mtd->oobsize = 32;
3636 break;
3637 case 5:
3638 mtd->oobsize = 16;
3639 break;
3640 default:
3641 mtd->oobsize = 640;
3642 break;
3643 }
3644 extid >>= 2;
3645 /* Calc blocksize */
3646 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3647 if (tmp < 0x03)
3648 mtd->erasesize = (128 * 1024) << tmp;
3649 else if (tmp == 0x03)
3650 mtd->erasesize = 768 * 1024;
3651 else
3652 mtd->erasesize = (64 * 1024) << tmp;
3653 *busw = 0;
3654 } else {
3655 /* Calc pagesize */
3656 mtd->writesize = 1024 << (extid & 0x03);
3657 extid >>= 2;
3658 /* Calc oobsize */
3659 mtd->oobsize = (8 << (extid & 0x01)) *
3660 (mtd->writesize >> 9);
3661 extid >>= 2;
3662 /* Calc blocksize. Blocksize is multiples of 64KiB */
3663 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3664 extid >>= 2;
3665 /* Get buswidth information */
3666 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003667
3668 /*
3669 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3670 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3671 * follows:
3672 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3673 * 110b -> 24nm
3674 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3675 */
3676 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3677 nand_is_slc(chip) &&
3678 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3679 !(id_data[4] & 0x80) /* !BENAND */) {
3680 mtd->oobsize = 32 * mtd->writesize >> 9;
3681 }
3682
Sergey Lapindfe64e22013-01-14 03:46:50 +00003683 }
3684}
3685
Heiko Schocherff94bc42014-06-24 10:10:04 +02003686/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003687 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3688 * decodes a matching ID table entry and assigns the MTD size parameters for
3689 * the chip.
3690 */
3691static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003692 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapindfe64e22013-01-14 03:46:50 +00003693 int *busw)
3694{
3695 int maf_id = id_data[0];
3696
3697 mtd->erasesize = type->erasesize;
3698 mtd->writesize = type->pagesize;
3699 mtd->oobsize = mtd->writesize / 32;
3700 *busw = type->options & NAND_BUSWIDTH_16;
3701
Heiko Schocherff94bc42014-06-24 10:10:04 +02003702 /* All legacy ID NAND are small-page, SLC */
3703 chip->bits_per_cell = 1;
3704
Sergey Lapindfe64e22013-01-14 03:46:50 +00003705 /*
3706 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3707 * some Spansion chips have erasesize that conflicts with size
3708 * listed in nand_ids table.
3709 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3710 */
3711 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3712 && id_data[6] == 0x00 && id_data[7] == 0x00
3713 && mtd->writesize == 512) {
3714 mtd->erasesize = 128 * 1024;
3715 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3716 }
3717}
3718
Heiko Schocherff94bc42014-06-24 10:10:04 +02003719/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003720 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3721 * heuristic patterns using various detected parameters (e.g., manufacturer,
3722 * page size, cell-type information).
3723 */
3724static void nand_decode_bbm_options(struct mtd_info *mtd,
3725 struct nand_chip *chip, u8 id_data[8])
3726{
3727 int maf_id = id_data[0];
3728
3729 /* Set the bad block position */
3730 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3731 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3732 else
3733 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3734
3735 /*
3736 * Bad block marker is stored in the last page of each block on Samsung
3737 * and Hynix MLC devices; stored in first two pages of each block on
3738 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3739 * AMD/Spansion, and Macronix. All others scan only the first page.
3740 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003741 if (!nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003742 (maf_id == NAND_MFR_SAMSUNG ||
3743 maf_id == NAND_MFR_HYNIX))
3744 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003745 else if ((nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003746 (maf_id == NAND_MFR_SAMSUNG ||
3747 maf_id == NAND_MFR_HYNIX ||
3748 maf_id == NAND_MFR_TOSHIBA ||
3749 maf_id == NAND_MFR_AMD ||
3750 maf_id == NAND_MFR_MACRONIX)) ||
3751 (mtd->writesize == 2048 &&
3752 maf_id == NAND_MFR_MICRON))
3753 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3754}
3755
Heiko Schocherff94bc42014-06-24 10:10:04 +02003756static inline bool is_full_id_nand(struct nand_flash_dev *type)
3757{
3758 return type->id_len;
3759}
3760
3761static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3762 struct nand_flash_dev *type, u8 *id_data, int *busw)
3763{
3764#ifndef __UBOOT__
3765 if (!strncmp(type->id, id_data, type->id_len)) {
3766#else
3767 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3768#endif
3769 mtd->writesize = type->pagesize;
3770 mtd->erasesize = type->erasesize;
3771 mtd->oobsize = type->oobsize;
3772
3773 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3774 chip->chipsize = (uint64_t)type->chipsize << 20;
3775 chip->options |= type->options;
3776 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3777 chip->ecc_step_ds = NAND_ECC_STEP(type);
3778
3779 *busw = type->options & NAND_BUSWIDTH_16;
3780
3781 if (!mtd->name)
3782 mtd->name = type->name;
3783
3784 return true;
3785 }
3786 return false;
3787}
3788
Sergey Lapindfe64e22013-01-14 03:46:50 +00003789/*
3790 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00003791 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003792static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Florian Fainelli0272c712011-02-25 00:01:34 +00003793 struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003794 int *maf_id, int *dev_id,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003795 struct nand_flash_dev *type)
Florian Fainelli0272c712011-02-25 00:01:34 +00003796{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003797 int busw;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003798 int i, maf_idx;
3799 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00003800
3801 /* Select the device */
3802 chip->select_chip(mtd, 0);
3803
3804 /*
3805 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003806 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00003807 */
3808 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3809
3810 /* Send the command for reading device ID */
3811 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3812
3813 /* Read manufacturer and device IDs */
3814 *maf_id = chip->read_byte(mtd);
3815 *dev_id = chip->read_byte(mtd);
3816
Sergey Lapindfe64e22013-01-14 03:46:50 +00003817 /*
3818 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00003819 * interface concerns can cause random data which looks like a
3820 * possibly credible NAND flash to appear. If the two results do
3821 * not match, ignore the device completely.
3822 */
3823
3824 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3825
Sergey Lapindfe64e22013-01-14 03:46:50 +00003826 /* Read entire ID string */
3827 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003828 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003829
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003830 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003831 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003832 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00003833 return ERR_PTR(-ENODEV);
3834 }
3835
3836 if (!type)
3837 type = nand_flash_ids;
3838
Heiko Schocherff94bc42014-06-24 10:10:04 +02003839 for (; type->name != NULL; type++) {
3840 if (is_full_id_nand(type)) {
3841 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3842 goto ident_done;
3843 } else if (*dev_id == type->dev_id) {
3844 break;
3845 }
3846 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003847
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003848 chip->onfi_version = 0;
3849 if (!type->name || !type->pagesize) {
3850 /* Check is chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00003851 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003852 goto ident_done;
Heiko Schocher4e67c572014-07-15 16:08:43 +02003853
3854 /* Check if the chip is JEDEC compliant */
3855 if (nand_flash_detect_jedec(mtd, chip, &busw))
3856 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00003857 }
3858
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003859 if (!type->name)
3860 return ERR_PTR(-ENODEV);
3861
Florian Fainelli0272c712011-02-25 00:01:34 +00003862 if (!mtd->name)
3863 mtd->name = type->name;
3864
3865 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00003866
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003867 if (!type->pagesize && chip->init_size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003868 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003869 busw = chip->init_size(mtd, chip, id_data);
3870 } else if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003871 /* Decode parameters from extended ID */
3872 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003873 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003874 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003875 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003876 /* Get chip options */
Marek Vasut9c790a72012-08-30 13:39:38 +00003877 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00003878
Sergey Lapindfe64e22013-01-14 03:46:50 +00003879 /*
3880 * Check if chip is not a Samsung device. Do not clear the
3881 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003882 */
3883 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3884 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3885ident_done:
3886
William Juulcfa460a2007-10-31 13:53:06 +01003887 /* Try to identify manufacturer */
3888 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3889 if (nand_manuf_ids[maf_idx].id == *maf_id)
3890 break;
3891 }
3892
Heiko Schocherff94bc42014-06-24 10:10:04 +02003893 if (chip->options & NAND_BUSWIDTH_AUTO) {
3894 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3895 chip->options |= busw;
3896 nand_set_defaults(chip, busw);
3897 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3898 /*
3899 * Check, if buswidth is correct. Hardware drivers should set
3900 * chip correct!
3901 */
3902 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3903 *maf_id, *dev_id);
3904 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3905 pr_warn("bus width %d instead %d bit\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003906 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3907 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01003908 return ERR_PTR(-EINVAL);
3909 }
3910
Sergey Lapindfe64e22013-01-14 03:46:50 +00003911 nand_decode_bbm_options(mtd, chip, id_data);
3912
William Juulcfa460a2007-10-31 13:53:06 +01003913 /* Calculate the address shift from the page size */
3914 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003915 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01003916 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3917
3918 chip->bbt_erase_shift = chip->phys_erase_shift =
3919 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003920 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003921 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003922 else {
3923 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3924 chip->chip_shift += 32 - 1;
3925 }
3926
3927 chip->badblockbits = 8;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003928 chip->erase_cmd = single_erase_cmd;
William Juulcfa460a2007-10-31 13:53:06 +01003929
Sergey Lapindfe64e22013-01-14 03:46:50 +00003930 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003931 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3932 chip->cmdfunc = nand_command_lp;
3933
Heiko Schocherff94bc42014-06-24 10:10:04 +02003934 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3935 *maf_id, *dev_id);
Heiko Schocher4e67c572014-07-15 16:08:43 +02003936
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003937#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher4e67c572014-07-15 16:08:43 +02003938 if (chip->onfi_version)
3939 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3940 chip->onfi_params.model);
3941 else if (chip->jedec_version)
3942 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3943 chip->jedec_params.model);
3944 else
3945 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3946 type->name);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003947#else
Heiko Schocher4e67c572014-07-15 16:08:43 +02003948 if (chip->jedec_version)
3949 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3950 chip->jedec_params.model);
3951 else
3952 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3953 type->name);
3954
3955 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3956 type->name);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003957#endif
Heiko Schocher4e67c572014-07-15 16:08:43 +02003958
Heiko Schocherff94bc42014-06-24 10:10:04 +02003959 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3960 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003961 mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003962 return type;
3963}
3964
3965/**
3966 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003967 * @mtd: MTD device structure
3968 * @maxchips: number of chips to scan for
3969 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003970 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003971 * This is the first phase of the normal nand_scan() function. It reads the
3972 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003973 *
3974 * The mtd->owner field must be set to the module of the caller.
3975 */
Lei Wen245eb902011-01-06 09:48:18 +08003976int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003977 struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003978{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003979 int i, nand_maf_id, nand_dev_id;
William Juulcfa460a2007-10-31 13:53:06 +01003980 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003981 struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01003982
William Juulcfa460a2007-10-31 13:53:06 +01003983 /* Set the default functions */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003984 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juulcfa460a2007-10-31 13:53:06 +01003985
3986 /* Read the flash type */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003987 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3988 &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003989
3990 if (IS_ERR(type)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003991 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3992 pr_warn("No NAND device found\n");
William Juulcfa460a2007-10-31 13:53:06 +01003993 chip->select_chip(mtd, -1);
3994 return PTR_ERR(type);
3995 }
3996
Heiko Schocherff94bc42014-06-24 10:10:04 +02003997 chip->select_chip(mtd, -1);
3998
William Juulcfa460a2007-10-31 13:53:06 +01003999 /* Check for a chip array */
4000 for (i = 1; i < maxchips; i++) {
4001 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02004002 /* See comment in nand_get_flash_type for reset */
4003 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01004004 /* Send the command for reading device ID */
4005 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4006 /* Read manufacturer and device IDs */
4007 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02004008 nand_dev_id != chip->read_byte(mtd)) {
4009 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01004010 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004011 }
4012 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01004013 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004014
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01004015#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01004016 if (i > 1)
Heiko Schocherff94bc42014-06-24 10:10:04 +02004017 pr_info("%d chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01004018#endif
William Juulcfa460a2007-10-31 13:53:06 +01004019
4020 /* Store the number of chips and calc total size for mtd */
4021 chip->numchips = i;
4022 mtd->size = i * chip->chipsize;
4023
4024 return 0;
4025}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004026EXPORT_SYMBOL(nand_scan_ident);
William Juulcfa460a2007-10-31 13:53:06 +01004027
4028
4029/**
4030 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004031 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01004032 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00004033 * This is the second phase of the normal nand_scan() function. It fills out
4034 * all the uninitialized function pointers with the defaults and scans for a
4035 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01004036 */
4037int nand_scan_tail(struct mtd_info *mtd)
4038{
4039 int i;
4040 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004041 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher4e67c572014-07-15 16:08:43 +02004042 struct nand_buffers *nbuf;
William Juulcfa460a2007-10-31 13:53:06 +01004043
Sergey Lapindfe64e22013-01-14 03:46:50 +00004044 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4045 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4046 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4047
Heiko Schocher4e67c572014-07-15 16:08:43 +02004048 if (!(chip->options & NAND_OWN_BUFFERS)) {
4049#ifndef __UBOOT__
4050 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4051 + mtd->oobsize * 3, GFP_KERNEL);
4052 if (!nbuf)
4053 return -ENOMEM;
4054 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4055 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4056 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4057#else
4058 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
4059#endif
4060
4061 chip->buffers = nbuf;
4062 } else {
4063 if (!chip->buffers)
4064 return -ENOMEM;
4065 }
William Juulcfa460a2007-10-31 13:53:06 +01004066
4067 /* Set the internal oob buffer location, just after the page data */
4068 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4069
4070 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00004071 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01004072 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004073 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01004074 switch (mtd->oobsize) {
4075 case 8:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004076 ecc->layout = &nand_oob_8;
William Juulcfa460a2007-10-31 13:53:06 +01004077 break;
4078 case 16:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004079 ecc->layout = &nand_oob_16;
William Juulcfa460a2007-10-31 13:53:06 +01004080 break;
4081 case 64:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004082 ecc->layout = &nand_oob_64;
William Juulcfa460a2007-10-31 13:53:06 +01004083 break;
4084 case 128:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004085 ecc->layout = &nand_oob_128;
William Juulcfa460a2007-10-31 13:53:06 +01004086 break;
4087 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004088 pr_warn("No oob scheme defined for oobsize %d\n",
4089 mtd->oobsize);
Heiko Schocherff94bc42014-06-24 10:10:04 +02004090 BUG();
William Juulcfa460a2007-10-31 13:53:06 +01004091 }
4092 }
4093
4094 if (!chip->write_page)
4095 chip->write_page = nand_write_page;
4096
4097 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00004098 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01004099 * selected and we have 256 byte pagesize fallback to software ECC
4100 */
William Juulcfa460a2007-10-31 13:53:06 +01004101
Heiko Schocherff94bc42014-06-24 10:10:04 +02004102 switch (ecc->mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004103 case NAND_ECC_HW_OOB_FIRST:
4104 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004105 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004106 pr_warn("No ECC functions supplied; "
4107 "hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004108 BUG();
4109 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004110 if (!ecc->read_page)
4111 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004112
William Juulcfa460a2007-10-31 13:53:06 +01004113 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004114 /* Use standard hwecc read page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004115 if (!ecc->read_page)
4116 ecc->read_page = nand_read_page_hwecc;
4117 if (!ecc->write_page)
4118 ecc->write_page = nand_write_page_hwecc;
4119 if (!ecc->read_page_raw)
4120 ecc->read_page_raw = nand_read_page_raw;
4121 if (!ecc->write_page_raw)
4122 ecc->write_page_raw = nand_write_page_raw;
4123 if (!ecc->read_oob)
4124 ecc->read_oob = nand_read_oob_std;
4125 if (!ecc->write_oob)
4126 ecc->write_oob = nand_write_oob_std;
4127 if (!ecc->read_subpage)
4128 ecc->read_subpage = nand_read_subpage;
4129 if (!ecc->write_subpage)
4130 ecc->write_subpage = nand_write_subpage_hwecc;
William Juulcfa460a2007-10-31 13:53:06 +01004131
4132 case NAND_ECC_HW_SYNDROME:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004133 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4134 (!ecc->read_page ||
4135 ecc->read_page == nand_read_page_hwecc ||
4136 !ecc->write_page ||
4137 ecc->write_page == nand_write_page_hwecc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004138 pr_warn("No ECC functions supplied; "
4139 "hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01004140 BUG();
4141 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00004142 /* Use standard syndrome read/write page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004143 if (!ecc->read_page)
4144 ecc->read_page = nand_read_page_syndrome;
4145 if (!ecc->write_page)
4146 ecc->write_page = nand_write_page_syndrome;
4147 if (!ecc->read_page_raw)
4148 ecc->read_page_raw = nand_read_page_raw_syndrome;
4149 if (!ecc->write_page_raw)
4150 ecc->write_page_raw = nand_write_page_raw_syndrome;
4151 if (!ecc->read_oob)
4152 ecc->read_oob = nand_read_oob_syndrome;
4153 if (!ecc->write_oob)
4154 ecc->write_oob = nand_write_oob_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01004155
Heiko Schocherff94bc42014-06-24 10:10:04 +02004156 if (mtd->writesize >= ecc->size) {
4157 if (!ecc->strength) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004158 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4159 BUG();
4160 }
William Juulcfa460a2007-10-31 13:53:06 +01004161 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004162 }
4163 pr_warn("%d byte HW ECC not possible on "
4164 "%d byte page size, fallback to SW ECC\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02004165 ecc->size, mtd->writesize);
4166 ecc->mode = NAND_ECC_SOFT;
William Juulcfa460a2007-10-31 13:53:06 +01004167
4168 case NAND_ECC_SOFT:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004169 ecc->calculate = nand_calculate_ecc;
4170 ecc->correct = nand_correct_data;
4171 ecc->read_page = nand_read_page_swecc;
4172 ecc->read_subpage = nand_read_subpage;
4173 ecc->write_page = nand_write_page_swecc;
4174 ecc->read_page_raw = nand_read_page_raw;
4175 ecc->write_page_raw = nand_write_page_raw;
4176 ecc->read_oob = nand_read_oob_std;
4177 ecc->write_oob = nand_write_oob_std;
4178 if (!ecc->size)
4179 ecc->size = 256;
4180 ecc->bytes = 3;
4181 ecc->strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01004182 break;
4183
Christian Hitz4c6de852011-10-12 09:31:59 +02004184 case NAND_ECC_SOFT_BCH:
4185 if (!mtd_nand_has_bch()) {
Heiko Schocher4e67c572014-07-15 16:08:43 +02004186 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004187 BUG();
Christian Hitz4c6de852011-10-12 09:31:59 +02004188 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004189 ecc->calculate = nand_bch_calculate_ecc;
4190 ecc->correct = nand_bch_correct_data;
4191 ecc->read_page = nand_read_page_swecc;
4192 ecc->read_subpage = nand_read_subpage;
4193 ecc->write_page = nand_write_page_swecc;
4194 ecc->read_page_raw = nand_read_page_raw;
4195 ecc->write_page_raw = nand_write_page_raw;
4196 ecc->read_oob = nand_read_oob_std;
4197 ecc->write_oob = nand_write_oob_std;
Christian Hitz4c6de852011-10-12 09:31:59 +02004198 /*
4199 * Board driver should supply ecc.size and ecc.bytes values to
4200 * select how many bits are correctable; see nand_bch_init()
Sergey Lapindfe64e22013-01-14 03:46:50 +00004201 * for details. Otherwise, default to 4 bits for large page
4202 * devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02004203 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004204 if (!ecc->size && (mtd->oobsize >= 64)) {
4205 ecc->size = 512;
4206 ecc->bytes = 7;
Christian Hitz4c6de852011-10-12 09:31:59 +02004207 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004208 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4209 &ecc->layout);
4210 if (!ecc->priv) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004211 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004212 BUG();
4213 }
4214 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Christian Hitz4c6de852011-10-12 09:31:59 +02004215 break;
4216
William Juulcfa460a2007-10-31 13:53:06 +01004217 case NAND_ECC_NONE:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004218 pr_warn("NAND_ECC_NONE selected by board driver. "
Heiko Schocherff94bc42014-06-24 10:10:04 +02004219 "This is not recommended!\n");
4220 ecc->read_page = nand_read_page_raw;
4221 ecc->write_page = nand_write_page_raw;
4222 ecc->read_oob = nand_read_oob_std;
4223 ecc->read_page_raw = nand_read_page_raw;
4224 ecc->write_page_raw = nand_write_page_raw;
4225 ecc->write_oob = nand_write_oob_std;
4226 ecc->size = mtd->writesize;
4227 ecc->bytes = 0;
4228 ecc->strength = 0;
William Juulcfa460a2007-10-31 13:53:06 +01004229 break;
4230
4231 default:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004232 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juulcfa460a2007-10-31 13:53:06 +01004233 BUG();
4234 }
4235
Sergey Lapindfe64e22013-01-14 03:46:50 +00004236 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004237 if (!ecc->read_oob_raw)
4238 ecc->read_oob_raw = ecc->read_oob;
4239 if (!ecc->write_oob_raw)
4240 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004241
William Juulcfa460a2007-10-31 13:53:06 +01004242 /*
4243 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00004244 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01004245 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004246 ecc->layout->oobavail = 0;
4247 for (i = 0; ecc->layout->oobfree[i].length
4248 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4249 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4250 mtd->oobavail = ecc->layout->oobavail;
William Juulcfa460a2007-10-31 13:53:06 +01004251
4252 /*
4253 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00004254 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01004255 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004256 ecc->steps = mtd->writesize / ecc->size;
4257 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004258 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01004259 BUG();
4260 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004261 ecc->total = ecc->steps * ecc->bytes;
William Juulcfa460a2007-10-31 13:53:06 +01004262
Sergey Lapindfe64e22013-01-14 03:46:50 +00004263 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004264 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4265 switch (ecc->steps) {
William Juulcfa460a2007-10-31 13:53:06 +01004266 case 2:
4267 mtd->subpage_sft = 1;
4268 break;
4269 case 4:
4270 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05004271 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01004272 mtd->subpage_sft = 2;
4273 break;
4274 }
4275 }
4276 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4277
4278 /* Initialize state */
4279 chip->state = FL_READY;
4280
William Juulcfa460a2007-10-31 13:53:06 +01004281 /* Invalidate the pagebuffer reference */
4282 chip->pagebuf = -1;
4283
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004284 /* Large page NAND with SOFT_ECC should support subpage reads */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004285 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004286 chip->options |= NAND_SUBPAGE_READ;
4287
William Juulcfa460a2007-10-31 13:53:06 +01004288 /* Fill in remaining MTD driver data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004289 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004290 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4291 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004292 mtd->_erase = nand_erase;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004293#ifndef __UBOOT__
Sergey Lapindfe64e22013-01-14 03:46:50 +00004294 mtd->_point = NULL;
4295 mtd->_unpoint = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004296#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00004297 mtd->_read = nand_read;
4298 mtd->_write = nand_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004299 mtd->_panic_write = panic_nand_write;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004300 mtd->_read_oob = nand_read_oob;
4301 mtd->_write_oob = nand_write_oob;
4302 mtd->_sync = nand_sync;
4303 mtd->_lock = NULL;
4304 mtd->_unlock = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004305#ifndef __UBOOT__
4306 mtd->_suspend = nand_suspend;
4307 mtd->_resume = nand_resume;
4308#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00004309 mtd->_block_isbad = nand_block_isbad;
4310 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004311 mtd->writebufsize = mtd->writesize;
William Juulcfa460a2007-10-31 13:53:06 +01004312
Sergey Lapindfe64e22013-01-14 03:46:50 +00004313 /* propagate ecc info to mtd_info */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004314 mtd->ecclayout = ecc->layout;
4315 mtd->ecc_strength = ecc->strength;
4316 mtd->ecc_step_size = ecc->size;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004317 /*
4318 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4319 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4320 * properly set.
4321 */
4322 if (!mtd->bitflip_threshold)
4323 mtd->bitflip_threshold = mtd->ecc_strength;
William Juulcfa460a2007-10-31 13:53:06 +01004324
4325 /* Check, if we should skip the bad block table scan */
4326 if (chip->options & NAND_SKIP_BBTSCAN)
Heiko Schocherff94bc42014-06-24 10:10:04 +02004327 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01004328
Heiko Schocherff94bc42014-06-24 10:10:04 +02004329 /* Build bad block table */
4330 return chip->scan_bbt(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01004331}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004332EXPORT_SYMBOL(nand_scan_tail);
4333
4334/*
4335 * is_module_text_address() isn't exported, and it's mostly a pointless
4336 * test if this is a module _anyway_ -- they'd have to try _really_ hard
4337 * to call us from in-kernel code if the core NAND support is modular.
4338 */
4339#ifdef MODULE
4340#define caller_is_module() (1)
4341#else
4342#define caller_is_module() \
4343 is_module_text_address((unsigned long)__builtin_return_address(0))
4344#endif
William Juulcfa460a2007-10-31 13:53:06 +01004345
William Juulcfa460a2007-10-31 13:53:06 +01004346/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02004347 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004348 * @mtd: MTD device structure
4349 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02004350 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00004351 * This fills out all the uninitialized function pointers with the defaults.
4352 * The flash ID is read and the mtd/chip structures are filled with the
4353 * appropriate values. The mtd->owner field must be set to the module of the
4354 * caller.
Wolfgang Denk932394a2005-08-17 12:55:25 +02004355 */
William Juulcfa460a2007-10-31 13:53:06 +01004356int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004357{
William Juulcfa460a2007-10-31 13:53:06 +01004358 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004359
Heiko Schocherff94bc42014-06-24 10:10:04 +02004360 /* Many callers got this wrong, so check for it for a while... */
4361 if (!mtd->owner && caller_is_module()) {
4362 pr_crit("%s called with NULL mtd->owner!\n", __func__);
4363 BUG();
4364 }
4365
Lei Wen245eb902011-01-06 09:48:18 +08004366 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01004367 if (!ret)
4368 ret = nand_scan_tail(mtd);
4369 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004370}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004371EXPORT_SYMBOL(nand_scan);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004372
Heiko Schocherff94bc42014-06-24 10:10:04 +02004373#ifndef __UBOOT__
Wolfgang Denk932394a2005-08-17 12:55:25 +02004374/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02004375 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004376 * @mtd: MTD device structure
4377 */
William Juulcfa460a2007-10-31 13:53:06 +01004378void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004379{
William Juulcfa460a2007-10-31 13:53:06 +01004380 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004381
Christian Hitz4c6de852011-10-12 09:31:59 +02004382 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4383 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4384
Heiko Schocherff94bc42014-06-24 10:10:04 +02004385 mtd_device_unregister(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01004386
4387 /* Free bad block table memory */
4388 kfree(chip->bbt);
4389 if (!(chip->options & NAND_OWN_BUFFERS))
4390 kfree(chip->buffers);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004391
4392 /* Free bad block descriptor memory */
4393 if (chip->badblock_pattern && chip->badblock_pattern->options
4394 & NAND_BBT_DYNAMICSTRUCT)
4395 kfree(chip->badblock_pattern);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004396}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004397EXPORT_SYMBOL_GPL(nand_release);
4398
4399static int __init nand_base_init(void)
4400{
4401 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4402 return 0;
4403}
4404
4405static void __exit nand_base_exit(void)
4406{
4407 led_trigger_unregister_simple(nand_led_trigger);
4408}
4409#endif
4410
4411module_init(nand_base_init);
4412module_exit(nand_base_exit);
4413
4414MODULE_LICENSE("GPL");
4415MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4416MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4417MODULE_DESCRIPTION("Generic NAND flash driver code");