blob: 6db6566e733656d55a88c024bd1b07b5c6d58c3c [file] [log] [blame]
Wolfgang Denk2f9b7e42005-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 Denk7b9bc3a2005-09-14 23:53:32 +02007 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02008 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -05009 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020010 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020011 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010012 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020013 *
William Juul52c07962007-10-31 13:53:06 +010014 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020015 * David Woodhouse for adding multichip support
16 *
Wolfgang Denk2f9b7e42005-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 Juul52c07962007-10-31 13:53:06 +010020 * TODO:
Wolfgang Denk2f9b7e42005-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 Lapin3a38a552013-01-14 03:46:50 +000023 * if we have HW ECC support.
Scott Wood3628f002008-10-24 16:20:43 -050024 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020025 *
Wolfgang Denk2f9b7e42005-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 Schocherf5895d12014-06-24 10:10:04 +020032#ifndef __UBOOT__
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
William Juul52c07962007-10-31 13:53:06 +010034
Heiko Schocherf5895d12014-06-24 10:10:04 +020035#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
53#include <common.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020054#include <malloc.h>
55#include <watchdog.h>
William Juul52c07962007-10-31 13:53:06 +010056#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000057#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-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 Hitz55f7bca2011-10-12 09:31:59 +020061#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020062#ifdef CONFIG_MTD_PARTITIONS
63#include <linux/mtd/partitions.h>
64#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020065#include <asm/io.h>
66#include <asm/errno.h>
67
Peter Tyserf9f36222009-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 Schocherf5895d12014-06-24 10:10:04 +020079static bool is_module_text_address(unsigned long addr) {return 0;}
80#endif
81
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020082/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010083static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020084 .eccbytes = 3,
85 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010086 .oobfree = {
87 {.offset = 3,
88 .length = 2},
89 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020090 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020091};
92
William Juul52c07962007-10-31 13:53:06 +010093static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094 .eccbytes = 6,
95 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010096 .oobfree = {
97 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020098 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020099};
100
William Juul52c07962007-10-31 13:53:06 +0100101static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200102 .eccbytes = 24,
103 .eccpos = {
William Juul52c07962007-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 Hitz13fc0e22011-10-12 09:32:01 +0200109 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200110};
111
William Juul52c07962007-10-31 13:53:06 +0100112static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200113 .eccbytes = 48,
114 .eccpos = {
Christian Hitz13fc0e22011-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 Juul52c07962007-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 Hitz13fc0e22011-10-12 09:32:01 +0200123 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200124};
125
Heiko Schocherf5895d12014-06-24 10:10:04 +0200126static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-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 Schocherf5895d12014-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 Poselenov04fbaa02008-06-06 15:42:43 +0200136
Christian Hitzb8a6b372011-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 Schocherf5895d12014-06-24 10:10:04 +0200144 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
145 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200146 ret = -EINVAL;
147 }
148
149 /* Length must align on block boundary */
Heiko Schocherf5895d12014-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 Hitzb8a6b372011-10-12 09:32:02 +0200152 ret = -EINVAL;
153 }
154
Christian Hitzb8a6b372011-10-12 09:32:02 +0200155 return ret;
156}
157
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200158/**
159 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000160 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200161 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200162 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200163 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200164static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100165{
Christian Hitzb8a6b372011-10-12 09:32:02 +0200166 struct nand_chip *chip = mtd->priv;
167
Heiko Schocherf5895d12014-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 Hitzb8a6b372011-10-12 09:32:02 +0200176 /* De-select the NAND device */
177 chip->select_chip(mtd, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200178#endif
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100179}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200180
181/**
182 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000183 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200184 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200185 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200186 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200187#ifndef __UBOOT__
188static uint8_t nand_read_byte(struct mtd_info *mtd)
189#else
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000190uint8_t nand_read_byte(struct mtd_info *mtd)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200191#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192{
William Juul52c07962007-10-31 13:53:06 +0100193 struct nand_chip *chip = mtd->priv;
194 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200195}
196
197/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200198 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000199 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
200 * @mtd: MTD device structure
201 *
202 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200203 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200204 */
William Juul52c07962007-10-31 13:53:06 +0100205static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200206{
William Juul52c07962007-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 Denk2f9b7e42005-08-17 12:55:25 +0200209}
210
211/**
212 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000213 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200214 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000215 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200216 */
217static u16 nand_read_word(struct mtd_info *mtd)
218{
William Juul52c07962007-10-31 13:53:06 +0100219 struct nand_chip *chip = mtd->priv;
220 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200221}
222
223/**
224 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000225 * @mtd: MTD device structure
226 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200227 *
228 * Default select function for 1 chip devices.
229 */
William Juul52c07962007-10-31 13:53:06 +0100230static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200231{
William Juul52c07962007-10-31 13:53:06 +0100232 struct nand_chip *chip = mtd->priv;
233
234 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200235 case -1:
William Juul52c07962007-10-31 13:53:06 +0100236 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200237 break;
238 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200239 break;
240
241 default:
242 BUG();
243 }
244}
245
246/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200247 * nand_write_byte - [DEFAULT] write single byte to chip
248 * @mtd: MTD device structure
249 * @byte: value to write
250 *
251 * Default function to write a byte to I/O[7:0]
252 */
253static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
254{
255 struct nand_chip *chip = mtd->priv;
256
257 chip->write_buf(mtd, &byte, 1);
258}
259
260/**
261 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
262 * @mtd: MTD device structure
263 * @byte: value to write
264 *
265 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
266 */
267static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
268{
269 struct nand_chip *chip = mtd->priv;
270 uint16_t word = byte;
271
272 /*
273 * It's not entirely clear what should happen to I/O[15:8] when writing
274 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
275 *
276 * When the host supports a 16-bit bus width, only data is
277 * transferred at the 16-bit width. All address and command line
278 * transfers shall use only the lower 8-bits of the data bus. During
279 * command transfers, the host may place any value on the upper
280 * 8-bits of the data bus. During address transfers, the host shall
281 * set the upper 8-bits of the data bus to 00h.
282 *
283 * One user of the write_byte callback is nand_onfi_set_features. The
284 * four parameters are specified to be written to I/O[7:0], but this is
285 * neither an address nor a command transfer. Let's assume a 0 on the
286 * upper I/O lines is OK.
287 */
288 chip->write_buf(mtd, (uint8_t *)&word, 2);
289}
290
291#if defined(__UBOOT__) && !defined(CONFIG_BLACKFIN)
292static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
293{
294 int i;
295
296 for (i = 0; i < len; i++)
297 writeb(buf[i], addr);
298}
299static void ioread8_rep(void *addr, uint8_t *buf, int len)
300{
301 int i;
302
303 for (i = 0; i < len; i++)
304 buf[i] = readb(addr);
305}
306
307static void ioread16_rep(void *addr, void *buf, int len)
308{
309 int i;
310 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200311
Heiko Schocherf5895d12014-06-24 10:10:04 +0200312 for (i = 0; i < len; i++)
313 p[i] = readw(addr);
314}
315
316static void iowrite16_rep(void *addr, void *buf, int len)
317{
318 int i;
319 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200320
321 for (i = 0; i < len; i++)
322 writew(p[i], addr);
323}
324#endif
325
326/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200327 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000328 * @mtd: MTD device structure
329 * @buf: data buffer
330 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200331 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000332 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200333 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200334#ifndef __UBOOT__
335static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
336#else
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000337void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200338#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200339{
William Juul52c07962007-10-31 13:53:06 +0100340 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200341
Heiko Schocherf5895d12014-06-24 10:10:04 +0200342 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200343}
344
345/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200346 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000347 * @mtd: MTD device structure
348 * @buf: buffer to store date
349 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200350 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000351 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200352 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200353#ifndef __UBOOT__
354static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
355#else
Simon Schwarz4f62e982011-09-14 15:30:16 -0400356void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200357#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200358{
William Juul52c07962007-10-31 13:53:06 +0100359 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200360
Heiko Schocherf5895d12014-06-24 10:10:04 +0200361 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200362}
363
Heiko Schocherf5895d12014-06-24 10:10:04 +0200364#ifdef __UBOOT__
365#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200366/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200367 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000368 * @mtd: MTD device structure
369 * @buf: buffer containing the data to compare
370 * @len: number of bytes to compare
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200371 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000372 * Default verify function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200373 */
William Juul52c07962007-10-31 13:53:06 +0100374static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200375{
376 int i;
William Juul52c07962007-10-31 13:53:06 +0100377 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200378
William Juul52c07962007-10-31 13:53:06 +0100379 for (i = 0; i < len; i++)
380 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200381 return -EFAULT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200382 return 0;
383}
384
385/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200386 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000387 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200388 * @buf: buffer containing the data to compare
389 * @len: number of bytes to compare
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200390 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200391 * Default verify function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200392 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200393static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200394{
395 int i;
William Juul52c07962007-10-31 13:53:06 +0100396 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200397 u16 *p = (u16 *) buf;
398 len >>= 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200399
William Juul52c07962007-10-31 13:53:06 +0100400 for (i = 0; i < len; i++)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200401 if (p[i] != readw(chip->IO_ADDR_R))
402 return -EFAULT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200403
Heiko Schocherf5895d12014-06-24 10:10:04 +0200404 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200405}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200406#endif
407#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200408
409/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200410 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000411 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200412 * @buf: data buffer
413 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200414 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200415 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200416 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200417#ifndef __UBOOT__
418static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
419#else
420void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
421#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200422{
William Juul52c07962007-10-31 13:53:06 +0100423 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200424 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200425
Heiko Schocherf5895d12014-06-24 10:10:04 +0200426 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200427}
428
429/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200430 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000431 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200432 * @buf: buffer to store date
433 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200434 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200435 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200436 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200437#ifndef __UBOOT__
438static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
439#else
440void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
441#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200442{
William Juul52c07962007-10-31 13:53:06 +0100443 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200444 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200445
Heiko Schocherf5895d12014-06-24 10:10:04 +0200446 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200447}
448
449/**
450 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000451 * @mtd: MTD device structure
452 * @ofs: offset from device start
453 * @getchip: 0, if the chip is already selected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200454 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200455 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200456 */
457static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
458{
Sergey Lapin3a38a552013-01-14 03:46:50 +0000459 int page, chipnr, res = 0, i = 0;
William Juul52c07962007-10-31 13:53:06 +0100460 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200461 u16 bad;
462
Sergey Lapin3a38a552013-01-14 03:46:50 +0000463 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200464 ofs += mtd->erasesize - mtd->writesize;
465
William Juul52c07962007-10-31 13:53:06 +0100466 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200467
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200468 if (getchip) {
William Juul52c07962007-10-31 13:53:06 +0100469 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200470
Heiko Schocherf5895d12014-06-24 10:10:04 +0200471 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200472
473 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100474 chip->select_chip(mtd, chipnr);
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200475 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200476
Sergey Lapin3a38a552013-01-14 03:46:50 +0000477 do {
478 if (chip->options & NAND_BUSWIDTH_16) {
479 chip->cmdfunc(mtd, NAND_CMD_READOOB,
480 chip->badblockpos & 0xFE, page);
481 bad = cpu_to_le16(chip->read_word(mtd));
482 if (chip->badblockpos & 0x1)
483 bad >>= 8;
484 else
485 bad &= 0xFF;
486 } else {
487 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
488 page);
489 bad = chip->read_byte(mtd);
490 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200491
Sergey Lapin3a38a552013-01-14 03:46:50 +0000492 if (likely(chip->badblockbits == 8))
493 res = bad != 0xFF;
494 else
495 res = hweight8(bad) < chip->badblockbits;
496 ofs += mtd->writesize;
497 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
498 i++;
499 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200500
Heiko Schocherf5895d12014-06-24 10:10:04 +0200501 if (getchip) {
502 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200503 nand_release_device(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200504 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200505
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200506 return res;
507}
508
509/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200510 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000511 * @mtd: MTD device structure
512 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200513 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000514 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200515 * specific driver. It provides the details for writing a bad block marker to a
516 * block.
517 */
518static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
519{
520 struct nand_chip *chip = mtd->priv;
521 struct mtd_oob_ops ops;
522 uint8_t buf[2] = { 0, 0 };
523 int ret = 0, res, i = 0;
524
525 ops.datbuf = NULL;
526 ops.oobbuf = buf;
527 ops.ooboffs = chip->badblockpos;
528 if (chip->options & NAND_BUSWIDTH_16) {
529 ops.ooboffs &= ~0x01;
530 ops.len = ops.ooblen = 2;
531 } else {
532 ops.len = ops.ooblen = 1;
533 }
534 ops.mode = MTD_OPS_PLACE_OOB;
535
536 /* Write to first/last page(s) if necessary */
537 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
538 ofs += mtd->erasesize - mtd->writesize;
539 do {
540 res = nand_do_write_oob(mtd, ofs, &ops);
541 if (!ret)
542 ret = res;
543
544 i++;
545 ofs += mtd->writesize;
546 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
547
548 return ret;
549}
550
551/**
552 * nand_block_markbad_lowlevel - mark a block bad
553 * @mtd: MTD device structure
554 * @ofs: offset from device start
555 *
556 * This function performs the generic NAND bad block marking steps (i.e., bad
557 * block table(s) and/or marker(s)). We only allow the hardware driver to
558 * specify how to write bad block markers to OOB (chip->block_markbad).
559 *
560 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000561 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200562 * (2) write bad block marker to OOB area of affected block (unless flag
563 * NAND_BBT_NO_OOB_BBM is present)
564 * (3) update the BBT
565 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000566 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200567*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200568static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200569{
William Juul52c07962007-10-31 13:53:06 +0100570 struct nand_chip *chip = mtd->priv;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200571 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200572
Heiko Schocherf5895d12014-06-24 10:10:04 +0200573 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000574 struct erase_info einfo;
575
576 /* Attempt erase before marking OOB */
577 memset(&einfo, 0, sizeof(einfo));
578 einfo.mtd = mtd;
579 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200580 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000581 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200582
Heiko Schocherf5895d12014-06-24 10:10:04 +0200583 /* Write bad block marker to OOB */
584 nand_get_device(mtd, FL_WRITING);
585 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500586 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100587 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000588
Heiko Schocherf5895d12014-06-24 10:10:04 +0200589 /* Mark block bad in BBT */
590 if (chip->bbt) {
591 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000592 if (!ret)
593 ret = res;
594 }
595
William Juul52c07962007-10-31 13:53:06 +0100596 if (!ret)
597 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500598
William Juul52c07962007-10-31 13:53:06 +0100599 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200600}
601
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200602/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000604 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200605 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000606 * Check, if the device is write protected. The function expects, that the
607 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200608 */
William Juul52c07962007-10-31 13:53:06 +0100609static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200610{
William Juul52c07962007-10-31 13:53:06 +0100611 struct nand_chip *chip = mtd->priv;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200612
Sergey Lapin3a38a552013-01-14 03:46:50 +0000613 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200614 if (chip->options & NAND_BROKEN_XD)
615 return 0;
616
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100618 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
619 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200620}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100621
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200622/**
623 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Sergey Lapin3a38a552013-01-14 03:46:50 +0000624 * @mtd: MTD device structure
625 * @ofs: offset from device start
626 * @getchip: 0, if the chip is already selected
627 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200628 *
629 * Check, if the block is bad. Either by reading the bad block table or
630 * calling of the scan function.
631 */
William Juul52c07962007-10-31 13:53:06 +0100632static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
633 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200634{
William Juul52c07962007-10-31 13:53:06 +0100635 struct nand_chip *chip = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200636
Masahiro Yamada8d100542014-12-26 22:20:58 +0900637 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
638 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200639 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900640 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200641 }
642
William Juul52c07962007-10-31 13:53:06 +0100643 if (!chip->bbt)
644 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200645
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200646 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100647 return nand_isbad_bbt(mtd, ofs, allowbbt);
648}
649
Heiko Schocherf5895d12014-06-24 10:10:04 +0200650#ifndef __UBOOT__
651/**
652 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
653 * @mtd: MTD device structure
654 * @timeo: Timeout
655 *
656 * Helper function for nand_wait_ready used when needing to wait in interrupt
657 * context.
658 */
659static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
660{
661 struct nand_chip *chip = mtd->priv;
662 int i;
663
664 /* Wait for the device to get ready */
665 for (i = 0; i < timeo; i++) {
666 if (chip->dev_ready(mtd))
667 break;
668 touch_softlockup_watchdog();
669 mdelay(1);
670 }
671}
672#endif
673
Sergey Lapin3a38a552013-01-14 03:46:50 +0000674/* Wait for the ready pin, after a command. The timeout is caught later. */
William Juul52c07962007-10-31 13:53:06 +0100675void nand_wait_ready(struct mtd_info *mtd)
676{
677 struct nand_chip *chip = mtd->priv;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200678#ifndef __UBOOT__
679 unsigned long timeo = jiffies + msecs_to_jiffies(20);
680
681 /* 400ms timeout */
682 if (in_interrupt() || oops_in_progress)
683 return panic_nand_wait_ready(mtd, 400);
684
685 led_trigger_event(nand_led_trigger, LED_FULL);
686 /* Wait until command is processed or timeout occurs */
687 do {
688 if (chip->dev_ready(mtd))
689 break;
690 touch_softlockup_watchdog();
691 } while (time_before(jiffies, timeo));
692 led_trigger_event(nand_led_trigger, LED_OFF);
693#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200694 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000695 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100696
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000697 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000698 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000699 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100700 if (chip->dev_ready)
701 if (chip->dev_ready(mtd))
702 break;
703 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200704#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200705}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200706EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200707
708/**
709 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000710 * @mtd: MTD device structure
711 * @command: the command to be sent
712 * @column: the column address for this command, -1 if none
713 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200714 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000715 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200716 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200717 */
William Juul52c07962007-10-31 13:53:06 +0100718static void nand_command(struct mtd_info *mtd, unsigned int command,
719 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200720{
William Juul52c07962007-10-31 13:53:06 +0100721 register struct nand_chip *chip = mtd->priv;
722 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyserf9f36222009-02-04 13:47:22 -0600723 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724
Sergey Lapin3a38a552013-01-14 03:46:50 +0000725 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200726 if (command == NAND_CMD_SEQIN) {
727 int readcmd;
728
William Juul52c07962007-10-31 13:53:06 +0100729 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200730 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100731 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732 readcmd = NAND_CMD_READOOB;
733 } else if (column < 256) {
734 /* First 256 bytes --> READ0 */
735 readcmd = NAND_CMD_READ0;
736 } else {
737 column -= 256;
738 readcmd = NAND_CMD_READ1;
739 }
William Juul52c07962007-10-31 13:53:06 +0100740 chip->cmd_ctrl(mtd, readcmd, ctrl);
741 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200742 }
William Juul52c07962007-10-31 13:53:06 +0100743 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200744
Sergey Lapin3a38a552013-01-14 03:46:50 +0000745 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100746 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
747 /* Serially input address */
748 if (column != -1) {
749 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200750 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530751 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100752 column >>= 1;
753 chip->cmd_ctrl(mtd, column, ctrl);
754 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200755 }
William Juul52c07962007-10-31 13:53:06 +0100756 if (page_addr != -1) {
757 chip->cmd_ctrl(mtd, page_addr, ctrl);
758 ctrl &= ~NAND_CTRL_CHANGE;
759 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
760 /* One more address cycle for devices > 32MiB */
761 if (chip->chipsize > (32 << 20))
762 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
763 }
764 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200765
766 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000767 * Program and erase have their own busy handlers status and sequential
768 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100769 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200770 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200771
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200772 case NAND_CMD_PAGEPROG:
773 case NAND_CMD_ERASE1:
774 case NAND_CMD_ERASE2:
775 case NAND_CMD_SEQIN:
776 case NAND_CMD_STATUS:
777 return;
778
779 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100780 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200781 break;
William Juul52c07962007-10-31 13:53:06 +0100782 udelay(chip->chip_delay);
783 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
784 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
785 chip->cmd_ctrl(mtd,
786 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600787 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
788 (rst_sts_cnt--));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200789 return;
790
William Juul52c07962007-10-31 13:53:06 +0100791 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200792 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200793 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200794 * If we don't have access to the busy pin, we apply the given
795 * command delay
William Juul52c07962007-10-31 13:53:06 +0100796 */
797 if (!chip->dev_ready) {
798 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200799 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200800 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200801 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000802 /*
803 * Apply this short delay always to ensure that we do wait tWB in
804 * any case on any machine.
805 */
William Juul52c07962007-10-31 13:53:06 +0100806 ndelay(100);
807
808 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200809}
810
811/**
812 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000813 * @mtd: MTD device structure
814 * @command: the command to be sent
815 * @column: the column address for this command, -1 if none
816 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200817 *
William Juul52c07962007-10-31 13:53:06 +0100818 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000819 * devices. We don't have the separate regions as we have in the small page
820 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200821 */
William Juul52c07962007-10-31 13:53:06 +0100822static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
823 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200824{
William Juul52c07962007-10-31 13:53:06 +0100825 register struct nand_chip *chip = mtd->priv;
Peter Tyserf9f36222009-02-04 13:47:22 -0600826 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200827
828 /* Emulate NAND_CMD_READOOB */
829 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100830 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200831 command = NAND_CMD_READ0;
832 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200833
William Juul52c07962007-10-31 13:53:06 +0100834 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200835 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200836
837 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100838 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200839
840 /* Serially input address */
841 if (column != -1) {
842 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200843 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530844 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200845 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100846 chip->cmd_ctrl(mtd, column, ctrl);
847 ctrl &= ~NAND_CTRL_CHANGE;
848 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200849 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200850 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100851 chip->cmd_ctrl(mtd, page_addr, ctrl);
852 chip->cmd_ctrl(mtd, page_addr >> 8,
853 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200854 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100855 if (chip->chipsize > (128 << 20))
856 chip->cmd_ctrl(mtd, page_addr >> 16,
857 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200858 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200859 }
William Juul52c07962007-10-31 13:53:06 +0100860 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200861
862 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000863 * Program and erase have their own busy handlers status, sequential
864 * in, and deplete1 need no delay.
William Juul52c07962007-10-31 13:53:06 +0100865 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200866 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200867
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200868 case NAND_CMD_CACHEDPROG:
869 case NAND_CMD_PAGEPROG:
870 case NAND_CMD_ERASE1:
871 case NAND_CMD_ERASE2:
872 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100873 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200874 case NAND_CMD_STATUS:
875 return;
876
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200877 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100878 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200879 break;
William Juul52c07962007-10-31 13:53:06 +0100880 udelay(chip->chip_delay);
881 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
882 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
883 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
884 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600885 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
886 (rst_sts_cnt--));
William Juul52c07962007-10-31 13:53:06 +0100887 return;
888
889 case NAND_CMD_RNDOUT:
890 /* No ready / busy check necessary */
891 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
892 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
893 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
894 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200895 return;
896
897 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100898 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
899 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
900 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
901 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200902
William Juul52c07962007-10-31 13:53:06 +0100903 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200904 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200905 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200906 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000907 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100908 */
909 if (!chip->dev_ready) {
910 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200911 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200912 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200913 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200914
Sergey Lapin3a38a552013-01-14 03:46:50 +0000915 /*
916 * Apply this short delay always to ensure that we do wait tWB in
917 * any case on any machine.
918 */
William Juul52c07962007-10-31 13:53:06 +0100919 ndelay(100);
920
921 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200922}
923
924/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200925 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000926 * @chip: the nand chip descriptor
927 * @mtd: MTD device structure
928 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200929 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200930 * Used when in panic, no locks are taken.
931 */
932static void panic_nand_get_device(struct nand_chip *chip,
933 struct mtd_info *mtd, int new_state)
934{
935 /* Hardware controller shared among independent devices */
936 chip->controller->active = chip;
937 chip->state = new_state;
938}
939
940/**
941 * nand_get_device - [GENERIC] Get chip for selected access
942 * @mtd: MTD device structure
943 * @new_state: the state which is requested
944 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200945 * Get the device and lock it for exclusive access
946 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200947static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200948nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100949{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200950 struct nand_chip *chip = mtd->priv;
951#ifndef __UBOOT__
952 spinlock_t *lock = &chip->controller->lock;
953 wait_queue_head_t *wq = &chip->controller->wq;
954 DECLARE_WAITQUEUE(wait, current);
955retry:
956 spin_lock(lock);
957
958 /* Hardware controller shared among independent devices */
959 if (!chip->controller->active)
960 chip->controller->active = chip;
961
962 if (chip->controller->active == chip && chip->state == FL_READY) {
963 chip->state = new_state;
964 spin_unlock(lock);
965 return 0;
966 }
967 if (new_state == FL_PM_SUSPENDED) {
968 if (chip->controller->active->state == FL_PM_SUSPENDED) {
969 chip->state = FL_PM_SUSPENDED;
970 spin_unlock(lock);
971 return 0;
972 }
973 }
974 set_current_state(TASK_UNINTERRUPTIBLE);
975 add_wait_queue(wq, &wait);
976 spin_unlock(lock);
977 schedule();
978 remove_wait_queue(wq, &wait);
979 goto retry;
980#else
Christian Hitzb8a6b372011-10-12 09:32:02 +0200981 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100982 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200983#endif
984}
985
986/**
987 * panic_nand_wait - [GENERIC] wait until the command is done
988 * @mtd: MTD device structure
989 * @chip: NAND chip structure
990 * @timeo: timeout
991 *
992 * Wait for command done. This is a helper function for nand_wait used when
993 * we are in interrupt context. May happen when in panic and trying to write
994 * an oops through mtdoops.
995 */
996static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
997 unsigned long timeo)
998{
999 int i;
1000 for (i = 0; i < timeo; i++) {
1001 if (chip->dev_ready) {
1002 if (chip->dev_ready(mtd))
1003 break;
1004 } else {
1005 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1006 break;
1007 }
1008 mdelay(1);
1009 }
William Juul52c07962007-10-31 13:53:06 +01001010}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001011
1012/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001013 * nand_wait - [DEFAULT] wait until the command is done
1014 * @mtd: MTD device structure
1015 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001016 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001017 * Wait for command done. This applies to erase and program only. Erase can
1018 * take up to 400ms and program up to 20ms according to general NAND and
1019 * SmartMedia specs.
William Juul52c07962007-10-31 13:53:06 +01001020 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001021static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001022{
Wolfgang Denk9a08dd12005-11-02 14:29:12 +01001023
Heiko Schocherf5895d12014-06-24 10:10:04 +02001024 int status, state = chip->state;
1025 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +01001026
Heiko Schocherf5895d12014-06-24 10:10:04 +02001027 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +01001028
Heiko Schocherf5895d12014-06-24 10:10:04 +02001029 /*
1030 * Apply this short delay always to ensure that we do wait tWB in any
1031 * case on any machine.
1032 */
1033 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +01001034
Heiko Schocherf5895d12014-06-24 10:10:04 +02001035 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +01001036
Heiko Schocherf5895d12014-06-24 10:10:04 +02001037#ifndef __UBOOT__
1038 if (in_interrupt() || oops_in_progress)
1039 panic_nand_wait(mtd, chip, timeo);
1040 else {
1041 timeo = jiffies + msecs_to_jiffies(timeo);
1042 while (time_before(jiffies, timeo)) {
1043 if (chip->dev_ready) {
1044 if (chip->dev_ready(mtd))
1045 break;
1046 } else {
1047 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1048 break;
1049 }
1050 cond_resched();
1051 }
1052 }
1053#else
1054 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
1055 u32 time_start;
1056
1057 time_start = get_timer(0);
1058 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001059 if (chip->dev_ready) {
1060 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +01001061 break;
1062 } else {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001063 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +01001064 break;
1065 }
1066 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001067#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02001068 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +01001069
Heiko Schocherf5895d12014-06-24 10:10:04 +02001070 status = (int)chip->read_byte(mtd);
1071 /* This can happen if in case of timeout or buggy dev_ready */
1072 WARN_ON(!(status & NAND_STATUS_READY));
1073 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001074}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001075
Heiko Schocherf5895d12014-06-24 10:10:04 +02001076#ifndef __UBOOT__
1077/**
1078 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1079 * @mtd: mtd info
1080 * @ofs: offset to start unlock from
1081 * @len: length to unlock
1082 * @invert: when = 0, unlock the range of blocks within the lower and
1083 * upper boundary address
1084 * when = 1, unlock the range of blocks outside the boundaries
1085 * of the lower and upper boundary address
1086 *
1087 * Returs unlock status.
1088 */
1089static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1090 uint64_t len, int invert)
1091{
1092 int ret = 0;
1093 int status, page;
1094 struct nand_chip *chip = mtd->priv;
1095
1096 /* Submit address of first page to unlock */
1097 page = ofs >> chip->page_shift;
1098 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1099
1100 /* Submit address of last page to unlock */
1101 page = (ofs + len) >> chip->page_shift;
1102 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1103 (page | invert) & chip->pagemask);
1104
1105 /* Call wait ready function */
1106 status = chip->waitfunc(mtd, chip);
1107 /* See if device thinks it succeeded */
1108 if (status & NAND_STATUS_FAIL) {
1109 pr_debug("%s: error status = 0x%08x\n",
1110 __func__, status);
1111 ret = -EIO;
1112 }
1113
1114 return ret;
1115}
1116
1117/**
1118 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1119 * @mtd: mtd info
1120 * @ofs: offset to start unlock from
1121 * @len: length to unlock
1122 *
1123 * Returns unlock status.
1124 */
1125int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1126{
1127 int ret = 0;
1128 int chipnr;
1129 struct nand_chip *chip = mtd->priv;
1130
1131 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1132 __func__, (unsigned long long)ofs, len);
1133
1134 if (check_offs_len(mtd, ofs, len))
1135 ret = -EINVAL;
1136
1137 /* Align to last block address if size addresses end of the device */
1138 if (ofs + len == mtd->size)
1139 len -= mtd->erasesize;
1140
1141 nand_get_device(mtd, FL_UNLOCKING);
1142
1143 /* Shift to get chip number */
1144 chipnr = ofs >> chip->chip_shift;
1145
1146 chip->select_chip(mtd, chipnr);
1147
1148 /* Check, if it is write protected */
1149 if (nand_check_wp(mtd)) {
1150 pr_debug("%s: device is write protected!\n",
1151 __func__);
1152 ret = -EIO;
1153 goto out;
1154 }
1155
1156 ret = __nand_unlock(mtd, ofs, len, 0);
1157
1158out:
1159 chip->select_chip(mtd, -1);
1160 nand_release_device(mtd);
1161
1162 return ret;
1163}
1164EXPORT_SYMBOL(nand_unlock);
1165
1166/**
1167 * nand_lock - [REPLACEABLE] locks all blocks present in the device
1168 * @mtd: mtd info
1169 * @ofs: offset to start unlock from
1170 * @len: length to unlock
1171 *
1172 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1173 * have this feature, but it allows only to lock all blocks, not for specified
1174 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1175 * now.
1176 *
1177 * Returns lock status.
1178 */
1179int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1180{
1181 int ret = 0;
1182 int chipnr, status, page;
1183 struct nand_chip *chip = mtd->priv;
1184
1185 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1186 __func__, (unsigned long long)ofs, len);
1187
1188 if (check_offs_len(mtd, ofs, len))
1189 ret = -EINVAL;
1190
1191 nand_get_device(mtd, FL_LOCKING);
1192
1193 /* Shift to get chip number */
1194 chipnr = ofs >> chip->chip_shift;
1195
1196 chip->select_chip(mtd, chipnr);
1197
1198 /* Check, if it is write protected */
1199 if (nand_check_wp(mtd)) {
1200 pr_debug("%s: device is write protected!\n",
1201 __func__);
1202 status = MTD_ERASE_FAILED;
1203 ret = -EIO;
1204 goto out;
1205 }
1206
1207 /* Submit address of first page to lock */
1208 page = ofs >> chip->page_shift;
1209 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1210
1211 /* Call wait ready function */
1212 status = chip->waitfunc(mtd, chip);
1213 /* See if device thinks it succeeded */
1214 if (status & NAND_STATUS_FAIL) {
1215 pr_debug("%s: error status = 0x%08x\n",
1216 __func__, status);
1217 ret = -EIO;
1218 goto out;
1219 }
1220
1221 ret = __nand_unlock(mtd, ofs, len, 0x1);
1222
1223out:
1224 chip->select_chip(mtd, -1);
1225 nand_release_device(mtd);
1226
1227 return ret;
1228}
1229EXPORT_SYMBOL(nand_lock);
1230#endif
1231
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001232/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001233 * nand_read_page_raw - [INTERN] read raw page data without ecc
1234 * @mtd: mtd info structure
1235 * @chip: nand chip info structure
1236 * @buf: buffer to store read data
1237 * @oob_required: caller requires OOB data read to chip->oob_poi
1238 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001239 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001240 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001241 */
William Juul52c07962007-10-31 13:53:06 +01001242static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001243 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001244{
William Juul52c07962007-10-31 13:53:06 +01001245 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001246 if (oob_required)
1247 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01001248 return 0;
1249}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001250
William Juul52c07962007-10-31 13:53:06 +01001251/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001252 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1253 * @mtd: mtd info structure
1254 * @chip: nand chip info structure
1255 * @buf: buffer to store read data
1256 * @oob_required: caller requires OOB data read to chip->oob_poi
1257 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001258 *
1259 * We need a special oob layout and handling even when OOB isn't used.
1260 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001261static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001262 struct nand_chip *chip, uint8_t *buf,
1263 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001264{
1265 int eccsize = chip->ecc.size;
1266 int eccbytes = chip->ecc.bytes;
1267 uint8_t *oob = chip->oob_poi;
1268 int steps, size;
1269
1270 for (steps = chip->ecc.steps; steps > 0; steps--) {
1271 chip->read_buf(mtd, buf, eccsize);
1272 buf += eccsize;
1273
1274 if (chip->ecc.prepad) {
1275 chip->read_buf(mtd, oob, chip->ecc.prepad);
1276 oob += chip->ecc.prepad;
1277 }
1278
1279 chip->read_buf(mtd, oob, eccbytes);
1280 oob += eccbytes;
1281
1282 if (chip->ecc.postpad) {
1283 chip->read_buf(mtd, oob, chip->ecc.postpad);
1284 oob += chip->ecc.postpad;
1285 }
1286 }
1287
1288 size = mtd->oobsize - (oob - chip->oob_poi);
1289 if (size)
1290 chip->read_buf(mtd, oob, size);
1291
1292 return 0;
1293}
1294
1295/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001296 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1297 * @mtd: mtd info structure
1298 * @chip: nand chip info structure
1299 * @buf: buffer to store read data
1300 * @oob_required: caller requires OOB data read to chip->oob_poi
1301 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001302 */
1303static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001304 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001305{
1306 int i, eccsize = chip->ecc.size;
1307 int eccbytes = chip->ecc.bytes;
1308 int eccsteps = chip->ecc.steps;
1309 uint8_t *p = buf;
1310 uint8_t *ecc_calc = chip->buffers->ecccalc;
1311 uint8_t *ecc_code = chip->buffers->ecccode;
1312 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001313 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001314
Sergey Lapin3a38a552013-01-14 03:46:50 +00001315 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001316
William Juul52c07962007-10-31 13:53:06 +01001317 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1318 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001319
William Juul52c07962007-10-31 13:53:06 +01001320 for (i = 0; i < chip->ecc.total; i++)
1321 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001322
William Juul52c07962007-10-31 13:53:06 +01001323 eccsteps = chip->ecc.steps;
1324 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001325
William Juul52c07962007-10-31 13:53:06 +01001326 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1327 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001328
William Juul52c07962007-10-31 13:53:06 +01001329 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001330 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001331 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001332 } else {
William Juul52c07962007-10-31 13:53:06 +01001333 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001334 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1335 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001336 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001337 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001338}
1339
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001340/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001341 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001342 * @mtd: mtd info structure
1343 * @chip: nand chip info structure
1344 * @data_offs: offset of requested data within the page
1345 * @readlen: data length
1346 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001347 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001348 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001349static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001350 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1351 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001352{
1353 int start_step, end_step, num_steps;
1354 uint32_t *eccpos = chip->ecc.layout->eccpos;
1355 uint8_t *p;
1356 int data_col_addr, i, gaps = 0;
1357 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1358 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001359 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001360 unsigned int max_bitflips = 0;
Scott Wood3628f002008-10-24 16:20:43 -05001361
Sergey Lapin3a38a552013-01-14 03:46:50 +00001362 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001363 start_step = data_offs / chip->ecc.size;
1364 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1365 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001366 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001367
Sergey Lapin3a38a552013-01-14 03:46:50 +00001368 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001369 datafrag_len = num_steps * chip->ecc.size;
1370 eccfrag_len = num_steps * chip->ecc.bytes;
1371
1372 data_col_addr = start_step * chip->ecc.size;
1373 /* If we read not a page aligned data */
1374 if (data_col_addr != 0)
1375 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1376
1377 p = bufpoi + data_col_addr;
1378 chip->read_buf(mtd, p, datafrag_len);
1379
Sergey Lapin3a38a552013-01-14 03:46:50 +00001380 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001381 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1382 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1383
Sergey Lapin3a38a552013-01-14 03:46:50 +00001384 /*
1385 * The performance is faster if we position offsets according to
1386 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1387 */
Scott Wood3628f002008-10-24 16:20:43 -05001388 for (i = 0; i < eccfrag_len - 1; i++) {
1389 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1390 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1391 gaps = 1;
1392 break;
1393 }
1394 }
1395 if (gaps) {
1396 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1397 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1398 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001399 /*
1400 * Send the command to read the particular ECC bytes take care
1401 * about buswidth alignment in read_buf.
1402 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001403 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001404 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001405 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001406 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001407 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001408 aligned_len++;
1409
Christian Hitzb8a6b372011-10-12 09:32:02 +02001410 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1411 mtd->writesize + aligned_pos, -1);
Scott Wood3628f002008-10-24 16:20:43 -05001412 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1413 }
1414
1415 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001416 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001417
1418 p = bufpoi + data_col_addr;
1419 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1420 int stat;
1421
Christian Hitzb8a6b372011-10-12 09:32:02 +02001422 stat = chip->ecc.correct(mtd, p,
1423 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001424 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001425 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001426 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001427 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001428 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1429 }
Scott Wood3628f002008-10-24 16:20:43 -05001430 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001431 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05001432}
1433
1434/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001435 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1436 * @mtd: mtd info structure
1437 * @chip: nand chip info structure
1438 * @buf: buffer to store read data
1439 * @oob_required: caller requires OOB data read to chip->oob_poi
1440 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001441 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001442 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001443 */
William Juul52c07962007-10-31 13:53:06 +01001444static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001445 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001446{
William Juul52c07962007-10-31 13:53:06 +01001447 int i, eccsize = chip->ecc.size;
1448 int eccbytes = chip->ecc.bytes;
1449 int eccsteps = chip->ecc.steps;
1450 uint8_t *p = buf;
1451 uint8_t *ecc_calc = chip->buffers->ecccalc;
1452 uint8_t *ecc_code = chip->buffers->ecccode;
1453 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001454 unsigned int max_bitflips = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001455
William Juul52c07962007-10-31 13:53:06 +01001456 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1457 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1458 chip->read_buf(mtd, p, eccsize);
1459 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1460 }
1461 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001462
William Juul52c07962007-10-31 13:53:06 +01001463 for (i = 0; i < chip->ecc.total; i++)
1464 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001465
William Juul52c07962007-10-31 13:53:06 +01001466 eccsteps = chip->ecc.steps;
1467 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001468
William Juul52c07962007-10-31 13:53:06 +01001469 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1470 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001471
William Juul52c07962007-10-31 13:53:06 +01001472 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001473 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001474 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001475 } else {
William Juul52c07962007-10-31 13:53:06 +01001476 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001477 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1478 }
William Juul52c07962007-10-31 13:53:06 +01001479 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001480 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001481}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001482
William Juul52c07962007-10-31 13:53:06 +01001483/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001484 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1485 * @mtd: mtd info structure
1486 * @chip: nand chip info structure
1487 * @buf: buffer to store read data
1488 * @oob_required: caller requires OOB data read to chip->oob_poi
1489 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001490 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001491 * Hardware ECC for large page chips, require OOB to be read first. For this
1492 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1493 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1494 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1495 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001496 */
1497static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001498 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001499{
1500 int i, eccsize = chip->ecc.size;
1501 int eccbytes = chip->ecc.bytes;
1502 int eccsteps = chip->ecc.steps;
1503 uint8_t *p = buf;
1504 uint8_t *ecc_code = chip->buffers->ecccode;
1505 uint32_t *eccpos = chip->ecc.layout->eccpos;
1506 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001507 unsigned int max_bitflips = 0;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001508
1509 /* Read the OOB area first */
1510 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1511 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1512 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1513
1514 for (i = 0; i < chip->ecc.total; i++)
1515 ecc_code[i] = chip->oob_poi[eccpos[i]];
1516
1517 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1518 int stat;
1519
1520 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1521 chip->read_buf(mtd, p, eccsize);
1522 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1523
1524 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001525 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001526 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001527 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001528 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001529 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1530 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001531 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001532 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001533}
1534
1535/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001536 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1537 * @mtd: mtd info structure
1538 * @chip: nand chip info structure
1539 * @buf: buffer to store read data
1540 * @oob_required: caller requires OOB data read to chip->oob_poi
1541 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001542 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001543 * The hw generator calculates the error syndrome automatically. Therefore we
1544 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01001545 */
1546static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001547 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001548{
1549 int i, eccsize = chip->ecc.size;
1550 int eccbytes = chip->ecc.bytes;
1551 int eccsteps = chip->ecc.steps;
1552 uint8_t *p = buf;
1553 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001554 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01001555
1556 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1557 int stat;
1558
1559 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1560 chip->read_buf(mtd, p, eccsize);
1561
1562 if (chip->ecc.prepad) {
1563 chip->read_buf(mtd, oob, chip->ecc.prepad);
1564 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001565 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001566
William Juul52c07962007-10-31 13:53:06 +01001567 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1568 chip->read_buf(mtd, oob, eccbytes);
1569 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001570
Heiko Schocherf5895d12014-06-24 10:10:04 +02001571 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001572 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001573 } else {
William Juul52c07962007-10-31 13:53:06 +01001574 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001575 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1576 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001577
William Juul52c07962007-10-31 13:53:06 +01001578 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001579
William Juul52c07962007-10-31 13:53:06 +01001580 if (chip->ecc.postpad) {
1581 chip->read_buf(mtd, oob, chip->ecc.postpad);
1582 oob += chip->ecc.postpad;
1583 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001584 }
William Juul52c07962007-10-31 13:53:06 +01001585
1586 /* Calculate remaining oob bytes */
1587 i = mtd->oobsize - (oob - chip->oob_poi);
1588 if (i)
1589 chip->read_buf(mtd, oob, i);
1590
Heiko Schocherf5895d12014-06-24 10:10:04 +02001591 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001592}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001593
1594/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001595 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1596 * @chip: nand chip structure
1597 * @oob: oob destination address
1598 * @ops: oob ops structure
1599 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01001600 */
1601static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1602 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001603{
Christian Hitz13fc0e22011-10-12 09:32:01 +02001604 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01001605
Sergey Lapin3a38a552013-01-14 03:46:50 +00001606 case MTD_OPS_PLACE_OOB:
1607 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001608 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1609 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001610
Sergey Lapin3a38a552013-01-14 03:46:50 +00001611 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01001612 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1613 uint32_t boffs = 0, roffs = ops->ooboffs;
1614 size_t bytes = 0;
1615
Christian Hitz13fc0e22011-10-12 09:32:01 +02001616 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001617 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01001618 if (unlikely(roffs)) {
1619 if (roffs >= free->length) {
1620 roffs -= free->length;
1621 continue;
1622 }
1623 boffs = free->offset + roffs;
1624 bytes = min_t(size_t, len,
1625 (free->length - roffs));
1626 roffs = 0;
1627 } else {
1628 bytes = min_t(size_t, len, free->length);
1629 boffs = free->offset;
1630 }
1631 memcpy(oob, chip->oob_poi + boffs, bytes);
1632 oob += bytes;
1633 }
1634 return oob;
1635 }
1636 default:
1637 BUG();
1638 }
1639 return NULL;
1640}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001641
1642/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001643 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1644 * @mtd: MTD device structure
1645 * @retry_mode: the retry mode to use
1646 *
1647 * Some vendors supply a special command to shift the Vt threshold, to be used
1648 * when there are too many bitflips in a page (i.e., ECC error). After setting
1649 * a new threshold, the host should retry reading the page.
1650 */
1651static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1652{
1653 struct nand_chip *chip = mtd->priv;
1654
1655 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1656
1657 if (retry_mode >= chip->read_retries)
1658 return -EINVAL;
1659
1660 if (!chip->setup_read_retry)
1661 return -EOPNOTSUPP;
1662
1663 return chip->setup_read_retry(mtd, retry_mode);
1664}
1665
1666/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001667 * nand_do_read_ops - [INTERN] Read data with ECC
1668 * @mtd: MTD device structure
1669 * @from: offset to read from
1670 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001671 *
William Juul52c07962007-10-31 13:53:06 +01001672 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001673 */
William Juul52c07962007-10-31 13:53:06 +01001674static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1675 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001676{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001677 int chipnr, page, realpage, col, bytes, aligned, oob_required;
William Juul52c07962007-10-31 13:53:06 +01001678 struct nand_chip *chip = mtd->priv;
William Juul52c07962007-10-31 13:53:06 +01001679 int ret = 0;
1680 uint32_t readlen = ops->len;
1681 uint32_t oobreadlen = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001682 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitzb8a6b372011-10-12 09:32:02 +02001683 mtd->oobavail : mtd->oobsize;
1684
William Juul52c07962007-10-31 13:53:06 +01001685 uint8_t *bufpoi, *oob, *buf;
Paul Burton700a76c2013-09-04 15:16:56 +01001686 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001687 int retry_mode = 0;
1688 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001689
William Juul52c07962007-10-31 13:53:06 +01001690 chipnr = (int)(from >> chip->chip_shift);
1691 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001692
William Juul52c07962007-10-31 13:53:06 +01001693 realpage = (int)(from >> chip->page_shift);
1694 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001695
William Juul52c07962007-10-31 13:53:06 +01001696 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001697
William Juul52c07962007-10-31 13:53:06 +01001698 buf = ops->datbuf;
1699 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001700 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001701
Christian Hitz13fc0e22011-10-12 09:32:01 +02001702 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001703 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06001704
Heiko Schocherf5895d12014-06-24 10:10:04 +02001705 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01001706 bytes = min(mtd->writesize - col, readlen);
1707 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001708
Sergey Lapin3a38a552013-01-14 03:46:50 +00001709 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01001710 if (realpage != chip->pagebuf || oob) {
1711 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001712
Heiko Schocherf5895d12014-06-24 10:10:04 +02001713read_retry:
Sergey Lapin3a38a552013-01-14 03:46:50 +00001714 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001715
Paul Burton700a76c2013-09-04 15:16:56 +01001716 /*
1717 * Now read the page into the buffer. Absent an error,
1718 * the read methods return max bitflips per ecc step.
1719 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001720 if (unlikely(ops->mode == MTD_OPS_RAW))
1721 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1722 oob_required,
1723 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001724 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001725 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02001726 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001727 col, bytes, bufpoi,
1728 page);
William Juul52c07962007-10-31 13:53:06 +01001729 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001730 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001731 oob_required, page);
1732 if (ret < 0) {
1733 if (!aligned)
1734 /* Invalidate page cache */
1735 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01001736 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001737 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001738
Paul Burton700a76c2013-09-04 15:16:56 +01001739 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1740
William Juul52c07962007-10-31 13:53:06 +01001741 /* Transfer not aligned data */
1742 if (!aligned) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001743 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001744 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01001745 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05001746 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01001747 chip->pagebuf_bitflips = ret;
1748 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001749 /* Invalidate page cache */
1750 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01001751 }
William Juul52c07962007-10-31 13:53:06 +01001752 memcpy(buf, chip->buffers->databuf + col, bytes);
1753 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001754
William Juul52c07962007-10-31 13:53:06 +01001755 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001756 int toread = min(oobreadlen, max_oobsize);
1757
1758 if (toread) {
1759 oob = nand_transfer_oob(chip,
1760 oob, ops, toread);
1761 oobreadlen -= toread;
1762 }
William Juul52c07962007-10-31 13:53:06 +01001763 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001764
1765 if (chip->options & NAND_NEED_READRDY) {
1766 /* Apply delay or wait for ready/busy pin */
1767 if (!chip->dev_ready)
1768 udelay(chip->chip_delay);
1769 else
1770 nand_wait_ready(mtd);
1771 }
1772
1773 if (mtd->ecc_stats.failed - ecc_failures) {
1774 if (retry_mode + 1 < chip->read_retries) {
1775 retry_mode++;
1776 ret = nand_setup_read_retry(mtd,
1777 retry_mode);
1778 if (ret < 0)
1779 break;
1780
1781 /* Reset failures; retry */
1782 mtd->ecc_stats.failed = ecc_failures;
1783 goto read_retry;
1784 } else {
1785 /* No more retry modes; real failure */
1786 ecc_fail = true;
1787 }
1788 }
1789
1790 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01001791 } else {
1792 memcpy(buf, chip->buffers->databuf + col, bytes);
1793 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01001794 max_bitflips = max_t(unsigned int, max_bitflips,
1795 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01001796 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001797
William Juul52c07962007-10-31 13:53:06 +01001798 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001799
Heiko Schocherf5895d12014-06-24 10:10:04 +02001800 /* Reset to retry mode 0 */
1801 if (retry_mode) {
1802 ret = nand_setup_read_retry(mtd, 0);
1803 if (ret < 0)
1804 break;
1805 retry_mode = 0;
1806 }
1807
William Juul52c07962007-10-31 13:53:06 +01001808 if (!readlen)
1809 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001810
Sergey Lapin3a38a552013-01-14 03:46:50 +00001811 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01001812 col = 0;
1813 /* Increment page address */
1814 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001815
William Juul52c07962007-10-31 13:53:06 +01001816 page = realpage & chip->pagemask;
1817 /* Check, if we cross a chip boundary */
1818 if (!page) {
1819 chipnr++;
1820 chip->select_chip(mtd, -1);
1821 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001822 }
William Juul52c07962007-10-31 13:53:06 +01001823 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001824 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001825
William Juul52c07962007-10-31 13:53:06 +01001826 ops->retlen = ops->len - (size_t) readlen;
1827 if (oob)
1828 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001829
Heiko Schocherf5895d12014-06-24 10:10:04 +02001830 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01001831 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001832
Heiko Schocherf5895d12014-06-24 10:10:04 +02001833 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01001834 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001835
Paul Burton700a76c2013-09-04 15:16:56 +01001836 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001837}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001838
William Juul52c07962007-10-31 13:53:06 +01001839/**
Christian Hitz13fc0e22011-10-12 09:32:01 +02001840 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapin3a38a552013-01-14 03:46:50 +00001841 * @mtd: MTD device structure
1842 * @from: offset to read from
1843 * @len: number of bytes to read
1844 * @retlen: pointer to variable to store the number of read bytes
1845 * @buf: the databuffer to put data
William Juul52c07962007-10-31 13:53:06 +01001846 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001847 * Get hold of the chip and call nand_do_read.
William Juul52c07962007-10-31 13:53:06 +01001848 */
1849static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1850 size_t *retlen, uint8_t *buf)
1851{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001852 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01001853 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001854
Heiko Schocherf5895d12014-06-24 10:10:04 +02001855 nand_get_device(mtd, FL_READING);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001856 ops.len = len;
1857 ops.datbuf = buf;
1858 ops.oobbuf = NULL;
1859 ops.mode = MTD_OPS_PLACE_OOB;
1860 ret = nand_do_read_ops(mtd, from, &ops);
1861 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01001862 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001863 return ret;
1864}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001865
William Juul52c07962007-10-31 13:53:06 +01001866/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001867 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1868 * @mtd: mtd info structure
1869 * @chip: nand chip info structure
1870 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001871 */
1872static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001873 int page)
William Juul52c07962007-10-31 13:53:06 +01001874{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001875 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juul52c07962007-10-31 13:53:06 +01001876 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001877 return 0;
William Juul52c07962007-10-31 13:53:06 +01001878}
1879
1880/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001881 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01001882 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00001883 * @mtd: mtd info structure
1884 * @chip: nand chip info structure
1885 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001886 */
1887static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001888 int page)
William Juul52c07962007-10-31 13:53:06 +01001889{
1890 uint8_t *buf = chip->oob_poi;
1891 int length = mtd->oobsize;
1892 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1893 int eccsize = chip->ecc.size;
1894 uint8_t *bufpoi = buf;
1895 int i, toread, sndrnd = 0, pos;
1896
1897 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1898 for (i = 0; i < chip->ecc.steps; i++) {
1899 if (sndrnd) {
1900 pos = eccsize + i * (eccsize + chunk);
1901 if (mtd->writesize > 512)
1902 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1903 else
1904 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001905 } else
William Juul52c07962007-10-31 13:53:06 +01001906 sndrnd = 1;
1907 toread = min_t(int, length, chunk);
1908 chip->read_buf(mtd, bufpoi, toread);
1909 bufpoi += toread;
1910 length -= toread;
1911 }
1912 if (length > 0)
1913 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001914
Sergey Lapin3a38a552013-01-14 03:46:50 +00001915 return 0;
William Juul52c07962007-10-31 13:53:06 +01001916}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001917
William Juul52c07962007-10-31 13:53:06 +01001918/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001919 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1920 * @mtd: mtd info structure
1921 * @chip: nand chip info structure
1922 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001923 */
1924static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1925 int page)
1926{
1927 int status = 0;
1928 const uint8_t *buf = chip->oob_poi;
1929 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001930
William Juul52c07962007-10-31 13:53:06 +01001931 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1932 chip->write_buf(mtd, buf, length);
1933 /* Send command to program the OOB data */
1934 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001935
William Juul52c07962007-10-31 13:53:06 +01001936 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001937
William Juul52c07962007-10-31 13:53:06 +01001938 return status & NAND_STATUS_FAIL ? -EIO : 0;
1939}
1940
1941/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001942 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1943 * with syndrome - only for large page flash
1944 * @mtd: mtd info structure
1945 * @chip: nand chip info structure
1946 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001947 */
1948static int nand_write_oob_syndrome(struct mtd_info *mtd,
1949 struct nand_chip *chip, int page)
1950{
1951 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1952 int eccsize = chip->ecc.size, length = mtd->oobsize;
1953 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1954 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001955
1956 /*
William Juul52c07962007-10-31 13:53:06 +01001957 * data-ecc-data-ecc ... ecc-oob
1958 * or
1959 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001960 */
William Juul52c07962007-10-31 13:53:06 +01001961 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1962 pos = steps * (eccsize + chunk);
1963 steps = 0;
1964 } else
1965 pos = eccsize;
1966
1967 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1968 for (i = 0; i < steps; i++) {
1969 if (sndcmd) {
1970 if (mtd->writesize <= 512) {
1971 uint32_t fill = 0xFFFFFFFF;
1972
1973 len = eccsize;
1974 while (len > 0) {
1975 int num = min_t(int, len, 4);
1976 chip->write_buf(mtd, (uint8_t *)&fill,
1977 num);
1978 len -= num;
1979 }
1980 } else {
1981 pos = eccsize + i * (eccsize + chunk);
1982 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1983 }
1984 } else
1985 sndcmd = 1;
1986 len = min_t(int, length, chunk);
1987 chip->write_buf(mtd, bufpoi, len);
1988 bufpoi += len;
1989 length -= len;
1990 }
1991 if (length > 0)
1992 chip->write_buf(mtd, bufpoi, length);
1993
1994 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1995 status = chip->waitfunc(mtd, chip);
1996
1997 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001998}
1999
2000/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002001 * nand_do_read_oob - [INTERN] NAND read out-of-band
2002 * @mtd: MTD device structure
2003 * @from: offset to read from
2004 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002005 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002006 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002007 */
William Juul52c07962007-10-31 13:53:06 +01002008static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2009 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002010{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002011 int page, realpage, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002012 struct nand_chip *chip = mtd->priv;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002013 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002014 int readlen = ops->ooblen;
2015 int len;
2016 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002017 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002018
Heiko Schocherf5895d12014-06-24 10:10:04 +02002019 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002020 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002021
Sergey Lapin3a38a552013-01-14 03:46:50 +00002022 stats = mtd->ecc_stats;
2023
2024 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juul52c07962007-10-31 13:53:06 +01002025 len = chip->ecc.layout->oobavail;
2026 else
2027 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002028
William Juul52c07962007-10-31 13:53:06 +01002029 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002030 pr_debug("%s: attempt to start read outside oob\n",
2031 __func__);
William Juul52c07962007-10-31 13:53:06 +01002032 return -EINVAL;
2033 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002034
2035 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002036 if (unlikely(from >= mtd->size ||
2037 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2038 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002039 pr_debug("%s: attempt to read beyond end of device\n",
2040 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002041 return -EINVAL;
2042 }
2043
William Juul52c07962007-10-31 13:53:06 +01002044 chipnr = (int)(from >> chip->chip_shift);
2045 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002046
William Juul52c07962007-10-31 13:53:06 +01002047 /* Shift to get page */
2048 realpage = (int)(from >> chip->page_shift);
2049 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002050
Christian Hitz13fc0e22011-10-12 09:32:01 +02002051 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002052 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002053
Sergey Lapin3a38a552013-01-14 03:46:50 +00002054 if (ops->mode == MTD_OPS_RAW)
2055 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2056 else
2057 ret = chip->ecc.read_oob(mtd, chip, page);
2058
2059 if (ret < 0)
2060 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002061
William Juul52c07962007-10-31 13:53:06 +01002062 len = min(len, readlen);
2063 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002064
Heiko Schocherf5895d12014-06-24 10:10:04 +02002065 if (chip->options & NAND_NEED_READRDY) {
2066 /* Apply delay or wait for ready/busy pin */
2067 if (!chip->dev_ready)
2068 udelay(chip->chip_delay);
2069 else
2070 nand_wait_ready(mtd);
2071 }
2072
William Juul52c07962007-10-31 13:53:06 +01002073 readlen -= len;
2074 if (!readlen)
2075 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002076
William Juul52c07962007-10-31 13:53:06 +01002077 /* Increment page address */
2078 realpage++;
2079
2080 page = realpage & chip->pagemask;
2081 /* Check, if we cross a chip boundary */
2082 if (!page) {
2083 chipnr++;
2084 chip->select_chip(mtd, -1);
2085 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002086 }
William Juul52c07962007-10-31 13:53:06 +01002087 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002088 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002089
Sergey Lapin3a38a552013-01-14 03:46:50 +00002090 ops->oobretlen = ops->ooblen - readlen;
2091
2092 if (ret < 0)
2093 return ret;
2094
2095 if (mtd->ecc_stats.failed - stats.failed)
2096 return -EBADMSG;
2097
2098 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002099}
2100
2101/**
William Juul52c07962007-10-31 13:53:06 +01002102 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002103 * @mtd: MTD device structure
2104 * @from: offset to read from
2105 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002106 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002107 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002108 */
William Juul52c07962007-10-31 13:53:06 +01002109static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2110 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002111{
William Juul52c07962007-10-31 13:53:06 +01002112 int ret = -ENOTSUPP;
2113
2114 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002115
2116 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002117 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002118 pr_debug("%s: attempt to read beyond end of device\n",
2119 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002120 return -EINVAL;
2121 }
2122
Heiko Schocherf5895d12014-06-24 10:10:04 +02002123 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002124
Christian Hitz13fc0e22011-10-12 09:32:01 +02002125 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002126 case MTD_OPS_PLACE_OOB:
2127 case MTD_OPS_AUTO_OOB:
2128 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002129 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002130
William Juul52c07962007-10-31 13:53:06 +01002131 default:
2132 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002133 }
2134
William Juul52c07962007-10-31 13:53:06 +01002135 if (!ops->datbuf)
2136 ret = nand_do_read_oob(mtd, from, ops);
2137 else
2138 ret = nand_do_read_ops(mtd, from, ops);
2139
Christian Hitz13fc0e22011-10-12 09:32:01 +02002140out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002141 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002142 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002143}
2144
2145
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002146/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002147 * nand_write_page_raw - [INTERN] raw page write function
2148 * @mtd: mtd info structure
2149 * @chip: nand chip info structure
2150 * @buf: data buffer
2151 * @oob_required: must write chip->oob_poi to OOB
David Brownellee86b8d2009-11-07 16:27:01 -05002152 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002153 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002154 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002155static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2156 const uint8_t *buf, int oob_required)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002157{
William Juul52c07962007-10-31 13:53:06 +01002158 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002159 if (oob_required)
2160 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2161
2162 return 0;
William Juul52c07962007-10-31 13:53:06 +01002163}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002164
William Juul52c07962007-10-31 13:53:06 +01002165/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002166 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2167 * @mtd: mtd info structure
2168 * @chip: nand chip info structure
2169 * @buf: data buffer
2170 * @oob_required: must write chip->oob_poi to OOB
David Brownellee86b8d2009-11-07 16:27:01 -05002171 *
2172 * We need a special oob layout and handling even when ECC isn't checked.
2173 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002174static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002175 struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002176 const uint8_t *buf, int oob_required)
David Brownellee86b8d2009-11-07 16:27:01 -05002177{
2178 int eccsize = chip->ecc.size;
2179 int eccbytes = chip->ecc.bytes;
2180 uint8_t *oob = chip->oob_poi;
2181 int steps, size;
2182
2183 for (steps = chip->ecc.steps; steps > 0; steps--) {
2184 chip->write_buf(mtd, buf, eccsize);
2185 buf += eccsize;
2186
2187 if (chip->ecc.prepad) {
2188 chip->write_buf(mtd, oob, chip->ecc.prepad);
2189 oob += chip->ecc.prepad;
2190 }
2191
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002192 chip->write_buf(mtd, oob, eccbytes);
David Brownellee86b8d2009-11-07 16:27:01 -05002193 oob += eccbytes;
2194
2195 if (chip->ecc.postpad) {
2196 chip->write_buf(mtd, oob, chip->ecc.postpad);
2197 oob += chip->ecc.postpad;
2198 }
2199 }
2200
2201 size = mtd->oobsize - (oob - chip->oob_poi);
2202 if (size)
2203 chip->write_buf(mtd, oob, size);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002204
2205 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002206}
2207/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002208 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2209 * @mtd: mtd info structure
2210 * @chip: nand chip info structure
2211 * @buf: data buffer
2212 * @oob_required: must write chip->oob_poi to OOB
William Juul52c07962007-10-31 13:53:06 +01002213 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002214static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2215 const uint8_t *buf, int oob_required)
William Juul52c07962007-10-31 13:53:06 +01002216{
2217 int i, eccsize = chip->ecc.size;
2218 int eccbytes = chip->ecc.bytes;
2219 int eccsteps = chip->ecc.steps;
2220 uint8_t *ecc_calc = chip->buffers->ecccalc;
2221 const uint8_t *p = buf;
2222 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002223
Sergey Lapin3a38a552013-01-14 03:46:50 +00002224 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002225 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2226 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002227
William Juul52c07962007-10-31 13:53:06 +01002228 for (i = 0; i < chip->ecc.total; i++)
2229 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002230
Sergey Lapin3a38a552013-01-14 03:46:50 +00002231 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002232}
2233
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002234/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002235 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2236 * @mtd: mtd info structure
2237 * @chip: nand chip info structure
2238 * @buf: data buffer
2239 * @oob_required: must write chip->oob_poi to OOB
William Juul52c07962007-10-31 13:53:06 +01002240 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002241static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2242 const uint8_t *buf, int oob_required)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002243{
William Juul52c07962007-10-31 13:53:06 +01002244 int i, eccsize = chip->ecc.size;
2245 int eccbytes = chip->ecc.bytes;
2246 int eccsteps = chip->ecc.steps;
2247 uint8_t *ecc_calc = chip->buffers->ecccalc;
2248 const uint8_t *p = buf;
2249 uint32_t *eccpos = chip->ecc.layout->eccpos;
2250
2251 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2252 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2253 chip->write_buf(mtd, p, eccsize);
2254 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2255 }
2256
2257 for (i = 0; i < chip->ecc.total; i++)
2258 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2259
2260 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002261
2262 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002263}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002264
Heiko Schocherf5895d12014-06-24 10:10:04 +02002265
2266/**
2267 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2268 * @mtd: mtd info structure
2269 * @chip: nand chip info structure
2270 * @offset: column address of subpage within the page
2271 * @data_len: data length
2272 * @buf: data buffer
2273 * @oob_required: must write chip->oob_poi to OOB
2274 */
2275static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2276 struct nand_chip *chip, uint32_t offset,
2277 uint32_t data_len, const uint8_t *buf,
2278 int oob_required)
2279{
2280 uint8_t *oob_buf = chip->oob_poi;
2281 uint8_t *ecc_calc = chip->buffers->ecccalc;
2282 int ecc_size = chip->ecc.size;
2283 int ecc_bytes = chip->ecc.bytes;
2284 int ecc_steps = chip->ecc.steps;
2285 uint32_t *eccpos = chip->ecc.layout->eccpos;
2286 uint32_t start_step = offset / ecc_size;
2287 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2288 int oob_bytes = mtd->oobsize / ecc_steps;
2289 int step, i;
2290
2291 for (step = 0; step < ecc_steps; step++) {
2292 /* configure controller for WRITE access */
2293 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2294
2295 /* write data (untouched subpages already masked by 0xFF) */
2296 chip->write_buf(mtd, buf, ecc_size);
2297
2298 /* mask ECC of un-touched subpages by padding 0xFF */
2299 if ((step < start_step) || (step > end_step))
2300 memset(ecc_calc, 0xff, ecc_bytes);
2301 else
2302 chip->ecc.calculate(mtd, buf, ecc_calc);
2303
2304 /* mask OOB of un-touched subpages by padding 0xFF */
2305 /* if oob_required, preserve OOB metadata of written subpage */
2306 if (!oob_required || (step < start_step) || (step > end_step))
2307 memset(oob_buf, 0xff, oob_bytes);
2308
2309 buf += ecc_size;
2310 ecc_calc += ecc_bytes;
2311 oob_buf += oob_bytes;
2312 }
2313
2314 /* copy calculated ECC for whole page to chip->buffer->oob */
2315 /* this include masked-value(0xFF) for unwritten subpages */
2316 ecc_calc = chip->buffers->ecccalc;
2317 for (i = 0; i < chip->ecc.total; i++)
2318 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2319
2320 /* write OOB buffer to NAND device */
2321 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2322
2323 return 0;
2324}
2325
2326
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002327/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002328 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2329 * @mtd: mtd info structure
2330 * @chip: nand chip info structure
2331 * @buf: data buffer
2332 * @oob_required: must write chip->oob_poi to OOB
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002333 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002334 * The hw generator calculates the error syndrome automatically. Therefore we
2335 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002336 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002337static int nand_write_page_syndrome(struct mtd_info *mtd,
2338 struct nand_chip *chip,
2339 const uint8_t *buf, int oob_required)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002340{
William Juul52c07962007-10-31 13:53:06 +01002341 int i, eccsize = chip->ecc.size;
2342 int eccbytes = chip->ecc.bytes;
2343 int eccsteps = chip->ecc.steps;
2344 const uint8_t *p = buf;
2345 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002346
William Juul52c07962007-10-31 13:53:06 +01002347 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002348
William Juul52c07962007-10-31 13:53:06 +01002349 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2350 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002351
William Juul52c07962007-10-31 13:53:06 +01002352 if (chip->ecc.prepad) {
2353 chip->write_buf(mtd, oob, chip->ecc.prepad);
2354 oob += chip->ecc.prepad;
2355 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002356
William Juul52c07962007-10-31 13:53:06 +01002357 chip->ecc.calculate(mtd, p, oob);
2358 chip->write_buf(mtd, oob, eccbytes);
2359 oob += eccbytes;
2360
2361 if (chip->ecc.postpad) {
2362 chip->write_buf(mtd, oob, chip->ecc.postpad);
2363 oob += chip->ecc.postpad;
2364 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002365 }
2366
William Juul52c07962007-10-31 13:53:06 +01002367 /* Calculate remaining oob bytes */
2368 i = mtd->oobsize - (oob - chip->oob_poi);
2369 if (i)
2370 chip->write_buf(mtd, oob, i);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002371
2372 return 0;
William Juul52c07962007-10-31 13:53:06 +01002373}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002374
William Juul52c07962007-10-31 13:53:06 +01002375/**
2376 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00002377 * @mtd: MTD device structure
2378 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02002379 * @offset: address offset within the page
2380 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00002381 * @buf: the data to write
2382 * @oob_required: must write chip->oob_poi to OOB
2383 * @page: page number to write
2384 * @cached: cached programming
2385 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01002386 */
2387static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002388 uint32_t offset, int data_len, const uint8_t *buf,
2389 int oob_required, int page, int cached, int raw)
William Juul52c07962007-10-31 13:53:06 +01002390{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002391 int status, subpage;
2392
2393 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2394 chip->ecc.write_subpage)
2395 subpage = offset || (data_len < mtd->writesize);
2396 else
2397 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002398
William Juul52c07962007-10-31 13:53:06 +01002399 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2400
2401 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02002402 status = chip->ecc.write_page_raw(mtd, chip, buf,
2403 oob_required);
2404 else if (subpage)
2405 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2406 buf, oob_required);
William Juul52c07962007-10-31 13:53:06 +01002407 else
Sergey Lapin3a38a552013-01-14 03:46:50 +00002408 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2409
2410 if (status < 0)
2411 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002412
William Juul52c07962007-10-31 13:53:06 +01002413 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00002414 * Cached progamming disabled for now. Not sure if it's worth the
2415 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juul52c07962007-10-31 13:53:06 +01002416 */
2417 cached = 0;
2418
Heiko Schocherf5895d12014-06-24 10:10:04 +02002419 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
William Juul52c07962007-10-31 13:53:06 +01002420
2421 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2422 status = chip->waitfunc(mtd, chip);
2423 /*
2424 * See if operation failed and additional status checks are
Sergey Lapin3a38a552013-01-14 03:46:50 +00002425 * available.
William Juul52c07962007-10-31 13:53:06 +01002426 */
2427 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2428 status = chip->errstat(mtd, chip, FL_WRITING, status,
2429 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002430
William Juul52c07962007-10-31 13:53:06 +01002431 if (status & NAND_STATUS_FAIL)
2432 return -EIO;
2433 } else {
2434 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2435 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002436 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002437
Heiko Schocherf5895d12014-06-24 10:10:04 +02002438
2439#ifdef __UBOOT__
2440#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
William Juul52c07962007-10-31 13:53:06 +01002441 /* Send command to read back the data */
2442 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002443
William Juul52c07962007-10-31 13:53:06 +01002444 if (chip->verify_buf(mtd, buf, mtd->writesize))
2445 return -EIO;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002446
2447 /* Make sure the next page prog is preceded by a status read */
2448 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002449#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02002450#endif
2451
William Juul52c07962007-10-31 13:53:06 +01002452 return 0;
2453}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002454
William Juul52c07962007-10-31 13:53:06 +01002455/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002456 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2457 * @mtd: MTD device structure
2458 * @oob: oob data buffer
2459 * @len: oob data write length
2460 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01002461 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002462static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2463 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01002464{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002465 struct nand_chip *chip = mtd->priv;
2466
2467 /*
2468 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2469 * data from a previous OOB read.
2470 */
2471 memset(chip->oob_poi, 0xff, mtd->oobsize);
2472
Christian Hitz13fc0e22011-10-12 09:32:01 +02002473 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002474
Sergey Lapin3a38a552013-01-14 03:46:50 +00002475 case MTD_OPS_PLACE_OOB:
2476 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002477 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2478 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002479
Sergey Lapin3a38a552013-01-14 03:46:50 +00002480 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002481 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2482 uint32_t boffs = 0, woffs = ops->ooboffs;
2483 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002484
Christian Hitz13fc0e22011-10-12 09:32:01 +02002485 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002486 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002487 if (unlikely(woffs)) {
2488 if (woffs >= free->length) {
2489 woffs -= free->length;
2490 continue;
2491 }
2492 boffs = free->offset + woffs;
2493 bytes = min_t(size_t, len,
2494 (free->length - woffs));
2495 woffs = 0;
2496 } else {
2497 bytes = min_t(size_t, len, free->length);
2498 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002499 }
William Juul52c07962007-10-31 13:53:06 +01002500 memcpy(chip->oob_poi + boffs, oob, bytes);
2501 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002502 }
William Juul52c07962007-10-31 13:53:06 +01002503 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002504 }
William Juul52c07962007-10-31 13:53:06 +01002505 default:
2506 BUG();
2507 }
2508 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002509}
2510
Christian Hitzb8a6b372011-10-12 09:32:02 +02002511#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002512
2513/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002514 * nand_do_write_ops - [INTERN] NAND write with ECC
2515 * @mtd: MTD device structure
2516 * @to: offset to write to
2517 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002518 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002519 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002520 */
William Juul52c07962007-10-31 13:53:06 +01002521static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2522 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002523{
William Juul52c07962007-10-31 13:53:06 +01002524 int chipnr, realpage, page, blockmask, column;
2525 struct nand_chip *chip = mtd->priv;
2526 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002527
2528 uint32_t oobwritelen = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002529 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitzb8a6b372011-10-12 09:32:02 +02002530 mtd->oobavail : mtd->oobsize;
2531
William Juul52c07962007-10-31 13:53:06 +01002532 uint8_t *oob = ops->oobbuf;
2533 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002534 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002535 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002536
William Juul52c07962007-10-31 13:53:06 +01002537 ops->retlen = 0;
2538 if (!writelen)
2539 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002540
Heiko Schocherf5895d12014-06-24 10:10:04 +02002541#ifndef __UBOOT__
2542 /* Reject writes, which are not page aligned */
2543 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2544#else
2545 /* Reject writes, which are not page aligned */
2546 if (NOTALIGNED(to)) {
2547#endif
2548 pr_notice("%s: attempt to write non page aligned data\n",
2549 __func__);
William Juul52c07962007-10-31 13:53:06 +01002550 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002551 }
2552
2553 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002554
William Juul52c07962007-10-31 13:53:06 +01002555 chipnr = (int)(to >> chip->chip_shift);
2556 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002557
2558 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01002559 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002560 ret = -EIO;
2561 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01002562 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002563
William Juul52c07962007-10-31 13:53:06 +01002564 realpage = (int)(to >> chip->page_shift);
2565 page = realpage & chip->pagemask;
2566 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002567
William Juul52c07962007-10-31 13:53:06 +01002568 /* Invalidate the page cache, when we write to the cached page */
2569 if (to <= (chip->pagebuf << chip->page_shift) &&
2570 (chip->pagebuf << chip->page_shift) < (to + ops->len))
2571 chip->pagebuf = -1;
2572
Christian Hitzb8a6b372011-10-12 09:32:02 +02002573 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002574 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2575 ret = -EINVAL;
2576 goto err_out;
2577 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02002578
Christian Hitz13fc0e22011-10-12 09:32:01 +02002579 while (1) {
William Juul52c07962007-10-31 13:53:06 +01002580 int bytes = mtd->writesize;
2581 int cached = writelen > bytes && page != blockmask;
2582 uint8_t *wbuf = buf;
2583
Heiko Schocherf5895d12014-06-24 10:10:04 +02002584 WATCHDOG_RESET();
Sergey Lapin3a38a552013-01-14 03:46:50 +00002585 /* Partial page write? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002586 if (unlikely(column || writelen < (mtd->writesize - 1))) {
William Juul52c07962007-10-31 13:53:06 +01002587 cached = 0;
2588 bytes = min_t(int, bytes - column, (int) writelen);
2589 chip->pagebuf = -1;
2590 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2591 memcpy(&chip->buffers->databuf[column], buf, bytes);
2592 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002593 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002594
Christian Hitzb8a6b372011-10-12 09:32:02 +02002595 if (unlikely(oob)) {
2596 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002597 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002598 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002599 } else {
2600 /* We still need to erase leftover OOB data */
2601 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002602 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002603 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2604 oob_required, page, cached,
2605 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01002606 if (ret)
2607 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002608
William Juul52c07962007-10-31 13:53:06 +01002609 writelen -= bytes;
2610 if (!writelen)
2611 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002612
Heiko Schocherf5895d12014-06-24 10:10:04 +02002613 column = 0;
2614 buf += bytes;
2615 realpage++;
2616
2617 page = realpage & chip->pagemask;
2618 /* Check, if we cross a chip boundary */
2619 if (!page) {
2620 chipnr++;
2621 chip->select_chip(mtd, -1);
2622 chip->select_chip(mtd, chipnr);
2623 }
2624 }
2625
2626 ops->retlen = ops->len - writelen;
2627 if (unlikely(oob))
2628 ops->oobretlen = ops->ooblen;
2629
2630err_out:
2631 chip->select_chip(mtd, -1);
2632 return ret;
2633}
2634
2635/**
2636 * panic_nand_write - [MTD Interface] NAND write with ECC
2637 * @mtd: MTD device structure
2638 * @to: offset to write to
2639 * @len: number of bytes to write
2640 * @retlen: pointer to variable to store the number of written bytes
2641 * @buf: the data to write
2642 *
2643 * NAND write with ECC. Used when performing writes in interrupt context, this
2644 * may for example be called by mtdoops when writing an oops while in panic.
2645 */
2646static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2647 size_t *retlen, const uint8_t *buf)
2648{
2649 struct nand_chip *chip = mtd->priv;
2650 struct mtd_oob_ops ops;
2651 int ret;
2652
2653 /* Wait for the device to get ready */
2654 panic_nand_wait(mtd, chip, 400);
2655
2656 /* Grab the device */
2657 panic_nand_get_device(chip, mtd, FL_WRITING);
2658
2659 ops.len = len;
2660 ops.datbuf = (uint8_t *)buf;
2661 ops.oobbuf = NULL;
2662 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01002663
Heiko Schocherf5895d12014-06-24 10:10:04 +02002664 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002665
Heiko Schocherf5895d12014-06-24 10:10:04 +02002666 *retlen = ops.retlen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002667 return ret;
2668}
2669
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002670/**
William Juul52c07962007-10-31 13:53:06 +01002671 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00002672 * @mtd: MTD device structure
2673 * @to: offset to write to
2674 * @len: number of bytes to write
2675 * @retlen: pointer to variable to store the number of written bytes
2676 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002677 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002678 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002679 */
William Juul52c07962007-10-31 13:53:06 +01002680static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2681 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002682{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002683 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01002684 int ret;
2685
Heiko Schocherf5895d12014-06-24 10:10:04 +02002686 nand_get_device(mtd, FL_WRITING);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002687 ops.len = len;
2688 ops.datbuf = (uint8_t *)buf;
2689 ops.oobbuf = NULL;
2690 ops.mode = MTD_OPS_PLACE_OOB;
2691 ret = nand_do_write_ops(mtd, to, &ops);
2692 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01002693 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002694 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002695}
2696
2697/**
William Juul52c07962007-10-31 13:53:06 +01002698 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002699 * @mtd: MTD device structure
2700 * @to: offset to write to
2701 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002702 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002703 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002704 */
William Juul52c07962007-10-31 13:53:06 +01002705static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2706 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002707{
William Juul52c07962007-10-31 13:53:06 +01002708 int chipnr, page, status, len;
2709 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002710
Heiko Schocherf5895d12014-06-24 10:10:04 +02002711 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002712 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002713
Sergey Lapin3a38a552013-01-14 03:46:50 +00002714 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juul52c07962007-10-31 13:53:06 +01002715 len = chip->ecc.layout->oobavail;
2716 else
2717 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002718
2719 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002720 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002721 pr_debug("%s: attempt to write past end of page\n",
2722 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002723 return -EINVAL;
2724 }
2725
William Juul52c07962007-10-31 13:53:06 +01002726 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002727 pr_debug("%s: attempt to start write outside oob\n",
2728 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002729 return -EINVAL;
2730 }
2731
Christian Hitz13fc0e22011-10-12 09:32:01 +02002732 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01002733 if (unlikely(to >= mtd->size ||
2734 ops->ooboffs + ops->ooblen >
2735 ((mtd->size >> chip->page_shift) -
2736 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002737 pr_debug("%s: attempt to write beyond end of device\n",
2738 __func__);
William Juul52c07962007-10-31 13:53:06 +01002739 return -EINVAL;
2740 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002741
William Juul52c07962007-10-31 13:53:06 +01002742 chipnr = (int)(to >> chip->chip_shift);
2743 chip->select_chip(mtd, chipnr);
2744
2745 /* Shift to get page */
2746 page = (int)(to >> chip->page_shift);
2747
2748 /*
2749 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2750 * of my DiskOnChip 2000 test units) will clear the whole data page too
2751 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2752 * it in the doc2000 driver in August 1999. dwmw2.
2753 */
2754 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002755
2756 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002757 if (nand_check_wp(mtd)) {
2758 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01002759 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002760 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002761
William Juul52c07962007-10-31 13:53:06 +01002762 /* Invalidate the page cache, if we write to the cached page */
2763 if (page == chip->pagebuf)
2764 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002765
Sergey Lapin3a38a552013-01-14 03:46:50 +00002766 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2767
2768 if (ops->mode == MTD_OPS_RAW)
2769 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2770 else
2771 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002772
Heiko Schocherf5895d12014-06-24 10:10:04 +02002773 chip->select_chip(mtd, -1);
2774
William Juul52c07962007-10-31 13:53:06 +01002775 if (status)
2776 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002777
William Juul52c07962007-10-31 13:53:06 +01002778 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779
William Juul52c07962007-10-31 13:53:06 +01002780 return 0;
2781}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002782
William Juul52c07962007-10-31 13:53:06 +01002783/**
2784 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002785 * @mtd: MTD device structure
2786 * @to: offset to write to
2787 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01002788 */
2789static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2790 struct mtd_oob_ops *ops)
2791{
William Juul52c07962007-10-31 13:53:06 +01002792 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002793
William Juul52c07962007-10-31 13:53:06 +01002794 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002795
William Juul52c07962007-10-31 13:53:06 +01002796 /* Do not allow writes past end of device */
2797 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002798 pr_debug("%s: attempt to write beyond end of device\n",
2799 __func__);
William Juul52c07962007-10-31 13:53:06 +01002800 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002801 }
William Juul52c07962007-10-31 13:53:06 +01002802
Heiko Schocherf5895d12014-06-24 10:10:04 +02002803 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01002804
Christian Hitz13fc0e22011-10-12 09:32:01 +02002805 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002806 case MTD_OPS_PLACE_OOB:
2807 case MTD_OPS_AUTO_OOB:
2808 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002809 break;
2810
2811 default:
2812 goto out;
2813 }
2814
2815 if (!ops->datbuf)
2816 ret = nand_do_write_oob(mtd, to, ops);
2817 else
2818 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002819
Christian Hitz13fc0e22011-10-12 09:32:01 +02002820out:
William Juul52c07962007-10-31 13:53:06 +01002821 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002822 return ret;
2823}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002824
2825/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002826 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2827 * @mtd: MTD device structure
2828 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002829 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002830 * Standard erase command for NAND chips.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002831 */
William Juul52c07962007-10-31 13:53:06 +01002832static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002833{
William Juul52c07962007-10-31 13:53:06 +01002834 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002835 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002836 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2837 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002838}
2839
2840/**
2841 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002842 * @mtd: MTD device structure
2843 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002844 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002845 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002846 */
William Juul52c07962007-10-31 13:53:06 +01002847static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002848{
William Juul52c07962007-10-31 13:53:06 +01002849 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002850}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002851
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002852/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002853 * nand_erase_nand - [INTERN] erase block(s)
2854 * @mtd: MTD device structure
2855 * @instr: erase instruction
2856 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002857 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002858 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002859 */
William Juul52c07962007-10-31 13:53:06 +01002860int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2861 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002862{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002863 int page, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002864 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002865 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002866
Heiko Schocherf5895d12014-06-24 10:10:04 +02002867 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2868 __func__, (unsigned long long)instr->addr,
2869 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002870
Christian Hitzb8a6b372011-10-12 09:32:02 +02002871 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002872 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002873
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002874 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002875 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002876
2877 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002878 page = (int)(instr->addr >> chip->page_shift);
2879 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002880
2881 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002882 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002883
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002884 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002885 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002886
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002887 /* Check, if it is write protected */
2888 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002889 pr_debug("%s: device is write protected!\n",
2890 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002891 instr->state = MTD_ERASE_FAILED;
2892 goto erase_exit;
2893 }
2894
2895 /* Loop through the pages */
2896 len = instr->len;
2897
2898 instr->state = MTD_ERASING;
2899
2900 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06002901 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002902
Sergey Lapin3a38a552013-01-14 03:46:50 +00002903 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09002904 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juul52c07962007-10-31 13:53:06 +01002905 chip->page_shift, 0, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002906 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002907 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002908 instr->state = MTD_ERASE_FAILED;
2909 goto erase_exit;
2910 }
William Juul52c07962007-10-31 13:53:06 +01002911
2912 /*
2913 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00002914 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01002915 */
2916 if (page <= chip->pagebuf && chip->pagebuf <
2917 (page + pages_per_block))
2918 chip->pagebuf = -1;
2919
2920 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002921
William Juul52c07962007-10-31 13:53:06 +01002922 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002923
William Juul52c07962007-10-31 13:53:06 +01002924 /*
2925 * See if operation failed and additional status checks are
2926 * available
2927 */
2928 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2929 status = chip->errstat(mtd, chip, FL_ERASING,
2930 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002931
2932 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002933 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002934 pr_debug("%s: failed erase, page 0x%08x\n",
2935 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002936 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02002937 instr->fail_addr =
2938 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002939 goto erase_exit;
2940 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002941
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002942 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002943 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002944 page += pages_per_block;
2945
2946 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002947 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002948 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002949 chip->select_chip(mtd, -1);
2950 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002951 }
2952 }
2953 instr->state = MTD_ERASE_DONE;
2954
Christian Hitz13fc0e22011-10-12 09:32:01 +02002955erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002956
2957 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002958
2959 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002960 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002961 nand_release_device(mtd);
2962
Scott Wood3628f002008-10-24 16:20:43 -05002963 /* Do call back function */
2964 if (!ret)
2965 mtd_erase_callback(instr);
2966
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002967 /* Return more or less happy */
2968 return ret;
2969}
2970
2971/**
2972 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00002973 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002974 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002975 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002976 */
William Juul52c07962007-10-31 13:53:06 +01002977static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002978{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002979 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002980
2981 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002982 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002983 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002984 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002985}
2986
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002987/**
William Juul52c07962007-10-31 13:53:06 +01002988 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002989 * @mtd: MTD device structure
2990 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002991 */
William Juul52c07962007-10-31 13:53:06 +01002992static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002993{
William Juul52c07962007-10-31 13:53:06 +01002994 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002995}
2996
2997/**
William Juul52c07962007-10-31 13:53:06 +01002998 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002999 * @mtd: MTD device structure
3000 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003001 */
William Juul52c07962007-10-31 13:53:06 +01003002static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003003{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003004 int ret;
3005
Christian Hitzb8a6b372011-10-12 09:32:02 +02003006 ret = nand_block_isbad(mtd, ofs);
3007 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003008 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003009 if (ret > 0)
3010 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003011 return ret;
3012 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003013
Heiko Schocherf5895d12014-06-24 10:10:04 +02003014 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003015}
3016
Heiko Schocherf5895d12014-06-24 10:10:04 +02003017/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003018 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3019 * @mtd: MTD device structure
3020 * @chip: nand chip info structure
3021 * @addr: feature address.
3022 * @subfeature_param: the subfeature parameters, a four bytes array.
3023 */
3024static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3025 int addr, uint8_t *subfeature_param)
3026{
3027 int status;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003028 int i;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003029
Heiko Schocherf5895d12014-06-24 10:10:04 +02003030#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3031 if (!chip->onfi_version ||
3032 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3033 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapin3a38a552013-01-14 03:46:50 +00003034 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003035#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003036
3037 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003038 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3039 chip->write_byte(mtd, subfeature_param[i]);
3040
Sergey Lapin3a38a552013-01-14 03:46:50 +00003041 status = chip->waitfunc(mtd, chip);
3042 if (status & NAND_STATUS_FAIL)
3043 return -EIO;
3044 return 0;
3045}
3046
3047/**
3048 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3049 * @mtd: MTD device structure
3050 * @chip: nand chip info structure
3051 * @addr: feature address.
3052 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003053 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003054static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3055 int addr, uint8_t *subfeature_param)
3056{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003057 int i;
3058
3059#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3060 if (!chip->onfi_version ||
3061 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3062 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapin3a38a552013-01-14 03:46:50 +00003063 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003064#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003065
3066 /* clear the sub feature parameters */
3067 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
3068
3069 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003070 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3071 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003072 return 0;
3073}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003074
3075#ifndef __UBOOT__
3076/**
3077 * nand_suspend - [MTD Interface] Suspend the NAND flash
3078 * @mtd: MTD device structure
3079 */
3080static int nand_suspend(struct mtd_info *mtd)
3081{
3082 return nand_get_device(mtd, FL_PM_SUSPENDED);
3083}
3084
3085/**
3086 * nand_resume - [MTD Interface] Resume the NAND flash
3087 * @mtd: MTD device structure
3088 */
3089static void nand_resume(struct mtd_info *mtd)
3090{
3091 struct nand_chip *chip = mtd->priv;
3092
3093 if (chip->state == FL_PM_SUSPENDED)
3094 nand_release_device(mtd);
3095 else
3096 pr_err("%s called for a chip which is not in suspended state\n",
3097 __func__);
3098}
3099#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003100
3101/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003102static void nand_set_defaults(struct nand_chip *chip, int busw)
3103{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003104 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003105 if (!chip->chip_delay)
3106 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003107
3108 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003109 if (chip->cmdfunc == NULL)
3110 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003111
3112 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003113 if (chip->waitfunc == NULL)
3114 chip->waitfunc = nand_wait;
3115
3116 if (!chip->select_chip)
3117 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003118
3119 /* set for ONFI nand */
3120 if (!chip->onfi_set_features)
3121 chip->onfi_set_features = nand_onfi_set_features;
3122 if (!chip->onfi_get_features)
3123 chip->onfi_get_features = nand_onfi_get_features;
3124
3125 /* If called twice, pointers that depend on busw may need to be reset */
3126 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003127 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3128 if (!chip->read_word)
3129 chip->read_word = nand_read_word;
3130 if (!chip->block_bad)
3131 chip->block_bad = nand_block_bad;
3132 if (!chip->block_markbad)
3133 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003134 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003135 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003136 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3137 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3138 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003139 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003140 if (!chip->scan_bbt)
3141 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003142#ifdef __UBOOT__
3143#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
3144 if (!chip->verify_buf)
3145 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
3146#endif
3147#endif
3148
3149 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003150 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003151 spin_lock_init(&chip->controller->lock);
3152 init_waitqueue_head(&chip->controller->wq);
3153 }
3154
William Juul52c07962007-10-31 13:53:06 +01003155}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003156
Sergey Lapin3a38a552013-01-14 03:46:50 +00003157/* Sanitize ONFI strings so we can safely print them */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003158#ifndef __UBOOT__
3159static void sanitize_string(uint8_t *s, size_t len)
3160#else
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003161static void sanitize_string(char *s, size_t len)
Heiko Schocherf5895d12014-06-24 10:10:04 +02003162#endif
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003163{
3164 ssize_t i;
3165
Sergey Lapin3a38a552013-01-14 03:46:50 +00003166 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003167 s[len - 1] = 0;
3168
Sergey Lapin3a38a552013-01-14 03:46:50 +00003169 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003170 for (i = 0; i < len - 1; i++) {
3171 if (s[i] < ' ' || s[i] > 127)
3172 s[i] = '?';
3173 }
3174
Sergey Lapin3a38a552013-01-14 03:46:50 +00003175 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003176 strim(s);
3177}
3178
Florian Fainellic98a9352011-02-25 00:01:34 +00003179static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3180{
3181 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003182 while (len--) {
3183 crc ^= *p++ << 8;
3184 for (i = 0; i < 8; i++)
3185 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3186 }
3187
3188 return crc;
3189}
3190
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003191#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003192/* Parse the Extended Parameter Page. */
3193static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3194 struct nand_chip *chip, struct nand_onfi_params *p)
3195{
3196 struct onfi_ext_param_page *ep;
3197 struct onfi_ext_section *s;
3198 struct onfi_ext_ecc_info *ecc;
3199 uint8_t *cursor;
3200 int ret = -EINVAL;
3201 int len;
3202 int i;
3203
3204 len = le16_to_cpu(p->ext_param_page_length) * 16;
3205 ep = kmalloc(len, GFP_KERNEL);
3206 if (!ep)
3207 return -ENOMEM;
3208
3209 /* Send our own NAND_CMD_PARAM. */
3210 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3211
3212 /* Use the Change Read Column command to skip the ONFI param pages. */
3213 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3214 sizeof(*p) * p->num_of_param_pages , -1);
3215
3216 /* Read out the Extended Parameter Page. */
3217 chip->read_buf(mtd, (uint8_t *)ep, len);
3218 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3219 != le16_to_cpu(ep->crc))) {
3220 pr_debug("fail in the CRC.\n");
3221 goto ext_out;
3222 }
3223
3224 /*
3225 * Check the signature.
3226 * Do not strictly follow the ONFI spec, maybe changed in future.
3227 */
3228#ifndef __UBOOT__
3229 if (strncmp(ep->sig, "EPPS", 4)) {
3230#else
3231 if (strncmp((char *)ep->sig, "EPPS", 4)) {
3232#endif
3233 pr_debug("The signature is invalid.\n");
3234 goto ext_out;
3235 }
3236
3237 /* find the ECC section. */
3238 cursor = (uint8_t *)(ep + 1);
3239 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3240 s = ep->sections + i;
3241 if (s->type == ONFI_SECTION_TYPE_2)
3242 break;
3243 cursor += s->length * 16;
3244 }
3245 if (i == ONFI_EXT_SECTION_MAX) {
3246 pr_debug("We can not find the ECC section.\n");
3247 goto ext_out;
3248 }
3249
3250 /* get the info we want. */
3251 ecc = (struct onfi_ext_ecc_info *)cursor;
3252
3253 if (!ecc->codeword_size) {
3254 pr_debug("Invalid codeword size\n");
3255 goto ext_out;
3256 }
3257
3258 chip->ecc_strength_ds = ecc->ecc_bits;
3259 chip->ecc_step_ds = 1 << ecc->codeword_size;
3260 ret = 0;
3261
3262ext_out:
3263 kfree(ep);
3264 return ret;
3265}
3266
3267static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3268{
3269 struct nand_chip *chip = mtd->priv;
3270 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3271
3272 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3273 feature);
3274}
3275
3276/*
3277 * Configure chip properties from Micron vendor-specific ONFI table
3278 */
3279static void nand_onfi_detect_micron(struct nand_chip *chip,
3280 struct nand_onfi_params *p)
3281{
3282 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3283
3284 if (le16_to_cpu(p->vendor_revision) < 1)
3285 return;
3286
3287 chip->read_retries = micron->read_retry_options;
3288 chip->setup_read_retry = nand_setup_read_retry_micron;
3289}
3290
Florian Fainellic98a9352011-02-25 00:01:34 +00003291/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003292 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003293 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003294static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003295 int *busw)
3296{
3297 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisf3832302014-05-06 00:46:16 +05303298 int i, j;
Florian Fainellic98a9352011-02-25 00:01:34 +00003299 int val;
3300
Sergey Lapin3a38a552013-01-14 03:46:50 +00003301 /* Try ONFI for unknown chip or LP */
Florian Fainellic98a9352011-02-25 00:01:34 +00003302 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3303 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3304 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3305 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003306
Florian Fainellic98a9352011-02-25 00:01:34 +00003307 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3308 for (i = 0; i < 3; i++) {
Brian Norrisf3832302014-05-06 00:46:16 +05303309 for (j = 0; j < sizeof(*p); j++)
3310 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003311 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003312 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003313 break;
3314 }
3315 }
3316
Heiko Schocherf5895d12014-06-24 10:10:04 +02003317 if (i == 3) {
3318 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003319 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003320 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003321
Sergey Lapin3a38a552013-01-14 03:46:50 +00003322 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003323 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003324 if (val & (1 << 5))
3325 chip->onfi_version = 23;
3326 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003327 chip->onfi_version = 22;
3328 else if (val & (1 << 3))
3329 chip->onfi_version = 21;
3330 else if (val & (1 << 2))
3331 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003332 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003333 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003334
3335 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003336 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003337 return 0;
3338 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003339
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003340 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3341 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003342 if (!mtd->name)
3343 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003344
Florian Fainellic98a9352011-02-25 00:01:34 +00003345 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003346
3347 /*
3348 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3349 * (don't ask me who thought of this...). MTD assumes that these
3350 * dimensions will be power-of-2, so just truncate the remaining area.
3351 */
3352 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3353 mtd->erasesize *= mtd->writesize;
3354
Florian Fainellic98a9352011-02-25 00:01:34 +00003355 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003356
3357 /* See erasesize comment */
3358 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003359 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003360 chip->bits_per_cell = p->bits_per_cell;
3361
3362 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003363 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003364 else
3365 *busw = 0;
3366
3367 if (p->ecc_bits != 0xff) {
3368 chip->ecc_strength_ds = p->ecc_bits;
3369 chip->ecc_step_ds = 512;
3370 } else if (chip->onfi_version >= 21 &&
3371 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3372
3373 /*
3374 * The nand_flash_detect_ext_param_page() uses the
3375 * Change Read Column command which maybe not supported
3376 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3377 * now. We do not replace user supplied command function.
3378 */
3379 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3380 chip->cmdfunc = nand_command_lp;
3381
3382 /* The Extended Parameter Page is supported since ONFI 2.1. */
3383 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3384 pr_warn("Failed to detect ONFI extended param page\n");
3385 } else {
3386 pr_warn("Could not retrieve ONFI ECC requirements\n");
3387 }
3388
3389 if (p->jedec_id == NAND_MFR_MICRON)
3390 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003391
3392 return 1;
3393}
3394#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02003395static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003396 int *busw)
3397{
3398 return 0;
3399}
3400#endif
3401
William Juul52c07962007-10-31 13:53:06 +01003402/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003403 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3404 */
3405static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3406 int *busw)
3407{
3408 struct nand_jedec_params *p = &chip->jedec_params;
3409 struct jedec_ecc_info *ecc;
3410 int val;
3411 int i, j;
3412
3413 /* Try JEDEC for unknown chip or LP */
3414 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3415 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3416 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3417 chip->read_byte(mtd) != 'C')
3418 return 0;
3419
3420 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3421 for (i = 0; i < 3; i++) {
3422 for (j = 0; j < sizeof(*p); j++)
3423 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3424
3425 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3426 le16_to_cpu(p->crc))
3427 break;
3428 }
3429
3430 if (i == 3) {
3431 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3432 return 0;
3433 }
3434
3435 /* Check version */
3436 val = le16_to_cpu(p->revision);
3437 if (val & (1 << 2))
3438 chip->jedec_version = 10;
3439 else if (val & (1 << 1))
3440 chip->jedec_version = 1; /* vendor specific version */
3441
3442 if (!chip->jedec_version) {
3443 pr_info("unsupported JEDEC version: %d\n", val);
3444 return 0;
3445 }
3446
3447 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3448 sanitize_string(p->model, sizeof(p->model));
3449 if (!mtd->name)
3450 mtd->name = p->model;
3451
3452 mtd->writesize = le32_to_cpu(p->byte_per_page);
3453
3454 /* Please reference to the comment for nand_flash_detect_onfi. */
3455 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3456 mtd->erasesize *= mtd->writesize;
3457
3458 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3459
3460 /* Please reference to the comment for nand_flash_detect_onfi. */
3461 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3462 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3463 chip->bits_per_cell = p->bits_per_cell;
3464
3465 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3466 *busw = NAND_BUSWIDTH_16;
3467 else
3468 *busw = 0;
3469
3470 /* ECC info */
3471 ecc = &p->ecc_info[0];
3472
3473 if (ecc->codeword_size >= 9) {
3474 chip->ecc_strength_ds = ecc->ecc_bits;
3475 chip->ecc_step_ds = 1 << ecc->codeword_size;
3476 } else {
3477 pr_warn("Invalid codeword size\n");
3478 }
3479
3480 return 1;
3481}
3482
3483/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003484 * nand_id_has_period - Check if an ID string has a given wraparound period
3485 * @id_data: the ID string
3486 * @arrlen: the length of the @id_data array
3487 * @period: the period of repitition
3488 *
3489 * Check if an ID string is repeated within a given sequence of bytes at
3490 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02003491 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00003492 * if the repetition has a period of @period; otherwise, returns zero.
3493 */
3494static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3495{
3496 int i, j;
3497 for (i = 0; i < period; i++)
3498 for (j = i + period; j < arrlen; j += period)
3499 if (id_data[i] != id_data[j])
3500 return 0;
3501 return 1;
3502}
3503
3504/*
3505 * nand_id_len - Get the length of an ID string returned by CMD_READID
3506 * @id_data: the ID string
3507 * @arrlen: the length of the @id_data array
3508
3509 * Returns the length of the ID string, according to known wraparound/trailing
3510 * zero patterns. If no pattern exists, returns the length of the array.
3511 */
3512static int nand_id_len(u8 *id_data, int arrlen)
3513{
3514 int last_nonzero, period;
3515
3516 /* Find last non-zero byte */
3517 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3518 if (id_data[last_nonzero])
3519 break;
3520
3521 /* All zeros */
3522 if (last_nonzero < 0)
3523 return 0;
3524
3525 /* Calculate wraparound period */
3526 for (period = 1; period < arrlen; period++)
3527 if (nand_id_has_period(id_data, arrlen, period))
3528 break;
3529
3530 /* There's a repeated pattern */
3531 if (period < arrlen)
3532 return period;
3533
3534 /* There are trailing zeros */
3535 if (last_nonzero < arrlen - 1)
3536 return last_nonzero + 1;
3537
3538 /* No pattern detected */
3539 return arrlen;
3540}
3541
Heiko Schocherf5895d12014-06-24 10:10:04 +02003542/* Extract the bits of per cell from the 3rd byte of the extended ID */
3543static int nand_get_bits_per_cell(u8 cellinfo)
3544{
3545 int bits;
3546
3547 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3548 bits >>= NAND_CI_CELLTYPE_SHIFT;
3549 return bits + 1;
3550}
3551
Sergey Lapin3a38a552013-01-14 03:46:50 +00003552/*
3553 * Many new NAND share similar device ID codes, which represent the size of the
3554 * chip. The rest of the parameters must be decoded according to generic or
3555 * manufacturer-specific "extended ID" decoding patterns.
3556 */
3557static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3558 u8 id_data[8], int *busw)
3559{
3560 int extid, id_len;
3561 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003562 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003563 /* The 4th id byte is the important one */
3564 extid = id_data[3];
3565
3566 id_len = nand_id_len(id_data, 8);
3567
3568 /*
3569 * Field definitions are in the following datasheets:
3570 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3571 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3572 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3573 *
3574 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3575 * ID to decide what to do.
3576 */
3577 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003578 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003579 /* Calc pagesize */
3580 mtd->writesize = 2048 << (extid & 0x03);
3581 extid >>= 2;
3582 /* Calc oobsize */
3583 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3584 case 1:
3585 mtd->oobsize = 128;
3586 break;
3587 case 2:
3588 mtd->oobsize = 218;
3589 break;
3590 case 3:
3591 mtd->oobsize = 400;
3592 break;
3593 case 4:
3594 mtd->oobsize = 436;
3595 break;
3596 case 5:
3597 mtd->oobsize = 512;
3598 break;
3599 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003600 mtd->oobsize = 640;
3601 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003602 case 7:
3603 default: /* Other cases are "reserved" (unknown) */
3604 mtd->oobsize = 1024;
3605 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003606 }
3607 extid >>= 2;
3608 /* Calc blocksize */
3609 mtd->erasesize = (128 * 1024) <<
3610 (((extid >> 1) & 0x04) | (extid & 0x03));
3611 *busw = 0;
3612 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003613 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003614 unsigned int tmp;
3615
3616 /* Calc pagesize */
3617 mtd->writesize = 2048 << (extid & 0x03);
3618 extid >>= 2;
3619 /* Calc oobsize */
3620 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3621 case 0:
3622 mtd->oobsize = 128;
3623 break;
3624 case 1:
3625 mtd->oobsize = 224;
3626 break;
3627 case 2:
3628 mtd->oobsize = 448;
3629 break;
3630 case 3:
3631 mtd->oobsize = 64;
3632 break;
3633 case 4:
3634 mtd->oobsize = 32;
3635 break;
3636 case 5:
3637 mtd->oobsize = 16;
3638 break;
3639 default:
3640 mtd->oobsize = 640;
3641 break;
3642 }
3643 extid >>= 2;
3644 /* Calc blocksize */
3645 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3646 if (tmp < 0x03)
3647 mtd->erasesize = (128 * 1024) << tmp;
3648 else if (tmp == 0x03)
3649 mtd->erasesize = 768 * 1024;
3650 else
3651 mtd->erasesize = (64 * 1024) << tmp;
3652 *busw = 0;
3653 } else {
3654 /* Calc pagesize */
3655 mtd->writesize = 1024 << (extid & 0x03);
3656 extid >>= 2;
3657 /* Calc oobsize */
3658 mtd->oobsize = (8 << (extid & 0x01)) *
3659 (mtd->writesize >> 9);
3660 extid >>= 2;
3661 /* Calc blocksize. Blocksize is multiples of 64KiB */
3662 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3663 extid >>= 2;
3664 /* Get buswidth information */
3665 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003666
3667 /*
3668 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3669 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3670 * follows:
3671 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3672 * 110b -> 24nm
3673 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3674 */
3675 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3676 nand_is_slc(chip) &&
3677 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3678 !(id_data[4] & 0x80) /* !BENAND */) {
3679 mtd->oobsize = 32 * mtd->writesize >> 9;
3680 }
3681
Sergey Lapin3a38a552013-01-14 03:46:50 +00003682 }
3683}
3684
Heiko Schocherf5895d12014-06-24 10:10:04 +02003685/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003686 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3687 * decodes a matching ID table entry and assigns the MTD size parameters for
3688 * the chip.
3689 */
3690static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003691 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00003692 int *busw)
3693{
3694 int maf_id = id_data[0];
3695
3696 mtd->erasesize = type->erasesize;
3697 mtd->writesize = type->pagesize;
3698 mtd->oobsize = mtd->writesize / 32;
3699 *busw = type->options & NAND_BUSWIDTH_16;
3700
Heiko Schocherf5895d12014-06-24 10:10:04 +02003701 /* All legacy ID NAND are small-page, SLC */
3702 chip->bits_per_cell = 1;
3703
Sergey Lapin3a38a552013-01-14 03:46:50 +00003704 /*
3705 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3706 * some Spansion chips have erasesize that conflicts with size
3707 * listed in nand_ids table.
3708 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3709 */
3710 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3711 && id_data[6] == 0x00 && id_data[7] == 0x00
3712 && mtd->writesize == 512) {
3713 mtd->erasesize = 128 * 1024;
3714 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3715 }
3716}
3717
Heiko Schocherf5895d12014-06-24 10:10:04 +02003718/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003719 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3720 * heuristic patterns using various detected parameters (e.g., manufacturer,
3721 * page size, cell-type information).
3722 */
3723static void nand_decode_bbm_options(struct mtd_info *mtd,
3724 struct nand_chip *chip, u8 id_data[8])
3725{
3726 int maf_id = id_data[0];
3727
3728 /* Set the bad block position */
3729 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3730 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3731 else
3732 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3733
3734 /*
3735 * Bad block marker is stored in the last page of each block on Samsung
3736 * and Hynix MLC devices; stored in first two pages of each block on
3737 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3738 * AMD/Spansion, and Macronix. All others scan only the first page.
3739 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003740 if (!nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003741 (maf_id == NAND_MFR_SAMSUNG ||
3742 maf_id == NAND_MFR_HYNIX))
3743 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003744 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003745 (maf_id == NAND_MFR_SAMSUNG ||
3746 maf_id == NAND_MFR_HYNIX ||
3747 maf_id == NAND_MFR_TOSHIBA ||
3748 maf_id == NAND_MFR_AMD ||
3749 maf_id == NAND_MFR_MACRONIX)) ||
3750 (mtd->writesize == 2048 &&
3751 maf_id == NAND_MFR_MICRON))
3752 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3753}
3754
Heiko Schocherf5895d12014-06-24 10:10:04 +02003755static inline bool is_full_id_nand(struct nand_flash_dev *type)
3756{
3757 return type->id_len;
3758}
3759
3760static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3761 struct nand_flash_dev *type, u8 *id_data, int *busw)
3762{
3763#ifndef __UBOOT__
3764 if (!strncmp(type->id, id_data, type->id_len)) {
3765#else
3766 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3767#endif
3768 mtd->writesize = type->pagesize;
3769 mtd->erasesize = type->erasesize;
3770 mtd->oobsize = type->oobsize;
3771
3772 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3773 chip->chipsize = (uint64_t)type->chipsize << 20;
3774 chip->options |= type->options;
3775 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3776 chip->ecc_step_ds = NAND_ECC_STEP(type);
3777
3778 *busw = type->options & NAND_BUSWIDTH_16;
3779
3780 if (!mtd->name)
3781 mtd->name = type->name;
3782
3783 return true;
3784 }
3785 return false;
3786}
3787
Sergey Lapin3a38a552013-01-14 03:46:50 +00003788/*
3789 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01003790 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003791static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01003792 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003793 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003794 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01003795{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003796 int busw;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003797 int i, maf_idx;
3798 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003799
3800 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01003801 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003802
Karl Beldanb6322fc2008-09-15 16:08:03 +02003803 /*
3804 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003805 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02003806 */
3807 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3808
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003809 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01003810 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003811
3812 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01003813 *maf_id = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003814 *dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003815
Sergey Lapin3a38a552013-01-14 03:46:50 +00003816 /*
3817 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05003818 * interface concerns can cause random data which looks like a
3819 * possibly credible NAND flash to appear. If the two results do
3820 * not match, ignore the device completely.
3821 */
3822
3823 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3824
Sergey Lapin3a38a552013-01-14 03:46:50 +00003825 /* Read entire ID string */
3826 for (i = 0; i < 8; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02003827 id_data[i] = chip->read_byte(mtd);
Scott Wood3628f002008-10-24 16:20:43 -05003828
Christian Hitzb8a6b372011-10-12 09:32:02 +02003829 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003830 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003831 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05003832 return ERR_PTR(-ENODEV);
3833 }
3834
Lei Wen75bde942011-01-06 09:48:18 +08003835 if (!type)
3836 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003837
Heiko Schocherf5895d12014-06-24 10:10:04 +02003838 for (; type->name != NULL; type++) {
3839 if (is_full_id_nand(type)) {
3840 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3841 goto ident_done;
3842 } else if (*dev_id == type->dev_id) {
3843 break;
3844 }
3845 }
Lei Wen75bde942011-01-06 09:48:18 +08003846
Christian Hitzb8a6b372011-10-12 09:32:02 +02003847 chip->onfi_version = 0;
3848 if (!type->name || !type->pagesize) {
3849 /* Check is chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003850 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02003851 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003852
3853 /* Check if the chip is JEDEC compliant */
3854 if (nand_flash_detect_jedec(mtd, chip, &busw))
3855 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02003856 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003857
Christian Hitzb8a6b372011-10-12 09:32:02 +02003858 if (!type->name)
3859 return ERR_PTR(-ENODEV);
3860
William Juul52c07962007-10-31 13:53:06 +01003861 if (!mtd->name)
3862 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003863
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003864 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003865
Christian Hitzb8a6b372011-10-12 09:32:02 +02003866 if (!type->pagesize && chip->init_size) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003867 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitzb8a6b372011-10-12 09:32:02 +02003868 busw = chip->init_size(mtd, chip, id_data);
3869 } else if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003870 /* Decode parameters from extended ID */
3871 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003872 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003873 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003874 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003875 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00003876 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00003877
Sergey Lapin3a38a552013-01-14 03:46:50 +00003878 /*
3879 * Check if chip is not a Samsung device. Do not clear the
3880 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02003881 */
3882 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3883 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3884ident_done:
3885
William Juul52c07962007-10-31 13:53:06 +01003886 /* Try to identify manufacturer */
3887 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3888 if (nand_manuf_ids[maf_idx].id == *maf_id)
3889 break;
3890 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003891
Heiko Schocherf5895d12014-06-24 10:10:04 +02003892 if (chip->options & NAND_BUSWIDTH_AUTO) {
3893 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3894 chip->options |= busw;
3895 nand_set_defaults(chip, busw);
3896 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3897 /*
3898 * Check, if buswidth is correct. Hardware drivers should set
3899 * chip correct!
3900 */
3901 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3902 *maf_id, *dev_id);
3903 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3904 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003905 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3906 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01003907 return ERR_PTR(-EINVAL);
3908 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003909
Sergey Lapin3a38a552013-01-14 03:46:50 +00003910 nand_decode_bbm_options(mtd, chip, id_data);
3911
William Juul52c07962007-10-31 13:53:06 +01003912 /* Calculate the address shift from the page size */
3913 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003914 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01003915 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003916
William Juul52c07962007-10-31 13:53:06 +01003917 chip->bbt_erase_shift = chip->phys_erase_shift =
3918 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003919 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05003920 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003921 else {
3922 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3923 chip->chip_shift += 32 - 1;
3924 }
3925
3926 chip->badblockbits = 8;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003927 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003928
Sergey Lapin3a38a552013-01-14 03:46:50 +00003929 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01003930 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3931 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003932
Heiko Schocherf5895d12014-06-24 10:10:04 +02003933 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3934 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003935
Christian Hitzb8a6b372011-10-12 09:32:02 +02003936#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003937 if (chip->onfi_version)
3938 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3939 chip->onfi_params.model);
3940 else if (chip->jedec_version)
3941 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3942 chip->jedec_params.model);
3943 else
3944 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3945 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003946#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003947 if (chip->jedec_version)
3948 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3949 chip->jedec_params.model);
3950 else
3951 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3952 type->name);
3953
3954 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3955 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003956#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003957
Heiko Schocherf5895d12014-06-24 10:10:04 +02003958 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3959 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003960 mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01003961 return type;
3962}
3963
3964/**
3965 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003966 * @mtd: MTD device structure
3967 * @maxchips: number of chips to scan for
3968 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01003969 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003970 * This is the first phase of the normal nand_scan() function. It reads the
3971 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01003972 *
3973 * The mtd->owner field must be set to the module of the caller.
3974 */
Lei Wen75bde942011-01-06 09:48:18 +08003975int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003976 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01003977{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003978 int i, nand_maf_id, nand_dev_id;
William Juul52c07962007-10-31 13:53:06 +01003979 struct nand_chip *chip = mtd->priv;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003980 struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003981
William Juul52c07962007-10-31 13:53:06 +01003982 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003983 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01003984
3985 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003986 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3987 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01003988
3989 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003990 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3991 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01003992 chip->select_chip(mtd, -1);
3993 return PTR_ERR(type);
3994 }
3995
Heiko Schocherf5895d12014-06-24 10:10:04 +02003996 chip->select_chip(mtd, -1);
3997
William Juul52c07962007-10-31 13:53:06 +01003998 /* Check for a chip array */
3999 for (i = 1; i < maxchips; i++) {
4000 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004001 /* See comment in nand_get_flash_type for reset */
4002 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01004003 /* Send the command for reading device ID */
4004 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004005 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01004006 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherf5895d12014-06-24 10:10:04 +02004007 nand_dev_id != chip->read_byte(mtd)) {
4008 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004009 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004010 }
4011 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004012 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004013
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004014#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004015 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004016 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004017#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004018
William Juul52c07962007-10-31 13:53:06 +01004019 /* Store the number of chips and calc total size for mtd */
4020 chip->numchips = i;
4021 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004022
William Juul52c07962007-10-31 13:53:06 +01004023 return 0;
4024}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004025EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004026
William Juul52c07962007-10-31 13:53:06 +01004027
4028/**
4029 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004030 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004031 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004032 * This is the second phase of the normal nand_scan() function. It fills out
4033 * all the uninitialized function pointers with the defaults and scans for a
4034 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004035 */
4036int nand_scan_tail(struct mtd_info *mtd)
4037{
4038 int i;
4039 struct nand_chip *chip = mtd->priv;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004040 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004041 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004042
Sergey Lapin3a38a552013-01-14 03:46:50 +00004043 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4044 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4045 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4046
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004047 if (!(chip->options & NAND_OWN_BUFFERS)) {
4048#ifndef __UBOOT__
4049 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4050 + mtd->oobsize * 3, GFP_KERNEL);
4051 if (!nbuf)
4052 return -ENOMEM;
4053 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4054 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4055 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4056#else
4057 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
4058#endif
4059
4060 chip->buffers = nbuf;
4061 } else {
4062 if (!chip->buffers)
4063 return -ENOMEM;
4064 }
William Juul52c07962007-10-31 13:53:06 +01004065
4066 /* Set the internal oob buffer location, just after the page data */
4067 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4068
4069 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004070 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004071 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004072 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004073 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004074 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004075 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004076 break;
4077 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004078 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004079 break;
4080 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004081 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004082 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004083 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004084 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004085 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004086 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004087 pr_warn("No oob scheme defined for oobsize %d\n",
4088 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004089 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004090 }
4091 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004092
William Juul52c07962007-10-31 13:53:06 +01004093 if (!chip->write_page)
4094 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004095
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004096 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004097 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01004098 * selected and we have 256 byte pagesize fallback to software ECC
4099 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004100
Heiko Schocherf5895d12014-06-24 10:10:04 +02004101 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004102 case NAND_ECC_HW_OOB_FIRST:
4103 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004104 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004105 pr_warn("No ECC functions supplied; "
4106 "hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004107 BUG();
4108 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004109 if (!ecc->read_page)
4110 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004111
William Juul52c07962007-10-31 13:53:06 +01004112 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004113 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004114 if (!ecc->read_page)
4115 ecc->read_page = nand_read_page_hwecc;
4116 if (!ecc->write_page)
4117 ecc->write_page = nand_write_page_hwecc;
4118 if (!ecc->read_page_raw)
4119 ecc->read_page_raw = nand_read_page_raw;
4120 if (!ecc->write_page_raw)
4121 ecc->write_page_raw = nand_write_page_raw;
4122 if (!ecc->read_oob)
4123 ecc->read_oob = nand_read_oob_std;
4124 if (!ecc->write_oob)
4125 ecc->write_oob = nand_write_oob_std;
4126 if (!ecc->read_subpage)
4127 ecc->read_subpage = nand_read_subpage;
4128 if (!ecc->write_subpage)
4129 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004130
William Juul52c07962007-10-31 13:53:06 +01004131 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004132 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4133 (!ecc->read_page ||
4134 ecc->read_page == nand_read_page_hwecc ||
4135 !ecc->write_page ||
4136 ecc->write_page == nand_write_page_hwecc)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004137 pr_warn("No ECC functions supplied; "
4138 "hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01004139 BUG();
4140 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00004141 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004142 if (!ecc->read_page)
4143 ecc->read_page = nand_read_page_syndrome;
4144 if (!ecc->write_page)
4145 ecc->write_page = nand_write_page_syndrome;
4146 if (!ecc->read_page_raw)
4147 ecc->read_page_raw = nand_read_page_raw_syndrome;
4148 if (!ecc->write_page_raw)
4149 ecc->write_page_raw = nand_write_page_raw_syndrome;
4150 if (!ecc->read_oob)
4151 ecc->read_oob = nand_read_oob_syndrome;
4152 if (!ecc->write_oob)
4153 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004154
Heiko Schocherf5895d12014-06-24 10:10:04 +02004155 if (mtd->writesize >= ecc->size) {
4156 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004157 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4158 BUG();
4159 }
William Juul52c07962007-10-31 13:53:06 +01004160 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004161 }
4162 pr_warn("%d byte HW ECC not possible on "
4163 "%d byte page size, fallback to SW ECC\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004164 ecc->size, mtd->writesize);
4165 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004166
William Juul52c07962007-10-31 13:53:06 +01004167 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004168 ecc->calculate = nand_calculate_ecc;
4169 ecc->correct = nand_correct_data;
4170 ecc->read_page = nand_read_page_swecc;
4171 ecc->read_subpage = nand_read_subpage;
4172 ecc->write_page = nand_write_page_swecc;
4173 ecc->read_page_raw = nand_read_page_raw;
4174 ecc->write_page_raw = nand_write_page_raw;
4175 ecc->read_oob = nand_read_oob_std;
4176 ecc->write_oob = nand_write_oob_std;
4177 if (!ecc->size)
4178 ecc->size = 256;
4179 ecc->bytes = 3;
4180 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004181 break;
4182
Christian Hitz55f7bca2011-10-12 09:31:59 +02004183 case NAND_ECC_SOFT_BCH:
4184 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004185 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004186 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02004187 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004188 ecc->calculate = nand_bch_calculate_ecc;
4189 ecc->correct = nand_bch_correct_data;
4190 ecc->read_page = nand_read_page_swecc;
4191 ecc->read_subpage = nand_read_subpage;
4192 ecc->write_page = nand_write_page_swecc;
4193 ecc->read_page_raw = nand_read_page_raw;
4194 ecc->write_page_raw = nand_write_page_raw;
4195 ecc->read_oob = nand_read_oob_std;
4196 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004197 /*
4198 * Board driver should supply ecc.size and ecc.bytes values to
4199 * select how many bits are correctable; see nand_bch_init()
Sergey Lapin3a38a552013-01-14 03:46:50 +00004200 * for details. Otherwise, default to 4 bits for large page
4201 * devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02004202 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004203 if (!ecc->size && (mtd->oobsize >= 64)) {
4204 ecc->size = 512;
4205 ecc->bytes = 7;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004206 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004207 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4208 &ecc->layout);
4209 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004210 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004211 BUG();
4212 }
4213 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Christian Hitz55f7bca2011-10-12 09:31:59 +02004214 break;
4215
William Juul52c07962007-10-31 13:53:06 +01004216 case NAND_ECC_NONE:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004217 pr_warn("NAND_ECC_NONE selected by board driver. "
Heiko Schocherf5895d12014-06-24 10:10:04 +02004218 "This is not recommended!\n");
4219 ecc->read_page = nand_read_page_raw;
4220 ecc->write_page = nand_write_page_raw;
4221 ecc->read_oob = nand_read_oob_std;
4222 ecc->read_page_raw = nand_read_page_raw;
4223 ecc->write_page_raw = nand_write_page_raw;
4224 ecc->write_oob = nand_write_oob_std;
4225 ecc->size = mtd->writesize;
4226 ecc->bytes = 0;
4227 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004228 break;
4229
4230 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004231 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01004232 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004233 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004234
Sergey Lapin3a38a552013-01-14 03:46:50 +00004235 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004236 if (!ecc->read_oob_raw)
4237 ecc->read_oob_raw = ecc->read_oob;
4238 if (!ecc->write_oob_raw)
4239 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004240
William Juul52c07962007-10-31 13:53:06 +01004241 /*
4242 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00004243 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01004244 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004245 ecc->layout->oobavail = 0;
4246 for (i = 0; ecc->layout->oobfree[i].length
4247 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4248 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4249 mtd->oobavail = ecc->layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004250
William Juul52c07962007-10-31 13:53:06 +01004251 /*
4252 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00004253 * mode.
William Juul52c07962007-10-31 13:53:06 +01004254 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004255 ecc->steps = mtd->writesize / ecc->size;
4256 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004257 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01004258 BUG();
4259 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004260 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004261
Sergey Lapin3a38a552013-01-14 03:46:50 +00004262 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004263 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4264 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01004265 case 2:
4266 mtd->subpage_sft = 1;
4267 break;
4268 case 4:
4269 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05004270 case 16:
William Juul52c07962007-10-31 13:53:06 +01004271 mtd->subpage_sft = 2;
4272 break;
4273 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004274 }
William Juul52c07962007-10-31 13:53:06 +01004275 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004276
William Juul52c07962007-10-31 13:53:06 +01004277 /* Initialize state */
4278 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004279
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004280 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01004281 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004282
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004283 /* Large page NAND with SOFT_ECC should support subpage reads */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004284 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004285 chip->options |= NAND_SUBPAGE_READ;
4286
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004287 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004288 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004289 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4290 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004291 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004292#ifndef __UBOOT__
Sergey Lapin3a38a552013-01-14 03:46:50 +00004293 mtd->_point = NULL;
4294 mtd->_unpoint = NULL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004295#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00004296 mtd->_read = nand_read;
4297 mtd->_write = nand_write;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004298 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004299 mtd->_read_oob = nand_read_oob;
4300 mtd->_write_oob = nand_write_oob;
4301 mtd->_sync = nand_sync;
4302 mtd->_lock = NULL;
4303 mtd->_unlock = NULL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004304#ifndef __UBOOT__
4305 mtd->_suspend = nand_suspend;
4306 mtd->_resume = nand_resume;
4307#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00004308 mtd->_block_isbad = nand_block_isbad;
4309 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004310 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004311
Sergey Lapin3a38a552013-01-14 03:46:50 +00004312 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004313 mtd->ecclayout = ecc->layout;
4314 mtd->ecc_strength = ecc->strength;
4315 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004316 /*
4317 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4318 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4319 * properly set.
4320 */
4321 if (!mtd->bitflip_threshold)
4322 mtd->bitflip_threshold = mtd->ecc_strength;
William Juul52c07962007-10-31 13:53:06 +01004323
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02004324 return 0;
William Juul52c07962007-10-31 13:53:06 +01004325}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004326EXPORT_SYMBOL(nand_scan_tail);
4327
4328/*
4329 * is_module_text_address() isn't exported, and it's mostly a pointless
4330 * test if this is a module _anyway_ -- they'd have to try _really_ hard
4331 * to call us from in-kernel code if the core NAND support is modular.
4332 */
4333#ifdef MODULE
4334#define caller_is_module() (1)
4335#else
4336#define caller_is_module() \
4337 is_module_text_address((unsigned long)__builtin_return_address(0))
4338#endif
William Juul52c07962007-10-31 13:53:06 +01004339
William Juul52c07962007-10-31 13:53:06 +01004340/**
4341 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004342 * @mtd: MTD device structure
4343 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01004344 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004345 * This fills out all the uninitialized function pointers with the defaults.
4346 * The flash ID is read and the mtd/chip structures are filled with the
4347 * appropriate values. The mtd->owner field must be set to the module of the
4348 * caller.
William Juul52c07962007-10-31 13:53:06 +01004349 */
4350int nand_scan(struct mtd_info *mtd, int maxchips)
4351{
4352 int ret;
4353
Heiko Schocherf5895d12014-06-24 10:10:04 +02004354 /* Many callers got this wrong, so check for it for a while... */
4355 if (!mtd->owner && caller_is_module()) {
4356 pr_crit("%s called with NULL mtd->owner!\n", __func__);
4357 BUG();
4358 }
4359
Lei Wen75bde942011-01-06 09:48:18 +08004360 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01004361 if (!ret)
4362 ret = nand_scan_tail(mtd);
4363 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004364}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004365EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004366
Heiko Schocherf5895d12014-06-24 10:10:04 +02004367#ifndef __UBOOT__
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004368/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004369 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004370 * @mtd: MTD device structure
4371 */
William Juul52c07962007-10-31 13:53:06 +01004372void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004373{
William Juul52c07962007-10-31 13:53:06 +01004374 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004375
Christian Hitz55f7bca2011-10-12 09:31:59 +02004376 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4377 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4378
Heiko Schocherf5895d12014-06-24 10:10:04 +02004379 mtd_device_unregister(mtd);
William Juul52c07962007-10-31 13:53:06 +01004380
4381 /* Free bad block table memory */
4382 kfree(chip->bbt);
4383 if (!(chip->options & NAND_OWN_BUFFERS))
4384 kfree(chip->buffers);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004385
4386 /* Free bad block descriptor memory */
4387 if (chip->badblock_pattern && chip->badblock_pattern->options
4388 & NAND_BBT_DYNAMICSTRUCT)
4389 kfree(chip->badblock_pattern);
William Juul52c07962007-10-31 13:53:06 +01004390}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004391EXPORT_SYMBOL_GPL(nand_release);
4392
4393static int __init nand_base_init(void)
4394{
4395 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4396 return 0;
4397}
4398
4399static void __exit nand_base_exit(void)
4400{
4401 led_trigger_unregister_simple(nand_led_trigger);
4402}
4403#endif
4404
4405module_init(nand_base_init);
4406module_exit(nand_base_exit);
4407
4408MODULE_LICENSE("GPL");
4409MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4410MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4411MODULE_DESCRIPTION("Generic NAND flash driver code");