blob: c4619e3ac4ab2194947c253de81f5a054401857e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02009 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020014 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 * David Woodhouse for adding multichip support
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
18 * rework for 2K page size chips
19 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020020 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Enable cached programming for 2k page size chips
22 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070023 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030024 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * 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
Ezequiel Garcia20171642013-11-25 08:30:31 -030032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
David Woodhouse552d9202006-05-14 01:20:46 +010034#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h>
36#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020037#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sched.h>
39#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040040#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010045#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020049#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mtd/partitions.h>
Brian Norris5844fee2015-01-23 00:22:27 -080051#include <linux/of_mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020054static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .eccbytes = 3,
56 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020057 .oobfree = {
58 {.offset = 3,
59 .length = 2},
60 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020061 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020064static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .eccbytes = 6,
66 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020067 .oobfree = {
68 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020069 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020072static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .eccbytes = 24,
74 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010075 40, 41, 42, 43, 44, 45, 46, 47,
76 48, 49, 50, 51, 52, 53, 54, 55,
77 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020078 .oobfree = {
79 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020080 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
Thomas Gleixner81ec5362007-12-12 17:27:03 +010083static struct nand_ecclayout nand_oob_128 = {
84 .eccbytes = 48,
85 .eccpos = {
86 80, 81, 82, 83, 84, 85, 86, 87,
87 88, 89, 90, 91, 92, 93, 94, 95,
88 96, 97, 98, 99, 100, 101, 102, 103,
89 104, 105, 106, 107, 108, 109, 110, 111,
90 112, 113, 114, 115, 116, 117, 118, 119,
91 120, 121, 122, 123, 124, 125, 126, 127},
92 .oobfree = {
93 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020094 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010095};
96
Huang Shijie6a8214a2012-11-19 14:43:30 +080097static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020099static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
100 struct mtd_oob_ops *ops);
101
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200102/*
Joe Perches8e87d782008-02-03 17:22:34 +0200103 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200104 * compiled away when LED support is disabled.
105 */
106DEFINE_LED_TRIGGER(nand_led_trigger);
107
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530108static int check_offs_len(struct mtd_info *mtd,
109 loff_t ofs, uint64_t len)
110{
111 struct nand_chip *chip = mtd->priv;
112 int ret = 0;
113
114 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300115 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700116 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530117 ret = -EINVAL;
118 }
119
120 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300121 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700122 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530123 ret = -EINVAL;
124 }
125
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530126 return ret;
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/**
130 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700131 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000132 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800133 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100135static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200137 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200139 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200140 spin_lock(&chip->controller->lock);
141 chip->controller->active = NULL;
142 chip->state = FL_READY;
143 wake_up(&chip->controller->wq);
144 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/**
148 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700149 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700151 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200153static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200155 struct nand_chip *chip = mtd->priv;
156 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/**
Masanari Iida064a7692012-11-09 23:20:58 +0900160 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700161 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700163 * Default read function for 16bit buswidth with endianness conversion.
164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200166static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200168 struct nand_chip *chip = mtd->priv;
169 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700174 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700176 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178static u16 nand_read_word(struct mtd_info *mtd)
179{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200180 struct nand_chip *chip = mtd->priv;
181 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700186 * @mtd: MTD device structure
187 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
189 * Default select function for 1 chip devices.
190 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200191static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200193 struct nand_chip *chip = mtd->priv;
194
195 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200197 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201
202 default:
203 BUG();
204 }
205}
206
207/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100208 * nand_write_byte - [DEFAULT] write single byte to chip
209 * @mtd: MTD device structure
210 * @byte: value to write
211 *
212 * Default function to write a byte to I/O[7:0]
213 */
214static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
215{
216 struct nand_chip *chip = mtd->priv;
217
218 chip->write_buf(mtd, &byte, 1);
219}
220
221/**
222 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
223 * @mtd: MTD device structure
224 * @byte: value to write
225 *
226 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
227 */
228static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
229{
230 struct nand_chip *chip = mtd->priv;
231 uint16_t word = byte;
232
233 /*
234 * It's not entirely clear what should happen to I/O[15:8] when writing
235 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
236 *
237 * When the host supports a 16-bit bus width, only data is
238 * transferred at the 16-bit width. All address and command line
239 * transfers shall use only the lower 8-bits of the data bus. During
240 * command transfers, the host may place any value on the upper
241 * 8-bits of the data bus. During address transfers, the host shall
242 * set the upper 8-bits of the data bus to 00h.
243 *
244 * One user of the write_byte callback is nand_onfi_set_features. The
245 * four parameters are specified to be written to I/O[7:0], but this is
246 * neither an address nor a command transfer. Let's assume a 0 on the
247 * upper I/O lines is OK.
248 */
249 chip->write_buf(mtd, (uint8_t *)&word, 2);
250}
251
252/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700254 * @mtd: MTD device structure
255 * @buf: data buffer
256 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700258 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200260static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Alexander Shiyan76413832013-04-13 09:32:13 +0400264 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000268 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
270 * @buf: buffer to store date
271 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700273 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200275static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Alexander Shiyan76413832013-04-13 09:32:13 +0400279 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700284 * @mtd: MTD device structure
285 * @buf: data buffer
286 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700288 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200290static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200292 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000294
Alexander Shiyan76413832013-04-13 09:32:13 +0400295 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000299 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700300 * @mtd: MTD device structure
301 * @buf: buffer to store date
302 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700304 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200306static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200308 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alexander Shiyan76413832013-04-13 09:32:13 +0400311 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700316 * @mtd: MTD device structure
317 * @ofs: offset from device start
318 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000320 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
322static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
323{
Brian Norriscdbec052012-01-13 18:11:48 -0800324 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200325 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 bad;
327
Brian Norris5fb15492011-05-31 16:31:21 -0700328 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700329 ofs += mtd->erasesize - mtd->writesize;
330
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100331 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200334 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Huang Shijie6a8214a2012-11-19 14:43:30 +0800336 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200339 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Brian Norriscdbec052012-01-13 18:11:48 -0800342 do {
343 if (chip->options & NAND_BUSWIDTH_16) {
344 chip->cmdfunc(mtd, NAND_CMD_READOOB,
345 chip->badblockpos & 0xFE, page);
346 bad = cpu_to_le16(chip->read_word(mtd));
347 if (chip->badblockpos & 0x1)
348 bad >>= 8;
349 else
350 bad &= 0xFF;
351 } else {
352 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
353 page);
354 bad = chip->read_byte(mtd);
355 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000356
Brian Norriscdbec052012-01-13 18:11:48 -0800357 if (likely(chip->badblockbits == 8))
358 res = bad != 0xFF;
359 else
360 res = hweight8(bad) < chip->badblockbits;
361 ofs += mtd->writesize;
362 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
363 i++;
364 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200365
Huang Shijieb0bb6902012-11-19 14:43:29 +0800366 if (getchip) {
367 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800369 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return res;
372}
373
374/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700375 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700376 * @mtd: MTD device structure
377 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700379 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700380 * specific driver. It provides the details for writing a bad block marker to a
381 * block.
382 */
383static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
384{
385 struct nand_chip *chip = mtd->priv;
386 struct mtd_oob_ops ops;
387 uint8_t buf[2] = { 0, 0 };
388 int ret = 0, res, i = 0;
389
Brian Norris0ec56dc2015-02-28 02:02:30 -0800390 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700391 ops.oobbuf = buf;
392 ops.ooboffs = chip->badblockpos;
393 if (chip->options & NAND_BUSWIDTH_16) {
394 ops.ooboffs &= ~0x01;
395 ops.len = ops.ooblen = 2;
396 } else {
397 ops.len = ops.ooblen = 1;
398 }
399 ops.mode = MTD_OPS_PLACE_OOB;
400
401 /* Write to first/last page(s) if necessary */
402 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
403 ofs += mtd->erasesize - mtd->writesize;
404 do {
405 res = nand_do_write_oob(mtd, ofs, &ops);
406 if (!ret)
407 ret = res;
408
409 i++;
410 ofs += mtd->writesize;
411 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
412
413 return ret;
414}
415
416/**
417 * nand_block_markbad_lowlevel - mark a block bad
418 * @mtd: MTD device structure
419 * @ofs: offset from device start
420 *
421 * This function performs the generic NAND bad block marking steps (i.e., bad
422 * block table(s) and/or marker(s)). We only allow the hardware driver to
423 * specify how to write bad block markers to OOB (chip->block_markbad).
424 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700425 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800426 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700427 * (2) write bad block marker to OOB area of affected block (unless flag
428 * NAND_BBT_NO_OOB_BBM is present)
429 * (3) update the BBT
430 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800431 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700433static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200435 struct nand_chip *chip = mtd->priv;
Brian Norrisb32843b2013-07-30 17:52:59 -0700436 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000437
Brian Norrisb32843b2013-07-30 17:52:59 -0700438 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800439 struct erase_info einfo;
440
441 /* Attempt erase before marking OOB */
442 memset(&einfo, 0, sizeof(einfo));
443 einfo.mtd = mtd;
444 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300445 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800446 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800447
Brian Norrisb32843b2013-07-30 17:52:59 -0700448 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800449 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700450 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300451 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200452 }
Brian Norrise2414f42012-02-06 13:44:00 -0800453
Brian Norrisb32843b2013-07-30 17:52:59 -0700454 /* Mark block bad in BBT */
455 if (chip->bbt) {
456 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800457 if (!ret)
458 ret = res;
459 }
460
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200461 if (!ret)
462 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300463
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700469 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700471 * Check, if the device is write protected. The function expects, that the
472 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100474static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200476 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200477
Brian Norris8b6e50c2011-05-25 14:59:01 -0700478 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200479 if (chip->options & NAND_BROKEN_XD)
480 return 0;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200483 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
484 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800488 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 * @mtd: MTD device structure
490 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300491 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800492 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300493 */
494static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
495{
496 struct nand_chip *chip = mtd->priv;
497
498 if (!chip->bbt)
499 return 0;
500 /* Return info from the table */
501 return nand_isreserved_bbt(mtd, ofs);
502}
503
504/**
505 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
506 * @mtd: MTD device structure
507 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700508 * @getchip: 0, if the chip is already selected
509 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
511 * Check, if the block is bad. Either by reading the bad block table or
512 * calling of the scan function.
513 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200514static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
515 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200517 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000518
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200519 if (!chip->bbt)
520 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100523 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200526/**
527 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700528 * @mtd: MTD device structure
529 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200530 *
531 * Helper function for nand_wait_ready used when needing to wait in interrupt
532 * context.
533 */
534static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
535{
536 struct nand_chip *chip = mtd->priv;
537 int i;
538
539 /* Wait for the device to get ready */
540 for (i = 0; i < timeo; i++) {
541 if (chip->dev_ready(mtd))
542 break;
543 touch_softlockup_watchdog();
544 mdelay(1);
545 }
546}
547
Brian Norris7854d3f2011-06-23 14:12:08 -0700548/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100549void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000550{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200551 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100552 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000553
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200554 /* 400ms timeout */
555 if (in_interrupt() || oops_in_progress)
556 return panic_nand_wait_ready(mtd, 400);
557
Richard Purdie8fe833c2006-03-31 02:31:14 -0800558 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700559 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000560 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200561 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800562 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700563 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000564 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800565 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000566}
David Woodhouse4b648b02006-09-25 17:05:24 +0100567EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200570 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
571 * @mtd: MTD device structure
572 * @timeo: Timeout in ms
573 *
574 * Wait for status ready (i.e. command done) or timeout.
575 */
576static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
577{
578 register struct nand_chip *chip = mtd->priv;
579
580 timeo = jiffies + msecs_to_jiffies(timeo);
581 do {
582 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
583 break;
584 touch_softlockup_watchdog();
585 } while (time_before(jiffies, timeo));
586};
587
588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700590 * @mtd: MTD device structure
591 * @command: the command to be sent
592 * @column: the column address for this command, -1 if none
593 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200596 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200598static void nand_command(struct mtd_info *mtd, unsigned int command,
599 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200601 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200602 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Brian Norris8b6e50c2011-05-25 14:59:01 -0700604 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (command == NAND_CMD_SEQIN) {
606 int readcmd;
607
Joern Engel28318772006-05-22 23:18:05 +0200608 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200610 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 readcmd = NAND_CMD_READOOB;
612 } else if (column < 256) {
613 /* First 256 bytes --> READ0 */
614 readcmd = NAND_CMD_READ0;
615 } else {
616 column -= 256;
617 readcmd = NAND_CMD_READ1;
618 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200619 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200620 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200622 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Brian Norris8b6e50c2011-05-25 14:59:01 -0700624 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200625 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
626 /* Serially input address */
627 if (column != -1) {
628 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800629 if (chip->options & NAND_BUSWIDTH_16 &&
630 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200631 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200632 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200633 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200635 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200636 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200637 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200638 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200639 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200640 if (chip->chipsize > (32 << 20))
641 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200643 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000644
645 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700646 * Program and erase have their own busy handlers status and sequential
647 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 case NAND_CMD_PAGEPROG:
652 case NAND_CMD_ERASE1:
653 case NAND_CMD_ERASE2:
654 case NAND_CMD_SEQIN:
655 case NAND_CMD_STATUS:
656 return;
657
658 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200659 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200661 udelay(chip->chip_delay);
662 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200663 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200664 chip->cmd_ctrl(mtd,
665 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200666 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
667 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return;
669
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000672 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * If we don't have access to the busy pin, we apply the given
674 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100675 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200676 if (!chip->dev_ready) {
677 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700681 /*
682 * Apply this short delay always to ensure that we do wait tWB in
683 * any case on any machine.
684 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100685 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000686
687 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/**
691 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700692 * @mtd: MTD device structure
693 * @command: the command to be sent
694 * @column: the column address for this command, -1 if none
695 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200697 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700698 * devices. We don't have the separate regions as we have in the small page
699 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200701static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
702 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200704 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* Emulate NAND_CMD_READOOB */
707 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200708 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 command = NAND_CMD_READ0;
710 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000711
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200712 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400713 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /* Serially input address */
719 if (column != -1) {
720 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800721 if (chip->options & NAND_BUSWIDTH_16 &&
722 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200725 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200726 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200729 chip->cmd_ctrl(mtd, page_addr, ctrl);
730 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200731 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200733 if (chip->chipsize > (128 << 20))
734 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200735 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200738 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000739
740 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700741 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100742 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 case NAND_CMD_CACHEDPROG:
747 case NAND_CMD_PAGEPROG:
748 case NAND_CMD_ERASE1:
749 case NAND_CMD_ERASE2:
750 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200751 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000753 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200756 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200758 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200759 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
760 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
761 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
762 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200763 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
764 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return;
766
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200767 case NAND_CMD_RNDOUT:
768 /* No ready / busy check necessary */
769 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
770 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
771 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
772 NAND_NCE | NAND_CTRL_CHANGE);
773 return;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200776 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
777 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
778 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
779 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000780
David Woodhousee0c7d762006-05-13 18:07:53 +0100781 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000783 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700785 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100786 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200787 if (!chip->dev_ready) {
788 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000792
Brian Norris8b6e50c2011-05-25 14:59:01 -0700793 /*
794 * Apply this short delay always to ensure that we do wait tWB in
795 * any case on any machine.
796 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100797 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000798
799 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200803 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700804 * @chip: the nand chip descriptor
805 * @mtd: MTD device structure
806 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200807 *
808 * Used when in panic, no locks are taken.
809 */
810static void panic_nand_get_device(struct nand_chip *chip,
811 struct mtd_info *mtd, int new_state)
812{
Brian Norris7854d3f2011-06-23 14:12:08 -0700813 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200814 chip->controller->active = chip;
815 chip->state = new_state;
816}
817
818/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700820 * @mtd: MTD device structure
821 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *
823 * Get the device and lock it for exclusive access
824 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200825static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800826nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800828 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200829 spinlock_t *lock = &chip->controller->lock;
830 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200832retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100833 spin_lock(lock);
834
vimal singhb8b3ee92009-07-09 20:41:22 +0530835 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200836 if (!chip->controller->active)
837 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200838
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200839 if (chip->controller->active == chip && chip->state == FL_READY) {
840 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100841 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100842 return 0;
843 }
844 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800845 if (chip->controller->active->state == FL_PM_SUSPENDED) {
846 chip->state = FL_PM_SUSPENDED;
847 spin_unlock(lock);
848 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800849 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100850 }
851 set_current_state(TASK_UNINTERRUPTIBLE);
852 add_wait_queue(wq, &wait);
853 spin_unlock(lock);
854 schedule();
855 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto retry;
857}
858
859/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 * panic_nand_wait - [GENERIC] wait until the command is done
861 * @mtd: MTD device structure
862 * @chip: NAND chip structure
863 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200864 *
865 * Wait for command done. This is a helper function for nand_wait used when
866 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400867 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200868 */
869static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
870 unsigned long timeo)
871{
872 int i;
873 for (i = 0; i < timeo; i++) {
874 if (chip->dev_ready) {
875 if (chip->dev_ready(mtd))
876 break;
877 } else {
878 if (chip->read_byte(mtd) & NAND_STATUS_READY)
879 break;
880 }
881 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200882 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200883}
884
885/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700886 * nand_wait - [DEFAULT] wait until the command is done
887 * @mtd: MTD device structure
888 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700890 * Wait for command done. This applies to erase and program only. Erase can
891 * take up to 400ms and program up to 20ms according to general NAND and
892 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700893 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200894static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200897 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800898 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Richard Purdie8fe833c2006-03-31 02:31:14 -0800900 led_trigger_event(nand_led_trigger, LED_FULL);
901
Brian Norris8b6e50c2011-05-25 14:59:01 -0700902 /*
903 * Apply this short delay always to ensure that we do wait tWB in any
904 * case on any machine.
905 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100906 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200908 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200910 if (in_interrupt() || oops_in_progress)
911 panic_nand_wait(mtd, chip, timeo);
912 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800913 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200914 while (time_before(jiffies, timeo)) {
915 if (chip->dev_ready) {
916 if (chip->dev_ready(mtd))
917 break;
918 } else {
919 if (chip->read_byte(mtd) & NAND_STATUS_READY)
920 break;
921 }
922 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800925 led_trigger_event(nand_led_trigger, LED_OFF);
926
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200927 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100928 /* This can happen if in case of timeout or buggy dev_ready */
929 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return status;
931}
932
933/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700934 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700935 * @mtd: mtd info
936 * @ofs: offset to start unlock from
937 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700938 * @invert: when = 0, unlock the range of blocks within the lower and
939 * upper boundary address
940 * when = 1, unlock the range of blocks outside the boundaries
941 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530942 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700943 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530944 */
945static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
946 uint64_t len, int invert)
947{
948 int ret = 0;
949 int status, page;
950 struct nand_chip *chip = mtd->priv;
951
952 /* Submit address of first page to unlock */
953 page = ofs >> chip->page_shift;
954 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
955
956 /* Submit address of last page to unlock */
957 page = (ofs + len) >> chip->page_shift;
958 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
959 (page | invert) & chip->pagemask);
960
961 /* Call wait ready function */
962 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530963 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400964 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700965 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530966 __func__, status);
967 ret = -EIO;
968 }
969
970 return ret;
971}
972
973/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700974 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700975 * @mtd: mtd info
976 * @ofs: offset to start unlock from
977 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530978 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700979 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530980 */
981int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
982{
983 int ret = 0;
984 int chipnr;
985 struct nand_chip *chip = mtd->priv;
986
Brian Norris289c0522011-07-19 10:06:09 -0700987 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530988 __func__, (unsigned long long)ofs, len);
989
990 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -0800991 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +0530992
993 /* Align to last block address if size addresses end of the device */
994 if (ofs + len == mtd->size)
995 len -= mtd->erasesize;
996
Huang Shijie6a8214a2012-11-19 14:43:30 +0800997 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530998
999 /* Shift to get chip number */
1000 chipnr = ofs >> chip->chip_shift;
1001
1002 chip->select_chip(mtd, chipnr);
1003
White Ding57d3a9a2014-07-24 00:10:45 +08001004 /*
1005 * Reset the chip.
1006 * If we want to check the WP through READ STATUS and check the bit 7
1007 * we must reset the chip
1008 * some operation can also clear the bit 7 of status register
1009 * eg. erase/program a locked block
1010 */
1011 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1012
Vimal Singh7d70f332010-02-08 15:50:49 +05301013 /* Check, if it is write protected */
1014 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001015 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301016 __func__);
1017 ret = -EIO;
1018 goto out;
1019 }
1020
1021 ret = __nand_unlock(mtd, ofs, len, 0);
1022
1023out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001024 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301025 nand_release_device(mtd);
1026
1027 return ret;
1028}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001029EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301030
1031/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001032 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001033 * @mtd: mtd info
1034 * @ofs: offset to start unlock from
1035 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301036 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001037 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1038 * have this feature, but it allows only to lock all blocks, not for specified
1039 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1040 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301041 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001042 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301043 */
1044int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1045{
1046 int ret = 0;
1047 int chipnr, status, page;
1048 struct nand_chip *chip = mtd->priv;
1049
Brian Norris289c0522011-07-19 10:06:09 -07001050 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301051 __func__, (unsigned long long)ofs, len);
1052
1053 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001054 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301055
Huang Shijie6a8214a2012-11-19 14:43:30 +08001056 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301057
1058 /* Shift to get chip number */
1059 chipnr = ofs >> chip->chip_shift;
1060
1061 chip->select_chip(mtd, chipnr);
1062
White Ding57d3a9a2014-07-24 00:10:45 +08001063 /*
1064 * Reset the chip.
1065 * If we want to check the WP through READ STATUS and check the bit 7
1066 * we must reset the chip
1067 * some operation can also clear the bit 7 of status register
1068 * eg. erase/program a locked block
1069 */
1070 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1071
Vimal Singh7d70f332010-02-08 15:50:49 +05301072 /* Check, if it is write protected */
1073 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001074 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301075 __func__);
1076 status = MTD_ERASE_FAILED;
1077 ret = -EIO;
1078 goto out;
1079 }
1080
1081 /* Submit address of first page to lock */
1082 page = ofs >> chip->page_shift;
1083 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1084
1085 /* Call wait ready function */
1086 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301087 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001088 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001089 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301090 __func__, status);
1091 ret = -EIO;
1092 goto out;
1093 }
1094
1095 ret = __nand_unlock(mtd, ofs, len, 0x1);
1096
1097out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001098 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301099 nand_release_device(mtd);
1100
1101 return ret;
1102}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001103EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301104
1105/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001106 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001107 * @mtd: mtd info structure
1108 * @chip: nand chip info structure
1109 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001110 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001111 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001112 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001113 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001114 */
1115static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001116 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001117{
1118 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001119 if (oob_required)
1120 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001121 return 0;
1122}
1123
1124/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001125 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001126 * @mtd: mtd info structure
1127 * @chip: nand chip info structure
1128 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001129 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001130 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001131 *
1132 * We need a special oob layout and handling even when OOB isn't used.
1133 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001134static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001135 struct nand_chip *chip, uint8_t *buf,
1136 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001137{
1138 int eccsize = chip->ecc.size;
1139 int eccbytes = chip->ecc.bytes;
1140 uint8_t *oob = chip->oob_poi;
1141 int steps, size;
1142
1143 for (steps = chip->ecc.steps; steps > 0; steps--) {
1144 chip->read_buf(mtd, buf, eccsize);
1145 buf += eccsize;
1146
1147 if (chip->ecc.prepad) {
1148 chip->read_buf(mtd, oob, chip->ecc.prepad);
1149 oob += chip->ecc.prepad;
1150 }
1151
1152 chip->read_buf(mtd, oob, eccbytes);
1153 oob += eccbytes;
1154
1155 if (chip->ecc.postpad) {
1156 chip->read_buf(mtd, oob, chip->ecc.postpad);
1157 oob += chip->ecc.postpad;
1158 }
1159 }
1160
1161 size = mtd->oobsize - (oob - chip->oob_poi);
1162 if (size)
1163 chip->read_buf(mtd, oob, size);
1164
1165 return 0;
1166}
1167
1168/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001169 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001170 * @mtd: mtd info structure
1171 * @chip: nand chip info structure
1172 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001173 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001174 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001175 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001176static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001177 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001179 int i, eccsize = chip->ecc.size;
1180 int eccbytes = chip->ecc.bytes;
1181 int eccsteps = chip->ecc.steps;
1182 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001183 uint8_t *ecc_calc = chip->buffers->ecccalc;
1184 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001185 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001186 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001187
Brian Norris1fbb9382012-05-02 10:14:55 -07001188 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001189
1190 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1191 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1192
1193 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001194 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001195
1196 eccsteps = chip->ecc.steps;
1197 p = buf;
1198
1199 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1200 int stat;
1201
1202 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001203 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001204 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001205 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001206 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001207 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1208 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001209 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001210 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301214 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001215 * @mtd: mtd info structure
1216 * @chip: nand chip info structure
1217 * @data_offs: offset of requested data within the page
1218 * @readlen: data length
1219 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001220 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001221 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001222static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001223 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1224 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001225{
1226 int start_step, end_step, num_steps;
1227 uint32_t *eccpos = chip->ecc.layout->eccpos;
1228 uint8_t *p;
1229 int data_col_addr, i, gaps = 0;
1230 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1231 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301232 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001233 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001234
Brian Norris7854d3f2011-06-23 14:12:08 -07001235 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001236 start_step = data_offs / chip->ecc.size;
1237 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1238 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301239 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001240
Brian Norris8b6e50c2011-05-25 14:59:01 -07001241 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001242 datafrag_len = num_steps * chip->ecc.size;
1243 eccfrag_len = num_steps * chip->ecc.bytes;
1244
1245 data_col_addr = start_step * chip->ecc.size;
1246 /* If we read not a page aligned data */
1247 if (data_col_addr != 0)
1248 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1249
1250 p = bufpoi + data_col_addr;
1251 chip->read_buf(mtd, p, datafrag_len);
1252
Brian Norris8b6e50c2011-05-25 14:59:01 -07001253 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001254 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1255 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1256
Brian Norris8b6e50c2011-05-25 14:59:01 -07001257 /*
1258 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001259 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001260 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001261 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301262 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001263 gaps = 1;
1264 break;
1265 }
1266 }
1267 if (gaps) {
1268 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1269 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1270 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001271 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001272 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001273 * about buswidth alignment in read_buf.
1274 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001275 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001276 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001277 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001278 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001279 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001280 aligned_len++;
1281
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001282 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1283 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001284 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1285 }
1286
1287 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001288 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001289
1290 p = bufpoi + data_col_addr;
1291 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1292 int stat;
1293
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001294 stat = chip->ecc.correct(mtd, p,
1295 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001296 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001297 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001298 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001299 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001300 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1301 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001302 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001303 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001304}
1305
1306/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001307 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001308 * @mtd: mtd info structure
1309 * @chip: nand chip info structure
1310 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001311 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001312 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001313 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001314 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001315 */
1316static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001317 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001318{
1319 int i, eccsize = chip->ecc.size;
1320 int eccbytes = chip->ecc.bytes;
1321 int eccsteps = chip->ecc.steps;
1322 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001323 uint8_t *ecc_calc = chip->buffers->ecccalc;
1324 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001325 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001326 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001327
1328 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1329 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1330 chip->read_buf(mtd, p, eccsize);
1331 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1332 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001333 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001334
1335 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001336 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001337
1338 eccsteps = chip->ecc.steps;
1339 p = buf;
1340
1341 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1342 int stat;
1343
1344 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001345 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001346 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001347 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001348 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001349 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1350 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001351 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001352 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001353}
1354
1355/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001356 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001357 * @mtd: mtd info structure
1358 * @chip: nand chip info structure
1359 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001360 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001361 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001362 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001363 * Hardware ECC for large page chips, require OOB to be read first. For this
1364 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1365 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1366 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1367 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001368 */
1369static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001370 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001371{
1372 int i, eccsize = chip->ecc.size;
1373 int eccbytes = chip->ecc.bytes;
1374 int eccsteps = chip->ecc.steps;
1375 uint8_t *p = buf;
1376 uint8_t *ecc_code = chip->buffers->ecccode;
1377 uint32_t *eccpos = chip->ecc.layout->eccpos;
1378 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001379 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001380
1381 /* Read the OOB area first */
1382 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1383 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1384 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1385
1386 for (i = 0; i < chip->ecc.total; i++)
1387 ecc_code[i] = chip->oob_poi[eccpos[i]];
1388
1389 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1390 int stat;
1391
1392 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1393 chip->read_buf(mtd, p, eccsize);
1394 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1395
1396 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001397 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001398 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001399 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001400 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001401 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1402 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001403 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001404 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001405}
1406
1407/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001408 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001409 * @mtd: mtd info structure
1410 * @chip: nand chip info structure
1411 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001412 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001413 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001414 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001415 * The hw generator calculates the error syndrome automatically. Therefore we
1416 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001417 */
1418static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001419 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001420{
1421 int i, eccsize = chip->ecc.size;
1422 int eccbytes = chip->ecc.bytes;
1423 int eccsteps = chip->ecc.steps;
1424 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001425 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001426 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001427
1428 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1429 int stat;
1430
1431 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1432 chip->read_buf(mtd, p, eccsize);
1433
1434 if (chip->ecc.prepad) {
1435 chip->read_buf(mtd, oob, chip->ecc.prepad);
1436 oob += chip->ecc.prepad;
1437 }
1438
1439 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1440 chip->read_buf(mtd, oob, eccbytes);
1441 stat = chip->ecc.correct(mtd, p, oob, NULL);
1442
Mike Dunn3f91e942012-04-25 12:06:09 -07001443 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001445 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001446 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001447 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1448 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001449
1450 oob += eccbytes;
1451
1452 if (chip->ecc.postpad) {
1453 chip->read_buf(mtd, oob, chip->ecc.postpad);
1454 oob += chip->ecc.postpad;
1455 }
1456 }
1457
1458 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001459 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001460 if (i)
1461 chip->read_buf(mtd, oob, i);
1462
Mike Dunn3f91e942012-04-25 12:06:09 -07001463 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001464}
1465
1466/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001467 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001468 * @chip: nand chip structure
1469 * @oob: oob destination address
1470 * @ops: oob ops structure
1471 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001472 */
1473static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001474 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001475{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001476 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001477
Brian Norris0612b9d2011-08-30 18:45:40 -07001478 case MTD_OPS_PLACE_OOB:
1479 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001480 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1481 return oob + len;
1482
Brian Norris0612b9d2011-08-30 18:45:40 -07001483 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001484 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001485 uint32_t boffs = 0, roffs = ops->ooboffs;
1486 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001487
Florian Fainellif8ac0412010-09-07 13:23:43 +02001488 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001489 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001490 if (unlikely(roffs)) {
1491 if (roffs >= free->length) {
1492 roffs -= free->length;
1493 continue;
1494 }
1495 boffs = free->offset + roffs;
1496 bytes = min_t(size_t, len,
1497 (free->length - roffs));
1498 roffs = 0;
1499 } else {
1500 bytes = min_t(size_t, len, free->length);
1501 boffs = free->offset;
1502 }
1503 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001504 oob += bytes;
1505 }
1506 return oob;
1507 }
1508 default:
1509 BUG();
1510 }
1511 return NULL;
1512}
1513
1514/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001515 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1516 * @mtd: MTD device structure
1517 * @retry_mode: the retry mode to use
1518 *
1519 * Some vendors supply a special command to shift the Vt threshold, to be used
1520 * when there are too many bitflips in a page (i.e., ECC error). After setting
1521 * a new threshold, the host should retry reading the page.
1522 */
1523static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1524{
1525 struct nand_chip *chip = mtd->priv;
1526
1527 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1528
1529 if (retry_mode >= chip->read_retries)
1530 return -EINVAL;
1531
1532 if (!chip->setup_read_retry)
1533 return -EOPNOTSUPP;
1534
1535 return chip->setup_read_retry(mtd, retry_mode);
1536}
1537
1538/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001539 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001540 * @mtd: MTD device structure
1541 * @from: offset to read from
1542 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001543 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001544 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001545 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001546static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1547 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001548{
Brian Norrise47f3db2012-05-02 10:14:56 -07001549 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001550 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001551 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001552 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001553 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001554 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001555 mtd->oobavail : mtd->oobsize;
1556
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001557 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001558 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001559 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001560 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001561 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001563 chipnr = (int)(from >> chip->chip_shift);
1564 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001566 realpage = (int)(from >> chip->page_shift);
1567 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001569 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001571 buf = ops->datbuf;
1572 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001573 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001574
Florian Fainellif8ac0412010-09-07 13:23:43 +02001575 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001576 unsigned int ecc_failures = mtd->ecc_stats.failed;
1577
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001578 bytes = min(mtd->writesize - col, readlen);
1579 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001580
Kamal Dasu66507c72014-05-01 20:51:19 -04001581 if (!aligned)
1582 use_bufpoi = 1;
1583 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1584 use_bufpoi = !virt_addr_valid(buf);
1585 else
1586 use_bufpoi = 0;
1587
Brian Norris8b6e50c2011-05-25 14:59:01 -07001588 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001589 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001590 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1591
1592 if (use_bufpoi && aligned)
1593 pr_debug("%s: using read bounce buffer for buf@%p\n",
1594 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Brian Norrisba84fb52014-01-03 15:13:33 -08001596read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001597 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Mike Dunnedbc45402012-04-25 12:06:11 -07001599 /*
1600 * Now read the page into the buffer. Absent an error,
1601 * the read methods return max bitflips per ecc step.
1602 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001603 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001604 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001605 oob_required,
1606 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001607 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1608 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001609 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001610 col, bytes, bufpoi,
1611 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001612 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001613 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001614 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001615 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001616 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001617 /* Invalidate page cache */
1618 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001619 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001620 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001621
Mike Dunnedbc45402012-04-25 12:06:11 -07001622 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1623
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001624 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001625 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001626 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001627 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001628 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001629 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001630 chip->pagebuf_bitflips = ret;
1631 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001632 /* Invalidate page cache */
1633 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001634 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001635 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001637
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001638 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001639 int toread = min(oobreadlen, max_oobsize);
1640
1641 if (toread) {
1642 oob = nand_transfer_oob(chip,
1643 oob, ops, toread);
1644 oobreadlen -= toread;
1645 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001646 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001647
1648 if (chip->options & NAND_NEED_READRDY) {
1649 /* Apply delay or wait for ready/busy pin */
1650 if (!chip->dev_ready)
1651 udelay(chip->chip_delay);
1652 else
1653 nand_wait_ready(mtd);
1654 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001655
Brian Norrisba84fb52014-01-03 15:13:33 -08001656 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001657 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001658 retry_mode++;
1659 ret = nand_setup_read_retry(mtd,
1660 retry_mode);
1661 if (ret < 0)
1662 break;
1663
1664 /* Reset failures; retry */
1665 mtd->ecc_stats.failed = ecc_failures;
1666 goto read_retry;
1667 } else {
1668 /* No more retry modes; real failure */
1669 ecc_fail = true;
1670 }
1671 }
1672
1673 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001674 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001675 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001676 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001677 max_bitflips = max_t(unsigned int, max_bitflips,
1678 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001681 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001682
Brian Norrisba84fb52014-01-03 15:13:33 -08001683 /* Reset to retry mode 0 */
1684 if (retry_mode) {
1685 ret = nand_setup_read_retry(mtd, 0);
1686 if (ret < 0)
1687 break;
1688 retry_mode = 0;
1689 }
1690
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001691 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Brian Norris8b6e50c2011-05-25 14:59:01 -07001694 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 col = 0;
1696 /* Increment page address */
1697 realpage++;
1698
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001699 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Check, if we cross a chip boundary */
1701 if (!page) {
1702 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001703 chip->select_chip(mtd, -1);
1704 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001707 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001709 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001710 if (oob)
1711 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Mike Dunn3f91e942012-04-25 12:06:09 -07001713 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001714 return ret;
1715
Brian Norrisb72f3df2013-12-03 11:04:14 -08001716 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001717 return -EBADMSG;
1718
Mike Dunnedbc45402012-04-25 12:06:11 -07001719 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001720}
1721
1722/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001723 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001724 * @mtd: MTD device structure
1725 * @from: offset to read from
1726 * @len: number of bytes to read
1727 * @retlen: pointer to variable to store the number of read bytes
1728 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001729 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001730 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001731 */
1732static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1733 size_t *retlen, uint8_t *buf)
1734{
Brian Norris4a89ff82011-08-30 18:45:45 -07001735 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001736 int ret;
1737
Huang Shijie6a8214a2012-11-19 14:43:30 +08001738 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001739 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001740 ops.len = len;
1741 ops.datbuf = buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08001742 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001743 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001744 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001745 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001746 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001750 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001751 * @mtd: mtd info structure
1752 * @chip: nand chip info structure
1753 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001754 */
1755static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001756 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001757{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001758 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001759 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001760 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001761}
1762
1763/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001764 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001765 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001766 * @mtd: mtd info structure
1767 * @chip: nand chip info structure
1768 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001769 */
1770static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001771 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001772{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001773 int length = mtd->oobsize;
1774 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1775 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001776 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001777 int i, toread, sndrnd = 0, pos;
1778
1779 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1780 for (i = 0; i < chip->ecc.steps; i++) {
1781 if (sndrnd) {
1782 pos = eccsize + i * (eccsize + chunk);
1783 if (mtd->writesize > 512)
1784 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1785 else
1786 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1787 } else
1788 sndrnd = 1;
1789 toread = min_t(int, length, chunk);
1790 chip->read_buf(mtd, bufpoi, toread);
1791 bufpoi += toread;
1792 length -= toread;
1793 }
1794 if (length > 0)
1795 chip->read_buf(mtd, bufpoi, length);
1796
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001797 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001798}
1799
1800/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001801 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001802 * @mtd: mtd info structure
1803 * @chip: nand chip info structure
1804 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001805 */
1806static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1807 int page)
1808{
1809 int status = 0;
1810 const uint8_t *buf = chip->oob_poi;
1811 int length = mtd->oobsize;
1812
1813 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1814 chip->write_buf(mtd, buf, length);
1815 /* Send command to program the OOB data */
1816 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1817
1818 status = chip->waitfunc(mtd, chip);
1819
Savin Zlobec0d420f92006-06-21 11:51:20 +02001820 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001821}
1822
1823/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001824 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001825 * with syndrome - only for large page flash
1826 * @mtd: mtd info structure
1827 * @chip: nand chip info structure
1828 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001829 */
1830static int nand_write_oob_syndrome(struct mtd_info *mtd,
1831 struct nand_chip *chip, int page)
1832{
1833 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1834 int eccsize = chip->ecc.size, length = mtd->oobsize;
1835 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1836 const uint8_t *bufpoi = chip->oob_poi;
1837
1838 /*
1839 * data-ecc-data-ecc ... ecc-oob
1840 * or
1841 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1842 */
1843 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1844 pos = steps * (eccsize + chunk);
1845 steps = 0;
1846 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001847 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001848
1849 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1850 for (i = 0; i < steps; i++) {
1851 if (sndcmd) {
1852 if (mtd->writesize <= 512) {
1853 uint32_t fill = 0xFFFFFFFF;
1854
1855 len = eccsize;
1856 while (len > 0) {
1857 int num = min_t(int, len, 4);
1858 chip->write_buf(mtd, (uint8_t *)&fill,
1859 num);
1860 len -= num;
1861 }
1862 } else {
1863 pos = eccsize + i * (eccsize + chunk);
1864 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1865 }
1866 } else
1867 sndcmd = 1;
1868 len = min_t(int, length, chunk);
1869 chip->write_buf(mtd, bufpoi, len);
1870 bufpoi += len;
1871 length -= len;
1872 }
1873 if (length > 0)
1874 chip->write_buf(mtd, bufpoi, length);
1875
1876 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1877 status = chip->waitfunc(mtd, chip);
1878
1879 return status & NAND_STATUS_FAIL ? -EIO : 0;
1880}
1881
1882/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001883 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001884 * @mtd: MTD device structure
1885 * @from: offset to read from
1886 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001888 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001890static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1891 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Brian Norrisc00a0992012-05-01 17:12:54 -07001893 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001894 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001895 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001896 int readlen = ops->ooblen;
1897 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001898 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001899 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Brian Norris289c0522011-07-19 10:06:09 -07001901 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301902 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Brian Norris041e4572011-06-23 16:45:24 -07001904 stats = mtd->ecc_stats;
1905
Brian Norris0612b9d2011-08-30 18:45:40 -07001906 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001907 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001908 else
1909 len = mtd->oobsize;
1910
1911 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001912 pr_debug("%s: attempt to start read outside oob\n",
1913 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001914 return -EINVAL;
1915 }
1916
1917 /* Do not allow reads past end of device */
1918 if (unlikely(from >= mtd->size ||
1919 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1920 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001921 pr_debug("%s: attempt to read beyond end of device\n",
1922 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001923 return -EINVAL;
1924 }
Vitaly Wool70145682006-11-03 18:20:38 +03001925
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001926 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001927 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001929 /* Shift to get page */
1930 realpage = (int)(from >> chip->page_shift);
1931 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Florian Fainellif8ac0412010-09-07 13:23:43 +02001933 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001934 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001935 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001936 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001937 ret = chip->ecc.read_oob(mtd, chip, page);
1938
1939 if (ret < 0)
1940 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001941
1942 len = min(len, readlen);
1943 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001944
Brian Norris5bc7c332013-03-13 09:51:31 -07001945 if (chip->options & NAND_NEED_READRDY) {
1946 /* Apply delay or wait for ready/busy pin */
1947 if (!chip->dev_ready)
1948 udelay(chip->chip_delay);
1949 else
1950 nand_wait_ready(mtd);
1951 }
1952
Vitaly Wool70145682006-11-03 18:20:38 +03001953 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001954 if (!readlen)
1955 break;
1956
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001957 /* Increment page address */
1958 realpage++;
1959
1960 page = realpage & chip->pagemask;
1961 /* Check, if we cross a chip boundary */
1962 if (!page) {
1963 chipnr++;
1964 chip->select_chip(mtd, -1);
1965 chip->select_chip(mtd, chipnr);
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001968 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001970 ops->oobretlen = ops->ooblen - readlen;
1971
1972 if (ret < 0)
1973 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001974
1975 if (mtd->ecc_stats.failed - stats.failed)
1976 return -EBADMSG;
1977
1978 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001982 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001983 * @mtd: MTD device structure
1984 * @from: offset to read from
1985 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001987 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001989static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1990 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001992 int ret = -ENOTSUPP;
1993
1994 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001997 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001998 pr_debug("%s: attempt to read beyond end of device\n",
1999 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return -EINVAL;
2001 }
2002
Huang Shijie6a8214a2012-11-19 14:43:30 +08002003 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Florian Fainellif8ac0412010-09-07 13:23:43 +02002005 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002006 case MTD_OPS_PLACE_OOB:
2007 case MTD_OPS_AUTO_OOB:
2008 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002009 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002010
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002011 default:
2012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002015 if (!ops->datbuf)
2016 ret = nand_do_read_oob(mtd, from, ops);
2017 else
2018 ret = nand_do_read_ops(mtd, from, ops);
2019
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002020out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002022 return ret;
2023}
2024
2025
2026/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002027 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002028 * @mtd: mtd info structure
2029 * @chip: nand chip info structure
2030 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002031 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002032 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002033 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002034 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002035static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002036 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002037{
2038 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002039 if (oob_required)
2040 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002041
2042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002045/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002046 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002047 * @mtd: mtd info structure
2048 * @chip: nand chip info structure
2049 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002050 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002051 *
2052 * We need a special oob layout and handling even when ECC isn't checked.
2053 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002054static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002055 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002056 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08002057{
2058 int eccsize = chip->ecc.size;
2059 int eccbytes = chip->ecc.bytes;
2060 uint8_t *oob = chip->oob_poi;
2061 int steps, size;
2062
2063 for (steps = chip->ecc.steps; steps > 0; steps--) {
2064 chip->write_buf(mtd, buf, eccsize);
2065 buf += eccsize;
2066
2067 if (chip->ecc.prepad) {
2068 chip->write_buf(mtd, oob, chip->ecc.prepad);
2069 oob += chip->ecc.prepad;
2070 }
2071
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002072 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002073 oob += eccbytes;
2074
2075 if (chip->ecc.postpad) {
2076 chip->write_buf(mtd, oob, chip->ecc.postpad);
2077 oob += chip->ecc.postpad;
2078 }
2079 }
2080
2081 size = mtd->oobsize - (oob - chip->oob_poi);
2082 if (size)
2083 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002084
2085 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002086}
2087/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002088 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002089 * @mtd: mtd info structure
2090 * @chip: nand chip info structure
2091 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002092 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002093 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002094static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002095 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002096{
2097 int i, eccsize = chip->ecc.size;
2098 int eccbytes = chip->ecc.bytes;
2099 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002100 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002101 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002102 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002103
Brian Norris7854d3f2011-06-23 14:12:08 -07002104 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002105 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2106 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002107
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002108 for (i = 0; i < chip->ecc.total; i++)
2109 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002110
Josh Wufdbad98d2012-06-25 18:07:45 +08002111 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002112}
2113
2114/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002115 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002116 * @mtd: mtd info structure
2117 * @chip: nand chip info structure
2118 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002119 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002120 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002121static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002122 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002123{
2124 int i, eccsize = chip->ecc.size;
2125 int eccbytes = chip->ecc.bytes;
2126 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002127 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002128 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002129 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002130
2131 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2132 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002133 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002134 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2135 }
2136
2137 for (i = 0; i < chip->ecc.total; i++)
2138 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2139
2140 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002141
2142 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002143}
2144
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302145
2146/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002147 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302148 * @mtd: mtd info structure
2149 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002150 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302151 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002152 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302153 * @oob_required: must write chip->oob_poi to OOB
2154 */
2155static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2156 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002157 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302158 int oob_required)
2159{
2160 uint8_t *oob_buf = chip->oob_poi;
2161 uint8_t *ecc_calc = chip->buffers->ecccalc;
2162 int ecc_size = chip->ecc.size;
2163 int ecc_bytes = chip->ecc.bytes;
2164 int ecc_steps = chip->ecc.steps;
2165 uint32_t *eccpos = chip->ecc.layout->eccpos;
2166 uint32_t start_step = offset / ecc_size;
2167 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2168 int oob_bytes = mtd->oobsize / ecc_steps;
2169 int step, i;
2170
2171 for (step = 0; step < ecc_steps; step++) {
2172 /* configure controller for WRITE access */
2173 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2174
2175 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002176 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302177
2178 /* mask ECC of un-touched subpages by padding 0xFF */
2179 if ((step < start_step) || (step > end_step))
2180 memset(ecc_calc, 0xff, ecc_bytes);
2181 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002182 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302183
2184 /* mask OOB of un-touched subpages by padding 0xFF */
2185 /* if oob_required, preserve OOB metadata of written subpage */
2186 if (!oob_required || (step < start_step) || (step > end_step))
2187 memset(oob_buf, 0xff, oob_bytes);
2188
Brian Norrisd6a950802013-08-08 17:16:36 -07002189 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302190 ecc_calc += ecc_bytes;
2191 oob_buf += oob_bytes;
2192 }
2193
2194 /* copy calculated ECC for whole page to chip->buffer->oob */
2195 /* this include masked-value(0xFF) for unwritten subpages */
2196 ecc_calc = chip->buffers->ecccalc;
2197 for (i = 0; i < chip->ecc.total; i++)
2198 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2199
2200 /* write OOB buffer to NAND device */
2201 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2202
2203 return 0;
2204}
2205
2206
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002207/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002208 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002209 * @mtd: mtd info structure
2210 * @chip: nand chip info structure
2211 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002212 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002213 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002214 * The hw generator calculates the error syndrome automatically. Therefore we
2215 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002216 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002217static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002218 struct nand_chip *chip,
2219 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002220{
2221 int i, eccsize = chip->ecc.size;
2222 int eccbytes = chip->ecc.bytes;
2223 int eccsteps = chip->ecc.steps;
2224 const uint8_t *p = buf;
2225 uint8_t *oob = chip->oob_poi;
2226
2227 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2228
2229 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2230 chip->write_buf(mtd, p, eccsize);
2231
2232 if (chip->ecc.prepad) {
2233 chip->write_buf(mtd, oob, chip->ecc.prepad);
2234 oob += chip->ecc.prepad;
2235 }
2236
2237 chip->ecc.calculate(mtd, p, oob);
2238 chip->write_buf(mtd, oob, eccbytes);
2239 oob += eccbytes;
2240
2241 if (chip->ecc.postpad) {
2242 chip->write_buf(mtd, oob, chip->ecc.postpad);
2243 oob += chip->ecc.postpad;
2244 }
2245 }
2246
2247 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002248 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002249 if (i)
2250 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002251
2252 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002253}
2254
2255/**
David Woodhouse956e9442006-09-25 17:12:39 +01002256 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002257 * @mtd: MTD device structure
2258 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302259 * @offset: address offset within the page
2260 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002261 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002262 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002263 * @page: page number to write
2264 * @cached: cached programming
2265 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002266 */
2267static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302268 uint32_t offset, int data_len, const uint8_t *buf,
2269 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002270{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302271 int status, subpage;
2272
2273 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2274 chip->ecc.write_subpage)
2275 subpage = offset || (data_len < mtd->writesize);
2276 else
2277 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002278
2279 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2280
David Woodhouse956e9442006-09-25 17:12:39 +01002281 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302282 status = chip->ecc.write_page_raw(mtd, chip, buf,
2283 oob_required);
2284 else if (subpage)
2285 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2286 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002287 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002288 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2289
2290 if (status < 0)
2291 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002292
2293 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002294 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002295 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002296 */
2297 cached = 0;
2298
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002299 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002300
2301 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002302 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002303 /*
2304 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002305 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002306 */
2307 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2308 status = chip->errstat(mtd, chip, FL_WRITING, status,
2309 page);
2310
2311 if (status & NAND_STATUS_FAIL)
2312 return -EIO;
2313 } else {
2314 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002315 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002316 }
2317
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002318 return 0;
2319}
2320
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002322 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002323 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002324 * @oob: oob data buffer
2325 * @len: oob data write length
2326 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002327 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002328static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2329 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002330{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002331 struct nand_chip *chip = mtd->priv;
2332
2333 /*
2334 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2335 * data from a previous OOB read.
2336 */
2337 memset(chip->oob_poi, 0xff, mtd->oobsize);
2338
Florian Fainellif8ac0412010-09-07 13:23:43 +02002339 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002340
Brian Norris0612b9d2011-08-30 18:45:40 -07002341 case MTD_OPS_PLACE_OOB:
2342 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002343 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2344 return oob + len;
2345
Brian Norris0612b9d2011-08-30 18:45:40 -07002346 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002347 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002348 uint32_t boffs = 0, woffs = ops->ooboffs;
2349 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002350
Florian Fainellif8ac0412010-09-07 13:23:43 +02002351 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002352 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002353 if (unlikely(woffs)) {
2354 if (woffs >= free->length) {
2355 woffs -= free->length;
2356 continue;
2357 }
2358 boffs = free->offset + woffs;
2359 bytes = min_t(size_t, len,
2360 (free->length - woffs));
2361 woffs = 0;
2362 } else {
2363 bytes = min_t(size_t, len, free->length);
2364 boffs = free->offset;
2365 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002366 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002367 oob += bytes;
2368 }
2369 return oob;
2370 }
2371 default:
2372 BUG();
2373 }
2374 return NULL;
2375}
2376
Florian Fainellif8ac0412010-09-07 13:23:43 +02002377#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002378
2379/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002380 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002381 * @mtd: MTD device structure
2382 * @to: offset to write to
2383 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002384 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002385 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002386 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002387static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2388 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002389{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002390 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002391 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002392 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002393
2394 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002395 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002396 mtd->oobavail : mtd->oobsize;
2397
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002398 uint8_t *oob = ops->oobbuf;
2399 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302400 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002401 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002402
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002403 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002404 if (!writelen)
2405 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002406
Brian Norris8b6e50c2011-05-25 14:59:01 -07002407 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002408 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002409 pr_notice("%s: attempt to write non page aligned data\n",
2410 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002411 return -EINVAL;
2412 }
2413
Thomas Gleixner29072b92006-09-28 15:38:36 +02002414 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002415
Thomas Gleixner6a930962006-06-28 00:11:45 +02002416 chipnr = (int)(to >> chip->chip_shift);
2417 chip->select_chip(mtd, chipnr);
2418
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002419 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002420 if (nand_check_wp(mtd)) {
2421 ret = -EIO;
2422 goto err_out;
2423 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002424
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002425 realpage = (int)(to >> chip->page_shift);
2426 page = realpage & chip->pagemask;
2427 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2428
2429 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002430 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2431 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002432 chip->pagebuf = -1;
2433
Maxim Levitsky782ce792010-02-22 20:39:36 +02002434 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002435 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2436 ret = -EINVAL;
2437 goto err_out;
2438 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002439
Florian Fainellif8ac0412010-09-07 13:23:43 +02002440 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002441 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002443 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002444 int use_bufpoi;
2445 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002446
Kamal Dasu66507c72014-05-01 20:51:19 -04002447 if (part_pagewr)
2448 use_bufpoi = 1;
2449 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2450 use_bufpoi = !virt_addr_valid(buf);
2451 else
2452 use_bufpoi = 0;
2453
2454 /* Partial page write?, or need to use bounce buffer */
2455 if (use_bufpoi) {
2456 pr_debug("%s: using write bounce buffer for buf@%p\n",
2457 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002458 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002459 if (part_pagewr)
2460 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002461 chip->pagebuf = -1;
2462 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2463 memcpy(&chip->buffers->databuf[column], buf, bytes);
2464 wbuf = chip->buffers->databuf;
2465 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002466
Maxim Levitsky782ce792010-02-22 20:39:36 +02002467 if (unlikely(oob)) {
2468 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002469 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002470 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002471 } else {
2472 /* We still need to erase leftover OOB data */
2473 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002474 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302475 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2476 oob_required, page, cached,
2477 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002478 if (ret)
2479 break;
2480
2481 writelen -= bytes;
2482 if (!writelen)
2483 break;
2484
Thomas Gleixner29072b92006-09-28 15:38:36 +02002485 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002486 buf += bytes;
2487 realpage++;
2488
2489 page = realpage & chip->pagemask;
2490 /* Check, if we cross a chip boundary */
2491 if (!page) {
2492 chipnr++;
2493 chip->select_chip(mtd, -1);
2494 chip->select_chip(mtd, chipnr);
2495 }
2496 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002497
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002498 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002499 if (unlikely(oob))
2500 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002501
2502err_out:
2503 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002504 return ret;
2505}
2506
2507/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002508 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002509 * @mtd: MTD device structure
2510 * @to: offset to write to
2511 * @len: number of bytes to write
2512 * @retlen: pointer to variable to store the number of written bytes
2513 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002514 *
2515 * NAND write with ECC. Used when performing writes in interrupt context, this
2516 * may for example be called by mtdoops when writing an oops while in panic.
2517 */
2518static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2519 size_t *retlen, const uint8_t *buf)
2520{
2521 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002522 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002523 int ret;
2524
Brian Norris8b6e50c2011-05-25 14:59:01 -07002525 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002526 panic_nand_wait(mtd, chip, 400);
2527
Brian Norris8b6e50c2011-05-25 14:59:01 -07002528 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002529 panic_nand_get_device(chip, mtd, FL_WRITING);
2530
Brian Norris0ec56dc2015-02-28 02:02:30 -08002531 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002532 ops.len = len;
2533 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002534 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002535
Brian Norris4a89ff82011-08-30 18:45:45 -07002536 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002537
Brian Norris4a89ff82011-08-30 18:45:45 -07002538 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002539 return ret;
2540}
2541
2542/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002543 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002544 * @mtd: MTD device structure
2545 * @to: offset to write to
2546 * @len: number of bytes to write
2547 * @retlen: pointer to variable to store the number of written bytes
2548 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002550 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002552static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002553 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Brian Norris4a89ff82011-08-30 18:45:45 -07002555 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002556 int ret;
2557
Huang Shijie6a8214a2012-11-19 14:43:30 +08002558 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002559 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002560 ops.len = len;
2561 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002562 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002563 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002564 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002565 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002566 return ret;
2567}
2568
2569/**
2570 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002571 * @mtd: MTD device structure
2572 * @to: offset to write to
2573 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002574 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002575 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002576 */
2577static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2578 struct mtd_oob_ops *ops)
2579{
Adrian Hunter03736152007-01-31 17:58:29 +02002580 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002581 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Brian Norris289c0522011-07-19 10:06:09 -07002583 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302584 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Brian Norris0612b9d2011-08-30 18:45:40 -07002586 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002587 len = chip->ecc.layout->oobavail;
2588 else
2589 len = mtd->oobsize;
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002592 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002593 pr_debug("%s: attempt to write past end of page\n",
2594 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return -EINVAL;
2596 }
2597
Adrian Hunter03736152007-01-31 17:58:29 +02002598 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002599 pr_debug("%s: attempt to start write outside oob\n",
2600 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002601 return -EINVAL;
2602 }
2603
Jason Liu775adc32011-02-25 13:06:18 +08002604 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002605 if (unlikely(to >= mtd->size ||
2606 ops->ooboffs + ops->ooblen >
2607 ((mtd->size >> chip->page_shift) -
2608 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002609 pr_debug("%s: attempt to write beyond end of device\n",
2610 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002611 return -EINVAL;
2612 }
2613
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002614 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002615 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002617 /* Shift to get page */
2618 page = (int)(to >> chip->page_shift);
2619
2620 /*
2621 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2622 * of my DiskOnChip 2000 test units) will clear the whole data page too
2623 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2624 * it in the doc2000 driver in August 1999. dwmw2.
2625 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002626 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002629 if (nand_check_wp(mtd)) {
2630 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002631 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002632 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002635 if (page == chip->pagebuf)
2636 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002638 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002639
Brian Norris0612b9d2011-08-30 18:45:40 -07002640 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002641 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2642 else
2643 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002644
Huang Shijieb0bb6902012-11-19 14:43:29 +08002645 chip->select_chip(mtd, -1);
2646
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002647 if (status)
2648 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Vitaly Wool70145682006-11-03 18:20:38 +03002650 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002652 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002655/**
2656 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002657 * @mtd: MTD device structure
2658 * @to: offset to write to
2659 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002660 */
2661static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2662 struct mtd_oob_ops *ops)
2663{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002664 int ret = -ENOTSUPP;
2665
2666 ops->retlen = 0;
2667
2668 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002669 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002670 pr_debug("%s: attempt to write beyond end of device\n",
2671 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002672 return -EINVAL;
2673 }
2674
Huang Shijie6a8214a2012-11-19 14:43:30 +08002675 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002676
Florian Fainellif8ac0412010-09-07 13:23:43 +02002677 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002678 case MTD_OPS_PLACE_OOB:
2679 case MTD_OPS_AUTO_OOB:
2680 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002681 break;
2682
2683 default:
2684 goto out;
2685 }
2686
2687 if (!ops->datbuf)
2688 ret = nand_do_write_oob(mtd, to, ops);
2689 else
2690 ret = nand_do_write_ops(mtd, to, ops);
2691
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002692out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002693 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return ret;
2695}
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697/**
Brian Norris49c50b92014-05-06 16:02:19 -07002698 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002699 * @mtd: MTD device structure
2700 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 *
Brian Norris49c50b92014-05-06 16:02:19 -07002702 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 */
Brian Norris49c50b92014-05-06 16:02:19 -07002704static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002706 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002708 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2709 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002710
2711 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
2714/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002716 * @mtd: MTD device structure
2717 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002719 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002721static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
David Woodhousee0c7d762006-05-13 18:07:53 +01002723 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002727 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002728 * @mtd: MTD device structure
2729 * @instr: erase instruction
2730 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002732 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002734int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2735 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Adrian Hunter69423d92008-12-10 13:37:21 +00002737 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002738 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002739 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Brian Norris289c0522011-07-19 10:06:09 -07002741 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2742 __func__, (unsigned long long)instr->addr,
2743 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302745 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002749 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002752 page = (int)(instr->addr >> chip->page_shift);
2753 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002756 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002759 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 /* Check, if it is write protected */
2762 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002763 pr_debug("%s: device is write protected!\n",
2764 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 instr->state = MTD_ERASE_FAILED;
2766 goto erase_exit;
2767 }
2768
2769 /* Loop through the pages */
2770 len = instr->len;
2771
2772 instr->state = MTD_ERASING;
2773
2774 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002775 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002776 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2777 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002778 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2779 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 instr->state = MTD_ERASE_FAILED;
2781 goto erase_exit;
2782 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002783
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002784 /*
2785 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002786 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002787 */
2788 if (page <= chip->pagebuf && chip->pagebuf <
2789 (page + pages_per_block))
2790 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Brian Norris49c50b92014-05-06 16:02:19 -07002792 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002794 /*
2795 * See if operation failed and additional status checks are
2796 * available
2797 */
2798 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2799 status = chip->errstat(mtd, chip, FL_ERASING,
2800 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002803 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002804 pr_debug("%s: failed erase, page 0x%08x\n",
2805 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002807 instr->fail_addr =
2808 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 goto erase_exit;
2810 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002813 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 page += pages_per_block;
2815
2816 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002817 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002819 chip->select_chip(mtd, -1);
2820 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822 }
2823 instr->state = MTD_ERASE_DONE;
2824
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002825erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002830 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 nand_release_device(mtd);
2832
David Woodhouse49defc02007-10-06 15:01:59 -04002833 /* Do call back function */
2834 if (!ret)
2835 mtd_erase_callback(instr);
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 /* Return more or less happy */
2838 return ret;
2839}
2840
2841/**
2842 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002843 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002845 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002847static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Brian Norris289c0522011-07-19 10:06:09 -07002849 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002852 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002854 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002858 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002859 * @mtd: MTD device structure
2860 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002862static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002864 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
2867/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002868 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002869 * @mtd: MTD device structure
2870 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002872static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 int ret;
2875
Florian Fainellif8ac0412010-09-07 13:23:43 +02002876 ret = nand_block_isbad(mtd, ofs);
2877 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002878 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (ret > 0)
2880 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002881 return ret;
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Brian Norris5a0edb22013-07-30 17:52:58 -07002884 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
2887/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002888 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2889 * @mtd: MTD device structure
2890 * @chip: nand chip info structure
2891 * @addr: feature address.
2892 * @subfeature_param: the subfeature parameters, a four bytes array.
2893 */
2894static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2895 int addr, uint8_t *subfeature_param)
2896{
2897 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002898 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002899
David Mosbergerd914c932013-05-29 15:30:13 +03002900 if (!chip->onfi_version ||
2901 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2902 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002903 return -EINVAL;
2904
2905 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002906 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2907 chip->write_byte(mtd, subfeature_param[i]);
2908
Huang Shijie7db03ec2012-09-13 14:57:52 +08002909 status = chip->waitfunc(mtd, chip);
2910 if (status & NAND_STATUS_FAIL)
2911 return -EIO;
2912 return 0;
2913}
2914
2915/**
2916 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2917 * @mtd: MTD device structure
2918 * @chip: nand chip info structure
2919 * @addr: feature address.
2920 * @subfeature_param: the subfeature parameters, a four bytes array.
2921 */
2922static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2923 int addr, uint8_t *subfeature_param)
2924{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002925 int i;
2926
David Mosbergerd914c932013-05-29 15:30:13 +03002927 if (!chip->onfi_version ||
2928 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2929 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002930 return -EINVAL;
2931
Huang Shijie7db03ec2012-09-13 14:57:52 +08002932 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002933 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2934 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002935 return 0;
2936}
2937
2938/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002939 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002940 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002941 */
2942static int nand_suspend(struct mtd_info *mtd)
2943{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002944 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002945}
2946
2947/**
2948 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002949 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002950 */
2951static void nand_resume(struct mtd_info *mtd)
2952{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002953 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002954
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002955 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002956 nand_release_device(mtd);
2957 else
Brian Norrisd0370212011-07-19 10:06:08 -07002958 pr_err("%s called for a chip which is not in suspended state\n",
2959 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002960}
2961
Scott Branden72ea4032014-11-20 11:18:05 -08002962/**
2963 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
2964 * prevent further operations
2965 * @mtd: MTD device structure
2966 */
2967static void nand_shutdown(struct mtd_info *mtd)
2968{
2969 nand_get_device(mtd, FL_SHUTDOWN);
2970}
2971
Brian Norris8b6e50c2011-05-25 14:59:01 -07002972/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002973static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002976 if (!chip->chip_delay)
2977 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002980 if (chip->cmdfunc == NULL)
2981 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002984 if (chip->waitfunc == NULL)
2985 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002987 if (!chip->select_chip)
2988 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002989
Huang Shijie4204ccc2013-08-16 10:10:07 +08002990 /* set for ONFI nand */
2991 if (!chip->onfi_set_features)
2992 chip->onfi_set_features = nand_onfi_set_features;
2993 if (!chip->onfi_get_features)
2994 chip->onfi_get_features = nand_onfi_get_features;
2995
Brian Norris68e80782013-07-18 01:17:02 -07002996 /* If called twice, pointers that depend on busw may need to be reset */
2997 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002998 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2999 if (!chip->read_word)
3000 chip->read_word = nand_read_word;
3001 if (!chip->block_bad)
3002 chip->block_bad = nand_block_bad;
3003 if (!chip->block_markbad)
3004 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003005 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003006 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003007 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3008 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003009 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003010 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003011 if (!chip->scan_bbt)
3012 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003013
3014 if (!chip->controller) {
3015 chip->controller = &chip->hwcontrol;
3016 spin_lock_init(&chip->controller->lock);
3017 init_waitqueue_head(&chip->controller->wq);
3018 }
3019
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003020}
3021
Brian Norris8b6e50c2011-05-25 14:59:01 -07003022/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003023static void sanitize_string(uint8_t *s, size_t len)
3024{
3025 ssize_t i;
3026
Brian Norris8b6e50c2011-05-25 14:59:01 -07003027 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003028 s[len - 1] = 0;
3029
Brian Norris8b6e50c2011-05-25 14:59:01 -07003030 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003031 for (i = 0; i < len - 1; i++) {
3032 if (s[i] < ' ' || s[i] > 127)
3033 s[i] = '?';
3034 }
3035
Brian Norris8b6e50c2011-05-25 14:59:01 -07003036 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003037 strim(s);
3038}
3039
3040static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3041{
3042 int i;
3043 while (len--) {
3044 crc ^= *p++ << 8;
3045 for (i = 0; i < 8; i++)
3046 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3047 }
3048
3049 return crc;
3050}
3051
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003052/* Parse the Extended Parameter Page. */
3053static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3054 struct nand_chip *chip, struct nand_onfi_params *p)
3055{
3056 struct onfi_ext_param_page *ep;
3057 struct onfi_ext_section *s;
3058 struct onfi_ext_ecc_info *ecc;
3059 uint8_t *cursor;
3060 int ret = -EINVAL;
3061 int len;
3062 int i;
3063
3064 len = le16_to_cpu(p->ext_param_page_length) * 16;
3065 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003066 if (!ep)
3067 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003068
3069 /* Send our own NAND_CMD_PARAM. */
3070 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3071
3072 /* Use the Change Read Column command to skip the ONFI param pages. */
3073 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3074 sizeof(*p) * p->num_of_param_pages , -1);
3075
3076 /* Read out the Extended Parameter Page. */
3077 chip->read_buf(mtd, (uint8_t *)ep, len);
3078 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3079 != le16_to_cpu(ep->crc))) {
3080 pr_debug("fail in the CRC.\n");
3081 goto ext_out;
3082 }
3083
3084 /*
3085 * Check the signature.
3086 * Do not strictly follow the ONFI spec, maybe changed in future.
3087 */
3088 if (strncmp(ep->sig, "EPPS", 4)) {
3089 pr_debug("The signature is invalid.\n");
3090 goto ext_out;
3091 }
3092
3093 /* find the ECC section. */
3094 cursor = (uint8_t *)(ep + 1);
3095 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3096 s = ep->sections + i;
3097 if (s->type == ONFI_SECTION_TYPE_2)
3098 break;
3099 cursor += s->length * 16;
3100 }
3101 if (i == ONFI_EXT_SECTION_MAX) {
3102 pr_debug("We can not find the ECC section.\n");
3103 goto ext_out;
3104 }
3105
3106 /* get the info we want. */
3107 ecc = (struct onfi_ext_ecc_info *)cursor;
3108
Brian Norris4ae7d222013-09-16 18:20:21 -07003109 if (!ecc->codeword_size) {
3110 pr_debug("Invalid codeword size\n");
3111 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003112 }
3113
Brian Norris4ae7d222013-09-16 18:20:21 -07003114 chip->ecc_strength_ds = ecc->ecc_bits;
3115 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003116 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003117
3118ext_out:
3119 kfree(ep);
3120 return ret;
3121}
3122
Brian Norris8429bb32013-12-03 15:51:09 -08003123static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3124{
3125 struct nand_chip *chip = mtd->priv;
3126 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3127
3128 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3129 feature);
3130}
3131
3132/*
3133 * Configure chip properties from Micron vendor-specific ONFI table
3134 */
3135static void nand_onfi_detect_micron(struct nand_chip *chip,
3136 struct nand_onfi_params *p)
3137{
3138 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3139
3140 if (le16_to_cpu(p->vendor_revision) < 1)
3141 return;
3142
3143 chip->read_retries = micron->read_retry_options;
3144 chip->setup_read_retry = nand_setup_read_retry_micron;
3145}
3146
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003147/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003148 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003149 */
3150static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003151 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003152{
3153 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003154 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003155 int val;
3156
Brian Norris7854d3f2011-06-23 14:12:08 -07003157 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003158 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3159 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3160 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3161 return 0;
3162
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003163 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3164 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003165 for (j = 0; j < sizeof(*p); j++)
3166 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003167 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3168 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003169 break;
3170 }
3171 }
3172
Brian Norrisc7f23a72013-08-13 10:51:55 -07003173 if (i == 3) {
3174 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003175 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003176 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003177
Brian Norris8b6e50c2011-05-25 14:59:01 -07003178 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003179 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003180 if (val & (1 << 5))
3181 chip->onfi_version = 23;
3182 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003183 chip->onfi_version = 22;
3184 else if (val & (1 << 3))
3185 chip->onfi_version = 21;
3186 else if (val & (1 << 2))
3187 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003188 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003189 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003190
3191 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003192 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003193 return 0;
3194 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003195
3196 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3197 sanitize_string(p->model, sizeof(p->model));
3198 if (!mtd->name)
3199 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003200
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003201 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003202
3203 /*
3204 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3205 * (don't ask me who thought of this...). MTD assumes that these
3206 * dimensions will be power-of-2, so just truncate the remaining area.
3207 */
3208 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3209 mtd->erasesize *= mtd->writesize;
3210
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003211 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003212
3213 /* See erasesize comment */
3214 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003215 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003216 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003217
3218 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003219 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003220 else
3221 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003222
Huang Shijie10c86ba2013-05-17 11:17:26 +08003223 if (p->ecc_bits != 0xff) {
3224 chip->ecc_strength_ds = p->ecc_bits;
3225 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003226 } else if (chip->onfi_version >= 21 &&
3227 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3228
3229 /*
3230 * The nand_flash_detect_ext_param_page() uses the
3231 * Change Read Column command which maybe not supported
3232 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3233 * now. We do not replace user supplied command function.
3234 */
3235 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3236 chip->cmdfunc = nand_command_lp;
3237
3238 /* The Extended Parameter Page is supported since ONFI 2.1. */
3239 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003240 pr_warn("Failed to detect ONFI extended param page\n");
3241 } else {
3242 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003243 }
3244
Brian Norris8429bb32013-12-03 15:51:09 -08003245 if (p->jedec_id == NAND_MFR_MICRON)
3246 nand_onfi_detect_micron(chip, p);
3247
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003248 return 1;
3249}
3250
3251/*
Huang Shijie91361812014-02-21 13:39:40 +08003252 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3253 */
3254static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3255 int *busw)
3256{
3257 struct nand_jedec_params *p = &chip->jedec_params;
3258 struct jedec_ecc_info *ecc;
3259 int val;
3260 int i, j;
3261
3262 /* Try JEDEC for unknown chip or LP */
3263 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3264 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3265 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3266 chip->read_byte(mtd) != 'C')
3267 return 0;
3268
3269 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3270 for (i = 0; i < 3; i++) {
3271 for (j = 0; j < sizeof(*p); j++)
3272 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3273
3274 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3275 le16_to_cpu(p->crc))
3276 break;
3277 }
3278
3279 if (i == 3) {
3280 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3281 return 0;
3282 }
3283
3284 /* Check version */
3285 val = le16_to_cpu(p->revision);
3286 if (val & (1 << 2))
3287 chip->jedec_version = 10;
3288 else if (val & (1 << 1))
3289 chip->jedec_version = 1; /* vendor specific version */
3290
3291 if (!chip->jedec_version) {
3292 pr_info("unsupported JEDEC version: %d\n", val);
3293 return 0;
3294 }
3295
3296 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3297 sanitize_string(p->model, sizeof(p->model));
3298 if (!mtd->name)
3299 mtd->name = p->model;
3300
3301 mtd->writesize = le32_to_cpu(p->byte_per_page);
3302
3303 /* Please reference to the comment for nand_flash_detect_onfi. */
3304 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3305 mtd->erasesize *= mtd->writesize;
3306
3307 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3308
3309 /* Please reference to the comment for nand_flash_detect_onfi. */
3310 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3311 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3312 chip->bits_per_cell = p->bits_per_cell;
3313
3314 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3315 *busw = NAND_BUSWIDTH_16;
3316 else
3317 *busw = 0;
3318
3319 /* ECC info */
3320 ecc = &p->ecc_info[0];
3321
3322 if (ecc->codeword_size >= 9) {
3323 chip->ecc_strength_ds = ecc->ecc_bits;
3324 chip->ecc_step_ds = 1 << ecc->codeword_size;
3325 } else {
3326 pr_warn("Invalid codeword size\n");
3327 }
3328
3329 return 1;
3330}
3331
3332/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003333 * nand_id_has_period - Check if an ID string has a given wraparound period
3334 * @id_data: the ID string
3335 * @arrlen: the length of the @id_data array
3336 * @period: the period of repitition
3337 *
3338 * Check if an ID string is repeated within a given sequence of bytes at
3339 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003340 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003341 * if the repetition has a period of @period; otherwise, returns zero.
3342 */
3343static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3344{
3345 int i, j;
3346 for (i = 0; i < period; i++)
3347 for (j = i + period; j < arrlen; j += period)
3348 if (id_data[i] != id_data[j])
3349 return 0;
3350 return 1;
3351}
3352
3353/*
3354 * nand_id_len - Get the length of an ID string returned by CMD_READID
3355 * @id_data: the ID string
3356 * @arrlen: the length of the @id_data array
3357
3358 * Returns the length of the ID string, according to known wraparound/trailing
3359 * zero patterns. If no pattern exists, returns the length of the array.
3360 */
3361static int nand_id_len(u8 *id_data, int arrlen)
3362{
3363 int last_nonzero, period;
3364
3365 /* Find last non-zero byte */
3366 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3367 if (id_data[last_nonzero])
3368 break;
3369
3370 /* All zeros */
3371 if (last_nonzero < 0)
3372 return 0;
3373
3374 /* Calculate wraparound period */
3375 for (period = 1; period < arrlen; period++)
3376 if (nand_id_has_period(id_data, arrlen, period))
3377 break;
3378
3379 /* There's a repeated pattern */
3380 if (period < arrlen)
3381 return period;
3382
3383 /* There are trailing zeros */
3384 if (last_nonzero < arrlen - 1)
3385 return last_nonzero + 1;
3386
3387 /* No pattern detected */
3388 return arrlen;
3389}
3390
Huang Shijie7db906b2013-09-25 14:58:11 +08003391/* Extract the bits of per cell from the 3rd byte of the extended ID */
3392static int nand_get_bits_per_cell(u8 cellinfo)
3393{
3394 int bits;
3395
3396 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3397 bits >>= NAND_CI_CELLTYPE_SHIFT;
3398 return bits + 1;
3399}
3400
Brian Norrise3b88bd2012-09-24 20:40:52 -07003401/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003402 * Many new NAND share similar device ID codes, which represent the size of the
3403 * chip. The rest of the parameters must be decoded according to generic or
3404 * manufacturer-specific "extended ID" decoding patterns.
3405 */
3406static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3407 u8 id_data[8], int *busw)
3408{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003409 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003410 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003411 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003412 /* The 4th id byte is the important one */
3413 extid = id_data[3];
3414
Brian Norrise3b88bd2012-09-24 20:40:52 -07003415 id_len = nand_id_len(id_data, 8);
3416
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003417 /*
3418 * Field definitions are in the following datasheets:
3419 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003420 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003421 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003422 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003423 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3424 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003425 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003426 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003427 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003428 /* Calc pagesize */
3429 mtd->writesize = 2048 << (extid & 0x03);
3430 extid >>= 2;
3431 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003432 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003433 case 1:
3434 mtd->oobsize = 128;
3435 break;
3436 case 2:
3437 mtd->oobsize = 218;
3438 break;
3439 case 3:
3440 mtd->oobsize = 400;
3441 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003442 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003443 mtd->oobsize = 436;
3444 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003445 case 5:
3446 mtd->oobsize = 512;
3447 break;
3448 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003449 mtd->oobsize = 640;
3450 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003451 case 7:
3452 default: /* Other cases are "reserved" (unknown) */
3453 mtd->oobsize = 1024;
3454 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003455 }
3456 extid >>= 2;
3457 /* Calc blocksize */
3458 mtd->erasesize = (128 * 1024) <<
3459 (((extid >> 1) & 0x04) | (extid & 0x03));
3460 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003461 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003462 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003463 unsigned int tmp;
3464
3465 /* Calc pagesize */
3466 mtd->writesize = 2048 << (extid & 0x03);
3467 extid >>= 2;
3468 /* Calc oobsize */
3469 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3470 case 0:
3471 mtd->oobsize = 128;
3472 break;
3473 case 1:
3474 mtd->oobsize = 224;
3475 break;
3476 case 2:
3477 mtd->oobsize = 448;
3478 break;
3479 case 3:
3480 mtd->oobsize = 64;
3481 break;
3482 case 4:
3483 mtd->oobsize = 32;
3484 break;
3485 case 5:
3486 mtd->oobsize = 16;
3487 break;
3488 default:
3489 mtd->oobsize = 640;
3490 break;
3491 }
3492 extid >>= 2;
3493 /* Calc blocksize */
3494 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3495 if (tmp < 0x03)
3496 mtd->erasesize = (128 * 1024) << tmp;
3497 else if (tmp == 0x03)
3498 mtd->erasesize = 768 * 1024;
3499 else
3500 mtd->erasesize = (64 * 1024) << tmp;
3501 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003502 } else {
3503 /* Calc pagesize */
3504 mtd->writesize = 1024 << (extid & 0x03);
3505 extid >>= 2;
3506 /* Calc oobsize */
3507 mtd->oobsize = (8 << (extid & 0x01)) *
3508 (mtd->writesize >> 9);
3509 extid >>= 2;
3510 /* Calc blocksize. Blocksize is multiples of 64KiB */
3511 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3512 extid >>= 2;
3513 /* Get buswidth information */
3514 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003515
3516 /*
3517 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3518 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3519 * follows:
3520 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3521 * 110b -> 24nm
3522 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3523 */
3524 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003525 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003526 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3527 !(id_data[4] & 0x80) /* !BENAND */) {
3528 mtd->oobsize = 32 * mtd->writesize >> 9;
3529 }
3530
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003531 }
3532}
3533
3534/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003535 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3536 * decodes a matching ID table entry and assigns the MTD size parameters for
3537 * the chip.
3538 */
3539static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3540 struct nand_flash_dev *type, u8 id_data[8],
3541 int *busw)
3542{
3543 int maf_id = id_data[0];
3544
3545 mtd->erasesize = type->erasesize;
3546 mtd->writesize = type->pagesize;
3547 mtd->oobsize = mtd->writesize / 32;
3548 *busw = type->options & NAND_BUSWIDTH_16;
3549
Huang Shijie1c195e92013-09-25 14:58:12 +08003550 /* All legacy ID NAND are small-page, SLC */
3551 chip->bits_per_cell = 1;
3552
Brian Norrisf23a4812012-09-24 20:40:51 -07003553 /*
3554 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3555 * some Spansion chips have erasesize that conflicts with size
3556 * listed in nand_ids table.
3557 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3558 */
3559 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3560 && id_data[6] == 0x00 && id_data[7] == 0x00
3561 && mtd->writesize == 512) {
3562 mtd->erasesize = 128 * 1024;
3563 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3564 }
3565}
3566
3567/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003568 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3569 * heuristic patterns using various detected parameters (e.g., manufacturer,
3570 * page size, cell-type information).
3571 */
3572static void nand_decode_bbm_options(struct mtd_info *mtd,
3573 struct nand_chip *chip, u8 id_data[8])
3574{
3575 int maf_id = id_data[0];
3576
3577 /* Set the bad block position */
3578 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3579 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3580 else
3581 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3582
3583 /*
3584 * Bad block marker is stored in the last page of each block on Samsung
3585 * and Hynix MLC devices; stored in first two pages of each block on
3586 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3587 * AMD/Spansion, and Macronix. All others scan only the first page.
3588 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003589 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003590 (maf_id == NAND_MFR_SAMSUNG ||
3591 maf_id == NAND_MFR_HYNIX))
3592 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003593 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003594 (maf_id == NAND_MFR_SAMSUNG ||
3595 maf_id == NAND_MFR_HYNIX ||
3596 maf_id == NAND_MFR_TOSHIBA ||
3597 maf_id == NAND_MFR_AMD ||
3598 maf_id == NAND_MFR_MACRONIX)) ||
3599 (mtd->writesize == 2048 &&
3600 maf_id == NAND_MFR_MICRON))
3601 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3602}
3603
Huang Shijieec6e87e2013-03-15 11:01:00 +08003604static inline bool is_full_id_nand(struct nand_flash_dev *type)
3605{
3606 return type->id_len;
3607}
3608
3609static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3610 struct nand_flash_dev *type, u8 *id_data, int *busw)
3611{
3612 if (!strncmp(type->id, id_data, type->id_len)) {
3613 mtd->writesize = type->pagesize;
3614 mtd->erasesize = type->erasesize;
3615 mtd->oobsize = type->oobsize;
3616
Huang Shijie7db906b2013-09-25 14:58:11 +08003617 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003618 chip->chipsize = (uint64_t)type->chipsize << 20;
3619 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003620 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3621 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003622 chip->onfi_timing_mode_default =
3623 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003624
3625 *busw = type->options & NAND_BUSWIDTH_16;
3626
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003627 if (!mtd->name)
3628 mtd->name = type->name;
3629
Huang Shijieec6e87e2013-03-15 11:01:00 +08003630 return true;
3631 }
3632 return false;
3633}
3634
Brian Norris7e74c2d2012-09-24 20:40:49 -07003635/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003636 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003637 */
3638static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003639 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003640 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003641 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003642{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003643 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003644 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003645 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003648 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Karl Beldanef89a882008-09-15 14:37:29 +02003650 /*
3651 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003652 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003653 */
3654 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003657 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003660 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003661 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Brian Norris8b6e50c2011-05-25 14:59:01 -07003663 /*
3664 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003665 * interface concerns can cause random data which looks like a
3666 * possibly credible NAND flash to appear. If the two results do
3667 * not match, ignore the device completely.
3668 */
3669
3670 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3671
Brian Norris4aef9b72012-09-24 20:40:48 -07003672 /* Read entire ID string */
3673 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003674 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003675
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003676 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003677 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003678 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003679 return ERR_PTR(-ENODEV);
3680 }
3681
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003682 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003683 type = nand_flash_ids;
3684
Huang Shijieec6e87e2013-03-15 11:01:00 +08003685 for (; type->name != NULL; type++) {
3686 if (is_full_id_nand(type)) {
3687 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3688 goto ident_done;
3689 } else if (*dev_id == type->dev_id) {
3690 break;
3691 }
3692 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003693
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003694 chip->onfi_version = 0;
3695 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003696 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003697 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003698 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003699
3700 /* Check if the chip is JEDEC compliant */
3701 if (nand_flash_detect_jedec(mtd, chip, &busw))
3702 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003703 }
3704
David Woodhouse5e81e882010-02-26 18:32:56 +00003705 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003706 return ERR_PTR(-ENODEV);
3707
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003708 if (!mtd->name)
3709 mtd->name = type->name;
3710
Adrian Hunter69423d92008-12-10 13:37:21 +00003711 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003712
Huang Shijie12a40a52010-09-27 10:43:53 +08003713 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003714 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003715 busw = chip->init_size(mtd, chip, id_data);
3716 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003717 /* Decode parameters from extended ID */
3718 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003719 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003720 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003721 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003722 /* Get chip options */
3723 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003724
Brian Norris8b6e50c2011-05-25 14:59:01 -07003725 /*
3726 * Check if chip is not a Samsung device. Do not clear the
3727 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003728 */
3729 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3730 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3731ident_done:
3732
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003733 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003734 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003735 if (nand_manuf_ids[maf_idx].id == *maf_id)
3736 break;
3737 }
3738
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003739 if (chip->options & NAND_BUSWIDTH_AUTO) {
3740 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3741 chip->options |= busw;
3742 nand_set_defaults(chip, busw);
3743 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3744 /*
3745 * Check, if buswidth is correct. Hardware drivers should set
3746 * chip correct!
3747 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003748 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3749 *maf_id, *dev_id);
3750 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3751 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003752 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3753 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003754 return ERR_PTR(-EINVAL);
3755 }
3756
Brian Norris7e74c2d2012-09-24 20:40:49 -07003757 nand_decode_bbm_options(mtd, chip, id_data);
3758
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003759 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003760 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003761 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003762 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003763
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003764 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003765 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003766 if (chip->chipsize & 0xffffffff)
3767 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003768 else {
3769 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3770 chip->chip_shift += 32 - 1;
3771 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003772
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003773 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003774 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003775
Brian Norris8b6e50c2011-05-25 14:59:01 -07003776 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003777 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3778 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003779
Ezequiel Garcia20171642013-11-25 08:30:31 -03003780 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3781 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003782
3783 if (chip->onfi_version)
3784 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3785 chip->onfi_params.model);
3786 else if (chip->jedec_version)
3787 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3788 chip->jedec_params.model);
3789 else
3790 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3791 type->name);
3792
Rafał Miłecki3755a992014-10-21 00:01:04 +02003793 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003794 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003795 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003796 return type;
3797}
3798
Brian Norris5844fee2015-01-23 00:22:27 -08003799static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip,
3800 struct device_node *dn)
3801{
3802 int ecc_mode, ecc_strength, ecc_step;
3803
3804 if (of_get_nand_bus_width(dn) == 16)
3805 chip->options |= NAND_BUSWIDTH_16;
3806
3807 if (of_get_nand_on_flash_bbt(dn))
3808 chip->bbt_options |= NAND_BBT_USE_FLASH;
3809
3810 ecc_mode = of_get_nand_ecc_mode(dn);
3811 ecc_strength = of_get_nand_ecc_strength(dn);
3812 ecc_step = of_get_nand_ecc_step_size(dn);
3813
3814 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3815 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3816 pr_err("must set both strength and step size in DT\n");
3817 return -EINVAL;
3818 }
3819
3820 if (ecc_mode >= 0)
3821 chip->ecc.mode = ecc_mode;
3822
3823 if (ecc_strength >= 0)
3824 chip->ecc.strength = ecc_strength;
3825
3826 if (ecc_step > 0)
3827 chip->ecc.size = ecc_step;
3828
3829 return 0;
3830}
3831
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003832/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003833 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003834 * @mtd: MTD device structure
3835 * @maxchips: number of chips to scan for
3836 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003837 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003838 * This is the first phase of the normal nand_scan() function. It reads the
3839 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003840 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003841 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003842 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003843int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3844 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003845{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003846 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003847 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003848 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08003849 int ret;
3850
3851 if (chip->dn) {
3852 ret = nand_dt_init(mtd, chip, chip->dn);
3853 if (ret)
3854 return ret;
3855 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003856
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003857 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003858 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003859
3860 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003861 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3862 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003863
3864 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003865 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003866 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003867 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003868 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 }
3870
Huang Shijie07300162012-11-09 16:23:45 +08003871 chip->select_chip(mtd, -1);
3872
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003873 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003874 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003875 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003876 /* See comment in nand_get_flash_type for reset */
3877 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003879 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003881 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003882 nand_dev_id != chip->read_byte(mtd)) {
3883 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 break;
Huang Shijie07300162012-11-09 16:23:45 +08003885 }
3886 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
3888 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003889 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003892 chip->numchips = i;
3893 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
David Woodhouse3b85c322006-09-25 17:06:53 +01003895 return 0;
3896}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003897EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003898
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03003899/*
3900 * Check if the chip configuration meet the datasheet requirements.
3901
3902 * If our configuration corrects A bits per B bytes and the minimum
3903 * required correction level is X bits per Y bytes, then we must ensure
3904 * both of the following are true:
3905 *
3906 * (1) A / B >= X / Y
3907 * (2) A >= X
3908 *
3909 * Requirement (1) ensures we can correct for the required bitflip density.
3910 * Requirement (2) ensures we can correct even when all bitflips are clumped
3911 * in the same sector.
3912 */
3913static bool nand_ecc_strength_good(struct mtd_info *mtd)
3914{
3915 struct nand_chip *chip = mtd->priv;
3916 struct nand_ecc_ctrl *ecc = &chip->ecc;
3917 int corr, ds_corr;
3918
3919 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3920 /* Not enough information */
3921 return true;
3922
3923 /*
3924 * We get the number of corrected bits per page to compare
3925 * the correction density.
3926 */
3927 corr = (mtd->writesize * ecc->strength) / ecc->size;
3928 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3929
3930 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3931}
David Woodhouse3b85c322006-09-25 17:06:53 +01003932
3933/**
3934 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003935 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003936 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003937 * This is the second phase of the normal nand_scan() function. It fills out
3938 * all the uninitialized function pointers with the defaults and scans for a
3939 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003940 */
3941int nand_scan_tail(struct mtd_info *mtd)
3942{
3943 int i;
3944 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003945 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08003946 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01003947
Brian Norrise2414f42012-02-06 13:44:00 -08003948 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3949 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3950 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3951
Huang Shijief02ea4e2014-01-13 14:27:12 +08003952 if (!(chip->options & NAND_OWN_BUFFERS)) {
3953 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3954 + mtd->oobsize * 3, GFP_KERNEL);
3955 if (!nbuf)
3956 return -ENOMEM;
3957 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3958 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3959 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3960
3961 chip->buffers = nbuf;
3962 } else {
3963 if (!chip->buffers)
3964 return -ENOMEM;
3965 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003966
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003967 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003968 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003969
3970 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003971 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003972 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003973 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003974 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003976 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 break;
3978 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003979 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 break;
3981 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003982 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003984 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003985 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003988 pr_warn("No oob scheme defined for oobsize %d\n",
3989 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 BUG();
3991 }
3992 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003993
David Woodhouse956e9442006-09-25 17:12:39 +01003994 if (!chip->write_page)
3995 chip->write_page = nand_write_page;
3996
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003997 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003998 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003999 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004000 */
David Woodhouse956e9442006-09-25 17:12:39 +01004001
Huang Shijie97de79e02013-10-18 14:20:53 +08004002 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004003 case NAND_ECC_HW_OOB_FIRST:
4004 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004005 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004006 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004007 BUG();
4008 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004009 if (!ecc->read_page)
4010 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004011
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004012 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004013 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004014 if (!ecc->read_page)
4015 ecc->read_page = nand_read_page_hwecc;
4016 if (!ecc->write_page)
4017 ecc->write_page = nand_write_page_hwecc;
4018 if (!ecc->read_page_raw)
4019 ecc->read_page_raw = nand_read_page_raw;
4020 if (!ecc->write_page_raw)
4021 ecc->write_page_raw = nand_write_page_raw;
4022 if (!ecc->read_oob)
4023 ecc->read_oob = nand_read_oob_std;
4024 if (!ecc->write_oob)
4025 ecc->write_oob = nand_write_oob_std;
4026 if (!ecc->read_subpage)
4027 ecc->read_subpage = nand_read_subpage;
4028 if (!ecc->write_subpage)
4029 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004030
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004031 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004032 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4033 (!ecc->read_page ||
4034 ecc->read_page == nand_read_page_hwecc ||
4035 !ecc->write_page ||
4036 ecc->write_page == nand_write_page_hwecc)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004037 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004038 BUG();
4039 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004040 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004041 if (!ecc->read_page)
4042 ecc->read_page = nand_read_page_syndrome;
4043 if (!ecc->write_page)
4044 ecc->write_page = nand_write_page_syndrome;
4045 if (!ecc->read_page_raw)
4046 ecc->read_page_raw = nand_read_page_raw_syndrome;
4047 if (!ecc->write_page_raw)
4048 ecc->write_page_raw = nand_write_page_raw_syndrome;
4049 if (!ecc->read_oob)
4050 ecc->read_oob = nand_read_oob_syndrome;
4051 if (!ecc->write_oob)
4052 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004053
Huang Shijie97de79e02013-10-18 14:20:53 +08004054 if (mtd->writesize >= ecc->size) {
4055 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07004056 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4057 BUG();
4058 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004059 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004060 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004061 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4062 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004063 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004065 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08004066 ecc->calculate = nand_calculate_ecc;
4067 ecc->correct = nand_correct_data;
4068 ecc->read_page = nand_read_page_swecc;
4069 ecc->read_subpage = nand_read_subpage;
4070 ecc->write_page = nand_write_page_swecc;
4071 ecc->read_page_raw = nand_read_page_raw;
4072 ecc->write_page_raw = nand_write_page_raw;
4073 ecc->read_oob = nand_read_oob_std;
4074 ecc->write_oob = nand_write_oob_std;
4075 if (!ecc->size)
4076 ecc->size = 256;
4077 ecc->bytes = 3;
4078 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004080
Ivan Djelic193bd402011-03-11 11:05:33 +01004081 case NAND_ECC_SOFT_BCH:
4082 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03004083 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004084 BUG();
4085 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004086 ecc->calculate = nand_bch_calculate_ecc;
4087 ecc->correct = nand_bch_correct_data;
4088 ecc->read_page = nand_read_page_swecc;
4089 ecc->read_subpage = nand_read_subpage;
4090 ecc->write_page = nand_write_page_swecc;
4091 ecc->read_page_raw = nand_read_page_raw;
4092 ecc->write_page_raw = nand_write_page_raw;
4093 ecc->read_oob = nand_read_oob_std;
4094 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004095 /*
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004096 * Board driver should supply ecc.size and ecc.strength values
4097 * to select how many bits are correctable. Otherwise, default
4098 * to 4 bits for large page devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004099 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004100 if (!ecc->size && (mtd->oobsize >= 64)) {
4101 ecc->size = 512;
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004102 ecc->strength = 4;
Ivan Djelic193bd402011-03-11 11:05:33 +01004103 }
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004104
4105 /* See nand_bch_init() for details. */
4106 ecc->bytes = DIV_ROUND_UP(
4107 ecc->strength * fls(8 * ecc->size), 8);
Huang Shijie97de79e02013-10-18 14:20:53 +08004108 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4109 &ecc->layout);
4110 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004111 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004112 BUG();
4113 }
4114 break;
4115
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004116 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004117 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004118 ecc->read_page = nand_read_page_raw;
4119 ecc->write_page = nand_write_page_raw;
4120 ecc->read_oob = nand_read_oob_std;
4121 ecc->read_page_raw = nand_read_page_raw;
4122 ecc->write_page_raw = nand_write_page_raw;
4123 ecc->write_oob = nand_write_oob_std;
4124 ecc->size = mtd->writesize;
4125 ecc->bytes = 0;
4126 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08004130 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004131 BUG();
4132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Brian Norris9ce244b2011-08-30 18:45:37 -07004134 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004135 if (!ecc->read_oob_raw)
4136 ecc->read_oob_raw = ecc->read_oob;
4137 if (!ecc->write_oob_raw)
4138 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004139
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004140 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004141 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004142 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004143 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004144 ecc->layout->oobavail = 0;
4145 for (i = 0; ecc->layout->oobfree[i].length
4146 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4147 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4148 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004149
Thomas Petazzoni54c39e92014-07-02 15:16:32 +02004150 /* ECC sanity check: warn if it's too weak */
4151 if (!nand_ecc_strength_good(mtd))
4152 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4153 mtd->name);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004154
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004155 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004156 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004157 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004158 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004159 ecc->steps = mtd->writesize / ecc->size;
4160 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004161 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004162 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004164 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004165
Brian Norris8b6e50c2011-05-25 14:59:01 -07004166 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004167 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004168 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004169 case 2:
4170 mtd->subpage_sft = 1;
4171 break;
4172 case 4:
4173 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004174 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004175 mtd->subpage_sft = 2;
4176 break;
4177 }
4178 }
4179 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4180
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004181 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004182 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004185 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004187 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304188 switch (ecc->mode) {
4189 case NAND_ECC_SOFT:
4190 case NAND_ECC_SOFT_BCH:
4191 if (chip->page_shift > 9)
4192 chip->options |= NAND_SUBPAGE_READ;
4193 break;
4194
4195 default:
4196 break;
4197 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004200 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004201 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4202 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004203 mtd->_erase = nand_erase;
4204 mtd->_point = NULL;
4205 mtd->_unpoint = NULL;
4206 mtd->_read = nand_read;
4207 mtd->_write = nand_write;
4208 mtd->_panic_write = panic_nand_write;
4209 mtd->_read_oob = nand_read_oob;
4210 mtd->_write_oob = nand_write_oob;
4211 mtd->_sync = nand_sync;
4212 mtd->_lock = NULL;
4213 mtd->_unlock = NULL;
4214 mtd->_suspend = nand_suspend;
4215 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004216 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004217 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004218 mtd->_block_isbad = nand_block_isbad;
4219 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004220 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Mike Dunn6a918ba2012-03-11 14:21:11 -07004222 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004223 mtd->ecclayout = ecc->layout;
4224 mtd->ecc_strength = ecc->strength;
4225 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004226 /*
4227 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4228 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4229 * properly set.
4230 */
4231 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004232 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004234 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004235 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
4238 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004239 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004241EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Brian Norris8b6e50c2011-05-25 14:59:01 -07004243/*
4244 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004245 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004246 * to call us from in-kernel code if the core NAND support is modular.
4247 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004248#ifdef MODULE
4249#define caller_is_module() (1)
4250#else
4251#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004252 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004253#endif
4254
4255/**
4256 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004257 * @mtd: MTD device structure
4258 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004259 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004260 * This fills out all the uninitialized function pointers with the defaults.
4261 * The flash ID is read and the mtd/chip structures are filled with the
4262 * appropriate values. The mtd->owner field must be set to the module of the
4263 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004264 */
4265int nand_scan(struct mtd_info *mtd, int maxchips)
4266{
4267 int ret;
4268
4269 /* Many callers got this wrong, so check for it for a while... */
4270 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004271 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004272 BUG();
4273 }
4274
David Woodhouse5e81e882010-02-26 18:32:56 +00004275 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004276 if (!ret)
4277 ret = nand_scan_tail(mtd);
4278 return ret;
4279}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004280EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004283 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004284 * @mtd: MTD device structure
4285 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004286void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004288 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Ivan Djelic193bd402011-03-11 11:05:33 +01004290 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4291 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4292
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004293 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Jesper Juhlfa671642005-11-07 01:01:27 -08004295 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004296 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004297 if (!(chip->options & NAND_OWN_BUFFERS))
4298 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004299
4300 /* Free bad block descriptor memory */
4301 if (chip->badblock_pattern && chip->badblock_pattern->options
4302 & NAND_BBT_DYNAMICSTRUCT)
4303 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304}
David Woodhousee0c7d762006-05-13 18:07:53 +01004305EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004306
4307static int __init nand_base_init(void)
4308{
4309 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4310 return 0;
4311}
4312
4313static void __exit nand_base_exit(void)
4314{
4315 led_trigger_unregister_simple(nand_led_trigger);
4316}
4317
4318module_init(nand_base_init);
4319module_exit(nand_base_exit);
4320
David Woodhousee0c7d762006-05-13 18:07:53 +01004321MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004322MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4323MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004324MODULE_DESCRIPTION("Generic NAND flash driver code");