blob: ceb68ca8277a9ebb49c763a72b0979f089b878bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080046#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Brian Norris5844fee2015-01-23 00:22:27 -080049#include <linux/of_mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020052static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .eccbytes = 3,
54 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020055 .oobfree = {
56 {.offset = 3,
57 .length = 2},
58 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020059 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020062static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .eccbytes = 6,
64 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020065 .oobfree = {
66 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020067 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020070static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .eccbytes = 24,
72 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010073 40, 41, 42, 43, 44, 45, 46, 47,
74 48, 49, 50, 51, 52, 53, 54, 55,
75 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020076 .oobfree = {
77 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020078 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Thomas Gleixner81ec5362007-12-12 17:27:03 +010081static struct nand_ecclayout nand_oob_128 = {
82 .eccbytes = 48,
83 .eccpos = {
84 80, 81, 82, 83, 84, 85, 86, 87,
85 88, 89, 90, 91, 92, 93, 94, 95,
86 96, 97, 98, 99, 100, 101, 102, 103,
87 104, 105, 106, 107, 108, 109, 110, 111,
88 112, 113, 114, 115, 116, 117, 118, 119,
89 120, 121, 122, 123, 124, 125, 126, 127},
90 .oobfree = {
91 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020092 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010093};
94
Huang Shijie6a8214a2012-11-19 14:43:30 +080095static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020097static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
98 struct mtd_oob_ops *ops);
99
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200100/*
Joe Perches8e87d782008-02-03 17:22:34 +0200101 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200102 * compiled away when LED support is disabled.
103 */
104DEFINE_LED_TRIGGER(nand_led_trigger);
105
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530106static int check_offs_len(struct mtd_info *mtd,
107 loff_t ofs, uint64_t len)
108{
109 struct nand_chip *chip = mtd->priv;
110 int ret = 0;
111
112 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300113 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700114 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530115 ret = -EINVAL;
116 }
117
118 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300119 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700120 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530121 ret = -EINVAL;
122 }
123
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530124 return ret;
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/**
128 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700129 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000130 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800131 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100133static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200135 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200137 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200138 spin_lock(&chip->controller->lock);
139 chip->controller->active = NULL;
140 chip->state = FL_READY;
141 wake_up(&chip->controller->wq);
142 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/**
146 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700147 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700149 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200151static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200153 struct nand_chip *chip = mtd->priv;
154 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/**
Masanari Iida064a7692012-11-09 23:20:58 +0900158 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700159 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700161 * Default read function for 16bit buswidth with endianness conversion.
162 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200164static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200166 struct nand_chip *chip = mtd->priv;
167 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700172 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700174 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
176static u16 nand_read_word(struct mtd_info *mtd)
177{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200178 struct nand_chip *chip = mtd->priv;
179 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700184 * @mtd: MTD device structure
185 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
187 * Default select function for 1 chip devices.
188 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200191 struct nand_chip *chip = mtd->priv;
192
193 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200195 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199
200 default:
201 BUG();
202 }
203}
204
205/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100206 * nand_write_byte - [DEFAULT] write single byte to chip
207 * @mtd: MTD device structure
208 * @byte: value to write
209 *
210 * Default function to write a byte to I/O[7:0]
211 */
212static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
213{
214 struct nand_chip *chip = mtd->priv;
215
216 chip->write_buf(mtd, &byte, 1);
217}
218
219/**
220 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
221 * @mtd: MTD device structure
222 * @byte: value to write
223 *
224 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
225 */
226static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
227{
228 struct nand_chip *chip = mtd->priv;
229 uint16_t word = byte;
230
231 /*
232 * It's not entirely clear what should happen to I/O[15:8] when writing
233 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
234 *
235 * When the host supports a 16-bit bus width, only data is
236 * transferred at the 16-bit width. All address and command line
237 * transfers shall use only the lower 8-bits of the data bus. During
238 * command transfers, the host may place any value on the upper
239 * 8-bits of the data bus. During address transfers, the host shall
240 * set the upper 8-bits of the data bus to 00h.
241 *
242 * One user of the write_byte callback is nand_onfi_set_features. The
243 * four parameters are specified to be written to I/O[7:0], but this is
244 * neither an address nor a command transfer. Let's assume a 0 on the
245 * upper I/O lines is OK.
246 */
247 chip->write_buf(mtd, (uint8_t *)&word, 2);
248}
249
250/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700252 * @mtd: MTD device structure
253 * @buf: data buffer
254 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700256 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200258static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200260 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Alexander Shiyan76413832013-04-13 09:32:13 +0400262 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000266 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700267 * @mtd: MTD device structure
268 * @buf: buffer to store date
269 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200273static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200275 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Alexander Shiyan76413832013-04-13 09:32:13 +0400277 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
283 * @buf: data buffer
284 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700286 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200288static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200290 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000292
Alexander Shiyan76413832013-04-13 09:32:13 +0400293 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000297 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700298 * @mtd: MTD device structure
299 * @buf: buffer to store date
300 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700302 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200304static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200306 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alexander Shiyan76413832013-04-13 09:32:13 +0400309 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700314 * @mtd: MTD device structure
315 * @ofs: offset from device start
316 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000318 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
321{
Brian Norriscdbec052012-01-13 18:11:48 -0800322 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200323 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 u16 bad;
325
Brian Norris5fb15492011-05-31 16:31:21 -0700326 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700327 ofs += mtd->erasesize - mtd->writesize;
328
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100329 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200332 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Huang Shijie6a8214a2012-11-19 14:43:30 +0800334 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200337 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Brian Norriscdbec052012-01-13 18:11:48 -0800340 do {
341 if (chip->options & NAND_BUSWIDTH_16) {
342 chip->cmdfunc(mtd, NAND_CMD_READOOB,
343 chip->badblockpos & 0xFE, page);
344 bad = cpu_to_le16(chip->read_word(mtd));
345 if (chip->badblockpos & 0x1)
346 bad >>= 8;
347 else
348 bad &= 0xFF;
349 } else {
350 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
351 page);
352 bad = chip->read_byte(mtd);
353 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000354
Brian Norriscdbec052012-01-13 18:11:48 -0800355 if (likely(chip->badblockbits == 8))
356 res = bad != 0xFF;
357 else
358 res = hweight8(bad) < chip->badblockbits;
359 ofs += mtd->writesize;
360 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
361 i++;
362 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200363
Huang Shijieb0bb6902012-11-19 14:43:29 +0800364 if (getchip) {
365 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800367 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return res;
370}
371
372/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700373 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700374 * @mtd: MTD device structure
375 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700377 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700378 * specific driver. It provides the details for writing a bad block marker to a
379 * block.
380 */
381static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
382{
383 struct nand_chip *chip = mtd->priv;
384 struct mtd_oob_ops ops;
385 uint8_t buf[2] = { 0, 0 };
386 int ret = 0, res, i = 0;
387
Brian Norris0ec56dc2015-02-28 02:02:30 -0800388 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700389 ops.oobbuf = buf;
390 ops.ooboffs = chip->badblockpos;
391 if (chip->options & NAND_BUSWIDTH_16) {
392 ops.ooboffs &= ~0x01;
393 ops.len = ops.ooblen = 2;
394 } else {
395 ops.len = ops.ooblen = 1;
396 }
397 ops.mode = MTD_OPS_PLACE_OOB;
398
399 /* Write to first/last page(s) if necessary */
400 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
401 ofs += mtd->erasesize - mtd->writesize;
402 do {
403 res = nand_do_write_oob(mtd, ofs, &ops);
404 if (!ret)
405 ret = res;
406
407 i++;
408 ofs += mtd->writesize;
409 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
410
411 return ret;
412}
413
414/**
415 * nand_block_markbad_lowlevel - mark a block bad
416 * @mtd: MTD device structure
417 * @ofs: offset from device start
418 *
419 * This function performs the generic NAND bad block marking steps (i.e., bad
420 * block table(s) and/or marker(s)). We only allow the hardware driver to
421 * specify how to write bad block markers to OOB (chip->block_markbad).
422 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700423 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800424 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700425 * (2) write bad block marker to OOB area of affected block (unless flag
426 * NAND_BBT_NO_OOB_BBM is present)
427 * (3) update the BBT
428 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800429 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700431static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200433 struct nand_chip *chip = mtd->priv;
Brian Norrisb32843b2013-07-30 17:52:59 -0700434 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000435
Brian Norrisb32843b2013-07-30 17:52:59 -0700436 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800437 struct erase_info einfo;
438
439 /* Attempt erase before marking OOB */
440 memset(&einfo, 0, sizeof(einfo));
441 einfo.mtd = mtd;
442 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300443 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800444 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800445
Brian Norrisb32843b2013-07-30 17:52:59 -0700446 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800447 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700448 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300449 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200450 }
Brian Norrise2414f42012-02-06 13:44:00 -0800451
Brian Norrisb32843b2013-07-30 17:52:59 -0700452 /* Mark block bad in BBT */
453 if (chip->bbt) {
454 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800455 if (!ret)
456 ret = res;
457 }
458
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200459 if (!ret)
460 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300461
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000465/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700467 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700469 * Check, if the device is write protected. The function expects, that the
470 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100472static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200474 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200475
Brian Norris8b6e50c2011-05-25 14:59:01 -0700476 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200477 if (chip->options & NAND_BROKEN_XD)
478 return 0;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200481 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
482 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800486 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 * @mtd: MTD device structure
488 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300489 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800490 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300491 */
492static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
493{
494 struct nand_chip *chip = mtd->priv;
495
496 if (!chip->bbt)
497 return 0;
498 /* Return info from the table */
499 return nand_isreserved_bbt(mtd, ofs);
500}
501
502/**
503 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
504 * @mtd: MTD device structure
505 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700506 * @getchip: 0, if the chip is already selected
507 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 *
509 * Check, if the block is bad. Either by reading the bad block table or
510 * calling of the scan function.
511 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200512static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
513 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200515 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000516
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200517 if (!chip->bbt)
518 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100521 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200524/**
525 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700526 * @mtd: MTD device structure
527 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200528 *
529 * Helper function for nand_wait_ready used when needing to wait in interrupt
530 * context.
531 */
532static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
533{
534 struct nand_chip *chip = mtd->priv;
535 int i;
536
537 /* Wait for the device to get ready */
538 for (i = 0; i < timeo; i++) {
539 if (chip->dev_ready(mtd))
540 break;
541 touch_softlockup_watchdog();
542 mdelay(1);
543 }
544}
545
Brian Norris7854d3f2011-06-23 14:12:08 -0700546/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100547void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000548{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200549 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100550 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000551
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200552 /* 400ms timeout */
553 if (in_interrupt() || oops_in_progress)
554 return panic_nand_wait_ready(mtd, 400);
555
Richard Purdie8fe833c2006-03-31 02:31:14 -0800556 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700557 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000558 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200559 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800560 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700561 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000562 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800563 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000564}
David Woodhouse4b648b02006-09-25 17:05:24 +0100565EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200568 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
569 * @mtd: MTD device structure
570 * @timeo: Timeout in ms
571 *
572 * Wait for status ready (i.e. command done) or timeout.
573 */
574static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
575{
576 register struct nand_chip *chip = mtd->priv;
577
578 timeo = jiffies + msecs_to_jiffies(timeo);
579 do {
580 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
581 break;
582 touch_softlockup_watchdog();
583 } while (time_before(jiffies, timeo));
584};
585
586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700588 * @mtd: MTD device structure
589 * @command: the command to be sent
590 * @column: the column address for this command, -1 if none
591 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700593 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200594 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200596static void nand_command(struct mtd_info *mtd, unsigned int command,
597 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200599 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200600 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Brian Norris8b6e50c2011-05-25 14:59:01 -0700602 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (command == NAND_CMD_SEQIN) {
604 int readcmd;
605
Joern Engel28318772006-05-22 23:18:05 +0200606 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200608 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 readcmd = NAND_CMD_READOOB;
610 } else if (column < 256) {
611 /* First 256 bytes --> READ0 */
612 readcmd = NAND_CMD_READ0;
613 } else {
614 column -= 256;
615 readcmd = NAND_CMD_READ1;
616 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200617 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200618 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200620 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Brian Norris8b6e50c2011-05-25 14:59:01 -0700622 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
624 /* Serially input address */
625 if (column != -1) {
626 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800627 if (chip->options & NAND_BUSWIDTH_16 &&
628 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200629 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200630 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200631 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200633 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200634 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200635 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200636 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200637 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200638 if (chip->chipsize > (32 << 20))
639 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200640 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000642
643 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700644 * Program and erase have their own busy handlers status and sequential
645 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 case NAND_CMD_PAGEPROG:
650 case NAND_CMD_ERASE1:
651 case NAND_CMD_ERASE2:
652 case NAND_CMD_SEQIN:
653 case NAND_CMD_STATUS:
654 return;
655
656 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200657 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200659 udelay(chip->chip_delay);
660 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200662 chip->cmd_ctrl(mtd,
663 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200664 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
665 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return;
667
David Woodhousee0c7d762006-05-13 18:07:53 +0100668 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000670 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * If we don't have access to the busy pin, we apply the given
672 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100673 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200674 if (!chip->dev_ready) {
675 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700679 /*
680 * Apply this short delay always to ensure that we do wait tWB in
681 * any case on any machine.
682 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100683 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000684
685 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688/**
689 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700690 * @mtd: MTD device structure
691 * @command: the command to be sent
692 * @column: the column address for this command, -1 if none
693 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200695 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700696 * devices. We don't have the separate regions as we have in the small page
697 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200699static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
700 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200702 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* Emulate NAND_CMD_READOOB */
705 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200706 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 command = NAND_CMD_READ0;
708 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200710 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400711 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200714 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Serially input address */
717 if (column != -1) {
718 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800719 if (chip->options & NAND_BUSWIDTH_16 &&
720 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200722 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200727 chip->cmd_ctrl(mtd, page_addr, ctrl);
728 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200729 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (chip->chipsize > (128 << 20))
732 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200733 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000737
738 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700739 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100740 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case NAND_CMD_CACHEDPROG:
745 case NAND_CMD_PAGEPROG:
746 case NAND_CMD_ERASE1:
747 case NAND_CMD_ERASE2:
748 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200749 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000751 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200756 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200757 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
758 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
759 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
760 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200761 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
762 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return;
764
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200765 case NAND_CMD_RNDOUT:
766 /* No ready / busy check necessary */
767 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
768 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
769 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
770 NAND_NCE | NAND_CTRL_CHANGE);
771 return;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200774 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
775 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
776 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
777 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000778
David Woodhousee0c7d762006-05-13 18:07:53 +0100779 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000781 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700783 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100784 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200785 if (!chip->dev_ready) {
786 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000790
Brian Norris8b6e50c2011-05-25 14:59:01 -0700791 /*
792 * Apply this short delay always to ensure that we do wait tWB in
793 * any case on any machine.
794 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100795 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000796
797 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200801 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700802 * @chip: the nand chip descriptor
803 * @mtd: MTD device structure
804 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200805 *
806 * Used when in panic, no locks are taken.
807 */
808static void panic_nand_get_device(struct nand_chip *chip,
809 struct mtd_info *mtd, int new_state)
810{
Brian Norris7854d3f2011-06-23 14:12:08 -0700811 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200812 chip->controller->active = chip;
813 chip->state = new_state;
814}
815
816/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700818 * @mtd: MTD device structure
819 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
821 * Get the device and lock it for exclusive access
822 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200823static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800824nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800826 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200827 spinlock_t *lock = &chip->controller->lock;
828 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100829 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200830retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100831 spin_lock(lock);
832
vimal singhb8b3ee92009-07-09 20:41:22 +0530833 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200834 if (!chip->controller->active)
835 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200836
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200837 if (chip->controller->active == chip && chip->state == FL_READY) {
838 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100839 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100840 return 0;
841 }
842 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800843 if (chip->controller->active->state == FL_PM_SUSPENDED) {
844 chip->state = FL_PM_SUSPENDED;
845 spin_unlock(lock);
846 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800847 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100848 }
849 set_current_state(TASK_UNINTERRUPTIBLE);
850 add_wait_queue(wq, &wait);
851 spin_unlock(lock);
852 schedule();
853 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 goto retry;
855}
856
857/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700858 * panic_nand_wait - [GENERIC] wait until the command is done
859 * @mtd: MTD device structure
860 * @chip: NAND chip structure
861 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200862 *
863 * Wait for command done. This is a helper function for nand_wait used when
864 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400865 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200866 */
867static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
868 unsigned long timeo)
869{
870 int i;
871 for (i = 0; i < timeo; i++) {
872 if (chip->dev_ready) {
873 if (chip->dev_ready(mtd))
874 break;
875 } else {
876 if (chip->read_byte(mtd) & NAND_STATUS_READY)
877 break;
878 }
879 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200880 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200881}
882
883/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700884 * nand_wait - [DEFAULT] wait until the command is done
885 * @mtd: MTD device structure
886 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700888 * Wait for command done. This applies to erase and program only. Erase can
889 * take up to 400ms and program up to 20ms according to general NAND and
890 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700891 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200892static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200895 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800896 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Richard Purdie8fe833c2006-03-31 02:31:14 -0800898 led_trigger_event(nand_led_trigger, LED_FULL);
899
Brian Norris8b6e50c2011-05-25 14:59:01 -0700900 /*
901 * Apply this short delay always to ensure that we do wait tWB in any
902 * case on any machine.
903 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100904 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200906 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200908 if (in_interrupt() || oops_in_progress)
909 panic_nand_wait(mtd, chip, timeo);
910 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800911 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200912 while (time_before(jiffies, timeo)) {
913 if (chip->dev_ready) {
914 if (chip->dev_ready(mtd))
915 break;
916 } else {
917 if (chip->read_byte(mtd) & NAND_STATUS_READY)
918 break;
919 }
920 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800923 led_trigger_event(nand_led_trigger, LED_OFF);
924
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200925 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100926 /* This can happen if in case of timeout or buggy dev_ready */
927 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return status;
929}
930
931/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700932 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700933 * @mtd: mtd info
934 * @ofs: offset to start unlock from
935 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700936 * @invert: when = 0, unlock the range of blocks within the lower and
937 * upper boundary address
938 * when = 1, unlock the range of blocks outside the boundaries
939 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530940 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700941 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530942 */
943static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
944 uint64_t len, int invert)
945{
946 int ret = 0;
947 int status, page;
948 struct nand_chip *chip = mtd->priv;
949
950 /* Submit address of first page to unlock */
951 page = ofs >> chip->page_shift;
952 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
953
954 /* Submit address of last page to unlock */
955 page = (ofs + len) >> chip->page_shift;
956 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
957 (page | invert) & chip->pagemask);
958
959 /* Call wait ready function */
960 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530961 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400962 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700963 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530964 __func__, status);
965 ret = -EIO;
966 }
967
968 return ret;
969}
970
971/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700972 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700973 * @mtd: mtd info
974 * @ofs: offset to start unlock from
975 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530976 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700977 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530978 */
979int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
980{
981 int ret = 0;
982 int chipnr;
983 struct nand_chip *chip = mtd->priv;
984
Brian Norris289c0522011-07-19 10:06:09 -0700985 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530986 __func__, (unsigned long long)ofs, len);
987
988 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -0800989 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +0530990
991 /* Align to last block address if size addresses end of the device */
992 if (ofs + len == mtd->size)
993 len -= mtd->erasesize;
994
Huang Shijie6a8214a2012-11-19 14:43:30 +0800995 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530996
997 /* Shift to get chip number */
998 chipnr = ofs >> chip->chip_shift;
999
1000 chip->select_chip(mtd, chipnr);
1001
White Ding57d3a9a2014-07-24 00:10:45 +08001002 /*
1003 * Reset the chip.
1004 * If we want to check the WP through READ STATUS and check the bit 7
1005 * we must reset the chip
1006 * some operation can also clear the bit 7 of status register
1007 * eg. erase/program a locked block
1008 */
1009 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1010
Vimal Singh7d70f332010-02-08 15:50:49 +05301011 /* Check, if it is write protected */
1012 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001013 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301014 __func__);
1015 ret = -EIO;
1016 goto out;
1017 }
1018
1019 ret = __nand_unlock(mtd, ofs, len, 0);
1020
1021out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001022 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301023 nand_release_device(mtd);
1024
1025 return ret;
1026}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001027EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301028
1029/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001030 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001031 * @mtd: mtd info
1032 * @ofs: offset to start unlock from
1033 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301034 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001035 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1036 * have this feature, but it allows only to lock all blocks, not for specified
1037 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1038 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301039 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001040 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301041 */
1042int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1043{
1044 int ret = 0;
1045 int chipnr, status, page;
1046 struct nand_chip *chip = mtd->priv;
1047
Brian Norris289c0522011-07-19 10:06:09 -07001048 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301049 __func__, (unsigned long long)ofs, len);
1050
1051 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001052 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301053
Huang Shijie6a8214a2012-11-19 14:43:30 +08001054 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301055
1056 /* Shift to get chip number */
1057 chipnr = ofs >> chip->chip_shift;
1058
1059 chip->select_chip(mtd, chipnr);
1060
White Ding57d3a9a2014-07-24 00:10:45 +08001061 /*
1062 * Reset the chip.
1063 * If we want to check the WP through READ STATUS and check the bit 7
1064 * we must reset the chip
1065 * some operation can also clear the bit 7 of status register
1066 * eg. erase/program a locked block
1067 */
1068 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1069
Vimal Singh7d70f332010-02-08 15:50:49 +05301070 /* Check, if it is write protected */
1071 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001072 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301073 __func__);
1074 status = MTD_ERASE_FAILED;
1075 ret = -EIO;
1076 goto out;
1077 }
1078
1079 /* Submit address of first page to lock */
1080 page = ofs >> chip->page_shift;
1081 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1082
1083 /* Call wait ready function */
1084 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301085 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001086 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001087 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301088 __func__, status);
1089 ret = -EIO;
1090 goto out;
1091 }
1092
1093 ret = __nand_unlock(mtd, ofs, len, 0x1);
1094
1095out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001096 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301097 nand_release_device(mtd);
1098
1099 return ret;
1100}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001101EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301102
1103/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001104 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001105 * @mtd: mtd info structure
1106 * @chip: nand chip info structure
1107 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001108 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001109 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001110 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001111 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001112 */
1113static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001114 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001115{
1116 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001117 if (oob_required)
1118 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001119 return 0;
1120}
1121
1122/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001123 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001124 * @mtd: mtd info structure
1125 * @chip: nand chip info structure
1126 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001127 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001128 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001129 *
1130 * We need a special oob layout and handling even when OOB isn't used.
1131 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001132static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001133 struct nand_chip *chip, uint8_t *buf,
1134 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001135{
1136 int eccsize = chip->ecc.size;
1137 int eccbytes = chip->ecc.bytes;
1138 uint8_t *oob = chip->oob_poi;
1139 int steps, size;
1140
1141 for (steps = chip->ecc.steps; steps > 0; steps--) {
1142 chip->read_buf(mtd, buf, eccsize);
1143 buf += eccsize;
1144
1145 if (chip->ecc.prepad) {
1146 chip->read_buf(mtd, oob, chip->ecc.prepad);
1147 oob += chip->ecc.prepad;
1148 }
1149
1150 chip->read_buf(mtd, oob, eccbytes);
1151 oob += eccbytes;
1152
1153 if (chip->ecc.postpad) {
1154 chip->read_buf(mtd, oob, chip->ecc.postpad);
1155 oob += chip->ecc.postpad;
1156 }
1157 }
1158
1159 size = mtd->oobsize - (oob - chip->oob_poi);
1160 if (size)
1161 chip->read_buf(mtd, oob, size);
1162
1163 return 0;
1164}
1165
1166/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001167 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001168 * @mtd: mtd info structure
1169 * @chip: nand chip info structure
1170 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001171 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001172 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001173 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001174static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001175 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001177 int i, eccsize = chip->ecc.size;
1178 int eccbytes = chip->ecc.bytes;
1179 int eccsteps = chip->ecc.steps;
1180 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001181 uint8_t *ecc_calc = chip->buffers->ecccalc;
1182 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001183 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001184 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001185
Brian Norris1fbb9382012-05-02 10:14:55 -07001186 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001187
1188 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1189 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1190
1191 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001192 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001193
1194 eccsteps = chip->ecc.steps;
1195 p = buf;
1196
1197 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1198 int stat;
1199
1200 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001201 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001202 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001203 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001204 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001205 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1206 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001207 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001208 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301212 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001213 * @mtd: mtd info structure
1214 * @chip: nand chip info structure
1215 * @data_offs: offset of requested data within the page
1216 * @readlen: data length
1217 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001218 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001219 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001220static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001221 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1222 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001223{
1224 int start_step, end_step, num_steps;
1225 uint32_t *eccpos = chip->ecc.layout->eccpos;
1226 uint8_t *p;
1227 int data_col_addr, i, gaps = 0;
1228 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1229 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301230 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001231 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001232
Brian Norris7854d3f2011-06-23 14:12:08 -07001233 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001234 start_step = data_offs / chip->ecc.size;
1235 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1236 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301237 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001238
Brian Norris8b6e50c2011-05-25 14:59:01 -07001239 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001240 datafrag_len = num_steps * chip->ecc.size;
1241 eccfrag_len = num_steps * chip->ecc.bytes;
1242
1243 data_col_addr = start_step * chip->ecc.size;
1244 /* If we read not a page aligned data */
1245 if (data_col_addr != 0)
1246 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1247
1248 p = bufpoi + data_col_addr;
1249 chip->read_buf(mtd, p, datafrag_len);
1250
Brian Norris8b6e50c2011-05-25 14:59:01 -07001251 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001252 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1253 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1254
Brian Norris8b6e50c2011-05-25 14:59:01 -07001255 /*
1256 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001257 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001258 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001259 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301260 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001261 gaps = 1;
1262 break;
1263 }
1264 }
1265 if (gaps) {
1266 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1267 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1268 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001269 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001270 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001271 * about buswidth alignment in read_buf.
1272 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001273 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001274 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001275 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001276 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001277 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001278 aligned_len++;
1279
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001280 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1281 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001282 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1283 }
1284
1285 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001286 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001287
1288 p = bufpoi + data_col_addr;
1289 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1290 int stat;
1291
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001292 stat = chip->ecc.correct(mtd, p,
1293 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001294 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001295 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001296 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001297 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001298 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1299 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001300 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001301 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001302}
1303
1304/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001305 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001306 * @mtd: mtd info structure
1307 * @chip: nand chip info structure
1308 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001309 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001310 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001311 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001312 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001313 */
1314static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001315 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001316{
1317 int i, eccsize = chip->ecc.size;
1318 int eccbytes = chip->ecc.bytes;
1319 int eccsteps = chip->ecc.steps;
1320 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001321 uint8_t *ecc_calc = chip->buffers->ecccalc;
1322 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001323 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001324 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001325
1326 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1327 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1328 chip->read_buf(mtd, p, eccsize);
1329 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1330 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001331 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001332
1333 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001334 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001335
1336 eccsteps = chip->ecc.steps;
1337 p = buf;
1338
1339 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1340 int stat;
1341
1342 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001343 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001344 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001345 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001346 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001347 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1348 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001349 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001350 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001351}
1352
1353/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001354 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001355 * @mtd: mtd info structure
1356 * @chip: nand chip info structure
1357 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001358 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001359 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001360 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001361 * Hardware ECC for large page chips, require OOB to be read first. For this
1362 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1363 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1364 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1365 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001366 */
1367static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001368 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001369{
1370 int i, eccsize = chip->ecc.size;
1371 int eccbytes = chip->ecc.bytes;
1372 int eccsteps = chip->ecc.steps;
1373 uint8_t *p = buf;
1374 uint8_t *ecc_code = chip->buffers->ecccode;
1375 uint32_t *eccpos = chip->ecc.layout->eccpos;
1376 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001377 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001378
1379 /* Read the OOB area first */
1380 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1381 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1382 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1383
1384 for (i = 0; i < chip->ecc.total; i++)
1385 ecc_code[i] = chip->oob_poi[eccpos[i]];
1386
1387 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1388 int stat;
1389
1390 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1391 chip->read_buf(mtd, p, eccsize);
1392 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1393
1394 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001395 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001396 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001397 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001398 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001399 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1400 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001401 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001402 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001403}
1404
1405/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001406 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001407 * @mtd: mtd info structure
1408 * @chip: nand chip info structure
1409 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001410 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001411 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001412 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001413 * The hw generator calculates the error syndrome automatically. Therefore we
1414 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001415 */
1416static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001417 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001418{
1419 int i, eccsize = chip->ecc.size;
1420 int eccbytes = chip->ecc.bytes;
1421 int eccsteps = chip->ecc.steps;
1422 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001423 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001424 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001425
1426 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1427 int stat;
1428
1429 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1430 chip->read_buf(mtd, p, eccsize);
1431
1432 if (chip->ecc.prepad) {
1433 chip->read_buf(mtd, oob, chip->ecc.prepad);
1434 oob += chip->ecc.prepad;
1435 }
1436
1437 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1438 chip->read_buf(mtd, oob, eccbytes);
1439 stat = chip->ecc.correct(mtd, p, oob, NULL);
1440
Mike Dunn3f91e942012-04-25 12:06:09 -07001441 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001442 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001443 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001445 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1446 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001447
1448 oob += eccbytes;
1449
1450 if (chip->ecc.postpad) {
1451 chip->read_buf(mtd, oob, chip->ecc.postpad);
1452 oob += chip->ecc.postpad;
1453 }
1454 }
1455
1456 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001457 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001458 if (i)
1459 chip->read_buf(mtd, oob, i);
1460
Mike Dunn3f91e942012-04-25 12:06:09 -07001461 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001462}
1463
1464/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001465 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001466 * @chip: nand chip structure
1467 * @oob: oob destination address
1468 * @ops: oob ops structure
1469 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001470 */
1471static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001472 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001473{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001474 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001475
Brian Norris0612b9d2011-08-30 18:45:40 -07001476 case MTD_OPS_PLACE_OOB:
1477 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001478 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1479 return oob + len;
1480
Brian Norris0612b9d2011-08-30 18:45:40 -07001481 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001482 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001483 uint32_t boffs = 0, roffs = ops->ooboffs;
1484 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001485
Florian Fainellif8ac0412010-09-07 13:23:43 +02001486 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001487 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001488 if (unlikely(roffs)) {
1489 if (roffs >= free->length) {
1490 roffs -= free->length;
1491 continue;
1492 }
1493 boffs = free->offset + roffs;
1494 bytes = min_t(size_t, len,
1495 (free->length - roffs));
1496 roffs = 0;
1497 } else {
1498 bytes = min_t(size_t, len, free->length);
1499 boffs = free->offset;
1500 }
1501 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001502 oob += bytes;
1503 }
1504 return oob;
1505 }
1506 default:
1507 BUG();
1508 }
1509 return NULL;
1510}
1511
1512/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001513 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1514 * @mtd: MTD device structure
1515 * @retry_mode: the retry mode to use
1516 *
1517 * Some vendors supply a special command to shift the Vt threshold, to be used
1518 * when there are too many bitflips in a page (i.e., ECC error). After setting
1519 * a new threshold, the host should retry reading the page.
1520 */
1521static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1522{
1523 struct nand_chip *chip = mtd->priv;
1524
1525 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1526
1527 if (retry_mode >= chip->read_retries)
1528 return -EINVAL;
1529
1530 if (!chip->setup_read_retry)
1531 return -EOPNOTSUPP;
1532
1533 return chip->setup_read_retry(mtd, retry_mode);
1534}
1535
1536/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001537 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001538 * @mtd: MTD device structure
1539 * @from: offset to read from
1540 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001541 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001542 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001543 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001544static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1545 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001546{
Brian Norrise47f3db2012-05-02 10:14:56 -07001547 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001548 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001549 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001550 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001551 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001552 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001553 mtd->oobavail : mtd->oobsize;
1554
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001555 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001556 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001557 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001558 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001559 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001561 chipnr = (int)(from >> chip->chip_shift);
1562 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001564 realpage = (int)(from >> chip->page_shift);
1565 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001567 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001569 buf = ops->datbuf;
1570 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001571 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001572
Florian Fainellif8ac0412010-09-07 13:23:43 +02001573 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001574 unsigned int ecc_failures = mtd->ecc_stats.failed;
1575
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001576 bytes = min(mtd->writesize - col, readlen);
1577 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001578
Kamal Dasu66507c72014-05-01 20:51:19 -04001579 if (!aligned)
1580 use_bufpoi = 1;
1581 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1582 use_bufpoi = !virt_addr_valid(buf);
1583 else
1584 use_bufpoi = 0;
1585
Brian Norris8b6e50c2011-05-25 14:59:01 -07001586 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001587 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001588 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1589
1590 if (use_bufpoi && aligned)
1591 pr_debug("%s: using read bounce buffer for buf@%p\n",
1592 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Brian Norrisba84fb52014-01-03 15:13:33 -08001594read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001595 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Mike Dunnedbc45402012-04-25 12:06:11 -07001597 /*
1598 * Now read the page into the buffer. Absent an error,
1599 * the read methods return max bitflips per ecc step.
1600 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001601 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001602 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001603 oob_required,
1604 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001605 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1606 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001607 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001608 col, bytes, bufpoi,
1609 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001610 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001611 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001612 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001613 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001614 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001615 /* Invalidate page cache */
1616 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001617 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001618 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001619
Mike Dunnedbc45402012-04-25 12:06:11 -07001620 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1621
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001622 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001623 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001624 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001625 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001626 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001627 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001628 chip->pagebuf_bitflips = ret;
1629 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001630 /* Invalidate page cache */
1631 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001632 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001633 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001635
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001636 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001637 int toread = min(oobreadlen, max_oobsize);
1638
1639 if (toread) {
1640 oob = nand_transfer_oob(chip,
1641 oob, ops, toread);
1642 oobreadlen -= toread;
1643 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001644 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001645
1646 if (chip->options & NAND_NEED_READRDY) {
1647 /* Apply delay or wait for ready/busy pin */
1648 if (!chip->dev_ready)
1649 udelay(chip->chip_delay);
1650 else
1651 nand_wait_ready(mtd);
1652 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001653
Brian Norrisba84fb52014-01-03 15:13:33 -08001654 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001655 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001656 retry_mode++;
1657 ret = nand_setup_read_retry(mtd,
1658 retry_mode);
1659 if (ret < 0)
1660 break;
1661
1662 /* Reset failures; retry */
1663 mtd->ecc_stats.failed = ecc_failures;
1664 goto read_retry;
1665 } else {
1666 /* No more retry modes; real failure */
1667 ecc_fail = true;
1668 }
1669 }
1670
1671 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001672 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001673 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001674 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001675 max_bitflips = max_t(unsigned int, max_bitflips,
1676 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001679 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001680
Brian Norrisba84fb52014-01-03 15:13:33 -08001681 /* Reset to retry mode 0 */
1682 if (retry_mode) {
1683 ret = nand_setup_read_retry(mtd, 0);
1684 if (ret < 0)
1685 break;
1686 retry_mode = 0;
1687 }
1688
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001689 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Brian Norris8b6e50c2011-05-25 14:59:01 -07001692 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 col = 0;
1694 /* Increment page address */
1695 realpage++;
1696
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001697 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 /* Check, if we cross a chip boundary */
1699 if (!page) {
1700 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001701 chip->select_chip(mtd, -1);
1702 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001705 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001707 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001708 if (oob)
1709 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Mike Dunn3f91e942012-04-25 12:06:09 -07001711 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001712 return ret;
1713
Brian Norrisb72f3df2013-12-03 11:04:14 -08001714 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001715 return -EBADMSG;
1716
Mike Dunnedbc45402012-04-25 12:06:11 -07001717 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001718}
1719
1720/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001721 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001722 * @mtd: MTD device structure
1723 * @from: offset to read from
1724 * @len: number of bytes to read
1725 * @retlen: pointer to variable to store the number of read bytes
1726 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001727 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001728 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001729 */
1730static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1731 size_t *retlen, uint8_t *buf)
1732{
Brian Norris4a89ff82011-08-30 18:45:45 -07001733 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001734 int ret;
1735
Huang Shijie6a8214a2012-11-19 14:43:30 +08001736 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001737 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001738 ops.len = len;
1739 ops.datbuf = buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08001740 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001741 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001742 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001743 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001744 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001748 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001749 * @mtd: mtd info structure
1750 * @chip: nand chip info structure
1751 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001752 */
1753static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001754 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001755{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001756 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001757 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001758 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001759}
1760
1761/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001762 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001763 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001764 * @mtd: mtd info structure
1765 * @chip: nand chip info structure
1766 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001767 */
1768static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001769 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001770{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001771 int length = mtd->oobsize;
1772 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1773 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001774 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001775 int i, toread, sndrnd = 0, pos;
1776
1777 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1778 for (i = 0; i < chip->ecc.steps; i++) {
1779 if (sndrnd) {
1780 pos = eccsize + i * (eccsize + chunk);
1781 if (mtd->writesize > 512)
1782 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1783 else
1784 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1785 } else
1786 sndrnd = 1;
1787 toread = min_t(int, length, chunk);
1788 chip->read_buf(mtd, bufpoi, toread);
1789 bufpoi += toread;
1790 length -= toread;
1791 }
1792 if (length > 0)
1793 chip->read_buf(mtd, bufpoi, length);
1794
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001795 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001796}
1797
1798/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001799 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001800 * @mtd: mtd info structure
1801 * @chip: nand chip info structure
1802 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001803 */
1804static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1805 int page)
1806{
1807 int status = 0;
1808 const uint8_t *buf = chip->oob_poi;
1809 int length = mtd->oobsize;
1810
1811 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1812 chip->write_buf(mtd, buf, length);
1813 /* Send command to program the OOB data */
1814 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1815
1816 status = chip->waitfunc(mtd, chip);
1817
Savin Zlobec0d420f92006-06-21 11:51:20 +02001818 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001819}
1820
1821/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001822 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001823 * with syndrome - only for large page flash
1824 * @mtd: mtd info structure
1825 * @chip: nand chip info structure
1826 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001827 */
1828static int nand_write_oob_syndrome(struct mtd_info *mtd,
1829 struct nand_chip *chip, int page)
1830{
1831 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1832 int eccsize = chip->ecc.size, length = mtd->oobsize;
1833 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1834 const uint8_t *bufpoi = chip->oob_poi;
1835
1836 /*
1837 * data-ecc-data-ecc ... ecc-oob
1838 * or
1839 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1840 */
1841 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1842 pos = steps * (eccsize + chunk);
1843 steps = 0;
1844 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001845 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001846
1847 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1848 for (i = 0; i < steps; i++) {
1849 if (sndcmd) {
1850 if (mtd->writesize <= 512) {
1851 uint32_t fill = 0xFFFFFFFF;
1852
1853 len = eccsize;
1854 while (len > 0) {
1855 int num = min_t(int, len, 4);
1856 chip->write_buf(mtd, (uint8_t *)&fill,
1857 num);
1858 len -= num;
1859 }
1860 } else {
1861 pos = eccsize + i * (eccsize + chunk);
1862 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1863 }
1864 } else
1865 sndcmd = 1;
1866 len = min_t(int, length, chunk);
1867 chip->write_buf(mtd, bufpoi, len);
1868 bufpoi += len;
1869 length -= len;
1870 }
1871 if (length > 0)
1872 chip->write_buf(mtd, bufpoi, length);
1873
1874 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1875 status = chip->waitfunc(mtd, chip);
1876
1877 return status & NAND_STATUS_FAIL ? -EIO : 0;
1878}
1879
1880/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001881 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001882 * @mtd: MTD device structure
1883 * @from: offset to read from
1884 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001886 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001888static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1889 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Brian Norrisc00a0992012-05-01 17:12:54 -07001891 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001892 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001893 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001894 int readlen = ops->ooblen;
1895 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001896 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001897 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Brian Norris289c0522011-07-19 10:06:09 -07001899 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301900 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Brian Norris041e4572011-06-23 16:45:24 -07001902 stats = mtd->ecc_stats;
1903
Brian Norris0612b9d2011-08-30 18:45:40 -07001904 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001905 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001906 else
1907 len = mtd->oobsize;
1908
1909 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001910 pr_debug("%s: attempt to start read outside oob\n",
1911 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001912 return -EINVAL;
1913 }
1914
1915 /* Do not allow reads past end of device */
1916 if (unlikely(from >= mtd->size ||
1917 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1918 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001919 pr_debug("%s: attempt to read beyond end of device\n",
1920 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001921 return -EINVAL;
1922 }
Vitaly Wool70145682006-11-03 18:20:38 +03001923
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001924 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001925 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001927 /* Shift to get page */
1928 realpage = (int)(from >> chip->page_shift);
1929 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Florian Fainellif8ac0412010-09-07 13:23:43 +02001931 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001932 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001933 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001934 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001935 ret = chip->ecc.read_oob(mtd, chip, page);
1936
1937 if (ret < 0)
1938 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001939
1940 len = min(len, readlen);
1941 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001942
Brian Norris5bc7c332013-03-13 09:51:31 -07001943 if (chip->options & NAND_NEED_READRDY) {
1944 /* Apply delay or wait for ready/busy pin */
1945 if (!chip->dev_ready)
1946 udelay(chip->chip_delay);
1947 else
1948 nand_wait_ready(mtd);
1949 }
1950
Vitaly Wool70145682006-11-03 18:20:38 +03001951 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001952 if (!readlen)
1953 break;
1954
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001955 /* Increment page address */
1956 realpage++;
1957
1958 page = realpage & chip->pagemask;
1959 /* Check, if we cross a chip boundary */
1960 if (!page) {
1961 chipnr++;
1962 chip->select_chip(mtd, -1);
1963 chip->select_chip(mtd, chipnr);
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001966 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001968 ops->oobretlen = ops->ooblen - readlen;
1969
1970 if (ret < 0)
1971 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001972
1973 if (mtd->ecc_stats.failed - stats.failed)
1974 return -EBADMSG;
1975
1976 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001980 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001981 * @mtd: MTD device structure
1982 * @from: offset to read from
1983 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001985 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001987static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1988 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001990 int ret = -ENOTSUPP;
1991
1992 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001995 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001996 pr_debug("%s: attempt to read beyond end of device\n",
1997 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return -EINVAL;
1999 }
2000
Huang Shijie6a8214a2012-11-19 14:43:30 +08002001 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Florian Fainellif8ac0412010-09-07 13:23:43 +02002003 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002004 case MTD_OPS_PLACE_OOB:
2005 case MTD_OPS_AUTO_OOB:
2006 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002007 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002008
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002009 default:
2010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002013 if (!ops->datbuf)
2014 ret = nand_do_read_oob(mtd, from, ops);
2015 else
2016 ret = nand_do_read_ops(mtd, from, ops);
2017
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002018out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002020 return ret;
2021}
2022
2023
2024/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002025 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002026 * @mtd: mtd info structure
2027 * @chip: nand chip info structure
2028 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002029 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002030 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002031 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002032 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002033static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002034 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002035{
2036 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002037 if (oob_required)
2038 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002039
2040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002043/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002044 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002045 * @mtd: mtd info structure
2046 * @chip: nand chip info structure
2047 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002048 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002049 *
2050 * We need a special oob layout and handling even when ECC isn't checked.
2051 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002052static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002053 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002054 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08002055{
2056 int eccsize = chip->ecc.size;
2057 int eccbytes = chip->ecc.bytes;
2058 uint8_t *oob = chip->oob_poi;
2059 int steps, size;
2060
2061 for (steps = chip->ecc.steps; steps > 0; steps--) {
2062 chip->write_buf(mtd, buf, eccsize);
2063 buf += eccsize;
2064
2065 if (chip->ecc.prepad) {
2066 chip->write_buf(mtd, oob, chip->ecc.prepad);
2067 oob += chip->ecc.prepad;
2068 }
2069
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002070 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002071 oob += eccbytes;
2072
2073 if (chip->ecc.postpad) {
2074 chip->write_buf(mtd, oob, chip->ecc.postpad);
2075 oob += chip->ecc.postpad;
2076 }
2077 }
2078
2079 size = mtd->oobsize - (oob - chip->oob_poi);
2080 if (size)
2081 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002082
2083 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002084}
2085/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002086 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002087 * @mtd: mtd info structure
2088 * @chip: nand chip info structure
2089 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002090 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002091 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002092static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002093 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002094{
2095 int i, eccsize = chip->ecc.size;
2096 int eccbytes = chip->ecc.bytes;
2097 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002098 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002099 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002100 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002101
Brian Norris7854d3f2011-06-23 14:12:08 -07002102 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002103 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2104 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002105
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002106 for (i = 0; i < chip->ecc.total; i++)
2107 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002108
Josh Wufdbad98d2012-06-25 18:07:45 +08002109 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002110}
2111
2112/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002113 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002114 * @mtd: mtd info structure
2115 * @chip: nand chip info structure
2116 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002117 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002118 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002119static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002120 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002121{
2122 int i, eccsize = chip->ecc.size;
2123 int eccbytes = chip->ecc.bytes;
2124 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002125 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002126 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002127 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002128
2129 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2130 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002131 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002132 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2133 }
2134
2135 for (i = 0; i < chip->ecc.total; i++)
2136 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2137
2138 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002139
2140 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002141}
2142
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302143
2144/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002145 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302146 * @mtd: mtd info structure
2147 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002148 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302149 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002150 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302151 * @oob_required: must write chip->oob_poi to OOB
2152 */
2153static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2154 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002155 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302156 int oob_required)
2157{
2158 uint8_t *oob_buf = chip->oob_poi;
2159 uint8_t *ecc_calc = chip->buffers->ecccalc;
2160 int ecc_size = chip->ecc.size;
2161 int ecc_bytes = chip->ecc.bytes;
2162 int ecc_steps = chip->ecc.steps;
2163 uint32_t *eccpos = chip->ecc.layout->eccpos;
2164 uint32_t start_step = offset / ecc_size;
2165 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2166 int oob_bytes = mtd->oobsize / ecc_steps;
2167 int step, i;
2168
2169 for (step = 0; step < ecc_steps; step++) {
2170 /* configure controller for WRITE access */
2171 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2172
2173 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002174 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302175
2176 /* mask ECC of un-touched subpages by padding 0xFF */
2177 if ((step < start_step) || (step > end_step))
2178 memset(ecc_calc, 0xff, ecc_bytes);
2179 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002180 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302181
2182 /* mask OOB of un-touched subpages by padding 0xFF */
2183 /* if oob_required, preserve OOB metadata of written subpage */
2184 if (!oob_required || (step < start_step) || (step > end_step))
2185 memset(oob_buf, 0xff, oob_bytes);
2186
Brian Norrisd6a950802013-08-08 17:16:36 -07002187 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302188 ecc_calc += ecc_bytes;
2189 oob_buf += oob_bytes;
2190 }
2191
2192 /* copy calculated ECC for whole page to chip->buffer->oob */
2193 /* this include masked-value(0xFF) for unwritten subpages */
2194 ecc_calc = chip->buffers->ecccalc;
2195 for (i = 0; i < chip->ecc.total; i++)
2196 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2197
2198 /* write OOB buffer to NAND device */
2199 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2200
2201 return 0;
2202}
2203
2204
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002205/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002206 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002207 * @mtd: mtd info structure
2208 * @chip: nand chip info structure
2209 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002210 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002211 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002212 * The hw generator calculates the error syndrome automatically. Therefore we
2213 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002214 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002215static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002216 struct nand_chip *chip,
2217 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002218{
2219 int i, eccsize = chip->ecc.size;
2220 int eccbytes = chip->ecc.bytes;
2221 int eccsteps = chip->ecc.steps;
2222 const uint8_t *p = buf;
2223 uint8_t *oob = chip->oob_poi;
2224
2225 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2226
2227 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2228 chip->write_buf(mtd, p, eccsize);
2229
2230 if (chip->ecc.prepad) {
2231 chip->write_buf(mtd, oob, chip->ecc.prepad);
2232 oob += chip->ecc.prepad;
2233 }
2234
2235 chip->ecc.calculate(mtd, p, oob);
2236 chip->write_buf(mtd, oob, eccbytes);
2237 oob += eccbytes;
2238
2239 if (chip->ecc.postpad) {
2240 chip->write_buf(mtd, oob, chip->ecc.postpad);
2241 oob += chip->ecc.postpad;
2242 }
2243 }
2244
2245 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002246 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002247 if (i)
2248 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002249
2250 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002251}
2252
2253/**
David Woodhouse956e9442006-09-25 17:12:39 +01002254 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002255 * @mtd: MTD device structure
2256 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302257 * @offset: address offset within the page
2258 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002259 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002260 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002261 * @page: page number to write
2262 * @cached: cached programming
2263 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002264 */
2265static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302266 uint32_t offset, int data_len, const uint8_t *buf,
2267 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002268{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302269 int status, subpage;
2270
2271 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2272 chip->ecc.write_subpage)
2273 subpage = offset || (data_len < mtd->writesize);
2274 else
2275 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002276
2277 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2278
David Woodhouse956e9442006-09-25 17:12:39 +01002279 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302280 status = chip->ecc.write_page_raw(mtd, chip, buf,
2281 oob_required);
2282 else if (subpage)
2283 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2284 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002285 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002286 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2287
2288 if (status < 0)
2289 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002290
2291 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002292 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002293 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002294 */
2295 cached = 0;
2296
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002297 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002298
2299 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002300 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002301 /*
2302 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002303 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002304 */
2305 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2306 status = chip->errstat(mtd, chip, FL_WRITING, status,
2307 page);
2308
2309 if (status & NAND_STATUS_FAIL)
2310 return -EIO;
2311 } else {
2312 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002313 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002314 }
2315
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002316 return 0;
2317}
2318
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002319/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002320 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002321 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002322 * @oob: oob data buffer
2323 * @len: oob data write length
2324 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002325 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002326static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2327 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002328{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002329 struct nand_chip *chip = mtd->priv;
2330
2331 /*
2332 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2333 * data from a previous OOB read.
2334 */
2335 memset(chip->oob_poi, 0xff, mtd->oobsize);
2336
Florian Fainellif8ac0412010-09-07 13:23:43 +02002337 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002338
Brian Norris0612b9d2011-08-30 18:45:40 -07002339 case MTD_OPS_PLACE_OOB:
2340 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002341 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2342 return oob + len;
2343
Brian Norris0612b9d2011-08-30 18:45:40 -07002344 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002345 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002346 uint32_t boffs = 0, woffs = ops->ooboffs;
2347 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002348
Florian Fainellif8ac0412010-09-07 13:23:43 +02002349 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002350 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002351 if (unlikely(woffs)) {
2352 if (woffs >= free->length) {
2353 woffs -= free->length;
2354 continue;
2355 }
2356 boffs = free->offset + woffs;
2357 bytes = min_t(size_t, len,
2358 (free->length - woffs));
2359 woffs = 0;
2360 } else {
2361 bytes = min_t(size_t, len, free->length);
2362 boffs = free->offset;
2363 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002364 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002365 oob += bytes;
2366 }
2367 return oob;
2368 }
2369 default:
2370 BUG();
2371 }
2372 return NULL;
2373}
2374
Florian Fainellif8ac0412010-09-07 13:23:43 +02002375#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002376
2377/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002378 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002379 * @mtd: MTD device structure
2380 * @to: offset to write to
2381 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002382 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002383 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002384 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002385static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2386 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002387{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002388 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002389 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002390 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002391
2392 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002393 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002394 mtd->oobavail : mtd->oobsize;
2395
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002396 uint8_t *oob = ops->oobbuf;
2397 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302398 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002399 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002400
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002401 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002402 if (!writelen)
2403 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002404
Brian Norris8b6e50c2011-05-25 14:59:01 -07002405 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002406 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002407 pr_notice("%s: attempt to write non page aligned data\n",
2408 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002409 return -EINVAL;
2410 }
2411
Thomas Gleixner29072b92006-09-28 15:38:36 +02002412 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002413
Thomas Gleixner6a930962006-06-28 00:11:45 +02002414 chipnr = (int)(to >> chip->chip_shift);
2415 chip->select_chip(mtd, chipnr);
2416
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002417 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002418 if (nand_check_wp(mtd)) {
2419 ret = -EIO;
2420 goto err_out;
2421 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002422
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002423 realpage = (int)(to >> chip->page_shift);
2424 page = realpage & chip->pagemask;
2425 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2426
2427 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002428 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2429 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002430 chip->pagebuf = -1;
2431
Maxim Levitsky782ce792010-02-22 20:39:36 +02002432 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002433 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2434 ret = -EINVAL;
2435 goto err_out;
2436 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002437
Florian Fainellif8ac0412010-09-07 13:23:43 +02002438 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002439 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002440 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002441 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002442 int use_bufpoi;
2443 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002444
Kamal Dasu66507c72014-05-01 20:51:19 -04002445 if (part_pagewr)
2446 use_bufpoi = 1;
2447 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2448 use_bufpoi = !virt_addr_valid(buf);
2449 else
2450 use_bufpoi = 0;
2451
2452 /* Partial page write?, or need to use bounce buffer */
2453 if (use_bufpoi) {
2454 pr_debug("%s: using write bounce buffer for buf@%p\n",
2455 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002456 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002457 if (part_pagewr)
2458 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002459 chip->pagebuf = -1;
2460 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2461 memcpy(&chip->buffers->databuf[column], buf, bytes);
2462 wbuf = chip->buffers->databuf;
2463 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002464
Maxim Levitsky782ce792010-02-22 20:39:36 +02002465 if (unlikely(oob)) {
2466 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002467 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002468 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002469 } else {
2470 /* We still need to erase leftover OOB data */
2471 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002472 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302473 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2474 oob_required, page, cached,
2475 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002476 if (ret)
2477 break;
2478
2479 writelen -= bytes;
2480 if (!writelen)
2481 break;
2482
Thomas Gleixner29072b92006-09-28 15:38:36 +02002483 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002484 buf += bytes;
2485 realpage++;
2486
2487 page = realpage & chip->pagemask;
2488 /* Check, if we cross a chip boundary */
2489 if (!page) {
2490 chipnr++;
2491 chip->select_chip(mtd, -1);
2492 chip->select_chip(mtd, chipnr);
2493 }
2494 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002495
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002496 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002497 if (unlikely(oob))
2498 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002499
2500err_out:
2501 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002502 return ret;
2503}
2504
2505/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002506 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002507 * @mtd: MTD device structure
2508 * @to: offset to write to
2509 * @len: number of bytes to write
2510 * @retlen: pointer to variable to store the number of written bytes
2511 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002512 *
2513 * NAND write with ECC. Used when performing writes in interrupt context, this
2514 * may for example be called by mtdoops when writing an oops while in panic.
2515 */
2516static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2517 size_t *retlen, const uint8_t *buf)
2518{
2519 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002520 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002521 int ret;
2522
Brian Norris8b6e50c2011-05-25 14:59:01 -07002523 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002524 panic_nand_wait(mtd, chip, 400);
2525
Brian Norris8b6e50c2011-05-25 14:59:01 -07002526 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002527 panic_nand_get_device(chip, mtd, FL_WRITING);
2528
Brian Norris0ec56dc2015-02-28 02:02:30 -08002529 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002530 ops.len = len;
2531 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002532 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002533
Brian Norris4a89ff82011-08-30 18:45:45 -07002534 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002535
Brian Norris4a89ff82011-08-30 18:45:45 -07002536 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002537 return ret;
2538}
2539
2540/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002541 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002542 * @mtd: MTD device structure
2543 * @to: offset to write to
2544 * @len: number of bytes to write
2545 * @retlen: pointer to variable to store the number of written bytes
2546 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002548 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002550static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002551 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Brian Norris4a89ff82011-08-30 18:45:45 -07002553 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002554 int ret;
2555
Huang Shijie6a8214a2012-11-19 14:43:30 +08002556 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002557 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002558 ops.len = len;
2559 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002560 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002561 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002562 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002563 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002564 return ret;
2565}
2566
2567/**
2568 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002569 * @mtd: MTD device structure
2570 * @to: offset to write to
2571 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002572 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002573 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002574 */
2575static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2576 struct mtd_oob_ops *ops)
2577{
Adrian Hunter03736152007-01-31 17:58:29 +02002578 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002579 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Brian Norris289c0522011-07-19 10:06:09 -07002581 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302582 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Brian Norris0612b9d2011-08-30 18:45:40 -07002584 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002585 len = chip->ecc.layout->oobavail;
2586 else
2587 len = mtd->oobsize;
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002590 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002591 pr_debug("%s: attempt to write past end of page\n",
2592 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return -EINVAL;
2594 }
2595
Adrian Hunter03736152007-01-31 17:58:29 +02002596 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002597 pr_debug("%s: attempt to start write outside oob\n",
2598 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002599 return -EINVAL;
2600 }
2601
Jason Liu775adc32011-02-25 13:06:18 +08002602 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002603 if (unlikely(to >= mtd->size ||
2604 ops->ooboffs + ops->ooblen >
2605 ((mtd->size >> chip->page_shift) -
2606 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002607 pr_debug("%s: attempt to write beyond end of device\n",
2608 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002609 return -EINVAL;
2610 }
2611
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002612 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002613 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002615 /* Shift to get page */
2616 page = (int)(to >> chip->page_shift);
2617
2618 /*
2619 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2620 * of my DiskOnChip 2000 test units) will clear the whole data page too
2621 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2622 * it in the doc2000 driver in August 1999. dwmw2.
2623 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002624 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002627 if (nand_check_wp(mtd)) {
2628 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002629 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002630 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002633 if (page == chip->pagebuf)
2634 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002636 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002637
Brian Norris0612b9d2011-08-30 18:45:40 -07002638 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002639 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2640 else
2641 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002642
Huang Shijieb0bb6902012-11-19 14:43:29 +08002643 chip->select_chip(mtd, -1);
2644
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002645 if (status)
2646 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Vitaly Wool70145682006-11-03 18:20:38 +03002648 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002650 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002653/**
2654 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002655 * @mtd: MTD device structure
2656 * @to: offset to write to
2657 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002658 */
2659static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2660 struct mtd_oob_ops *ops)
2661{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002662 int ret = -ENOTSUPP;
2663
2664 ops->retlen = 0;
2665
2666 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002667 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002668 pr_debug("%s: attempt to write beyond end of device\n",
2669 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002670 return -EINVAL;
2671 }
2672
Huang Shijie6a8214a2012-11-19 14:43:30 +08002673 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002674
Florian Fainellif8ac0412010-09-07 13:23:43 +02002675 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002676 case MTD_OPS_PLACE_OOB:
2677 case MTD_OPS_AUTO_OOB:
2678 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002679 break;
2680
2681 default:
2682 goto out;
2683 }
2684
2685 if (!ops->datbuf)
2686 ret = nand_do_write_oob(mtd, to, ops);
2687 else
2688 ret = nand_do_write_ops(mtd, to, ops);
2689
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002690out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002691 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return ret;
2693}
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695/**
Brian Norris49c50b92014-05-06 16:02:19 -07002696 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002697 * @mtd: MTD device structure
2698 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 *
Brian Norris49c50b92014-05-06 16:02:19 -07002700 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 */
Brian Norris49c50b92014-05-06 16:02:19 -07002702static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002704 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002706 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2707 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002708
2709 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
2712/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002714 * @mtd: MTD device structure
2715 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002717 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002719static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
David Woodhousee0c7d762006-05-13 18:07:53 +01002721 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002725 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002726 * @mtd: MTD device structure
2727 * @instr: erase instruction
2728 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002730 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002732int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2733 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Adrian Hunter69423d92008-12-10 13:37:21 +00002735 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002736 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002737 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Brian Norris289c0522011-07-19 10:06:09 -07002739 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2740 __func__, (unsigned long long)instr->addr,
2741 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302743 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002747 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002750 page = (int)(instr->addr >> chip->page_shift);
2751 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002754 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002757 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /* Check, if it is write protected */
2760 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002761 pr_debug("%s: device is write protected!\n",
2762 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 instr->state = MTD_ERASE_FAILED;
2764 goto erase_exit;
2765 }
2766
2767 /* Loop through the pages */
2768 len = instr->len;
2769
2770 instr->state = MTD_ERASING;
2771
2772 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002773 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002774 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2775 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002776 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2777 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 instr->state = MTD_ERASE_FAILED;
2779 goto erase_exit;
2780 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002781
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002782 /*
2783 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002784 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002785 */
2786 if (page <= chip->pagebuf && chip->pagebuf <
2787 (page + pages_per_block))
2788 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Brian Norris49c50b92014-05-06 16:02:19 -07002790 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002792 /*
2793 * See if operation failed and additional status checks are
2794 * available
2795 */
2796 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2797 status = chip->errstat(mtd, chip, FL_ERASING,
2798 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002801 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002802 pr_debug("%s: failed erase, page 0x%08x\n",
2803 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002805 instr->fail_addr =
2806 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 goto erase_exit;
2808 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002811 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 page += pages_per_block;
2813
2814 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002815 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002817 chip->select_chip(mtd, -1);
2818 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820 }
2821 instr->state = MTD_ERASE_DONE;
2822
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002823erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002828 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 nand_release_device(mtd);
2830
David Woodhouse49defc02007-10-06 15:01:59 -04002831 /* Do call back function */
2832 if (!ret)
2833 mtd_erase_callback(instr);
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 /* Return more or less happy */
2836 return ret;
2837}
2838
2839/**
2840 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002841 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002843 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002845static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Brian Norris289c0522011-07-19 10:06:09 -07002847 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002850 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002852 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002856 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002857 * @mtd: MTD device structure
2858 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002860static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002862 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
2865/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002866 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002867 * @mtd: MTD device structure
2868 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002870static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 int ret;
2873
Florian Fainellif8ac0412010-09-07 13:23:43 +02002874 ret = nand_block_isbad(mtd, ofs);
2875 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002876 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (ret > 0)
2878 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002879 return ret;
2880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Brian Norris5a0edb22013-07-30 17:52:58 -07002882 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
2885/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002886 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2887 * @mtd: MTD device structure
2888 * @chip: nand chip info structure
2889 * @addr: feature address.
2890 * @subfeature_param: the subfeature parameters, a four bytes array.
2891 */
2892static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2893 int addr, uint8_t *subfeature_param)
2894{
2895 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002896 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002897
David Mosbergerd914c932013-05-29 15:30:13 +03002898 if (!chip->onfi_version ||
2899 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2900 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002901 return -EINVAL;
2902
2903 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002904 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2905 chip->write_byte(mtd, subfeature_param[i]);
2906
Huang Shijie7db03ec2012-09-13 14:57:52 +08002907 status = chip->waitfunc(mtd, chip);
2908 if (status & NAND_STATUS_FAIL)
2909 return -EIO;
2910 return 0;
2911}
2912
2913/**
2914 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2915 * @mtd: MTD device structure
2916 * @chip: nand chip info structure
2917 * @addr: feature address.
2918 * @subfeature_param: the subfeature parameters, a four bytes array.
2919 */
2920static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2921 int addr, uint8_t *subfeature_param)
2922{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002923 int i;
2924
David Mosbergerd914c932013-05-29 15:30:13 +03002925 if (!chip->onfi_version ||
2926 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2927 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002928 return -EINVAL;
2929
Huang Shijie7db03ec2012-09-13 14:57:52 +08002930 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002931 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2932 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002933 return 0;
2934}
2935
2936/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002937 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002938 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002939 */
2940static int nand_suspend(struct mtd_info *mtd)
2941{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002942 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002943}
2944
2945/**
2946 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002947 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002948 */
2949static void nand_resume(struct mtd_info *mtd)
2950{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002951 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002952
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002953 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002954 nand_release_device(mtd);
2955 else
Brian Norrisd0370212011-07-19 10:06:08 -07002956 pr_err("%s called for a chip which is not in suspended state\n",
2957 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002958}
2959
Scott Branden72ea4032014-11-20 11:18:05 -08002960/**
2961 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
2962 * prevent further operations
2963 * @mtd: MTD device structure
2964 */
2965static void nand_shutdown(struct mtd_info *mtd)
2966{
2967 nand_get_device(mtd, FL_SHUTDOWN);
2968}
2969
Brian Norris8b6e50c2011-05-25 14:59:01 -07002970/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002971static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002972{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002974 if (!chip->chip_delay)
2975 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002978 if (chip->cmdfunc == NULL)
2979 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002982 if (chip->waitfunc == NULL)
2983 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002985 if (!chip->select_chip)
2986 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002987
Huang Shijie4204ccc2013-08-16 10:10:07 +08002988 /* set for ONFI nand */
2989 if (!chip->onfi_set_features)
2990 chip->onfi_set_features = nand_onfi_set_features;
2991 if (!chip->onfi_get_features)
2992 chip->onfi_get_features = nand_onfi_get_features;
2993
Brian Norris68e80782013-07-18 01:17:02 -07002994 /* If called twice, pointers that depend on busw may need to be reset */
2995 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002996 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2997 if (!chip->read_word)
2998 chip->read_word = nand_read_word;
2999 if (!chip->block_bad)
3000 chip->block_bad = nand_block_bad;
3001 if (!chip->block_markbad)
3002 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003003 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003004 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003005 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3006 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003007 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003008 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003009 if (!chip->scan_bbt)
3010 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003011
3012 if (!chip->controller) {
3013 chip->controller = &chip->hwcontrol;
3014 spin_lock_init(&chip->controller->lock);
3015 init_waitqueue_head(&chip->controller->wq);
3016 }
3017
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003018}
3019
Brian Norris8b6e50c2011-05-25 14:59:01 -07003020/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003021static void sanitize_string(uint8_t *s, size_t len)
3022{
3023 ssize_t i;
3024
Brian Norris8b6e50c2011-05-25 14:59:01 -07003025 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003026 s[len - 1] = 0;
3027
Brian Norris8b6e50c2011-05-25 14:59:01 -07003028 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003029 for (i = 0; i < len - 1; i++) {
3030 if (s[i] < ' ' || s[i] > 127)
3031 s[i] = '?';
3032 }
3033
Brian Norris8b6e50c2011-05-25 14:59:01 -07003034 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003035 strim(s);
3036}
3037
3038static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3039{
3040 int i;
3041 while (len--) {
3042 crc ^= *p++ << 8;
3043 for (i = 0; i < 8; i++)
3044 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3045 }
3046
3047 return crc;
3048}
3049
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003050/* Parse the Extended Parameter Page. */
3051static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3052 struct nand_chip *chip, struct nand_onfi_params *p)
3053{
3054 struct onfi_ext_param_page *ep;
3055 struct onfi_ext_section *s;
3056 struct onfi_ext_ecc_info *ecc;
3057 uint8_t *cursor;
3058 int ret = -EINVAL;
3059 int len;
3060 int i;
3061
3062 len = le16_to_cpu(p->ext_param_page_length) * 16;
3063 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003064 if (!ep)
3065 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003066
3067 /* Send our own NAND_CMD_PARAM. */
3068 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3069
3070 /* Use the Change Read Column command to skip the ONFI param pages. */
3071 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3072 sizeof(*p) * p->num_of_param_pages , -1);
3073
3074 /* Read out the Extended Parameter Page. */
3075 chip->read_buf(mtd, (uint8_t *)ep, len);
3076 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3077 != le16_to_cpu(ep->crc))) {
3078 pr_debug("fail in the CRC.\n");
3079 goto ext_out;
3080 }
3081
3082 /*
3083 * Check the signature.
3084 * Do not strictly follow the ONFI spec, maybe changed in future.
3085 */
3086 if (strncmp(ep->sig, "EPPS", 4)) {
3087 pr_debug("The signature is invalid.\n");
3088 goto ext_out;
3089 }
3090
3091 /* find the ECC section. */
3092 cursor = (uint8_t *)(ep + 1);
3093 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3094 s = ep->sections + i;
3095 if (s->type == ONFI_SECTION_TYPE_2)
3096 break;
3097 cursor += s->length * 16;
3098 }
3099 if (i == ONFI_EXT_SECTION_MAX) {
3100 pr_debug("We can not find the ECC section.\n");
3101 goto ext_out;
3102 }
3103
3104 /* get the info we want. */
3105 ecc = (struct onfi_ext_ecc_info *)cursor;
3106
Brian Norris4ae7d222013-09-16 18:20:21 -07003107 if (!ecc->codeword_size) {
3108 pr_debug("Invalid codeword size\n");
3109 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003110 }
3111
Brian Norris4ae7d222013-09-16 18:20:21 -07003112 chip->ecc_strength_ds = ecc->ecc_bits;
3113 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003114 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003115
3116ext_out:
3117 kfree(ep);
3118 return ret;
3119}
3120
Brian Norris8429bb32013-12-03 15:51:09 -08003121static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3122{
3123 struct nand_chip *chip = mtd->priv;
3124 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3125
3126 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3127 feature);
3128}
3129
3130/*
3131 * Configure chip properties from Micron vendor-specific ONFI table
3132 */
3133static void nand_onfi_detect_micron(struct nand_chip *chip,
3134 struct nand_onfi_params *p)
3135{
3136 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3137
3138 if (le16_to_cpu(p->vendor_revision) < 1)
3139 return;
3140
3141 chip->read_retries = micron->read_retry_options;
3142 chip->setup_read_retry = nand_setup_read_retry_micron;
3143}
3144
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003145/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003146 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003147 */
3148static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003149 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003150{
3151 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003152 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003153 int val;
3154
Brian Norris7854d3f2011-06-23 14:12:08 -07003155 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003156 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3157 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3158 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3159 return 0;
3160
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003161 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3162 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003163 for (j = 0; j < sizeof(*p); j++)
3164 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003165 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3166 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003167 break;
3168 }
3169 }
3170
Brian Norrisc7f23a72013-08-13 10:51:55 -07003171 if (i == 3) {
3172 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003173 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003174 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003175
Brian Norris8b6e50c2011-05-25 14:59:01 -07003176 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003177 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003178 if (val & (1 << 5))
3179 chip->onfi_version = 23;
3180 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003181 chip->onfi_version = 22;
3182 else if (val & (1 << 3))
3183 chip->onfi_version = 21;
3184 else if (val & (1 << 2))
3185 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003186 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003187 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003188
3189 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003190 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003191 return 0;
3192 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003193
3194 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3195 sanitize_string(p->model, sizeof(p->model));
3196 if (!mtd->name)
3197 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003198
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003199 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003200
3201 /*
3202 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3203 * (don't ask me who thought of this...). MTD assumes that these
3204 * dimensions will be power-of-2, so just truncate the remaining area.
3205 */
3206 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3207 mtd->erasesize *= mtd->writesize;
3208
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003209 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003210
3211 /* See erasesize comment */
3212 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003213 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003214 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003215
3216 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003217 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003218 else
3219 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003220
Huang Shijie10c86ba2013-05-17 11:17:26 +08003221 if (p->ecc_bits != 0xff) {
3222 chip->ecc_strength_ds = p->ecc_bits;
3223 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003224 } else if (chip->onfi_version >= 21 &&
3225 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3226
3227 /*
3228 * The nand_flash_detect_ext_param_page() uses the
3229 * Change Read Column command which maybe not supported
3230 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3231 * now. We do not replace user supplied command function.
3232 */
3233 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3234 chip->cmdfunc = nand_command_lp;
3235
3236 /* The Extended Parameter Page is supported since ONFI 2.1. */
3237 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003238 pr_warn("Failed to detect ONFI extended param page\n");
3239 } else {
3240 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003241 }
3242
Brian Norris8429bb32013-12-03 15:51:09 -08003243 if (p->jedec_id == NAND_MFR_MICRON)
3244 nand_onfi_detect_micron(chip, p);
3245
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003246 return 1;
3247}
3248
3249/*
Huang Shijie91361812014-02-21 13:39:40 +08003250 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3251 */
3252static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3253 int *busw)
3254{
3255 struct nand_jedec_params *p = &chip->jedec_params;
3256 struct jedec_ecc_info *ecc;
3257 int val;
3258 int i, j;
3259
3260 /* Try JEDEC for unknown chip or LP */
3261 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3262 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3263 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3264 chip->read_byte(mtd) != 'C')
3265 return 0;
3266
3267 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3268 for (i = 0; i < 3; i++) {
3269 for (j = 0; j < sizeof(*p); j++)
3270 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3271
3272 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3273 le16_to_cpu(p->crc))
3274 break;
3275 }
3276
3277 if (i == 3) {
3278 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3279 return 0;
3280 }
3281
3282 /* Check version */
3283 val = le16_to_cpu(p->revision);
3284 if (val & (1 << 2))
3285 chip->jedec_version = 10;
3286 else if (val & (1 << 1))
3287 chip->jedec_version = 1; /* vendor specific version */
3288
3289 if (!chip->jedec_version) {
3290 pr_info("unsupported JEDEC version: %d\n", val);
3291 return 0;
3292 }
3293
3294 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3295 sanitize_string(p->model, sizeof(p->model));
3296 if (!mtd->name)
3297 mtd->name = p->model;
3298
3299 mtd->writesize = le32_to_cpu(p->byte_per_page);
3300
3301 /* Please reference to the comment for nand_flash_detect_onfi. */
3302 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3303 mtd->erasesize *= mtd->writesize;
3304
3305 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3306
3307 /* Please reference to the comment for nand_flash_detect_onfi. */
3308 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3309 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3310 chip->bits_per_cell = p->bits_per_cell;
3311
3312 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3313 *busw = NAND_BUSWIDTH_16;
3314 else
3315 *busw = 0;
3316
3317 /* ECC info */
3318 ecc = &p->ecc_info[0];
3319
3320 if (ecc->codeword_size >= 9) {
3321 chip->ecc_strength_ds = ecc->ecc_bits;
3322 chip->ecc_step_ds = 1 << ecc->codeword_size;
3323 } else {
3324 pr_warn("Invalid codeword size\n");
3325 }
3326
3327 return 1;
3328}
3329
3330/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003331 * nand_id_has_period - Check if an ID string has a given wraparound period
3332 * @id_data: the ID string
3333 * @arrlen: the length of the @id_data array
3334 * @period: the period of repitition
3335 *
3336 * Check if an ID string is repeated within a given sequence of bytes at
3337 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003338 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003339 * if the repetition has a period of @period; otherwise, returns zero.
3340 */
3341static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3342{
3343 int i, j;
3344 for (i = 0; i < period; i++)
3345 for (j = i + period; j < arrlen; j += period)
3346 if (id_data[i] != id_data[j])
3347 return 0;
3348 return 1;
3349}
3350
3351/*
3352 * nand_id_len - Get the length of an ID string returned by CMD_READID
3353 * @id_data: the ID string
3354 * @arrlen: the length of the @id_data array
3355
3356 * Returns the length of the ID string, according to known wraparound/trailing
3357 * zero patterns. If no pattern exists, returns the length of the array.
3358 */
3359static int nand_id_len(u8 *id_data, int arrlen)
3360{
3361 int last_nonzero, period;
3362
3363 /* Find last non-zero byte */
3364 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3365 if (id_data[last_nonzero])
3366 break;
3367
3368 /* All zeros */
3369 if (last_nonzero < 0)
3370 return 0;
3371
3372 /* Calculate wraparound period */
3373 for (period = 1; period < arrlen; period++)
3374 if (nand_id_has_period(id_data, arrlen, period))
3375 break;
3376
3377 /* There's a repeated pattern */
3378 if (period < arrlen)
3379 return period;
3380
3381 /* There are trailing zeros */
3382 if (last_nonzero < arrlen - 1)
3383 return last_nonzero + 1;
3384
3385 /* No pattern detected */
3386 return arrlen;
3387}
3388
Huang Shijie7db906b2013-09-25 14:58:11 +08003389/* Extract the bits of per cell from the 3rd byte of the extended ID */
3390static int nand_get_bits_per_cell(u8 cellinfo)
3391{
3392 int bits;
3393
3394 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3395 bits >>= NAND_CI_CELLTYPE_SHIFT;
3396 return bits + 1;
3397}
3398
Brian Norrise3b88bd2012-09-24 20:40:52 -07003399/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003400 * Many new NAND share similar device ID codes, which represent the size of the
3401 * chip. The rest of the parameters must be decoded according to generic or
3402 * manufacturer-specific "extended ID" decoding patterns.
3403 */
3404static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3405 u8 id_data[8], int *busw)
3406{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003407 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003408 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003409 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003410 /* The 4th id byte is the important one */
3411 extid = id_data[3];
3412
Brian Norrise3b88bd2012-09-24 20:40:52 -07003413 id_len = nand_id_len(id_data, 8);
3414
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003415 /*
3416 * Field definitions are in the following datasheets:
3417 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003418 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003419 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003420 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003421 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3422 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003423 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003424 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003425 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003426 /* Calc pagesize */
3427 mtd->writesize = 2048 << (extid & 0x03);
3428 extid >>= 2;
3429 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003430 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003431 case 1:
3432 mtd->oobsize = 128;
3433 break;
3434 case 2:
3435 mtd->oobsize = 218;
3436 break;
3437 case 3:
3438 mtd->oobsize = 400;
3439 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003440 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003441 mtd->oobsize = 436;
3442 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003443 case 5:
3444 mtd->oobsize = 512;
3445 break;
3446 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003447 mtd->oobsize = 640;
3448 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003449 case 7:
3450 default: /* Other cases are "reserved" (unknown) */
3451 mtd->oobsize = 1024;
3452 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003453 }
3454 extid >>= 2;
3455 /* Calc blocksize */
3456 mtd->erasesize = (128 * 1024) <<
3457 (((extid >> 1) & 0x04) | (extid & 0x03));
3458 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003459 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003460 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003461 unsigned int tmp;
3462
3463 /* Calc pagesize */
3464 mtd->writesize = 2048 << (extid & 0x03);
3465 extid >>= 2;
3466 /* Calc oobsize */
3467 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3468 case 0:
3469 mtd->oobsize = 128;
3470 break;
3471 case 1:
3472 mtd->oobsize = 224;
3473 break;
3474 case 2:
3475 mtd->oobsize = 448;
3476 break;
3477 case 3:
3478 mtd->oobsize = 64;
3479 break;
3480 case 4:
3481 mtd->oobsize = 32;
3482 break;
3483 case 5:
3484 mtd->oobsize = 16;
3485 break;
3486 default:
3487 mtd->oobsize = 640;
3488 break;
3489 }
3490 extid >>= 2;
3491 /* Calc blocksize */
3492 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3493 if (tmp < 0x03)
3494 mtd->erasesize = (128 * 1024) << tmp;
3495 else if (tmp == 0x03)
3496 mtd->erasesize = 768 * 1024;
3497 else
3498 mtd->erasesize = (64 * 1024) << tmp;
3499 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003500 } else {
3501 /* Calc pagesize */
3502 mtd->writesize = 1024 << (extid & 0x03);
3503 extid >>= 2;
3504 /* Calc oobsize */
3505 mtd->oobsize = (8 << (extid & 0x01)) *
3506 (mtd->writesize >> 9);
3507 extid >>= 2;
3508 /* Calc blocksize. Blocksize is multiples of 64KiB */
3509 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3510 extid >>= 2;
3511 /* Get buswidth information */
3512 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003513
3514 /*
3515 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3516 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3517 * follows:
3518 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3519 * 110b -> 24nm
3520 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3521 */
3522 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003523 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003524 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3525 !(id_data[4] & 0x80) /* !BENAND */) {
3526 mtd->oobsize = 32 * mtd->writesize >> 9;
3527 }
3528
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003529 }
3530}
3531
3532/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003533 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3534 * decodes a matching ID table entry and assigns the MTD size parameters for
3535 * the chip.
3536 */
3537static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3538 struct nand_flash_dev *type, u8 id_data[8],
3539 int *busw)
3540{
3541 int maf_id = id_data[0];
3542
3543 mtd->erasesize = type->erasesize;
3544 mtd->writesize = type->pagesize;
3545 mtd->oobsize = mtd->writesize / 32;
3546 *busw = type->options & NAND_BUSWIDTH_16;
3547
Huang Shijie1c195e92013-09-25 14:58:12 +08003548 /* All legacy ID NAND are small-page, SLC */
3549 chip->bits_per_cell = 1;
3550
Brian Norrisf23a4812012-09-24 20:40:51 -07003551 /*
3552 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3553 * some Spansion chips have erasesize that conflicts with size
3554 * listed in nand_ids table.
3555 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3556 */
3557 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3558 && id_data[6] == 0x00 && id_data[7] == 0x00
3559 && mtd->writesize == 512) {
3560 mtd->erasesize = 128 * 1024;
3561 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3562 }
3563}
3564
3565/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003566 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3567 * heuristic patterns using various detected parameters (e.g., manufacturer,
3568 * page size, cell-type information).
3569 */
3570static void nand_decode_bbm_options(struct mtd_info *mtd,
3571 struct nand_chip *chip, u8 id_data[8])
3572{
3573 int maf_id = id_data[0];
3574
3575 /* Set the bad block position */
3576 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3577 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3578 else
3579 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3580
3581 /*
3582 * Bad block marker is stored in the last page of each block on Samsung
3583 * and Hynix MLC devices; stored in first two pages of each block on
3584 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3585 * AMD/Spansion, and Macronix. All others scan only the first page.
3586 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003587 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003588 (maf_id == NAND_MFR_SAMSUNG ||
3589 maf_id == NAND_MFR_HYNIX))
3590 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003591 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003592 (maf_id == NAND_MFR_SAMSUNG ||
3593 maf_id == NAND_MFR_HYNIX ||
3594 maf_id == NAND_MFR_TOSHIBA ||
3595 maf_id == NAND_MFR_AMD ||
3596 maf_id == NAND_MFR_MACRONIX)) ||
3597 (mtd->writesize == 2048 &&
3598 maf_id == NAND_MFR_MICRON))
3599 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3600}
3601
Huang Shijieec6e87e2013-03-15 11:01:00 +08003602static inline bool is_full_id_nand(struct nand_flash_dev *type)
3603{
3604 return type->id_len;
3605}
3606
3607static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3608 struct nand_flash_dev *type, u8 *id_data, int *busw)
3609{
3610 if (!strncmp(type->id, id_data, type->id_len)) {
3611 mtd->writesize = type->pagesize;
3612 mtd->erasesize = type->erasesize;
3613 mtd->oobsize = type->oobsize;
3614
Huang Shijie7db906b2013-09-25 14:58:11 +08003615 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003616 chip->chipsize = (uint64_t)type->chipsize << 20;
3617 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003618 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3619 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003620 chip->onfi_timing_mode_default =
3621 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003622
3623 *busw = type->options & NAND_BUSWIDTH_16;
3624
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003625 if (!mtd->name)
3626 mtd->name = type->name;
3627
Huang Shijieec6e87e2013-03-15 11:01:00 +08003628 return true;
3629 }
3630 return false;
3631}
3632
Brian Norris7e74c2d2012-09-24 20:40:49 -07003633/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003634 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003635 */
3636static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003637 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003638 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003639 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003640{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003641 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003642 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003643 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003646 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Karl Beldanef89a882008-09-15 14:37:29 +02003648 /*
3649 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003650 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003651 */
3652 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003655 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003658 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003659 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Brian Norris8b6e50c2011-05-25 14:59:01 -07003661 /*
3662 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003663 * interface concerns can cause random data which looks like a
3664 * possibly credible NAND flash to appear. If the two results do
3665 * not match, ignore the device completely.
3666 */
3667
3668 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3669
Brian Norris4aef9b72012-09-24 20:40:48 -07003670 /* Read entire ID string */
3671 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003672 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003673
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003674 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003675 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003676 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003677 return ERR_PTR(-ENODEV);
3678 }
3679
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003680 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003681 type = nand_flash_ids;
3682
Huang Shijieec6e87e2013-03-15 11:01:00 +08003683 for (; type->name != NULL; type++) {
3684 if (is_full_id_nand(type)) {
3685 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3686 goto ident_done;
3687 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003688 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003689 }
3690 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003691
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003692 chip->onfi_version = 0;
3693 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003694 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003695 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003696 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003697
3698 /* Check if the chip is JEDEC compliant */
3699 if (nand_flash_detect_jedec(mtd, chip, &busw))
3700 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003701 }
3702
David Woodhouse5e81e882010-02-26 18:32:56 +00003703 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003704 return ERR_PTR(-ENODEV);
3705
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003706 if (!mtd->name)
3707 mtd->name = type->name;
3708
Adrian Hunter69423d92008-12-10 13:37:21 +00003709 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003710
Huang Shijie12a40a52010-09-27 10:43:53 +08003711 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003712 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003713 busw = chip->init_size(mtd, chip, id_data);
3714 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003715 /* Decode parameters from extended ID */
3716 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003717 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003718 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003719 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003720 /* Get chip options */
3721 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003722
Brian Norris8b6e50c2011-05-25 14:59:01 -07003723 /*
3724 * Check if chip is not a Samsung device. Do not clear the
3725 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003726 */
3727 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3728 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3729ident_done:
3730
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003731 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003732 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003733 if (nand_manuf_ids[maf_idx].id == *maf_id)
3734 break;
3735 }
3736
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003737 if (chip->options & NAND_BUSWIDTH_AUTO) {
3738 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3739 chip->options |= busw;
3740 nand_set_defaults(chip, busw);
3741 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3742 /*
3743 * Check, if buswidth is correct. Hardware drivers should set
3744 * chip correct!
3745 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003746 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3747 *maf_id, *dev_id);
3748 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3749 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003750 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3751 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003752 return ERR_PTR(-EINVAL);
3753 }
3754
Brian Norris7e74c2d2012-09-24 20:40:49 -07003755 nand_decode_bbm_options(mtd, chip, id_data);
3756
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003757 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003758 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003759 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003760 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003761
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003762 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003763 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003764 if (chip->chipsize & 0xffffffff)
3765 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003766 else {
3767 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3768 chip->chip_shift += 32 - 1;
3769 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003770
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003771 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003772 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003773
Brian Norris8b6e50c2011-05-25 14:59:01 -07003774 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003775 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3776 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003777
Ezequiel Garcia20171642013-11-25 08:30:31 -03003778 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3779 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003780
3781 if (chip->onfi_version)
3782 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3783 chip->onfi_params.model);
3784 else if (chip->jedec_version)
3785 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3786 chip->jedec_params.model);
3787 else
3788 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3789 type->name);
3790
Rafał Miłecki3755a992014-10-21 00:01:04 +02003791 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003792 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003793 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003794 return type;
3795}
3796
Brian Norris5844fee2015-01-23 00:22:27 -08003797static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip,
3798 struct device_node *dn)
3799{
3800 int ecc_mode, ecc_strength, ecc_step;
3801
3802 if (of_get_nand_bus_width(dn) == 16)
3803 chip->options |= NAND_BUSWIDTH_16;
3804
3805 if (of_get_nand_on_flash_bbt(dn))
3806 chip->bbt_options |= NAND_BBT_USE_FLASH;
3807
3808 ecc_mode = of_get_nand_ecc_mode(dn);
3809 ecc_strength = of_get_nand_ecc_strength(dn);
3810 ecc_step = of_get_nand_ecc_step_size(dn);
3811
3812 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3813 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3814 pr_err("must set both strength and step size in DT\n");
3815 return -EINVAL;
3816 }
3817
3818 if (ecc_mode >= 0)
3819 chip->ecc.mode = ecc_mode;
3820
3821 if (ecc_strength >= 0)
3822 chip->ecc.strength = ecc_strength;
3823
3824 if (ecc_step > 0)
3825 chip->ecc.size = ecc_step;
3826
3827 return 0;
3828}
3829
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003830/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003831 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003832 * @mtd: MTD device structure
3833 * @maxchips: number of chips to scan for
3834 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003835 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003836 * This is the first phase of the normal nand_scan() function. It reads the
3837 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003838 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003839 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003840 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003841int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3842 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003843{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003844 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003845 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003846 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08003847 int ret;
3848
3849 if (chip->dn) {
3850 ret = nand_dt_init(mtd, chip, chip->dn);
3851 if (ret)
3852 return ret;
3853 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003854
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003855 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003856 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003857
3858 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003859 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3860 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003861
3862 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003863 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003864 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003865 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003866 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
3868
Huang Shijie07300162012-11-09 16:23:45 +08003869 chip->select_chip(mtd, -1);
3870
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003871 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003872 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003873 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003874 /* See comment in nand_get_flash_type for reset */
3875 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003877 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003879 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003880 nand_dev_id != chip->read_byte(mtd)) {
3881 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 break;
Huang Shijie07300162012-11-09 16:23:45 +08003883 }
3884 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 }
3886 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003887 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003890 chip->numchips = i;
3891 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
David Woodhouse3b85c322006-09-25 17:06:53 +01003893 return 0;
3894}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003895EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003896
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03003897/*
3898 * Check if the chip configuration meet the datasheet requirements.
3899
3900 * If our configuration corrects A bits per B bytes and the minimum
3901 * required correction level is X bits per Y bytes, then we must ensure
3902 * both of the following are true:
3903 *
3904 * (1) A / B >= X / Y
3905 * (2) A >= X
3906 *
3907 * Requirement (1) ensures we can correct for the required bitflip density.
3908 * Requirement (2) ensures we can correct even when all bitflips are clumped
3909 * in the same sector.
3910 */
3911static bool nand_ecc_strength_good(struct mtd_info *mtd)
3912{
3913 struct nand_chip *chip = mtd->priv;
3914 struct nand_ecc_ctrl *ecc = &chip->ecc;
3915 int corr, ds_corr;
3916
3917 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3918 /* Not enough information */
3919 return true;
3920
3921 /*
3922 * We get the number of corrected bits per page to compare
3923 * the correction density.
3924 */
3925 corr = (mtd->writesize * ecc->strength) / ecc->size;
3926 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3927
3928 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3929}
David Woodhouse3b85c322006-09-25 17:06:53 +01003930
3931/**
3932 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003933 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003934 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003935 * This is the second phase of the normal nand_scan() function. It fills out
3936 * all the uninitialized function pointers with the defaults and scans for a
3937 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003938 */
3939int nand_scan_tail(struct mtd_info *mtd)
3940{
3941 int i;
3942 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003943 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08003944 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01003945
Brian Norrise2414f42012-02-06 13:44:00 -08003946 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3947 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3948 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3949
Huang Shijief02ea4e2014-01-13 14:27:12 +08003950 if (!(chip->options & NAND_OWN_BUFFERS)) {
3951 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3952 + mtd->oobsize * 3, GFP_KERNEL);
3953 if (!nbuf)
3954 return -ENOMEM;
3955 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3956 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3957 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3958
3959 chip->buffers = nbuf;
3960 } else {
3961 if (!chip->buffers)
3962 return -ENOMEM;
3963 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003964
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003965 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003966 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003967
3968 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003969 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003970 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003971 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003972 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003974 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 break;
3976 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003977 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 break;
3979 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003980 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003982 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003983 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003986 pr_warn("No oob scheme defined for oobsize %d\n",
3987 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 BUG();
3989 }
3990 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003991
David Woodhouse956e9442006-09-25 17:12:39 +01003992 if (!chip->write_page)
3993 chip->write_page = nand_write_page;
3994
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003995 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003996 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003997 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003998 */
David Woodhouse956e9442006-09-25 17:12:39 +01003999
Huang Shijie97de79e02013-10-18 14:20:53 +08004000 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004001 case NAND_ECC_HW_OOB_FIRST:
4002 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004003 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004004 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004005 BUG();
4006 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004007 if (!ecc->read_page)
4008 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004009
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004010 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004011 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004012 if (!ecc->read_page)
4013 ecc->read_page = nand_read_page_hwecc;
4014 if (!ecc->write_page)
4015 ecc->write_page = nand_write_page_hwecc;
4016 if (!ecc->read_page_raw)
4017 ecc->read_page_raw = nand_read_page_raw;
4018 if (!ecc->write_page_raw)
4019 ecc->write_page_raw = nand_write_page_raw;
4020 if (!ecc->read_oob)
4021 ecc->read_oob = nand_read_oob_std;
4022 if (!ecc->write_oob)
4023 ecc->write_oob = nand_write_oob_std;
4024 if (!ecc->read_subpage)
4025 ecc->read_subpage = nand_read_subpage;
4026 if (!ecc->write_subpage)
4027 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004028
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004029 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004030 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4031 (!ecc->read_page ||
4032 ecc->read_page == nand_read_page_hwecc ||
4033 !ecc->write_page ||
4034 ecc->write_page == nand_write_page_hwecc)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004035 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004036 BUG();
4037 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004038 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004039 if (!ecc->read_page)
4040 ecc->read_page = nand_read_page_syndrome;
4041 if (!ecc->write_page)
4042 ecc->write_page = nand_write_page_syndrome;
4043 if (!ecc->read_page_raw)
4044 ecc->read_page_raw = nand_read_page_raw_syndrome;
4045 if (!ecc->write_page_raw)
4046 ecc->write_page_raw = nand_write_page_raw_syndrome;
4047 if (!ecc->read_oob)
4048 ecc->read_oob = nand_read_oob_syndrome;
4049 if (!ecc->write_oob)
4050 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004051
Huang Shijie97de79e02013-10-18 14:20:53 +08004052 if (mtd->writesize >= ecc->size) {
4053 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07004054 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4055 BUG();
4056 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004057 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004058 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004059 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4060 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004061 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004063 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08004064 ecc->calculate = nand_calculate_ecc;
4065 ecc->correct = nand_correct_data;
4066 ecc->read_page = nand_read_page_swecc;
4067 ecc->read_subpage = nand_read_subpage;
4068 ecc->write_page = nand_write_page_swecc;
4069 ecc->read_page_raw = nand_read_page_raw;
4070 ecc->write_page_raw = nand_write_page_raw;
4071 ecc->read_oob = nand_read_oob_std;
4072 ecc->write_oob = nand_write_oob_std;
4073 if (!ecc->size)
4074 ecc->size = 256;
4075 ecc->bytes = 3;
4076 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004078
Ivan Djelic193bd402011-03-11 11:05:33 +01004079 case NAND_ECC_SOFT_BCH:
4080 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03004081 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004082 BUG();
4083 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004084 ecc->calculate = nand_bch_calculate_ecc;
4085 ecc->correct = nand_bch_correct_data;
4086 ecc->read_page = nand_read_page_swecc;
4087 ecc->read_subpage = nand_read_subpage;
4088 ecc->write_page = nand_write_page_swecc;
4089 ecc->read_page_raw = nand_read_page_raw;
4090 ecc->write_page_raw = nand_write_page_raw;
4091 ecc->read_oob = nand_read_oob_std;
4092 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004093 /*
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004094 * Board driver should supply ecc.size and ecc.strength values
4095 * to select how many bits are correctable. Otherwise, default
4096 * to 4 bits for large page devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004097 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004098 if (!ecc->size && (mtd->oobsize >= 64)) {
4099 ecc->size = 512;
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004100 ecc->strength = 4;
Ivan Djelic193bd402011-03-11 11:05:33 +01004101 }
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004102
4103 /* See nand_bch_init() for details. */
4104 ecc->bytes = DIV_ROUND_UP(
4105 ecc->strength * fls(8 * ecc->size), 8);
Huang Shijie97de79e02013-10-18 14:20:53 +08004106 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4107 &ecc->layout);
4108 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004109 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004110 BUG();
4111 }
4112 break;
4113
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004114 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004115 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004116 ecc->read_page = nand_read_page_raw;
4117 ecc->write_page = nand_write_page_raw;
4118 ecc->read_oob = nand_read_oob_std;
4119 ecc->read_page_raw = nand_read_page_raw;
4120 ecc->write_page_raw = nand_write_page_raw;
4121 ecc->write_oob = nand_write_oob_std;
4122 ecc->size = mtd->writesize;
4123 ecc->bytes = 0;
4124 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08004128 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004129 BUG();
4130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Brian Norris9ce244b2011-08-30 18:45:37 -07004132 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004133 if (!ecc->read_oob_raw)
4134 ecc->read_oob_raw = ecc->read_oob;
4135 if (!ecc->write_oob_raw)
4136 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004137
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004138 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004139 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004140 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004141 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004142 ecc->layout->oobavail = 0;
4143 for (i = 0; ecc->layout->oobfree[i].length
4144 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4145 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4146 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004147
Thomas Petazzoni54c39e92014-07-02 15:16:32 +02004148 /* ECC sanity check: warn if it's too weak */
4149 if (!nand_ecc_strength_good(mtd))
4150 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4151 mtd->name);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004152
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004153 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004154 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004155 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004156 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004157 ecc->steps = mtd->writesize / ecc->size;
4158 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004159 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004160 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004162 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004163
Brian Norris8b6e50c2011-05-25 14:59:01 -07004164 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004165 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004166 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004167 case 2:
4168 mtd->subpage_sft = 1;
4169 break;
4170 case 4:
4171 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004172 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004173 mtd->subpage_sft = 2;
4174 break;
4175 }
4176 }
4177 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4178
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004179 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004180 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004183 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004185 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304186 switch (ecc->mode) {
4187 case NAND_ECC_SOFT:
4188 case NAND_ECC_SOFT_BCH:
4189 if (chip->page_shift > 9)
4190 chip->options |= NAND_SUBPAGE_READ;
4191 break;
4192
4193 default:
4194 break;
4195 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004196
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004198 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004199 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4200 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004201 mtd->_erase = nand_erase;
4202 mtd->_point = NULL;
4203 mtd->_unpoint = NULL;
4204 mtd->_read = nand_read;
4205 mtd->_write = nand_write;
4206 mtd->_panic_write = panic_nand_write;
4207 mtd->_read_oob = nand_read_oob;
4208 mtd->_write_oob = nand_write_oob;
4209 mtd->_sync = nand_sync;
4210 mtd->_lock = NULL;
4211 mtd->_unlock = NULL;
4212 mtd->_suspend = nand_suspend;
4213 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004214 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004215 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004216 mtd->_block_isbad = nand_block_isbad;
4217 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004218 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Mike Dunn6a918ba2012-03-11 14:21:11 -07004220 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004221 mtd->ecclayout = ecc->layout;
4222 mtd->ecc_strength = ecc->strength;
4223 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004224 /*
4225 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4226 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4227 * properly set.
4228 */
4229 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004230 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004232 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004233 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004237 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004239EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Brian Norris8b6e50c2011-05-25 14:59:01 -07004241/*
4242 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004243 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004244 * to call us from in-kernel code if the core NAND support is modular.
4245 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004246#ifdef MODULE
4247#define caller_is_module() (1)
4248#else
4249#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004250 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004251#endif
4252
4253/**
4254 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004255 * @mtd: MTD device structure
4256 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004257 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004258 * This fills out all the uninitialized function pointers with the defaults.
4259 * The flash ID is read and the mtd/chip structures are filled with the
4260 * appropriate values. The mtd->owner field must be set to the module of the
4261 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004262 */
4263int nand_scan(struct mtd_info *mtd, int maxchips)
4264{
4265 int ret;
4266
4267 /* Many callers got this wrong, so check for it for a while... */
4268 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004269 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004270 BUG();
4271 }
4272
David Woodhouse5e81e882010-02-26 18:32:56 +00004273 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004274 if (!ret)
4275 ret = nand_scan_tail(mtd);
4276 return ret;
4277}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004278EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004281 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004282 * @mtd: MTD device structure
4283 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004284void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004286 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Ivan Djelic193bd402011-03-11 11:05:33 +01004288 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4289 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4290
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004291 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Jesper Juhlfa671642005-11-07 01:01:27 -08004293 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004294 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004295 if (!(chip->options & NAND_OWN_BUFFERS))
4296 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004297
4298 /* Free bad block descriptor memory */
4299 if (chip->badblock_pattern && chip->badblock_pattern->options
4300 & NAND_BBT_DYNAMICSTRUCT)
4301 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
David Woodhousee0c7d762006-05-13 18:07:53 +01004303EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004304
4305static int __init nand_base_init(void)
4306{
4307 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4308 return 0;
4309}
4310
4311static void __exit nand_base_exit(void)
4312{
4313 led_trigger_unregister_simple(nand_led_trigger);
4314}
4315
4316module_init(nand_base_init);
4317module_exit(nand_base_exit);
4318
David Woodhousee0c7d762006-05-13 18:07:53 +01004319MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004320MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4321MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004322MODULE_DESCRIPTION("Generic NAND flash driver code");