blob: 596a9b0503dafcf581041d0fa988ebbc97a9f3c4 [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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100109 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530110 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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100135 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100153 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200154 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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100166 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200167 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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100178 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200179 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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100191 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200192
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100214 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100215
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100228 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100229 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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100260 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100275 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100290 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100306 struct nand_chip *chip = mtd_to_nand(mtd);
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;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100323 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100383 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700384 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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100433 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100474 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100494 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300495
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100515 struct nand_chip *chip = mtd_to_nand(mtd);
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{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100534 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200535 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
Alex Smithb70af9b2015-10-06 14:52:07 +0100546/**
547 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
548 * @mtd: MTD device structure
549 *
550 * Wait for the ready pin after a command, and warn if a timeout occurs.
551 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100552void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000553{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100554 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100555 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000556
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200557 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100558 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200559
Richard Purdie8fe833c2006-03-31 02:31:14 -0800560 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700561 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100562 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000563 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200564 if (chip->dev_ready(mtd))
Alex Smithb70af9b2015-10-06 14:52:07 +0100565 goto out;
566 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000567 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100568
Brian Norris9ebfdf52016-03-04 17:19:23 -0800569 if (!chip->dev_ready(mtd))
570 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Alex Smithb70af9b2015-10-06 14:52:07 +0100571out:
Richard Purdie8fe833c2006-03-31 02:31:14 -0800572 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000573}
David Woodhouse4b648b02006-09-25 17:05:24 +0100574EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200577 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
578 * @mtd: MTD device structure
579 * @timeo: Timeout in ms
580 *
581 * Wait for status ready (i.e. command done) or timeout.
582 */
583static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
584{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100585 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200586
587 timeo = jiffies + msecs_to_jiffies(timeo);
588 do {
589 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
590 break;
591 touch_softlockup_watchdog();
592 } while (time_before(jiffies, timeo));
593};
594
595/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700597 * @mtd: MTD device structure
598 * @command: the command to be sent
599 * @column: the column address for this command, -1 if none
600 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700602 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200603 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200605static void nand_command(struct mtd_info *mtd, unsigned int command,
606 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100608 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200609 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Brian Norris8b6e50c2011-05-25 14:59:01 -0700611 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (command == NAND_CMD_SEQIN) {
613 int readcmd;
614
Joern Engel28318772006-05-22 23:18:05 +0200615 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200617 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 readcmd = NAND_CMD_READOOB;
619 } else if (column < 256) {
620 /* First 256 bytes --> READ0 */
621 readcmd = NAND_CMD_READ0;
622 } else {
623 column -= 256;
624 readcmd = NAND_CMD_READ1;
625 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200626 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200627 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200629 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Brian Norris8b6e50c2011-05-25 14:59:01 -0700631 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200632 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
633 /* Serially input address */
634 if (column != -1) {
635 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800636 if (chip->options & NAND_BUSWIDTH_16 &&
637 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200638 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200639 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200640 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200643 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200644 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200645 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200646 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200647 if (chip->chipsize > (32 << 20))
648 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200649 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200650 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000651
652 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700653 * Program and erase have their own busy handlers status and sequential
654 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 case NAND_CMD_PAGEPROG:
659 case NAND_CMD_ERASE1:
660 case NAND_CMD_ERASE2:
661 case NAND_CMD_SEQIN:
662 case NAND_CMD_STATUS:
663 return;
664
665 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200666 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200668 udelay(chip->chip_delay);
669 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200670 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200671 chip->cmd_ctrl(mtd,
672 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200673 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
674 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return;
676
David Woodhousee0c7d762006-05-13 18:07:53 +0100677 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000679 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * If we don't have access to the busy pin, we apply the given
681 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100682 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 if (!chip->dev_ready) {
684 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700688 /*
689 * Apply this short delay always to ensure that we do wait tWB in
690 * any case on any machine.
691 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100692 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000693
694 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/**
698 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700699 * @mtd: MTD device structure
700 * @command: the command to be sent
701 * @column: the column address for this command, -1 if none
702 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200704 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700705 * devices. We don't have the separate regions as we have in the small page
706 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200708static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
709 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100711 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* Emulate NAND_CMD_READOOB */
714 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200715 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 command = NAND_CMD_READ0;
717 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000718
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200719 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400720 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* Serially input address */
726 if (column != -1) {
727 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800728 if (chip->options & NAND_BUSWIDTH_16 &&
729 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200732 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200733 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 chip->cmd_ctrl(mtd, page_addr, ctrl);
737 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200738 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200740 if (chip->chipsize > (128 << 20))
741 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200742 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200745 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000746
747 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700748 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100749 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 case NAND_CMD_CACHEDPROG:
754 case NAND_CMD_PAGEPROG:
755 case NAND_CMD_ERASE1:
756 case NAND_CMD_ERASE2:
757 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200758 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200763 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200765 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200766 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
767 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
768 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
769 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200770 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
771 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return;
773
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200774 case NAND_CMD_RNDOUT:
775 /* No ready / busy check necessary */
776 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
777 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
778 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
779 NAND_NCE | NAND_CTRL_CHANGE);
780 return;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200783 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
784 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
785 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
786 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000787
David Woodhousee0c7d762006-05-13 18:07:53 +0100788 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000790 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700792 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100793 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200794 if (!chip->dev_ready) {
795 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000799
Brian Norris8b6e50c2011-05-25 14:59:01 -0700800 /*
801 * Apply this short delay always to ensure that we do wait tWB in
802 * any case on any machine.
803 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100804 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000805
806 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200810 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700811 * @chip: the nand chip descriptor
812 * @mtd: MTD device structure
813 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200814 *
815 * Used when in panic, no locks are taken.
816 */
817static void panic_nand_get_device(struct nand_chip *chip,
818 struct mtd_info *mtd, int new_state)
819{
Brian Norris7854d3f2011-06-23 14:12:08 -0700820 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200821 chip->controller->active = chip;
822 chip->state = new_state;
823}
824
825/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700827 * @mtd: MTD device structure
828 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 *
830 * Get the device and lock it for exclusive access
831 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200832static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800833nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100835 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200836 spinlock_t *lock = &chip->controller->lock;
837 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100838 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200839retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100840 spin_lock(lock);
841
vimal singhb8b3ee92009-07-09 20:41:22 +0530842 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200843 if (!chip->controller->active)
844 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200845
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200846 if (chip->controller->active == chip && chip->state == FL_READY) {
847 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100848 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100849 return 0;
850 }
851 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800852 if (chip->controller->active->state == FL_PM_SUSPENDED) {
853 chip->state = FL_PM_SUSPENDED;
854 spin_unlock(lock);
855 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800856 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100857 }
858 set_current_state(TASK_UNINTERRUPTIBLE);
859 add_wait_queue(wq, &wait);
860 spin_unlock(lock);
861 schedule();
862 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto retry;
864}
865
866/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700867 * panic_nand_wait - [GENERIC] wait until the command is done
868 * @mtd: MTD device structure
869 * @chip: NAND chip structure
870 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200871 *
872 * Wait for command done. This is a helper function for nand_wait used when
873 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400874 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200875 */
876static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
877 unsigned long timeo)
878{
879 int i;
880 for (i = 0; i < timeo; i++) {
881 if (chip->dev_ready) {
882 if (chip->dev_ready(mtd))
883 break;
884 } else {
885 if (chip->read_byte(mtd) & NAND_STATUS_READY)
886 break;
887 }
888 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200889 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200890}
891
892/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700893 * nand_wait - [DEFAULT] wait until the command is done
894 * @mtd: MTD device structure
895 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100897 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700898 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200899static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901
Alex Smithb70af9b2015-10-06 14:52:07 +0100902 int status;
903 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Richard Purdie8fe833c2006-03-31 02:31:14 -0800905 led_trigger_event(nand_led_trigger, LED_FULL);
906
Brian Norris8b6e50c2011-05-25 14:59:01 -0700907 /*
908 * Apply this short delay always to ensure that we do wait tWB in any
909 * case on any machine.
910 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100911 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200913 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200915 if (in_interrupt() || oops_in_progress)
916 panic_nand_wait(mtd, chip, timeo);
917 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800918 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100919 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200920 if (chip->dev_ready) {
921 if (chip->dev_ready(mtd))
922 break;
923 } else {
924 if (chip->read_byte(mtd) & NAND_STATUS_READY)
925 break;
926 }
927 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100928 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800930 led_trigger_event(nand_led_trigger, LED_OFF);
931
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200932 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100933 /* This can happen if in case of timeout or buggy dev_ready */
934 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return status;
936}
937
938/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700939 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700940 * @mtd: mtd info
941 * @ofs: offset to start unlock from
942 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700943 * @invert: when = 0, unlock the range of blocks within the lower and
944 * upper boundary address
945 * when = 1, unlock the range of blocks outside the boundaries
946 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530947 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700948 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530949 */
950static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
951 uint64_t len, int invert)
952{
953 int ret = 0;
954 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100955 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +0530956
957 /* Submit address of first page to unlock */
958 page = ofs >> chip->page_shift;
959 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
960
961 /* Submit address of last page to unlock */
962 page = (ofs + len) >> chip->page_shift;
963 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
964 (page | invert) & chip->pagemask);
965
966 /* Call wait ready function */
967 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530968 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400969 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700970 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530971 __func__, status);
972 ret = -EIO;
973 }
974
975 return ret;
976}
977
978/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700979 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700980 * @mtd: mtd info
981 * @ofs: offset to start unlock from
982 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530983 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700984 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530985 */
986int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
987{
988 int ret = 0;
989 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100990 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +0530991
Brian Norris289c0522011-07-19 10:06:09 -0700992 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530993 __func__, (unsigned long long)ofs, len);
994
995 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -0800996 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +0530997
998 /* Align to last block address if size addresses end of the device */
999 if (ofs + len == mtd->size)
1000 len -= mtd->erasesize;
1001
Huang Shijie6a8214a2012-11-19 14:43:30 +08001002 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301003
1004 /* Shift to get chip number */
1005 chipnr = ofs >> chip->chip_shift;
1006
1007 chip->select_chip(mtd, chipnr);
1008
White Ding57d3a9a2014-07-24 00:10:45 +08001009 /*
1010 * Reset the chip.
1011 * If we want to check the WP through READ STATUS and check the bit 7
1012 * we must reset the chip
1013 * some operation can also clear the bit 7 of status register
1014 * eg. erase/program a locked block
1015 */
1016 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1017
Vimal Singh7d70f332010-02-08 15:50:49 +05301018 /* Check, if it is write protected */
1019 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001020 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301021 __func__);
1022 ret = -EIO;
1023 goto out;
1024 }
1025
1026 ret = __nand_unlock(mtd, ofs, len, 0);
1027
1028out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001029 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301030 nand_release_device(mtd);
1031
1032 return ret;
1033}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001034EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301035
1036/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001037 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001038 * @mtd: mtd info
1039 * @ofs: offset to start unlock from
1040 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301041 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001042 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1043 * have this feature, but it allows only to lock all blocks, not for specified
1044 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1045 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301046 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001047 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301048 */
1049int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1050{
1051 int ret = 0;
1052 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001053 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301054
Brian Norris289c0522011-07-19 10:06:09 -07001055 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301056 __func__, (unsigned long long)ofs, len);
1057
1058 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001059 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301060
Huang Shijie6a8214a2012-11-19 14:43:30 +08001061 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301062
1063 /* Shift to get chip number */
1064 chipnr = ofs >> chip->chip_shift;
1065
1066 chip->select_chip(mtd, chipnr);
1067
White Ding57d3a9a2014-07-24 00:10:45 +08001068 /*
1069 * Reset the chip.
1070 * If we want to check the WP through READ STATUS and check the bit 7
1071 * we must reset the chip
1072 * some operation can also clear the bit 7 of status register
1073 * eg. erase/program a locked block
1074 */
1075 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1076
Vimal Singh7d70f332010-02-08 15:50:49 +05301077 /* Check, if it is write protected */
1078 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001079 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301080 __func__);
1081 status = MTD_ERASE_FAILED;
1082 ret = -EIO;
1083 goto out;
1084 }
1085
1086 /* Submit address of first page to lock */
1087 page = ofs >> chip->page_shift;
1088 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1089
1090 /* Call wait ready function */
1091 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301092 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001093 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001094 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301095 __func__, status);
1096 ret = -EIO;
1097 goto out;
1098 }
1099
1100 ret = __nand_unlock(mtd, ofs, len, 0x1);
1101
1102out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001103 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301104 nand_release_device(mtd);
1105
1106 return ret;
1107}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001108EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301109
1110/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001111 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1112 * @buf: buffer to test
1113 * @len: buffer length
1114 * @bitflips_threshold: maximum number of bitflips
1115 *
1116 * Check if a buffer contains only 0xff, which means the underlying region
1117 * has been erased and is ready to be programmed.
1118 * The bitflips_threshold specify the maximum number of bitflips before
1119 * considering the region is not erased.
1120 * Note: The logic of this function has been extracted from the memweight
1121 * implementation, except that nand_check_erased_buf function exit before
1122 * testing the whole buffer if the number of bitflips exceed the
1123 * bitflips_threshold value.
1124 *
1125 * Returns a positive number of bitflips less than or equal to
1126 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1127 * threshold.
1128 */
1129static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1130{
1131 const unsigned char *bitmap = buf;
1132 int bitflips = 0;
1133 int weight;
1134
1135 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1136 len--, bitmap++) {
1137 weight = hweight8(*bitmap);
1138 bitflips += BITS_PER_BYTE - weight;
1139 if (unlikely(bitflips > bitflips_threshold))
1140 return -EBADMSG;
1141 }
1142
1143 for (; len >= sizeof(long);
1144 len -= sizeof(long), bitmap += sizeof(long)) {
1145 weight = hweight_long(*((unsigned long *)bitmap));
1146 bitflips += BITS_PER_LONG - weight;
1147 if (unlikely(bitflips > bitflips_threshold))
1148 return -EBADMSG;
1149 }
1150
1151 for (; len > 0; len--, bitmap++) {
1152 weight = hweight8(*bitmap);
1153 bitflips += BITS_PER_BYTE - weight;
1154 if (unlikely(bitflips > bitflips_threshold))
1155 return -EBADMSG;
1156 }
1157
1158 return bitflips;
1159}
1160
1161/**
1162 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1163 * 0xff data
1164 * @data: data buffer to test
1165 * @datalen: data length
1166 * @ecc: ECC buffer
1167 * @ecclen: ECC length
1168 * @extraoob: extra OOB buffer
1169 * @extraooblen: extra OOB length
1170 * @bitflips_threshold: maximum number of bitflips
1171 *
1172 * Check if a data buffer and its associated ECC and OOB data contains only
1173 * 0xff pattern, which means the underlying region has been erased and is
1174 * ready to be programmed.
1175 * The bitflips_threshold specify the maximum number of bitflips before
1176 * considering the region as not erased.
1177 *
1178 * Note:
1179 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1180 * different from the NAND page size. When fixing bitflips, ECC engines will
1181 * report the number of errors per chunk, and the NAND core infrastructure
1182 * expect you to return the maximum number of bitflips for the whole page.
1183 * This is why you should always use this function on a single chunk and
1184 * not on the whole page. After checking each chunk you should update your
1185 * max_bitflips value accordingly.
1186 * 2/ When checking for bitflips in erased pages you should not only check
1187 * the payload data but also their associated ECC data, because a user might
1188 * have programmed almost all bits to 1 but a few. In this case, we
1189 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1190 * this case.
1191 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1192 * data are protected by the ECC engine.
1193 * It could also be used if you support subpages and want to attach some
1194 * extra OOB data to an ECC chunk.
1195 *
1196 * Returns a positive number of bitflips less than or equal to
1197 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1198 * threshold. In case of success, the passed buffers are filled with 0xff.
1199 */
1200int nand_check_erased_ecc_chunk(void *data, int datalen,
1201 void *ecc, int ecclen,
1202 void *extraoob, int extraooblen,
1203 int bitflips_threshold)
1204{
1205 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1206
1207 data_bitflips = nand_check_erased_buf(data, datalen,
1208 bitflips_threshold);
1209 if (data_bitflips < 0)
1210 return data_bitflips;
1211
1212 bitflips_threshold -= data_bitflips;
1213
1214 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1215 if (ecc_bitflips < 0)
1216 return ecc_bitflips;
1217
1218 bitflips_threshold -= ecc_bitflips;
1219
1220 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1221 bitflips_threshold);
1222 if (extraoob_bitflips < 0)
1223 return extraoob_bitflips;
1224
1225 if (data_bitflips)
1226 memset(data, 0xff, datalen);
1227
1228 if (ecc_bitflips)
1229 memset(ecc, 0xff, ecclen);
1230
1231 if (extraoob_bitflips)
1232 memset(extraoob, 0xff, extraooblen);
1233
1234 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1235}
1236EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1237
1238/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001239 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001240 * @mtd: mtd info structure
1241 * @chip: nand chip info structure
1242 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001243 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001244 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001245 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001246 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001247 */
1248static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001249 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001250{
1251 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001252 if (oob_required)
1253 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001254 return 0;
1255}
1256
1257/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001258 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001259 * @mtd: mtd info structure
1260 * @chip: nand chip info structure
1261 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001262 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001263 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001264 *
1265 * We need a special oob layout and handling even when OOB isn't used.
1266 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001267static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001268 struct nand_chip *chip, uint8_t *buf,
1269 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001270{
1271 int eccsize = chip->ecc.size;
1272 int eccbytes = chip->ecc.bytes;
1273 uint8_t *oob = chip->oob_poi;
1274 int steps, size;
1275
1276 for (steps = chip->ecc.steps; steps > 0; steps--) {
1277 chip->read_buf(mtd, buf, eccsize);
1278 buf += eccsize;
1279
1280 if (chip->ecc.prepad) {
1281 chip->read_buf(mtd, oob, chip->ecc.prepad);
1282 oob += chip->ecc.prepad;
1283 }
1284
1285 chip->read_buf(mtd, oob, eccbytes);
1286 oob += eccbytes;
1287
1288 if (chip->ecc.postpad) {
1289 chip->read_buf(mtd, oob, chip->ecc.postpad);
1290 oob += chip->ecc.postpad;
1291 }
1292 }
1293
1294 size = mtd->oobsize - (oob - chip->oob_poi);
1295 if (size)
1296 chip->read_buf(mtd, oob, size);
1297
1298 return 0;
1299}
1300
1301/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001302 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001303 * @mtd: mtd info structure
1304 * @chip: nand chip info structure
1305 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001306 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001307 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001308 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001309static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001310 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001312 int i, eccsize = chip->ecc.size;
1313 int eccbytes = chip->ecc.bytes;
1314 int eccsteps = chip->ecc.steps;
1315 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001316 uint8_t *ecc_calc = chip->buffers->ecccalc;
1317 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001318 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001319 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001320
Brian Norris1fbb9382012-05-02 10:14:55 -07001321 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001322
1323 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1324 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1325
1326 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001327 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001328
1329 eccsteps = chip->ecc.steps;
1330 p = buf;
1331
1332 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1333 int stat;
1334
1335 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001336 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001337 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001338 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001339 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001340 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1341 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001342 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001343 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001344}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301347 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001348 * @mtd: mtd info structure
1349 * @chip: nand chip info structure
1350 * @data_offs: offset of requested data within the page
1351 * @readlen: data length
1352 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001353 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001354 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001355static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001356 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1357 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001358{
1359 int start_step, end_step, num_steps;
1360 uint32_t *eccpos = chip->ecc.layout->eccpos;
1361 uint8_t *p;
1362 int data_col_addr, i, gaps = 0;
1363 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1364 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301365 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001366 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001367
Brian Norris7854d3f2011-06-23 14:12:08 -07001368 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001369 start_step = data_offs / chip->ecc.size;
1370 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1371 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301372 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001373
Brian Norris8b6e50c2011-05-25 14:59:01 -07001374 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001375 datafrag_len = num_steps * chip->ecc.size;
1376 eccfrag_len = num_steps * chip->ecc.bytes;
1377
1378 data_col_addr = start_step * chip->ecc.size;
1379 /* If we read not a page aligned data */
1380 if (data_col_addr != 0)
1381 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1382
1383 p = bufpoi + data_col_addr;
1384 chip->read_buf(mtd, p, datafrag_len);
1385
Brian Norris8b6e50c2011-05-25 14:59:01 -07001386 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001387 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1388 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1389
Brian Norris8b6e50c2011-05-25 14:59:01 -07001390 /*
1391 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001392 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001393 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001394 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301395 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001396 gaps = 1;
1397 break;
1398 }
1399 }
1400 if (gaps) {
1401 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1402 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1403 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001404 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001405 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001406 * about buswidth alignment in read_buf.
1407 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001408 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001409 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001410 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001411 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001412 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001413 aligned_len++;
1414
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001415 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1416 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001417 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1418 }
1419
1420 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001421 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001422
1423 p = bufpoi + data_col_addr;
1424 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1425 int stat;
1426
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001427 stat = chip->ecc.correct(mtd, p,
1428 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001429 if (stat == -EBADMSG &&
1430 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1431 /* check for empty pages with bitflips */
1432 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1433 &chip->buffers->ecccode[i],
1434 chip->ecc.bytes,
1435 NULL, 0,
1436 chip->ecc.strength);
1437 }
1438
Mike Dunn3f91e942012-04-25 12:06:09 -07001439 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001440 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001441 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001442 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001443 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1444 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001445 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001446 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001447}
1448
1449/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001450 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001451 * @mtd: mtd info structure
1452 * @chip: nand chip info structure
1453 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001454 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001455 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001456 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001457 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001458 */
1459static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001460 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001461{
1462 int i, eccsize = chip->ecc.size;
1463 int eccbytes = chip->ecc.bytes;
1464 int eccsteps = chip->ecc.steps;
1465 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001466 uint8_t *ecc_calc = chip->buffers->ecccalc;
1467 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001468 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001469 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001470
1471 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1472 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1473 chip->read_buf(mtd, p, eccsize);
1474 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1475 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001476 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001477
1478 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001479 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001480
1481 eccsteps = chip->ecc.steps;
1482 p = buf;
1483
1484 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1485 int stat;
1486
1487 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001488 if (stat == -EBADMSG &&
1489 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1490 /* check for empty pages with bitflips */
1491 stat = nand_check_erased_ecc_chunk(p, eccsize,
1492 &ecc_code[i], eccbytes,
1493 NULL, 0,
1494 chip->ecc.strength);
1495 }
1496
Mike Dunn3f91e942012-04-25 12:06:09 -07001497 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001498 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001499 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001500 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001501 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1502 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001503 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001504 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001505}
1506
1507/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001508 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001509 * @mtd: mtd info structure
1510 * @chip: nand chip info structure
1511 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001512 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001513 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001514 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001515 * Hardware ECC for large page chips, require OOB to be read first. For this
1516 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1517 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1518 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1519 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001520 */
1521static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001522 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001523{
1524 int i, eccsize = chip->ecc.size;
1525 int eccbytes = chip->ecc.bytes;
1526 int eccsteps = chip->ecc.steps;
1527 uint8_t *p = buf;
1528 uint8_t *ecc_code = chip->buffers->ecccode;
1529 uint32_t *eccpos = chip->ecc.layout->eccpos;
1530 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001531 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001532
1533 /* Read the OOB area first */
1534 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1535 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1536 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1537
1538 for (i = 0; i < chip->ecc.total; i++)
1539 ecc_code[i] = chip->oob_poi[eccpos[i]];
1540
1541 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1542 int stat;
1543
1544 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1545 chip->read_buf(mtd, p, eccsize);
1546 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1547
1548 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001549 if (stat == -EBADMSG &&
1550 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1551 /* check for empty pages with bitflips */
1552 stat = nand_check_erased_ecc_chunk(p, eccsize,
1553 &ecc_code[i], eccbytes,
1554 NULL, 0,
1555 chip->ecc.strength);
1556 }
1557
Mike Dunn3f91e942012-04-25 12:06:09 -07001558 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001559 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001560 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001561 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001562 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1563 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001564 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001565 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001566}
1567
1568/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001569 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001570 * @mtd: mtd info structure
1571 * @chip: nand chip info structure
1572 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001573 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001574 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001575 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001576 * The hw generator calculates the error syndrome automatically. Therefore we
1577 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001578 */
1579static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001580 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001581{
1582 int i, eccsize = chip->ecc.size;
1583 int eccbytes = chip->ecc.bytes;
1584 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001585 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001586 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001587 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001588 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001589
1590 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1591 int stat;
1592
1593 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1594 chip->read_buf(mtd, p, eccsize);
1595
1596 if (chip->ecc.prepad) {
1597 chip->read_buf(mtd, oob, chip->ecc.prepad);
1598 oob += chip->ecc.prepad;
1599 }
1600
1601 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1602 chip->read_buf(mtd, oob, eccbytes);
1603 stat = chip->ecc.correct(mtd, p, oob, NULL);
1604
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001605 oob += eccbytes;
1606
1607 if (chip->ecc.postpad) {
1608 chip->read_buf(mtd, oob, chip->ecc.postpad);
1609 oob += chip->ecc.postpad;
1610 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001611
1612 if (stat == -EBADMSG &&
1613 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1614 /* check for empty pages with bitflips */
1615 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1616 oob - eccpadbytes,
1617 eccpadbytes,
1618 NULL, 0,
1619 chip->ecc.strength);
1620 }
1621
1622 if (stat < 0) {
1623 mtd->ecc_stats.failed++;
1624 } else {
1625 mtd->ecc_stats.corrected += stat;
1626 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1627 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001628 }
1629
1630 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001631 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001632 if (i)
1633 chip->read_buf(mtd, oob, i);
1634
Mike Dunn3f91e942012-04-25 12:06:09 -07001635 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001636}
1637
1638/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001639 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001640 * @chip: nand chip structure
1641 * @oob: oob destination address
1642 * @ops: oob ops structure
1643 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001644 */
1645static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001646 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001647{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001648 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001649
Brian Norris0612b9d2011-08-30 18:45:40 -07001650 case MTD_OPS_PLACE_OOB:
1651 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001652 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1653 return oob + len;
1654
Brian Norris0612b9d2011-08-30 18:45:40 -07001655 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001656 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001657 uint32_t boffs = 0, roffs = ops->ooboffs;
1658 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001659
Florian Fainellif8ac0412010-09-07 13:23:43 +02001660 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001661 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001662 if (unlikely(roffs)) {
1663 if (roffs >= free->length) {
1664 roffs -= free->length;
1665 continue;
1666 }
1667 boffs = free->offset + roffs;
1668 bytes = min_t(size_t, len,
1669 (free->length - roffs));
1670 roffs = 0;
1671 } else {
1672 bytes = min_t(size_t, len, free->length);
1673 boffs = free->offset;
1674 }
1675 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001676 oob += bytes;
1677 }
1678 return oob;
1679 }
1680 default:
1681 BUG();
1682 }
1683 return NULL;
1684}
1685
1686/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001687 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1688 * @mtd: MTD device structure
1689 * @retry_mode: the retry mode to use
1690 *
1691 * Some vendors supply a special command to shift the Vt threshold, to be used
1692 * when there are too many bitflips in a page (i.e., ECC error). After setting
1693 * a new threshold, the host should retry reading the page.
1694 */
1695static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1696{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001697 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001698
1699 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1700
1701 if (retry_mode >= chip->read_retries)
1702 return -EINVAL;
1703
1704 if (!chip->setup_read_retry)
1705 return -EOPNOTSUPP;
1706
1707 return chip->setup_read_retry(mtd, retry_mode);
1708}
1709
1710/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001711 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001712 * @mtd: MTD device structure
1713 * @from: offset to read from
1714 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001715 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001716 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001717 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001718static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1719 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001720{
Brian Norrise47f3db2012-05-02 10:14:56 -07001721 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001722 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001723 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001724 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001725 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001726 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001727 mtd->oobavail : mtd->oobsize;
1728
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001729 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001730 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001731 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001732 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001733 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001735 chipnr = (int)(from >> chip->chip_shift);
1736 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001738 realpage = (int)(from >> chip->page_shift);
1739 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001741 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001743 buf = ops->datbuf;
1744 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001745 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001746
Florian Fainellif8ac0412010-09-07 13:23:43 +02001747 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001748 unsigned int ecc_failures = mtd->ecc_stats.failed;
1749
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001750 bytes = min(mtd->writesize - col, readlen);
1751 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001752
Kamal Dasu66507c72014-05-01 20:51:19 -04001753 if (!aligned)
1754 use_bufpoi = 1;
1755 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1756 use_bufpoi = !virt_addr_valid(buf);
1757 else
1758 use_bufpoi = 0;
1759
Brian Norris8b6e50c2011-05-25 14:59:01 -07001760 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001761 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001762 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1763
1764 if (use_bufpoi && aligned)
1765 pr_debug("%s: using read bounce buffer for buf@%p\n",
1766 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Brian Norrisba84fb52014-01-03 15:13:33 -08001768read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001769 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Mike Dunnedbc45402012-04-25 12:06:11 -07001771 /*
1772 * Now read the page into the buffer. Absent an error,
1773 * the read methods return max bitflips per ecc step.
1774 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001775 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001776 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001777 oob_required,
1778 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001779 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1780 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001781 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001782 col, bytes, bufpoi,
1783 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001784 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001785 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001786 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001787 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001788 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001789 /* Invalidate page cache */
1790 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001791 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001792 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001793
Mike Dunnedbc45402012-04-25 12:06:11 -07001794 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1795
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001796 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001797 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001798 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001799 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001800 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001801 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001802 chip->pagebuf_bitflips = ret;
1803 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001804 /* Invalidate page cache */
1805 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001806 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001807 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001809
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001810 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001811 int toread = min(oobreadlen, max_oobsize);
1812
1813 if (toread) {
1814 oob = nand_transfer_oob(chip,
1815 oob, ops, toread);
1816 oobreadlen -= toread;
1817 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001818 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001819
1820 if (chip->options & NAND_NEED_READRDY) {
1821 /* Apply delay or wait for ready/busy pin */
1822 if (!chip->dev_ready)
1823 udelay(chip->chip_delay);
1824 else
1825 nand_wait_ready(mtd);
1826 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001827
Brian Norrisba84fb52014-01-03 15:13:33 -08001828 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001829 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001830 retry_mode++;
1831 ret = nand_setup_read_retry(mtd,
1832 retry_mode);
1833 if (ret < 0)
1834 break;
1835
1836 /* Reset failures; retry */
1837 mtd->ecc_stats.failed = ecc_failures;
1838 goto read_retry;
1839 } else {
1840 /* No more retry modes; real failure */
1841 ecc_fail = true;
1842 }
1843 }
1844
1845 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001846 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001847 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001848 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001849 max_bitflips = max_t(unsigned int, max_bitflips,
1850 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001853 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001854
Brian Norrisba84fb52014-01-03 15:13:33 -08001855 /* Reset to retry mode 0 */
1856 if (retry_mode) {
1857 ret = nand_setup_read_retry(mtd, 0);
1858 if (ret < 0)
1859 break;
1860 retry_mode = 0;
1861 }
1862
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001863 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001864 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Brian Norris8b6e50c2011-05-25 14:59:01 -07001866 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 col = 0;
1868 /* Increment page address */
1869 realpage++;
1870
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001871 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Check, if we cross a chip boundary */
1873 if (!page) {
1874 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001875 chip->select_chip(mtd, -1);
1876 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001879 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001881 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001882 if (oob)
1883 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Mike Dunn3f91e942012-04-25 12:06:09 -07001885 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001886 return ret;
1887
Brian Norrisb72f3df2013-12-03 11:04:14 -08001888 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001889 return -EBADMSG;
1890
Mike Dunnedbc45402012-04-25 12:06:11 -07001891 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001892}
1893
1894/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001895 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001896 * @mtd: MTD device structure
1897 * @from: offset to read from
1898 * @len: number of bytes to read
1899 * @retlen: pointer to variable to store the number of read bytes
1900 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001901 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001902 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001903 */
1904static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1905 size_t *retlen, uint8_t *buf)
1906{
Brian Norris4a89ff82011-08-30 18:45:45 -07001907 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001908 int ret;
1909
Huang Shijie6a8214a2012-11-19 14:43:30 +08001910 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001911 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001912 ops.len = len;
1913 ops.datbuf = buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08001914 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001915 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001916 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001917 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001918 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
1921/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001922 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001923 * @mtd: mtd info structure
1924 * @chip: nand chip info structure
1925 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001926 */
1927static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001928 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001929{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001930 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001931 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001932 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001933}
1934
1935/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001936 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001937 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001938 * @mtd: mtd info structure
1939 * @chip: nand chip info structure
1940 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001941 */
1942static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001943 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001944{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001945 int length = mtd->oobsize;
1946 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1947 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001948 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001949 int i, toread, sndrnd = 0, pos;
1950
1951 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1952 for (i = 0; i < chip->ecc.steps; i++) {
1953 if (sndrnd) {
1954 pos = eccsize + i * (eccsize + chunk);
1955 if (mtd->writesize > 512)
1956 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1957 else
1958 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1959 } else
1960 sndrnd = 1;
1961 toread = min_t(int, length, chunk);
1962 chip->read_buf(mtd, bufpoi, toread);
1963 bufpoi += toread;
1964 length -= toread;
1965 }
1966 if (length > 0)
1967 chip->read_buf(mtd, bufpoi, length);
1968
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001969 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001970}
1971
1972/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001973 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001974 * @mtd: mtd info structure
1975 * @chip: nand chip info structure
1976 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001977 */
1978static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1979 int page)
1980{
1981 int status = 0;
1982 const uint8_t *buf = chip->oob_poi;
1983 int length = mtd->oobsize;
1984
1985 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1986 chip->write_buf(mtd, buf, length);
1987 /* Send command to program the OOB data */
1988 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1989
1990 status = chip->waitfunc(mtd, chip);
1991
Savin Zlobec0d420f92006-06-21 11:51:20 +02001992 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001993}
1994
1995/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001996 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001997 * with syndrome - only for large page flash
1998 * @mtd: mtd info structure
1999 * @chip: nand chip info structure
2000 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002001 */
2002static int nand_write_oob_syndrome(struct mtd_info *mtd,
2003 struct nand_chip *chip, int page)
2004{
2005 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2006 int eccsize = chip->ecc.size, length = mtd->oobsize;
2007 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2008 const uint8_t *bufpoi = chip->oob_poi;
2009
2010 /*
2011 * data-ecc-data-ecc ... ecc-oob
2012 * or
2013 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2014 */
2015 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2016 pos = steps * (eccsize + chunk);
2017 steps = 0;
2018 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002019 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002020
2021 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2022 for (i = 0; i < steps; i++) {
2023 if (sndcmd) {
2024 if (mtd->writesize <= 512) {
2025 uint32_t fill = 0xFFFFFFFF;
2026
2027 len = eccsize;
2028 while (len > 0) {
2029 int num = min_t(int, len, 4);
2030 chip->write_buf(mtd, (uint8_t *)&fill,
2031 num);
2032 len -= num;
2033 }
2034 } else {
2035 pos = eccsize + i * (eccsize + chunk);
2036 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2037 }
2038 } else
2039 sndcmd = 1;
2040 len = min_t(int, length, chunk);
2041 chip->write_buf(mtd, bufpoi, len);
2042 bufpoi += len;
2043 length -= len;
2044 }
2045 if (length > 0)
2046 chip->write_buf(mtd, bufpoi, length);
2047
2048 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2049 status = chip->waitfunc(mtd, chip);
2050
2051 return status & NAND_STATUS_FAIL ? -EIO : 0;
2052}
2053
2054/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002055 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002056 * @mtd: MTD device structure
2057 * @from: offset to read from
2058 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002060 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002062static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2063 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Brian Norrisc00a0992012-05-01 17:12:54 -07002065 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002066 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002067 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002068 int readlen = ops->ooblen;
2069 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002070 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002071 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Brian Norris289c0522011-07-19 10:06:09 -07002073 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302074 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Brian Norris041e4572011-06-23 16:45:24 -07002076 stats = mtd->ecc_stats;
2077
Brian Norris0612b9d2011-08-30 18:45:40 -07002078 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03002079 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02002080 else
2081 len = mtd->oobsize;
2082
2083 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002084 pr_debug("%s: attempt to start read outside oob\n",
2085 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002086 return -EINVAL;
2087 }
2088
2089 /* Do not allow reads past end of device */
2090 if (unlikely(from >= mtd->size ||
2091 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2092 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002093 pr_debug("%s: attempt to read beyond end of device\n",
2094 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002095 return -EINVAL;
2096 }
Vitaly Wool70145682006-11-03 18:20:38 +03002097
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002098 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002099 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002101 /* Shift to get page */
2102 realpage = (int)(from >> chip->page_shift);
2103 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Florian Fainellif8ac0412010-09-07 13:23:43 +02002105 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002106 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002107 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002108 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002109 ret = chip->ecc.read_oob(mtd, chip, page);
2110
2111 if (ret < 0)
2112 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002113
2114 len = min(len, readlen);
2115 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002116
Brian Norris5bc7c332013-03-13 09:51:31 -07002117 if (chip->options & NAND_NEED_READRDY) {
2118 /* Apply delay or wait for ready/busy pin */
2119 if (!chip->dev_ready)
2120 udelay(chip->chip_delay);
2121 else
2122 nand_wait_ready(mtd);
2123 }
2124
Vitaly Wool70145682006-11-03 18:20:38 +03002125 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002126 if (!readlen)
2127 break;
2128
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002129 /* Increment page address */
2130 realpage++;
2131
2132 page = realpage & chip->pagemask;
2133 /* Check, if we cross a chip boundary */
2134 if (!page) {
2135 chipnr++;
2136 chip->select_chip(mtd, -1);
2137 chip->select_chip(mtd, chipnr);
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002140 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002142 ops->oobretlen = ops->ooblen - readlen;
2143
2144 if (ret < 0)
2145 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002146
2147 if (mtd->ecc_stats.failed - stats.failed)
2148 return -EBADMSG;
2149
2150 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002154 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002155 * @mtd: MTD device structure
2156 * @from: offset to read from
2157 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002159 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002161static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2162 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002164 int ret = -ENOTSUPP;
2165
2166 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002169 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002170 pr_debug("%s: attempt to read beyond end of device\n",
2171 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return -EINVAL;
2173 }
2174
Huang Shijie6a8214a2012-11-19 14:43:30 +08002175 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Florian Fainellif8ac0412010-09-07 13:23:43 +02002177 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002178 case MTD_OPS_PLACE_OOB:
2179 case MTD_OPS_AUTO_OOB:
2180 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002181 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002182
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002183 default:
2184 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002187 if (!ops->datbuf)
2188 ret = nand_do_read_oob(mtd, from, ops);
2189 else
2190 ret = nand_do_read_ops(mtd, from, ops);
2191
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002192out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002194 return ret;
2195}
2196
2197
2198/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002199 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002200 * @mtd: mtd info structure
2201 * @chip: nand chip info structure
2202 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002203 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002204 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002205 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002206 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002207 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002208static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002209 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002210{
2211 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002212 if (oob_required)
2213 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002214
2215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002218/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002219 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002220 * @mtd: mtd info structure
2221 * @chip: nand chip info structure
2222 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002223 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002224 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002225 *
2226 * We need a special oob layout and handling even when ECC isn't checked.
2227 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002228static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002229 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002230 const uint8_t *buf, int oob_required,
2231 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002232{
2233 int eccsize = chip->ecc.size;
2234 int eccbytes = chip->ecc.bytes;
2235 uint8_t *oob = chip->oob_poi;
2236 int steps, size;
2237
2238 for (steps = chip->ecc.steps; steps > 0; steps--) {
2239 chip->write_buf(mtd, buf, eccsize);
2240 buf += eccsize;
2241
2242 if (chip->ecc.prepad) {
2243 chip->write_buf(mtd, oob, chip->ecc.prepad);
2244 oob += chip->ecc.prepad;
2245 }
2246
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002247 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002248 oob += eccbytes;
2249
2250 if (chip->ecc.postpad) {
2251 chip->write_buf(mtd, oob, chip->ecc.postpad);
2252 oob += chip->ecc.postpad;
2253 }
2254 }
2255
2256 size = mtd->oobsize - (oob - chip->oob_poi);
2257 if (size)
2258 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002259
2260 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002261}
2262/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002263 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002264 * @mtd: mtd info structure
2265 * @chip: nand chip info structure
2266 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002267 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002268 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002269 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002270static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002271 const uint8_t *buf, int oob_required,
2272 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002273{
2274 int i, eccsize = chip->ecc.size;
2275 int eccbytes = chip->ecc.bytes;
2276 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002277 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002278 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002279 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002280
Brian Norris7854d3f2011-06-23 14:12:08 -07002281 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002282 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2283 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002284
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002285 for (i = 0; i < chip->ecc.total; i++)
2286 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002287
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002288 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002289}
2290
2291/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002292 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002293 * @mtd: mtd info structure
2294 * @chip: nand chip info structure
2295 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002296 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002297 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002298 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002299static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002300 const uint8_t *buf, int oob_required,
2301 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002302{
2303 int i, eccsize = chip->ecc.size;
2304 int eccbytes = chip->ecc.bytes;
2305 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002306 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002307 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002308 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002309
2310 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2311 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002312 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002313 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2314 }
2315
2316 for (i = 0; i < chip->ecc.total; i++)
2317 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2318
2319 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002320
2321 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002322}
2323
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302324
2325/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002326 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302327 * @mtd: mtd info structure
2328 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002329 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302330 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002331 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302332 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002333 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302334 */
2335static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2336 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002337 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002338 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302339{
2340 uint8_t *oob_buf = chip->oob_poi;
2341 uint8_t *ecc_calc = chip->buffers->ecccalc;
2342 int ecc_size = chip->ecc.size;
2343 int ecc_bytes = chip->ecc.bytes;
2344 int ecc_steps = chip->ecc.steps;
2345 uint32_t *eccpos = chip->ecc.layout->eccpos;
2346 uint32_t start_step = offset / ecc_size;
2347 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2348 int oob_bytes = mtd->oobsize / ecc_steps;
2349 int step, i;
2350
2351 for (step = 0; step < ecc_steps; step++) {
2352 /* configure controller for WRITE access */
2353 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2354
2355 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002356 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302357
2358 /* mask ECC of un-touched subpages by padding 0xFF */
2359 if ((step < start_step) || (step > end_step))
2360 memset(ecc_calc, 0xff, ecc_bytes);
2361 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002362 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302363
2364 /* mask OOB of un-touched subpages by padding 0xFF */
2365 /* if oob_required, preserve OOB metadata of written subpage */
2366 if (!oob_required || (step < start_step) || (step > end_step))
2367 memset(oob_buf, 0xff, oob_bytes);
2368
Brian Norrisd6a950802013-08-08 17:16:36 -07002369 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302370 ecc_calc += ecc_bytes;
2371 oob_buf += oob_bytes;
2372 }
2373
2374 /* copy calculated ECC for whole page to chip->buffer->oob */
2375 /* this include masked-value(0xFF) for unwritten subpages */
2376 ecc_calc = chip->buffers->ecccalc;
2377 for (i = 0; i < chip->ecc.total; i++)
2378 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2379
2380 /* write OOB buffer to NAND device */
2381 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2382
2383 return 0;
2384}
2385
2386
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002387/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002388 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002389 * @mtd: mtd info structure
2390 * @chip: nand chip info structure
2391 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002392 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002393 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002394 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002395 * The hw generator calculates the error syndrome automatically. Therefore we
2396 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002397 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002398static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002399 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002400 const uint8_t *buf, int oob_required,
2401 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002402{
2403 int i, eccsize = chip->ecc.size;
2404 int eccbytes = chip->ecc.bytes;
2405 int eccsteps = chip->ecc.steps;
2406 const uint8_t *p = buf;
2407 uint8_t *oob = chip->oob_poi;
2408
2409 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2410
2411 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2412 chip->write_buf(mtd, p, eccsize);
2413
2414 if (chip->ecc.prepad) {
2415 chip->write_buf(mtd, oob, chip->ecc.prepad);
2416 oob += chip->ecc.prepad;
2417 }
2418
2419 chip->ecc.calculate(mtd, p, oob);
2420 chip->write_buf(mtd, oob, eccbytes);
2421 oob += eccbytes;
2422
2423 if (chip->ecc.postpad) {
2424 chip->write_buf(mtd, oob, chip->ecc.postpad);
2425 oob += chip->ecc.postpad;
2426 }
2427 }
2428
2429 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002430 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002431 if (i)
2432 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002433
2434 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002435}
2436
2437/**
David Woodhouse956e9442006-09-25 17:12:39 +01002438 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002439 * @mtd: MTD device structure
2440 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302441 * @offset: address offset within the page
2442 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002443 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002444 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002445 * @page: page number to write
2446 * @cached: cached programming
2447 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002448 */
2449static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302450 uint32_t offset, int data_len, const uint8_t *buf,
2451 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002452{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302453 int status, subpage;
2454
2455 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2456 chip->ecc.write_subpage)
2457 subpage = offset || (data_len < mtd->writesize);
2458 else
2459 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002460
2461 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2462
David Woodhouse956e9442006-09-25 17:12:39 +01002463 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302464 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002465 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302466 else if (subpage)
2467 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002468 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002469 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002470 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2471 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002472
2473 if (status < 0)
2474 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002475
2476 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002477 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002478 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002479 */
2480 cached = 0;
2481
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002482 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002483
2484 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002485 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002486 /*
2487 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002488 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002489 */
2490 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2491 status = chip->errstat(mtd, chip, FL_WRITING, status,
2492 page);
2493
2494 if (status & NAND_STATUS_FAIL)
2495 return -EIO;
2496 } else {
2497 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002498 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002499 }
2500
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002501 return 0;
2502}
2503
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002504/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002505 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002506 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002507 * @oob: oob data buffer
2508 * @len: oob data write length
2509 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002510 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002511static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2512 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002513{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002514 struct nand_chip *chip = mtd_to_nand(mtd);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002515
2516 /*
2517 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2518 * data from a previous OOB read.
2519 */
2520 memset(chip->oob_poi, 0xff, mtd->oobsize);
2521
Florian Fainellif8ac0412010-09-07 13:23:43 +02002522 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002523
Brian Norris0612b9d2011-08-30 18:45:40 -07002524 case MTD_OPS_PLACE_OOB:
2525 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002526 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2527 return oob + len;
2528
Brian Norris0612b9d2011-08-30 18:45:40 -07002529 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002530 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002531 uint32_t boffs = 0, woffs = ops->ooboffs;
2532 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002533
Florian Fainellif8ac0412010-09-07 13:23:43 +02002534 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002535 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002536 if (unlikely(woffs)) {
2537 if (woffs >= free->length) {
2538 woffs -= free->length;
2539 continue;
2540 }
2541 boffs = free->offset + woffs;
2542 bytes = min_t(size_t, len,
2543 (free->length - woffs));
2544 woffs = 0;
2545 } else {
2546 bytes = min_t(size_t, len, free->length);
2547 boffs = free->offset;
2548 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002549 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002550 oob += bytes;
2551 }
2552 return oob;
2553 }
2554 default:
2555 BUG();
2556 }
2557 return NULL;
2558}
2559
Florian Fainellif8ac0412010-09-07 13:23:43 +02002560#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002561
2562/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002563 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002564 * @mtd: MTD device structure
2565 * @to: offset to write to
2566 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002567 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002568 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002569 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002570static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2571 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002572{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002573 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002574 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002575 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002576
2577 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002578 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002579 mtd->oobavail : mtd->oobsize;
2580
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002581 uint8_t *oob = ops->oobbuf;
2582 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302583 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002584 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002585
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002586 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002587 if (!writelen)
2588 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002589
Brian Norris8b6e50c2011-05-25 14:59:01 -07002590 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002591 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002592 pr_notice("%s: attempt to write non page aligned data\n",
2593 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002594 return -EINVAL;
2595 }
2596
Thomas Gleixner29072b92006-09-28 15:38:36 +02002597 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002598
Thomas Gleixner6a930962006-06-28 00:11:45 +02002599 chipnr = (int)(to >> chip->chip_shift);
2600 chip->select_chip(mtd, chipnr);
2601
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002602 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002603 if (nand_check_wp(mtd)) {
2604 ret = -EIO;
2605 goto err_out;
2606 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002607
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002608 realpage = (int)(to >> chip->page_shift);
2609 page = realpage & chip->pagemask;
2610 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2611
2612 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002613 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2614 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002615 chip->pagebuf = -1;
2616
Maxim Levitsky782ce792010-02-22 20:39:36 +02002617 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002618 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2619 ret = -EINVAL;
2620 goto err_out;
2621 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002622
Florian Fainellif8ac0412010-09-07 13:23:43 +02002623 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002624 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002625 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002626 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002627 int use_bufpoi;
2628 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002629
Kamal Dasu66507c72014-05-01 20:51:19 -04002630 if (part_pagewr)
2631 use_bufpoi = 1;
2632 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2633 use_bufpoi = !virt_addr_valid(buf);
2634 else
2635 use_bufpoi = 0;
2636
2637 /* Partial page write?, or need to use bounce buffer */
2638 if (use_bufpoi) {
2639 pr_debug("%s: using write bounce buffer for buf@%p\n",
2640 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002641 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002642 if (part_pagewr)
2643 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002644 chip->pagebuf = -1;
2645 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2646 memcpy(&chip->buffers->databuf[column], buf, bytes);
2647 wbuf = chip->buffers->databuf;
2648 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002649
Maxim Levitsky782ce792010-02-22 20:39:36 +02002650 if (unlikely(oob)) {
2651 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002652 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002653 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002654 } else {
2655 /* We still need to erase leftover OOB data */
2656 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002657 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302658 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2659 oob_required, page, cached,
2660 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002661 if (ret)
2662 break;
2663
2664 writelen -= bytes;
2665 if (!writelen)
2666 break;
2667
Thomas Gleixner29072b92006-09-28 15:38:36 +02002668 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002669 buf += bytes;
2670 realpage++;
2671
2672 page = realpage & chip->pagemask;
2673 /* Check, if we cross a chip boundary */
2674 if (!page) {
2675 chipnr++;
2676 chip->select_chip(mtd, -1);
2677 chip->select_chip(mtd, chipnr);
2678 }
2679 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002680
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002681 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002682 if (unlikely(oob))
2683 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002684
2685err_out:
2686 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002687 return ret;
2688}
2689
2690/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002691 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002692 * @mtd: MTD device structure
2693 * @to: offset to write to
2694 * @len: number of bytes to write
2695 * @retlen: pointer to variable to store the number of written bytes
2696 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002697 *
2698 * NAND write with ECC. Used when performing writes in interrupt context, this
2699 * may for example be called by mtdoops when writing an oops while in panic.
2700 */
2701static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2702 size_t *retlen, const uint8_t *buf)
2703{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002704 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002705 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002706 int ret;
2707
Brian Norris8b6e50c2011-05-25 14:59:01 -07002708 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002709 panic_nand_wait(mtd, chip, 400);
2710
Brian Norris8b6e50c2011-05-25 14:59:01 -07002711 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002712 panic_nand_get_device(chip, mtd, FL_WRITING);
2713
Brian Norris0ec56dc2015-02-28 02:02:30 -08002714 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002715 ops.len = len;
2716 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002717 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002718
Brian Norris4a89ff82011-08-30 18:45:45 -07002719 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002720
Brian Norris4a89ff82011-08-30 18:45:45 -07002721 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002722 return ret;
2723}
2724
2725/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002726 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002727 * @mtd: MTD device structure
2728 * @to: offset to write to
2729 * @len: number of bytes to write
2730 * @retlen: pointer to variable to store the number of written bytes
2731 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002733 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002735static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002736 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Brian Norris4a89ff82011-08-30 18:45:45 -07002738 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002739 int ret;
2740
Huang Shijie6a8214a2012-11-19 14:43:30 +08002741 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002742 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002743 ops.len = len;
2744 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002745 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002746 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002747 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002748 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002749 return ret;
2750}
2751
2752/**
2753 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002754 * @mtd: MTD device structure
2755 * @to: offset to write to
2756 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002757 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002758 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002759 */
2760static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2761 struct mtd_oob_ops *ops)
2762{
Adrian Hunter03736152007-01-31 17:58:29 +02002763 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002764 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Brian Norris289c0522011-07-19 10:06:09 -07002766 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302767 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Brian Norris0612b9d2011-08-30 18:45:40 -07002769 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002770 len = chip->ecc.layout->oobavail;
2771 else
2772 len = mtd->oobsize;
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002775 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002776 pr_debug("%s: attempt to write past end of page\n",
2777 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return -EINVAL;
2779 }
2780
Adrian Hunter03736152007-01-31 17:58:29 +02002781 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002782 pr_debug("%s: attempt to start write outside oob\n",
2783 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002784 return -EINVAL;
2785 }
2786
Jason Liu775adc32011-02-25 13:06:18 +08002787 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002788 if (unlikely(to >= mtd->size ||
2789 ops->ooboffs + ops->ooblen >
2790 ((mtd->size >> chip->page_shift) -
2791 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002792 pr_debug("%s: attempt to write beyond end of device\n",
2793 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002794 return -EINVAL;
2795 }
2796
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002797 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002798 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002800 /* Shift to get page */
2801 page = (int)(to >> chip->page_shift);
2802
2803 /*
2804 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2805 * of my DiskOnChip 2000 test units) will clear the whole data page too
2806 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2807 * it in the doc2000 driver in August 1999. dwmw2.
2808 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002809 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002812 if (nand_check_wp(mtd)) {
2813 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002814 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002815 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002818 if (page == chip->pagebuf)
2819 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002821 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002822
Brian Norris0612b9d2011-08-30 18:45:40 -07002823 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002824 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2825 else
2826 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002827
Huang Shijieb0bb6902012-11-19 14:43:29 +08002828 chip->select_chip(mtd, -1);
2829
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002830 if (status)
2831 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Vitaly Wool70145682006-11-03 18:20:38 +03002833 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002835 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002836}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002838/**
2839 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002840 * @mtd: MTD device structure
2841 * @to: offset to write to
2842 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002843 */
2844static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2845 struct mtd_oob_ops *ops)
2846{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002847 int ret = -ENOTSUPP;
2848
2849 ops->retlen = 0;
2850
2851 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002852 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002853 pr_debug("%s: attempt to write beyond end of device\n",
2854 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002855 return -EINVAL;
2856 }
2857
Huang Shijie6a8214a2012-11-19 14:43:30 +08002858 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002859
Florian Fainellif8ac0412010-09-07 13:23:43 +02002860 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002861 case MTD_OPS_PLACE_OOB:
2862 case MTD_OPS_AUTO_OOB:
2863 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002864 break;
2865
2866 default:
2867 goto out;
2868 }
2869
2870 if (!ops->datbuf)
2871 ret = nand_do_write_oob(mtd, to, ops);
2872 else
2873 ret = nand_do_write_ops(mtd, to, ops);
2874
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002875out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002876 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return ret;
2878}
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880/**
Brian Norris49c50b92014-05-06 16:02:19 -07002881 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002882 * @mtd: MTD device structure
2883 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 *
Brian Norris49c50b92014-05-06 16:02:19 -07002885 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 */
Brian Norris49c50b92014-05-06 16:02:19 -07002887static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002889 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002891 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2892 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002893
2894 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
2897/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002899 * @mtd: MTD device structure
2900 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002902 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002904static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
David Woodhousee0c7d762006-05-13 18:07:53 +01002906 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002910 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002911 * @mtd: MTD device structure
2912 * @instr: erase instruction
2913 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002915 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002917int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2918 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Adrian Hunter69423d92008-12-10 13:37:21 +00002920 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002921 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00002922 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Brian Norris289c0522011-07-19 10:06:09 -07002924 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2925 __func__, (unsigned long long)instr->addr,
2926 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302928 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002932 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002935 page = (int)(instr->addr >> chip->page_shift);
2936 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002939 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002942 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /* Check, if it is write protected */
2945 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002946 pr_debug("%s: device is write protected!\n",
2947 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 instr->state = MTD_ERASE_FAILED;
2949 goto erase_exit;
2950 }
2951
2952 /* Loop through the pages */
2953 len = instr->len;
2954
2955 instr->state = MTD_ERASING;
2956
2957 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002958 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002959 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2960 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002961 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2962 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 instr->state = MTD_ERASE_FAILED;
2964 goto erase_exit;
2965 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002966
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002967 /*
2968 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002969 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002970 */
2971 if (page <= chip->pagebuf && chip->pagebuf <
2972 (page + pages_per_block))
2973 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Brian Norris49c50b92014-05-06 16:02:19 -07002975 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002977 /*
2978 * See if operation failed and additional status checks are
2979 * available
2980 */
2981 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2982 status = chip->errstat(mtd, chip, FL_ERASING,
2983 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002986 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002987 pr_debug("%s: failed erase, page 0x%08x\n",
2988 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002990 instr->fail_addr =
2991 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 goto erase_exit;
2993 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002996 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 page += pages_per_block;
2998
2999 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003000 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003002 chip->select_chip(mtd, -1);
3003 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 }
3006 instr->state = MTD_ERASE_DONE;
3007
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003008erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003013 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 nand_release_device(mtd);
3015
David Woodhouse49defc02007-10-06 15:01:59 -04003016 /* Do call back function */
3017 if (!ret)
3018 mtd_erase_callback(instr);
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 /* Return more or less happy */
3021 return ret;
3022}
3023
3024/**
3025 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003026 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003028 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003030static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Brian Norris289c0522011-07-19 10:06:09 -07003032 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003035 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003037 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003041 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003042 * @mtd: MTD device structure
3043 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003045static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003047 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
3050/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003051 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003052 * @mtd: MTD device structure
3053 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003055static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 int ret;
3058
Florian Fainellif8ac0412010-09-07 13:23:43 +02003059 ret = nand_block_isbad(mtd, ofs);
3060 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003061 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 if (ret > 0)
3063 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003064 return ret;
3065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Brian Norris5a0edb22013-07-30 17:52:58 -07003067 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003071 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3072 * @mtd: MTD device structure
3073 * @chip: nand chip info structure
3074 * @addr: feature address.
3075 * @subfeature_param: the subfeature parameters, a four bytes array.
3076 */
3077static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3078 int addr, uint8_t *subfeature_param)
3079{
3080 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003081 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003082
David Mosbergerd914c932013-05-29 15:30:13 +03003083 if (!chip->onfi_version ||
3084 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3085 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003086 return -EINVAL;
3087
3088 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003089 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3090 chip->write_byte(mtd, subfeature_param[i]);
3091
Huang Shijie7db03ec2012-09-13 14:57:52 +08003092 status = chip->waitfunc(mtd, chip);
3093 if (status & NAND_STATUS_FAIL)
3094 return -EIO;
3095 return 0;
3096}
3097
3098/**
3099 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3100 * @mtd: MTD device structure
3101 * @chip: nand chip info structure
3102 * @addr: feature address.
3103 * @subfeature_param: the subfeature parameters, a four bytes array.
3104 */
3105static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3106 int addr, uint8_t *subfeature_param)
3107{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003108 int i;
3109
David Mosbergerd914c932013-05-29 15:30:13 +03003110 if (!chip->onfi_version ||
3111 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3112 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003113 return -EINVAL;
3114
Huang Shijie7db03ec2012-09-13 14:57:52 +08003115 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003116 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3117 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003118 return 0;
3119}
3120
3121/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003122 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003123 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003124 */
3125static int nand_suspend(struct mtd_info *mtd)
3126{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003127 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003128}
3129
3130/**
3131 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003132 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003133 */
3134static void nand_resume(struct mtd_info *mtd)
3135{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003136 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003137
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003138 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003139 nand_release_device(mtd);
3140 else
Brian Norrisd0370212011-07-19 10:06:08 -07003141 pr_err("%s called for a chip which is not in suspended state\n",
3142 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003143}
3144
Scott Branden72ea4032014-11-20 11:18:05 -08003145/**
3146 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3147 * prevent further operations
3148 * @mtd: MTD device structure
3149 */
3150static void nand_shutdown(struct mtd_info *mtd)
3151{
Brian Norris9ca641b2015-11-09 16:37:28 -08003152 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003153}
3154
Brian Norris8b6e50c2011-05-25 14:59:01 -07003155/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003156static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003159 if (!chip->chip_delay)
3160 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003163 if (chip->cmdfunc == NULL)
3164 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003167 if (chip->waitfunc == NULL)
3168 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003170 if (!chip->select_chip)
3171 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003172
Huang Shijie4204ccc2013-08-16 10:10:07 +08003173 /* set for ONFI nand */
3174 if (!chip->onfi_set_features)
3175 chip->onfi_set_features = nand_onfi_set_features;
3176 if (!chip->onfi_get_features)
3177 chip->onfi_get_features = nand_onfi_get_features;
3178
Brian Norris68e80782013-07-18 01:17:02 -07003179 /* If called twice, pointers that depend on busw may need to be reset */
3180 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003181 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3182 if (!chip->read_word)
3183 chip->read_word = nand_read_word;
3184 if (!chip->block_bad)
3185 chip->block_bad = nand_block_bad;
3186 if (!chip->block_markbad)
3187 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003188 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003189 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003190 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3191 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003192 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003193 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003194 if (!chip->scan_bbt)
3195 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003196
3197 if (!chip->controller) {
3198 chip->controller = &chip->hwcontrol;
3199 spin_lock_init(&chip->controller->lock);
3200 init_waitqueue_head(&chip->controller->wq);
3201 }
3202
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003203}
3204
Brian Norris8b6e50c2011-05-25 14:59:01 -07003205/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003206static void sanitize_string(uint8_t *s, size_t len)
3207{
3208 ssize_t i;
3209
Brian Norris8b6e50c2011-05-25 14:59:01 -07003210 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003211 s[len - 1] = 0;
3212
Brian Norris8b6e50c2011-05-25 14:59:01 -07003213 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003214 for (i = 0; i < len - 1; i++) {
3215 if (s[i] < ' ' || s[i] > 127)
3216 s[i] = '?';
3217 }
3218
Brian Norris8b6e50c2011-05-25 14:59:01 -07003219 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003220 strim(s);
3221}
3222
3223static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3224{
3225 int i;
3226 while (len--) {
3227 crc ^= *p++ << 8;
3228 for (i = 0; i < 8; i++)
3229 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3230 }
3231
3232 return crc;
3233}
3234
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003235/* Parse the Extended Parameter Page. */
3236static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3237 struct nand_chip *chip, struct nand_onfi_params *p)
3238{
3239 struct onfi_ext_param_page *ep;
3240 struct onfi_ext_section *s;
3241 struct onfi_ext_ecc_info *ecc;
3242 uint8_t *cursor;
3243 int ret = -EINVAL;
3244 int len;
3245 int i;
3246
3247 len = le16_to_cpu(p->ext_param_page_length) * 16;
3248 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003249 if (!ep)
3250 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003251
3252 /* Send our own NAND_CMD_PARAM. */
3253 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3254
3255 /* Use the Change Read Column command to skip the ONFI param pages. */
3256 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3257 sizeof(*p) * p->num_of_param_pages , -1);
3258
3259 /* Read out the Extended Parameter Page. */
3260 chip->read_buf(mtd, (uint8_t *)ep, len);
3261 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3262 != le16_to_cpu(ep->crc))) {
3263 pr_debug("fail in the CRC.\n");
3264 goto ext_out;
3265 }
3266
3267 /*
3268 * Check the signature.
3269 * Do not strictly follow the ONFI spec, maybe changed in future.
3270 */
3271 if (strncmp(ep->sig, "EPPS", 4)) {
3272 pr_debug("The signature is invalid.\n");
3273 goto ext_out;
3274 }
3275
3276 /* find the ECC section. */
3277 cursor = (uint8_t *)(ep + 1);
3278 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3279 s = ep->sections + i;
3280 if (s->type == ONFI_SECTION_TYPE_2)
3281 break;
3282 cursor += s->length * 16;
3283 }
3284 if (i == ONFI_EXT_SECTION_MAX) {
3285 pr_debug("We can not find the ECC section.\n");
3286 goto ext_out;
3287 }
3288
3289 /* get the info we want. */
3290 ecc = (struct onfi_ext_ecc_info *)cursor;
3291
Brian Norris4ae7d222013-09-16 18:20:21 -07003292 if (!ecc->codeword_size) {
3293 pr_debug("Invalid codeword size\n");
3294 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003295 }
3296
Brian Norris4ae7d222013-09-16 18:20:21 -07003297 chip->ecc_strength_ds = ecc->ecc_bits;
3298 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003299 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003300
3301ext_out:
3302 kfree(ep);
3303 return ret;
3304}
3305
Brian Norris8429bb32013-12-03 15:51:09 -08003306static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3307{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003308 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003309 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3310
3311 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3312 feature);
3313}
3314
3315/*
3316 * Configure chip properties from Micron vendor-specific ONFI table
3317 */
3318static void nand_onfi_detect_micron(struct nand_chip *chip,
3319 struct nand_onfi_params *p)
3320{
3321 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3322
3323 if (le16_to_cpu(p->vendor_revision) < 1)
3324 return;
3325
3326 chip->read_retries = micron->read_retry_options;
3327 chip->setup_read_retry = nand_setup_read_retry_micron;
3328}
3329
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003330/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003331 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003332 */
3333static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003334 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003335{
3336 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003337 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003338 int val;
3339
Brian Norris7854d3f2011-06-23 14:12:08 -07003340 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003341 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3342 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3343 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3344 return 0;
3345
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003346 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3347 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003348 for (j = 0; j < sizeof(*p); j++)
3349 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003350 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3351 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003352 break;
3353 }
3354 }
3355
Brian Norrisc7f23a72013-08-13 10:51:55 -07003356 if (i == 3) {
3357 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003358 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003359 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003360
Brian Norris8b6e50c2011-05-25 14:59:01 -07003361 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003362 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003363 if (val & (1 << 5))
3364 chip->onfi_version = 23;
3365 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003366 chip->onfi_version = 22;
3367 else if (val & (1 << 3))
3368 chip->onfi_version = 21;
3369 else if (val & (1 << 2))
3370 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003371 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003372 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003373
3374 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003375 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003376 return 0;
3377 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003378
3379 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3380 sanitize_string(p->model, sizeof(p->model));
3381 if (!mtd->name)
3382 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003383
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003384 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003385
3386 /*
3387 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3388 * (don't ask me who thought of this...). MTD assumes that these
3389 * dimensions will be power-of-2, so just truncate the remaining area.
3390 */
3391 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3392 mtd->erasesize *= mtd->writesize;
3393
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003394 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003395
3396 /* See erasesize comment */
3397 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003398 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003399 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003400
3401 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003402 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003403 else
3404 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003405
Huang Shijie10c86ba2013-05-17 11:17:26 +08003406 if (p->ecc_bits != 0xff) {
3407 chip->ecc_strength_ds = p->ecc_bits;
3408 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003409 } else if (chip->onfi_version >= 21 &&
3410 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3411
3412 /*
3413 * The nand_flash_detect_ext_param_page() uses the
3414 * Change Read Column command which maybe not supported
3415 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3416 * now. We do not replace user supplied command function.
3417 */
3418 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3419 chip->cmdfunc = nand_command_lp;
3420
3421 /* The Extended Parameter Page is supported since ONFI 2.1. */
3422 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003423 pr_warn("Failed to detect ONFI extended param page\n");
3424 } else {
3425 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003426 }
3427
Brian Norris8429bb32013-12-03 15:51:09 -08003428 if (p->jedec_id == NAND_MFR_MICRON)
3429 nand_onfi_detect_micron(chip, p);
3430
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003431 return 1;
3432}
3433
3434/*
Huang Shijie91361812014-02-21 13:39:40 +08003435 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3436 */
3437static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3438 int *busw)
3439{
3440 struct nand_jedec_params *p = &chip->jedec_params;
3441 struct jedec_ecc_info *ecc;
3442 int val;
3443 int i, j;
3444
3445 /* Try JEDEC for unknown chip or LP */
3446 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3447 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3448 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3449 chip->read_byte(mtd) != 'C')
3450 return 0;
3451
3452 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3453 for (i = 0; i < 3; i++) {
3454 for (j = 0; j < sizeof(*p); j++)
3455 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3456
3457 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3458 le16_to_cpu(p->crc))
3459 break;
3460 }
3461
3462 if (i == 3) {
3463 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3464 return 0;
3465 }
3466
3467 /* Check version */
3468 val = le16_to_cpu(p->revision);
3469 if (val & (1 << 2))
3470 chip->jedec_version = 10;
3471 else if (val & (1 << 1))
3472 chip->jedec_version = 1; /* vendor specific version */
3473
3474 if (!chip->jedec_version) {
3475 pr_info("unsupported JEDEC version: %d\n", val);
3476 return 0;
3477 }
3478
3479 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3480 sanitize_string(p->model, sizeof(p->model));
3481 if (!mtd->name)
3482 mtd->name = p->model;
3483
3484 mtd->writesize = le32_to_cpu(p->byte_per_page);
3485
3486 /* Please reference to the comment for nand_flash_detect_onfi. */
3487 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3488 mtd->erasesize *= mtd->writesize;
3489
3490 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3491
3492 /* Please reference to the comment for nand_flash_detect_onfi. */
3493 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3494 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3495 chip->bits_per_cell = p->bits_per_cell;
3496
3497 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3498 *busw = NAND_BUSWIDTH_16;
3499 else
3500 *busw = 0;
3501
3502 /* ECC info */
3503 ecc = &p->ecc_info[0];
3504
3505 if (ecc->codeword_size >= 9) {
3506 chip->ecc_strength_ds = ecc->ecc_bits;
3507 chip->ecc_step_ds = 1 << ecc->codeword_size;
3508 } else {
3509 pr_warn("Invalid codeword size\n");
3510 }
3511
3512 return 1;
3513}
3514
3515/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003516 * nand_id_has_period - Check if an ID string has a given wraparound period
3517 * @id_data: the ID string
3518 * @arrlen: the length of the @id_data array
3519 * @period: the period of repitition
3520 *
3521 * Check if an ID string is repeated within a given sequence of bytes at
3522 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003523 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003524 * if the repetition has a period of @period; otherwise, returns zero.
3525 */
3526static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3527{
3528 int i, j;
3529 for (i = 0; i < period; i++)
3530 for (j = i + period; j < arrlen; j += period)
3531 if (id_data[i] != id_data[j])
3532 return 0;
3533 return 1;
3534}
3535
3536/*
3537 * nand_id_len - Get the length of an ID string returned by CMD_READID
3538 * @id_data: the ID string
3539 * @arrlen: the length of the @id_data array
3540
3541 * Returns the length of the ID string, according to known wraparound/trailing
3542 * zero patterns. If no pattern exists, returns the length of the array.
3543 */
3544static int nand_id_len(u8 *id_data, int arrlen)
3545{
3546 int last_nonzero, period;
3547
3548 /* Find last non-zero byte */
3549 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3550 if (id_data[last_nonzero])
3551 break;
3552
3553 /* All zeros */
3554 if (last_nonzero < 0)
3555 return 0;
3556
3557 /* Calculate wraparound period */
3558 for (period = 1; period < arrlen; period++)
3559 if (nand_id_has_period(id_data, arrlen, period))
3560 break;
3561
3562 /* There's a repeated pattern */
3563 if (period < arrlen)
3564 return period;
3565
3566 /* There are trailing zeros */
3567 if (last_nonzero < arrlen - 1)
3568 return last_nonzero + 1;
3569
3570 /* No pattern detected */
3571 return arrlen;
3572}
3573
Huang Shijie7db906b2013-09-25 14:58:11 +08003574/* Extract the bits of per cell from the 3rd byte of the extended ID */
3575static int nand_get_bits_per_cell(u8 cellinfo)
3576{
3577 int bits;
3578
3579 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3580 bits >>= NAND_CI_CELLTYPE_SHIFT;
3581 return bits + 1;
3582}
3583
Brian Norrise3b88bd2012-09-24 20:40:52 -07003584/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003585 * Many new NAND share similar device ID codes, which represent the size of the
3586 * chip. The rest of the parameters must be decoded according to generic or
3587 * manufacturer-specific "extended ID" decoding patterns.
3588 */
3589static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3590 u8 id_data[8], int *busw)
3591{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003592 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003593 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003594 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003595 /* The 4th id byte is the important one */
3596 extid = id_data[3];
3597
Brian Norrise3b88bd2012-09-24 20:40:52 -07003598 id_len = nand_id_len(id_data, 8);
3599
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003600 /*
3601 * Field definitions are in the following datasheets:
3602 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003603 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003604 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003605 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003606 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3607 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003608 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003609 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003610 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003611 /* Calc pagesize */
3612 mtd->writesize = 2048 << (extid & 0x03);
3613 extid >>= 2;
3614 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003615 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003616 case 1:
3617 mtd->oobsize = 128;
3618 break;
3619 case 2:
3620 mtd->oobsize = 218;
3621 break;
3622 case 3:
3623 mtd->oobsize = 400;
3624 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003625 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003626 mtd->oobsize = 436;
3627 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003628 case 5:
3629 mtd->oobsize = 512;
3630 break;
3631 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003632 mtd->oobsize = 640;
3633 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003634 case 7:
3635 default: /* Other cases are "reserved" (unknown) */
3636 mtd->oobsize = 1024;
3637 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003638 }
3639 extid >>= 2;
3640 /* Calc blocksize */
3641 mtd->erasesize = (128 * 1024) <<
3642 (((extid >> 1) & 0x04) | (extid & 0x03));
3643 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003644 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003645 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003646 unsigned int tmp;
3647
3648 /* Calc pagesize */
3649 mtd->writesize = 2048 << (extid & 0x03);
3650 extid >>= 2;
3651 /* Calc oobsize */
3652 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3653 case 0:
3654 mtd->oobsize = 128;
3655 break;
3656 case 1:
3657 mtd->oobsize = 224;
3658 break;
3659 case 2:
3660 mtd->oobsize = 448;
3661 break;
3662 case 3:
3663 mtd->oobsize = 64;
3664 break;
3665 case 4:
3666 mtd->oobsize = 32;
3667 break;
3668 case 5:
3669 mtd->oobsize = 16;
3670 break;
3671 default:
3672 mtd->oobsize = 640;
3673 break;
3674 }
3675 extid >>= 2;
3676 /* Calc blocksize */
3677 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3678 if (tmp < 0x03)
3679 mtd->erasesize = (128 * 1024) << tmp;
3680 else if (tmp == 0x03)
3681 mtd->erasesize = 768 * 1024;
3682 else
3683 mtd->erasesize = (64 * 1024) << tmp;
3684 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003685 } else {
3686 /* Calc pagesize */
3687 mtd->writesize = 1024 << (extid & 0x03);
3688 extid >>= 2;
3689 /* Calc oobsize */
3690 mtd->oobsize = (8 << (extid & 0x01)) *
3691 (mtd->writesize >> 9);
3692 extid >>= 2;
3693 /* Calc blocksize. Blocksize is multiples of 64KiB */
3694 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3695 extid >>= 2;
3696 /* Get buswidth information */
3697 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003698
3699 /*
3700 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3701 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3702 * follows:
3703 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3704 * 110b -> 24nm
3705 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3706 */
3707 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003708 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003709 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3710 !(id_data[4] & 0x80) /* !BENAND */) {
3711 mtd->oobsize = 32 * mtd->writesize >> 9;
3712 }
3713
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003714 }
3715}
3716
3717/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003718 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3719 * decodes a matching ID table entry and assigns the MTD size parameters for
3720 * the chip.
3721 */
3722static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3723 struct nand_flash_dev *type, u8 id_data[8],
3724 int *busw)
3725{
3726 int maf_id = id_data[0];
3727
3728 mtd->erasesize = type->erasesize;
3729 mtd->writesize = type->pagesize;
3730 mtd->oobsize = mtd->writesize / 32;
3731 *busw = type->options & NAND_BUSWIDTH_16;
3732
Huang Shijie1c195e92013-09-25 14:58:12 +08003733 /* All legacy ID NAND are small-page, SLC */
3734 chip->bits_per_cell = 1;
3735
Brian Norrisf23a4812012-09-24 20:40:51 -07003736 /*
3737 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3738 * some Spansion chips have erasesize that conflicts with size
3739 * listed in nand_ids table.
3740 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3741 */
3742 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3743 && id_data[6] == 0x00 && id_data[7] == 0x00
3744 && mtd->writesize == 512) {
3745 mtd->erasesize = 128 * 1024;
3746 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3747 }
3748}
3749
3750/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003751 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3752 * heuristic patterns using various detected parameters (e.g., manufacturer,
3753 * page size, cell-type information).
3754 */
3755static void nand_decode_bbm_options(struct mtd_info *mtd,
3756 struct nand_chip *chip, u8 id_data[8])
3757{
3758 int maf_id = id_data[0];
3759
3760 /* Set the bad block position */
3761 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3762 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3763 else
3764 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3765
3766 /*
3767 * Bad block marker is stored in the last page of each block on Samsung
3768 * and Hynix MLC devices; stored in first two pages of each block on
3769 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3770 * AMD/Spansion, and Macronix. All others scan only the first page.
3771 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003772 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003773 (maf_id == NAND_MFR_SAMSUNG ||
3774 maf_id == NAND_MFR_HYNIX))
3775 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003776 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003777 (maf_id == NAND_MFR_SAMSUNG ||
3778 maf_id == NAND_MFR_HYNIX ||
3779 maf_id == NAND_MFR_TOSHIBA ||
3780 maf_id == NAND_MFR_AMD ||
3781 maf_id == NAND_MFR_MACRONIX)) ||
3782 (mtd->writesize == 2048 &&
3783 maf_id == NAND_MFR_MICRON))
3784 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3785}
3786
Huang Shijieec6e87e2013-03-15 11:01:00 +08003787static inline bool is_full_id_nand(struct nand_flash_dev *type)
3788{
3789 return type->id_len;
3790}
3791
3792static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3793 struct nand_flash_dev *type, u8 *id_data, int *busw)
3794{
3795 if (!strncmp(type->id, id_data, type->id_len)) {
3796 mtd->writesize = type->pagesize;
3797 mtd->erasesize = type->erasesize;
3798 mtd->oobsize = type->oobsize;
3799
Huang Shijie7db906b2013-09-25 14:58:11 +08003800 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003801 chip->chipsize = (uint64_t)type->chipsize << 20;
3802 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003803 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3804 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003805 chip->onfi_timing_mode_default =
3806 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003807
3808 *busw = type->options & NAND_BUSWIDTH_16;
3809
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003810 if (!mtd->name)
3811 mtd->name = type->name;
3812
Huang Shijieec6e87e2013-03-15 11:01:00 +08003813 return true;
3814 }
3815 return false;
3816}
3817
Brian Norris7e74c2d2012-09-24 20:40:49 -07003818/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003819 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003820 */
3821static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003822 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003823 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003824 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003825{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003826 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003827 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003828 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003831 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Karl Beldanef89a882008-09-15 14:37:29 +02003833 /*
3834 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003835 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003836 */
3837 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003840 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003843 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003844 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Brian Norris8b6e50c2011-05-25 14:59:01 -07003846 /*
3847 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003848 * interface concerns can cause random data which looks like a
3849 * possibly credible NAND flash to appear. If the two results do
3850 * not match, ignore the device completely.
3851 */
3852
3853 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3854
Brian Norris4aef9b72012-09-24 20:40:48 -07003855 /* Read entire ID string */
3856 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003857 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003858
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003859 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003860 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003861 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003862 return ERR_PTR(-ENODEV);
3863 }
3864
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003865 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003866 type = nand_flash_ids;
3867
Huang Shijieec6e87e2013-03-15 11:01:00 +08003868 for (; type->name != NULL; type++) {
3869 if (is_full_id_nand(type)) {
3870 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3871 goto ident_done;
3872 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003873 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003874 }
3875 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003876
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003877 chip->onfi_version = 0;
3878 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003879 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003880 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003881 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003882
3883 /* Check if the chip is JEDEC compliant */
3884 if (nand_flash_detect_jedec(mtd, chip, &busw))
3885 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003886 }
3887
David Woodhouse5e81e882010-02-26 18:32:56 +00003888 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003889 return ERR_PTR(-ENODEV);
3890
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003891 if (!mtd->name)
3892 mtd->name = type->name;
3893
Adrian Hunter69423d92008-12-10 13:37:21 +00003894 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003895
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02003896 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003897 /* Decode parameters from extended ID */
3898 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003899 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003900 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003901 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003902 /* Get chip options */
3903 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003904
Brian Norris8b6e50c2011-05-25 14:59:01 -07003905 /*
3906 * Check if chip is not a Samsung device. Do not clear the
3907 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003908 */
3909 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3910 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3911ident_done:
3912
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003913 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003914 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003915 if (nand_manuf_ids[maf_idx].id == *maf_id)
3916 break;
3917 }
3918
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003919 if (chip->options & NAND_BUSWIDTH_AUTO) {
3920 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3921 chip->options |= busw;
3922 nand_set_defaults(chip, busw);
3923 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3924 /*
3925 * Check, if buswidth is correct. Hardware drivers should set
3926 * chip correct!
3927 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003928 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3929 *maf_id, *dev_id);
3930 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3931 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003932 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3933 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003934 return ERR_PTR(-EINVAL);
3935 }
3936
Brian Norris7e74c2d2012-09-24 20:40:49 -07003937 nand_decode_bbm_options(mtd, chip, id_data);
3938
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003939 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003940 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003941 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003942 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003943
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003944 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003945 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003946 if (chip->chipsize & 0xffffffff)
3947 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003948 else {
3949 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3950 chip->chip_shift += 32 - 1;
3951 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003952
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003953 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003954 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003955
Brian Norris8b6e50c2011-05-25 14:59:01 -07003956 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003957 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3958 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003959
Ezequiel Garcia20171642013-11-25 08:30:31 -03003960 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3961 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003962
3963 if (chip->onfi_version)
3964 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3965 chip->onfi_params.model);
3966 else if (chip->jedec_version)
3967 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3968 chip->jedec_params.model);
3969 else
3970 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3971 type->name);
3972
Rafał Miłecki3755a992014-10-21 00:01:04 +02003973 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003974 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003975 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003976 return type;
3977}
3978
Boris BREZILLON7194a292015-12-10 09:00:37 +01003979static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08003980{
Boris BREZILLON7194a292015-12-10 09:00:37 +01003981 struct device_node *dn = nand_get_flash_node(chip);
Brian Norris5844fee2015-01-23 00:22:27 -08003982 int ecc_mode, ecc_strength, ecc_step;
3983
Boris BREZILLON7194a292015-12-10 09:00:37 +01003984 if (!dn)
3985 return 0;
3986
Brian Norris5844fee2015-01-23 00:22:27 -08003987 if (of_get_nand_bus_width(dn) == 16)
3988 chip->options |= NAND_BUSWIDTH_16;
3989
3990 if (of_get_nand_on_flash_bbt(dn))
3991 chip->bbt_options |= NAND_BBT_USE_FLASH;
3992
3993 ecc_mode = of_get_nand_ecc_mode(dn);
3994 ecc_strength = of_get_nand_ecc_strength(dn);
3995 ecc_step = of_get_nand_ecc_step_size(dn);
3996
3997 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3998 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3999 pr_err("must set both strength and step size in DT\n");
4000 return -EINVAL;
4001 }
4002
4003 if (ecc_mode >= 0)
4004 chip->ecc.mode = ecc_mode;
4005
4006 if (ecc_strength >= 0)
4007 chip->ecc.strength = ecc_strength;
4008
4009 if (ecc_step > 0)
4010 chip->ecc.size = ecc_step;
4011
4012 return 0;
4013}
4014
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004015/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004016 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004017 * @mtd: MTD device structure
4018 * @maxchips: number of chips to scan for
4019 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004020 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004021 * This is the first phase of the normal nand_scan() function. It reads the
4022 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004023 *
David Woodhouse3b85c322006-09-25 17:06:53 +01004024 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004025 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004026int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4027 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004028{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004029 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004030 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004031 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004032 int ret;
4033
Boris BREZILLON7194a292015-12-10 09:00:37 +01004034 ret = nand_dt_init(chip);
4035 if (ret)
4036 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004037
Brian Norrisf7a8e382016-01-05 10:39:45 -08004038 if (!mtd->name && mtd->dev.parent)
4039 mtd->name = dev_name(mtd->dev.parent);
4040
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004041 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004042 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004043
4044 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004045 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4046 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004047
4048 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004049 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004050 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004051 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004052 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
4054
Huang Shijie07300162012-11-09 16:23:45 +08004055 chip->select_chip(mtd, -1);
4056
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004057 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004058 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004059 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004060 /* See comment in nand_get_flash_type for reset */
4061 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004063 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004065 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004066 nand_dev_id != chip->read_byte(mtd)) {
4067 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 break;
Huang Shijie07300162012-11-09 16:23:45 +08004069 }
4070 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
4072 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004073 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004076 chip->numchips = i;
4077 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
David Woodhouse3b85c322006-09-25 17:06:53 +01004079 return 0;
4080}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004081EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004082
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004083/*
4084 * Check if the chip configuration meet the datasheet requirements.
4085
4086 * If our configuration corrects A bits per B bytes and the minimum
4087 * required correction level is X bits per Y bytes, then we must ensure
4088 * both of the following are true:
4089 *
4090 * (1) A / B >= X / Y
4091 * (2) A >= X
4092 *
4093 * Requirement (1) ensures we can correct for the required bitflip density.
4094 * Requirement (2) ensures we can correct even when all bitflips are clumped
4095 * in the same sector.
4096 */
4097static bool nand_ecc_strength_good(struct mtd_info *mtd)
4098{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004099 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004100 struct nand_ecc_ctrl *ecc = &chip->ecc;
4101 int corr, ds_corr;
4102
4103 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4104 /* Not enough information */
4105 return true;
4106
4107 /*
4108 * We get the number of corrected bits per page to compare
4109 * the correction density.
4110 */
4111 corr = (mtd->writesize * ecc->strength) / ecc->size;
4112 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4113
4114 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4115}
David Woodhouse3b85c322006-09-25 17:06:53 +01004116
4117/**
4118 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004119 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004120 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004121 * This is the second phase of the normal nand_scan() function. It fills out
4122 * all the uninitialized function pointers with the defaults and scans for a
4123 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004124 */
4125int nand_scan_tail(struct mtd_info *mtd)
4126{
4127 int i;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004128 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004129 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004130 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01004131
Brian Norrise2414f42012-02-06 13:44:00 -08004132 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4133 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4134 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4135
Huang Shijief02ea4e2014-01-13 14:27:12 +08004136 if (!(chip->options & NAND_OWN_BUFFERS)) {
4137 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4138 + mtd->oobsize * 3, GFP_KERNEL);
4139 if (!nbuf)
4140 return -ENOMEM;
4141 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4142 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4143 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4144
4145 chip->buffers = nbuf;
4146 } else {
4147 if (!chip->buffers)
4148 return -ENOMEM;
4149 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004150
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004151 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004152 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004153
4154 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004155 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004156 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004157 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004158 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08004160 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 break;
4162 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08004163 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 break;
4165 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08004166 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004168 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08004169 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 default:
Brian Norrisd0370212011-07-19 10:06:08 -07004172 pr_warn("No oob scheme defined for oobsize %d\n",
4173 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 BUG();
4175 }
4176 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004177
David Woodhouse956e9442006-09-25 17:12:39 +01004178 if (!chip->write_page)
4179 chip->write_page = nand_write_page;
4180
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004181 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004182 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004183 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004184 */
David Woodhouse956e9442006-09-25 17:12:39 +01004185
Huang Shijie97de79e02013-10-18 14:20:53 +08004186 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004187 case NAND_ECC_HW_OOB_FIRST:
4188 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004189 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004190 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004191 BUG();
4192 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004193 if (!ecc->read_page)
4194 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004195
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004196 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004197 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004198 if (!ecc->read_page)
4199 ecc->read_page = nand_read_page_hwecc;
4200 if (!ecc->write_page)
4201 ecc->write_page = nand_write_page_hwecc;
4202 if (!ecc->read_page_raw)
4203 ecc->read_page_raw = nand_read_page_raw;
4204 if (!ecc->write_page_raw)
4205 ecc->write_page_raw = nand_write_page_raw;
4206 if (!ecc->read_oob)
4207 ecc->read_oob = nand_read_oob_std;
4208 if (!ecc->write_oob)
4209 ecc->write_oob = nand_write_oob_std;
4210 if (!ecc->read_subpage)
4211 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004212 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004213 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004214
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004215 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004216 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4217 (!ecc->read_page ||
4218 ecc->read_page == nand_read_page_hwecc ||
4219 !ecc->write_page ||
4220 ecc->write_page == nand_write_page_hwecc)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004221 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004222 BUG();
4223 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004224 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004225 if (!ecc->read_page)
4226 ecc->read_page = nand_read_page_syndrome;
4227 if (!ecc->write_page)
4228 ecc->write_page = nand_write_page_syndrome;
4229 if (!ecc->read_page_raw)
4230 ecc->read_page_raw = nand_read_page_raw_syndrome;
4231 if (!ecc->write_page_raw)
4232 ecc->write_page_raw = nand_write_page_raw_syndrome;
4233 if (!ecc->read_oob)
4234 ecc->read_oob = nand_read_oob_syndrome;
4235 if (!ecc->write_oob)
4236 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004237
Huang Shijie97de79e02013-10-18 14:20:53 +08004238 if (mtd->writesize >= ecc->size) {
4239 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07004240 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4241 BUG();
4242 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004243 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004244 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004245 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4246 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004247 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004249 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08004250 ecc->calculate = nand_calculate_ecc;
4251 ecc->correct = nand_correct_data;
4252 ecc->read_page = nand_read_page_swecc;
4253 ecc->read_subpage = nand_read_subpage;
4254 ecc->write_page = nand_write_page_swecc;
4255 ecc->read_page_raw = nand_read_page_raw;
4256 ecc->write_page_raw = nand_write_page_raw;
4257 ecc->read_oob = nand_read_oob_std;
4258 ecc->write_oob = nand_write_oob_std;
4259 if (!ecc->size)
4260 ecc->size = 256;
4261 ecc->bytes = 3;
4262 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004264
Ivan Djelic193bd402011-03-11 11:05:33 +01004265 case NAND_ECC_SOFT_BCH:
4266 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03004267 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004268 BUG();
4269 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004270 ecc->calculate = nand_bch_calculate_ecc;
4271 ecc->correct = nand_bch_correct_data;
4272 ecc->read_page = nand_read_page_swecc;
4273 ecc->read_subpage = nand_read_subpage;
4274 ecc->write_page = nand_write_page_swecc;
4275 ecc->read_page_raw = nand_read_page_raw;
4276 ecc->write_page_raw = nand_write_page_raw;
4277 ecc->read_oob = nand_read_oob_std;
4278 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004279 /*
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004280 * Board driver should supply ecc.size and ecc.strength values
4281 * to select how many bits are correctable. Otherwise, default
4282 * to 4 bits for large page devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004283 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004284 if (!ecc->size && (mtd->oobsize >= 64)) {
4285 ecc->size = 512;
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004286 ecc->strength = 4;
Ivan Djelic193bd402011-03-11 11:05:33 +01004287 }
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004288
4289 /* See nand_bch_init() for details. */
4290 ecc->bytes = DIV_ROUND_UP(
4291 ecc->strength * fls(8 * ecc->size), 8);
Huang Shijie97de79e02013-10-18 14:20:53 +08004292 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4293 &ecc->layout);
4294 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004295 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004296 BUG();
4297 }
4298 break;
4299
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004300 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004301 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004302 ecc->read_page = nand_read_page_raw;
4303 ecc->write_page = nand_write_page_raw;
4304 ecc->read_oob = nand_read_oob_std;
4305 ecc->read_page_raw = nand_read_page_raw;
4306 ecc->write_page_raw = nand_write_page_raw;
4307 ecc->write_oob = nand_write_oob_std;
4308 ecc->size = mtd->writesize;
4309 ecc->bytes = 0;
4310 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08004314 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004315 BUG();
4316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Brian Norris9ce244b2011-08-30 18:45:37 -07004318 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004319 if (!ecc->read_oob_raw)
4320 ecc->read_oob_raw = ecc->read_oob;
4321 if (!ecc->write_oob_raw)
4322 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004323
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004324 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004325 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004326 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004327 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004328 ecc->layout->oobavail = 0;
4329 for (i = 0; ecc->layout->oobfree[i].length
4330 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4331 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4332 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004333
Thomas Petazzoni54c39e92014-07-02 15:16:32 +02004334 /* ECC sanity check: warn if it's too weak */
4335 if (!nand_ecc_strength_good(mtd))
4336 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4337 mtd->name);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004338
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004339 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004340 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004341 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004342 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004343 ecc->steps = mtd->writesize / ecc->size;
4344 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004345 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004346 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004348 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004349
Brian Norris8b6e50c2011-05-25 14:59:01 -07004350 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004351 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004352 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004353 case 2:
4354 mtd->subpage_sft = 1;
4355 break;
4356 case 4:
4357 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004358 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004359 mtd->subpage_sft = 2;
4360 break;
4361 }
4362 }
4363 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4364
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004365 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004366 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004369 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004371 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304372 switch (ecc->mode) {
4373 case NAND_ECC_SOFT:
4374 case NAND_ECC_SOFT_BCH:
4375 if (chip->page_shift > 9)
4376 chip->options |= NAND_SUBPAGE_READ;
4377 break;
4378
4379 default:
4380 break;
4381 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004384 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004385 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4386 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004387 mtd->_erase = nand_erase;
4388 mtd->_point = NULL;
4389 mtd->_unpoint = NULL;
4390 mtd->_read = nand_read;
4391 mtd->_write = nand_write;
4392 mtd->_panic_write = panic_nand_write;
4393 mtd->_read_oob = nand_read_oob;
4394 mtd->_write_oob = nand_write_oob;
4395 mtd->_sync = nand_sync;
4396 mtd->_lock = NULL;
4397 mtd->_unlock = NULL;
4398 mtd->_suspend = nand_suspend;
4399 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004400 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004401 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004402 mtd->_block_isbad = nand_block_isbad;
4403 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004404 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Mike Dunn6a918ba2012-03-11 14:21:11 -07004406 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004407 mtd->ecclayout = ecc->layout;
4408 mtd->ecc_strength = ecc->strength;
4409 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004410 /*
4411 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4412 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4413 * properly set.
4414 */
4415 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004416 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004418 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004419 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
4422 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004423 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004425EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Brian Norris8b6e50c2011-05-25 14:59:01 -07004427/*
4428 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004429 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004430 * to call us from in-kernel code if the core NAND support is modular.
4431 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004432#ifdef MODULE
4433#define caller_is_module() (1)
4434#else
4435#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004436 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004437#endif
4438
4439/**
4440 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004441 * @mtd: MTD device structure
4442 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004443 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004444 * This fills out all the uninitialized function pointers with the defaults.
4445 * The flash ID is read and the mtd/chip structures are filled with the
4446 * appropriate values. The mtd->owner field must be set to the module of the
4447 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004448 */
4449int nand_scan(struct mtd_info *mtd, int maxchips)
4450{
4451 int ret;
4452
4453 /* Many callers got this wrong, so check for it for a while... */
4454 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004455 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004456 BUG();
4457 }
4458
David Woodhouse5e81e882010-02-26 18:32:56 +00004459 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004460 if (!ret)
4461 ret = nand_scan_tail(mtd);
4462 return ret;
4463}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004464EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004467 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004468 * @mtd: MTD device structure
4469 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004470void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004472 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Ivan Djelic193bd402011-03-11 11:05:33 +01004474 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4475 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4476
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004477 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Jesper Juhlfa671642005-11-07 01:01:27 -08004479 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004480 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004481 if (!(chip->options & NAND_OWN_BUFFERS))
4482 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004483
4484 /* Free bad block descriptor memory */
4485 if (chip->badblock_pattern && chip->badblock_pattern->options
4486 & NAND_BBT_DYNAMICSTRUCT)
4487 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
David Woodhousee0c7d762006-05-13 18:07:53 +01004489EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004490
4491static int __init nand_base_init(void)
4492{
4493 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4494 return 0;
4495}
4496
4497static void __exit nand_base_exit(void)
4498{
4499 led_trigger_unregister_simple(nand_led_trigger);
4500}
4501
4502module_init(nand_base_init);
4503module_exit(nand_base_exit);
4504
David Woodhousee0c7d762006-05-13 18:07:53 +01004505MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004506MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4507MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004508MODULE_DESCRIPTION("Generic NAND flash driver code");