blob: afbb80f4375a6b277f4f301edb855a9ab061e559 [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{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900357 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100358 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900359 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
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;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900365 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100366
Masahiro Yamadac120e752017-03-23 05:07:01 +0900367 for (; page < page_end; page++) {
368 res = chip->ecc.read_oob(mtd, chip, page);
369 if (res)
370 return res;
371
372 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000373
Brian Norriscdbec052012-01-13 18:11:48 -0800374 if (likely(chip->badblockbits == 8))
375 res = bad != 0xFF;
376 else
377 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900378 if (res)
379 return res;
380 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200381
Masahiro Yamadac120e752017-03-23 05:07:01 +0900382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700386 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700387 * @mtd: MTD device structure
388 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700390 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700391 * specific driver. It provides the details for writing a bad block marker to a
392 * block.
393 */
394static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
395{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100396 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700397 struct mtd_oob_ops ops;
398 uint8_t buf[2] = { 0, 0 };
399 int ret = 0, res, i = 0;
400
Brian Norris0ec56dc2015-02-28 02:02:30 -0800401 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700402 ops.oobbuf = buf;
403 ops.ooboffs = chip->badblockpos;
404 if (chip->options & NAND_BUSWIDTH_16) {
405 ops.ooboffs &= ~0x01;
406 ops.len = ops.ooblen = 2;
407 } else {
408 ops.len = ops.ooblen = 1;
409 }
410 ops.mode = MTD_OPS_PLACE_OOB;
411
412 /* Write to first/last page(s) if necessary */
413 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
414 ofs += mtd->erasesize - mtd->writesize;
415 do {
416 res = nand_do_write_oob(mtd, ofs, &ops);
417 if (!ret)
418 ret = res;
419
420 i++;
421 ofs += mtd->writesize;
422 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
423
424 return ret;
425}
426
427/**
428 * nand_block_markbad_lowlevel - mark a block bad
429 * @mtd: MTD device structure
430 * @ofs: offset from device start
431 *
432 * This function performs the generic NAND bad block marking steps (i.e., bad
433 * block table(s) and/or marker(s)). We only allow the hardware driver to
434 * specify how to write bad block markers to OOB (chip->block_markbad).
435 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700436 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800437 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700438 * (2) write bad block marker to OOB area of affected block (unless flag
439 * NAND_BBT_NO_OOB_BBM is present)
440 * (3) update the BBT
441 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800442 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700444static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100446 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700447 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000448
Brian Norrisb32843b2013-07-30 17:52:59 -0700449 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800450 struct erase_info einfo;
451
452 /* Attempt erase before marking OOB */
453 memset(&einfo, 0, sizeof(einfo));
454 einfo.mtd = mtd;
455 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300456 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800457 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800458
Brian Norrisb32843b2013-07-30 17:52:59 -0700459 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800460 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700461 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300462 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200463 }
Brian Norrise2414f42012-02-06 13:44:00 -0800464
Brian Norrisb32843b2013-07-30 17:52:59 -0700465 /* Mark block bad in BBT */
466 if (chip->bbt) {
467 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800468 if (!ret)
469 ret = res;
470 }
471
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200472 if (!ret)
473 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300474
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000478/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700480 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700482 * Check, if the device is write protected. The function expects, that the
483 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100485static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100487 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200488
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200490 if (chip->options & NAND_BROKEN_XD)
491 return 0;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200494 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
495 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800499 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700500 * @mtd: MTD device structure
501 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300502 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800503 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300504 */
505static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
506{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100507 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300508
509 if (!chip->bbt)
510 return 0;
511 /* Return info from the table */
512 return nand_isreserved_bbt(mtd, ofs);
513}
514
515/**
516 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
517 * @mtd: MTD device structure
518 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700519 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 *
521 * Check, if the block is bad. Either by reading the bad block table or
522 * calling of the scan function.
523 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530524static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100526 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000527
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200528 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530529 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100532 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200535/**
536 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700537 * @mtd: MTD device structure
538 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200539 *
540 * Helper function for nand_wait_ready used when needing to wait in interrupt
541 * context.
542 */
543static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
544{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100545 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200546 int i;
547
548 /* Wait for the device to get ready */
549 for (i = 0; i < timeo; i++) {
550 if (chip->dev_ready(mtd))
551 break;
552 touch_softlockup_watchdog();
553 mdelay(1);
554 }
555}
556
Alex Smithb70af9b2015-10-06 14:52:07 +0100557/**
558 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
559 * @mtd: MTD device structure
560 *
561 * Wait for the ready pin after a command, and warn if a timeout occurs.
562 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100563void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000564{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100565 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100566 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000567
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200568 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100569 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200570
Brian Norris7854d3f2011-06-23 14:12:08 -0700571 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100572 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000573 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200574 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300575 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100576 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000577 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100578
Brian Norris9ebfdf52016-03-04 17:19:23 -0800579 if (!chip->dev_ready(mtd))
580 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000581}
David Woodhouse4b648b02006-09-25 17:05:24 +0100582EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200585 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
586 * @mtd: MTD device structure
587 * @timeo: Timeout in ms
588 *
589 * Wait for status ready (i.e. command done) or timeout.
590 */
591static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
592{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100593 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200594
595 timeo = jiffies + msecs_to_jiffies(timeo);
596 do {
597 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
598 break;
599 touch_softlockup_watchdog();
600 } while (time_before(jiffies, timeo));
601};
602
603/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700605 * @mtd: MTD device structure
606 * @command: the command to be sent
607 * @column: the column address for this command, -1 if none
608 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700610 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200611 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200613static void nand_command(struct mtd_info *mtd, unsigned int command,
614 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100616 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200617 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Brian Norris8b6e50c2011-05-25 14:59:01 -0700619 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (command == NAND_CMD_SEQIN) {
621 int readcmd;
622
Joern Engel28318772006-05-22 23:18:05 +0200623 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200625 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 readcmd = NAND_CMD_READOOB;
627 } else if (column < 256) {
628 /* First 256 bytes --> READ0 */
629 readcmd = NAND_CMD_READ0;
630 } else {
631 column -= 256;
632 readcmd = NAND_CMD_READ1;
633 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200634 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200635 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200637 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Brian Norris8b6e50c2011-05-25 14:59:01 -0700639 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200640 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
641 /* Serially input address */
642 if (column != -1) {
643 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800644 if (chip->options & NAND_BUSWIDTH_16 &&
645 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200646 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200647 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200648 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200650 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200651 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200652 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200653 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200654 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->chipsize > (32 << 20))
656 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200657 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200658 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000659
660 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700661 * Program and erase have their own busy handlers status and sequential
662 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100663 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 case NAND_CMD_PAGEPROG:
667 case NAND_CMD_ERASE1:
668 case NAND_CMD_ERASE2:
669 case NAND_CMD_SEQIN:
670 case NAND_CMD_STATUS:
671 return;
672
673 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200674 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200676 udelay(chip->chip_delay);
677 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200678 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200679 chip->cmd_ctrl(mtd,
680 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200681 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
682 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return;
684
David Woodhousee0c7d762006-05-13 18:07:53 +0100685 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000687 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * If we don't have access to the busy pin, we apply the given
689 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100690 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200691 if (!chip->dev_ready) {
692 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700696 /*
697 * Apply this short delay always to ensure that we do wait tWB in
698 * any case on any machine.
699 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100700 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000701
702 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200705static void nand_ccs_delay(struct nand_chip *chip)
706{
707 /*
708 * The controller already takes care of waiting for tCCS when the RNDIN
709 * or RNDOUT command is sent, return directly.
710 */
711 if (!(chip->options & NAND_WAIT_TCCS))
712 return;
713
714 /*
715 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
716 * (which should be safe for all NANDs).
717 */
718 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
719 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
720 else
721 ndelay(500);
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/**
725 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700726 * @mtd: MTD device structure
727 * @command: the command to be sent
728 * @column: the column address for this command, -1 if none
729 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200731 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700732 * devices. We don't have the separate regions as we have in the small page
733 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200735static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
736 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100738 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* Emulate NAND_CMD_READOOB */
741 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200742 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 command = NAND_CMD_READ0;
744 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000745
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200746 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400747 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200750 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* Serially input address */
753 if (column != -1) {
754 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800755 if (chip->options & NAND_BUSWIDTH_16 &&
756 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200758 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200759 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200760
Brian Norrisf5b88de2016-10-03 09:49:35 -0700761 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200762 if (!nand_opcode_8bits(command))
763 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200766 chip->cmd_ctrl(mtd, page_addr, ctrl);
767 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200768 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200770 if (chip->chipsize > (128 << 20))
771 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200772 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200775 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000776
777 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700778 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100779 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 case NAND_CMD_CACHEDPROG:
784 case NAND_CMD_PAGEPROG:
785 case NAND_CMD_ERASE1:
786 case NAND_CMD_ERASE2:
787 case NAND_CMD_SEQIN:
788 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000789 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200791 case NAND_CMD_RNDIN:
792 nand_ccs_delay(chip);
793 return;
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200796 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200798 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200799 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
800 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
801 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
802 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200803 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
804 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return;
806
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200807 case NAND_CMD_RNDOUT:
808 /* No ready / busy check necessary */
809 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
810 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
811 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
812 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200813
814 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200815 return;
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200818 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
819 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
820 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
821 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000822
David Woodhousee0c7d762006-05-13 18:07:53 +0100823 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000825 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700827 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100828 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200829 if (!chip->dev_ready) {
830 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000834
Brian Norris8b6e50c2011-05-25 14:59:01 -0700835 /*
836 * Apply this short delay always to ensure that we do wait tWB in
837 * any case on any machine.
838 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100839 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000840
841 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200845 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700846 * @chip: the nand chip descriptor
847 * @mtd: MTD device structure
848 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200849 *
850 * Used when in panic, no locks are taken.
851 */
852static void panic_nand_get_device(struct nand_chip *chip,
853 struct mtd_info *mtd, int new_state)
854{
Brian Norris7854d3f2011-06-23 14:12:08 -0700855 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200856 chip->controller->active = chip;
857 chip->state = new_state;
858}
859
860/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700862 * @mtd: MTD device structure
863 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
865 * Get the device and lock it for exclusive access
866 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200867static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800868nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100870 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200871 spinlock_t *lock = &chip->controller->lock;
872 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100873 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200874retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100875 spin_lock(lock);
876
vimal singhb8b3ee92009-07-09 20:41:22 +0530877 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200878 if (!chip->controller->active)
879 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200880
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200881 if (chip->controller->active == chip && chip->state == FL_READY) {
882 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100883 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100884 return 0;
885 }
886 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800887 if (chip->controller->active->state == FL_PM_SUSPENDED) {
888 chip->state = FL_PM_SUSPENDED;
889 spin_unlock(lock);
890 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800891 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100892 }
893 set_current_state(TASK_UNINTERRUPTIBLE);
894 add_wait_queue(wq, &wait);
895 spin_unlock(lock);
896 schedule();
897 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto retry;
899}
900
901/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700902 * panic_nand_wait - [GENERIC] wait until the command is done
903 * @mtd: MTD device structure
904 * @chip: NAND chip structure
905 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200906 *
907 * Wait for command done. This is a helper function for nand_wait used when
908 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400909 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200910 */
911static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
912 unsigned long timeo)
913{
914 int i;
915 for (i = 0; i < timeo; i++) {
916 if (chip->dev_ready) {
917 if (chip->dev_ready(mtd))
918 break;
919 } else {
920 if (chip->read_byte(mtd) & NAND_STATUS_READY)
921 break;
922 }
923 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200924 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200925}
926
927/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700928 * nand_wait - [DEFAULT] wait until the command is done
929 * @mtd: MTD device structure
930 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100932 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700933 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200934static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936
Alex Smithb70af9b2015-10-06 14:52:07 +0100937 int status;
938 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 /*
941 * Apply this short delay always to ensure that we do wait tWB in any
942 * case on any machine.
943 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100944 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200946 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200948 if (in_interrupt() || oops_in_progress)
949 panic_nand_wait(mtd, chip, timeo);
950 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800951 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100952 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200953 if (chip->dev_ready) {
954 if (chip->dev_ready(mtd))
955 break;
956 } else {
957 if (chip->read_byte(mtd) & NAND_STATUS_READY)
958 break;
959 }
960 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100961 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800963
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200964 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100965 /* This can happen if in case of timeout or buggy dev_ready */
966 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return status;
968}
969
970/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200971 * nand_reset_data_interface - Reset data interface and timings
972 * @chip: The NAND chip
973 *
974 * Reset the Data interface and timings to ONFI mode 0.
975 *
976 * Returns 0 for success or negative error code otherwise.
977 */
978static int nand_reset_data_interface(struct nand_chip *chip)
979{
980 struct mtd_info *mtd = nand_to_mtd(chip);
981 const struct nand_data_interface *conf;
982 int ret;
983
984 if (!chip->setup_data_interface)
985 return 0;
986
987 /*
988 * The ONFI specification says:
989 * "
990 * To transition from NV-DDR or NV-DDR2 to the SDR data
991 * interface, the host shall use the Reset (FFh) command
992 * using SDR timing mode 0. A device in any timing mode is
993 * required to recognize Reset (FFh) command issued in SDR
994 * timing mode 0.
995 * "
996 *
997 * Configure the data interface in SDR mode and set the
998 * timings to timing mode 0.
999 */
1000
1001 conf = nand_get_default_data_interface();
1002 ret = chip->setup_data_interface(mtd, conf, false);
1003 if (ret)
1004 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1005
1006 return ret;
1007}
1008
1009/**
1010 * nand_setup_data_interface - Setup the best data interface and timings
1011 * @chip: The NAND chip
1012 *
1013 * Find and configure the best data interface and NAND timings supported by
1014 * the chip and the driver.
1015 * First tries to retrieve supported timing modes from ONFI information,
1016 * and if the NAND chip does not support ONFI, relies on the
1017 * ->onfi_timing_mode_default specified in the nand_ids table.
1018 *
1019 * Returns 0 for success or negative error code otherwise.
1020 */
1021static int nand_setup_data_interface(struct nand_chip *chip)
1022{
1023 struct mtd_info *mtd = nand_to_mtd(chip);
1024 int ret;
1025
1026 if (!chip->setup_data_interface || !chip->data_interface)
1027 return 0;
1028
1029 /*
1030 * Ensure the timing mode has been changed on the chip side
1031 * before changing timings on the controller side.
1032 */
1033 if (chip->onfi_version) {
1034 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1035 chip->onfi_timing_mode_default,
1036 };
1037
1038 ret = chip->onfi_set_features(mtd, chip,
1039 ONFI_FEATURE_ADDR_TIMING_MODE,
1040 tmode_param);
1041 if (ret)
1042 goto err;
1043 }
1044
1045 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1046err:
1047 return ret;
1048}
1049
1050/**
1051 * nand_init_data_interface - find the best data interface and timings
1052 * @chip: The NAND chip
1053 *
1054 * Find the best data interface and NAND timings supported by the chip
1055 * and the driver.
1056 * First tries to retrieve supported timing modes from ONFI information,
1057 * and if the NAND chip does not support ONFI, relies on the
1058 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1059 * function nand_chip->data_interface is initialized with the best timing mode
1060 * available.
1061 *
1062 * Returns 0 for success or negative error code otherwise.
1063 */
1064static int nand_init_data_interface(struct nand_chip *chip)
1065{
1066 struct mtd_info *mtd = nand_to_mtd(chip);
1067 int modes, mode, ret;
1068
1069 if (!chip->setup_data_interface)
1070 return 0;
1071
1072 /*
1073 * First try to identify the best timings from ONFI parameters and
1074 * if the NAND does not support ONFI, fallback to the default ONFI
1075 * timing mode.
1076 */
1077 modes = onfi_get_async_timing_mode(chip);
1078 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1079 if (!chip->onfi_timing_mode_default)
1080 return 0;
1081
1082 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1083 }
1084
1085 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1086 GFP_KERNEL);
1087 if (!chip->data_interface)
1088 return -ENOMEM;
1089
1090 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1091 ret = onfi_init_data_interface(chip, chip->data_interface,
1092 NAND_SDR_IFACE, mode);
1093 if (ret)
1094 continue;
1095
1096 ret = chip->setup_data_interface(mtd, chip->data_interface,
1097 true);
1098 if (!ret) {
1099 chip->onfi_timing_mode_default = mode;
1100 break;
1101 }
1102 }
1103
1104 return 0;
1105}
1106
1107static void nand_release_data_interface(struct nand_chip *chip)
1108{
1109 kfree(chip->data_interface);
1110}
1111
1112/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001113 * nand_reset - Reset and initialize a NAND device
1114 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001115 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001116 *
1117 * Returns 0 for success or negative error code otherwise
1118 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001119int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001120{
1121 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001122 int ret;
1123
1124 ret = nand_reset_data_interface(chip);
1125 if (ret)
1126 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001127
Boris Brezillon73f907f2016-10-24 16:46:20 +02001128 /*
1129 * The CS line has to be released before we can apply the new NAND
1130 * interface settings, hence this weird ->select_chip() dance.
1131 */
1132 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001133 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001134 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001135
Boris Brezillon73f907f2016-10-24 16:46:20 +02001136 chip->select_chip(mtd, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001137 ret = nand_setup_data_interface(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001138 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001139 if (ret)
1140 return ret;
1141
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001142 return 0;
1143}
1144
1145/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001146 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001147 * @mtd: mtd info
1148 * @ofs: offset to start unlock from
1149 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001150 * @invert: when = 0, unlock the range of blocks within the lower and
1151 * upper boundary address
1152 * when = 1, unlock the range of blocks outside the boundaries
1153 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301154 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001155 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301156 */
1157static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1158 uint64_t len, int invert)
1159{
1160 int ret = 0;
1161 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001162 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301163
1164 /* Submit address of first page to unlock */
1165 page = ofs >> chip->page_shift;
1166 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1167
1168 /* Submit address of last page to unlock */
1169 page = (ofs + len) >> chip->page_shift;
1170 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1171 (page | invert) & chip->pagemask);
1172
1173 /* Call wait ready function */
1174 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301175 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001176 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001177 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301178 __func__, status);
1179 ret = -EIO;
1180 }
1181
1182 return ret;
1183}
1184
1185/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001186 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001187 * @mtd: mtd info
1188 * @ofs: offset to start unlock from
1189 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301190 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001191 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301192 */
1193int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1194{
1195 int ret = 0;
1196 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001197 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301198
Brian Norris289c0522011-07-19 10:06:09 -07001199 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301200 __func__, (unsigned long long)ofs, len);
1201
1202 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001203 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301204
1205 /* Align to last block address if size addresses end of the device */
1206 if (ofs + len == mtd->size)
1207 len -= mtd->erasesize;
1208
Huang Shijie6a8214a2012-11-19 14:43:30 +08001209 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301210
1211 /* Shift to get chip number */
1212 chipnr = ofs >> chip->chip_shift;
1213
White Ding57d3a9a2014-07-24 00:10:45 +08001214 /*
1215 * Reset the chip.
1216 * If we want to check the WP through READ STATUS and check the bit 7
1217 * we must reset the chip
1218 * some operation can also clear the bit 7 of status register
1219 * eg. erase/program a locked block
1220 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001221 nand_reset(chip, chipnr);
1222
1223 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001224
Vimal Singh7d70f332010-02-08 15:50:49 +05301225 /* Check, if it is write protected */
1226 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001227 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301228 __func__);
1229 ret = -EIO;
1230 goto out;
1231 }
1232
1233 ret = __nand_unlock(mtd, ofs, len, 0);
1234
1235out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001236 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301237 nand_release_device(mtd);
1238
1239 return ret;
1240}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001241EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301242
1243/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001244 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001245 * @mtd: mtd info
1246 * @ofs: offset to start unlock from
1247 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301248 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001249 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1250 * have this feature, but it allows only to lock all blocks, not for specified
1251 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1252 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301253 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001254 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301255 */
1256int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1257{
1258 int ret = 0;
1259 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001260 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301261
Brian Norris289c0522011-07-19 10:06:09 -07001262 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301263 __func__, (unsigned long long)ofs, len);
1264
1265 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001266 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301267
Huang Shijie6a8214a2012-11-19 14:43:30 +08001268 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301269
1270 /* Shift to get chip number */
1271 chipnr = ofs >> chip->chip_shift;
1272
White Ding57d3a9a2014-07-24 00:10:45 +08001273 /*
1274 * Reset the chip.
1275 * If we want to check the WP through READ STATUS and check the bit 7
1276 * we must reset the chip
1277 * some operation can also clear the bit 7 of status register
1278 * eg. erase/program a locked block
1279 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001280 nand_reset(chip, chipnr);
1281
1282 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001283
Vimal Singh7d70f332010-02-08 15:50:49 +05301284 /* Check, if it is write protected */
1285 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001286 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301287 __func__);
1288 status = MTD_ERASE_FAILED;
1289 ret = -EIO;
1290 goto out;
1291 }
1292
1293 /* Submit address of first page to lock */
1294 page = ofs >> chip->page_shift;
1295 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1296
1297 /* Call wait ready function */
1298 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301299 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001300 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001301 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301302 __func__, status);
1303 ret = -EIO;
1304 goto out;
1305 }
1306
1307 ret = __nand_unlock(mtd, ofs, len, 0x1);
1308
1309out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001310 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301311 nand_release_device(mtd);
1312
1313 return ret;
1314}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001315EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301316
1317/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001318 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1319 * @buf: buffer to test
1320 * @len: buffer length
1321 * @bitflips_threshold: maximum number of bitflips
1322 *
1323 * Check if a buffer contains only 0xff, which means the underlying region
1324 * has been erased and is ready to be programmed.
1325 * The bitflips_threshold specify the maximum number of bitflips before
1326 * considering the region is not erased.
1327 * Note: The logic of this function has been extracted from the memweight
1328 * implementation, except that nand_check_erased_buf function exit before
1329 * testing the whole buffer if the number of bitflips exceed the
1330 * bitflips_threshold value.
1331 *
1332 * Returns a positive number of bitflips less than or equal to
1333 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1334 * threshold.
1335 */
1336static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1337{
1338 const unsigned char *bitmap = buf;
1339 int bitflips = 0;
1340 int weight;
1341
1342 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1343 len--, bitmap++) {
1344 weight = hweight8(*bitmap);
1345 bitflips += BITS_PER_BYTE - weight;
1346 if (unlikely(bitflips > bitflips_threshold))
1347 return -EBADMSG;
1348 }
1349
1350 for (; len >= sizeof(long);
1351 len -= sizeof(long), bitmap += sizeof(long)) {
1352 weight = hweight_long(*((unsigned long *)bitmap));
1353 bitflips += BITS_PER_LONG - weight;
1354 if (unlikely(bitflips > bitflips_threshold))
1355 return -EBADMSG;
1356 }
1357
1358 for (; len > 0; len--, bitmap++) {
1359 weight = hweight8(*bitmap);
1360 bitflips += BITS_PER_BYTE - weight;
1361 if (unlikely(bitflips > bitflips_threshold))
1362 return -EBADMSG;
1363 }
1364
1365 return bitflips;
1366}
1367
1368/**
1369 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1370 * 0xff data
1371 * @data: data buffer to test
1372 * @datalen: data length
1373 * @ecc: ECC buffer
1374 * @ecclen: ECC length
1375 * @extraoob: extra OOB buffer
1376 * @extraooblen: extra OOB length
1377 * @bitflips_threshold: maximum number of bitflips
1378 *
1379 * Check if a data buffer and its associated ECC and OOB data contains only
1380 * 0xff pattern, which means the underlying region has been erased and is
1381 * ready to be programmed.
1382 * The bitflips_threshold specify the maximum number of bitflips before
1383 * considering the region as not erased.
1384 *
1385 * Note:
1386 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1387 * different from the NAND page size. When fixing bitflips, ECC engines will
1388 * report the number of errors per chunk, and the NAND core infrastructure
1389 * expect you to return the maximum number of bitflips for the whole page.
1390 * This is why you should always use this function on a single chunk and
1391 * not on the whole page. After checking each chunk you should update your
1392 * max_bitflips value accordingly.
1393 * 2/ When checking for bitflips in erased pages you should not only check
1394 * the payload data but also their associated ECC data, because a user might
1395 * have programmed almost all bits to 1 but a few. In this case, we
1396 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1397 * this case.
1398 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1399 * data are protected by the ECC engine.
1400 * It could also be used if you support subpages and want to attach some
1401 * extra OOB data to an ECC chunk.
1402 *
1403 * Returns a positive number of bitflips less than or equal to
1404 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1405 * threshold. In case of success, the passed buffers are filled with 0xff.
1406 */
1407int nand_check_erased_ecc_chunk(void *data, int datalen,
1408 void *ecc, int ecclen,
1409 void *extraoob, int extraooblen,
1410 int bitflips_threshold)
1411{
1412 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1413
1414 data_bitflips = nand_check_erased_buf(data, datalen,
1415 bitflips_threshold);
1416 if (data_bitflips < 0)
1417 return data_bitflips;
1418
1419 bitflips_threshold -= data_bitflips;
1420
1421 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1422 if (ecc_bitflips < 0)
1423 return ecc_bitflips;
1424
1425 bitflips_threshold -= ecc_bitflips;
1426
1427 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1428 bitflips_threshold);
1429 if (extraoob_bitflips < 0)
1430 return extraoob_bitflips;
1431
1432 if (data_bitflips)
1433 memset(data, 0xff, datalen);
1434
1435 if (ecc_bitflips)
1436 memset(ecc, 0xff, ecclen);
1437
1438 if (extraoob_bitflips)
1439 memset(extraoob, 0xff, extraooblen);
1440
1441 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1442}
1443EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1444
1445/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001446 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001447 * @mtd: mtd info structure
1448 * @chip: nand chip info structure
1449 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001450 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001451 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001452 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001453 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001454 */
1455static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001456 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001457{
1458 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001459 if (oob_required)
1460 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001461 return 0;
1462}
1463
1464/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001465 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001466 * @mtd: mtd info structure
1467 * @chip: nand chip info structure
1468 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001469 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001470 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001471 *
1472 * We need a special oob layout and handling even when OOB isn't used.
1473 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001474static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001475 struct nand_chip *chip, uint8_t *buf,
1476 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001477{
1478 int eccsize = chip->ecc.size;
1479 int eccbytes = chip->ecc.bytes;
1480 uint8_t *oob = chip->oob_poi;
1481 int steps, size;
1482
1483 for (steps = chip->ecc.steps; steps > 0; steps--) {
1484 chip->read_buf(mtd, buf, eccsize);
1485 buf += eccsize;
1486
1487 if (chip->ecc.prepad) {
1488 chip->read_buf(mtd, oob, chip->ecc.prepad);
1489 oob += chip->ecc.prepad;
1490 }
1491
1492 chip->read_buf(mtd, oob, eccbytes);
1493 oob += eccbytes;
1494
1495 if (chip->ecc.postpad) {
1496 chip->read_buf(mtd, oob, chip->ecc.postpad);
1497 oob += chip->ecc.postpad;
1498 }
1499 }
1500
1501 size = mtd->oobsize - (oob - chip->oob_poi);
1502 if (size)
1503 chip->read_buf(mtd, oob, size);
1504
1505 return 0;
1506}
1507
1508/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001509 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001510 * @mtd: mtd info structure
1511 * @chip: nand chip info structure
1512 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001513 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001514 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001515 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001516static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001517 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Boris Brezillon846031d2016-02-03 20:11:00 +01001519 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001520 int eccbytes = chip->ecc.bytes;
1521 int eccsteps = chip->ecc.steps;
1522 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001523 uint8_t *ecc_calc = chip->buffers->ecccalc;
1524 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001525 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001526
Brian Norris1fbb9382012-05-02 10:14:55 -07001527 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001528
1529 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1530 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1531
Boris Brezillon846031d2016-02-03 20:11:00 +01001532 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1533 chip->ecc.total);
1534 if (ret)
1535 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001536
1537 eccsteps = chip->ecc.steps;
1538 p = buf;
1539
1540 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1541 int stat;
1542
1543 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001544 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001545 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001546 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001547 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001548 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1549 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001550 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001551 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301555 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001556 * @mtd: mtd info structure
1557 * @chip: nand chip info structure
1558 * @data_offs: offset of requested data within the page
1559 * @readlen: data length
1560 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001561 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001562 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001563static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001564 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1565 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001566{
Boris Brezillon846031d2016-02-03 20:11:00 +01001567 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001568 uint8_t *p;
1569 int data_col_addr, i, gaps = 0;
1570 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1571 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001572 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001573 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001574 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001575
Brian Norris7854d3f2011-06-23 14:12:08 -07001576 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001577 start_step = data_offs / chip->ecc.size;
1578 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1579 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301580 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001581
Brian Norris8b6e50c2011-05-25 14:59:01 -07001582 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001583 datafrag_len = num_steps * chip->ecc.size;
1584 eccfrag_len = num_steps * chip->ecc.bytes;
1585
1586 data_col_addr = start_step * chip->ecc.size;
1587 /* If we read not a page aligned data */
1588 if (data_col_addr != 0)
1589 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1590
1591 p = bufpoi + data_col_addr;
1592 chip->read_buf(mtd, p, datafrag_len);
1593
Brian Norris8b6e50c2011-05-25 14:59:01 -07001594 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001595 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1596 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1597
Brian Norris8b6e50c2011-05-25 14:59:01 -07001598 /*
1599 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001600 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001601 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001602 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1603 if (ret)
1604 return ret;
1605
1606 if (oobregion.length < eccfrag_len)
1607 gaps = 1;
1608
Alexey Korolev3d459552008-05-15 17:23:18 +01001609 if (gaps) {
1610 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1611 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1612 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001613 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001614 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001615 * about buswidth alignment in read_buf.
1616 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001617 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001618 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001619 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001620 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001621 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1622 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001623 aligned_len++;
1624
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001625 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001626 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001627 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1628 }
1629
Boris Brezillon846031d2016-02-03 20:11:00 +01001630 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1631 chip->oob_poi, index, eccfrag_len);
1632 if (ret)
1633 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001634
1635 p = bufpoi + data_col_addr;
1636 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1637 int stat;
1638
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001639 stat = chip->ecc.correct(mtd, p,
1640 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001641 if (stat == -EBADMSG &&
1642 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1643 /* check for empty pages with bitflips */
1644 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1645 &chip->buffers->ecccode[i],
1646 chip->ecc.bytes,
1647 NULL, 0,
1648 chip->ecc.strength);
1649 }
1650
Mike Dunn3f91e942012-04-25 12:06:09 -07001651 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001652 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001653 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001654 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001655 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1656 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001657 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001658 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001659}
1660
1661/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001662 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001663 * @mtd: mtd info structure
1664 * @chip: nand chip info structure
1665 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001666 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001667 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001668 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001669 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001670 */
1671static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001672 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001673{
Boris Brezillon846031d2016-02-03 20:11:00 +01001674 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001675 int eccbytes = chip->ecc.bytes;
1676 int eccsteps = chip->ecc.steps;
1677 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001678 uint8_t *ecc_calc = chip->buffers->ecccalc;
1679 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001680 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001681
1682 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1683 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1684 chip->read_buf(mtd, p, eccsize);
1685 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1686 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001687 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001688
Boris Brezillon846031d2016-02-03 20:11:00 +01001689 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1690 chip->ecc.total);
1691 if (ret)
1692 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001693
1694 eccsteps = chip->ecc.steps;
1695 p = buf;
1696
1697 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1698 int stat;
1699
1700 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001701 if (stat == -EBADMSG &&
1702 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1703 /* check for empty pages with bitflips */
1704 stat = nand_check_erased_ecc_chunk(p, eccsize,
1705 &ecc_code[i], eccbytes,
1706 NULL, 0,
1707 chip->ecc.strength);
1708 }
1709
Mike Dunn3f91e942012-04-25 12:06:09 -07001710 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001711 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001712 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001713 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001714 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1715 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001716 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001717 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001718}
1719
1720/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001721 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001722 * @mtd: mtd info structure
1723 * @chip: nand chip info structure
1724 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001725 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001726 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001727 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001728 * Hardware ECC for large page chips, require OOB to be read first. For this
1729 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1730 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1731 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1732 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001733 */
1734static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001735 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001736{
Boris Brezillon846031d2016-02-03 20:11:00 +01001737 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001738 int eccbytes = chip->ecc.bytes;
1739 int eccsteps = chip->ecc.steps;
1740 uint8_t *p = buf;
1741 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001742 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001743 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001744
1745 /* Read the OOB area first */
1746 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1747 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1748 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1749
Boris Brezillon846031d2016-02-03 20:11:00 +01001750 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1751 chip->ecc.total);
1752 if (ret)
1753 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001754
1755 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1756 int stat;
1757
1758 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1759 chip->read_buf(mtd, p, eccsize);
1760 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1761
1762 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001763 if (stat == -EBADMSG &&
1764 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1765 /* check for empty pages with bitflips */
1766 stat = nand_check_erased_ecc_chunk(p, eccsize,
1767 &ecc_code[i], eccbytes,
1768 NULL, 0,
1769 chip->ecc.strength);
1770 }
1771
Mike Dunn3f91e942012-04-25 12:06:09 -07001772 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001773 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001774 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001775 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001776 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1777 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001778 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001779 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001780}
1781
1782/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001783 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001784 * @mtd: mtd info structure
1785 * @chip: nand chip info structure
1786 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001787 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001788 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001789 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001790 * The hw generator calculates the error syndrome automatically. Therefore we
1791 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001792 */
1793static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001794 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001795{
1796 int i, eccsize = chip->ecc.size;
1797 int eccbytes = chip->ecc.bytes;
1798 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001799 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001800 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001801 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001802 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001803
1804 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1805 int stat;
1806
1807 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1808 chip->read_buf(mtd, p, eccsize);
1809
1810 if (chip->ecc.prepad) {
1811 chip->read_buf(mtd, oob, chip->ecc.prepad);
1812 oob += chip->ecc.prepad;
1813 }
1814
1815 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1816 chip->read_buf(mtd, oob, eccbytes);
1817 stat = chip->ecc.correct(mtd, p, oob, NULL);
1818
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001819 oob += eccbytes;
1820
1821 if (chip->ecc.postpad) {
1822 chip->read_buf(mtd, oob, chip->ecc.postpad);
1823 oob += chip->ecc.postpad;
1824 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001825
1826 if (stat == -EBADMSG &&
1827 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1828 /* check for empty pages with bitflips */
1829 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1830 oob - eccpadbytes,
1831 eccpadbytes,
1832 NULL, 0,
1833 chip->ecc.strength);
1834 }
1835
1836 if (stat < 0) {
1837 mtd->ecc_stats.failed++;
1838 } else {
1839 mtd->ecc_stats.corrected += stat;
1840 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1841 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001842 }
1843
1844 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001845 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001846 if (i)
1847 chip->read_buf(mtd, oob, i);
1848
Mike Dunn3f91e942012-04-25 12:06:09 -07001849 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001850}
1851
1852/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001853 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001854 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001855 * @oob: oob destination address
1856 * @ops: oob ops structure
1857 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001858 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001859static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001860 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001861{
Boris Brezillon846031d2016-02-03 20:11:00 +01001862 struct nand_chip *chip = mtd_to_nand(mtd);
1863 int ret;
1864
Florian Fainellif8ac0412010-09-07 13:23:43 +02001865 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001866
Brian Norris0612b9d2011-08-30 18:45:40 -07001867 case MTD_OPS_PLACE_OOB:
1868 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001869 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1870 return oob + len;
1871
Boris Brezillon846031d2016-02-03 20:11:00 +01001872 case MTD_OPS_AUTO_OOB:
1873 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1874 ops->ooboffs, len);
1875 BUG_ON(ret);
1876 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001877
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001878 default:
1879 BUG();
1880 }
1881 return NULL;
1882}
1883
1884/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001885 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1886 * @mtd: MTD device structure
1887 * @retry_mode: the retry mode to use
1888 *
1889 * Some vendors supply a special command to shift the Vt threshold, to be used
1890 * when there are too many bitflips in a page (i.e., ECC error). After setting
1891 * a new threshold, the host should retry reading the page.
1892 */
1893static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1894{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001895 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001896
1897 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1898
1899 if (retry_mode >= chip->read_retries)
1900 return -EINVAL;
1901
1902 if (!chip->setup_read_retry)
1903 return -EOPNOTSUPP;
1904
1905 return chip->setup_read_retry(mtd, retry_mode);
1906}
1907
1908/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001909 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001910 * @mtd: MTD device structure
1911 * @from: offset to read from
1912 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001913 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001914 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001915 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001916static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1917 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001918{
Brian Norrise47f3db2012-05-02 10:14:56 -07001919 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001920 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001921 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001922 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001923 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001924 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001925
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001926 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001927 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001928 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001929 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001930 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001932 chipnr = (int)(from >> chip->chip_shift);
1933 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001935 realpage = (int)(from >> chip->page_shift);
1936 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001938 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001940 buf = ops->datbuf;
1941 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001942 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001943
Florian Fainellif8ac0412010-09-07 13:23:43 +02001944 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001945 unsigned int ecc_failures = mtd->ecc_stats.failed;
1946
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001947 bytes = min(mtd->writesize - col, readlen);
1948 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001949
Kamal Dasu66507c72014-05-01 20:51:19 -04001950 if (!aligned)
1951 use_bufpoi = 1;
1952 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1953 use_bufpoi = !virt_addr_valid(buf);
1954 else
1955 use_bufpoi = 0;
1956
Brian Norris8b6e50c2011-05-25 14:59:01 -07001957 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001958 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001959 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1960
1961 if (use_bufpoi && aligned)
1962 pr_debug("%s: using read bounce buffer for buf@%p\n",
1963 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Brian Norrisba84fb52014-01-03 15:13:33 -08001965read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001966 if (nand_standard_page_accessors(&chip->ecc))
1967 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Mike Dunnedbc45402012-04-25 12:06:11 -07001969 /*
1970 * Now read the page into the buffer. Absent an error,
1971 * the read methods return max bitflips per ecc step.
1972 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001973 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001974 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001975 oob_required,
1976 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001977 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1978 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001979 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001980 col, bytes, bufpoi,
1981 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001982 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001983 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001984 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001985 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001986 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001987 /* Invalidate page cache */
1988 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001989 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001990 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001991
Mike Dunnedbc45402012-04-25 12:06:11 -07001992 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1993
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001994 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001995 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001996 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001997 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001998 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001999 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002000 chip->pagebuf_bitflips = ret;
2001 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002002 /* Invalidate page cache */
2003 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002004 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002005 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002007
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002008 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002009 int toread = min(oobreadlen, max_oobsize);
2010
2011 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002012 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002013 oob, ops, toread);
2014 oobreadlen -= toread;
2015 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002016 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002017
2018 if (chip->options & NAND_NEED_READRDY) {
2019 /* Apply delay or wait for ready/busy pin */
2020 if (!chip->dev_ready)
2021 udelay(chip->chip_delay);
2022 else
2023 nand_wait_ready(mtd);
2024 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002025
Brian Norrisba84fb52014-01-03 15:13:33 -08002026 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002027 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002028 retry_mode++;
2029 ret = nand_setup_read_retry(mtd,
2030 retry_mode);
2031 if (ret < 0)
2032 break;
2033
2034 /* Reset failures; retry */
2035 mtd->ecc_stats.failed = ecc_failures;
2036 goto read_retry;
2037 } else {
2038 /* No more retry modes; real failure */
2039 ecc_fail = true;
2040 }
2041 }
2042
2043 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002044 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002045 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002046 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002047 max_bitflips = max_t(unsigned int, max_bitflips,
2048 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002051 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002052
Brian Norrisba84fb52014-01-03 15:13:33 -08002053 /* Reset to retry mode 0 */
2054 if (retry_mode) {
2055 ret = nand_setup_read_retry(mtd, 0);
2056 if (ret < 0)
2057 break;
2058 retry_mode = 0;
2059 }
2060
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002061 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Brian Norris8b6e50c2011-05-25 14:59:01 -07002064 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 col = 0;
2066 /* Increment page address */
2067 realpage++;
2068
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002069 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* Check, if we cross a chip boundary */
2071 if (!page) {
2072 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002073 chip->select_chip(mtd, -1);
2074 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002077 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002079 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002080 if (oob)
2081 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Mike Dunn3f91e942012-04-25 12:06:09 -07002083 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002084 return ret;
2085
Brian Norrisb72f3df2013-12-03 11:04:14 -08002086 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002087 return -EBADMSG;
2088
Mike Dunnedbc45402012-04-25 12:06:11 -07002089 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002090}
2091
2092/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002093 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002094 * @mtd: MTD device structure
2095 * @from: offset to read from
2096 * @len: number of bytes to read
2097 * @retlen: pointer to variable to store the number of read bytes
2098 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002099 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002100 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002101 */
2102static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2103 size_t *retlen, uint8_t *buf)
2104{
Brian Norris4a89ff82011-08-30 18:45:45 -07002105 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002106 int ret;
2107
Huang Shijie6a8214a2012-11-19 14:43:30 +08002108 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002109 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002110 ops.len = len;
2111 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002112 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002113 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002114 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002115 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
2119/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002120 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002121 * @mtd: mtd info structure
2122 * @chip: nand chip info structure
2123 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002124 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002125int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002126{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002127 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002128 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002129 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002130}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002131EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002132
2133/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002134 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002135 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002136 * @mtd: mtd info structure
2137 * @chip: nand chip info structure
2138 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002139 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002140int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2141 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002142{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002143 int length = mtd->oobsize;
2144 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2145 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002146 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002147 int i, toread, sndrnd = 0, pos;
2148
2149 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2150 for (i = 0; i < chip->ecc.steps; i++) {
2151 if (sndrnd) {
2152 pos = eccsize + i * (eccsize + chunk);
2153 if (mtd->writesize > 512)
2154 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2155 else
2156 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2157 } else
2158 sndrnd = 1;
2159 toread = min_t(int, length, chunk);
2160 chip->read_buf(mtd, bufpoi, toread);
2161 bufpoi += toread;
2162 length -= toread;
2163 }
2164 if (length > 0)
2165 chip->read_buf(mtd, bufpoi, length);
2166
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002167 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002168}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002169EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002170
2171/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002172 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002173 * @mtd: mtd info structure
2174 * @chip: nand chip info structure
2175 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002176 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002177int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002178{
2179 int status = 0;
2180 const uint8_t *buf = chip->oob_poi;
2181 int length = mtd->oobsize;
2182
2183 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2184 chip->write_buf(mtd, buf, length);
2185 /* Send command to program the OOB data */
2186 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2187
2188 status = chip->waitfunc(mtd, chip);
2189
Savin Zlobec0d420f92006-06-21 11:51:20 +02002190 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002191}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002192EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002193
2194/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002195 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002196 * with syndrome - only for large page flash
2197 * @mtd: mtd info structure
2198 * @chip: nand chip info structure
2199 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002200 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002201int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2202 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002203{
2204 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2205 int eccsize = chip->ecc.size, length = mtd->oobsize;
2206 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2207 const uint8_t *bufpoi = chip->oob_poi;
2208
2209 /*
2210 * data-ecc-data-ecc ... ecc-oob
2211 * or
2212 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2213 */
2214 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2215 pos = steps * (eccsize + chunk);
2216 steps = 0;
2217 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002218 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002219
2220 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2221 for (i = 0; i < steps; i++) {
2222 if (sndcmd) {
2223 if (mtd->writesize <= 512) {
2224 uint32_t fill = 0xFFFFFFFF;
2225
2226 len = eccsize;
2227 while (len > 0) {
2228 int num = min_t(int, len, 4);
2229 chip->write_buf(mtd, (uint8_t *)&fill,
2230 num);
2231 len -= num;
2232 }
2233 } else {
2234 pos = eccsize + i * (eccsize + chunk);
2235 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2236 }
2237 } else
2238 sndcmd = 1;
2239 len = min_t(int, length, chunk);
2240 chip->write_buf(mtd, bufpoi, len);
2241 bufpoi += len;
2242 length -= len;
2243 }
2244 if (length > 0)
2245 chip->write_buf(mtd, bufpoi, length);
2246
2247 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2248 status = chip->waitfunc(mtd, chip);
2249
2250 return status & NAND_STATUS_FAIL ? -EIO : 0;
2251}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002252EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002253
2254/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002255 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002256 * @mtd: MTD device structure
2257 * @from: offset to read from
2258 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002260 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002262static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2263 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Brian Norrisc00a0992012-05-01 17:12:54 -07002265 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002266 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002267 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002268 int readlen = ops->ooblen;
2269 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002270 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002271 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Brian Norris289c0522011-07-19 10:06:09 -07002273 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302274 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Brian Norris041e4572011-06-23 16:45:24 -07002276 stats = mtd->ecc_stats;
2277
Boris BREZILLON29f10582016-03-07 10:46:52 +01002278 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002279
2280 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002281 pr_debug("%s: attempt to start read outside oob\n",
2282 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002283 return -EINVAL;
2284 }
2285
2286 /* Do not allow reads past end of device */
2287 if (unlikely(from >= mtd->size ||
2288 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2289 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002290 pr_debug("%s: attempt to read beyond end of device\n",
2291 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002292 return -EINVAL;
2293 }
Vitaly Wool70145682006-11-03 18:20:38 +03002294
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002295 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002296 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002298 /* Shift to get page */
2299 realpage = (int)(from >> chip->page_shift);
2300 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Florian Fainellif8ac0412010-09-07 13:23:43 +02002302 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002303 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002304 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002305 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002306 ret = chip->ecc.read_oob(mtd, chip, page);
2307
2308 if (ret < 0)
2309 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002310
2311 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002312 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002313
Brian Norris5bc7c332013-03-13 09:51:31 -07002314 if (chip->options & NAND_NEED_READRDY) {
2315 /* Apply delay or wait for ready/busy pin */
2316 if (!chip->dev_ready)
2317 udelay(chip->chip_delay);
2318 else
2319 nand_wait_ready(mtd);
2320 }
2321
Vitaly Wool70145682006-11-03 18:20:38 +03002322 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002323 if (!readlen)
2324 break;
2325
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002326 /* Increment page address */
2327 realpage++;
2328
2329 page = realpage & chip->pagemask;
2330 /* Check, if we cross a chip boundary */
2331 if (!page) {
2332 chipnr++;
2333 chip->select_chip(mtd, -1);
2334 chip->select_chip(mtd, chipnr);
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002337 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002339 ops->oobretlen = ops->ooblen - readlen;
2340
2341 if (ret < 0)
2342 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002343
2344 if (mtd->ecc_stats.failed - stats.failed)
2345 return -EBADMSG;
2346
2347 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002351 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002352 * @mtd: MTD device structure
2353 * @from: offset to read from
2354 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002356 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002358static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2359 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002361 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002362
2363 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002366 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002367 pr_debug("%s: attempt to read beyond end of device\n",
2368 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -EINVAL;
2370 }
2371
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002372 if (ops->mode != MTD_OPS_PLACE_OOB &&
2373 ops->mode != MTD_OPS_AUTO_OOB &&
2374 ops->mode != MTD_OPS_RAW)
2375 return -ENOTSUPP;
2376
Huang Shijie6a8214a2012-11-19 14:43:30 +08002377 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002379 if (!ops->datbuf)
2380 ret = nand_do_read_oob(mtd, from, ops);
2381 else
2382 ret = nand_do_read_ops(mtd, from, ops);
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002385 return ret;
2386}
2387
2388
2389/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002390 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002391 * @mtd: mtd info structure
2392 * @chip: nand chip info structure
2393 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002394 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002395 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002396 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002397 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002398 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002399static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002400 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002401{
2402 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002403 if (oob_required)
2404 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002405
2406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002409/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002410 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002411 * @mtd: mtd info structure
2412 * @chip: nand chip info structure
2413 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002414 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002415 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002416 *
2417 * We need a special oob layout and handling even when ECC isn't checked.
2418 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002419static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002420 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002421 const uint8_t *buf, int oob_required,
2422 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002423{
2424 int eccsize = chip->ecc.size;
2425 int eccbytes = chip->ecc.bytes;
2426 uint8_t *oob = chip->oob_poi;
2427 int steps, size;
2428
2429 for (steps = chip->ecc.steps; steps > 0; steps--) {
2430 chip->write_buf(mtd, buf, eccsize);
2431 buf += eccsize;
2432
2433 if (chip->ecc.prepad) {
2434 chip->write_buf(mtd, oob, chip->ecc.prepad);
2435 oob += chip->ecc.prepad;
2436 }
2437
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002438 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002439 oob += eccbytes;
2440
2441 if (chip->ecc.postpad) {
2442 chip->write_buf(mtd, oob, chip->ecc.postpad);
2443 oob += chip->ecc.postpad;
2444 }
2445 }
2446
2447 size = mtd->oobsize - (oob - chip->oob_poi);
2448 if (size)
2449 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002450
2451 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002452}
2453/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002454 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002455 * @mtd: mtd info structure
2456 * @chip: nand chip info structure
2457 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002458 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002459 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002460 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002461static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002462 const uint8_t *buf, int oob_required,
2463 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002464{
Boris Brezillon846031d2016-02-03 20:11:00 +01002465 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002466 int eccbytes = chip->ecc.bytes;
2467 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002468 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002469 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002470
Brian Norris7854d3f2011-06-23 14:12:08 -07002471 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002472 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2473 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002474
Boris Brezillon846031d2016-02-03 20:11:00 +01002475 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2476 chip->ecc.total);
2477 if (ret)
2478 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002479
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002480 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002481}
2482
2483/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002484 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002485 * @mtd: mtd info structure
2486 * @chip: nand chip info structure
2487 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002488 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002489 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002490 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002491static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002492 const uint8_t *buf, int oob_required,
2493 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002494{
Boris Brezillon846031d2016-02-03 20:11:00 +01002495 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002496 int eccbytes = chip->ecc.bytes;
2497 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002498 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002499 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002500
2501 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2502 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002503 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002504 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2505 }
2506
Boris Brezillon846031d2016-02-03 20:11:00 +01002507 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2508 chip->ecc.total);
2509 if (ret)
2510 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002511
2512 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002513
2514 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002515}
2516
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302517
2518/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002519 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302520 * @mtd: mtd info structure
2521 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002522 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302523 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002524 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302525 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002526 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302527 */
2528static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2529 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002530 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002531 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302532{
2533 uint8_t *oob_buf = chip->oob_poi;
2534 uint8_t *ecc_calc = chip->buffers->ecccalc;
2535 int ecc_size = chip->ecc.size;
2536 int ecc_bytes = chip->ecc.bytes;
2537 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302538 uint32_t start_step = offset / ecc_size;
2539 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2540 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002541 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302542
2543 for (step = 0; step < ecc_steps; step++) {
2544 /* configure controller for WRITE access */
2545 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2546
2547 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002548 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302549
2550 /* mask ECC of un-touched subpages by padding 0xFF */
2551 if ((step < start_step) || (step > end_step))
2552 memset(ecc_calc, 0xff, ecc_bytes);
2553 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002554 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302555
2556 /* mask OOB of un-touched subpages by padding 0xFF */
2557 /* if oob_required, preserve OOB metadata of written subpage */
2558 if (!oob_required || (step < start_step) || (step > end_step))
2559 memset(oob_buf, 0xff, oob_bytes);
2560
Brian Norrisd6a950802013-08-08 17:16:36 -07002561 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302562 ecc_calc += ecc_bytes;
2563 oob_buf += oob_bytes;
2564 }
2565
2566 /* copy calculated ECC for whole page to chip->buffer->oob */
2567 /* this include masked-value(0xFF) for unwritten subpages */
2568 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002569 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2570 chip->ecc.total);
2571 if (ret)
2572 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302573
2574 /* write OOB buffer to NAND device */
2575 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2576
2577 return 0;
2578}
2579
2580
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002581/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002582 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002583 * @mtd: mtd info structure
2584 * @chip: nand chip info structure
2585 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002586 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002587 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002588 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002589 * The hw generator calculates the error syndrome automatically. Therefore we
2590 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002591 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002592static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002593 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002594 const uint8_t *buf, int oob_required,
2595 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002596{
2597 int i, eccsize = chip->ecc.size;
2598 int eccbytes = chip->ecc.bytes;
2599 int eccsteps = chip->ecc.steps;
2600 const uint8_t *p = buf;
2601 uint8_t *oob = chip->oob_poi;
2602
2603 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2604
2605 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2606 chip->write_buf(mtd, p, eccsize);
2607
2608 if (chip->ecc.prepad) {
2609 chip->write_buf(mtd, oob, chip->ecc.prepad);
2610 oob += chip->ecc.prepad;
2611 }
2612
2613 chip->ecc.calculate(mtd, p, oob);
2614 chip->write_buf(mtd, oob, eccbytes);
2615 oob += eccbytes;
2616
2617 if (chip->ecc.postpad) {
2618 chip->write_buf(mtd, oob, chip->ecc.postpad);
2619 oob += chip->ecc.postpad;
2620 }
2621 }
2622
2623 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002624 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002625 if (i)
2626 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002627
2628 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002629}
2630
2631/**
David Woodhouse956e9442006-09-25 17:12:39 +01002632 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002633 * @mtd: MTD device structure
2634 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302635 * @offset: address offset within the page
2636 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002637 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002638 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002639 * @page: page number to write
2640 * @cached: cached programming
2641 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002642 */
2643static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302644 uint32_t offset, int data_len, const uint8_t *buf,
2645 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002646{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302647 int status, subpage;
2648
2649 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2650 chip->ecc.write_subpage)
2651 subpage = offset || (data_len < mtd->writesize);
2652 else
2653 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002654
Marc Gonzalez3371d662016-11-15 10:56:20 +01002655 if (nand_standard_page_accessors(&chip->ecc))
2656 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002657
David Woodhouse956e9442006-09-25 17:12:39 +01002658 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302659 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002660 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302661 else if (subpage)
2662 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002663 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002664 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002665 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2666 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002667
2668 if (status < 0)
2669 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002670
2671 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002672 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002673 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002674 */
2675 cached = 0;
2676
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002677 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002678
Marc Gonzalez3371d662016-11-15 10:56:20 +01002679 if (nand_standard_page_accessors(&chip->ecc))
2680 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002681 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002682 /*
2683 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002684 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002685 */
2686 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2687 status = chip->errstat(mtd, chip, FL_WRITING, status,
2688 page);
2689
2690 if (status & NAND_STATUS_FAIL)
2691 return -EIO;
2692 } else {
2693 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002694 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002695 }
2696
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002697 return 0;
2698}
2699
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002700/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002701 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002702 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002703 * @oob: oob data buffer
2704 * @len: oob data write length
2705 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002706 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002707static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2708 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002709{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002710 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002711 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002712
2713 /*
2714 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2715 * data from a previous OOB read.
2716 */
2717 memset(chip->oob_poi, 0xff, mtd->oobsize);
2718
Florian Fainellif8ac0412010-09-07 13:23:43 +02002719 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002720
Brian Norris0612b9d2011-08-30 18:45:40 -07002721 case MTD_OPS_PLACE_OOB:
2722 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002723 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2724 return oob + len;
2725
Boris Brezillon846031d2016-02-03 20:11:00 +01002726 case MTD_OPS_AUTO_OOB:
2727 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2728 ops->ooboffs, len);
2729 BUG_ON(ret);
2730 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002731
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002732 default:
2733 BUG();
2734 }
2735 return NULL;
2736}
2737
Florian Fainellif8ac0412010-09-07 13:23:43 +02002738#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002739
2740/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002741 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002742 * @mtd: MTD device structure
2743 * @to: offset to write to
2744 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002745 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002746 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002747 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002748static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2749 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002750{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002751 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002752 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002753 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002754
2755 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002756 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002757
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002758 uint8_t *oob = ops->oobbuf;
2759 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302760 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002761 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002762
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002763 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002764 if (!writelen)
2765 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002766
Brian Norris8b6e50c2011-05-25 14:59:01 -07002767 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002768 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002769 pr_notice("%s: attempt to write non page aligned data\n",
2770 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002771 return -EINVAL;
2772 }
2773
Thomas Gleixner29072b92006-09-28 15:38:36 +02002774 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002775
Thomas Gleixner6a930962006-06-28 00:11:45 +02002776 chipnr = (int)(to >> chip->chip_shift);
2777 chip->select_chip(mtd, chipnr);
2778
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002779 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002780 if (nand_check_wp(mtd)) {
2781 ret = -EIO;
2782 goto err_out;
2783 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002784
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002785 realpage = (int)(to >> chip->page_shift);
2786 page = realpage & chip->pagemask;
2787 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2788
2789 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002790 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2791 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002792 chip->pagebuf = -1;
2793
Maxim Levitsky782ce792010-02-22 20:39:36 +02002794 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002795 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2796 ret = -EINVAL;
2797 goto err_out;
2798 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002799
Florian Fainellif8ac0412010-09-07 13:23:43 +02002800 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002801 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002802 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002803 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002804 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002805 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002806
Kamal Dasu66507c72014-05-01 20:51:19 -04002807 if (part_pagewr)
2808 use_bufpoi = 1;
2809 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2810 use_bufpoi = !virt_addr_valid(buf);
2811 else
2812 use_bufpoi = 0;
2813
2814 /* Partial page write?, or need to use bounce buffer */
2815 if (use_bufpoi) {
2816 pr_debug("%s: using write bounce buffer for buf@%p\n",
2817 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002818 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002819 if (part_pagewr)
2820 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002821 chip->pagebuf = -1;
2822 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2823 memcpy(&chip->buffers->databuf[column], buf, bytes);
2824 wbuf = chip->buffers->databuf;
2825 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002826
Maxim Levitsky782ce792010-02-22 20:39:36 +02002827 if (unlikely(oob)) {
2828 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002829 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002830 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002831 } else {
2832 /* We still need to erase leftover OOB data */
2833 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002834 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302835 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2836 oob_required, page, cached,
2837 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002838 if (ret)
2839 break;
2840
2841 writelen -= bytes;
2842 if (!writelen)
2843 break;
2844
Thomas Gleixner29072b92006-09-28 15:38:36 +02002845 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002846 buf += bytes;
2847 realpage++;
2848
2849 page = realpage & chip->pagemask;
2850 /* Check, if we cross a chip boundary */
2851 if (!page) {
2852 chipnr++;
2853 chip->select_chip(mtd, -1);
2854 chip->select_chip(mtd, chipnr);
2855 }
2856 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002857
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002858 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002859 if (unlikely(oob))
2860 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002861
2862err_out:
2863 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002864 return ret;
2865}
2866
2867/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002868 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002869 * @mtd: MTD device structure
2870 * @to: offset to write to
2871 * @len: number of bytes to write
2872 * @retlen: pointer to variable to store the number of written bytes
2873 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002874 *
2875 * NAND write with ECC. Used when performing writes in interrupt context, this
2876 * may for example be called by mtdoops when writing an oops while in panic.
2877 */
2878static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2879 size_t *retlen, const uint8_t *buf)
2880{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002881 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002882 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002883 int ret;
2884
Brian Norris8b6e50c2011-05-25 14:59:01 -07002885 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002886 panic_nand_wait(mtd, chip, 400);
2887
Brian Norris8b6e50c2011-05-25 14:59:01 -07002888 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002889 panic_nand_get_device(chip, mtd, FL_WRITING);
2890
Brian Norris0ec56dc2015-02-28 02:02:30 -08002891 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002892 ops.len = len;
2893 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002894 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002895
Brian Norris4a89ff82011-08-30 18:45:45 -07002896 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002897
Brian Norris4a89ff82011-08-30 18:45:45 -07002898 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002899 return ret;
2900}
2901
2902/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002903 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002904 * @mtd: MTD device structure
2905 * @to: offset to write to
2906 * @len: number of bytes to write
2907 * @retlen: pointer to variable to store the number of written bytes
2908 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002910 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002912static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002913 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Brian Norris4a89ff82011-08-30 18:45:45 -07002915 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002916 int ret;
2917
Huang Shijie6a8214a2012-11-19 14:43:30 +08002918 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002919 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002920 ops.len = len;
2921 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002922 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002923 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002924 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002925 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002926 return ret;
2927}
2928
2929/**
2930 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002931 * @mtd: MTD device structure
2932 * @to: offset to write to
2933 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002934 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002935 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002936 */
2937static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2938 struct mtd_oob_ops *ops)
2939{
Adrian Hunter03736152007-01-31 17:58:29 +02002940 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002941 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Brian Norris289c0522011-07-19 10:06:09 -07002943 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302944 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Boris BREZILLON29f10582016-03-07 10:46:52 +01002946 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002949 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002950 pr_debug("%s: attempt to write past end of page\n",
2951 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return -EINVAL;
2953 }
2954
Adrian Hunter03736152007-01-31 17:58:29 +02002955 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002956 pr_debug("%s: attempt to start write outside oob\n",
2957 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002958 return -EINVAL;
2959 }
2960
Jason Liu775adc3d42011-02-25 13:06:18 +08002961 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002962 if (unlikely(to >= mtd->size ||
2963 ops->ooboffs + ops->ooblen >
2964 ((mtd->size >> chip->page_shift) -
2965 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002966 pr_debug("%s: attempt to write beyond end of device\n",
2967 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002968 return -EINVAL;
2969 }
2970
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002971 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002972
2973 /*
2974 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2975 * of my DiskOnChip 2000 test units) will clear the whole data page too
2976 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2977 * it in the doc2000 driver in August 1999. dwmw2.
2978 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002979 nand_reset(chip, chipnr);
2980
2981 chip->select_chip(mtd, chipnr);
2982
2983 /* Shift to get page */
2984 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002987 if (nand_check_wp(mtd)) {
2988 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002989 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002990 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002993 if (page == chip->pagebuf)
2994 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002996 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002997
Brian Norris0612b9d2011-08-30 18:45:40 -07002998 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002999 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3000 else
3001 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003002
Huang Shijieb0bb6902012-11-19 14:43:29 +08003003 chip->select_chip(mtd, -1);
3004
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003005 if (status)
3006 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Vitaly Wool70145682006-11-03 18:20:38 +03003008 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003010 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003013/**
3014 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003015 * @mtd: MTD device structure
3016 * @to: offset to write to
3017 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003018 */
3019static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3020 struct mtd_oob_ops *ops)
3021{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003022 int ret = -ENOTSUPP;
3023
3024 ops->retlen = 0;
3025
3026 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003027 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003028 pr_debug("%s: attempt to write beyond end of device\n",
3029 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003030 return -EINVAL;
3031 }
3032
Huang Shijie6a8214a2012-11-19 14:43:30 +08003033 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003034
Florian Fainellif8ac0412010-09-07 13:23:43 +02003035 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003036 case MTD_OPS_PLACE_OOB:
3037 case MTD_OPS_AUTO_OOB:
3038 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003039 break;
3040
3041 default:
3042 goto out;
3043 }
3044
3045 if (!ops->datbuf)
3046 ret = nand_do_write_oob(mtd, to, ops);
3047 else
3048 ret = nand_do_write_ops(mtd, to, ops);
3049
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003050out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003051 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return ret;
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055/**
Brian Norris49c50b92014-05-06 16:02:19 -07003056 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003057 * @mtd: MTD device structure
3058 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 *
Brian Norris49c50b92014-05-06 16:02:19 -07003060 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 */
Brian Norris49c50b92014-05-06 16:02:19 -07003062static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003064 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003066 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3067 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003068
3069 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
3072/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003074 * @mtd: MTD device structure
3075 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003077 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003079static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
David Woodhousee0c7d762006-05-13 18:07:53 +01003081 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003085 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003086 * @mtd: MTD device structure
3087 * @instr: erase instruction
3088 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003090 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003092int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3093 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Adrian Hunter69423d92008-12-10 13:37:21 +00003095 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003096 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003097 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Brian Norris289c0522011-07-19 10:06:09 -07003099 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3100 __func__, (unsigned long long)instr->addr,
3101 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303103 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003107 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003110 page = (int)(instr->addr >> chip->page_shift);
3111 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003114 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003117 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /* Check, if it is write protected */
3120 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003121 pr_debug("%s: device is write protected!\n",
3122 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 instr->state = MTD_ERASE_FAILED;
3124 goto erase_exit;
3125 }
3126
3127 /* Loop through the pages */
3128 len = instr->len;
3129
3130 instr->state = MTD_ERASING;
3131
3132 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003133 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003134 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303135 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003136 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3137 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 instr->state = MTD_ERASE_FAILED;
3139 goto erase_exit;
3140 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003141
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003142 /*
3143 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003144 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003145 */
3146 if (page <= chip->pagebuf && chip->pagebuf <
3147 (page + pages_per_block))
3148 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Brian Norris49c50b92014-05-06 16:02:19 -07003150 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003152 /*
3153 * See if operation failed and additional status checks are
3154 * available
3155 */
3156 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3157 status = chip->errstat(mtd, chip, FL_ERASING,
3158 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003161 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003162 pr_debug("%s: failed erase, page 0x%08x\n",
3163 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003165 instr->fail_addr =
3166 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 goto erase_exit;
3168 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003171 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 page += pages_per_block;
3173
3174 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003175 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003177 chip->select_chip(mtd, -1);
3178 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180 }
3181 instr->state = MTD_ERASE_DONE;
3182
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003183erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003188 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 nand_release_device(mtd);
3190
David Woodhouse49defc02007-10-06 15:01:59 -04003191 /* Do call back function */
3192 if (!ret)
3193 mtd_erase_callback(instr);
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 /* Return more or less happy */
3196 return ret;
3197}
3198
3199/**
3200 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003201 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003203 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003205static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Brian Norris289c0522011-07-19 10:06:09 -07003207 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003210 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003212 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003216 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003217 * @mtd: MTD device structure
3218 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003220static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303222 struct nand_chip *chip = mtd_to_nand(mtd);
3223 int chipnr = (int)(offs >> chip->chip_shift);
3224 int ret;
3225
3226 /* Select the NAND device */
3227 nand_get_device(mtd, FL_READING);
3228 chip->select_chip(mtd, chipnr);
3229
3230 ret = nand_block_checkbad(mtd, offs, 0);
3231
3232 chip->select_chip(mtd, -1);
3233 nand_release_device(mtd);
3234
3235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
3238/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003239 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003240 * @mtd: MTD device structure
3241 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003243static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 int ret;
3246
Florian Fainellif8ac0412010-09-07 13:23:43 +02003247 ret = nand_block_isbad(mtd, ofs);
3248 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003249 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (ret > 0)
3251 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003252 return ret;
3253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Brian Norris5a0edb22013-07-30 17:52:58 -07003255 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
3258/**
Zach Brown56718422017-01-10 13:30:20 -06003259 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3260 * @mtd: MTD device structure
3261 * @ofs: offset relative to mtd start
3262 * @len: length of mtd
3263 */
3264static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3265{
3266 struct nand_chip *chip = mtd_to_nand(mtd);
3267 u32 part_start_block;
3268 u32 part_end_block;
3269 u32 part_start_die;
3270 u32 part_end_die;
3271
3272 /*
3273 * max_bb_per_die and blocks_per_die used to determine
3274 * the maximum bad block count.
3275 */
3276 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3277 return -ENOTSUPP;
3278
3279 /* Get the start and end of the partition in erase blocks. */
3280 part_start_block = mtd_div_by_eb(ofs, mtd);
3281 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3282
3283 /* Get the start and end LUNs of the partition. */
3284 part_start_die = part_start_block / chip->blocks_per_die;
3285 part_end_die = part_end_block / chip->blocks_per_die;
3286
3287 /*
3288 * Look up the bad blocks per unit and multiply by the number of units
3289 * that the partition spans.
3290 */
3291 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3292}
3293
3294/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003295 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3296 * @mtd: MTD device structure
3297 * @chip: nand chip info structure
3298 * @addr: feature address.
3299 * @subfeature_param: the subfeature parameters, a four bytes array.
3300 */
3301static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3302 int addr, uint8_t *subfeature_param)
3303{
3304 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003305 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003306
David Mosbergerd914c932013-05-29 15:30:13 +03003307 if (!chip->onfi_version ||
3308 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3309 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003310 return -EINVAL;
3311
3312 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003313 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3314 chip->write_byte(mtd, subfeature_param[i]);
3315
Huang Shijie7db03ec2012-09-13 14:57:52 +08003316 status = chip->waitfunc(mtd, chip);
3317 if (status & NAND_STATUS_FAIL)
3318 return -EIO;
3319 return 0;
3320}
3321
3322/**
3323 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3324 * @mtd: MTD device structure
3325 * @chip: nand chip info structure
3326 * @addr: feature address.
3327 * @subfeature_param: the subfeature parameters, a four bytes array.
3328 */
3329static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3330 int addr, uint8_t *subfeature_param)
3331{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003332 int i;
3333
David Mosbergerd914c932013-05-29 15:30:13 +03003334 if (!chip->onfi_version ||
3335 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3336 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003337 return -EINVAL;
3338
Huang Shijie7db03ec2012-09-13 14:57:52 +08003339 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003340 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3341 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003342 return 0;
3343}
3344
3345/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003346 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003347 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003348 */
3349static int nand_suspend(struct mtd_info *mtd)
3350{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003351 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003352}
3353
3354/**
3355 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003356 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003357 */
3358static void nand_resume(struct mtd_info *mtd)
3359{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003360 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003361
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003362 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003363 nand_release_device(mtd);
3364 else
Brian Norrisd0370212011-07-19 10:06:08 -07003365 pr_err("%s called for a chip which is not in suspended state\n",
3366 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003367}
3368
Scott Branden72ea4032014-11-20 11:18:05 -08003369/**
3370 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3371 * prevent further operations
3372 * @mtd: MTD device structure
3373 */
3374static void nand_shutdown(struct mtd_info *mtd)
3375{
Brian Norris9ca641b2015-11-09 16:37:28 -08003376 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003377}
3378
Brian Norris8b6e50c2011-05-25 14:59:01 -07003379/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003380static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003381{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003382 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003385 if (!chip->chip_delay)
3386 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
3388 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003389 if (chip->cmdfunc == NULL)
3390 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003393 if (chip->waitfunc == NULL)
3394 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003396 if (!chip->select_chip)
3397 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003398
Huang Shijie4204ccc2013-08-16 10:10:07 +08003399 /* set for ONFI nand */
3400 if (!chip->onfi_set_features)
3401 chip->onfi_set_features = nand_onfi_set_features;
3402 if (!chip->onfi_get_features)
3403 chip->onfi_get_features = nand_onfi_get_features;
3404
Brian Norris68e80782013-07-18 01:17:02 -07003405 /* If called twice, pointers that depend on busw may need to be reset */
3406 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003407 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3408 if (!chip->read_word)
3409 chip->read_word = nand_read_word;
3410 if (!chip->block_bad)
3411 chip->block_bad = nand_block_bad;
3412 if (!chip->block_markbad)
3413 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003414 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003415 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003416 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3417 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003418 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003419 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003420 if (!chip->scan_bbt)
3421 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003422
3423 if (!chip->controller) {
3424 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003425 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003426 }
3427
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003428}
3429
Brian Norris8b6e50c2011-05-25 14:59:01 -07003430/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003431static void sanitize_string(uint8_t *s, size_t len)
3432{
3433 ssize_t i;
3434
Brian Norris8b6e50c2011-05-25 14:59:01 -07003435 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003436 s[len - 1] = 0;
3437
Brian Norris8b6e50c2011-05-25 14:59:01 -07003438 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003439 for (i = 0; i < len - 1; i++) {
3440 if (s[i] < ' ' || s[i] > 127)
3441 s[i] = '?';
3442 }
3443
Brian Norris8b6e50c2011-05-25 14:59:01 -07003444 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003445 strim(s);
3446}
3447
3448static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3449{
3450 int i;
3451 while (len--) {
3452 crc ^= *p++ << 8;
3453 for (i = 0; i < 8; i++)
3454 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3455 }
3456
3457 return crc;
3458}
3459
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003460/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003461static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3462 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003463{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003464 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003465 struct onfi_ext_param_page *ep;
3466 struct onfi_ext_section *s;
3467 struct onfi_ext_ecc_info *ecc;
3468 uint8_t *cursor;
3469 int ret = -EINVAL;
3470 int len;
3471 int i;
3472
3473 len = le16_to_cpu(p->ext_param_page_length) * 16;
3474 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003475 if (!ep)
3476 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003477
3478 /* Send our own NAND_CMD_PARAM. */
3479 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3480
3481 /* Use the Change Read Column command to skip the ONFI param pages. */
3482 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3483 sizeof(*p) * p->num_of_param_pages , -1);
3484
3485 /* Read out the Extended Parameter Page. */
3486 chip->read_buf(mtd, (uint8_t *)ep, len);
3487 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3488 != le16_to_cpu(ep->crc))) {
3489 pr_debug("fail in the CRC.\n");
3490 goto ext_out;
3491 }
3492
3493 /*
3494 * Check the signature.
3495 * Do not strictly follow the ONFI spec, maybe changed in future.
3496 */
3497 if (strncmp(ep->sig, "EPPS", 4)) {
3498 pr_debug("The signature is invalid.\n");
3499 goto ext_out;
3500 }
3501
3502 /* find the ECC section. */
3503 cursor = (uint8_t *)(ep + 1);
3504 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3505 s = ep->sections + i;
3506 if (s->type == ONFI_SECTION_TYPE_2)
3507 break;
3508 cursor += s->length * 16;
3509 }
3510 if (i == ONFI_EXT_SECTION_MAX) {
3511 pr_debug("We can not find the ECC section.\n");
3512 goto ext_out;
3513 }
3514
3515 /* get the info we want. */
3516 ecc = (struct onfi_ext_ecc_info *)cursor;
3517
Brian Norris4ae7d222013-09-16 18:20:21 -07003518 if (!ecc->codeword_size) {
3519 pr_debug("Invalid codeword size\n");
3520 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003521 }
3522
Brian Norris4ae7d222013-09-16 18:20:21 -07003523 chip->ecc_strength_ds = ecc->ecc_bits;
3524 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003525 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003526
3527ext_out:
3528 kfree(ep);
3529 return ret;
3530}
3531
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003532/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003533 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003534 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003535static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003536{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003537 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003538 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003539 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003540 int val;
3541
Brian Norris7854d3f2011-06-23 14:12:08 -07003542 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003543 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3544 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3545 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3546 return 0;
3547
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003548 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3549 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003550 for (j = 0; j < sizeof(*p); j++)
3551 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003552 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3553 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003554 break;
3555 }
3556 }
3557
Brian Norrisc7f23a72013-08-13 10:51:55 -07003558 if (i == 3) {
3559 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003560 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003561 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003562
Brian Norris8b6e50c2011-05-25 14:59:01 -07003563 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003564 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003565 if (val & (1 << 5))
3566 chip->onfi_version = 23;
3567 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003568 chip->onfi_version = 22;
3569 else if (val & (1 << 3))
3570 chip->onfi_version = 21;
3571 else if (val & (1 << 2))
3572 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003573 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003574 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003575
3576 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003577 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003578 return 0;
3579 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003580
3581 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3582 sanitize_string(p->model, sizeof(p->model));
3583 if (!mtd->name)
3584 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003585
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003586 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003587
3588 /*
3589 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3590 * (don't ask me who thought of this...). MTD assumes that these
3591 * dimensions will be power-of-2, so just truncate the remaining area.
3592 */
3593 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3594 mtd->erasesize *= mtd->writesize;
3595
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003596 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003597
3598 /* See erasesize comment */
3599 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003600 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003601 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003602
Zach Brown34da5f52017-01-10 13:30:21 -06003603 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3604 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3605
Huang Shijiee2985fc2013-05-17 11:17:30 +08003606 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003607 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003608
Huang Shijie10c86ba2013-05-17 11:17:26 +08003609 if (p->ecc_bits != 0xff) {
3610 chip->ecc_strength_ds = p->ecc_bits;
3611 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003612 } else if (chip->onfi_version >= 21 &&
3613 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3614
3615 /*
3616 * The nand_flash_detect_ext_param_page() uses the
3617 * Change Read Column command which maybe not supported
3618 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3619 * now. We do not replace user supplied command function.
3620 */
3621 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3622 chip->cmdfunc = nand_command_lp;
3623
3624 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003625 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003626 pr_warn("Failed to detect ONFI extended param page\n");
3627 } else {
3628 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003629 }
3630
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003631 return 1;
3632}
3633
3634/*
Huang Shijie91361812014-02-21 13:39:40 +08003635 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3636 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003637static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003638{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003639 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003640 struct nand_jedec_params *p = &chip->jedec_params;
3641 struct jedec_ecc_info *ecc;
3642 int val;
3643 int i, j;
3644
3645 /* Try JEDEC for unknown chip or LP */
3646 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3647 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3648 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3649 chip->read_byte(mtd) != 'C')
3650 return 0;
3651
3652 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3653 for (i = 0; i < 3; i++) {
3654 for (j = 0; j < sizeof(*p); j++)
3655 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3656
3657 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3658 le16_to_cpu(p->crc))
3659 break;
3660 }
3661
3662 if (i == 3) {
3663 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3664 return 0;
3665 }
3666
3667 /* Check version */
3668 val = le16_to_cpu(p->revision);
3669 if (val & (1 << 2))
3670 chip->jedec_version = 10;
3671 else if (val & (1 << 1))
3672 chip->jedec_version = 1; /* vendor specific version */
3673
3674 if (!chip->jedec_version) {
3675 pr_info("unsupported JEDEC version: %d\n", val);
3676 return 0;
3677 }
3678
3679 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3680 sanitize_string(p->model, sizeof(p->model));
3681 if (!mtd->name)
3682 mtd->name = p->model;
3683
3684 mtd->writesize = le32_to_cpu(p->byte_per_page);
3685
3686 /* Please reference to the comment for nand_flash_detect_onfi. */
3687 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3688 mtd->erasesize *= mtd->writesize;
3689
3690 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3691
3692 /* Please reference to the comment for nand_flash_detect_onfi. */
3693 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3694 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3695 chip->bits_per_cell = p->bits_per_cell;
3696
3697 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003698 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003699
3700 /* ECC info */
3701 ecc = &p->ecc_info[0];
3702
3703 if (ecc->codeword_size >= 9) {
3704 chip->ecc_strength_ds = ecc->ecc_bits;
3705 chip->ecc_step_ds = 1 << ecc->codeword_size;
3706 } else {
3707 pr_warn("Invalid codeword size\n");
3708 }
3709
3710 return 1;
3711}
3712
3713/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003714 * nand_id_has_period - Check if an ID string has a given wraparound period
3715 * @id_data: the ID string
3716 * @arrlen: the length of the @id_data array
3717 * @period: the period of repitition
3718 *
3719 * Check if an ID string is repeated within a given sequence of bytes at
3720 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003721 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003722 * if the repetition has a period of @period; otherwise, returns zero.
3723 */
3724static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3725{
3726 int i, j;
3727 for (i = 0; i < period; i++)
3728 for (j = i + period; j < arrlen; j += period)
3729 if (id_data[i] != id_data[j])
3730 return 0;
3731 return 1;
3732}
3733
3734/*
3735 * nand_id_len - Get the length of an ID string returned by CMD_READID
3736 * @id_data: the ID string
3737 * @arrlen: the length of the @id_data array
3738
3739 * Returns the length of the ID string, according to known wraparound/trailing
3740 * zero patterns. If no pattern exists, returns the length of the array.
3741 */
3742static int nand_id_len(u8 *id_data, int arrlen)
3743{
3744 int last_nonzero, period;
3745
3746 /* Find last non-zero byte */
3747 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3748 if (id_data[last_nonzero])
3749 break;
3750
3751 /* All zeros */
3752 if (last_nonzero < 0)
3753 return 0;
3754
3755 /* Calculate wraparound period */
3756 for (period = 1; period < arrlen; period++)
3757 if (nand_id_has_period(id_data, arrlen, period))
3758 break;
3759
3760 /* There's a repeated pattern */
3761 if (period < arrlen)
3762 return period;
3763
3764 /* There are trailing zeros */
3765 if (last_nonzero < arrlen - 1)
3766 return last_nonzero + 1;
3767
3768 /* No pattern detected */
3769 return arrlen;
3770}
3771
Huang Shijie7db906b2013-09-25 14:58:11 +08003772/* Extract the bits of per cell from the 3rd byte of the extended ID */
3773static int nand_get_bits_per_cell(u8 cellinfo)
3774{
3775 int bits;
3776
3777 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3778 bits >>= NAND_CI_CELLTYPE_SHIFT;
3779 return bits + 1;
3780}
3781
Brian Norrise3b88bd2012-09-24 20:40:52 -07003782/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003783 * Many new NAND share similar device ID codes, which represent the size of the
3784 * chip. The rest of the parameters must be decoded according to generic or
3785 * manufacturer-specific "extended ID" decoding patterns.
3786 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003787void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003788{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003789 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003790 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003791 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003792 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003793 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003794 /* The 4th id byte is the important one */
3795 extid = id_data[3];
3796
Boris Brezillon01389b62016-06-08 10:30:18 +02003797 /* Calc pagesize */
3798 mtd->writesize = 1024 << (extid & 0x03);
3799 extid >>= 2;
3800 /* Calc oobsize */
3801 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3802 extid >>= 2;
3803 /* Calc blocksize. Blocksize is multiples of 64KiB */
3804 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3805 extid >>= 2;
3806 /* Get buswidth information */
3807 if (extid & 0x1)
3808 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003809}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003810EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003811
3812/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003813 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3814 * decodes a matching ID table entry and assigns the MTD size parameters for
3815 * the chip.
3816 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003817static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003818{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003819 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003820
3821 mtd->erasesize = type->erasesize;
3822 mtd->writesize = type->pagesize;
3823 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003824
Huang Shijie1c195e92013-09-25 14:58:12 +08003825 /* All legacy ID NAND are small-page, SLC */
3826 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003827}
3828
3829/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003830 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3831 * heuristic patterns using various detected parameters (e.g., manufacturer,
3832 * page size, cell-type information).
3833 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003834static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003835{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003836 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003837
3838 /* Set the bad block position */
3839 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3840 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3841 else
3842 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003843}
3844
Huang Shijieec6e87e2013-03-15 11:01:00 +08003845static inline bool is_full_id_nand(struct nand_flash_dev *type)
3846{
3847 return type->id_len;
3848}
3849
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003850static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003851 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003852{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003853 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003854 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003855
Huang Shijieec6e87e2013-03-15 11:01:00 +08003856 if (!strncmp(type->id, id_data, type->id_len)) {
3857 mtd->writesize = type->pagesize;
3858 mtd->erasesize = type->erasesize;
3859 mtd->oobsize = type->oobsize;
3860
Huang Shijie7db906b2013-09-25 14:58:11 +08003861 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003862 chip->chipsize = (uint64_t)type->chipsize << 20;
3863 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003864 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3865 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003866 chip->onfi_timing_mode_default =
3867 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003868
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003869 if (!mtd->name)
3870 mtd->name = type->name;
3871
Huang Shijieec6e87e2013-03-15 11:01:00 +08003872 return true;
3873 }
3874 return false;
3875}
3876
Brian Norris7e74c2d2012-09-24 20:40:49 -07003877/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003878 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3879 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3880 * table.
3881 */
3882static void nand_manufacturer_detect(struct nand_chip *chip)
3883{
3884 /*
3885 * Try manufacturer detection if available and use
3886 * nand_decode_ext_id() otherwise.
3887 */
3888 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3889 chip->manufacturer.desc->ops->detect)
3890 chip->manufacturer.desc->ops->detect(chip);
3891 else
3892 nand_decode_ext_id(chip);
3893}
3894
3895/*
3896 * Manufacturer initialization. This function is called for all NANDs including
3897 * ONFI and JEDEC compliant ones.
3898 * Manufacturer drivers should put all their specific initialization code in
3899 * their ->init() hook.
3900 */
3901static int nand_manufacturer_init(struct nand_chip *chip)
3902{
3903 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3904 !chip->manufacturer.desc->ops->init)
3905 return 0;
3906
3907 return chip->manufacturer.desc->ops->init(chip);
3908}
3909
3910/*
3911 * Manufacturer cleanup. This function is called for all NANDs including
3912 * ONFI and JEDEC compliant ones.
3913 * Manufacturer drivers should put all their specific cleanup code in their
3914 * ->cleanup() hook.
3915 */
3916static void nand_manufacturer_cleanup(struct nand_chip *chip)
3917{
3918 /* Release manufacturer private data */
3919 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3920 chip->manufacturer.desc->ops->cleanup)
3921 chip->manufacturer.desc->ops->cleanup(chip);
3922}
3923
3924/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003925 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003926 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02003927static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003928{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003929 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003930 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08003931 int busw;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003932 int i, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003933 u8 *id_data = chip->id.data;
3934 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Karl Beldanef89a882008-09-15 14:37:29 +02003936 /*
3937 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003938 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003939 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003940 nand_reset(chip, 0);
3941
3942 /* Select the device */
3943 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02003944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003946 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003949 maf_id = chip->read_byte(mtd);
3950 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Brian Norris8b6e50c2011-05-25 14:59:01 -07003952 /*
3953 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003954 * interface concerns can cause random data which looks like a
3955 * possibly credible NAND flash to appear. If the two results do
3956 * not match, ignore the device completely.
3957 */
3958
3959 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3960
Brian Norris4aef9b72012-09-24 20:40:48 -07003961 /* Read entire ID string */
3962 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003963 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003964
Boris Brezillon7f501f02016-05-24 19:20:05 +02003965 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003966 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003967 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003968 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01003969 }
3970
Boris Brezillon7f501f02016-05-24 19:20:05 +02003971 chip->id.len = nand_id_len(id_data, 8);
3972
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003973 /* Try to identify manufacturer */
3974 manufacturer = nand_get_manufacturer(maf_id);
3975 chip->manufacturer.desc = manufacturer;
3976
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003977 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003978 type = nand_flash_ids;
3979
Boris Brezillon29a198a2016-05-24 20:17:48 +02003980 /*
3981 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
3982 * override it.
3983 * This is required to make sure initial NAND bus width set by the
3984 * NAND controller driver is coherent with the real NAND bus width
3985 * (extracted by auto-detection code).
3986 */
3987 busw = chip->options & NAND_BUSWIDTH_16;
3988
3989 /*
3990 * The flag is only set (never cleared), reset it to its default value
3991 * before starting auto-detection.
3992 */
3993 chip->options &= ~NAND_BUSWIDTH_16;
3994
Huang Shijieec6e87e2013-03-15 11:01:00 +08003995 for (; type->name != NULL; type++) {
3996 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003997 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08003998 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003999 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004000 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004001 }
4002 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004003
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004004 chip->onfi_version = 0;
4005 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004006 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004007 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004008 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004009
4010 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004011 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004012 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004013 }
4014
David Woodhouse5e81e882010-02-26 18:32:56 +00004015 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004016 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004017
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004018 if (!mtd->name)
4019 mtd->name = type->name;
4020
Adrian Hunter69423d92008-12-10 13:37:21 +00004021 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004022
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004023 if (!type->pagesize)
4024 nand_manufacturer_detect(chip);
4025 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004026 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004027
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004028 /* Get chip options */
4029 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004030
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004031ident_done:
4032
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004033 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004034 WARN_ON(busw & NAND_BUSWIDTH_16);
4035 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004036 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4037 /*
4038 * Check, if buswidth is correct. Hardware drivers should set
4039 * chip correct!
4040 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004041 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004042 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004043 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4044 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004045 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4046 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004047 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004048 }
4049
Boris Brezillon7f501f02016-05-24 19:20:05 +02004050 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004051
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004052 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004053 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004054 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004055 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004056
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004057 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004058 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004059 if (chip->chipsize & 0xffffffff)
4060 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004061 else {
4062 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4063 chip->chip_shift += 32 - 1;
4064 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004065
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004066 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004067 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004068
Brian Norris8b6e50c2011-05-25 14:59:01 -07004069 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004070 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4071 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004072
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004073 ret = nand_manufacturer_init(chip);
4074 if (ret)
4075 return ret;
4076
Ezequiel Garcia20171642013-11-25 08:30:31 -03004077 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004078 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004079
4080 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004081 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4082 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004083 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004084 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4085 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004086 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004087 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4088 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004089
Rafał Miłecki3755a992014-10-21 00:01:04 +02004090 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004091 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004092 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004093 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004094}
4095
Boris Brezillond48f62b2016-04-01 14:54:32 +02004096static const char * const nand_ecc_modes[] = {
4097 [NAND_ECC_NONE] = "none",
4098 [NAND_ECC_SOFT] = "soft",
4099 [NAND_ECC_HW] = "hw",
4100 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4101 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004102};
4103
4104static int of_get_nand_ecc_mode(struct device_node *np)
4105{
4106 const char *pm;
4107 int err, i;
4108
4109 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4110 if (err < 0)
4111 return err;
4112
4113 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4114 if (!strcasecmp(pm, nand_ecc_modes[i]))
4115 return i;
4116
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004117 /*
4118 * For backward compatibility we support few obsoleted values that don't
4119 * have their mappings into nand_ecc_modes_t anymore (they were merged
4120 * with other enums).
4121 */
4122 if (!strcasecmp(pm, "soft_bch"))
4123 return NAND_ECC_SOFT;
4124
Boris Brezillond48f62b2016-04-01 14:54:32 +02004125 return -ENODEV;
4126}
4127
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004128static const char * const nand_ecc_algos[] = {
4129 [NAND_ECC_HAMMING] = "hamming",
4130 [NAND_ECC_BCH] = "bch",
4131};
4132
Boris Brezillond48f62b2016-04-01 14:54:32 +02004133static int of_get_nand_ecc_algo(struct device_node *np)
4134{
4135 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004136 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004137
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004138 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4139 if (!err) {
4140 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4141 if (!strcasecmp(pm, nand_ecc_algos[i]))
4142 return i;
4143 return -ENODEV;
4144 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004145
4146 /*
4147 * For backward compatibility we also read "nand-ecc-mode" checking
4148 * for some obsoleted values that were specifying ECC algorithm.
4149 */
4150 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4151 if (err < 0)
4152 return err;
4153
4154 if (!strcasecmp(pm, "soft"))
4155 return NAND_ECC_HAMMING;
4156 else if (!strcasecmp(pm, "soft_bch"))
4157 return NAND_ECC_BCH;
4158
4159 return -ENODEV;
4160}
4161
4162static int of_get_nand_ecc_step_size(struct device_node *np)
4163{
4164 int ret;
4165 u32 val;
4166
4167 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4168 return ret ? ret : val;
4169}
4170
4171static int of_get_nand_ecc_strength(struct device_node *np)
4172{
4173 int ret;
4174 u32 val;
4175
4176 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4177 return ret ? ret : val;
4178}
4179
4180static int of_get_nand_bus_width(struct device_node *np)
4181{
4182 u32 val;
4183
4184 if (of_property_read_u32(np, "nand-bus-width", &val))
4185 return 8;
4186
4187 switch (val) {
4188 case 8:
4189 case 16:
4190 return val;
4191 default:
4192 return -EIO;
4193 }
4194}
4195
4196static bool of_get_nand_on_flash_bbt(struct device_node *np)
4197{
4198 return of_property_read_bool(np, "nand-on-flash-bbt");
4199}
4200
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004201static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004202{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004203 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004204 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004205
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004206 if (!dn)
4207 return 0;
4208
Brian Norris5844fee2015-01-23 00:22:27 -08004209 if (of_get_nand_bus_width(dn) == 16)
4210 chip->options |= NAND_BUSWIDTH_16;
4211
4212 if (of_get_nand_on_flash_bbt(dn))
4213 chip->bbt_options |= NAND_BBT_USE_FLASH;
4214
4215 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004216 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004217 ecc_strength = of_get_nand_ecc_strength(dn);
4218 ecc_step = of_get_nand_ecc_step_size(dn);
4219
Brian Norris5844fee2015-01-23 00:22:27 -08004220 if (ecc_mode >= 0)
4221 chip->ecc.mode = ecc_mode;
4222
Rafał Miłecki79082452016-03-23 11:19:02 +01004223 if (ecc_algo >= 0)
4224 chip->ecc.algo = ecc_algo;
4225
Brian Norris5844fee2015-01-23 00:22:27 -08004226 if (ecc_strength >= 0)
4227 chip->ecc.strength = ecc_strength;
4228
4229 if (ecc_step > 0)
4230 chip->ecc.size = ecc_step;
4231
Boris Brezillonba78ee02016-06-08 17:04:22 +02004232 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4233 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4234
Brian Norris5844fee2015-01-23 00:22:27 -08004235 return 0;
4236}
4237
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004238/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004239 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004240 * @mtd: MTD device structure
4241 * @maxchips: number of chips to scan for
4242 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004243 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004244 * This is the first phase of the normal nand_scan() function. It reads the
4245 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004246 *
4247 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004248int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4249 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004250{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004251 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004252 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004253 int ret;
4254
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004255 ret = nand_dt_init(chip);
4256 if (ret)
4257 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004258
Brian Norrisf7a8e382016-01-05 10:39:45 -08004259 if (!mtd->name && mtd->dev.parent)
4260 mtd->name = dev_name(mtd->dev.parent);
4261
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004262 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4263 /*
4264 * Default functions assigned for chip_select() and
4265 * cmdfunc() both expect cmd_ctrl() to be populated,
4266 * so we need to check that that's the case
4267 */
4268 pr_err("chip.cmd_ctrl() callback is not provided");
4269 return -EINVAL;
4270 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004271 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004272 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004273
4274 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004275 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004276 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004277 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004278 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004279 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004280 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
4282
Boris Brezillon73f907f2016-10-24 16:46:20 +02004283 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004284 ret = nand_init_data_interface(chip);
4285 if (ret)
4286 return ret;
4287
Boris Brezillon73f907f2016-10-24 16:46:20 +02004288 /*
4289 * Setup the data interface correctly on the chip and controller side.
4290 * This explicit call to nand_setup_data_interface() is only required
4291 * for the first die, because nand_reset() has been called before
4292 * ->data_interface and ->default_onfi_timing_mode were set.
4293 * For the other dies, nand_reset() will automatically switch to the
4294 * best mode for us.
4295 */
4296 ret = nand_setup_data_interface(chip);
4297 if (ret)
4298 return ret;
4299
Boris Brezillon7f501f02016-05-24 19:20:05 +02004300 nand_maf_id = chip->id.data[0];
4301 nand_dev_id = chip->id.data[1];
4302
Huang Shijie07300162012-11-09 16:23:45 +08004303 chip->select_chip(mtd, -1);
4304
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004305 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004306 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004307 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004308 nand_reset(chip, i);
4309
4310 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004312 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004314 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004315 nand_dev_id != chip->read_byte(mtd)) {
4316 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 break;
Huang Shijie07300162012-11-09 16:23:45 +08004318 }
4319 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
4321 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004322 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004325 chip->numchips = i;
4326 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
David Woodhouse3b85c322006-09-25 17:06:53 +01004328 return 0;
4329}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004330EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004331
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004332static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4333{
4334 struct nand_chip *chip = mtd_to_nand(mtd);
4335 struct nand_ecc_ctrl *ecc = &chip->ecc;
4336
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004337 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004338 return -EINVAL;
4339
4340 switch (ecc->algo) {
4341 case NAND_ECC_HAMMING:
4342 ecc->calculate = nand_calculate_ecc;
4343 ecc->correct = nand_correct_data;
4344 ecc->read_page = nand_read_page_swecc;
4345 ecc->read_subpage = nand_read_subpage;
4346 ecc->write_page = nand_write_page_swecc;
4347 ecc->read_page_raw = nand_read_page_raw;
4348 ecc->write_page_raw = nand_write_page_raw;
4349 ecc->read_oob = nand_read_oob_std;
4350 ecc->write_oob = nand_write_oob_std;
4351 if (!ecc->size)
4352 ecc->size = 256;
4353 ecc->bytes = 3;
4354 ecc->strength = 1;
4355 return 0;
4356 case NAND_ECC_BCH:
4357 if (!mtd_nand_has_bch()) {
4358 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4359 return -EINVAL;
4360 }
4361 ecc->calculate = nand_bch_calculate_ecc;
4362 ecc->correct = nand_bch_correct_data;
4363 ecc->read_page = nand_read_page_swecc;
4364 ecc->read_subpage = nand_read_subpage;
4365 ecc->write_page = nand_write_page_swecc;
4366 ecc->read_page_raw = nand_read_page_raw;
4367 ecc->write_page_raw = nand_write_page_raw;
4368 ecc->read_oob = nand_read_oob_std;
4369 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004370
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004371 /*
4372 * Board driver should supply ecc.size and ecc.strength
4373 * values to select how many bits are correctable.
4374 * Otherwise, default to 4 bits for large page devices.
4375 */
4376 if (!ecc->size && (mtd->oobsize >= 64)) {
4377 ecc->size = 512;
4378 ecc->strength = 4;
4379 }
4380
4381 /*
4382 * if no ecc placement scheme was provided pickup the default
4383 * large page one.
4384 */
4385 if (!mtd->ooblayout) {
4386 /* handle large page devices only */
4387 if (mtd->oobsize < 64) {
4388 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4389 return -EINVAL;
4390 }
4391
4392 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004393
4394 }
4395
4396 /*
4397 * We can only maximize ECC config when the default layout is
4398 * used, otherwise we don't know how many bytes can really be
4399 * used.
4400 */
4401 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4402 ecc->options & NAND_ECC_MAXIMIZE) {
4403 int steps, bytes;
4404
4405 /* Always prefer 1k blocks over 512bytes ones */
4406 ecc->size = 1024;
4407 steps = mtd->writesize / ecc->size;
4408
4409 /* Reserve 2 bytes for the BBM */
4410 bytes = (mtd->oobsize - 2) / steps;
4411 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004412 }
4413
4414 /* See nand_bch_init() for details. */
4415 ecc->bytes = 0;
4416 ecc->priv = nand_bch_init(mtd);
4417 if (!ecc->priv) {
4418 WARN(1, "BCH ECC initialization failed!\n");
4419 return -EINVAL;
4420 }
4421 return 0;
4422 default:
4423 WARN(1, "Unsupported ECC algorithm!\n");
4424 return -EINVAL;
4425 }
4426}
4427
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004428/*
4429 * Check if the chip configuration meet the datasheet requirements.
4430
4431 * If our configuration corrects A bits per B bytes and the minimum
4432 * required correction level is X bits per Y bytes, then we must ensure
4433 * both of the following are true:
4434 *
4435 * (1) A / B >= X / Y
4436 * (2) A >= X
4437 *
4438 * Requirement (1) ensures we can correct for the required bitflip density.
4439 * Requirement (2) ensures we can correct even when all bitflips are clumped
4440 * in the same sector.
4441 */
4442static bool nand_ecc_strength_good(struct mtd_info *mtd)
4443{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004444 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004445 struct nand_ecc_ctrl *ecc = &chip->ecc;
4446 int corr, ds_corr;
4447
4448 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4449 /* Not enough information */
4450 return true;
4451
4452 /*
4453 * We get the number of corrected bits per page to compare
4454 * the correction density.
4455 */
4456 corr = (mtd->writesize * ecc->strength) / ecc->size;
4457 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4458
4459 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4460}
David Woodhouse3b85c322006-09-25 17:06:53 +01004461
Marc Gonzalez3371d662016-11-15 10:56:20 +01004462static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4463{
4464 struct nand_ecc_ctrl *ecc = &chip->ecc;
4465
4466 if (nand_standard_page_accessors(ecc))
4467 return false;
4468
4469 /*
4470 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4471 * controller driver implements all the page accessors because
4472 * default helpers are not suitable when the core does not
4473 * send the READ0/PAGEPROG commands.
4474 */
4475 return (!ecc->read_page || !ecc->write_page ||
4476 !ecc->read_page_raw || !ecc->write_page_raw ||
4477 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4478 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4479 ecc->hwctl && ecc->calculate));
4480}
4481
David Woodhouse3b85c322006-09-25 17:06:53 +01004482/**
4483 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004484 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004485 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004486 * This is the second phase of the normal nand_scan() function. It fills out
4487 * all the uninitialized function pointers with the defaults and scans for a
4488 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004489 */
4490int nand_scan_tail(struct mtd_info *mtd)
4491{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004492 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004493 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004494 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004495 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004496
Brian Norrise2414f42012-02-06 13:44:00 -08004497 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004498 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4499 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4500 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004501
Marc Gonzalez3371d662016-11-15 10:56:20 +01004502 if (invalid_ecc_page_accessors(chip)) {
4503 pr_err("Invalid ECC page accessors setup\n");
4504 return -EINVAL;
4505 }
4506
Huang Shijief02ea4e2014-01-13 14:27:12 +08004507 if (!(chip->options & NAND_OWN_BUFFERS)) {
4508 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4509 + mtd->oobsize * 3, GFP_KERNEL);
4510 if (!nbuf)
4511 return -ENOMEM;
4512 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4513 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4514 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4515
4516 chip->buffers = nbuf;
4517 } else {
4518 if (!chip->buffers)
4519 return -ENOMEM;
4520 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004521
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004522 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004523 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004524
4525 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004526 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004527 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004528 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004529 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004530 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004533 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 break;
4535 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004536 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004537 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004540 WARN(1, "No oob scheme defined for oobsize %d\n",
4541 mtd->oobsize);
4542 ret = -EINVAL;
4543 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 }
4545 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004546
David Woodhouse956e9442006-09-25 17:12:39 +01004547 if (!chip->write_page)
4548 chip->write_page = nand_write_page;
4549
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004550 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004551 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004552 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004553 */
David Woodhouse956e9442006-09-25 17:12:39 +01004554
Huang Shijie97de79e02013-10-18 14:20:53 +08004555 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004556 case NAND_ECC_HW_OOB_FIRST:
4557 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004558 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004559 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4560 ret = -EINVAL;
4561 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004562 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004563 if (!ecc->read_page)
4564 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004565
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004566 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004567 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004568 if (!ecc->read_page)
4569 ecc->read_page = nand_read_page_hwecc;
4570 if (!ecc->write_page)
4571 ecc->write_page = nand_write_page_hwecc;
4572 if (!ecc->read_page_raw)
4573 ecc->read_page_raw = nand_read_page_raw;
4574 if (!ecc->write_page_raw)
4575 ecc->write_page_raw = nand_write_page_raw;
4576 if (!ecc->read_oob)
4577 ecc->read_oob = nand_read_oob_std;
4578 if (!ecc->write_oob)
4579 ecc->write_oob = nand_write_oob_std;
4580 if (!ecc->read_subpage)
4581 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004582 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004583 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004584
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004585 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004586 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4587 (!ecc->read_page ||
4588 ecc->read_page == nand_read_page_hwecc ||
4589 !ecc->write_page ||
4590 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004591 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4592 ret = -EINVAL;
4593 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004594 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004595 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004596 if (!ecc->read_page)
4597 ecc->read_page = nand_read_page_syndrome;
4598 if (!ecc->write_page)
4599 ecc->write_page = nand_write_page_syndrome;
4600 if (!ecc->read_page_raw)
4601 ecc->read_page_raw = nand_read_page_raw_syndrome;
4602 if (!ecc->write_page_raw)
4603 ecc->write_page_raw = nand_write_page_raw_syndrome;
4604 if (!ecc->read_oob)
4605 ecc->read_oob = nand_read_oob_syndrome;
4606 if (!ecc->write_oob)
4607 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004608
Huang Shijie97de79e02013-10-18 14:20:53 +08004609 if (mtd->writesize >= ecc->size) {
4610 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004611 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4612 ret = -EINVAL;
4613 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004614 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004615 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004616 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004617 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4618 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004619 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004620 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004622 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004623 ret = nand_set_ecc_soft_ops(mtd);
4624 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004625 ret = -EINVAL;
4626 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004627 }
4628 break;
4629
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004630 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004631 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004632 ecc->read_page = nand_read_page_raw;
4633 ecc->write_page = nand_write_page_raw;
4634 ecc->read_oob = nand_read_oob_std;
4635 ecc->read_page_raw = nand_read_page_raw;
4636 ecc->write_page_raw = nand_write_page_raw;
4637 ecc->write_oob = nand_write_oob_std;
4638 ecc->size = mtd->writesize;
4639 ecc->bytes = 0;
4640 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004644 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4645 ret = -EINVAL;
4646 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Brian Norris9ce244b2011-08-30 18:45:37 -07004649 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004650 if (!ecc->read_oob_raw)
4651 ecc->read_oob_raw = ecc->read_oob;
4652 if (!ecc->write_oob_raw)
4653 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004654
Boris Brezillon846031d2016-02-03 20:11:00 +01004655 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004656 mtd->ecc_strength = ecc->strength;
4657 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004658
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004659 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004660 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004661 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004662 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004663 ecc->steps = mtd->writesize / ecc->size;
4664 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004665 WARN(1, "Invalid ECC parameters\n");
4666 ret = -EINVAL;
4667 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004669 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004670
Boris Brezillon846031d2016-02-03 20:11:00 +01004671 /*
4672 * The number of bytes available for a client to place data into
4673 * the out of band area.
4674 */
4675 ret = mtd_ooblayout_count_freebytes(mtd);
4676 if (ret < 0)
4677 ret = 0;
4678
4679 mtd->oobavail = ret;
4680
4681 /* ECC sanity check: warn if it's too weak */
4682 if (!nand_ecc_strength_good(mtd))
4683 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4684 mtd->name);
4685
Brian Norris8b6e50c2011-05-25 14:59:01 -07004686 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004687 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004688 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004689 case 2:
4690 mtd->subpage_sft = 1;
4691 break;
4692 case 4:
4693 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004694 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004695 mtd->subpage_sft = 2;
4696 break;
4697 }
4698 }
4699 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4700
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004701 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004702 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004705 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004707 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304708 switch (ecc->mode) {
4709 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304710 if (chip->page_shift > 9)
4711 chip->options |= NAND_SUBPAGE_READ;
4712 break;
4713
4714 default:
4715 break;
4716 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004719 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004720 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4721 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004722 mtd->_erase = nand_erase;
4723 mtd->_point = NULL;
4724 mtd->_unpoint = NULL;
4725 mtd->_read = nand_read;
4726 mtd->_write = nand_write;
4727 mtd->_panic_write = panic_nand_write;
4728 mtd->_read_oob = nand_read_oob;
4729 mtd->_write_oob = nand_write_oob;
4730 mtd->_sync = nand_sync;
4731 mtd->_lock = NULL;
4732 mtd->_unlock = NULL;
4733 mtd->_suspend = nand_suspend;
4734 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004735 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004736 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004737 mtd->_block_isbad = nand_block_isbad;
4738 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004739 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004740 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004742 /*
4743 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4744 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4745 * properly set.
4746 */
4747 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004748 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004750 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004751 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
4754 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004755 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004756err_free:
4757 if (!(chip->options & NAND_OWN_BUFFERS))
4758 kfree(chip->buffers);
4759 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004761EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Brian Norris8b6e50c2011-05-25 14:59:01 -07004763/*
4764 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004765 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004766 * to call us from in-kernel code if the core NAND support is modular.
4767 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004768#ifdef MODULE
4769#define caller_is_module() (1)
4770#else
4771#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004772 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004773#endif
4774
4775/**
4776 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004777 * @mtd: MTD device structure
4778 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004779 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004780 * This fills out all the uninitialized function pointers with the defaults.
4781 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004782 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004783 */
4784int nand_scan(struct mtd_info *mtd, int maxchips)
4785{
4786 int ret;
4787
David Woodhouse5e81e882010-02-26 18:32:56 +00004788 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004789 if (!ret)
4790 ret = nand_scan_tail(mtd);
4791 return ret;
4792}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004793EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004796 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4797 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004798 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004799void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004801 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004802 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004803 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4804
Boris Brezillond8e725d2016-09-15 10:32:50 +02004805 nand_release_data_interface(chip);
4806
Jesper Juhlfa671642005-11-07 01:01:27 -08004807 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004808 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004809 if (!(chip->options & NAND_OWN_BUFFERS))
4810 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004811
4812 /* Free bad block descriptor memory */
4813 if (chip->badblock_pattern && chip->badblock_pattern->options
4814 & NAND_BBT_DYNAMICSTRUCT)
4815 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004816
4817 /* Free manufacturer priv data. */
4818 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004820EXPORT_SYMBOL_GPL(nand_cleanup);
4821
4822/**
4823 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4824 * held by the NAND device
4825 * @mtd: MTD device structure
4826 */
4827void nand_release(struct mtd_info *mtd)
4828{
4829 mtd_device_unregister(mtd);
4830 nand_cleanup(mtd_to_nand(mtd));
4831}
David Woodhousee0c7d762006-05-13 18:07:53 +01004832EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004833
David Woodhousee0c7d762006-05-13 18:07:53 +01004834MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004835MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4836MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004837MODULE_DESCRIPTION("Generic NAND flash driver code");