blob: 37c0d9d624784e2f8b89df61201e2abb7971d7d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080046#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Brian Norris5844fee2015-01-23 00:22:27 -080049#include <linux/of_mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020052static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .eccbytes = 3,
54 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020055 .oobfree = {
56 {.offset = 3,
57 .length = 2},
58 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020059 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020062static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .eccbytes = 6,
64 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020065 .oobfree = {
66 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020067 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020070static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .eccbytes = 24,
72 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010073 40, 41, 42, 43, 44, 45, 46, 47,
74 48, 49, 50, 51, 52, 53, 54, 55,
75 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020076 .oobfree = {
77 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020078 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Thomas Gleixner81ec5362007-12-12 17:27:03 +010081static struct nand_ecclayout nand_oob_128 = {
82 .eccbytes = 48,
83 .eccpos = {
84 80, 81, 82, 83, 84, 85, 86, 87,
85 88, 89, 90, 91, 92, 93, 94, 95,
86 96, 97, 98, 99, 100, 101, 102, 103,
87 104, 105, 106, 107, 108, 109, 110, 111,
88 112, 113, 114, 115, 116, 117, 118, 119,
89 120, 121, 122, 123, 124, 125, 126, 127},
90 .oobfree = {
91 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020092 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010093};
94
Huang Shijie6a8214a2012-11-19 14:43:30 +080095static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020097static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
98 struct mtd_oob_ops *ops);
99
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200100/*
Joe Perches8e87d782008-02-03 17:22:34 +0200101 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200102 * compiled away when LED support is disabled.
103 */
104DEFINE_LED_TRIGGER(nand_led_trigger);
105
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530106static int check_offs_len(struct mtd_info *mtd,
107 loff_t ofs, uint64_t len)
108{
109 struct nand_chip *chip = mtd->priv;
110 int ret = 0;
111
112 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300113 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700114 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530115 ret = -EINVAL;
116 }
117
118 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300119 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700120 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530121 ret = -EINVAL;
122 }
123
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530124 return ret;
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/**
128 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700129 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000130 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800131 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100133static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200135 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200137 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200138 spin_lock(&chip->controller->lock);
139 chip->controller->active = NULL;
140 chip->state = FL_READY;
141 wake_up(&chip->controller->wq);
142 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/**
146 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700147 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700149 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200151static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200153 struct nand_chip *chip = mtd->priv;
154 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/**
Masanari Iida064a7692012-11-09 23:20:58 +0900158 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700159 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700161 * Default read function for 16bit buswidth with endianness conversion.
162 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200164static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200166 struct nand_chip *chip = mtd->priv;
167 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700172 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700174 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
176static u16 nand_read_word(struct mtd_info *mtd)
177{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200178 struct nand_chip *chip = mtd->priv;
179 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700184 * @mtd: MTD device structure
185 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
187 * Default select function for 1 chip devices.
188 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200191 struct nand_chip *chip = mtd->priv;
192
193 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200195 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199
200 default:
201 BUG();
202 }
203}
204
205/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100206 * nand_write_byte - [DEFAULT] write single byte to chip
207 * @mtd: MTD device structure
208 * @byte: value to write
209 *
210 * Default function to write a byte to I/O[7:0]
211 */
212static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
213{
214 struct nand_chip *chip = mtd->priv;
215
216 chip->write_buf(mtd, &byte, 1);
217}
218
219/**
220 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
221 * @mtd: MTD device structure
222 * @byte: value to write
223 *
224 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
225 */
226static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
227{
228 struct nand_chip *chip = mtd->priv;
229 uint16_t word = byte;
230
231 /*
232 * It's not entirely clear what should happen to I/O[15:8] when writing
233 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
234 *
235 * When the host supports a 16-bit bus width, only data is
236 * transferred at the 16-bit width. All address and command line
237 * transfers shall use only the lower 8-bits of the data bus. During
238 * command transfers, the host may place any value on the upper
239 * 8-bits of the data bus. During address transfers, the host shall
240 * set the upper 8-bits of the data bus to 00h.
241 *
242 * One user of the write_byte callback is nand_onfi_set_features. The
243 * four parameters are specified to be written to I/O[7:0], but this is
244 * neither an address nor a command transfer. Let's assume a 0 on the
245 * upper I/O lines is OK.
246 */
247 chip->write_buf(mtd, (uint8_t *)&word, 2);
248}
249
250/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700252 * @mtd: MTD device structure
253 * @buf: data buffer
254 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700256 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200258static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200260 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Alexander Shiyan76413832013-04-13 09:32:13 +0400262 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000266 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700267 * @mtd: MTD device structure
268 * @buf: buffer to store date
269 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200273static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200275 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Alexander Shiyan76413832013-04-13 09:32:13 +0400277 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
283 * @buf: data buffer
284 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700286 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200288static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200290 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000292
Alexander Shiyan76413832013-04-13 09:32:13 +0400293 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000297 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700298 * @mtd: MTD device structure
299 * @buf: buffer to store date
300 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700302 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200304static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200306 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alexander Shiyan76413832013-04-13 09:32:13 +0400309 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700314 * @mtd: MTD device structure
315 * @ofs: offset from device start
316 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000318 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
321{
Brian Norriscdbec052012-01-13 18:11:48 -0800322 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200323 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 u16 bad;
325
Brian Norris5fb15492011-05-31 16:31:21 -0700326 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700327 ofs += mtd->erasesize - mtd->writesize;
328
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100329 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200332 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Huang Shijie6a8214a2012-11-19 14:43:30 +0800334 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200337 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Brian Norriscdbec052012-01-13 18:11:48 -0800340 do {
341 if (chip->options & NAND_BUSWIDTH_16) {
342 chip->cmdfunc(mtd, NAND_CMD_READOOB,
343 chip->badblockpos & 0xFE, page);
344 bad = cpu_to_le16(chip->read_word(mtd));
345 if (chip->badblockpos & 0x1)
346 bad >>= 8;
347 else
348 bad &= 0xFF;
349 } else {
350 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
351 page);
352 bad = chip->read_byte(mtd);
353 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000354
Brian Norriscdbec052012-01-13 18:11:48 -0800355 if (likely(chip->badblockbits == 8))
356 res = bad != 0xFF;
357 else
358 res = hweight8(bad) < chip->badblockbits;
359 ofs += mtd->writesize;
360 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
361 i++;
362 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200363
Huang Shijieb0bb6902012-11-19 14:43:29 +0800364 if (getchip) {
365 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800367 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return res;
370}
371
372/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700373 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700374 * @mtd: MTD device structure
375 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700377 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700378 * specific driver. It provides the details for writing a bad block marker to a
379 * block.
380 */
381static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
382{
383 struct nand_chip *chip = mtd->priv;
384 struct mtd_oob_ops ops;
385 uint8_t buf[2] = { 0, 0 };
386 int ret = 0, res, i = 0;
387
Brian Norris0ec56dc2015-02-28 02:02:30 -0800388 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700389 ops.oobbuf = buf;
390 ops.ooboffs = chip->badblockpos;
391 if (chip->options & NAND_BUSWIDTH_16) {
392 ops.ooboffs &= ~0x01;
393 ops.len = ops.ooblen = 2;
394 } else {
395 ops.len = ops.ooblen = 1;
396 }
397 ops.mode = MTD_OPS_PLACE_OOB;
398
399 /* Write to first/last page(s) if necessary */
400 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
401 ofs += mtd->erasesize - mtd->writesize;
402 do {
403 res = nand_do_write_oob(mtd, ofs, &ops);
404 if (!ret)
405 ret = res;
406
407 i++;
408 ofs += mtd->writesize;
409 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
410
411 return ret;
412}
413
414/**
415 * nand_block_markbad_lowlevel - mark a block bad
416 * @mtd: MTD device structure
417 * @ofs: offset from device start
418 *
419 * This function performs the generic NAND bad block marking steps (i.e., bad
420 * block table(s) and/or marker(s)). We only allow the hardware driver to
421 * specify how to write bad block markers to OOB (chip->block_markbad).
422 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700423 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800424 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700425 * (2) write bad block marker to OOB area of affected block (unless flag
426 * NAND_BBT_NO_OOB_BBM is present)
427 * (3) update the BBT
428 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800429 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700431static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200433 struct nand_chip *chip = mtd->priv;
Brian Norrisb32843b2013-07-30 17:52:59 -0700434 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000435
Brian Norrisb32843b2013-07-30 17:52:59 -0700436 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800437 struct erase_info einfo;
438
439 /* Attempt erase before marking OOB */
440 memset(&einfo, 0, sizeof(einfo));
441 einfo.mtd = mtd;
442 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300443 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800444 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800445
Brian Norrisb32843b2013-07-30 17:52:59 -0700446 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800447 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700448 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300449 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200450 }
Brian Norrise2414f42012-02-06 13:44:00 -0800451
Brian Norrisb32843b2013-07-30 17:52:59 -0700452 /* Mark block bad in BBT */
453 if (chip->bbt) {
454 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800455 if (!ret)
456 ret = res;
457 }
458
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200459 if (!ret)
460 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300461
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000465/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700467 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700469 * Check, if the device is write protected. The function expects, that the
470 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100472static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200474 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200475
Brian Norris8b6e50c2011-05-25 14:59:01 -0700476 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200477 if (chip->options & NAND_BROKEN_XD)
478 return 0;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200481 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
482 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800486 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 * @mtd: MTD device structure
488 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300489 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800490 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300491 */
492static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
493{
494 struct nand_chip *chip = mtd->priv;
495
496 if (!chip->bbt)
497 return 0;
498 /* Return info from the table */
499 return nand_isreserved_bbt(mtd, ofs);
500}
501
502/**
503 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
504 * @mtd: MTD device structure
505 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700506 * @getchip: 0, if the chip is already selected
507 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 *
509 * Check, if the block is bad. Either by reading the bad block table or
510 * calling of the scan function.
511 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200512static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
513 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200515 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000516
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200517 if (!chip->bbt)
518 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100521 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200524/**
525 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700526 * @mtd: MTD device structure
527 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200528 *
529 * Helper function for nand_wait_ready used when needing to wait in interrupt
530 * context.
531 */
532static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
533{
534 struct nand_chip *chip = mtd->priv;
535 int i;
536
537 /* Wait for the device to get ready */
538 for (i = 0; i < timeo; i++) {
539 if (chip->dev_ready(mtd))
540 break;
541 touch_softlockup_watchdog();
542 mdelay(1);
543 }
544}
545
Brian Norris7854d3f2011-06-23 14:12:08 -0700546/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100547void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000548{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200549 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100550 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000551
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200552 /* 400ms timeout */
553 if (in_interrupt() || oops_in_progress)
554 return panic_nand_wait_ready(mtd, 400);
555
Richard Purdie8fe833c2006-03-31 02:31:14 -0800556 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700557 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000558 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200559 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800560 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700561 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000562 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800563 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000564}
David Woodhouse4b648b02006-09-25 17:05:24 +0100565EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200568 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
569 * @mtd: MTD device structure
570 * @timeo: Timeout in ms
571 *
572 * Wait for status ready (i.e. command done) or timeout.
573 */
574static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
575{
576 register struct nand_chip *chip = mtd->priv;
577
578 timeo = jiffies + msecs_to_jiffies(timeo);
579 do {
580 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
581 break;
582 touch_softlockup_watchdog();
583 } while (time_before(jiffies, timeo));
584};
585
586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700588 * @mtd: MTD device structure
589 * @command: the command to be sent
590 * @column: the column address for this command, -1 if none
591 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700593 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200594 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200596static void nand_command(struct mtd_info *mtd, unsigned int command,
597 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200599 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200600 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Brian Norris8b6e50c2011-05-25 14:59:01 -0700602 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (command == NAND_CMD_SEQIN) {
604 int readcmd;
605
Joern Engel28318772006-05-22 23:18:05 +0200606 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200608 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 readcmd = NAND_CMD_READOOB;
610 } else if (column < 256) {
611 /* First 256 bytes --> READ0 */
612 readcmd = NAND_CMD_READ0;
613 } else {
614 column -= 256;
615 readcmd = NAND_CMD_READ1;
616 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200617 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200618 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200620 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Brian Norris8b6e50c2011-05-25 14:59:01 -0700622 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
624 /* Serially input address */
625 if (column != -1) {
626 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800627 if (chip->options & NAND_BUSWIDTH_16 &&
628 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200629 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200630 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200631 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200633 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200634 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200635 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200636 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200637 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200638 if (chip->chipsize > (32 << 20))
639 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200640 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000642
643 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700644 * Program and erase have their own busy handlers status and sequential
645 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 case NAND_CMD_PAGEPROG:
650 case NAND_CMD_ERASE1:
651 case NAND_CMD_ERASE2:
652 case NAND_CMD_SEQIN:
653 case NAND_CMD_STATUS:
654 return;
655
656 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200657 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200659 udelay(chip->chip_delay);
660 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200662 chip->cmd_ctrl(mtd,
663 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200664 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
665 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return;
667
David Woodhousee0c7d762006-05-13 18:07:53 +0100668 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000670 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * If we don't have access to the busy pin, we apply the given
672 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100673 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200674 if (!chip->dev_ready) {
675 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700679 /*
680 * Apply this short delay always to ensure that we do wait tWB in
681 * any case on any machine.
682 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100683 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000684
685 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688/**
689 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700690 * @mtd: MTD device structure
691 * @command: the command to be sent
692 * @column: the column address for this command, -1 if none
693 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200695 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700696 * devices. We don't have the separate regions as we have in the small page
697 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200699static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
700 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200702 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* Emulate NAND_CMD_READOOB */
705 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200706 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 command = NAND_CMD_READ0;
708 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200710 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400711 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200714 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Serially input address */
717 if (column != -1) {
718 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800719 if (chip->options & NAND_BUSWIDTH_16 &&
720 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200722 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 chip->cmd_ctrl(mtd, page_addr, ctrl);
728 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200729 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->chipsize > (128 << 20))
732 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200733 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000737
738 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700739 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100740 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case NAND_CMD_CACHEDPROG:
745 case NAND_CMD_PAGEPROG:
746 case NAND_CMD_ERASE1:
747 case NAND_CMD_ERASE2:
748 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200749 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000751 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200756 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200757 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
758 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
759 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
760 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200761 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
762 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return;
764
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200765 case NAND_CMD_RNDOUT:
766 /* No ready / busy check necessary */
767 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
768 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
769 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
770 NAND_NCE | NAND_CTRL_CHANGE);
771 return;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200774 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
775 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
776 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
777 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000778
David Woodhousee0c7d762006-05-13 18:07:53 +0100779 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000781 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700783 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100784 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200785 if (!chip->dev_ready) {
786 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000790
Brian Norris8b6e50c2011-05-25 14:59:01 -0700791 /*
792 * Apply this short delay always to ensure that we do wait tWB in
793 * any case on any machine.
794 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100795 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000796
797 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200801 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700802 * @chip: the nand chip descriptor
803 * @mtd: MTD device structure
804 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200805 *
806 * Used when in panic, no locks are taken.
807 */
808static void panic_nand_get_device(struct nand_chip *chip,
809 struct mtd_info *mtd, int new_state)
810{
Brian Norris7854d3f2011-06-23 14:12:08 -0700811 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200812 chip->controller->active = chip;
813 chip->state = new_state;
814}
815
816/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700818 * @mtd: MTD device structure
819 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
821 * Get the device and lock it for exclusive access
822 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200823static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800824nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800826 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200827 spinlock_t *lock = &chip->controller->lock;
828 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100829 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200830retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100831 spin_lock(lock);
832
vimal singhb8b3ee92009-07-09 20:41:22 +0530833 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200834 if (!chip->controller->active)
835 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200836
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200837 if (chip->controller->active == chip && chip->state == FL_READY) {
838 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100839 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100840 return 0;
841 }
842 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800843 if (chip->controller->active->state == FL_PM_SUSPENDED) {
844 chip->state = FL_PM_SUSPENDED;
845 spin_unlock(lock);
846 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800847 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100848 }
849 set_current_state(TASK_UNINTERRUPTIBLE);
850 add_wait_queue(wq, &wait);
851 spin_unlock(lock);
852 schedule();
853 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 goto retry;
855}
856
857/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700858 * panic_nand_wait - [GENERIC] wait until the command is done
859 * @mtd: MTD device structure
860 * @chip: NAND chip structure
861 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200862 *
863 * Wait for command done. This is a helper function for nand_wait used when
864 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400865 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200866 */
867static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
868 unsigned long timeo)
869{
870 int i;
871 for (i = 0; i < timeo; i++) {
872 if (chip->dev_ready) {
873 if (chip->dev_ready(mtd))
874 break;
875 } else {
876 if (chip->read_byte(mtd) & NAND_STATUS_READY)
877 break;
878 }
879 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200880 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200881}
882
883/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700884 * nand_wait - [DEFAULT] wait until the command is done
885 * @mtd: MTD device structure
886 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700888 * Wait for command done. This applies to erase and program only. Erase can
889 * take up to 400ms and program up to 20ms according to general NAND and
890 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700891 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200892static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200895 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800896 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Richard Purdie8fe833c2006-03-31 02:31:14 -0800898 led_trigger_event(nand_led_trigger, LED_FULL);
899
Brian Norris8b6e50c2011-05-25 14:59:01 -0700900 /*
901 * Apply this short delay always to ensure that we do wait tWB in any
902 * case on any machine.
903 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100904 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200906 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200908 if (in_interrupt() || oops_in_progress)
909 panic_nand_wait(mtd, chip, timeo);
910 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800911 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200912 while (time_before(jiffies, timeo)) {
913 if (chip->dev_ready) {
914 if (chip->dev_ready(mtd))
915 break;
916 } else {
917 if (chip->read_byte(mtd) & NAND_STATUS_READY)
918 break;
919 }
920 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800923 led_trigger_event(nand_led_trigger, LED_OFF);
924
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200925 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100926 /* This can happen if in case of timeout or buggy dev_ready */
927 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return status;
929}
930
931/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700932 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700933 * @mtd: mtd info
934 * @ofs: offset to start unlock from
935 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700936 * @invert: when = 0, unlock the range of blocks within the lower and
937 * upper boundary address
938 * when = 1, unlock the range of blocks outside the boundaries
939 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530940 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700941 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530942 */
943static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
944 uint64_t len, int invert)
945{
946 int ret = 0;
947 int status, page;
948 struct nand_chip *chip = mtd->priv;
949
950 /* Submit address of first page to unlock */
951 page = ofs >> chip->page_shift;
952 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
953
954 /* Submit address of last page to unlock */
955 page = (ofs + len) >> chip->page_shift;
956 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
957 (page | invert) & chip->pagemask);
958
959 /* Call wait ready function */
960 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530961 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400962 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700963 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530964 __func__, status);
965 ret = -EIO;
966 }
967
968 return ret;
969}
970
971/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700972 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700973 * @mtd: mtd info
974 * @ofs: offset to start unlock from
975 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530976 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700977 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530978 */
979int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
980{
981 int ret = 0;
982 int chipnr;
983 struct nand_chip *chip = mtd->priv;
984
Brian Norris289c0522011-07-19 10:06:09 -0700985 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530986 __func__, (unsigned long long)ofs, len);
987
988 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -0800989 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +0530990
991 /* Align to last block address if size addresses end of the device */
992 if (ofs + len == mtd->size)
993 len -= mtd->erasesize;
994
Huang Shijie6a8214a2012-11-19 14:43:30 +0800995 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530996
997 /* Shift to get chip number */
998 chipnr = ofs >> chip->chip_shift;
999
1000 chip->select_chip(mtd, chipnr);
1001
White Ding57d3a9a2014-07-24 00:10:45 +08001002 /*
1003 * Reset the chip.
1004 * If we want to check the WP through READ STATUS and check the bit 7
1005 * we must reset the chip
1006 * some operation can also clear the bit 7 of status register
1007 * eg. erase/program a locked block
1008 */
1009 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1010
Vimal Singh7d70f332010-02-08 15:50:49 +05301011 /* Check, if it is write protected */
1012 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001013 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301014 __func__);
1015 ret = -EIO;
1016 goto out;
1017 }
1018
1019 ret = __nand_unlock(mtd, ofs, len, 0);
1020
1021out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001022 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301023 nand_release_device(mtd);
1024
1025 return ret;
1026}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001027EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301028
1029/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001030 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001031 * @mtd: mtd info
1032 * @ofs: offset to start unlock from
1033 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301034 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001035 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1036 * have this feature, but it allows only to lock all blocks, not for specified
1037 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1038 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301039 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001040 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301041 */
1042int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1043{
1044 int ret = 0;
1045 int chipnr, status, page;
1046 struct nand_chip *chip = mtd->priv;
1047
Brian Norris289c0522011-07-19 10:06:09 -07001048 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301049 __func__, (unsigned long long)ofs, len);
1050
1051 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001052 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301053
Huang Shijie6a8214a2012-11-19 14:43:30 +08001054 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301055
1056 /* Shift to get chip number */
1057 chipnr = ofs >> chip->chip_shift;
1058
1059 chip->select_chip(mtd, chipnr);
1060
White Ding57d3a9a2014-07-24 00:10:45 +08001061 /*
1062 * Reset the chip.
1063 * If we want to check the WP through READ STATUS and check the bit 7
1064 * we must reset the chip
1065 * some operation can also clear the bit 7 of status register
1066 * eg. erase/program a locked block
1067 */
1068 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1069
Vimal Singh7d70f332010-02-08 15:50:49 +05301070 /* Check, if it is write protected */
1071 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001072 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301073 __func__);
1074 status = MTD_ERASE_FAILED;
1075 ret = -EIO;
1076 goto out;
1077 }
1078
1079 /* Submit address of first page to lock */
1080 page = ofs >> chip->page_shift;
1081 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1082
1083 /* Call wait ready function */
1084 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301085 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001086 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001087 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301088 __func__, status);
1089 ret = -EIO;
1090 goto out;
1091 }
1092
1093 ret = __nand_unlock(mtd, ofs, len, 0x1);
1094
1095out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001096 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301097 nand_release_device(mtd);
1098
1099 return ret;
1100}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001101EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301102
1103/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001104 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1105 * @buf: buffer to test
1106 * @len: buffer length
1107 * @bitflips_threshold: maximum number of bitflips
1108 *
1109 * Check if a buffer contains only 0xff, which means the underlying region
1110 * has been erased and is ready to be programmed.
1111 * The bitflips_threshold specify the maximum number of bitflips before
1112 * considering the region is not erased.
1113 * Note: The logic of this function has been extracted from the memweight
1114 * implementation, except that nand_check_erased_buf function exit before
1115 * testing the whole buffer if the number of bitflips exceed the
1116 * bitflips_threshold value.
1117 *
1118 * Returns a positive number of bitflips less than or equal to
1119 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1120 * threshold.
1121 */
1122static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1123{
1124 const unsigned char *bitmap = buf;
1125 int bitflips = 0;
1126 int weight;
1127
1128 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1129 len--, bitmap++) {
1130 weight = hweight8(*bitmap);
1131 bitflips += BITS_PER_BYTE - weight;
1132 if (unlikely(bitflips > bitflips_threshold))
1133 return -EBADMSG;
1134 }
1135
1136 for (; len >= sizeof(long);
1137 len -= sizeof(long), bitmap += sizeof(long)) {
1138 weight = hweight_long(*((unsigned long *)bitmap));
1139 bitflips += BITS_PER_LONG - weight;
1140 if (unlikely(bitflips > bitflips_threshold))
1141 return -EBADMSG;
1142 }
1143
1144 for (; len > 0; len--, bitmap++) {
1145 weight = hweight8(*bitmap);
1146 bitflips += BITS_PER_BYTE - weight;
1147 if (unlikely(bitflips > bitflips_threshold))
1148 return -EBADMSG;
1149 }
1150
1151 return bitflips;
1152}
1153
1154/**
1155 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1156 * 0xff data
1157 * @data: data buffer to test
1158 * @datalen: data length
1159 * @ecc: ECC buffer
1160 * @ecclen: ECC length
1161 * @extraoob: extra OOB buffer
1162 * @extraooblen: extra OOB length
1163 * @bitflips_threshold: maximum number of bitflips
1164 *
1165 * Check if a data buffer and its associated ECC and OOB data contains only
1166 * 0xff pattern, which means the underlying region has been erased and is
1167 * ready to be programmed.
1168 * The bitflips_threshold specify the maximum number of bitflips before
1169 * considering the region as not erased.
1170 *
1171 * Note:
1172 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1173 * different from the NAND page size. When fixing bitflips, ECC engines will
1174 * report the number of errors per chunk, and the NAND core infrastructure
1175 * expect you to return the maximum number of bitflips for the whole page.
1176 * This is why you should always use this function on a single chunk and
1177 * not on the whole page. After checking each chunk you should update your
1178 * max_bitflips value accordingly.
1179 * 2/ When checking for bitflips in erased pages you should not only check
1180 * the payload data but also their associated ECC data, because a user might
1181 * have programmed almost all bits to 1 but a few. In this case, we
1182 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1183 * this case.
1184 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1185 * data are protected by the ECC engine.
1186 * It could also be used if you support subpages and want to attach some
1187 * extra OOB data to an ECC chunk.
1188 *
1189 * Returns a positive number of bitflips less than or equal to
1190 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1191 * threshold. In case of success, the passed buffers are filled with 0xff.
1192 */
1193int nand_check_erased_ecc_chunk(void *data, int datalen,
1194 void *ecc, int ecclen,
1195 void *extraoob, int extraooblen,
1196 int bitflips_threshold)
1197{
1198 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1199
1200 data_bitflips = nand_check_erased_buf(data, datalen,
1201 bitflips_threshold);
1202 if (data_bitflips < 0)
1203 return data_bitflips;
1204
1205 bitflips_threshold -= data_bitflips;
1206
1207 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1208 if (ecc_bitflips < 0)
1209 return ecc_bitflips;
1210
1211 bitflips_threshold -= ecc_bitflips;
1212
1213 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1214 bitflips_threshold);
1215 if (extraoob_bitflips < 0)
1216 return extraoob_bitflips;
1217
1218 if (data_bitflips)
1219 memset(data, 0xff, datalen);
1220
1221 if (ecc_bitflips)
1222 memset(ecc, 0xff, ecclen);
1223
1224 if (extraoob_bitflips)
1225 memset(extraoob, 0xff, extraooblen);
1226
1227 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1228}
1229EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1230
1231/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001232 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001233 * @mtd: mtd info structure
1234 * @chip: nand chip info structure
1235 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001236 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001237 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001238 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001239 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001240 */
1241static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001242 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001243{
1244 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001245 if (oob_required)
1246 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001247 return 0;
1248}
1249
1250/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001251 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001252 * @mtd: mtd info structure
1253 * @chip: nand chip info structure
1254 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001255 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001256 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001257 *
1258 * We need a special oob layout and handling even when OOB isn't used.
1259 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001260static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001261 struct nand_chip *chip, uint8_t *buf,
1262 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001263{
1264 int eccsize = chip->ecc.size;
1265 int eccbytes = chip->ecc.bytes;
1266 uint8_t *oob = chip->oob_poi;
1267 int steps, size;
1268
1269 for (steps = chip->ecc.steps; steps > 0; steps--) {
1270 chip->read_buf(mtd, buf, eccsize);
1271 buf += eccsize;
1272
1273 if (chip->ecc.prepad) {
1274 chip->read_buf(mtd, oob, chip->ecc.prepad);
1275 oob += chip->ecc.prepad;
1276 }
1277
1278 chip->read_buf(mtd, oob, eccbytes);
1279 oob += eccbytes;
1280
1281 if (chip->ecc.postpad) {
1282 chip->read_buf(mtd, oob, chip->ecc.postpad);
1283 oob += chip->ecc.postpad;
1284 }
1285 }
1286
1287 size = mtd->oobsize - (oob - chip->oob_poi);
1288 if (size)
1289 chip->read_buf(mtd, oob, size);
1290
1291 return 0;
1292}
1293
1294/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001295 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001296 * @mtd: mtd info structure
1297 * @chip: nand chip info structure
1298 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001299 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001300 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001301 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001302static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001303 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001305 int i, eccsize = chip->ecc.size;
1306 int eccbytes = chip->ecc.bytes;
1307 int eccsteps = chip->ecc.steps;
1308 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001309 uint8_t *ecc_calc = chip->buffers->ecccalc;
1310 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001311 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001312 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001313
Brian Norris1fbb9382012-05-02 10:14:55 -07001314 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001315
1316 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1317 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1318
1319 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001320 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001321
1322 eccsteps = chip->ecc.steps;
1323 p = buf;
1324
1325 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1326 int stat;
1327
1328 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001329 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001330 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001331 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001332 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001333 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1334 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001335 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001336 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301340 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001341 * @mtd: mtd info structure
1342 * @chip: nand chip info structure
1343 * @data_offs: offset of requested data within the page
1344 * @readlen: data length
1345 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001346 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001347 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001348static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001349 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1350 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001351{
1352 int start_step, end_step, num_steps;
1353 uint32_t *eccpos = chip->ecc.layout->eccpos;
1354 uint8_t *p;
1355 int data_col_addr, i, gaps = 0;
1356 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1357 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301358 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001359 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001360
Brian Norris7854d3f2011-06-23 14:12:08 -07001361 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001362 start_step = data_offs / chip->ecc.size;
1363 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1364 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301365 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001366
Brian Norris8b6e50c2011-05-25 14:59:01 -07001367 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001368 datafrag_len = num_steps * chip->ecc.size;
1369 eccfrag_len = num_steps * chip->ecc.bytes;
1370
1371 data_col_addr = start_step * chip->ecc.size;
1372 /* If we read not a page aligned data */
1373 if (data_col_addr != 0)
1374 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1375
1376 p = bufpoi + data_col_addr;
1377 chip->read_buf(mtd, p, datafrag_len);
1378
Brian Norris8b6e50c2011-05-25 14:59:01 -07001379 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001380 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1381 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1382
Brian Norris8b6e50c2011-05-25 14:59:01 -07001383 /*
1384 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001385 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001386 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001387 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301388 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001389 gaps = 1;
1390 break;
1391 }
1392 }
1393 if (gaps) {
1394 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1395 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1396 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001397 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001398 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001399 * about buswidth alignment in read_buf.
1400 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001401 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001402 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001403 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001404 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001405 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001406 aligned_len++;
1407
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001408 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1409 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001410 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1411 }
1412
1413 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001414 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001415
1416 p = bufpoi + data_col_addr;
1417 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1418 int stat;
1419
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001420 stat = chip->ecc.correct(mtd, p,
1421 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001422 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001423 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001424 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001425 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001426 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1427 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001428 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001429 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001430}
1431
1432/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001433 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001434 * @mtd: mtd info structure
1435 * @chip: nand chip info structure
1436 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001437 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001438 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001439 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001440 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001441 */
1442static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001443 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444{
1445 int i, eccsize = chip->ecc.size;
1446 int eccbytes = chip->ecc.bytes;
1447 int eccsteps = chip->ecc.steps;
1448 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001449 uint8_t *ecc_calc = chip->buffers->ecccalc;
1450 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001451 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001452 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001453
1454 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1455 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1456 chip->read_buf(mtd, p, eccsize);
1457 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1458 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001459 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001460
1461 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001462 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001463
1464 eccsteps = chip->ecc.steps;
1465 p = buf;
1466
1467 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1468 int stat;
1469
1470 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001471 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001472 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001473 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001474 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001475 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1476 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001477 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001478 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001479}
1480
1481/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001482 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001483 * @mtd: mtd info structure
1484 * @chip: nand chip info structure
1485 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001486 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001487 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001488 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001489 * Hardware ECC for large page chips, require OOB to be read first. For this
1490 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1491 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1492 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1493 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001494 */
1495static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001496 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001497{
1498 int i, eccsize = chip->ecc.size;
1499 int eccbytes = chip->ecc.bytes;
1500 int eccsteps = chip->ecc.steps;
1501 uint8_t *p = buf;
1502 uint8_t *ecc_code = chip->buffers->ecccode;
1503 uint32_t *eccpos = chip->ecc.layout->eccpos;
1504 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001505 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001506
1507 /* Read the OOB area first */
1508 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1509 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1510 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1511
1512 for (i = 0; i < chip->ecc.total; i++)
1513 ecc_code[i] = chip->oob_poi[eccpos[i]];
1514
1515 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1516 int stat;
1517
1518 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1519 chip->read_buf(mtd, p, eccsize);
1520 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1521
1522 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001523 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001524 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001525 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001526 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001527 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1528 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001529 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001530 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001531}
1532
1533/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001534 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001535 * @mtd: mtd info structure
1536 * @chip: nand chip info structure
1537 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001538 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001539 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001540 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001541 * The hw generator calculates the error syndrome automatically. Therefore we
1542 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001543 */
1544static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001545 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001546{
1547 int i, eccsize = chip->ecc.size;
1548 int eccbytes = chip->ecc.bytes;
1549 int eccsteps = chip->ecc.steps;
1550 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001551 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001552 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001553
1554 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1555 int stat;
1556
1557 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1558 chip->read_buf(mtd, p, eccsize);
1559
1560 if (chip->ecc.prepad) {
1561 chip->read_buf(mtd, oob, chip->ecc.prepad);
1562 oob += chip->ecc.prepad;
1563 }
1564
1565 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1566 chip->read_buf(mtd, oob, eccbytes);
1567 stat = chip->ecc.correct(mtd, p, oob, NULL);
1568
Mike Dunn3f91e942012-04-25 12:06:09 -07001569 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001570 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001571 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001572 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001573 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1574 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001575
1576 oob += eccbytes;
1577
1578 if (chip->ecc.postpad) {
1579 chip->read_buf(mtd, oob, chip->ecc.postpad);
1580 oob += chip->ecc.postpad;
1581 }
1582 }
1583
1584 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001585 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001586 if (i)
1587 chip->read_buf(mtd, oob, i);
1588
Mike Dunn3f91e942012-04-25 12:06:09 -07001589 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001590}
1591
1592/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001593 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001594 * @chip: nand chip structure
1595 * @oob: oob destination address
1596 * @ops: oob ops structure
1597 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001598 */
1599static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001600 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001601{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001602 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001603
Brian Norris0612b9d2011-08-30 18:45:40 -07001604 case MTD_OPS_PLACE_OOB:
1605 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001606 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1607 return oob + len;
1608
Brian Norris0612b9d2011-08-30 18:45:40 -07001609 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001610 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001611 uint32_t boffs = 0, roffs = ops->ooboffs;
1612 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001613
Florian Fainellif8ac0412010-09-07 13:23:43 +02001614 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001615 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001616 if (unlikely(roffs)) {
1617 if (roffs >= free->length) {
1618 roffs -= free->length;
1619 continue;
1620 }
1621 boffs = free->offset + roffs;
1622 bytes = min_t(size_t, len,
1623 (free->length - roffs));
1624 roffs = 0;
1625 } else {
1626 bytes = min_t(size_t, len, free->length);
1627 boffs = free->offset;
1628 }
1629 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001630 oob += bytes;
1631 }
1632 return oob;
1633 }
1634 default:
1635 BUG();
1636 }
1637 return NULL;
1638}
1639
1640/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001641 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1642 * @mtd: MTD device structure
1643 * @retry_mode: the retry mode to use
1644 *
1645 * Some vendors supply a special command to shift the Vt threshold, to be used
1646 * when there are too many bitflips in a page (i.e., ECC error). After setting
1647 * a new threshold, the host should retry reading the page.
1648 */
1649static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1650{
1651 struct nand_chip *chip = mtd->priv;
1652
1653 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1654
1655 if (retry_mode >= chip->read_retries)
1656 return -EINVAL;
1657
1658 if (!chip->setup_read_retry)
1659 return -EOPNOTSUPP;
1660
1661 return chip->setup_read_retry(mtd, retry_mode);
1662}
1663
1664/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001665 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001666 * @mtd: MTD device structure
1667 * @from: offset to read from
1668 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001669 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001670 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001671 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001672static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1673 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001674{
Brian Norrise47f3db2012-05-02 10:14:56 -07001675 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001676 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001677 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001678 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001679 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001680 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001681 mtd->oobavail : mtd->oobsize;
1682
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001683 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001684 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001685 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001686 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001687 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001689 chipnr = (int)(from >> chip->chip_shift);
1690 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001692 realpage = (int)(from >> chip->page_shift);
1693 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001695 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001697 buf = ops->datbuf;
1698 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001699 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001700
Florian Fainellif8ac0412010-09-07 13:23:43 +02001701 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001702 unsigned int ecc_failures = mtd->ecc_stats.failed;
1703
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001704 bytes = min(mtd->writesize - col, readlen);
1705 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001706
Kamal Dasu66507c72014-05-01 20:51:19 -04001707 if (!aligned)
1708 use_bufpoi = 1;
1709 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1710 use_bufpoi = !virt_addr_valid(buf);
1711 else
1712 use_bufpoi = 0;
1713
Brian Norris8b6e50c2011-05-25 14:59:01 -07001714 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001715 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001716 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1717
1718 if (use_bufpoi && aligned)
1719 pr_debug("%s: using read bounce buffer for buf@%p\n",
1720 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Brian Norrisba84fb52014-01-03 15:13:33 -08001722read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001723 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Mike Dunnedbc45402012-04-25 12:06:11 -07001725 /*
1726 * Now read the page into the buffer. Absent an error,
1727 * the read methods return max bitflips per ecc step.
1728 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001729 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001730 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001731 oob_required,
1732 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001733 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1734 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001735 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001736 col, bytes, bufpoi,
1737 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001738 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001739 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001740 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001741 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001742 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001743 /* Invalidate page cache */
1744 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001745 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001746 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001747
Mike Dunnedbc45402012-04-25 12:06:11 -07001748 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1749
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001750 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001751 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001752 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001753 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001754 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001755 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001756 chip->pagebuf_bitflips = ret;
1757 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001758 /* Invalidate page cache */
1759 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001760 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001761 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001763
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001764 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001765 int toread = min(oobreadlen, max_oobsize);
1766
1767 if (toread) {
1768 oob = nand_transfer_oob(chip,
1769 oob, ops, toread);
1770 oobreadlen -= toread;
1771 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001772 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001773
1774 if (chip->options & NAND_NEED_READRDY) {
1775 /* Apply delay or wait for ready/busy pin */
1776 if (!chip->dev_ready)
1777 udelay(chip->chip_delay);
1778 else
1779 nand_wait_ready(mtd);
1780 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001781
Brian Norrisba84fb52014-01-03 15:13:33 -08001782 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001783 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001784 retry_mode++;
1785 ret = nand_setup_read_retry(mtd,
1786 retry_mode);
1787 if (ret < 0)
1788 break;
1789
1790 /* Reset failures; retry */
1791 mtd->ecc_stats.failed = ecc_failures;
1792 goto read_retry;
1793 } else {
1794 /* No more retry modes; real failure */
1795 ecc_fail = true;
1796 }
1797 }
1798
1799 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001800 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001801 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001802 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001803 max_bitflips = max_t(unsigned int, max_bitflips,
1804 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001807 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001808
Brian Norrisba84fb52014-01-03 15:13:33 -08001809 /* Reset to retry mode 0 */
1810 if (retry_mode) {
1811 ret = nand_setup_read_retry(mtd, 0);
1812 if (ret < 0)
1813 break;
1814 retry_mode = 0;
1815 }
1816
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001817 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Brian Norris8b6e50c2011-05-25 14:59:01 -07001820 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 col = 0;
1822 /* Increment page address */
1823 realpage++;
1824
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001825 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /* Check, if we cross a chip boundary */
1827 if (!page) {
1828 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001829 chip->select_chip(mtd, -1);
1830 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001833 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001835 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001836 if (oob)
1837 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Mike Dunn3f91e942012-04-25 12:06:09 -07001839 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001840 return ret;
1841
Brian Norrisb72f3df2013-12-03 11:04:14 -08001842 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001843 return -EBADMSG;
1844
Mike Dunnedbc45402012-04-25 12:06:11 -07001845 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001846}
1847
1848/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001849 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001850 * @mtd: MTD device structure
1851 * @from: offset to read from
1852 * @len: number of bytes to read
1853 * @retlen: pointer to variable to store the number of read bytes
1854 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001855 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001856 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001857 */
1858static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1859 size_t *retlen, uint8_t *buf)
1860{
Brian Norris4a89ff82011-08-30 18:45:45 -07001861 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001862 int ret;
1863
Huang Shijie6a8214a2012-11-19 14:43:30 +08001864 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001865 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001866 ops.len = len;
1867 ops.datbuf = buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08001868 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001869 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001870 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001871 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001872 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001876 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001877 * @mtd: mtd info structure
1878 * @chip: nand chip info structure
1879 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001880 */
1881static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001882 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001883{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001884 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001885 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001886 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001887}
1888
1889/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001890 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001891 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001892 * @mtd: mtd info structure
1893 * @chip: nand chip info structure
1894 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001895 */
1896static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001897 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001898{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001899 int length = mtd->oobsize;
1900 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1901 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001902 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001903 int i, toread, sndrnd = 0, pos;
1904
1905 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1906 for (i = 0; i < chip->ecc.steps; i++) {
1907 if (sndrnd) {
1908 pos = eccsize + i * (eccsize + chunk);
1909 if (mtd->writesize > 512)
1910 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1911 else
1912 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1913 } else
1914 sndrnd = 1;
1915 toread = min_t(int, length, chunk);
1916 chip->read_buf(mtd, bufpoi, toread);
1917 bufpoi += toread;
1918 length -= toread;
1919 }
1920 if (length > 0)
1921 chip->read_buf(mtd, bufpoi, length);
1922
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001923 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001924}
1925
1926/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001927 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001928 * @mtd: mtd info structure
1929 * @chip: nand chip info structure
1930 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001931 */
1932static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1933 int page)
1934{
1935 int status = 0;
1936 const uint8_t *buf = chip->oob_poi;
1937 int length = mtd->oobsize;
1938
1939 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1940 chip->write_buf(mtd, buf, length);
1941 /* Send command to program the OOB data */
1942 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1943
1944 status = chip->waitfunc(mtd, chip);
1945
Savin Zlobec0d420f92006-06-21 11:51:20 +02001946 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001947}
1948
1949/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001950 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001951 * with syndrome - only for large page flash
1952 * @mtd: mtd info structure
1953 * @chip: nand chip info structure
1954 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001955 */
1956static int nand_write_oob_syndrome(struct mtd_info *mtd,
1957 struct nand_chip *chip, int page)
1958{
1959 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1960 int eccsize = chip->ecc.size, length = mtd->oobsize;
1961 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1962 const uint8_t *bufpoi = chip->oob_poi;
1963
1964 /*
1965 * data-ecc-data-ecc ... ecc-oob
1966 * or
1967 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1968 */
1969 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1970 pos = steps * (eccsize + chunk);
1971 steps = 0;
1972 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001973 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001974
1975 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1976 for (i = 0; i < steps; i++) {
1977 if (sndcmd) {
1978 if (mtd->writesize <= 512) {
1979 uint32_t fill = 0xFFFFFFFF;
1980
1981 len = eccsize;
1982 while (len > 0) {
1983 int num = min_t(int, len, 4);
1984 chip->write_buf(mtd, (uint8_t *)&fill,
1985 num);
1986 len -= num;
1987 }
1988 } else {
1989 pos = eccsize + i * (eccsize + chunk);
1990 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1991 }
1992 } else
1993 sndcmd = 1;
1994 len = min_t(int, length, chunk);
1995 chip->write_buf(mtd, bufpoi, len);
1996 bufpoi += len;
1997 length -= len;
1998 }
1999 if (length > 0)
2000 chip->write_buf(mtd, bufpoi, length);
2001
2002 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2003 status = chip->waitfunc(mtd, chip);
2004
2005 return status & NAND_STATUS_FAIL ? -EIO : 0;
2006}
2007
2008/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002009 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002010 * @mtd: MTD device structure
2011 * @from: offset to read from
2012 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002014 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002016static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2017 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Brian Norrisc00a0992012-05-01 17:12:54 -07002019 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002020 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07002021 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002022 int readlen = ops->ooblen;
2023 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002024 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002025 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Brian Norris289c0522011-07-19 10:06:09 -07002027 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302028 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Brian Norris041e4572011-06-23 16:45:24 -07002030 stats = mtd->ecc_stats;
2031
Brian Norris0612b9d2011-08-30 18:45:40 -07002032 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03002033 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02002034 else
2035 len = mtd->oobsize;
2036
2037 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002038 pr_debug("%s: attempt to start read outside oob\n",
2039 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002040 return -EINVAL;
2041 }
2042
2043 /* Do not allow reads past end of device */
2044 if (unlikely(from >= mtd->size ||
2045 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2046 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002047 pr_debug("%s: attempt to read beyond end of device\n",
2048 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002049 return -EINVAL;
2050 }
Vitaly Wool70145682006-11-03 18:20:38 +03002051
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002052 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002053 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002055 /* Shift to get page */
2056 realpage = (int)(from >> chip->page_shift);
2057 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Florian Fainellif8ac0412010-09-07 13:23:43 +02002059 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002060 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002061 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002062 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002063 ret = chip->ecc.read_oob(mtd, chip, page);
2064
2065 if (ret < 0)
2066 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002067
2068 len = min(len, readlen);
2069 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002070
Brian Norris5bc7c332013-03-13 09:51:31 -07002071 if (chip->options & NAND_NEED_READRDY) {
2072 /* Apply delay or wait for ready/busy pin */
2073 if (!chip->dev_ready)
2074 udelay(chip->chip_delay);
2075 else
2076 nand_wait_ready(mtd);
2077 }
2078
Vitaly Wool70145682006-11-03 18:20:38 +03002079 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002080 if (!readlen)
2081 break;
2082
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002083 /* Increment page address */
2084 realpage++;
2085
2086 page = realpage & chip->pagemask;
2087 /* Check, if we cross a chip boundary */
2088 if (!page) {
2089 chipnr++;
2090 chip->select_chip(mtd, -1);
2091 chip->select_chip(mtd, chipnr);
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002094 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002096 ops->oobretlen = ops->ooblen - readlen;
2097
2098 if (ret < 0)
2099 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002100
2101 if (mtd->ecc_stats.failed - stats.failed)
2102 return -EBADMSG;
2103
2104 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
2107/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002108 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002109 * @mtd: MTD device structure
2110 * @from: offset to read from
2111 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002113 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002115static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2116 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002118 int ret = -ENOTSUPP;
2119
2120 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002123 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002124 pr_debug("%s: attempt to read beyond end of device\n",
2125 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return -EINVAL;
2127 }
2128
Huang Shijie6a8214a2012-11-19 14:43:30 +08002129 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Florian Fainellif8ac0412010-09-07 13:23:43 +02002131 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002132 case MTD_OPS_PLACE_OOB:
2133 case MTD_OPS_AUTO_OOB:
2134 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002135 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002136
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002137 default:
2138 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002141 if (!ops->datbuf)
2142 ret = nand_do_read_oob(mtd, from, ops);
2143 else
2144 ret = nand_do_read_ops(mtd, from, ops);
2145
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002146out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002148 return ret;
2149}
2150
2151
2152/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002153 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002154 * @mtd: mtd info structure
2155 * @chip: nand chip info structure
2156 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002157 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002158 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002159 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002160 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002161static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002162 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002163{
2164 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002165 if (oob_required)
2166 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002167
2168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002171/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002172 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002173 * @mtd: mtd info structure
2174 * @chip: nand chip info structure
2175 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002176 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002177 *
2178 * We need a special oob layout and handling even when ECC isn't checked.
2179 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002180static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002181 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002182 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08002183{
2184 int eccsize = chip->ecc.size;
2185 int eccbytes = chip->ecc.bytes;
2186 uint8_t *oob = chip->oob_poi;
2187 int steps, size;
2188
2189 for (steps = chip->ecc.steps; steps > 0; steps--) {
2190 chip->write_buf(mtd, buf, eccsize);
2191 buf += eccsize;
2192
2193 if (chip->ecc.prepad) {
2194 chip->write_buf(mtd, oob, chip->ecc.prepad);
2195 oob += chip->ecc.prepad;
2196 }
2197
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002198 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002199 oob += eccbytes;
2200
2201 if (chip->ecc.postpad) {
2202 chip->write_buf(mtd, oob, chip->ecc.postpad);
2203 oob += chip->ecc.postpad;
2204 }
2205 }
2206
2207 size = mtd->oobsize - (oob - chip->oob_poi);
2208 if (size)
2209 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002210
2211 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002212}
2213/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002214 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002215 * @mtd: mtd info structure
2216 * @chip: nand chip info structure
2217 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002218 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002219 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002220static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002221 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002222{
2223 int i, eccsize = chip->ecc.size;
2224 int eccbytes = chip->ecc.bytes;
2225 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002226 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002227 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002228 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002229
Brian Norris7854d3f2011-06-23 14:12:08 -07002230 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002231 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2232 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002233
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002234 for (i = 0; i < chip->ecc.total; i++)
2235 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002236
Josh Wufdbad98d2012-06-25 18:07:45 +08002237 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002238}
2239
2240/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002241 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002242 * @mtd: mtd info structure
2243 * @chip: nand chip info structure
2244 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002245 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002246 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002247static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002248 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002249{
2250 int i, eccsize = chip->ecc.size;
2251 int eccbytes = chip->ecc.bytes;
2252 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002253 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002254 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002255 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002256
2257 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2258 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002259 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002260 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2261 }
2262
2263 for (i = 0; i < chip->ecc.total; i++)
2264 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2265
2266 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002267
2268 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002269}
2270
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302271
2272/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002273 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302274 * @mtd: mtd info structure
2275 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002276 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302277 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002278 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302279 * @oob_required: must write chip->oob_poi to OOB
2280 */
2281static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2282 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002283 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302284 int oob_required)
2285{
2286 uint8_t *oob_buf = chip->oob_poi;
2287 uint8_t *ecc_calc = chip->buffers->ecccalc;
2288 int ecc_size = chip->ecc.size;
2289 int ecc_bytes = chip->ecc.bytes;
2290 int ecc_steps = chip->ecc.steps;
2291 uint32_t *eccpos = chip->ecc.layout->eccpos;
2292 uint32_t start_step = offset / ecc_size;
2293 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2294 int oob_bytes = mtd->oobsize / ecc_steps;
2295 int step, i;
2296
2297 for (step = 0; step < ecc_steps; step++) {
2298 /* configure controller for WRITE access */
2299 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2300
2301 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002302 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302303
2304 /* mask ECC of un-touched subpages by padding 0xFF */
2305 if ((step < start_step) || (step > end_step))
2306 memset(ecc_calc, 0xff, ecc_bytes);
2307 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002308 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302309
2310 /* mask OOB of un-touched subpages by padding 0xFF */
2311 /* if oob_required, preserve OOB metadata of written subpage */
2312 if (!oob_required || (step < start_step) || (step > end_step))
2313 memset(oob_buf, 0xff, oob_bytes);
2314
Brian Norrisd6a950802013-08-08 17:16:36 -07002315 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302316 ecc_calc += ecc_bytes;
2317 oob_buf += oob_bytes;
2318 }
2319
2320 /* copy calculated ECC for whole page to chip->buffer->oob */
2321 /* this include masked-value(0xFF) for unwritten subpages */
2322 ecc_calc = chip->buffers->ecccalc;
2323 for (i = 0; i < chip->ecc.total; i++)
2324 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2325
2326 /* write OOB buffer to NAND device */
2327 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2328
2329 return 0;
2330}
2331
2332
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002333/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002334 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002335 * @mtd: mtd info structure
2336 * @chip: nand chip info structure
2337 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002338 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002339 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002340 * The hw generator calculates the error syndrome automatically. Therefore we
2341 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002342 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002343static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002344 struct nand_chip *chip,
2345 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002346{
2347 int i, eccsize = chip->ecc.size;
2348 int eccbytes = chip->ecc.bytes;
2349 int eccsteps = chip->ecc.steps;
2350 const uint8_t *p = buf;
2351 uint8_t *oob = chip->oob_poi;
2352
2353 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2354
2355 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2356 chip->write_buf(mtd, p, eccsize);
2357
2358 if (chip->ecc.prepad) {
2359 chip->write_buf(mtd, oob, chip->ecc.prepad);
2360 oob += chip->ecc.prepad;
2361 }
2362
2363 chip->ecc.calculate(mtd, p, oob);
2364 chip->write_buf(mtd, oob, eccbytes);
2365 oob += eccbytes;
2366
2367 if (chip->ecc.postpad) {
2368 chip->write_buf(mtd, oob, chip->ecc.postpad);
2369 oob += chip->ecc.postpad;
2370 }
2371 }
2372
2373 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002374 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002375 if (i)
2376 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002377
2378 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002379}
2380
2381/**
David Woodhouse956e9442006-09-25 17:12:39 +01002382 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002383 * @mtd: MTD device structure
2384 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302385 * @offset: address offset within the page
2386 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002387 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002388 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002389 * @page: page number to write
2390 * @cached: cached programming
2391 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002392 */
2393static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302394 uint32_t offset, int data_len, const uint8_t *buf,
2395 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002396{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302397 int status, subpage;
2398
2399 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2400 chip->ecc.write_subpage)
2401 subpage = offset || (data_len < mtd->writesize);
2402 else
2403 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002404
2405 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2406
David Woodhouse956e9442006-09-25 17:12:39 +01002407 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302408 status = chip->ecc.write_page_raw(mtd, chip, buf,
2409 oob_required);
2410 else if (subpage)
2411 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2412 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002413 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002414 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2415
2416 if (status < 0)
2417 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002418
2419 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002420 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002421 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002422 */
2423 cached = 0;
2424
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002425 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002426
2427 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002428 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002429 /*
2430 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002431 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002432 */
2433 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2434 status = chip->errstat(mtd, chip, FL_WRITING, status,
2435 page);
2436
2437 if (status & NAND_STATUS_FAIL)
2438 return -EIO;
2439 } else {
2440 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002441 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442 }
2443
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002444 return 0;
2445}
2446
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002447/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002448 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002449 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002450 * @oob: oob data buffer
2451 * @len: oob data write length
2452 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002453 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002454static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2455 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002456{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002457 struct nand_chip *chip = mtd->priv;
2458
2459 /*
2460 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2461 * data from a previous OOB read.
2462 */
2463 memset(chip->oob_poi, 0xff, mtd->oobsize);
2464
Florian Fainellif8ac0412010-09-07 13:23:43 +02002465 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002466
Brian Norris0612b9d2011-08-30 18:45:40 -07002467 case MTD_OPS_PLACE_OOB:
2468 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002469 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2470 return oob + len;
2471
Brian Norris0612b9d2011-08-30 18:45:40 -07002472 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002473 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002474 uint32_t boffs = 0, woffs = ops->ooboffs;
2475 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002476
Florian Fainellif8ac0412010-09-07 13:23:43 +02002477 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002478 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002479 if (unlikely(woffs)) {
2480 if (woffs >= free->length) {
2481 woffs -= free->length;
2482 continue;
2483 }
2484 boffs = free->offset + woffs;
2485 bytes = min_t(size_t, len,
2486 (free->length - woffs));
2487 woffs = 0;
2488 } else {
2489 bytes = min_t(size_t, len, free->length);
2490 boffs = free->offset;
2491 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002492 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002493 oob += bytes;
2494 }
2495 return oob;
2496 }
2497 default:
2498 BUG();
2499 }
2500 return NULL;
2501}
2502
Florian Fainellif8ac0412010-09-07 13:23:43 +02002503#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002504
2505/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002506 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002507 * @mtd: MTD device structure
2508 * @to: offset to write to
2509 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002510 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002511 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002512 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002513static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2514 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002515{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002516 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002517 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002518 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002519
2520 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002521 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002522 mtd->oobavail : mtd->oobsize;
2523
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002524 uint8_t *oob = ops->oobbuf;
2525 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302526 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002527 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002528
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002529 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002530 if (!writelen)
2531 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002532
Brian Norris8b6e50c2011-05-25 14:59:01 -07002533 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002534 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002535 pr_notice("%s: attempt to write non page aligned data\n",
2536 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002537 return -EINVAL;
2538 }
2539
Thomas Gleixner29072b92006-09-28 15:38:36 +02002540 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002541
Thomas Gleixner6a930962006-06-28 00:11:45 +02002542 chipnr = (int)(to >> chip->chip_shift);
2543 chip->select_chip(mtd, chipnr);
2544
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002545 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002546 if (nand_check_wp(mtd)) {
2547 ret = -EIO;
2548 goto err_out;
2549 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002550
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002551 realpage = (int)(to >> chip->page_shift);
2552 page = realpage & chip->pagemask;
2553 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2554
2555 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002556 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2557 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002558 chip->pagebuf = -1;
2559
Maxim Levitsky782ce792010-02-22 20:39:36 +02002560 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002561 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2562 ret = -EINVAL;
2563 goto err_out;
2564 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002565
Florian Fainellif8ac0412010-09-07 13:23:43 +02002566 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002567 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002568 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002569 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002570 int use_bufpoi;
2571 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002572
Kamal Dasu66507c72014-05-01 20:51:19 -04002573 if (part_pagewr)
2574 use_bufpoi = 1;
2575 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2576 use_bufpoi = !virt_addr_valid(buf);
2577 else
2578 use_bufpoi = 0;
2579
2580 /* Partial page write?, or need to use bounce buffer */
2581 if (use_bufpoi) {
2582 pr_debug("%s: using write bounce buffer for buf@%p\n",
2583 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002584 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002585 if (part_pagewr)
2586 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002587 chip->pagebuf = -1;
2588 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2589 memcpy(&chip->buffers->databuf[column], buf, bytes);
2590 wbuf = chip->buffers->databuf;
2591 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002592
Maxim Levitsky782ce792010-02-22 20:39:36 +02002593 if (unlikely(oob)) {
2594 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002595 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002596 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002597 } else {
2598 /* We still need to erase leftover OOB data */
2599 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002600 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302601 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2602 oob_required, page, cached,
2603 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002604 if (ret)
2605 break;
2606
2607 writelen -= bytes;
2608 if (!writelen)
2609 break;
2610
Thomas Gleixner29072b92006-09-28 15:38:36 +02002611 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002612 buf += bytes;
2613 realpage++;
2614
2615 page = realpage & chip->pagemask;
2616 /* Check, if we cross a chip boundary */
2617 if (!page) {
2618 chipnr++;
2619 chip->select_chip(mtd, -1);
2620 chip->select_chip(mtd, chipnr);
2621 }
2622 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002623
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002624 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002625 if (unlikely(oob))
2626 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002627
2628err_out:
2629 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002630 return ret;
2631}
2632
2633/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002634 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002635 * @mtd: MTD device structure
2636 * @to: offset to write to
2637 * @len: number of bytes to write
2638 * @retlen: pointer to variable to store the number of written bytes
2639 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002640 *
2641 * NAND write with ECC. Used when performing writes in interrupt context, this
2642 * may for example be called by mtdoops when writing an oops while in panic.
2643 */
2644static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2645 size_t *retlen, const uint8_t *buf)
2646{
2647 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002648 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002649 int ret;
2650
Brian Norris8b6e50c2011-05-25 14:59:01 -07002651 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002652 panic_nand_wait(mtd, chip, 400);
2653
Brian Norris8b6e50c2011-05-25 14:59:01 -07002654 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002655 panic_nand_get_device(chip, mtd, FL_WRITING);
2656
Brian Norris0ec56dc2015-02-28 02:02:30 -08002657 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002658 ops.len = len;
2659 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002660 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002661
Brian Norris4a89ff82011-08-30 18:45:45 -07002662 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002663
Brian Norris4a89ff82011-08-30 18:45:45 -07002664 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002665 return ret;
2666}
2667
2668/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002669 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002670 * @mtd: MTD device structure
2671 * @to: offset to write to
2672 * @len: number of bytes to write
2673 * @retlen: pointer to variable to store the number of written bytes
2674 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002676 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002678static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002679 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Brian Norris4a89ff82011-08-30 18:45:45 -07002681 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002682 int ret;
2683
Huang Shijie6a8214a2012-11-19 14:43:30 +08002684 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002685 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002686 ops.len = len;
2687 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002688 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002689 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002690 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002691 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002692 return ret;
2693}
2694
2695/**
2696 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002697 * @mtd: MTD device structure
2698 * @to: offset to write to
2699 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002700 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002701 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002702 */
2703static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2704 struct mtd_oob_ops *ops)
2705{
Adrian Hunter03736152007-01-31 17:58:29 +02002706 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002707 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Brian Norris289c0522011-07-19 10:06:09 -07002709 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302710 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Brian Norris0612b9d2011-08-30 18:45:40 -07002712 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002713 len = chip->ecc.layout->oobavail;
2714 else
2715 len = mtd->oobsize;
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002718 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002719 pr_debug("%s: attempt to write past end of page\n",
2720 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return -EINVAL;
2722 }
2723
Adrian Hunter03736152007-01-31 17:58:29 +02002724 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002725 pr_debug("%s: attempt to start write outside oob\n",
2726 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002727 return -EINVAL;
2728 }
2729
Jason Liu775adc32011-02-25 13:06:18 +08002730 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002731 if (unlikely(to >= mtd->size ||
2732 ops->ooboffs + ops->ooblen >
2733 ((mtd->size >> chip->page_shift) -
2734 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002735 pr_debug("%s: attempt to write beyond end of device\n",
2736 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002737 return -EINVAL;
2738 }
2739
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002740 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002741 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002743 /* Shift to get page */
2744 page = (int)(to >> chip->page_shift);
2745
2746 /*
2747 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2748 * of my DiskOnChip 2000 test units) will clear the whole data page too
2749 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2750 * it in the doc2000 driver in August 1999. dwmw2.
2751 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002752 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002755 if (nand_check_wp(mtd)) {
2756 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002757 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002758 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002761 if (page == chip->pagebuf)
2762 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002764 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002765
Brian Norris0612b9d2011-08-30 18:45:40 -07002766 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002767 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2768 else
2769 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002770
Huang Shijieb0bb6902012-11-19 14:43:29 +08002771 chip->select_chip(mtd, -1);
2772
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002773 if (status)
2774 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Vitaly Wool70145682006-11-03 18:20:38 +03002776 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002778 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002781/**
2782 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002783 * @mtd: MTD device structure
2784 * @to: offset to write to
2785 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002786 */
2787static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2788 struct mtd_oob_ops *ops)
2789{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002790 int ret = -ENOTSUPP;
2791
2792 ops->retlen = 0;
2793
2794 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002795 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002796 pr_debug("%s: attempt to write beyond end of device\n",
2797 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002798 return -EINVAL;
2799 }
2800
Huang Shijie6a8214a2012-11-19 14:43:30 +08002801 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002802
Florian Fainellif8ac0412010-09-07 13:23:43 +02002803 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002804 case MTD_OPS_PLACE_OOB:
2805 case MTD_OPS_AUTO_OOB:
2806 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002807 break;
2808
2809 default:
2810 goto out;
2811 }
2812
2813 if (!ops->datbuf)
2814 ret = nand_do_write_oob(mtd, to, ops);
2815 else
2816 ret = nand_do_write_ops(mtd, to, ops);
2817
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002818out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002819 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return ret;
2821}
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/**
Brian Norris49c50b92014-05-06 16:02:19 -07002824 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002825 * @mtd: MTD device structure
2826 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 *
Brian Norris49c50b92014-05-06 16:02:19 -07002828 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 */
Brian Norris49c50b92014-05-06 16:02:19 -07002830static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002832 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002834 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2835 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002836
2837 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002842 * @mtd: MTD device structure
2843 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002845 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002847static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
David Woodhousee0c7d762006-05-13 18:07:53 +01002849 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002853 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002854 * @mtd: MTD device structure
2855 * @instr: erase instruction
2856 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002858 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002860int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2861 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Adrian Hunter69423d92008-12-10 13:37:21 +00002863 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002864 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002865 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Brian Norris289c0522011-07-19 10:06:09 -07002867 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2868 __func__, (unsigned long long)instr->addr,
2869 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302871 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002875 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002878 page = (int)(instr->addr >> chip->page_shift);
2879 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002882 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002885 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 /* Check, if it is write protected */
2888 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002889 pr_debug("%s: device is write protected!\n",
2890 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 instr->state = MTD_ERASE_FAILED;
2892 goto erase_exit;
2893 }
2894
2895 /* Loop through the pages */
2896 len = instr->len;
2897
2898 instr->state = MTD_ERASING;
2899
2900 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002901 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002902 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2903 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002904 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2905 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 instr->state = MTD_ERASE_FAILED;
2907 goto erase_exit;
2908 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002909
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002910 /*
2911 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002912 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002913 */
2914 if (page <= chip->pagebuf && chip->pagebuf <
2915 (page + pages_per_block))
2916 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Brian Norris49c50b92014-05-06 16:02:19 -07002918 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002920 /*
2921 * See if operation failed and additional status checks are
2922 * available
2923 */
2924 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2925 status = chip->errstat(mtd, chip, FL_ERASING,
2926 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002929 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002930 pr_debug("%s: failed erase, page 0x%08x\n",
2931 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002933 instr->fail_addr =
2934 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 goto erase_exit;
2936 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002939 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 page += pages_per_block;
2941
2942 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002943 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002945 chip->select_chip(mtd, -1);
2946 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948 }
2949 instr->state = MTD_ERASE_DONE;
2950
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002951erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002956 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 nand_release_device(mtd);
2958
David Woodhouse49defc02007-10-06 15:01:59 -04002959 /* Do call back function */
2960 if (!ret)
2961 mtd_erase_callback(instr);
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 /* Return more or less happy */
2964 return ret;
2965}
2966
2967/**
2968 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002969 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002973static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Brian Norris289c0522011-07-19 10:06:09 -07002975 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002978 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002980 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002984 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002985 * @mtd: MTD device structure
2986 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002988static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002990 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002994 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002995 * @mtd: MTD device structure
2996 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002998static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 int ret;
3001
Florian Fainellif8ac0412010-09-07 13:23:43 +02003002 ret = nand_block_isbad(mtd, ofs);
3003 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003004 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (ret > 0)
3006 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003007 return ret;
3008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Brian Norris5a0edb22013-07-30 17:52:58 -07003010 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
3013/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003014 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3015 * @mtd: MTD device structure
3016 * @chip: nand chip info structure
3017 * @addr: feature address.
3018 * @subfeature_param: the subfeature parameters, a four bytes array.
3019 */
3020static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3021 int addr, uint8_t *subfeature_param)
3022{
3023 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003024 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003025
David Mosbergerd914c932013-05-29 15:30:13 +03003026 if (!chip->onfi_version ||
3027 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3028 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003029 return -EINVAL;
3030
3031 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003032 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3033 chip->write_byte(mtd, subfeature_param[i]);
3034
Huang Shijie7db03ec2012-09-13 14:57:52 +08003035 status = chip->waitfunc(mtd, chip);
3036 if (status & NAND_STATUS_FAIL)
3037 return -EIO;
3038 return 0;
3039}
3040
3041/**
3042 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3043 * @mtd: MTD device structure
3044 * @chip: nand chip info structure
3045 * @addr: feature address.
3046 * @subfeature_param: the subfeature parameters, a four bytes array.
3047 */
3048static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3049 int addr, uint8_t *subfeature_param)
3050{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003051 int i;
3052
David Mosbergerd914c932013-05-29 15:30:13 +03003053 if (!chip->onfi_version ||
3054 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3055 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003056 return -EINVAL;
3057
Huang Shijie7db03ec2012-09-13 14:57:52 +08003058 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003059 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3060 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003061 return 0;
3062}
3063
3064/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003065 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003066 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003067 */
3068static int nand_suspend(struct mtd_info *mtd)
3069{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003070 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003071}
3072
3073/**
3074 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003075 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003076 */
3077static void nand_resume(struct mtd_info *mtd)
3078{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003079 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01003080
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003081 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003082 nand_release_device(mtd);
3083 else
Brian Norrisd0370212011-07-19 10:06:08 -07003084 pr_err("%s called for a chip which is not in suspended state\n",
3085 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003086}
3087
Scott Branden72ea4032014-11-20 11:18:05 -08003088/**
3089 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3090 * prevent further operations
3091 * @mtd: MTD device structure
3092 */
3093static void nand_shutdown(struct mtd_info *mtd)
3094{
3095 nand_get_device(mtd, FL_SHUTDOWN);
3096}
3097
Brian Norris8b6e50c2011-05-25 14:59:01 -07003098/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003099static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003102 if (!chip->chip_delay)
3103 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003106 if (chip->cmdfunc == NULL)
3107 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003110 if (chip->waitfunc == NULL)
3111 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003113 if (!chip->select_chip)
3114 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003115
Huang Shijie4204ccc2013-08-16 10:10:07 +08003116 /* set for ONFI nand */
3117 if (!chip->onfi_set_features)
3118 chip->onfi_set_features = nand_onfi_set_features;
3119 if (!chip->onfi_get_features)
3120 chip->onfi_get_features = nand_onfi_get_features;
3121
Brian Norris68e80782013-07-18 01:17:02 -07003122 /* If called twice, pointers that depend on busw may need to be reset */
3123 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003124 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3125 if (!chip->read_word)
3126 chip->read_word = nand_read_word;
3127 if (!chip->block_bad)
3128 chip->block_bad = nand_block_bad;
3129 if (!chip->block_markbad)
3130 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003131 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003132 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003133 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3134 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003135 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003136 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003137 if (!chip->scan_bbt)
3138 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003139
3140 if (!chip->controller) {
3141 chip->controller = &chip->hwcontrol;
3142 spin_lock_init(&chip->controller->lock);
3143 init_waitqueue_head(&chip->controller->wq);
3144 }
3145
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003146}
3147
Brian Norris8b6e50c2011-05-25 14:59:01 -07003148/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003149static void sanitize_string(uint8_t *s, size_t len)
3150{
3151 ssize_t i;
3152
Brian Norris8b6e50c2011-05-25 14:59:01 -07003153 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003154 s[len - 1] = 0;
3155
Brian Norris8b6e50c2011-05-25 14:59:01 -07003156 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003157 for (i = 0; i < len - 1; i++) {
3158 if (s[i] < ' ' || s[i] > 127)
3159 s[i] = '?';
3160 }
3161
Brian Norris8b6e50c2011-05-25 14:59:01 -07003162 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003163 strim(s);
3164}
3165
3166static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3167{
3168 int i;
3169 while (len--) {
3170 crc ^= *p++ << 8;
3171 for (i = 0; i < 8; i++)
3172 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3173 }
3174
3175 return crc;
3176}
3177
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003178/* Parse the Extended Parameter Page. */
3179static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3180 struct nand_chip *chip, struct nand_onfi_params *p)
3181{
3182 struct onfi_ext_param_page *ep;
3183 struct onfi_ext_section *s;
3184 struct onfi_ext_ecc_info *ecc;
3185 uint8_t *cursor;
3186 int ret = -EINVAL;
3187 int len;
3188 int i;
3189
3190 len = le16_to_cpu(p->ext_param_page_length) * 16;
3191 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003192 if (!ep)
3193 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003194
3195 /* Send our own NAND_CMD_PARAM. */
3196 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3197
3198 /* Use the Change Read Column command to skip the ONFI param pages. */
3199 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3200 sizeof(*p) * p->num_of_param_pages , -1);
3201
3202 /* Read out the Extended Parameter Page. */
3203 chip->read_buf(mtd, (uint8_t *)ep, len);
3204 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3205 != le16_to_cpu(ep->crc))) {
3206 pr_debug("fail in the CRC.\n");
3207 goto ext_out;
3208 }
3209
3210 /*
3211 * Check the signature.
3212 * Do not strictly follow the ONFI spec, maybe changed in future.
3213 */
3214 if (strncmp(ep->sig, "EPPS", 4)) {
3215 pr_debug("The signature is invalid.\n");
3216 goto ext_out;
3217 }
3218
3219 /* find the ECC section. */
3220 cursor = (uint8_t *)(ep + 1);
3221 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3222 s = ep->sections + i;
3223 if (s->type == ONFI_SECTION_TYPE_2)
3224 break;
3225 cursor += s->length * 16;
3226 }
3227 if (i == ONFI_EXT_SECTION_MAX) {
3228 pr_debug("We can not find the ECC section.\n");
3229 goto ext_out;
3230 }
3231
3232 /* get the info we want. */
3233 ecc = (struct onfi_ext_ecc_info *)cursor;
3234
Brian Norris4ae7d222013-09-16 18:20:21 -07003235 if (!ecc->codeword_size) {
3236 pr_debug("Invalid codeword size\n");
3237 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003238 }
3239
Brian Norris4ae7d222013-09-16 18:20:21 -07003240 chip->ecc_strength_ds = ecc->ecc_bits;
3241 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003242 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003243
3244ext_out:
3245 kfree(ep);
3246 return ret;
3247}
3248
Brian Norris8429bb32013-12-03 15:51:09 -08003249static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3250{
3251 struct nand_chip *chip = mtd->priv;
3252 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3253
3254 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3255 feature);
3256}
3257
3258/*
3259 * Configure chip properties from Micron vendor-specific ONFI table
3260 */
3261static void nand_onfi_detect_micron(struct nand_chip *chip,
3262 struct nand_onfi_params *p)
3263{
3264 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3265
3266 if (le16_to_cpu(p->vendor_revision) < 1)
3267 return;
3268
3269 chip->read_retries = micron->read_retry_options;
3270 chip->setup_read_retry = nand_setup_read_retry_micron;
3271}
3272
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003273/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003274 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003275 */
3276static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003277 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003278{
3279 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003280 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003281 int val;
3282
Brian Norris7854d3f2011-06-23 14:12:08 -07003283 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003284 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3285 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3286 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3287 return 0;
3288
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003289 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3290 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003291 for (j = 0; j < sizeof(*p); j++)
3292 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003293 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3294 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003295 break;
3296 }
3297 }
3298
Brian Norrisc7f23a72013-08-13 10:51:55 -07003299 if (i == 3) {
3300 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003301 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003302 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003303
Brian Norris8b6e50c2011-05-25 14:59:01 -07003304 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003305 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003306 if (val & (1 << 5))
3307 chip->onfi_version = 23;
3308 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003309 chip->onfi_version = 22;
3310 else if (val & (1 << 3))
3311 chip->onfi_version = 21;
3312 else if (val & (1 << 2))
3313 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003314 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003315 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003316
3317 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003318 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003319 return 0;
3320 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003321
3322 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3323 sanitize_string(p->model, sizeof(p->model));
3324 if (!mtd->name)
3325 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003326
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003327 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003328
3329 /*
3330 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3331 * (don't ask me who thought of this...). MTD assumes that these
3332 * dimensions will be power-of-2, so just truncate the remaining area.
3333 */
3334 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3335 mtd->erasesize *= mtd->writesize;
3336
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003337 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003338
3339 /* See erasesize comment */
3340 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003341 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003342 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003343
3344 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003345 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003346 else
3347 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003348
Huang Shijie10c86ba2013-05-17 11:17:26 +08003349 if (p->ecc_bits != 0xff) {
3350 chip->ecc_strength_ds = p->ecc_bits;
3351 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003352 } else if (chip->onfi_version >= 21 &&
3353 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3354
3355 /*
3356 * The nand_flash_detect_ext_param_page() uses the
3357 * Change Read Column command which maybe not supported
3358 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3359 * now. We do not replace user supplied command function.
3360 */
3361 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3362 chip->cmdfunc = nand_command_lp;
3363
3364 /* The Extended Parameter Page is supported since ONFI 2.1. */
3365 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003366 pr_warn("Failed to detect ONFI extended param page\n");
3367 } else {
3368 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003369 }
3370
Brian Norris8429bb32013-12-03 15:51:09 -08003371 if (p->jedec_id == NAND_MFR_MICRON)
3372 nand_onfi_detect_micron(chip, p);
3373
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003374 return 1;
3375}
3376
3377/*
Huang Shijie91361812014-02-21 13:39:40 +08003378 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3379 */
3380static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3381 int *busw)
3382{
3383 struct nand_jedec_params *p = &chip->jedec_params;
3384 struct jedec_ecc_info *ecc;
3385 int val;
3386 int i, j;
3387
3388 /* Try JEDEC for unknown chip or LP */
3389 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3390 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3391 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3392 chip->read_byte(mtd) != 'C')
3393 return 0;
3394
3395 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3396 for (i = 0; i < 3; i++) {
3397 for (j = 0; j < sizeof(*p); j++)
3398 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3399
3400 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3401 le16_to_cpu(p->crc))
3402 break;
3403 }
3404
3405 if (i == 3) {
3406 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3407 return 0;
3408 }
3409
3410 /* Check version */
3411 val = le16_to_cpu(p->revision);
3412 if (val & (1 << 2))
3413 chip->jedec_version = 10;
3414 else if (val & (1 << 1))
3415 chip->jedec_version = 1; /* vendor specific version */
3416
3417 if (!chip->jedec_version) {
3418 pr_info("unsupported JEDEC version: %d\n", val);
3419 return 0;
3420 }
3421
3422 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3423 sanitize_string(p->model, sizeof(p->model));
3424 if (!mtd->name)
3425 mtd->name = p->model;
3426
3427 mtd->writesize = le32_to_cpu(p->byte_per_page);
3428
3429 /* Please reference to the comment for nand_flash_detect_onfi. */
3430 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3431 mtd->erasesize *= mtd->writesize;
3432
3433 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3434
3435 /* Please reference to the comment for nand_flash_detect_onfi. */
3436 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3437 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3438 chip->bits_per_cell = p->bits_per_cell;
3439
3440 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3441 *busw = NAND_BUSWIDTH_16;
3442 else
3443 *busw = 0;
3444
3445 /* ECC info */
3446 ecc = &p->ecc_info[0];
3447
3448 if (ecc->codeword_size >= 9) {
3449 chip->ecc_strength_ds = ecc->ecc_bits;
3450 chip->ecc_step_ds = 1 << ecc->codeword_size;
3451 } else {
3452 pr_warn("Invalid codeword size\n");
3453 }
3454
3455 return 1;
3456}
3457
3458/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003459 * nand_id_has_period - Check if an ID string has a given wraparound period
3460 * @id_data: the ID string
3461 * @arrlen: the length of the @id_data array
3462 * @period: the period of repitition
3463 *
3464 * Check if an ID string is repeated within a given sequence of bytes at
3465 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003466 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003467 * if the repetition has a period of @period; otherwise, returns zero.
3468 */
3469static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3470{
3471 int i, j;
3472 for (i = 0; i < period; i++)
3473 for (j = i + period; j < arrlen; j += period)
3474 if (id_data[i] != id_data[j])
3475 return 0;
3476 return 1;
3477}
3478
3479/*
3480 * nand_id_len - Get the length of an ID string returned by CMD_READID
3481 * @id_data: the ID string
3482 * @arrlen: the length of the @id_data array
3483
3484 * Returns the length of the ID string, according to known wraparound/trailing
3485 * zero patterns. If no pattern exists, returns the length of the array.
3486 */
3487static int nand_id_len(u8 *id_data, int arrlen)
3488{
3489 int last_nonzero, period;
3490
3491 /* Find last non-zero byte */
3492 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3493 if (id_data[last_nonzero])
3494 break;
3495
3496 /* All zeros */
3497 if (last_nonzero < 0)
3498 return 0;
3499
3500 /* Calculate wraparound period */
3501 for (period = 1; period < arrlen; period++)
3502 if (nand_id_has_period(id_data, arrlen, period))
3503 break;
3504
3505 /* There's a repeated pattern */
3506 if (period < arrlen)
3507 return period;
3508
3509 /* There are trailing zeros */
3510 if (last_nonzero < arrlen - 1)
3511 return last_nonzero + 1;
3512
3513 /* No pattern detected */
3514 return arrlen;
3515}
3516
Huang Shijie7db906b2013-09-25 14:58:11 +08003517/* Extract the bits of per cell from the 3rd byte of the extended ID */
3518static int nand_get_bits_per_cell(u8 cellinfo)
3519{
3520 int bits;
3521
3522 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3523 bits >>= NAND_CI_CELLTYPE_SHIFT;
3524 return bits + 1;
3525}
3526
Brian Norrise3b88bd2012-09-24 20:40:52 -07003527/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003528 * Many new NAND share similar device ID codes, which represent the size of the
3529 * chip. The rest of the parameters must be decoded according to generic or
3530 * manufacturer-specific "extended ID" decoding patterns.
3531 */
3532static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3533 u8 id_data[8], int *busw)
3534{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003535 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003536 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003537 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003538 /* The 4th id byte is the important one */
3539 extid = id_data[3];
3540
Brian Norrise3b88bd2012-09-24 20:40:52 -07003541 id_len = nand_id_len(id_data, 8);
3542
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003543 /*
3544 * Field definitions are in the following datasheets:
3545 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003546 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003547 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003548 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003549 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3550 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003551 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003552 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003553 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003554 /* Calc pagesize */
3555 mtd->writesize = 2048 << (extid & 0x03);
3556 extid >>= 2;
3557 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003558 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003559 case 1:
3560 mtd->oobsize = 128;
3561 break;
3562 case 2:
3563 mtd->oobsize = 218;
3564 break;
3565 case 3:
3566 mtd->oobsize = 400;
3567 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003568 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003569 mtd->oobsize = 436;
3570 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003571 case 5:
3572 mtd->oobsize = 512;
3573 break;
3574 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003575 mtd->oobsize = 640;
3576 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003577 case 7:
3578 default: /* Other cases are "reserved" (unknown) */
3579 mtd->oobsize = 1024;
3580 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003581 }
3582 extid >>= 2;
3583 /* Calc blocksize */
3584 mtd->erasesize = (128 * 1024) <<
3585 (((extid >> 1) & 0x04) | (extid & 0x03));
3586 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003587 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003588 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003589 unsigned int tmp;
3590
3591 /* Calc pagesize */
3592 mtd->writesize = 2048 << (extid & 0x03);
3593 extid >>= 2;
3594 /* Calc oobsize */
3595 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3596 case 0:
3597 mtd->oobsize = 128;
3598 break;
3599 case 1:
3600 mtd->oobsize = 224;
3601 break;
3602 case 2:
3603 mtd->oobsize = 448;
3604 break;
3605 case 3:
3606 mtd->oobsize = 64;
3607 break;
3608 case 4:
3609 mtd->oobsize = 32;
3610 break;
3611 case 5:
3612 mtd->oobsize = 16;
3613 break;
3614 default:
3615 mtd->oobsize = 640;
3616 break;
3617 }
3618 extid >>= 2;
3619 /* Calc blocksize */
3620 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3621 if (tmp < 0x03)
3622 mtd->erasesize = (128 * 1024) << tmp;
3623 else if (tmp == 0x03)
3624 mtd->erasesize = 768 * 1024;
3625 else
3626 mtd->erasesize = (64 * 1024) << tmp;
3627 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003628 } else {
3629 /* Calc pagesize */
3630 mtd->writesize = 1024 << (extid & 0x03);
3631 extid >>= 2;
3632 /* Calc oobsize */
3633 mtd->oobsize = (8 << (extid & 0x01)) *
3634 (mtd->writesize >> 9);
3635 extid >>= 2;
3636 /* Calc blocksize. Blocksize is multiples of 64KiB */
3637 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3638 extid >>= 2;
3639 /* Get buswidth information */
3640 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003641
3642 /*
3643 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3644 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3645 * follows:
3646 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3647 * 110b -> 24nm
3648 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3649 */
3650 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003651 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003652 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3653 !(id_data[4] & 0x80) /* !BENAND */) {
3654 mtd->oobsize = 32 * mtd->writesize >> 9;
3655 }
3656
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003657 }
3658}
3659
3660/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003661 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3662 * decodes a matching ID table entry and assigns the MTD size parameters for
3663 * the chip.
3664 */
3665static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3666 struct nand_flash_dev *type, u8 id_data[8],
3667 int *busw)
3668{
3669 int maf_id = id_data[0];
3670
3671 mtd->erasesize = type->erasesize;
3672 mtd->writesize = type->pagesize;
3673 mtd->oobsize = mtd->writesize / 32;
3674 *busw = type->options & NAND_BUSWIDTH_16;
3675
Huang Shijie1c195e92013-09-25 14:58:12 +08003676 /* All legacy ID NAND are small-page, SLC */
3677 chip->bits_per_cell = 1;
3678
Brian Norrisf23a4812012-09-24 20:40:51 -07003679 /*
3680 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3681 * some Spansion chips have erasesize that conflicts with size
3682 * listed in nand_ids table.
3683 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3684 */
3685 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3686 && id_data[6] == 0x00 && id_data[7] == 0x00
3687 && mtd->writesize == 512) {
3688 mtd->erasesize = 128 * 1024;
3689 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3690 }
3691}
3692
3693/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003694 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3695 * heuristic patterns using various detected parameters (e.g., manufacturer,
3696 * page size, cell-type information).
3697 */
3698static void nand_decode_bbm_options(struct mtd_info *mtd,
3699 struct nand_chip *chip, u8 id_data[8])
3700{
3701 int maf_id = id_data[0];
3702
3703 /* Set the bad block position */
3704 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3705 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3706 else
3707 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3708
3709 /*
3710 * Bad block marker is stored in the last page of each block on Samsung
3711 * and Hynix MLC devices; stored in first two pages of each block on
3712 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3713 * AMD/Spansion, and Macronix. All others scan only the first page.
3714 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003715 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003716 (maf_id == NAND_MFR_SAMSUNG ||
3717 maf_id == NAND_MFR_HYNIX))
3718 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003719 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003720 (maf_id == NAND_MFR_SAMSUNG ||
3721 maf_id == NAND_MFR_HYNIX ||
3722 maf_id == NAND_MFR_TOSHIBA ||
3723 maf_id == NAND_MFR_AMD ||
3724 maf_id == NAND_MFR_MACRONIX)) ||
3725 (mtd->writesize == 2048 &&
3726 maf_id == NAND_MFR_MICRON))
3727 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3728}
3729
Huang Shijieec6e87e2013-03-15 11:01:00 +08003730static inline bool is_full_id_nand(struct nand_flash_dev *type)
3731{
3732 return type->id_len;
3733}
3734
3735static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3736 struct nand_flash_dev *type, u8 *id_data, int *busw)
3737{
3738 if (!strncmp(type->id, id_data, type->id_len)) {
3739 mtd->writesize = type->pagesize;
3740 mtd->erasesize = type->erasesize;
3741 mtd->oobsize = type->oobsize;
3742
Huang Shijie7db906b2013-09-25 14:58:11 +08003743 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003744 chip->chipsize = (uint64_t)type->chipsize << 20;
3745 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003746 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3747 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003748 chip->onfi_timing_mode_default =
3749 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003750
3751 *busw = type->options & NAND_BUSWIDTH_16;
3752
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003753 if (!mtd->name)
3754 mtd->name = type->name;
3755
Huang Shijieec6e87e2013-03-15 11:01:00 +08003756 return true;
3757 }
3758 return false;
3759}
3760
Brian Norris7e74c2d2012-09-24 20:40:49 -07003761/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003762 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003763 */
3764static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003765 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003766 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003767 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003768{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003769 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003770 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003771 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003774 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Karl Beldanef89a882008-09-15 14:37:29 +02003776 /*
3777 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003778 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003779 */
3780 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003783 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
3785 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003786 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003787 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Brian Norris8b6e50c2011-05-25 14:59:01 -07003789 /*
3790 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003791 * interface concerns can cause random data which looks like a
3792 * possibly credible NAND flash to appear. If the two results do
3793 * not match, ignore the device completely.
3794 */
3795
3796 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3797
Brian Norris4aef9b72012-09-24 20:40:48 -07003798 /* Read entire ID string */
3799 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003800 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003801
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003802 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003803 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003804 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003805 return ERR_PTR(-ENODEV);
3806 }
3807
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003808 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003809 type = nand_flash_ids;
3810
Huang Shijieec6e87e2013-03-15 11:01:00 +08003811 for (; type->name != NULL; type++) {
3812 if (is_full_id_nand(type)) {
3813 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3814 goto ident_done;
3815 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003816 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003817 }
3818 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003819
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003820 chip->onfi_version = 0;
3821 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003822 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003823 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003824 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003825
3826 /* Check if the chip is JEDEC compliant */
3827 if (nand_flash_detect_jedec(mtd, chip, &busw))
3828 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003829 }
3830
David Woodhouse5e81e882010-02-26 18:32:56 +00003831 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003832 return ERR_PTR(-ENODEV);
3833
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003834 if (!mtd->name)
3835 mtd->name = type->name;
3836
Adrian Hunter69423d92008-12-10 13:37:21 +00003837 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003838
Huang Shijie12a40a52010-09-27 10:43:53 +08003839 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003840 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003841 busw = chip->init_size(mtd, chip, id_data);
3842 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003843 /* Decode parameters from extended ID */
3844 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003845 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003846 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003847 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003848 /* Get chip options */
3849 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003850
Brian Norris8b6e50c2011-05-25 14:59:01 -07003851 /*
3852 * Check if chip is not a Samsung device. Do not clear the
3853 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003854 */
3855 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3856 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3857ident_done:
3858
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003859 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003860 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003861 if (nand_manuf_ids[maf_idx].id == *maf_id)
3862 break;
3863 }
3864
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003865 if (chip->options & NAND_BUSWIDTH_AUTO) {
3866 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3867 chip->options |= busw;
3868 nand_set_defaults(chip, busw);
3869 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3870 /*
3871 * Check, if buswidth is correct. Hardware drivers should set
3872 * chip correct!
3873 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003874 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3875 *maf_id, *dev_id);
3876 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3877 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003878 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3879 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003880 return ERR_PTR(-EINVAL);
3881 }
3882
Brian Norris7e74c2d2012-09-24 20:40:49 -07003883 nand_decode_bbm_options(mtd, chip, id_data);
3884
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003885 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003886 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003887 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003888 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003889
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003890 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003891 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003892 if (chip->chipsize & 0xffffffff)
3893 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003894 else {
3895 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3896 chip->chip_shift += 32 - 1;
3897 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003898
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003899 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003900 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003901
Brian Norris8b6e50c2011-05-25 14:59:01 -07003902 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003903 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3904 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003905
Ezequiel Garcia20171642013-11-25 08:30:31 -03003906 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3907 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003908
3909 if (chip->onfi_version)
3910 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3911 chip->onfi_params.model);
3912 else if (chip->jedec_version)
3913 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3914 chip->jedec_params.model);
3915 else
3916 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3917 type->name);
3918
Rafał Miłecki3755a992014-10-21 00:01:04 +02003919 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003920 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003921 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003922 return type;
3923}
3924
Brian Norris5844fee2015-01-23 00:22:27 -08003925static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip,
3926 struct device_node *dn)
3927{
3928 int ecc_mode, ecc_strength, ecc_step;
3929
3930 if (of_get_nand_bus_width(dn) == 16)
3931 chip->options |= NAND_BUSWIDTH_16;
3932
3933 if (of_get_nand_on_flash_bbt(dn))
3934 chip->bbt_options |= NAND_BBT_USE_FLASH;
3935
3936 ecc_mode = of_get_nand_ecc_mode(dn);
3937 ecc_strength = of_get_nand_ecc_strength(dn);
3938 ecc_step = of_get_nand_ecc_step_size(dn);
3939
3940 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3941 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3942 pr_err("must set both strength and step size in DT\n");
3943 return -EINVAL;
3944 }
3945
3946 if (ecc_mode >= 0)
3947 chip->ecc.mode = ecc_mode;
3948
3949 if (ecc_strength >= 0)
3950 chip->ecc.strength = ecc_strength;
3951
3952 if (ecc_step > 0)
3953 chip->ecc.size = ecc_step;
3954
3955 return 0;
3956}
3957
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003958/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003959 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003960 * @mtd: MTD device structure
3961 * @maxchips: number of chips to scan for
3962 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003963 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003964 * This is the first phase of the normal nand_scan() function. It reads the
3965 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003966 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003967 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003968 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003969int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3970 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003971{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003972 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003973 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003974 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08003975 int ret;
3976
Marek Vasut61528d82015-09-03 18:35:37 +02003977 if (chip->flash_node) {
3978 ret = nand_dt_init(mtd, chip, chip->flash_node);
Brian Norris5844fee2015-01-23 00:22:27 -08003979 if (ret)
3980 return ret;
3981 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003982
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003983 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003984 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003985
3986 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003987 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3988 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003989
3990 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003991 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003992 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003993 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003994 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996
Huang Shijie07300162012-11-09 16:23:45 +08003997 chip->select_chip(mtd, -1);
3998
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003999 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004000 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004001 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004002 /* See comment in nand_get_flash_type for reset */
4003 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004005 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004007 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004008 nand_dev_id != chip->read_byte(mtd)) {
4009 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 break;
Huang Shijie07300162012-11-09 16:23:45 +08004011 }
4012 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
4014 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004015 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004016
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004018 chip->numchips = i;
4019 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
David Woodhouse3b85c322006-09-25 17:06:53 +01004021 return 0;
4022}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004023EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004024
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004025/*
4026 * Check if the chip configuration meet the datasheet requirements.
4027
4028 * If our configuration corrects A bits per B bytes and the minimum
4029 * required correction level is X bits per Y bytes, then we must ensure
4030 * both of the following are true:
4031 *
4032 * (1) A / B >= X / Y
4033 * (2) A >= X
4034 *
4035 * Requirement (1) ensures we can correct for the required bitflip density.
4036 * Requirement (2) ensures we can correct even when all bitflips are clumped
4037 * in the same sector.
4038 */
4039static bool nand_ecc_strength_good(struct mtd_info *mtd)
4040{
4041 struct nand_chip *chip = mtd->priv;
4042 struct nand_ecc_ctrl *ecc = &chip->ecc;
4043 int corr, ds_corr;
4044
4045 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4046 /* Not enough information */
4047 return true;
4048
4049 /*
4050 * We get the number of corrected bits per page to compare
4051 * the correction density.
4052 */
4053 corr = (mtd->writesize * ecc->strength) / ecc->size;
4054 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4055
4056 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4057}
David Woodhouse3b85c322006-09-25 17:06:53 +01004058
4059/**
4060 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004061 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004062 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004063 * This is the second phase of the normal nand_scan() function. It fills out
4064 * all the uninitialized function pointers with the defaults and scans for a
4065 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004066 */
4067int nand_scan_tail(struct mtd_info *mtd)
4068{
4069 int i;
4070 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08004071 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004072 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01004073
Brian Norrise2414f42012-02-06 13:44:00 -08004074 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4075 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4076 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4077
Huang Shijief02ea4e2014-01-13 14:27:12 +08004078 if (!(chip->options & NAND_OWN_BUFFERS)) {
4079 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4080 + mtd->oobsize * 3, GFP_KERNEL);
4081 if (!nbuf)
4082 return -ENOMEM;
4083 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4084 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4085 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4086
4087 chip->buffers = nbuf;
4088 } else {
4089 if (!chip->buffers)
4090 return -ENOMEM;
4091 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004092
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004093 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004094 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004095
4096 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004097 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004098 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004099 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004100 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08004102 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 break;
4104 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08004105 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 break;
4107 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08004108 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004110 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08004111 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 default:
Brian Norrisd0370212011-07-19 10:06:08 -07004114 pr_warn("No oob scheme defined for oobsize %d\n",
4115 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 BUG();
4117 }
4118 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004119
David Woodhouse956e9442006-09-25 17:12:39 +01004120 if (!chip->write_page)
4121 chip->write_page = nand_write_page;
4122
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004123 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004124 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004125 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004126 */
David Woodhouse956e9442006-09-25 17:12:39 +01004127
Huang Shijie97de79e02013-10-18 14:20:53 +08004128 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004129 case NAND_ECC_HW_OOB_FIRST:
4130 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004131 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004132 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004133 BUG();
4134 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004135 if (!ecc->read_page)
4136 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004137
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004138 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004139 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004140 if (!ecc->read_page)
4141 ecc->read_page = nand_read_page_hwecc;
4142 if (!ecc->write_page)
4143 ecc->write_page = nand_write_page_hwecc;
4144 if (!ecc->read_page_raw)
4145 ecc->read_page_raw = nand_read_page_raw;
4146 if (!ecc->write_page_raw)
4147 ecc->write_page_raw = nand_write_page_raw;
4148 if (!ecc->read_oob)
4149 ecc->read_oob = nand_read_oob_std;
4150 if (!ecc->write_oob)
4151 ecc->write_oob = nand_write_oob_std;
4152 if (!ecc->read_subpage)
4153 ecc->read_subpage = nand_read_subpage;
4154 if (!ecc->write_subpage)
4155 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004156
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004157 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004158 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4159 (!ecc->read_page ||
4160 ecc->read_page == nand_read_page_hwecc ||
4161 !ecc->write_page ||
4162 ecc->write_page == nand_write_page_hwecc)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004163 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004164 BUG();
4165 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004166 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004167 if (!ecc->read_page)
4168 ecc->read_page = nand_read_page_syndrome;
4169 if (!ecc->write_page)
4170 ecc->write_page = nand_write_page_syndrome;
4171 if (!ecc->read_page_raw)
4172 ecc->read_page_raw = nand_read_page_raw_syndrome;
4173 if (!ecc->write_page_raw)
4174 ecc->write_page_raw = nand_write_page_raw_syndrome;
4175 if (!ecc->read_oob)
4176 ecc->read_oob = nand_read_oob_syndrome;
4177 if (!ecc->write_oob)
4178 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004179
Huang Shijie97de79e02013-10-18 14:20:53 +08004180 if (mtd->writesize >= ecc->size) {
4181 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07004182 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4183 BUG();
4184 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004185 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004186 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004187 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4188 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004189 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004191 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08004192 ecc->calculate = nand_calculate_ecc;
4193 ecc->correct = nand_correct_data;
4194 ecc->read_page = nand_read_page_swecc;
4195 ecc->read_subpage = nand_read_subpage;
4196 ecc->write_page = nand_write_page_swecc;
4197 ecc->read_page_raw = nand_read_page_raw;
4198 ecc->write_page_raw = nand_write_page_raw;
4199 ecc->read_oob = nand_read_oob_std;
4200 ecc->write_oob = nand_write_oob_std;
4201 if (!ecc->size)
4202 ecc->size = 256;
4203 ecc->bytes = 3;
4204 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004206
Ivan Djelic193bd402011-03-11 11:05:33 +01004207 case NAND_ECC_SOFT_BCH:
4208 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03004209 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004210 BUG();
4211 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004212 ecc->calculate = nand_bch_calculate_ecc;
4213 ecc->correct = nand_bch_correct_data;
4214 ecc->read_page = nand_read_page_swecc;
4215 ecc->read_subpage = nand_read_subpage;
4216 ecc->write_page = nand_write_page_swecc;
4217 ecc->read_page_raw = nand_read_page_raw;
4218 ecc->write_page_raw = nand_write_page_raw;
4219 ecc->read_oob = nand_read_oob_std;
4220 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004221 /*
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004222 * Board driver should supply ecc.size and ecc.strength values
4223 * to select how many bits are correctable. Otherwise, default
4224 * to 4 bits for large page devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004225 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004226 if (!ecc->size && (mtd->oobsize >= 64)) {
4227 ecc->size = 512;
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004228 ecc->strength = 4;
Ivan Djelic193bd402011-03-11 11:05:33 +01004229 }
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004230
4231 /* See nand_bch_init() for details. */
4232 ecc->bytes = DIV_ROUND_UP(
4233 ecc->strength * fls(8 * ecc->size), 8);
Huang Shijie97de79e02013-10-18 14:20:53 +08004234 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4235 &ecc->layout);
4236 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004237 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004238 BUG();
4239 }
4240 break;
4241
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004242 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004243 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004244 ecc->read_page = nand_read_page_raw;
4245 ecc->write_page = nand_write_page_raw;
4246 ecc->read_oob = nand_read_oob_std;
4247 ecc->read_page_raw = nand_read_page_raw;
4248 ecc->write_page_raw = nand_write_page_raw;
4249 ecc->write_oob = nand_write_oob_std;
4250 ecc->size = mtd->writesize;
4251 ecc->bytes = 0;
4252 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08004256 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004257 BUG();
4258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Brian Norris9ce244b2011-08-30 18:45:37 -07004260 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004261 if (!ecc->read_oob_raw)
4262 ecc->read_oob_raw = ecc->read_oob;
4263 if (!ecc->write_oob_raw)
4264 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004265
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004266 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004267 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004268 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004269 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004270 ecc->layout->oobavail = 0;
4271 for (i = 0; ecc->layout->oobfree[i].length
4272 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4273 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4274 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004275
Thomas Petazzoni54c39e92014-07-02 15:16:32 +02004276 /* ECC sanity check: warn if it's too weak */
4277 if (!nand_ecc_strength_good(mtd))
4278 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4279 mtd->name);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004280
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004281 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004282 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004283 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004284 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004285 ecc->steps = mtd->writesize / ecc->size;
4286 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004287 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004288 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004290 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004291
Brian Norris8b6e50c2011-05-25 14:59:01 -07004292 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004293 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004294 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004295 case 2:
4296 mtd->subpage_sft = 1;
4297 break;
4298 case 4:
4299 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004300 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004301 mtd->subpage_sft = 2;
4302 break;
4303 }
4304 }
4305 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4306
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004307 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004308 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004311 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004313 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304314 switch (ecc->mode) {
4315 case NAND_ECC_SOFT:
4316 case NAND_ECC_SOFT_BCH:
4317 if (chip->page_shift > 9)
4318 chip->options |= NAND_SUBPAGE_READ;
4319 break;
4320
4321 default:
4322 break;
4323 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004326 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004327 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4328 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004329 mtd->_erase = nand_erase;
4330 mtd->_point = NULL;
4331 mtd->_unpoint = NULL;
4332 mtd->_read = nand_read;
4333 mtd->_write = nand_write;
4334 mtd->_panic_write = panic_nand_write;
4335 mtd->_read_oob = nand_read_oob;
4336 mtd->_write_oob = nand_write_oob;
4337 mtd->_sync = nand_sync;
4338 mtd->_lock = NULL;
4339 mtd->_unlock = NULL;
4340 mtd->_suspend = nand_suspend;
4341 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004342 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004343 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004344 mtd->_block_isbad = nand_block_isbad;
4345 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004346 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Mike Dunn6a918ba2012-03-11 14:21:11 -07004348 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004349 mtd->ecclayout = ecc->layout;
4350 mtd->ecc_strength = ecc->strength;
4351 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004352 /*
4353 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4354 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4355 * properly set.
4356 */
4357 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004358 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004360 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004361 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004365 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004367EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Brian Norris8b6e50c2011-05-25 14:59:01 -07004369/*
4370 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004371 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004372 * to call us from in-kernel code if the core NAND support is modular.
4373 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004374#ifdef MODULE
4375#define caller_is_module() (1)
4376#else
4377#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004378 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004379#endif
4380
4381/**
4382 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004383 * @mtd: MTD device structure
4384 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004385 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004386 * This fills out all the uninitialized function pointers with the defaults.
4387 * The flash ID is read and the mtd/chip structures are filled with the
4388 * appropriate values. The mtd->owner field must be set to the module of the
4389 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004390 */
4391int nand_scan(struct mtd_info *mtd, int maxchips)
4392{
4393 int ret;
4394
4395 /* Many callers got this wrong, so check for it for a while... */
4396 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004397 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004398 BUG();
4399 }
4400
David Woodhouse5e81e882010-02-26 18:32:56 +00004401 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004402 if (!ret)
4403 ret = nand_scan_tail(mtd);
4404 return ret;
4405}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004406EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004409 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004410 * @mtd: MTD device structure
4411 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004412void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004414 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Ivan Djelic193bd402011-03-11 11:05:33 +01004416 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4417 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4418
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004419 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Jesper Juhlfa671642005-11-07 01:01:27 -08004421 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004422 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004423 if (!(chip->options & NAND_OWN_BUFFERS))
4424 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004425
4426 /* Free bad block descriptor memory */
4427 if (chip->badblock_pattern && chip->badblock_pattern->options
4428 & NAND_BBT_DYNAMICSTRUCT)
4429 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
David Woodhousee0c7d762006-05-13 18:07:53 +01004431EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004432
4433static int __init nand_base_init(void)
4434{
4435 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4436 return 0;
4437}
4438
4439static void __exit nand_base_exit(void)
4440{
4441 led_trigger_unregister_simple(nand_led_trigger);
4442}
4443
4444module_init(nand_base_init);
4445module_exit(nand_base_exit);
4446
David Woodhousee0c7d762006-05-13 18:07:53 +01004447MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004448MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4449MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004450MODULE_DESCRIPTION("Generic NAND flash driver code");