blob: 6f3ae626cabccf118e0177589a13cd35928fe3df [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>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
42#include <linux/mtd/nand.h>
43#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.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>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
68 oobregion->length = 4;
69 } else {
70 oobregion->offset = 6;
71 oobregion->length = ecc->total - 4;
72 }
73
74 return 0;
75}
76
77static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
78 struct mtd_oob_region *oobregion)
79{
80 if (section > 1)
81 return -ERANGE;
82
83 if (mtd->oobsize == 16) {
84 if (section)
85 return -ERANGE;
86
87 oobregion->length = 8;
88 oobregion->offset = 8;
89 } else {
90 oobregion->length = 2;
91 if (!section)
92 oobregion->offset = 3;
93 else
94 oobregion->offset = 6;
95 }
96
97 return 0;
98}
99
100const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
101 .ecc = nand_ooblayout_ecc_sp,
102 .free = nand_ooblayout_free_sp,
103};
104EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
105
106static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
107 struct mtd_oob_region *oobregion)
108{
109 struct nand_chip *chip = mtd_to_nand(mtd);
110 struct nand_ecc_ctrl *ecc = &chip->ecc;
111
112 if (section)
113 return -ERANGE;
114
115 oobregion->length = ecc->total;
116 oobregion->offset = mtd->oobsize - oobregion->length;
117
118 return 0;
119}
120
121static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
122 struct mtd_oob_region *oobregion)
123{
124 struct nand_chip *chip = mtd_to_nand(mtd);
125 struct nand_ecc_ctrl *ecc = &chip->ecc;
126
127 if (section)
128 return -ERANGE;
129
130 oobregion->length = mtd->oobsize - ecc->total - 2;
131 oobregion->offset = 2;
132
133 return 0;
134}
135
136const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
137 .ecc = nand_ooblayout_ecc_lp,
138 .free = nand_ooblayout_free_lp,
139};
140EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200141
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530142static int check_offs_len(struct mtd_info *mtd,
143 loff_t ofs, uint64_t len)
144{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100145 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530146 int ret = 0;
147
148 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300149 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700150 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530151 ret = -EINVAL;
152 }
153
154 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300155 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700156 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530157 ret = -EINVAL;
158 }
159
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530160 return ret;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700165 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000166 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800167 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100169static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100171 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200173 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200174 spin_lock(&chip->controller->lock);
175 chip->controller->active = NULL;
176 chip->state = FL_READY;
177 wake_up(&chip->controller->wq);
178 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/**
182 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700183 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700185 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200187static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100189 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/**
Masanari Iida064a7692012-11-09 23:20:58 +0900194 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700195 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700197 * Default read function for 16bit buswidth with endianness conversion.
198 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200200static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100202 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200203 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700208 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700210 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
212static u16 nand_read_word(struct mtd_info *mtd)
213{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100214 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200215 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700220 * @mtd: MTD device structure
221 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
223 * Default select function for 1 chip devices.
224 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200225static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100227 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200228
229 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200231 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
233 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235
236 default:
237 BUG();
238 }
239}
240
241/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100242 * nand_write_byte - [DEFAULT] write single byte to chip
243 * @mtd: MTD device structure
244 * @byte: value to write
245 *
246 * Default function to write a byte to I/O[7:0]
247 */
248static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
249{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100250 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100251
252 chip->write_buf(mtd, &byte, 1);
253}
254
255/**
256 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
257 * @mtd: MTD device structure
258 * @byte: value to write
259 *
260 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
261 */
262static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
263{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100264 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100265 uint16_t word = byte;
266
267 /*
268 * It's not entirely clear what should happen to I/O[15:8] when writing
269 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
270 *
271 * When the host supports a 16-bit bus width, only data is
272 * transferred at the 16-bit width. All address and command line
273 * transfers shall use only the lower 8-bits of the data bus. During
274 * command transfers, the host may place any value on the upper
275 * 8-bits of the data bus. During address transfers, the host shall
276 * set the upper 8-bits of the data bus to 00h.
277 *
278 * One user of the write_byte callback is nand_onfi_set_features. The
279 * four parameters are specified to be written to I/O[7:0], but this is
280 * neither an address nor a command transfer. Let's assume a 0 on the
281 * upper I/O lines is OK.
282 */
283 chip->write_buf(mtd, (uint8_t *)&word, 2);
284}
285
286/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700288 * @mtd: MTD device structure
289 * @buf: data buffer
290 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700292 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200294static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100296 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Alexander Shiyan76413832013-04-13 09:32:13 +0400298 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000302 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700303 * @mtd: MTD device structure
304 * @buf: buffer to store date
305 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700307 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200309static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100311 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Alexander Shiyan76413832013-04-13 09:32:13 +0400313 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700318 * @mtd: MTD device structure
319 * @buf: data buffer
320 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700322 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200324static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100326 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000328
Alexander Shiyan76413832013-04-13 09:32:13 +0400329 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000333 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700334 * @mtd: MTD device structure
335 * @buf: buffer to store date
336 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700338 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200340static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100342 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Alexander Shiyan76413832013-04-13 09:32:13 +0400345 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700350 * @mtd: MTD device structure
351 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000353 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530355static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Archit Taneja9f3e0422016-02-03 14:29:49 +0530357 int page, res = 0, i = 0;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100358 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 u16 bad;
360
Brian Norris5fb15492011-05-31 16:31:21 -0700361 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700362 ofs += mtd->erasesize - mtd->writesize;
363
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100364 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
365
Brian Norriscdbec052012-01-13 18:11:48 -0800366 do {
367 if (chip->options & NAND_BUSWIDTH_16) {
368 chip->cmdfunc(mtd, NAND_CMD_READOOB,
369 chip->badblockpos & 0xFE, page);
370 bad = cpu_to_le16(chip->read_word(mtd));
371 if (chip->badblockpos & 0x1)
372 bad >>= 8;
373 else
374 bad &= 0xFF;
375 } else {
376 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
377 page);
378 bad = chip->read_byte(mtd);
379 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380
Brian Norriscdbec052012-01-13 18:11:48 -0800381 if (likely(chip->badblockbits == 8))
382 res = bad != 0xFF;
383 else
384 res = hweight8(bad) < chip->badblockbits;
385 ofs += mtd->writesize;
386 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
387 i++;
388 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return res;
391}
392
393/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700394 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700395 * @mtd: MTD device structure
396 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700398 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700399 * specific driver. It provides the details for writing a bad block marker to a
400 * block.
401 */
402static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
403{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100404 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700405 struct mtd_oob_ops ops;
406 uint8_t buf[2] = { 0, 0 };
407 int ret = 0, res, i = 0;
408
Brian Norris0ec56dc2015-02-28 02:02:30 -0800409 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700410 ops.oobbuf = buf;
411 ops.ooboffs = chip->badblockpos;
412 if (chip->options & NAND_BUSWIDTH_16) {
413 ops.ooboffs &= ~0x01;
414 ops.len = ops.ooblen = 2;
415 } else {
416 ops.len = ops.ooblen = 1;
417 }
418 ops.mode = MTD_OPS_PLACE_OOB;
419
420 /* Write to first/last page(s) if necessary */
421 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
422 ofs += mtd->erasesize - mtd->writesize;
423 do {
424 res = nand_do_write_oob(mtd, ofs, &ops);
425 if (!ret)
426 ret = res;
427
428 i++;
429 ofs += mtd->writesize;
430 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
431
432 return ret;
433}
434
435/**
436 * nand_block_markbad_lowlevel - mark a block bad
437 * @mtd: MTD device structure
438 * @ofs: offset from device start
439 *
440 * This function performs the generic NAND bad block marking steps (i.e., bad
441 * block table(s) and/or marker(s)). We only allow the hardware driver to
442 * specify how to write bad block markers to OOB (chip->block_markbad).
443 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700444 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800445 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700446 * (2) write bad block marker to OOB area of affected block (unless flag
447 * NAND_BBT_NO_OOB_BBM is present)
448 * (3) update the BBT
449 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800450 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700452static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100454 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700455 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000456
Brian Norrisb32843b2013-07-30 17:52:59 -0700457 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800458 struct erase_info einfo;
459
460 /* Attempt erase before marking OOB */
461 memset(&einfo, 0, sizeof(einfo));
462 einfo.mtd = mtd;
463 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300464 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800465 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800466
Brian Norrisb32843b2013-07-30 17:52:59 -0700467 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800468 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700469 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300470 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200471 }
Brian Norrise2414f42012-02-06 13:44:00 -0800472
Brian Norrisb32843b2013-07-30 17:52:59 -0700473 /* Mark block bad in BBT */
474 if (chip->bbt) {
475 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800476 if (!ret)
477 ret = res;
478 }
479
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200480 if (!ret)
481 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300482
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000486/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700488 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700490 * Check, if the device is write protected. The function expects, that the
491 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100493static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100495 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200496
Brian Norris8b6e50c2011-05-25 14:59:01 -0700497 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200498 if (chip->options & NAND_BROKEN_XD)
499 return 0;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200502 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
503 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800507 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700508 * @mtd: MTD device structure
509 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300510 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800511 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300512 */
513static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
514{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100515 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300516
517 if (!chip->bbt)
518 return 0;
519 /* Return info from the table */
520 return nand_isreserved_bbt(mtd, ofs);
521}
522
523/**
524 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
525 * @mtd: MTD device structure
526 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700527 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
529 * Check, if the block is bad. Either by reading the bad block table or
530 * calling of the scan function.
531 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530532static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100534 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000535
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200536 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530537 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100540 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200543/**
544 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700545 * @mtd: MTD device structure
546 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200547 *
548 * Helper function for nand_wait_ready used when needing to wait in interrupt
549 * context.
550 */
551static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
552{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100553 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200554 int i;
555
556 /* Wait for the device to get ready */
557 for (i = 0; i < timeo; i++) {
558 if (chip->dev_ready(mtd))
559 break;
560 touch_softlockup_watchdog();
561 mdelay(1);
562 }
563}
564
Alex Smithb70af9b2015-10-06 14:52:07 +0100565/**
566 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
567 * @mtd: MTD device structure
568 *
569 * Wait for the ready pin after a command, and warn if a timeout occurs.
570 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100571void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000572{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100573 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100574 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000575
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200576 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100577 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200578
Brian Norris7854d3f2011-06-23 14:12:08 -0700579 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100580 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000581 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300583 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100584 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000585 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100586
Brian Norris9ebfdf52016-03-04 17:19:23 -0800587 if (!chip->dev_ready(mtd))
588 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000589}
David Woodhouse4b648b02006-09-25 17:05:24 +0100590EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200593 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
594 * @mtd: MTD device structure
595 * @timeo: Timeout in ms
596 *
597 * Wait for status ready (i.e. command done) or timeout.
598 */
599static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
600{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100601 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200602
603 timeo = jiffies + msecs_to_jiffies(timeo);
604 do {
605 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
606 break;
607 touch_softlockup_watchdog();
608 } while (time_before(jiffies, timeo));
609};
610
611/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * @mtd: MTD device structure
614 * @command: the command to be sent
615 * @column: the column address for this command, -1 if none
616 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200619 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200621static void nand_command(struct mtd_info *mtd, unsigned int command,
622 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100624 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200625 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Brian Norris8b6e50c2011-05-25 14:59:01 -0700627 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (command == NAND_CMD_SEQIN) {
629 int readcmd;
630
Joern Engel28318772006-05-22 23:18:05 +0200631 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200633 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 readcmd = NAND_CMD_READOOB;
635 } else if (column < 256) {
636 /* First 256 bytes --> READ0 */
637 readcmd = NAND_CMD_READ0;
638 } else {
639 column -= 256;
640 readcmd = NAND_CMD_READ1;
641 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200643 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200645 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Brian Norris8b6e50c2011-05-25 14:59:01 -0700647 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200648 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
649 /* Serially input address */
650 if (column != -1) {
651 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800652 if (chip->options & NAND_BUSWIDTH_16 &&
653 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200654 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200656 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200658 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200659 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200660 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200661 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200662 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200663 if (chip->chipsize > (32 << 20))
664 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200665 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200666 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000667
668 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700669 * Program and erase have their own busy handlers status and sequential
670 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case NAND_CMD_PAGEPROG:
675 case NAND_CMD_ERASE1:
676 case NAND_CMD_ERASE2:
677 case NAND_CMD_SEQIN:
678 case NAND_CMD_STATUS:
679 return;
680
681 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200682 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200684 udelay(chip->chip_delay);
685 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200686 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200687 chip->cmd_ctrl(mtd,
688 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200689 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
690 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return;
692
David Woodhousee0c7d762006-05-13 18:07:53 +0100693 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000695 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 * If we don't have access to the busy pin, we apply the given
697 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100698 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200699 if (!chip->dev_ready) {
700 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700704 /*
705 * Apply this short delay always to ensure that we do wait tWB in
706 * any case on any machine.
707 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100708 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000709
710 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200713static void nand_ccs_delay(struct nand_chip *chip)
714{
715 /*
716 * The controller already takes care of waiting for tCCS when the RNDIN
717 * or RNDOUT command is sent, return directly.
718 */
719 if (!(chip->options & NAND_WAIT_TCCS))
720 return;
721
722 /*
723 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
724 * (which should be safe for all NANDs).
725 */
726 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
727 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
728 else
729 ndelay(500);
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/**
733 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700734 * @mtd: MTD device structure
735 * @command: the command to be sent
736 * @column: the column address for this command, -1 if none
737 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200739 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700740 * devices. We don't have the separate regions as we have in the small page
741 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200743static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
744 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100746 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /* Emulate NAND_CMD_READOOB */
749 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200750 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 command = NAND_CMD_READ0;
752 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200754 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400755 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200758 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /* Serially input address */
761 if (column != -1) {
762 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800763 if (chip->options & NAND_BUSWIDTH_16 &&
764 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200766 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200767 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200768
Brian Norrisf5b88de2016-10-03 09:49:35 -0700769 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200770 if (!nand_opcode_8bits(command))
771 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200774 chip->cmd_ctrl(mtd, page_addr, ctrl);
775 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200776 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200778 if (chip->chipsize > (128 << 20))
779 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200780 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200783 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000784
785 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700786 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100787 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case NAND_CMD_CACHEDPROG:
792 case NAND_CMD_PAGEPROG:
793 case NAND_CMD_ERASE1:
794 case NAND_CMD_ERASE2:
795 case NAND_CMD_SEQIN:
796 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000797 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200799 case NAND_CMD_RNDIN:
800 nand_ccs_delay(chip);
801 return;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200804 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200806 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200807 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
808 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
809 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
810 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200811 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
812 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return;
814
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200815 case NAND_CMD_RNDOUT:
816 /* No ready / busy check necessary */
817 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
818 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
819 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
820 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200821
822 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200823 return;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200826 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
827 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
828 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
829 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000830
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000833 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700835 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100836 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200837 if (!chip->dev_ready) {
838 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000842
Brian Norris8b6e50c2011-05-25 14:59:01 -0700843 /*
844 * Apply this short delay always to ensure that we do wait tWB in
845 * any case on any machine.
846 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100847 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000848
849 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200853 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700854 * @chip: the nand chip descriptor
855 * @mtd: MTD device structure
856 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200857 *
858 * Used when in panic, no locks are taken.
859 */
860static void panic_nand_get_device(struct nand_chip *chip,
861 struct mtd_info *mtd, int new_state)
862{
Brian Norris7854d3f2011-06-23 14:12:08 -0700863 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200864 chip->controller->active = chip;
865 chip->state = new_state;
866}
867
868/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700870 * @mtd: MTD device structure
871 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 *
873 * Get the device and lock it for exclusive access
874 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200875static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800876nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100878 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200879 spinlock_t *lock = &chip->controller->lock;
880 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100881 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200882retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100883 spin_lock(lock);
884
vimal singhb8b3ee92009-07-09 20:41:22 +0530885 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200886 if (!chip->controller->active)
887 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200888
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200889 if (chip->controller->active == chip && chip->state == FL_READY) {
890 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100891 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100892 return 0;
893 }
894 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800895 if (chip->controller->active->state == FL_PM_SUSPENDED) {
896 chip->state = FL_PM_SUSPENDED;
897 spin_unlock(lock);
898 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800899 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100900 }
901 set_current_state(TASK_UNINTERRUPTIBLE);
902 add_wait_queue(wq, &wait);
903 spin_unlock(lock);
904 schedule();
905 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto retry;
907}
908
909/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700910 * panic_nand_wait - [GENERIC] wait until the command is done
911 * @mtd: MTD device structure
912 * @chip: NAND chip structure
913 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200914 *
915 * Wait for command done. This is a helper function for nand_wait used when
916 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400917 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200918 */
919static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
920 unsigned long timeo)
921{
922 int i;
923 for (i = 0; i < timeo; i++) {
924 if (chip->dev_ready) {
925 if (chip->dev_ready(mtd))
926 break;
927 } else {
928 if (chip->read_byte(mtd) & NAND_STATUS_READY)
929 break;
930 }
931 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200932 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200933}
934
935/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700936 * nand_wait - [DEFAULT] wait until the command is done
937 * @mtd: MTD device structure
938 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100940 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700941 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200942static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944
Alex Smithb70af9b2015-10-06 14:52:07 +0100945 int status;
946 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Brian Norris8b6e50c2011-05-25 14:59:01 -0700948 /*
949 * Apply this short delay always to ensure that we do wait tWB in any
950 * case on any machine.
951 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100952 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200954 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200956 if (in_interrupt() || oops_in_progress)
957 panic_nand_wait(mtd, chip, timeo);
958 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800959 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100960 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200961 if (chip->dev_ready) {
962 if (chip->dev_ready(mtd))
963 break;
964 } else {
965 if (chip->read_byte(mtd) & NAND_STATUS_READY)
966 break;
967 }
968 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100969 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800971
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200972 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100973 /* This can happen if in case of timeout or buggy dev_ready */
974 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return status;
976}
977
978/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200979 * nand_reset_data_interface - Reset data interface and timings
980 * @chip: The NAND chip
981 *
982 * Reset the Data interface and timings to ONFI mode 0.
983 *
984 * Returns 0 for success or negative error code otherwise.
985 */
986static int nand_reset_data_interface(struct nand_chip *chip)
987{
988 struct mtd_info *mtd = nand_to_mtd(chip);
989 const struct nand_data_interface *conf;
990 int ret;
991
992 if (!chip->setup_data_interface)
993 return 0;
994
995 /*
996 * The ONFI specification says:
997 * "
998 * To transition from NV-DDR or NV-DDR2 to the SDR data
999 * interface, the host shall use the Reset (FFh) command
1000 * using SDR timing mode 0. A device in any timing mode is
1001 * required to recognize Reset (FFh) command issued in SDR
1002 * timing mode 0.
1003 * "
1004 *
1005 * Configure the data interface in SDR mode and set the
1006 * timings to timing mode 0.
1007 */
1008
1009 conf = nand_get_default_data_interface();
1010 ret = chip->setup_data_interface(mtd, conf, false);
1011 if (ret)
1012 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1013
1014 return ret;
1015}
1016
1017/**
1018 * nand_setup_data_interface - Setup the best data interface and timings
1019 * @chip: The NAND chip
1020 *
1021 * Find and configure the best data interface and NAND timings supported by
1022 * the chip and the driver.
1023 * First tries to retrieve supported timing modes from ONFI information,
1024 * and if the NAND chip does not support ONFI, relies on the
1025 * ->onfi_timing_mode_default specified in the nand_ids table.
1026 *
1027 * Returns 0 for success or negative error code otherwise.
1028 */
1029static int nand_setup_data_interface(struct nand_chip *chip)
1030{
1031 struct mtd_info *mtd = nand_to_mtd(chip);
1032 int ret;
1033
1034 if (!chip->setup_data_interface || !chip->data_interface)
1035 return 0;
1036
1037 /*
1038 * Ensure the timing mode has been changed on the chip side
1039 * before changing timings on the controller side.
1040 */
1041 if (chip->onfi_version) {
1042 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1043 chip->onfi_timing_mode_default,
1044 };
1045
1046 ret = chip->onfi_set_features(mtd, chip,
1047 ONFI_FEATURE_ADDR_TIMING_MODE,
1048 tmode_param);
1049 if (ret)
1050 goto err;
1051 }
1052
1053 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1054err:
1055 return ret;
1056}
1057
1058/**
1059 * nand_init_data_interface - find the best data interface and timings
1060 * @chip: The NAND chip
1061 *
1062 * Find the best data interface and NAND timings supported by the chip
1063 * and the driver.
1064 * First tries to retrieve supported timing modes from ONFI information,
1065 * and if the NAND chip does not support ONFI, relies on the
1066 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1067 * function nand_chip->data_interface is initialized with the best timing mode
1068 * available.
1069 *
1070 * Returns 0 for success or negative error code otherwise.
1071 */
1072static int nand_init_data_interface(struct nand_chip *chip)
1073{
1074 struct mtd_info *mtd = nand_to_mtd(chip);
1075 int modes, mode, ret;
1076
1077 if (!chip->setup_data_interface)
1078 return 0;
1079
1080 /*
1081 * First try to identify the best timings from ONFI parameters and
1082 * if the NAND does not support ONFI, fallback to the default ONFI
1083 * timing mode.
1084 */
1085 modes = onfi_get_async_timing_mode(chip);
1086 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1087 if (!chip->onfi_timing_mode_default)
1088 return 0;
1089
1090 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1091 }
1092
1093 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1094 GFP_KERNEL);
1095 if (!chip->data_interface)
1096 return -ENOMEM;
1097
1098 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1099 ret = onfi_init_data_interface(chip, chip->data_interface,
1100 NAND_SDR_IFACE, mode);
1101 if (ret)
1102 continue;
1103
1104 ret = chip->setup_data_interface(mtd, chip->data_interface,
1105 true);
1106 if (!ret) {
1107 chip->onfi_timing_mode_default = mode;
1108 break;
1109 }
1110 }
1111
1112 return 0;
1113}
1114
1115static void nand_release_data_interface(struct nand_chip *chip)
1116{
1117 kfree(chip->data_interface);
1118}
1119
1120/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001121 * nand_reset - Reset and initialize a NAND device
1122 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001123 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001124 *
1125 * Returns 0 for success or negative error code otherwise
1126 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001127int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001128{
1129 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001130 int ret;
1131
1132 ret = nand_reset_data_interface(chip);
1133 if (ret)
1134 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001135
Boris Brezillon73f907f2016-10-24 16:46:20 +02001136 /*
1137 * The CS line has to be released before we can apply the new NAND
1138 * interface settings, hence this weird ->select_chip() dance.
1139 */
1140 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001141 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001142 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001143
Boris Brezillon73f907f2016-10-24 16:46:20 +02001144 chip->select_chip(mtd, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001145 ret = nand_setup_data_interface(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001146 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001147 if (ret)
1148 return ret;
1149
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001150 return 0;
1151}
1152
1153/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001154 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001155 * @mtd: mtd info
1156 * @ofs: offset to start unlock from
1157 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001158 * @invert: when = 0, unlock the range of blocks within the lower and
1159 * upper boundary address
1160 * when = 1, unlock the range of blocks outside the boundaries
1161 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301162 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001163 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301164 */
1165static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1166 uint64_t len, int invert)
1167{
1168 int ret = 0;
1169 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001170 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301171
1172 /* Submit address of first page to unlock */
1173 page = ofs >> chip->page_shift;
1174 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1175
1176 /* Submit address of last page to unlock */
1177 page = (ofs + len) >> chip->page_shift;
1178 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1179 (page | invert) & chip->pagemask);
1180
1181 /* Call wait ready function */
1182 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301183 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001184 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001185 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301186 __func__, status);
1187 ret = -EIO;
1188 }
1189
1190 return ret;
1191}
1192
1193/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001194 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001195 * @mtd: mtd info
1196 * @ofs: offset to start unlock from
1197 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301198 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001199 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301200 */
1201int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1202{
1203 int ret = 0;
1204 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001205 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301206
Brian Norris289c0522011-07-19 10:06:09 -07001207 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301208 __func__, (unsigned long long)ofs, len);
1209
1210 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001211 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301212
1213 /* Align to last block address if size addresses end of the device */
1214 if (ofs + len == mtd->size)
1215 len -= mtd->erasesize;
1216
Huang Shijie6a8214a2012-11-19 14:43:30 +08001217 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301218
1219 /* Shift to get chip number */
1220 chipnr = ofs >> chip->chip_shift;
1221
White Ding57d3a9a2014-07-24 00:10:45 +08001222 /*
1223 * Reset the chip.
1224 * If we want to check the WP through READ STATUS and check the bit 7
1225 * we must reset the chip
1226 * some operation can also clear the bit 7 of status register
1227 * eg. erase/program a locked block
1228 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001229 nand_reset(chip, chipnr);
1230
1231 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001232
Vimal Singh7d70f332010-02-08 15:50:49 +05301233 /* Check, if it is write protected */
1234 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001235 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301236 __func__);
1237 ret = -EIO;
1238 goto out;
1239 }
1240
1241 ret = __nand_unlock(mtd, ofs, len, 0);
1242
1243out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001244 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301245 nand_release_device(mtd);
1246
1247 return ret;
1248}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001249EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301250
1251/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001252 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001253 * @mtd: mtd info
1254 * @ofs: offset to start unlock from
1255 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301256 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001257 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1258 * have this feature, but it allows only to lock all blocks, not for specified
1259 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1260 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301261 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001262 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301263 */
1264int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1265{
1266 int ret = 0;
1267 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001268 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301269
Brian Norris289c0522011-07-19 10:06:09 -07001270 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301271 __func__, (unsigned long long)ofs, len);
1272
1273 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001274 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301275
Huang Shijie6a8214a2012-11-19 14:43:30 +08001276 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301277
1278 /* Shift to get chip number */
1279 chipnr = ofs >> chip->chip_shift;
1280
White Ding57d3a9a2014-07-24 00:10:45 +08001281 /*
1282 * Reset the chip.
1283 * If we want to check the WP through READ STATUS and check the bit 7
1284 * we must reset the chip
1285 * some operation can also clear the bit 7 of status register
1286 * eg. erase/program a locked block
1287 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001288 nand_reset(chip, chipnr);
1289
1290 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001291
Vimal Singh7d70f332010-02-08 15:50:49 +05301292 /* Check, if it is write protected */
1293 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001294 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301295 __func__);
1296 status = MTD_ERASE_FAILED;
1297 ret = -EIO;
1298 goto out;
1299 }
1300
1301 /* Submit address of first page to lock */
1302 page = ofs >> chip->page_shift;
1303 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1304
1305 /* Call wait ready function */
1306 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301307 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001308 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001309 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301310 __func__, status);
1311 ret = -EIO;
1312 goto out;
1313 }
1314
1315 ret = __nand_unlock(mtd, ofs, len, 0x1);
1316
1317out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001318 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301319 nand_release_device(mtd);
1320
1321 return ret;
1322}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001323EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301324
1325/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001326 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1327 * @buf: buffer to test
1328 * @len: buffer length
1329 * @bitflips_threshold: maximum number of bitflips
1330 *
1331 * Check if a buffer contains only 0xff, which means the underlying region
1332 * has been erased and is ready to be programmed.
1333 * The bitflips_threshold specify the maximum number of bitflips before
1334 * considering the region is not erased.
1335 * Note: The logic of this function has been extracted from the memweight
1336 * implementation, except that nand_check_erased_buf function exit before
1337 * testing the whole buffer if the number of bitflips exceed the
1338 * bitflips_threshold value.
1339 *
1340 * Returns a positive number of bitflips less than or equal to
1341 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1342 * threshold.
1343 */
1344static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1345{
1346 const unsigned char *bitmap = buf;
1347 int bitflips = 0;
1348 int weight;
1349
1350 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1351 len--, bitmap++) {
1352 weight = hweight8(*bitmap);
1353 bitflips += BITS_PER_BYTE - weight;
1354 if (unlikely(bitflips > bitflips_threshold))
1355 return -EBADMSG;
1356 }
1357
1358 for (; len >= sizeof(long);
1359 len -= sizeof(long), bitmap += sizeof(long)) {
1360 weight = hweight_long(*((unsigned long *)bitmap));
1361 bitflips += BITS_PER_LONG - weight;
1362 if (unlikely(bitflips > bitflips_threshold))
1363 return -EBADMSG;
1364 }
1365
1366 for (; len > 0; len--, bitmap++) {
1367 weight = hweight8(*bitmap);
1368 bitflips += BITS_PER_BYTE - weight;
1369 if (unlikely(bitflips > bitflips_threshold))
1370 return -EBADMSG;
1371 }
1372
1373 return bitflips;
1374}
1375
1376/**
1377 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1378 * 0xff data
1379 * @data: data buffer to test
1380 * @datalen: data length
1381 * @ecc: ECC buffer
1382 * @ecclen: ECC length
1383 * @extraoob: extra OOB buffer
1384 * @extraooblen: extra OOB length
1385 * @bitflips_threshold: maximum number of bitflips
1386 *
1387 * Check if a data buffer and its associated ECC and OOB data contains only
1388 * 0xff pattern, which means the underlying region has been erased and is
1389 * ready to be programmed.
1390 * The bitflips_threshold specify the maximum number of bitflips before
1391 * considering the region as not erased.
1392 *
1393 * Note:
1394 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1395 * different from the NAND page size. When fixing bitflips, ECC engines will
1396 * report the number of errors per chunk, and the NAND core infrastructure
1397 * expect you to return the maximum number of bitflips for the whole page.
1398 * This is why you should always use this function on a single chunk and
1399 * not on the whole page. After checking each chunk you should update your
1400 * max_bitflips value accordingly.
1401 * 2/ When checking for bitflips in erased pages you should not only check
1402 * the payload data but also their associated ECC data, because a user might
1403 * have programmed almost all bits to 1 but a few. In this case, we
1404 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1405 * this case.
1406 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1407 * data are protected by the ECC engine.
1408 * It could also be used if you support subpages and want to attach some
1409 * extra OOB data to an ECC chunk.
1410 *
1411 * Returns a positive number of bitflips less than or equal to
1412 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1413 * threshold. In case of success, the passed buffers are filled with 0xff.
1414 */
1415int nand_check_erased_ecc_chunk(void *data, int datalen,
1416 void *ecc, int ecclen,
1417 void *extraoob, int extraooblen,
1418 int bitflips_threshold)
1419{
1420 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1421
1422 data_bitflips = nand_check_erased_buf(data, datalen,
1423 bitflips_threshold);
1424 if (data_bitflips < 0)
1425 return data_bitflips;
1426
1427 bitflips_threshold -= data_bitflips;
1428
1429 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1430 if (ecc_bitflips < 0)
1431 return ecc_bitflips;
1432
1433 bitflips_threshold -= ecc_bitflips;
1434
1435 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1436 bitflips_threshold);
1437 if (extraoob_bitflips < 0)
1438 return extraoob_bitflips;
1439
1440 if (data_bitflips)
1441 memset(data, 0xff, datalen);
1442
1443 if (ecc_bitflips)
1444 memset(ecc, 0xff, ecclen);
1445
1446 if (extraoob_bitflips)
1447 memset(extraoob, 0xff, extraooblen);
1448
1449 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1450}
1451EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1452
1453/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001454 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001455 * @mtd: mtd info structure
1456 * @chip: nand chip info structure
1457 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001458 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001459 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001460 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001461 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001462 */
1463static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001464 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001465{
1466 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001467 if (oob_required)
1468 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001469 return 0;
1470}
1471
1472/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001473 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001474 * @mtd: mtd info structure
1475 * @chip: nand chip info structure
1476 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001477 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001478 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001479 *
1480 * We need a special oob layout and handling even when OOB isn't used.
1481 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001482static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001483 struct nand_chip *chip, uint8_t *buf,
1484 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001485{
1486 int eccsize = chip->ecc.size;
1487 int eccbytes = chip->ecc.bytes;
1488 uint8_t *oob = chip->oob_poi;
1489 int steps, size;
1490
1491 for (steps = chip->ecc.steps; steps > 0; steps--) {
1492 chip->read_buf(mtd, buf, eccsize);
1493 buf += eccsize;
1494
1495 if (chip->ecc.prepad) {
1496 chip->read_buf(mtd, oob, chip->ecc.prepad);
1497 oob += chip->ecc.prepad;
1498 }
1499
1500 chip->read_buf(mtd, oob, eccbytes);
1501 oob += eccbytes;
1502
1503 if (chip->ecc.postpad) {
1504 chip->read_buf(mtd, oob, chip->ecc.postpad);
1505 oob += chip->ecc.postpad;
1506 }
1507 }
1508
1509 size = mtd->oobsize - (oob - chip->oob_poi);
1510 if (size)
1511 chip->read_buf(mtd, oob, size);
1512
1513 return 0;
1514}
1515
1516/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001517 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001518 * @mtd: mtd info structure
1519 * @chip: nand chip info structure
1520 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001521 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001522 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001523 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001524static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001525 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Boris Brezillon846031d2016-02-03 20:11:00 +01001527 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001528 int eccbytes = chip->ecc.bytes;
1529 int eccsteps = chip->ecc.steps;
1530 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001531 uint8_t *ecc_calc = chip->buffers->ecccalc;
1532 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001533 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001534
Brian Norris1fbb9382012-05-02 10:14:55 -07001535 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001536
1537 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1538 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1539
Boris Brezillon846031d2016-02-03 20:11:00 +01001540 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1541 chip->ecc.total);
1542 if (ret)
1543 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001544
1545 eccsteps = chip->ecc.steps;
1546 p = buf;
1547
1548 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1549 int stat;
1550
1551 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001552 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001553 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001554 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001555 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001556 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1557 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001558 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001559 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301563 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001564 * @mtd: mtd info structure
1565 * @chip: nand chip info structure
1566 * @data_offs: offset of requested data within the page
1567 * @readlen: data length
1568 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001569 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001570 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001571static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001572 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1573 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001574{
Boris Brezillon846031d2016-02-03 20:11:00 +01001575 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001576 uint8_t *p;
1577 int data_col_addr, i, gaps = 0;
1578 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1579 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001580 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001581 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001582 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001583
Brian Norris7854d3f2011-06-23 14:12:08 -07001584 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001585 start_step = data_offs / chip->ecc.size;
1586 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1587 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301588 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001589
Brian Norris8b6e50c2011-05-25 14:59:01 -07001590 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001591 datafrag_len = num_steps * chip->ecc.size;
1592 eccfrag_len = num_steps * chip->ecc.bytes;
1593
1594 data_col_addr = start_step * chip->ecc.size;
1595 /* If we read not a page aligned data */
1596 if (data_col_addr != 0)
1597 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1598
1599 p = bufpoi + data_col_addr;
1600 chip->read_buf(mtd, p, datafrag_len);
1601
Brian Norris8b6e50c2011-05-25 14:59:01 -07001602 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001603 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1604 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1605
Brian Norris8b6e50c2011-05-25 14:59:01 -07001606 /*
1607 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001608 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001609 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001610 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1611 if (ret)
1612 return ret;
1613
1614 if (oobregion.length < eccfrag_len)
1615 gaps = 1;
1616
Alexey Korolev3d459552008-05-15 17:23:18 +01001617 if (gaps) {
1618 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1619 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1620 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001621 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001622 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001623 * about buswidth alignment in read_buf.
1624 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001625 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001626 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001627 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001628 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001629 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1630 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001631 aligned_len++;
1632
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001633 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001634 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001635 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1636 }
1637
Boris Brezillon846031d2016-02-03 20:11:00 +01001638 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1639 chip->oob_poi, index, eccfrag_len);
1640 if (ret)
1641 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001642
1643 p = bufpoi + data_col_addr;
1644 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1645 int stat;
1646
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001647 stat = chip->ecc.correct(mtd, p,
1648 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001649 if (stat == -EBADMSG &&
1650 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1651 /* check for empty pages with bitflips */
1652 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1653 &chip->buffers->ecccode[i],
1654 chip->ecc.bytes,
1655 NULL, 0,
1656 chip->ecc.strength);
1657 }
1658
Mike Dunn3f91e942012-04-25 12:06:09 -07001659 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001660 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001661 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001662 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001663 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1664 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001665 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001666 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001667}
1668
1669/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001670 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001671 * @mtd: mtd info structure
1672 * @chip: nand chip info structure
1673 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001674 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001675 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001676 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001677 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001678 */
1679static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001680 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001681{
Boris Brezillon846031d2016-02-03 20:11:00 +01001682 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001683 int eccbytes = chip->ecc.bytes;
1684 int eccsteps = chip->ecc.steps;
1685 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001686 uint8_t *ecc_calc = chip->buffers->ecccalc;
1687 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001688 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001689
1690 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1691 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1692 chip->read_buf(mtd, p, eccsize);
1693 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1694 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001695 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001696
Boris Brezillon846031d2016-02-03 20:11:00 +01001697 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1698 chip->ecc.total);
1699 if (ret)
1700 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001701
1702 eccsteps = chip->ecc.steps;
1703 p = buf;
1704
1705 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1706 int stat;
1707
1708 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001709 if (stat == -EBADMSG &&
1710 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1711 /* check for empty pages with bitflips */
1712 stat = nand_check_erased_ecc_chunk(p, eccsize,
1713 &ecc_code[i], eccbytes,
1714 NULL, 0,
1715 chip->ecc.strength);
1716 }
1717
Mike Dunn3f91e942012-04-25 12:06:09 -07001718 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001719 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001720 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001721 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001722 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1723 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001724 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001725 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001726}
1727
1728/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001729 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001730 * @mtd: mtd info structure
1731 * @chip: nand chip info structure
1732 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001733 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001734 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001735 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001736 * Hardware ECC for large page chips, require OOB to be read first. For this
1737 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1738 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1739 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1740 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001741 */
1742static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001743 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001744{
Boris Brezillon846031d2016-02-03 20:11:00 +01001745 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001746 int eccbytes = chip->ecc.bytes;
1747 int eccsteps = chip->ecc.steps;
1748 uint8_t *p = buf;
1749 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001750 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001751 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001752
1753 /* Read the OOB area first */
1754 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1755 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1756 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1757
Boris Brezillon846031d2016-02-03 20:11:00 +01001758 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1759 chip->ecc.total);
1760 if (ret)
1761 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001762
1763 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1764 int stat;
1765
1766 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1767 chip->read_buf(mtd, p, eccsize);
1768 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1769
1770 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001771 if (stat == -EBADMSG &&
1772 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1773 /* check for empty pages with bitflips */
1774 stat = nand_check_erased_ecc_chunk(p, eccsize,
1775 &ecc_code[i], eccbytes,
1776 NULL, 0,
1777 chip->ecc.strength);
1778 }
1779
Mike Dunn3f91e942012-04-25 12:06:09 -07001780 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001781 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001782 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001783 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001784 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1785 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001786 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001787 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001788}
1789
1790/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001791 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001792 * @mtd: mtd info structure
1793 * @chip: nand chip info structure
1794 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001795 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001796 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001797 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001798 * The hw generator calculates the error syndrome automatically. Therefore we
1799 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001800 */
1801static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001802 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001803{
1804 int i, eccsize = chip->ecc.size;
1805 int eccbytes = chip->ecc.bytes;
1806 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001807 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001808 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001809 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001810 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001811
1812 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1813 int stat;
1814
1815 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1816 chip->read_buf(mtd, p, eccsize);
1817
1818 if (chip->ecc.prepad) {
1819 chip->read_buf(mtd, oob, chip->ecc.prepad);
1820 oob += chip->ecc.prepad;
1821 }
1822
1823 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1824 chip->read_buf(mtd, oob, eccbytes);
1825 stat = chip->ecc.correct(mtd, p, oob, NULL);
1826
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001827 oob += eccbytes;
1828
1829 if (chip->ecc.postpad) {
1830 chip->read_buf(mtd, oob, chip->ecc.postpad);
1831 oob += chip->ecc.postpad;
1832 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001833
1834 if (stat == -EBADMSG &&
1835 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1836 /* check for empty pages with bitflips */
1837 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1838 oob - eccpadbytes,
1839 eccpadbytes,
1840 NULL, 0,
1841 chip->ecc.strength);
1842 }
1843
1844 if (stat < 0) {
1845 mtd->ecc_stats.failed++;
1846 } else {
1847 mtd->ecc_stats.corrected += stat;
1848 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1849 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001850 }
1851
1852 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001853 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001854 if (i)
1855 chip->read_buf(mtd, oob, i);
1856
Mike Dunn3f91e942012-04-25 12:06:09 -07001857 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001858}
1859
1860/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001861 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001862 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001863 * @oob: oob destination address
1864 * @ops: oob ops structure
1865 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001866 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001867static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001868 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001869{
Boris Brezillon846031d2016-02-03 20:11:00 +01001870 struct nand_chip *chip = mtd_to_nand(mtd);
1871 int ret;
1872
Florian Fainellif8ac0412010-09-07 13:23:43 +02001873 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001874
Brian Norris0612b9d2011-08-30 18:45:40 -07001875 case MTD_OPS_PLACE_OOB:
1876 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001877 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1878 return oob + len;
1879
Boris Brezillon846031d2016-02-03 20:11:00 +01001880 case MTD_OPS_AUTO_OOB:
1881 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1882 ops->ooboffs, len);
1883 BUG_ON(ret);
1884 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001885
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001886 default:
1887 BUG();
1888 }
1889 return NULL;
1890}
1891
1892/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001893 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1894 * @mtd: MTD device structure
1895 * @retry_mode: the retry mode to use
1896 *
1897 * Some vendors supply a special command to shift the Vt threshold, to be used
1898 * when there are too many bitflips in a page (i.e., ECC error). After setting
1899 * a new threshold, the host should retry reading the page.
1900 */
1901static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1902{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001903 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001904
1905 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1906
1907 if (retry_mode >= chip->read_retries)
1908 return -EINVAL;
1909
1910 if (!chip->setup_read_retry)
1911 return -EOPNOTSUPP;
1912
1913 return chip->setup_read_retry(mtd, retry_mode);
1914}
1915
1916/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001917 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001918 * @mtd: MTD device structure
1919 * @from: offset to read from
1920 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001921 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001922 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001923 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001924static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1925 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001926{
Brian Norrise47f3db2012-05-02 10:14:56 -07001927 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001928 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001929 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001930 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001931 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001932 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001933
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001934 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001935 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001936 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001937 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001938 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001940 chipnr = (int)(from >> chip->chip_shift);
1941 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001943 realpage = (int)(from >> chip->page_shift);
1944 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001946 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001948 buf = ops->datbuf;
1949 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001950 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001951
Florian Fainellif8ac0412010-09-07 13:23:43 +02001952 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001953 unsigned int ecc_failures = mtd->ecc_stats.failed;
1954
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001955 bytes = min(mtd->writesize - col, readlen);
1956 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001957
Kamal Dasu66507c72014-05-01 20:51:19 -04001958 if (!aligned)
1959 use_bufpoi = 1;
1960 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1961 use_bufpoi = !virt_addr_valid(buf);
1962 else
1963 use_bufpoi = 0;
1964
Brian Norris8b6e50c2011-05-25 14:59:01 -07001965 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001966 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001967 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1968
1969 if (use_bufpoi && aligned)
1970 pr_debug("%s: using read bounce buffer for buf@%p\n",
1971 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Brian Norrisba84fb52014-01-03 15:13:33 -08001973read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001974 if (nand_standard_page_accessors(&chip->ecc))
1975 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Mike Dunnedbc45402012-04-25 12:06:11 -07001977 /*
1978 * Now read the page into the buffer. Absent an error,
1979 * the read methods return max bitflips per ecc step.
1980 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001981 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001982 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001983 oob_required,
1984 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001985 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1986 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001987 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001988 col, bytes, bufpoi,
1989 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001990 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001991 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001992 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001993 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001994 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001995 /* Invalidate page cache */
1996 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001997 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001998 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001999
Mike Dunnedbc45402012-04-25 12:06:11 -07002000 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2001
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002002 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002003 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002004 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002005 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002006 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002007 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002008 chip->pagebuf_bitflips = ret;
2009 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002010 /* Invalidate page cache */
2011 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002012 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002013 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002015
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002016 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002017 int toread = min(oobreadlen, max_oobsize);
2018
2019 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002020 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002021 oob, ops, toread);
2022 oobreadlen -= toread;
2023 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002024 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002025
2026 if (chip->options & NAND_NEED_READRDY) {
2027 /* Apply delay or wait for ready/busy pin */
2028 if (!chip->dev_ready)
2029 udelay(chip->chip_delay);
2030 else
2031 nand_wait_ready(mtd);
2032 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002033
Brian Norrisba84fb52014-01-03 15:13:33 -08002034 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002035 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002036 retry_mode++;
2037 ret = nand_setup_read_retry(mtd,
2038 retry_mode);
2039 if (ret < 0)
2040 break;
2041
2042 /* Reset failures; retry */
2043 mtd->ecc_stats.failed = ecc_failures;
2044 goto read_retry;
2045 } else {
2046 /* No more retry modes; real failure */
2047 ecc_fail = true;
2048 }
2049 }
2050
2051 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002052 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002053 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002054 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002055 max_bitflips = max_t(unsigned int, max_bitflips,
2056 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002059 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002060
Brian Norrisba84fb52014-01-03 15:13:33 -08002061 /* Reset to retry mode 0 */
2062 if (retry_mode) {
2063 ret = nand_setup_read_retry(mtd, 0);
2064 if (ret < 0)
2065 break;
2066 retry_mode = 0;
2067 }
2068
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002069 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Brian Norris8b6e50c2011-05-25 14:59:01 -07002072 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 col = 0;
2074 /* Increment page address */
2075 realpage++;
2076
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002077 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /* Check, if we cross a chip boundary */
2079 if (!page) {
2080 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002081 chip->select_chip(mtd, -1);
2082 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002085 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002087 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002088 if (oob)
2089 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Mike Dunn3f91e942012-04-25 12:06:09 -07002091 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002092 return ret;
2093
Brian Norrisb72f3df2013-12-03 11:04:14 -08002094 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002095 return -EBADMSG;
2096
Mike Dunnedbc45402012-04-25 12:06:11 -07002097 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002098}
2099
2100/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002101 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002102 * @mtd: MTD device structure
2103 * @from: offset to read from
2104 * @len: number of bytes to read
2105 * @retlen: pointer to variable to store the number of read bytes
2106 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002107 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002108 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002109 */
2110static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2111 size_t *retlen, uint8_t *buf)
2112{
Brian Norris4a89ff82011-08-30 18:45:45 -07002113 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002114 int ret;
2115
Huang Shijie6a8214a2012-11-19 14:43:30 +08002116 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002117 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002118 ops.len = len;
2119 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002120 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002121 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002122 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002123 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002124 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
2127/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002128 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002129 * @mtd: mtd info structure
2130 * @chip: nand chip info structure
2131 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002132 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002133int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002134{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002135 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002136 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002137 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002138}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002139EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002140
2141/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002142 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002143 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002144 * @mtd: mtd info structure
2145 * @chip: nand chip info structure
2146 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002147 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002148int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2149 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002150{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002151 int length = mtd->oobsize;
2152 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2153 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002154 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002155 int i, toread, sndrnd = 0, pos;
2156
2157 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2158 for (i = 0; i < chip->ecc.steps; i++) {
2159 if (sndrnd) {
2160 pos = eccsize + i * (eccsize + chunk);
2161 if (mtd->writesize > 512)
2162 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2163 else
2164 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2165 } else
2166 sndrnd = 1;
2167 toread = min_t(int, length, chunk);
2168 chip->read_buf(mtd, bufpoi, toread);
2169 bufpoi += toread;
2170 length -= toread;
2171 }
2172 if (length > 0)
2173 chip->read_buf(mtd, bufpoi, length);
2174
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002175 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002176}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002177EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002178
2179/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002180 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002181 * @mtd: mtd info structure
2182 * @chip: nand chip info structure
2183 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002184 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002185int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002186{
2187 int status = 0;
2188 const uint8_t *buf = chip->oob_poi;
2189 int length = mtd->oobsize;
2190
2191 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2192 chip->write_buf(mtd, buf, length);
2193 /* Send command to program the OOB data */
2194 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2195
2196 status = chip->waitfunc(mtd, chip);
2197
Savin Zlobec0d420f92006-06-21 11:51:20 +02002198 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002199}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002200EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002201
2202/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002203 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002204 * with syndrome - only for large page flash
2205 * @mtd: mtd info structure
2206 * @chip: nand chip info structure
2207 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002208 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002209int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2210 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002211{
2212 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2213 int eccsize = chip->ecc.size, length = mtd->oobsize;
2214 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2215 const uint8_t *bufpoi = chip->oob_poi;
2216
2217 /*
2218 * data-ecc-data-ecc ... ecc-oob
2219 * or
2220 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2221 */
2222 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2223 pos = steps * (eccsize + chunk);
2224 steps = 0;
2225 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002226 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002227
2228 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2229 for (i = 0; i < steps; i++) {
2230 if (sndcmd) {
2231 if (mtd->writesize <= 512) {
2232 uint32_t fill = 0xFFFFFFFF;
2233
2234 len = eccsize;
2235 while (len > 0) {
2236 int num = min_t(int, len, 4);
2237 chip->write_buf(mtd, (uint8_t *)&fill,
2238 num);
2239 len -= num;
2240 }
2241 } else {
2242 pos = eccsize + i * (eccsize + chunk);
2243 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2244 }
2245 } else
2246 sndcmd = 1;
2247 len = min_t(int, length, chunk);
2248 chip->write_buf(mtd, bufpoi, len);
2249 bufpoi += len;
2250 length -= len;
2251 }
2252 if (length > 0)
2253 chip->write_buf(mtd, bufpoi, length);
2254
2255 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2256 status = chip->waitfunc(mtd, chip);
2257
2258 return status & NAND_STATUS_FAIL ? -EIO : 0;
2259}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002260EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002261
2262/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002263 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002264 * @mtd: MTD device structure
2265 * @from: offset to read from
2266 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002268 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002270static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2271 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Brian Norrisc00a0992012-05-01 17:12:54 -07002273 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002274 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002275 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002276 int readlen = ops->ooblen;
2277 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002278 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002279 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Brian Norris289c0522011-07-19 10:06:09 -07002281 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302282 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Brian Norris041e4572011-06-23 16:45:24 -07002284 stats = mtd->ecc_stats;
2285
Boris BREZILLON29f10582016-03-07 10:46:52 +01002286 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002287
2288 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002289 pr_debug("%s: attempt to start read outside oob\n",
2290 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002291 return -EINVAL;
2292 }
2293
2294 /* Do not allow reads past end of device */
2295 if (unlikely(from >= mtd->size ||
2296 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2297 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002298 pr_debug("%s: attempt to read beyond end of device\n",
2299 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002300 return -EINVAL;
2301 }
Vitaly Wool70145682006-11-03 18:20:38 +03002302
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002303 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002304 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002306 /* Shift to get page */
2307 realpage = (int)(from >> chip->page_shift);
2308 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Florian Fainellif8ac0412010-09-07 13:23:43 +02002310 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002311 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002312 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002313 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002314 ret = chip->ecc.read_oob(mtd, chip, page);
2315
2316 if (ret < 0)
2317 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002318
2319 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002320 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321
Brian Norris5bc7c332013-03-13 09:51:31 -07002322 if (chip->options & NAND_NEED_READRDY) {
2323 /* Apply delay or wait for ready/busy pin */
2324 if (!chip->dev_ready)
2325 udelay(chip->chip_delay);
2326 else
2327 nand_wait_ready(mtd);
2328 }
2329
Vitaly Wool70145682006-11-03 18:20:38 +03002330 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002331 if (!readlen)
2332 break;
2333
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002334 /* Increment page address */
2335 realpage++;
2336
2337 page = realpage & chip->pagemask;
2338 /* Check, if we cross a chip boundary */
2339 if (!page) {
2340 chipnr++;
2341 chip->select_chip(mtd, -1);
2342 chip->select_chip(mtd, chipnr);
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002345 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002347 ops->oobretlen = ops->ooblen - readlen;
2348
2349 if (ret < 0)
2350 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002351
2352 if (mtd->ecc_stats.failed - stats.failed)
2353 return -EBADMSG;
2354
2355 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
2358/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002359 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002360 * @mtd: MTD device structure
2361 * @from: offset to read from
2362 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002364 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002366static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2367 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002369 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002370
2371 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002374 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002375 pr_debug("%s: attempt to read beyond end of device\n",
2376 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return -EINVAL;
2378 }
2379
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002380 if (ops->mode != MTD_OPS_PLACE_OOB &&
2381 ops->mode != MTD_OPS_AUTO_OOB &&
2382 ops->mode != MTD_OPS_RAW)
2383 return -ENOTSUPP;
2384
Huang Shijie6a8214a2012-11-19 14:43:30 +08002385 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002387 if (!ops->datbuf)
2388 ret = nand_do_read_oob(mtd, from, ops);
2389 else
2390 ret = nand_do_read_ops(mtd, from, ops);
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002393 return ret;
2394}
2395
2396
2397/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002398 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002399 * @mtd: mtd info structure
2400 * @chip: nand chip info structure
2401 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002402 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002403 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002404 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002405 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002406 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002407static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002408 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002409{
2410 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002411 if (oob_required)
2412 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002413
2414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002417/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002418 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002419 * @mtd: mtd info structure
2420 * @chip: nand chip info structure
2421 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002422 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002423 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002424 *
2425 * We need a special oob layout and handling even when ECC isn't checked.
2426 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002427static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002428 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002429 const uint8_t *buf, int oob_required,
2430 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002431{
2432 int eccsize = chip->ecc.size;
2433 int eccbytes = chip->ecc.bytes;
2434 uint8_t *oob = chip->oob_poi;
2435 int steps, size;
2436
2437 for (steps = chip->ecc.steps; steps > 0; steps--) {
2438 chip->write_buf(mtd, buf, eccsize);
2439 buf += eccsize;
2440
2441 if (chip->ecc.prepad) {
2442 chip->write_buf(mtd, oob, chip->ecc.prepad);
2443 oob += chip->ecc.prepad;
2444 }
2445
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002446 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002447 oob += eccbytes;
2448
2449 if (chip->ecc.postpad) {
2450 chip->write_buf(mtd, oob, chip->ecc.postpad);
2451 oob += chip->ecc.postpad;
2452 }
2453 }
2454
2455 size = mtd->oobsize - (oob - chip->oob_poi);
2456 if (size)
2457 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002458
2459 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002460}
2461/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002462 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002463 * @mtd: mtd info structure
2464 * @chip: nand chip info structure
2465 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002466 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002467 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002468 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002469static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002470 const uint8_t *buf, int oob_required,
2471 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002472{
Boris Brezillon846031d2016-02-03 20:11:00 +01002473 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002474 int eccbytes = chip->ecc.bytes;
2475 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002476 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002477 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002478
Brian Norris7854d3f2011-06-23 14:12:08 -07002479 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002480 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2481 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002482
Boris Brezillon846031d2016-02-03 20:11:00 +01002483 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2484 chip->ecc.total);
2485 if (ret)
2486 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002487
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002488 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002489}
2490
2491/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002492 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002493 * @mtd: mtd info structure
2494 * @chip: nand chip info structure
2495 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002496 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002497 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002498 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002499static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002500 const uint8_t *buf, int oob_required,
2501 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002502{
Boris Brezillon846031d2016-02-03 20:11:00 +01002503 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002504 int eccbytes = chip->ecc.bytes;
2505 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002506 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002507 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002508
2509 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2510 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002511 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002512 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2513 }
2514
Boris Brezillon846031d2016-02-03 20:11:00 +01002515 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2516 chip->ecc.total);
2517 if (ret)
2518 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002519
2520 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002521
2522 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002523}
2524
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302525
2526/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002527 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302528 * @mtd: mtd info structure
2529 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002530 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302531 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002532 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302533 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002534 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302535 */
2536static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2537 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002538 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002539 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302540{
2541 uint8_t *oob_buf = chip->oob_poi;
2542 uint8_t *ecc_calc = chip->buffers->ecccalc;
2543 int ecc_size = chip->ecc.size;
2544 int ecc_bytes = chip->ecc.bytes;
2545 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302546 uint32_t start_step = offset / ecc_size;
2547 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2548 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002549 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302550
2551 for (step = 0; step < ecc_steps; step++) {
2552 /* configure controller for WRITE access */
2553 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2554
2555 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002556 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302557
2558 /* mask ECC of un-touched subpages by padding 0xFF */
2559 if ((step < start_step) || (step > end_step))
2560 memset(ecc_calc, 0xff, ecc_bytes);
2561 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002562 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302563
2564 /* mask OOB of un-touched subpages by padding 0xFF */
2565 /* if oob_required, preserve OOB metadata of written subpage */
2566 if (!oob_required || (step < start_step) || (step > end_step))
2567 memset(oob_buf, 0xff, oob_bytes);
2568
Brian Norrisd6a950802013-08-08 17:16:36 -07002569 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302570 ecc_calc += ecc_bytes;
2571 oob_buf += oob_bytes;
2572 }
2573
2574 /* copy calculated ECC for whole page to chip->buffer->oob */
2575 /* this include masked-value(0xFF) for unwritten subpages */
2576 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002577 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2578 chip->ecc.total);
2579 if (ret)
2580 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302581
2582 /* write OOB buffer to NAND device */
2583 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2584
2585 return 0;
2586}
2587
2588
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002589/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002590 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002591 * @mtd: mtd info structure
2592 * @chip: nand chip info structure
2593 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002594 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002595 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002596 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002597 * The hw generator calculates the error syndrome automatically. Therefore we
2598 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002599 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002600static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002601 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002602 const uint8_t *buf, int oob_required,
2603 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002604{
2605 int i, eccsize = chip->ecc.size;
2606 int eccbytes = chip->ecc.bytes;
2607 int eccsteps = chip->ecc.steps;
2608 const uint8_t *p = buf;
2609 uint8_t *oob = chip->oob_poi;
2610
2611 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2612
2613 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2614 chip->write_buf(mtd, p, eccsize);
2615
2616 if (chip->ecc.prepad) {
2617 chip->write_buf(mtd, oob, chip->ecc.prepad);
2618 oob += chip->ecc.prepad;
2619 }
2620
2621 chip->ecc.calculate(mtd, p, oob);
2622 chip->write_buf(mtd, oob, eccbytes);
2623 oob += eccbytes;
2624
2625 if (chip->ecc.postpad) {
2626 chip->write_buf(mtd, oob, chip->ecc.postpad);
2627 oob += chip->ecc.postpad;
2628 }
2629 }
2630
2631 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002632 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002633 if (i)
2634 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002635
2636 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002637}
2638
2639/**
David Woodhouse956e9442006-09-25 17:12:39 +01002640 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002641 * @mtd: MTD device structure
2642 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302643 * @offset: address offset within the page
2644 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002645 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002646 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002647 * @page: page number to write
2648 * @cached: cached programming
2649 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002650 */
2651static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302652 uint32_t offset, int data_len, const uint8_t *buf,
2653 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002654{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302655 int status, subpage;
2656
2657 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2658 chip->ecc.write_subpage)
2659 subpage = offset || (data_len < mtd->writesize);
2660 else
2661 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002662
Marc Gonzalez3371d662016-11-15 10:56:20 +01002663 if (nand_standard_page_accessors(&chip->ecc))
2664 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002665
David Woodhouse956e9442006-09-25 17:12:39 +01002666 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302667 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002668 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302669 else if (subpage)
2670 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002671 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002672 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002673 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2674 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002675
2676 if (status < 0)
2677 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002678
2679 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002680 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002681 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002682 */
2683 cached = 0;
2684
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002685 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002686
Marc Gonzalez3371d662016-11-15 10:56:20 +01002687 if (nand_standard_page_accessors(&chip->ecc))
2688 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002689 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002690 /*
2691 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002692 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002693 */
2694 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2695 status = chip->errstat(mtd, chip, FL_WRITING, status,
2696 page);
2697
2698 if (status & NAND_STATUS_FAIL)
2699 return -EIO;
2700 } else {
2701 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002702 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002703 }
2704
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002705 return 0;
2706}
2707
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002708/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002709 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002710 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002711 * @oob: oob data buffer
2712 * @len: oob data write length
2713 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002714 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002715static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2716 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002717{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002718 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002719 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002720
2721 /*
2722 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2723 * data from a previous OOB read.
2724 */
2725 memset(chip->oob_poi, 0xff, mtd->oobsize);
2726
Florian Fainellif8ac0412010-09-07 13:23:43 +02002727 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002728
Brian Norris0612b9d2011-08-30 18:45:40 -07002729 case MTD_OPS_PLACE_OOB:
2730 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002731 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2732 return oob + len;
2733
Boris Brezillon846031d2016-02-03 20:11:00 +01002734 case MTD_OPS_AUTO_OOB:
2735 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2736 ops->ooboffs, len);
2737 BUG_ON(ret);
2738 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002739
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002740 default:
2741 BUG();
2742 }
2743 return NULL;
2744}
2745
Florian Fainellif8ac0412010-09-07 13:23:43 +02002746#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002747
2748/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002749 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002750 * @mtd: MTD device structure
2751 * @to: offset to write to
2752 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002753 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002754 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002755 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002756static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2757 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002758{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002759 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002760 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002761 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002762
2763 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002764 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002765
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002766 uint8_t *oob = ops->oobbuf;
2767 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302768 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002769 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002770
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002771 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002772 if (!writelen)
2773 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002774
Brian Norris8b6e50c2011-05-25 14:59:01 -07002775 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002776 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002777 pr_notice("%s: attempt to write non page aligned data\n",
2778 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002779 return -EINVAL;
2780 }
2781
Thomas Gleixner29072b92006-09-28 15:38:36 +02002782 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002783
Thomas Gleixner6a930962006-06-28 00:11:45 +02002784 chipnr = (int)(to >> chip->chip_shift);
2785 chip->select_chip(mtd, chipnr);
2786
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002787 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002788 if (nand_check_wp(mtd)) {
2789 ret = -EIO;
2790 goto err_out;
2791 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002792
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002793 realpage = (int)(to >> chip->page_shift);
2794 page = realpage & chip->pagemask;
2795 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2796
2797 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002798 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2799 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002800 chip->pagebuf = -1;
2801
Maxim Levitsky782ce792010-02-22 20:39:36 +02002802 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002803 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2804 ret = -EINVAL;
2805 goto err_out;
2806 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002807
Florian Fainellif8ac0412010-09-07 13:23:43 +02002808 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002809 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002810 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002811 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002812 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002813 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002814
Kamal Dasu66507c72014-05-01 20:51:19 -04002815 if (part_pagewr)
2816 use_bufpoi = 1;
2817 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2818 use_bufpoi = !virt_addr_valid(buf);
2819 else
2820 use_bufpoi = 0;
2821
2822 /* Partial page write?, or need to use bounce buffer */
2823 if (use_bufpoi) {
2824 pr_debug("%s: using write bounce buffer for buf@%p\n",
2825 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002826 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002827 if (part_pagewr)
2828 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002829 chip->pagebuf = -1;
2830 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2831 memcpy(&chip->buffers->databuf[column], buf, bytes);
2832 wbuf = chip->buffers->databuf;
2833 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002834
Maxim Levitsky782ce792010-02-22 20:39:36 +02002835 if (unlikely(oob)) {
2836 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002837 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002838 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002839 } else {
2840 /* We still need to erase leftover OOB data */
2841 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002842 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302843 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2844 oob_required, page, cached,
2845 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002846 if (ret)
2847 break;
2848
2849 writelen -= bytes;
2850 if (!writelen)
2851 break;
2852
Thomas Gleixner29072b92006-09-28 15:38:36 +02002853 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002854 buf += bytes;
2855 realpage++;
2856
2857 page = realpage & chip->pagemask;
2858 /* Check, if we cross a chip boundary */
2859 if (!page) {
2860 chipnr++;
2861 chip->select_chip(mtd, -1);
2862 chip->select_chip(mtd, chipnr);
2863 }
2864 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002865
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002866 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002867 if (unlikely(oob))
2868 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002869
2870err_out:
2871 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002872 return ret;
2873}
2874
2875/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002876 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002877 * @mtd: MTD device structure
2878 * @to: offset to write to
2879 * @len: number of bytes to write
2880 * @retlen: pointer to variable to store the number of written bytes
2881 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002882 *
2883 * NAND write with ECC. Used when performing writes in interrupt context, this
2884 * may for example be called by mtdoops when writing an oops while in panic.
2885 */
2886static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2887 size_t *retlen, const uint8_t *buf)
2888{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002889 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002890 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002891 int ret;
2892
Brian Norris8b6e50c2011-05-25 14:59:01 -07002893 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002894 panic_nand_wait(mtd, chip, 400);
2895
Brian Norris8b6e50c2011-05-25 14:59:01 -07002896 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002897 panic_nand_get_device(chip, mtd, FL_WRITING);
2898
Brian Norris0ec56dc2015-02-28 02:02:30 -08002899 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002900 ops.len = len;
2901 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002902 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002903
Brian Norris4a89ff82011-08-30 18:45:45 -07002904 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002905
Brian Norris4a89ff82011-08-30 18:45:45 -07002906 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002907 return ret;
2908}
2909
2910/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002911 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002912 * @mtd: MTD device structure
2913 * @to: offset to write to
2914 * @len: number of bytes to write
2915 * @retlen: pointer to variable to store the number of written bytes
2916 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002918 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002920static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002921 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Brian Norris4a89ff82011-08-30 18:45:45 -07002923 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002924 int ret;
2925
Huang Shijie6a8214a2012-11-19 14:43:30 +08002926 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002927 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002928 ops.len = len;
2929 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002930 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002931 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002932 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002933 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002934 return ret;
2935}
2936
2937/**
2938 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002939 * @mtd: MTD device structure
2940 * @to: offset to write to
2941 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002942 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002943 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002944 */
2945static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2946 struct mtd_oob_ops *ops)
2947{
Adrian Hunter03736152007-01-31 17:58:29 +02002948 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002949 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Brian Norris289c0522011-07-19 10:06:09 -07002951 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302952 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Boris BREZILLON29f10582016-03-07 10:46:52 +01002954 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002957 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002958 pr_debug("%s: attempt to write past end of page\n",
2959 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return -EINVAL;
2961 }
2962
Adrian Hunter03736152007-01-31 17:58:29 +02002963 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002964 pr_debug("%s: attempt to start write outside oob\n",
2965 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002966 return -EINVAL;
2967 }
2968
Jason Liu775adc3d42011-02-25 13:06:18 +08002969 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002970 if (unlikely(to >= mtd->size ||
2971 ops->ooboffs + ops->ooblen >
2972 ((mtd->size >> chip->page_shift) -
2973 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002974 pr_debug("%s: attempt to write beyond end of device\n",
2975 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002976 return -EINVAL;
2977 }
2978
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002979 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002980
2981 /*
2982 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2983 * of my DiskOnChip 2000 test units) will clear the whole data page too
2984 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2985 * it in the doc2000 driver in August 1999. dwmw2.
2986 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002987 nand_reset(chip, chipnr);
2988
2989 chip->select_chip(mtd, chipnr);
2990
2991 /* Shift to get page */
2992 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002995 if (nand_check_wp(mtd)) {
2996 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002997 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002998 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003001 if (page == chip->pagebuf)
3002 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003004 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003005
Brian Norris0612b9d2011-08-30 18:45:40 -07003006 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003007 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3008 else
3009 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003010
Huang Shijieb0bb6902012-11-19 14:43:29 +08003011 chip->select_chip(mtd, -1);
3012
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003013 if (status)
3014 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Vitaly Wool70145682006-11-03 18:20:38 +03003016 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003018 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003021/**
3022 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003023 * @mtd: MTD device structure
3024 * @to: offset to write to
3025 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003026 */
3027static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3028 struct mtd_oob_ops *ops)
3029{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003030 int ret = -ENOTSUPP;
3031
3032 ops->retlen = 0;
3033
3034 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003035 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003036 pr_debug("%s: attempt to write beyond end of device\n",
3037 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003038 return -EINVAL;
3039 }
3040
Huang Shijie6a8214a2012-11-19 14:43:30 +08003041 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003042
Florian Fainellif8ac0412010-09-07 13:23:43 +02003043 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003044 case MTD_OPS_PLACE_OOB:
3045 case MTD_OPS_AUTO_OOB:
3046 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003047 break;
3048
3049 default:
3050 goto out;
3051 }
3052
3053 if (!ops->datbuf)
3054 ret = nand_do_write_oob(mtd, to, ops);
3055 else
3056 ret = nand_do_write_ops(mtd, to, ops);
3057
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003058out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003059 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return ret;
3061}
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063/**
Brian Norris49c50b92014-05-06 16:02:19 -07003064 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003065 * @mtd: MTD device structure
3066 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 *
Brian Norris49c50b92014-05-06 16:02:19 -07003068 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 */
Brian Norris49c50b92014-05-06 16:02:19 -07003070static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003072 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003074 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3075 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003076
3077 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
3080/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003082 * @mtd: MTD device structure
3083 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003085 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003087static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
David Woodhousee0c7d762006-05-13 18:07:53 +01003089 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003093 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003094 * @mtd: MTD device structure
3095 * @instr: erase instruction
3096 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003098 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003100int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3101 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Adrian Hunter69423d92008-12-10 13:37:21 +00003103 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003104 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003105 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Brian Norris289c0522011-07-19 10:06:09 -07003107 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3108 __func__, (unsigned long long)instr->addr,
3109 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303111 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003115 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003118 page = (int)(instr->addr >> chip->page_shift);
3119 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003122 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003125 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /* Check, if it is write protected */
3128 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003129 pr_debug("%s: device is write protected!\n",
3130 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 instr->state = MTD_ERASE_FAILED;
3132 goto erase_exit;
3133 }
3134
3135 /* Loop through the pages */
3136 len = instr->len;
3137
3138 instr->state = MTD_ERASING;
3139
3140 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003141 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003142 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303143 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003144 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3145 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 instr->state = MTD_ERASE_FAILED;
3147 goto erase_exit;
3148 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003149
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003150 /*
3151 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003152 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003153 */
3154 if (page <= chip->pagebuf && chip->pagebuf <
3155 (page + pages_per_block))
3156 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Brian Norris49c50b92014-05-06 16:02:19 -07003158 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003160 /*
3161 * See if operation failed and additional status checks are
3162 * available
3163 */
3164 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3165 status = chip->errstat(mtd, chip, FL_ERASING,
3166 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003169 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003170 pr_debug("%s: failed erase, page 0x%08x\n",
3171 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003173 instr->fail_addr =
3174 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 goto erase_exit;
3176 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003179 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 page += pages_per_block;
3181
3182 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003183 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003185 chip->select_chip(mtd, -1);
3186 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188 }
3189 instr->state = MTD_ERASE_DONE;
3190
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003191erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003196 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 nand_release_device(mtd);
3198
David Woodhouse49defc02007-10-06 15:01:59 -04003199 /* Do call back function */
3200 if (!ret)
3201 mtd_erase_callback(instr);
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 /* Return more or less happy */
3204 return ret;
3205}
3206
3207/**
3208 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003209 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003211 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003213static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Brian Norris289c0522011-07-19 10:06:09 -07003215 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003218 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003220 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003224 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003225 * @mtd: MTD device structure
3226 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003228static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303230 struct nand_chip *chip = mtd_to_nand(mtd);
3231 int chipnr = (int)(offs >> chip->chip_shift);
3232 int ret;
3233
3234 /* Select the NAND device */
3235 nand_get_device(mtd, FL_READING);
3236 chip->select_chip(mtd, chipnr);
3237
3238 ret = nand_block_checkbad(mtd, offs, 0);
3239
3240 chip->select_chip(mtd, -1);
3241 nand_release_device(mtd);
3242
3243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
3246/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003247 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003248 * @mtd: MTD device structure
3249 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003251static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 int ret;
3254
Florian Fainellif8ac0412010-09-07 13:23:43 +02003255 ret = nand_block_isbad(mtd, ofs);
3256 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003257 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (ret > 0)
3259 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003260 return ret;
3261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Brian Norris5a0edb22013-07-30 17:52:58 -07003263 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
3266/**
Zach Brown56718422017-01-10 13:30:20 -06003267 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3268 * @mtd: MTD device structure
3269 * @ofs: offset relative to mtd start
3270 * @len: length of mtd
3271 */
3272static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3273{
3274 struct nand_chip *chip = mtd_to_nand(mtd);
3275 u32 part_start_block;
3276 u32 part_end_block;
3277 u32 part_start_die;
3278 u32 part_end_die;
3279
3280 /*
3281 * max_bb_per_die and blocks_per_die used to determine
3282 * the maximum bad block count.
3283 */
3284 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3285 return -ENOTSUPP;
3286
3287 /* Get the start and end of the partition in erase blocks. */
3288 part_start_block = mtd_div_by_eb(ofs, mtd);
3289 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3290
3291 /* Get the start and end LUNs of the partition. */
3292 part_start_die = part_start_block / chip->blocks_per_die;
3293 part_end_die = part_end_block / chip->blocks_per_die;
3294
3295 /*
3296 * Look up the bad blocks per unit and multiply by the number of units
3297 * that the partition spans.
3298 */
3299 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3300}
3301
3302/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003303 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3304 * @mtd: MTD device structure
3305 * @chip: nand chip info structure
3306 * @addr: feature address.
3307 * @subfeature_param: the subfeature parameters, a four bytes array.
3308 */
3309static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3310 int addr, uint8_t *subfeature_param)
3311{
3312 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003313 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003314
David Mosbergerd914c932013-05-29 15:30:13 +03003315 if (!chip->onfi_version ||
3316 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3317 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003318 return -EINVAL;
3319
3320 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003321 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3322 chip->write_byte(mtd, subfeature_param[i]);
3323
Huang Shijie7db03ec2012-09-13 14:57:52 +08003324 status = chip->waitfunc(mtd, chip);
3325 if (status & NAND_STATUS_FAIL)
3326 return -EIO;
3327 return 0;
3328}
3329
3330/**
3331 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3332 * @mtd: MTD device structure
3333 * @chip: nand chip info structure
3334 * @addr: feature address.
3335 * @subfeature_param: the subfeature parameters, a four bytes array.
3336 */
3337static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3338 int addr, uint8_t *subfeature_param)
3339{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003340 int i;
3341
David Mosbergerd914c932013-05-29 15:30:13 +03003342 if (!chip->onfi_version ||
3343 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3344 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003345 return -EINVAL;
3346
Huang Shijie7db03ec2012-09-13 14:57:52 +08003347 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003348 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3349 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003350 return 0;
3351}
3352
3353/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003354 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003355 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003356 */
3357static int nand_suspend(struct mtd_info *mtd)
3358{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003359 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003360}
3361
3362/**
3363 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003364 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003365 */
3366static void nand_resume(struct mtd_info *mtd)
3367{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003368 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003369
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003370 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003371 nand_release_device(mtd);
3372 else
Brian Norrisd0370212011-07-19 10:06:08 -07003373 pr_err("%s called for a chip which is not in suspended state\n",
3374 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003375}
3376
Scott Branden72ea4032014-11-20 11:18:05 -08003377/**
3378 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3379 * prevent further operations
3380 * @mtd: MTD device structure
3381 */
3382static void nand_shutdown(struct mtd_info *mtd)
3383{
Brian Norris9ca641b2015-11-09 16:37:28 -08003384 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003385}
3386
Brian Norris8b6e50c2011-05-25 14:59:01 -07003387/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003388static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003391 if (!chip->chip_delay)
3392 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003395 if (chip->cmdfunc == NULL)
3396 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003399 if (chip->waitfunc == NULL)
3400 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003402 if (!chip->select_chip)
3403 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003404
Huang Shijie4204ccc2013-08-16 10:10:07 +08003405 /* set for ONFI nand */
3406 if (!chip->onfi_set_features)
3407 chip->onfi_set_features = nand_onfi_set_features;
3408 if (!chip->onfi_get_features)
3409 chip->onfi_get_features = nand_onfi_get_features;
3410
Brian Norris68e80782013-07-18 01:17:02 -07003411 /* If called twice, pointers that depend on busw may need to be reset */
3412 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003413 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3414 if (!chip->read_word)
3415 chip->read_word = nand_read_word;
3416 if (!chip->block_bad)
3417 chip->block_bad = nand_block_bad;
3418 if (!chip->block_markbad)
3419 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003420 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003421 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003422 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3423 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003424 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003425 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003426 if (!chip->scan_bbt)
3427 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003428
3429 if (!chip->controller) {
3430 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003431 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003432 }
3433
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003434}
3435
Brian Norris8b6e50c2011-05-25 14:59:01 -07003436/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003437static void sanitize_string(uint8_t *s, size_t len)
3438{
3439 ssize_t i;
3440
Brian Norris8b6e50c2011-05-25 14:59:01 -07003441 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003442 s[len - 1] = 0;
3443
Brian Norris8b6e50c2011-05-25 14:59:01 -07003444 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003445 for (i = 0; i < len - 1; i++) {
3446 if (s[i] < ' ' || s[i] > 127)
3447 s[i] = '?';
3448 }
3449
Brian Norris8b6e50c2011-05-25 14:59:01 -07003450 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003451 strim(s);
3452}
3453
3454static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3455{
3456 int i;
3457 while (len--) {
3458 crc ^= *p++ << 8;
3459 for (i = 0; i < 8; i++)
3460 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3461 }
3462
3463 return crc;
3464}
3465
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003466/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003467static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3468 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003469{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003470 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003471 struct onfi_ext_param_page *ep;
3472 struct onfi_ext_section *s;
3473 struct onfi_ext_ecc_info *ecc;
3474 uint8_t *cursor;
3475 int ret = -EINVAL;
3476 int len;
3477 int i;
3478
3479 len = le16_to_cpu(p->ext_param_page_length) * 16;
3480 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003481 if (!ep)
3482 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003483
3484 /* Send our own NAND_CMD_PARAM. */
3485 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3486
3487 /* Use the Change Read Column command to skip the ONFI param pages. */
3488 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3489 sizeof(*p) * p->num_of_param_pages , -1);
3490
3491 /* Read out the Extended Parameter Page. */
3492 chip->read_buf(mtd, (uint8_t *)ep, len);
3493 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3494 != le16_to_cpu(ep->crc))) {
3495 pr_debug("fail in the CRC.\n");
3496 goto ext_out;
3497 }
3498
3499 /*
3500 * Check the signature.
3501 * Do not strictly follow the ONFI spec, maybe changed in future.
3502 */
3503 if (strncmp(ep->sig, "EPPS", 4)) {
3504 pr_debug("The signature is invalid.\n");
3505 goto ext_out;
3506 }
3507
3508 /* find the ECC section. */
3509 cursor = (uint8_t *)(ep + 1);
3510 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3511 s = ep->sections + i;
3512 if (s->type == ONFI_SECTION_TYPE_2)
3513 break;
3514 cursor += s->length * 16;
3515 }
3516 if (i == ONFI_EXT_SECTION_MAX) {
3517 pr_debug("We can not find the ECC section.\n");
3518 goto ext_out;
3519 }
3520
3521 /* get the info we want. */
3522 ecc = (struct onfi_ext_ecc_info *)cursor;
3523
Brian Norris4ae7d222013-09-16 18:20:21 -07003524 if (!ecc->codeword_size) {
3525 pr_debug("Invalid codeword size\n");
3526 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003527 }
3528
Brian Norris4ae7d222013-09-16 18:20:21 -07003529 chip->ecc_strength_ds = ecc->ecc_bits;
3530 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003531 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003532
3533ext_out:
3534 kfree(ep);
3535 return ret;
3536}
3537
Brian Norris8429bb32013-12-03 15:51:09 -08003538static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3539{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003540 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003541 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3542
3543 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3544 feature);
3545}
3546
3547/*
3548 * Configure chip properties from Micron vendor-specific ONFI table
3549 */
3550static void nand_onfi_detect_micron(struct nand_chip *chip,
3551 struct nand_onfi_params *p)
3552{
3553 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3554
3555 if (le16_to_cpu(p->vendor_revision) < 1)
3556 return;
3557
3558 chip->read_retries = micron->read_retry_options;
3559 chip->setup_read_retry = nand_setup_read_retry_micron;
3560}
3561
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003562/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003563 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003564 */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003565static int nand_flash_detect_onfi(struct nand_chip *chip, int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003566{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003567 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003568 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003569 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003570 int val;
3571
Brian Norris7854d3f2011-06-23 14:12:08 -07003572 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003573 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3574 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3575 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3576 return 0;
3577
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003578 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3579 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003580 for (j = 0; j < sizeof(*p); j++)
3581 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003582 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3583 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003584 break;
3585 }
3586 }
3587
Brian Norrisc7f23a72013-08-13 10:51:55 -07003588 if (i == 3) {
3589 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003590 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003591 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003592
Brian Norris8b6e50c2011-05-25 14:59:01 -07003593 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003594 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003595 if (val & (1 << 5))
3596 chip->onfi_version = 23;
3597 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003598 chip->onfi_version = 22;
3599 else if (val & (1 << 3))
3600 chip->onfi_version = 21;
3601 else if (val & (1 << 2))
3602 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003603 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003604 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003605
3606 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003607 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003608 return 0;
3609 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003610
3611 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3612 sanitize_string(p->model, sizeof(p->model));
3613 if (!mtd->name)
3614 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003615
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003616 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003617
3618 /*
3619 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3620 * (don't ask me who thought of this...). MTD assumes that these
3621 * dimensions will be power-of-2, so just truncate the remaining area.
3622 */
3623 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3624 mtd->erasesize *= mtd->writesize;
3625
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003626 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003627
3628 /* See erasesize comment */
3629 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003630 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003631 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003632
Zach Brown34da5f52017-01-10 13:30:21 -06003633 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3634 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3635
Huang Shijiee2985fc2013-05-17 11:17:30 +08003636 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003637 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003638 else
3639 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003640
Huang Shijie10c86ba2013-05-17 11:17:26 +08003641 if (p->ecc_bits != 0xff) {
3642 chip->ecc_strength_ds = p->ecc_bits;
3643 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003644 } else if (chip->onfi_version >= 21 &&
3645 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3646
3647 /*
3648 * The nand_flash_detect_ext_param_page() uses the
3649 * Change Read Column command which maybe not supported
3650 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3651 * now. We do not replace user supplied command function.
3652 */
3653 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3654 chip->cmdfunc = nand_command_lp;
3655
3656 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003657 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003658 pr_warn("Failed to detect ONFI extended param page\n");
3659 } else {
3660 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003661 }
3662
Brian Norris8429bb32013-12-03 15:51:09 -08003663 if (p->jedec_id == NAND_MFR_MICRON)
3664 nand_onfi_detect_micron(chip, p);
3665
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003666 return 1;
3667}
3668
3669/*
Huang Shijie91361812014-02-21 13:39:40 +08003670 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3671 */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003672static int nand_flash_detect_jedec(struct nand_chip *chip, int *busw)
Huang Shijie91361812014-02-21 13:39:40 +08003673{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003674 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003675 struct nand_jedec_params *p = &chip->jedec_params;
3676 struct jedec_ecc_info *ecc;
3677 int val;
3678 int i, j;
3679
3680 /* Try JEDEC for unknown chip or LP */
3681 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3682 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3683 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3684 chip->read_byte(mtd) != 'C')
3685 return 0;
3686
3687 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3688 for (i = 0; i < 3; i++) {
3689 for (j = 0; j < sizeof(*p); j++)
3690 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3691
3692 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3693 le16_to_cpu(p->crc))
3694 break;
3695 }
3696
3697 if (i == 3) {
3698 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3699 return 0;
3700 }
3701
3702 /* Check version */
3703 val = le16_to_cpu(p->revision);
3704 if (val & (1 << 2))
3705 chip->jedec_version = 10;
3706 else if (val & (1 << 1))
3707 chip->jedec_version = 1; /* vendor specific version */
3708
3709 if (!chip->jedec_version) {
3710 pr_info("unsupported JEDEC version: %d\n", val);
3711 return 0;
3712 }
3713
3714 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3715 sanitize_string(p->model, sizeof(p->model));
3716 if (!mtd->name)
3717 mtd->name = p->model;
3718
3719 mtd->writesize = le32_to_cpu(p->byte_per_page);
3720
3721 /* Please reference to the comment for nand_flash_detect_onfi. */
3722 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3723 mtd->erasesize *= mtd->writesize;
3724
3725 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3726
3727 /* Please reference to the comment for nand_flash_detect_onfi. */
3728 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3729 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3730 chip->bits_per_cell = p->bits_per_cell;
3731
3732 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3733 *busw = NAND_BUSWIDTH_16;
3734 else
3735 *busw = 0;
3736
3737 /* ECC info */
3738 ecc = &p->ecc_info[0];
3739
3740 if (ecc->codeword_size >= 9) {
3741 chip->ecc_strength_ds = ecc->ecc_bits;
3742 chip->ecc_step_ds = 1 << ecc->codeword_size;
3743 } else {
3744 pr_warn("Invalid codeword size\n");
3745 }
3746
3747 return 1;
3748}
3749
3750/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003751 * nand_id_has_period - Check if an ID string has a given wraparound period
3752 * @id_data: the ID string
3753 * @arrlen: the length of the @id_data array
3754 * @period: the period of repitition
3755 *
3756 * Check if an ID string is repeated within a given sequence of bytes at
3757 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003758 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003759 * if the repetition has a period of @period; otherwise, returns zero.
3760 */
3761static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3762{
3763 int i, j;
3764 for (i = 0; i < period; i++)
3765 for (j = i + period; j < arrlen; j += period)
3766 if (id_data[i] != id_data[j])
3767 return 0;
3768 return 1;
3769}
3770
3771/*
3772 * nand_id_len - Get the length of an ID string returned by CMD_READID
3773 * @id_data: the ID string
3774 * @arrlen: the length of the @id_data array
3775
3776 * Returns the length of the ID string, according to known wraparound/trailing
3777 * zero patterns. If no pattern exists, returns the length of the array.
3778 */
3779static int nand_id_len(u8 *id_data, int arrlen)
3780{
3781 int last_nonzero, period;
3782
3783 /* Find last non-zero byte */
3784 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3785 if (id_data[last_nonzero])
3786 break;
3787
3788 /* All zeros */
3789 if (last_nonzero < 0)
3790 return 0;
3791
3792 /* Calculate wraparound period */
3793 for (period = 1; period < arrlen; period++)
3794 if (nand_id_has_period(id_data, arrlen, period))
3795 break;
3796
3797 /* There's a repeated pattern */
3798 if (period < arrlen)
3799 return period;
3800
3801 /* There are trailing zeros */
3802 if (last_nonzero < arrlen - 1)
3803 return last_nonzero + 1;
3804
3805 /* No pattern detected */
3806 return arrlen;
3807}
3808
Huang Shijie7db906b2013-09-25 14:58:11 +08003809/* Extract the bits of per cell from the 3rd byte of the extended ID */
3810static int nand_get_bits_per_cell(u8 cellinfo)
3811{
3812 int bits;
3813
3814 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3815 bits >>= NAND_CI_CELLTYPE_SHIFT;
3816 return bits + 1;
3817}
3818
Brian Norrise3b88bd2012-09-24 20:40:52 -07003819/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003820 * Many new NAND share similar device ID codes, which represent the size of the
3821 * chip. The rest of the parameters must be decoded according to generic or
3822 * manufacturer-specific "extended ID" decoding patterns.
3823 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003824static void nand_decode_ext_id(struct nand_chip *chip, int *busw)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003825{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003826 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003827 int extid, id_len = chip->id.len;
3828 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003829 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003830 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003831 /* The 4th id byte is the important one */
3832 extid = id_data[3];
3833
3834 /*
3835 * Field definitions are in the following datasheets:
3836 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003837 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003838 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003839 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003840 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3841 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003842 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003843 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003844 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003845 /* Calc pagesize */
3846 mtd->writesize = 2048 << (extid & 0x03);
3847 extid >>= 2;
3848 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003849 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003850 case 1:
3851 mtd->oobsize = 128;
3852 break;
3853 case 2:
3854 mtd->oobsize = 218;
3855 break;
3856 case 3:
3857 mtd->oobsize = 400;
3858 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003859 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003860 mtd->oobsize = 436;
3861 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003862 case 5:
3863 mtd->oobsize = 512;
3864 break;
3865 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003866 mtd->oobsize = 640;
3867 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003868 case 7:
3869 default: /* Other cases are "reserved" (unknown) */
3870 mtd->oobsize = 1024;
3871 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003872 }
3873 extid >>= 2;
3874 /* Calc blocksize */
3875 mtd->erasesize = (128 * 1024) <<
3876 (((extid >> 1) & 0x04) | (extid & 0x03));
3877 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003878 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003879 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003880 unsigned int tmp;
3881
3882 /* Calc pagesize */
3883 mtd->writesize = 2048 << (extid & 0x03);
3884 extid >>= 2;
3885 /* Calc oobsize */
3886 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3887 case 0:
3888 mtd->oobsize = 128;
3889 break;
3890 case 1:
3891 mtd->oobsize = 224;
3892 break;
3893 case 2:
3894 mtd->oobsize = 448;
3895 break;
3896 case 3:
3897 mtd->oobsize = 64;
3898 break;
3899 case 4:
3900 mtd->oobsize = 32;
3901 break;
3902 case 5:
3903 mtd->oobsize = 16;
3904 break;
3905 default:
3906 mtd->oobsize = 640;
3907 break;
3908 }
3909 extid >>= 2;
3910 /* Calc blocksize */
3911 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3912 if (tmp < 0x03)
3913 mtd->erasesize = (128 * 1024) << tmp;
3914 else if (tmp == 0x03)
3915 mtd->erasesize = 768 * 1024;
3916 else
3917 mtd->erasesize = (64 * 1024) << tmp;
3918 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003919 } else {
3920 /* Calc pagesize */
3921 mtd->writesize = 1024 << (extid & 0x03);
3922 extid >>= 2;
3923 /* Calc oobsize */
3924 mtd->oobsize = (8 << (extid & 0x01)) *
3925 (mtd->writesize >> 9);
3926 extid >>= 2;
3927 /* Calc blocksize. Blocksize is multiples of 64KiB */
3928 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3929 extid >>= 2;
3930 /* Get buswidth information */
3931 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003932
3933 /*
3934 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3935 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3936 * follows:
3937 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3938 * 110b -> 24nm
3939 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3940 */
3941 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003942 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003943 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3944 !(id_data[4] & 0x80) /* !BENAND */) {
3945 mtd->oobsize = 32 * mtd->writesize >> 9;
3946 }
3947
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003948 }
3949}
3950
3951/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003952 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3953 * decodes a matching ID table entry and assigns the MTD size parameters for
3954 * the chip.
3955 */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003956static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type,
Boris Brezillon7f501f02016-05-24 19:20:05 +02003957 int *busw)
Brian Norrisf23a4812012-09-24 20:40:51 -07003958{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003959 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003960 u8 *id_data = chip->id.data;
Brian Norrisf23a4812012-09-24 20:40:51 -07003961 int maf_id = id_data[0];
3962
3963 mtd->erasesize = type->erasesize;
3964 mtd->writesize = type->pagesize;
3965 mtd->oobsize = mtd->writesize / 32;
3966 *busw = type->options & NAND_BUSWIDTH_16;
3967
Huang Shijie1c195e92013-09-25 14:58:12 +08003968 /* All legacy ID NAND are small-page, SLC */
3969 chip->bits_per_cell = 1;
3970
Brian Norrisf23a4812012-09-24 20:40:51 -07003971 /*
3972 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3973 * some Spansion chips have erasesize that conflicts with size
3974 * listed in nand_ids table.
3975 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3976 */
3977 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3978 && id_data[6] == 0x00 && id_data[7] == 0x00
3979 && mtd->writesize == 512) {
3980 mtd->erasesize = 128 * 1024;
3981 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3982 }
3983}
3984
3985/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003986 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3987 * heuristic patterns using various detected parameters (e.g., manufacturer,
3988 * page size, cell-type information).
3989 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003990static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003991{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003992 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003993 u8 *id_data = chip->id.data;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003994 int maf_id = id_data[0];
3995
3996 /* Set the bad block position */
3997 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3998 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3999 else
4000 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4001
4002 /*
4003 * Bad block marker is stored in the last page of each block on Samsung
4004 * and Hynix MLC devices; stored in first two pages of each block on
4005 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4006 * AMD/Spansion, and Macronix. All others scan only the first page.
4007 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004008 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07004009 (maf_id == NAND_MFR_SAMSUNG ||
4010 maf_id == NAND_MFR_HYNIX))
4011 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08004012 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07004013 (maf_id == NAND_MFR_SAMSUNG ||
4014 maf_id == NAND_MFR_HYNIX ||
4015 maf_id == NAND_MFR_TOSHIBA ||
4016 maf_id == NAND_MFR_AMD ||
4017 maf_id == NAND_MFR_MACRONIX)) ||
4018 (mtd->writesize == 2048 &&
4019 maf_id == NAND_MFR_MICRON))
4020 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4021}
4022
Huang Shijieec6e87e2013-03-15 11:01:00 +08004023static inline bool is_full_id_nand(struct nand_flash_dev *type)
4024{
4025 return type->id_len;
4026}
4027
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004028static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon7f501f02016-05-24 19:20:05 +02004029 struct nand_flash_dev *type, int *busw)
Huang Shijieec6e87e2013-03-15 11:01:00 +08004030{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004031 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02004032 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004033
Huang Shijieec6e87e2013-03-15 11:01:00 +08004034 if (!strncmp(type->id, id_data, type->id_len)) {
4035 mtd->writesize = type->pagesize;
4036 mtd->erasesize = type->erasesize;
4037 mtd->oobsize = type->oobsize;
4038
Huang Shijie7db906b2013-09-25 14:58:11 +08004039 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004040 chip->chipsize = (uint64_t)type->chipsize << 20;
4041 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004042 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4043 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004044 chip->onfi_timing_mode_default =
4045 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004046
4047 *busw = type->options & NAND_BUSWIDTH_16;
4048
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004049 if (!mtd->name)
4050 mtd->name = type->name;
4051
Huang Shijieec6e87e2013-03-15 11:01:00 +08004052 return true;
4053 }
4054 return false;
4055}
4056
Brian Norris7e74c2d2012-09-24 20:40:49 -07004057/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004058 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004059 */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004060static int nand_get_flash_type(struct nand_chip *chip,
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004061 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004062{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004063 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08004064 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004065 int i, maf_idx;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004066 u8 *id_data = chip->id.data;
4067 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Karl Beldanef89a882008-09-15 14:37:29 +02004069 /*
4070 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004071 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004072 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004073 nand_reset(chip, 0);
4074
4075 /* Select the device */
4076 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004079 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004082 maf_id = chip->read_byte(mtd);
4083 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Brian Norris8b6e50c2011-05-25 14:59:01 -07004085 /*
4086 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004087 * interface concerns can cause random data which looks like a
4088 * possibly credible NAND flash to appear. If the two results do
4089 * not match, ignore the device completely.
4090 */
4091
4092 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4093
Brian Norris4aef9b72012-09-24 20:40:48 -07004094 /* Read entire ID string */
4095 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004096 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004097
Boris Brezillon7f501f02016-05-24 19:20:05 +02004098 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004099 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004100 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004101 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004102 }
4103
Boris Brezillon7f501f02016-05-24 19:20:05 +02004104 chip->id.len = nand_id_len(id_data, 8);
4105
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004106 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004107 type = nand_flash_ids;
4108
Huang Shijieec6e87e2013-03-15 11:01:00 +08004109 for (; type->name != NULL; type++) {
4110 if (is_full_id_nand(type)) {
Boris Brezillon7f501f02016-05-24 19:20:05 +02004111 if (find_full_id_nand(chip, type, &busw))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004112 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004113 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004114 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004115 }
4116 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004117
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004118 chip->onfi_version = 0;
4119 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004120 /* Check if the chip is ONFI compliant */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004121 if (nand_flash_detect_onfi(chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004122 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004123
4124 /* Check if the chip is JEDEC compliant */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004125 if (nand_flash_detect_jedec(chip, &busw))
Huang Shijie91361812014-02-21 13:39:40 +08004126 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004127 }
4128
David Woodhouse5e81e882010-02-26 18:32:56 +00004129 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004130 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004131
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02004132 if (!mtd->name)
4133 mtd->name = type->name;
4134
Adrian Hunter69423d92008-12-10 13:37:21 +00004135 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004136
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02004137 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004138 /* Decode parameters from extended ID */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004139 nand_decode_ext_id(chip, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004140 } else {
Boris Brezillon7f501f02016-05-24 19:20:05 +02004141 nand_decode_id(chip, type, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004142 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004143 /* Get chip options */
4144 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004145
Brian Norris8b6e50c2011-05-25 14:59:01 -07004146 /*
4147 * Check if chip is not a Samsung device. Do not clear the
4148 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004149 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004150 if (maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004151 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4152ident_done:
4153
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004154 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01004155 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Boris Brezillon7f501f02016-05-24 19:20:05 +02004156 if (nand_manuf_ids[maf_idx].id == maf_id)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004157 break;
4158 }
4159
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004160 if (chip->options & NAND_BUSWIDTH_AUTO) {
4161 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4162 chip->options |= busw;
4163 nand_set_defaults(chip, busw);
4164 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4165 /*
4166 * Check, if buswidth is correct. Hardware drivers should set
4167 * chip correct!
4168 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004169 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004170 maf_id, dev_id);
Ezequiel Garcia20171642013-11-25 08:30:31 -03004171 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4172 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004173 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4174 busw ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004175 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004176 }
4177
Boris Brezillon7f501f02016-05-24 19:20:05 +02004178 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004179
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004180 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004181 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004182 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004183 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004184
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004185 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004186 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004187 if (chip->chipsize & 0xffffffff)
4188 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004189 else {
4190 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4191 chip->chip_shift += 32 - 1;
4192 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004193
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004194 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004195 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004196
Brian Norris8b6e50c2011-05-25 14:59:01 -07004197 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004198 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4199 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004200
Ezequiel Garcia20171642013-11-25 08:30:31 -03004201 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004202 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004203
4204 if (chip->onfi_version)
4205 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4206 chip->onfi_params.model);
4207 else if (chip->jedec_version)
4208 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4209 chip->jedec_params.model);
4210 else
4211 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4212 type->name);
4213
Rafał Miłecki3755a992014-10-21 00:01:04 +02004214 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004215 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004216 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004217 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004218}
4219
Boris Brezillond48f62b2016-04-01 14:54:32 +02004220static const char * const nand_ecc_modes[] = {
4221 [NAND_ECC_NONE] = "none",
4222 [NAND_ECC_SOFT] = "soft",
4223 [NAND_ECC_HW] = "hw",
4224 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4225 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004226};
4227
4228static int of_get_nand_ecc_mode(struct device_node *np)
4229{
4230 const char *pm;
4231 int err, i;
4232
4233 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4234 if (err < 0)
4235 return err;
4236
4237 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4238 if (!strcasecmp(pm, nand_ecc_modes[i]))
4239 return i;
4240
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004241 /*
4242 * For backward compatibility we support few obsoleted values that don't
4243 * have their mappings into nand_ecc_modes_t anymore (they were merged
4244 * with other enums).
4245 */
4246 if (!strcasecmp(pm, "soft_bch"))
4247 return NAND_ECC_SOFT;
4248
Boris Brezillond48f62b2016-04-01 14:54:32 +02004249 return -ENODEV;
4250}
4251
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004252static const char * const nand_ecc_algos[] = {
4253 [NAND_ECC_HAMMING] = "hamming",
4254 [NAND_ECC_BCH] = "bch",
4255};
4256
Boris Brezillond48f62b2016-04-01 14:54:32 +02004257static int of_get_nand_ecc_algo(struct device_node *np)
4258{
4259 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004260 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004261
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004262 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4263 if (!err) {
4264 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4265 if (!strcasecmp(pm, nand_ecc_algos[i]))
4266 return i;
4267 return -ENODEV;
4268 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004269
4270 /*
4271 * For backward compatibility we also read "nand-ecc-mode" checking
4272 * for some obsoleted values that were specifying ECC algorithm.
4273 */
4274 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4275 if (err < 0)
4276 return err;
4277
4278 if (!strcasecmp(pm, "soft"))
4279 return NAND_ECC_HAMMING;
4280 else if (!strcasecmp(pm, "soft_bch"))
4281 return NAND_ECC_BCH;
4282
4283 return -ENODEV;
4284}
4285
4286static int of_get_nand_ecc_step_size(struct device_node *np)
4287{
4288 int ret;
4289 u32 val;
4290
4291 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4292 return ret ? ret : val;
4293}
4294
4295static int of_get_nand_ecc_strength(struct device_node *np)
4296{
4297 int ret;
4298 u32 val;
4299
4300 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4301 return ret ? ret : val;
4302}
4303
4304static int of_get_nand_bus_width(struct device_node *np)
4305{
4306 u32 val;
4307
4308 if (of_property_read_u32(np, "nand-bus-width", &val))
4309 return 8;
4310
4311 switch (val) {
4312 case 8:
4313 case 16:
4314 return val;
4315 default:
4316 return -EIO;
4317 }
4318}
4319
4320static bool of_get_nand_on_flash_bbt(struct device_node *np)
4321{
4322 return of_property_read_bool(np, "nand-on-flash-bbt");
4323}
4324
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004325static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004326{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004327 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004328 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004329
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004330 if (!dn)
4331 return 0;
4332
Brian Norris5844fee2015-01-23 00:22:27 -08004333 if (of_get_nand_bus_width(dn) == 16)
4334 chip->options |= NAND_BUSWIDTH_16;
4335
4336 if (of_get_nand_on_flash_bbt(dn))
4337 chip->bbt_options |= NAND_BBT_USE_FLASH;
4338
4339 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004340 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004341 ecc_strength = of_get_nand_ecc_strength(dn);
4342 ecc_step = of_get_nand_ecc_step_size(dn);
4343
4344 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4345 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4346 pr_err("must set both strength and step size in DT\n");
4347 return -EINVAL;
4348 }
4349
4350 if (ecc_mode >= 0)
4351 chip->ecc.mode = ecc_mode;
4352
Rafał Miłecki79082452016-03-23 11:19:02 +01004353 if (ecc_algo >= 0)
4354 chip->ecc.algo = ecc_algo;
4355
Brian Norris5844fee2015-01-23 00:22:27 -08004356 if (ecc_strength >= 0)
4357 chip->ecc.strength = ecc_strength;
4358
4359 if (ecc_step > 0)
4360 chip->ecc.size = ecc_step;
4361
Boris Brezillonba78ee02016-06-08 17:04:22 +02004362 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4363 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4364
Brian Norris5844fee2015-01-23 00:22:27 -08004365 return 0;
4366}
4367
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004368/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004369 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004370 * @mtd: MTD device structure
4371 * @maxchips: number of chips to scan for
4372 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004373 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004374 * This is the first phase of the normal nand_scan() function. It reads the
4375 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004376 *
4377 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004378int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4379 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004380{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004381 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004382 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004383 int ret;
4384
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004385 ret = nand_dt_init(chip);
4386 if (ret)
4387 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004388
Brian Norrisf7a8e382016-01-05 10:39:45 -08004389 if (!mtd->name && mtd->dev.parent)
4390 mtd->name = dev_name(mtd->dev.parent);
4391
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004392 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4393 /*
4394 * Default functions assigned for chip_select() and
4395 * cmdfunc() both expect cmd_ctrl() to be populated,
4396 * so we need to check that that's the case
4397 */
4398 pr_err("chip.cmd_ctrl() callback is not provided");
4399 return -EINVAL;
4400 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004401 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004402 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004403
4404 /* Read the flash type */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004405 ret = nand_get_flash_type(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004406 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004407 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004408 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004409 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 }
4412
Boris Brezillon73f907f2016-10-24 16:46:20 +02004413 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004414 ret = nand_init_data_interface(chip);
4415 if (ret)
4416 return ret;
4417
Boris Brezillon73f907f2016-10-24 16:46:20 +02004418 /*
4419 * Setup the data interface correctly on the chip and controller side.
4420 * This explicit call to nand_setup_data_interface() is only required
4421 * for the first die, because nand_reset() has been called before
4422 * ->data_interface and ->default_onfi_timing_mode were set.
4423 * For the other dies, nand_reset() will automatically switch to the
4424 * best mode for us.
4425 */
4426 ret = nand_setup_data_interface(chip);
4427 if (ret)
4428 return ret;
4429
Boris Brezillon7f501f02016-05-24 19:20:05 +02004430 nand_maf_id = chip->id.data[0];
4431 nand_dev_id = chip->id.data[1];
4432
Huang Shijie07300162012-11-09 16:23:45 +08004433 chip->select_chip(mtd, -1);
4434
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004435 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004436 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004437 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004438 nand_reset(chip, i);
4439
4440 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004442 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004444 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004445 nand_dev_id != chip->read_byte(mtd)) {
4446 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 break;
Huang Shijie07300162012-11-09 16:23:45 +08004448 }
4449 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 }
4451 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004452 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004455 chip->numchips = i;
4456 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
David Woodhouse3b85c322006-09-25 17:06:53 +01004458 return 0;
4459}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004460EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004461
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004462static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4463{
4464 struct nand_chip *chip = mtd_to_nand(mtd);
4465 struct nand_ecc_ctrl *ecc = &chip->ecc;
4466
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004467 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004468 return -EINVAL;
4469
4470 switch (ecc->algo) {
4471 case NAND_ECC_HAMMING:
4472 ecc->calculate = nand_calculate_ecc;
4473 ecc->correct = nand_correct_data;
4474 ecc->read_page = nand_read_page_swecc;
4475 ecc->read_subpage = nand_read_subpage;
4476 ecc->write_page = nand_write_page_swecc;
4477 ecc->read_page_raw = nand_read_page_raw;
4478 ecc->write_page_raw = nand_write_page_raw;
4479 ecc->read_oob = nand_read_oob_std;
4480 ecc->write_oob = nand_write_oob_std;
4481 if (!ecc->size)
4482 ecc->size = 256;
4483 ecc->bytes = 3;
4484 ecc->strength = 1;
4485 return 0;
4486 case NAND_ECC_BCH:
4487 if (!mtd_nand_has_bch()) {
4488 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4489 return -EINVAL;
4490 }
4491 ecc->calculate = nand_bch_calculate_ecc;
4492 ecc->correct = nand_bch_correct_data;
4493 ecc->read_page = nand_read_page_swecc;
4494 ecc->read_subpage = nand_read_subpage;
4495 ecc->write_page = nand_write_page_swecc;
4496 ecc->read_page_raw = nand_read_page_raw;
4497 ecc->write_page_raw = nand_write_page_raw;
4498 ecc->read_oob = nand_read_oob_std;
4499 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004500
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004501 /*
4502 * Board driver should supply ecc.size and ecc.strength
4503 * values to select how many bits are correctable.
4504 * Otherwise, default to 4 bits for large page devices.
4505 */
4506 if (!ecc->size && (mtd->oobsize >= 64)) {
4507 ecc->size = 512;
4508 ecc->strength = 4;
4509 }
4510
4511 /*
4512 * if no ecc placement scheme was provided pickup the default
4513 * large page one.
4514 */
4515 if (!mtd->ooblayout) {
4516 /* handle large page devices only */
4517 if (mtd->oobsize < 64) {
4518 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4519 return -EINVAL;
4520 }
4521
4522 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004523
4524 }
4525
4526 /*
4527 * We can only maximize ECC config when the default layout is
4528 * used, otherwise we don't know how many bytes can really be
4529 * used.
4530 */
4531 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4532 ecc->options & NAND_ECC_MAXIMIZE) {
4533 int steps, bytes;
4534
4535 /* Always prefer 1k blocks over 512bytes ones */
4536 ecc->size = 1024;
4537 steps = mtd->writesize / ecc->size;
4538
4539 /* Reserve 2 bytes for the BBM */
4540 bytes = (mtd->oobsize - 2) / steps;
4541 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004542 }
4543
4544 /* See nand_bch_init() for details. */
4545 ecc->bytes = 0;
4546 ecc->priv = nand_bch_init(mtd);
4547 if (!ecc->priv) {
4548 WARN(1, "BCH ECC initialization failed!\n");
4549 return -EINVAL;
4550 }
4551 return 0;
4552 default:
4553 WARN(1, "Unsupported ECC algorithm!\n");
4554 return -EINVAL;
4555 }
4556}
4557
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004558/*
4559 * Check if the chip configuration meet the datasheet requirements.
4560
4561 * If our configuration corrects A bits per B bytes and the minimum
4562 * required correction level is X bits per Y bytes, then we must ensure
4563 * both of the following are true:
4564 *
4565 * (1) A / B >= X / Y
4566 * (2) A >= X
4567 *
4568 * Requirement (1) ensures we can correct for the required bitflip density.
4569 * Requirement (2) ensures we can correct even when all bitflips are clumped
4570 * in the same sector.
4571 */
4572static bool nand_ecc_strength_good(struct mtd_info *mtd)
4573{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004574 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004575 struct nand_ecc_ctrl *ecc = &chip->ecc;
4576 int corr, ds_corr;
4577
4578 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4579 /* Not enough information */
4580 return true;
4581
4582 /*
4583 * We get the number of corrected bits per page to compare
4584 * the correction density.
4585 */
4586 corr = (mtd->writesize * ecc->strength) / ecc->size;
4587 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4588
4589 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4590}
David Woodhouse3b85c322006-09-25 17:06:53 +01004591
Marc Gonzalez3371d662016-11-15 10:56:20 +01004592static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4593{
4594 struct nand_ecc_ctrl *ecc = &chip->ecc;
4595
4596 if (nand_standard_page_accessors(ecc))
4597 return false;
4598
4599 /*
4600 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4601 * controller driver implements all the page accessors because
4602 * default helpers are not suitable when the core does not
4603 * send the READ0/PAGEPROG commands.
4604 */
4605 return (!ecc->read_page || !ecc->write_page ||
4606 !ecc->read_page_raw || !ecc->write_page_raw ||
4607 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4608 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4609 ecc->hwctl && ecc->calculate));
4610}
4611
David Woodhouse3b85c322006-09-25 17:06:53 +01004612/**
4613 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004614 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004615 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004616 * This is the second phase of the normal nand_scan() function. It fills out
4617 * all the uninitialized function pointers with the defaults and scans for a
4618 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004619 */
4620int nand_scan_tail(struct mtd_info *mtd)
4621{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004622 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004623 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004624 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004625 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004626
Brian Norrise2414f42012-02-06 13:44:00 -08004627 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004628 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4629 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4630 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004631
Marc Gonzalez3371d662016-11-15 10:56:20 +01004632 if (invalid_ecc_page_accessors(chip)) {
4633 pr_err("Invalid ECC page accessors setup\n");
4634 return -EINVAL;
4635 }
4636
Huang Shijief02ea4e2014-01-13 14:27:12 +08004637 if (!(chip->options & NAND_OWN_BUFFERS)) {
4638 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4639 + mtd->oobsize * 3, GFP_KERNEL);
4640 if (!nbuf)
4641 return -ENOMEM;
4642 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4643 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4644 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4645
4646 chip->buffers = nbuf;
4647 } else {
4648 if (!chip->buffers)
4649 return -ENOMEM;
4650 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004651
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004652 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004653 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004654
4655 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004656 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004657 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004658 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004659 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004660 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004663 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 break;
4665 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004666 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004667 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004668 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004670 WARN(1, "No oob scheme defined for oobsize %d\n",
4671 mtd->oobsize);
4672 ret = -EINVAL;
4673 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004676
David Woodhouse956e9442006-09-25 17:12:39 +01004677 if (!chip->write_page)
4678 chip->write_page = nand_write_page;
4679
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004680 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004681 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004682 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004683 */
David Woodhouse956e9442006-09-25 17:12:39 +01004684
Huang Shijie97de79e02013-10-18 14:20:53 +08004685 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004686 case NAND_ECC_HW_OOB_FIRST:
4687 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004688 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004689 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4690 ret = -EINVAL;
4691 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004692 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004693 if (!ecc->read_page)
4694 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004695
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004696 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004697 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004698 if (!ecc->read_page)
4699 ecc->read_page = nand_read_page_hwecc;
4700 if (!ecc->write_page)
4701 ecc->write_page = nand_write_page_hwecc;
4702 if (!ecc->read_page_raw)
4703 ecc->read_page_raw = nand_read_page_raw;
4704 if (!ecc->write_page_raw)
4705 ecc->write_page_raw = nand_write_page_raw;
4706 if (!ecc->read_oob)
4707 ecc->read_oob = nand_read_oob_std;
4708 if (!ecc->write_oob)
4709 ecc->write_oob = nand_write_oob_std;
4710 if (!ecc->read_subpage)
4711 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004712 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004713 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004714
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004715 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004716 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4717 (!ecc->read_page ||
4718 ecc->read_page == nand_read_page_hwecc ||
4719 !ecc->write_page ||
4720 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004721 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4722 ret = -EINVAL;
4723 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004724 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004725 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004726 if (!ecc->read_page)
4727 ecc->read_page = nand_read_page_syndrome;
4728 if (!ecc->write_page)
4729 ecc->write_page = nand_write_page_syndrome;
4730 if (!ecc->read_page_raw)
4731 ecc->read_page_raw = nand_read_page_raw_syndrome;
4732 if (!ecc->write_page_raw)
4733 ecc->write_page_raw = nand_write_page_raw_syndrome;
4734 if (!ecc->read_oob)
4735 ecc->read_oob = nand_read_oob_syndrome;
4736 if (!ecc->write_oob)
4737 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004738
Huang Shijie97de79e02013-10-18 14:20:53 +08004739 if (mtd->writesize >= ecc->size) {
4740 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004741 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4742 ret = -EINVAL;
4743 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004744 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004745 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004746 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004747 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4748 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004749 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004750 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004752 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004753 ret = nand_set_ecc_soft_ops(mtd);
4754 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004755 ret = -EINVAL;
4756 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004757 }
4758 break;
4759
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004760 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004761 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004762 ecc->read_page = nand_read_page_raw;
4763 ecc->write_page = nand_write_page_raw;
4764 ecc->read_oob = nand_read_oob_std;
4765 ecc->read_page_raw = nand_read_page_raw;
4766 ecc->write_page_raw = nand_write_page_raw;
4767 ecc->write_oob = nand_write_oob_std;
4768 ecc->size = mtd->writesize;
4769 ecc->bytes = 0;
4770 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004772
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004774 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4775 ret = -EINVAL;
4776 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
Brian Norris9ce244b2011-08-30 18:45:37 -07004779 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004780 if (!ecc->read_oob_raw)
4781 ecc->read_oob_raw = ecc->read_oob;
4782 if (!ecc->write_oob_raw)
4783 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004784
Boris Brezillon846031d2016-02-03 20:11:00 +01004785 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004786 mtd->ecc_strength = ecc->strength;
4787 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004788
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004789 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004790 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004791 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004792 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004793 ecc->steps = mtd->writesize / ecc->size;
4794 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004795 WARN(1, "Invalid ECC parameters\n");
4796 ret = -EINVAL;
4797 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004799 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004800
Boris Brezillon846031d2016-02-03 20:11:00 +01004801 /*
4802 * The number of bytes available for a client to place data into
4803 * the out of band area.
4804 */
4805 ret = mtd_ooblayout_count_freebytes(mtd);
4806 if (ret < 0)
4807 ret = 0;
4808
4809 mtd->oobavail = ret;
4810
4811 /* ECC sanity check: warn if it's too weak */
4812 if (!nand_ecc_strength_good(mtd))
4813 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4814 mtd->name);
4815
Brian Norris8b6e50c2011-05-25 14:59:01 -07004816 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004817 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004818 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004819 case 2:
4820 mtd->subpage_sft = 1;
4821 break;
4822 case 4:
4823 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004824 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004825 mtd->subpage_sft = 2;
4826 break;
4827 }
4828 }
4829 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4830
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004831 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004832 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004835 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004837 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304838 switch (ecc->mode) {
4839 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304840 if (chip->page_shift > 9)
4841 chip->options |= NAND_SUBPAGE_READ;
4842 break;
4843
4844 default:
4845 break;
4846 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004849 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004850 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4851 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004852 mtd->_erase = nand_erase;
4853 mtd->_point = NULL;
4854 mtd->_unpoint = NULL;
4855 mtd->_read = nand_read;
4856 mtd->_write = nand_write;
4857 mtd->_panic_write = panic_nand_write;
4858 mtd->_read_oob = nand_read_oob;
4859 mtd->_write_oob = nand_write_oob;
4860 mtd->_sync = nand_sync;
4861 mtd->_lock = NULL;
4862 mtd->_unlock = NULL;
4863 mtd->_suspend = nand_suspend;
4864 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004865 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004866 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004867 mtd->_block_isbad = nand_block_isbad;
4868 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004869 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004870 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004872 /*
4873 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4874 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4875 * properly set.
4876 */
4877 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004878 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004880 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004881 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
4884 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004885 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004886err_free:
4887 if (!(chip->options & NAND_OWN_BUFFERS))
4888 kfree(chip->buffers);
4889 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004891EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
Brian Norris8b6e50c2011-05-25 14:59:01 -07004893/*
4894 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004895 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004896 * to call us from in-kernel code if the core NAND support is modular.
4897 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004898#ifdef MODULE
4899#define caller_is_module() (1)
4900#else
4901#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004902 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004903#endif
4904
4905/**
4906 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004907 * @mtd: MTD device structure
4908 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004909 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004910 * This fills out all the uninitialized function pointers with the defaults.
4911 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004912 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004913 */
4914int nand_scan(struct mtd_info *mtd, int maxchips)
4915{
4916 int ret;
4917
David Woodhouse5e81e882010-02-26 18:32:56 +00004918 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004919 if (!ret)
4920 ret = nand_scan_tail(mtd);
4921 return ret;
4922}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004923EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004926 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4927 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004928 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004929void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004931 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004932 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004933 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4934
Boris Brezillond8e725d2016-09-15 10:32:50 +02004935 nand_release_data_interface(chip);
4936
Jesper Juhlfa671642005-11-07 01:01:27 -08004937 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004938 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004939 if (!(chip->options & NAND_OWN_BUFFERS))
4940 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004941
4942 /* Free bad block descriptor memory */
4943 if (chip->badblock_pattern && chip->badblock_pattern->options
4944 & NAND_BBT_DYNAMICSTRUCT)
4945 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004947EXPORT_SYMBOL_GPL(nand_cleanup);
4948
4949/**
4950 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4951 * held by the NAND device
4952 * @mtd: MTD device structure
4953 */
4954void nand_release(struct mtd_info *mtd)
4955{
4956 mtd_device_unregister(mtd);
4957 nand_cleanup(mtd_to_nand(mtd));
4958}
David Woodhousee0c7d762006-05-13 18:07:53 +01004959EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004960
David Woodhousee0c7d762006-05-13 18:07:53 +01004961MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004962MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4963MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004964MODULE_DESCRIPTION("Generic NAND flash driver code");