blob: 6669dfcf9e794e1d9727bb6e58097fdb5ac7e65a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020048#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Huang Shijie6a8214a2012-11-19 14:43:30 +080050static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020052static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
53 struct mtd_oob_ops *ops);
54
Boris Brezillon41b207a2016-02-03 19:06:15 +010055/* Define default oob placement schemes for large and small page devices */
56static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
57 struct mtd_oob_region *oobregion)
58{
59 struct nand_chip *chip = mtd_to_nand(mtd);
60 struct nand_ecc_ctrl *ecc = &chip->ecc;
61
62 if (section > 1)
63 return -ERANGE;
64
65 if (!section) {
66 oobregion->offset = 0;
67 oobregion->length = 4;
68 } else {
69 oobregion->offset = 6;
70 oobregion->length = ecc->total - 4;
71 }
72
73 return 0;
74}
75
76static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
77 struct mtd_oob_region *oobregion)
78{
79 if (section > 1)
80 return -ERANGE;
81
82 if (mtd->oobsize == 16) {
83 if (section)
84 return -ERANGE;
85
86 oobregion->length = 8;
87 oobregion->offset = 8;
88 } else {
89 oobregion->length = 2;
90 if (!section)
91 oobregion->offset = 3;
92 else
93 oobregion->offset = 6;
94 }
95
96 return 0;
97}
98
99const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
100 .ecc = nand_ooblayout_ecc_sp,
101 .free = nand_ooblayout_free_sp,
102};
103EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
104
105static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
106 struct mtd_oob_region *oobregion)
107{
108 struct nand_chip *chip = mtd_to_nand(mtd);
109 struct nand_ecc_ctrl *ecc = &chip->ecc;
110
111 if (section)
112 return -ERANGE;
113
114 oobregion->length = ecc->total;
115 oobregion->offset = mtd->oobsize - oobregion->length;
116
117 return 0;
118}
119
120static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
121 struct mtd_oob_region *oobregion)
122{
123 struct nand_chip *chip = mtd_to_nand(mtd);
124 struct nand_ecc_ctrl *ecc = &chip->ecc;
125
126 if (section)
127 return -ERANGE;
128
129 oobregion->length = mtd->oobsize - ecc->total - 2;
130 oobregion->offset = 2;
131
132 return 0;
133}
134
135const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
136 .ecc = nand_ooblayout_ecc_lp,
137 .free = nand_ooblayout_free_lp,
138};
139EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200140
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530141static int check_offs_len(struct mtd_info *mtd,
142 loff_t ofs, uint64_t len)
143{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100144 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530145 int ret = 0;
146
147 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300148 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700149 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530150 ret = -EINVAL;
151 }
152
153 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300154 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700155 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530156 ret = -EINVAL;
157 }
158
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530159 return ret;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700164 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000165 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800166 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100168static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100170 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200172 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200173 spin_lock(&chip->controller->lock);
174 chip->controller->active = NULL;
175 chip->state = FL_READY;
176 wake_up(&chip->controller->wq);
177 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/**
181 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700182 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700184 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200186static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100188 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/**
Masanari Iida064a7692012-11-09 23:20:58 +0900193 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700194 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700196 * Default read function for 16bit buswidth with endianness conversion.
197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200199static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100201 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200202 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700207 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700209 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
211static u16 nand_read_word(struct mtd_info *mtd)
212{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100213 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200214 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700219 * @mtd: MTD device structure
220 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
222 * Default select function for 1 chip devices.
223 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100226 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200227
228 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200230 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234
235 default:
236 BUG();
237 }
238}
239
240/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100241 * nand_write_byte - [DEFAULT] write single byte to chip
242 * @mtd: MTD device structure
243 * @byte: value to write
244 *
245 * Default function to write a byte to I/O[7:0]
246 */
247static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
248{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100249 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100250
251 chip->write_buf(mtd, &byte, 1);
252}
253
254/**
255 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
256 * @mtd: MTD device structure
257 * @byte: value to write
258 *
259 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
260 */
261static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100264 uint16_t word = byte;
265
266 /*
267 * It's not entirely clear what should happen to I/O[15:8] when writing
268 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
269 *
270 * When the host supports a 16-bit bus width, only data is
271 * transferred at the 16-bit width. All address and command line
272 * transfers shall use only the lower 8-bits of the data bus. During
273 * command transfers, the host may place any value on the upper
274 * 8-bits of the data bus. During address transfers, the host shall
275 * set the upper 8-bits of the data bus to 00h.
276 *
277 * One user of the write_byte callback is nand_onfi_set_features. The
278 * four parameters are specified to be written to I/O[7:0], but this is
279 * neither an address nor a command transfer. Let's assume a 0 on the
280 * upper I/O lines is OK.
281 */
282 chip->write_buf(mtd, (uint8_t *)&word, 2);
283}
284
285/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700287 * @mtd: MTD device structure
288 * @buf: data buffer
289 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700291 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200293static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100295 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Alexander Shiyan76413832013-04-13 09:32:13 +0400297 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000301 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700302 * @mtd: MTD device structure
303 * @buf: buffer to store date
304 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700306 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200308static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100310 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Alexander Shiyan76413832013-04-13 09:32:13 +0400312 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700317 * @mtd: MTD device structure
318 * @buf: data buffer
319 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700321 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200323static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100325 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000327
Alexander Shiyan76413832013-04-13 09:32:13 +0400328 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000332 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700333 * @mtd: MTD device structure
334 * @buf: buffer to store date
335 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700337 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200339static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100341 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Alexander Shiyan76413832013-04-13 09:32:13 +0400344 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700349 * @mtd: MTD device structure
350 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000352 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530354static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Archit Taneja9f3e0422016-02-03 14:29:49 +0530356 int page, res = 0, i = 0;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100357 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 u16 bad;
359
Brian Norris5fb15492011-05-31 16:31:21 -0700360 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700361 ofs += mtd->erasesize - mtd->writesize;
362
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100363 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
364
Brian Norriscdbec052012-01-13 18:11:48 -0800365 do {
366 if (chip->options & NAND_BUSWIDTH_16) {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB,
368 chip->badblockpos & 0xFE, page);
369 bad = cpu_to_le16(chip->read_word(mtd));
370 if (chip->badblockpos & 0x1)
371 bad >>= 8;
372 else
373 bad &= 0xFF;
374 } else {
375 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
376 page);
377 bad = chip->read_byte(mtd);
378 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379
Brian Norriscdbec052012-01-13 18:11:48 -0800380 if (likely(chip->badblockbits == 8))
381 res = bad != 0xFF;
382 else
383 res = hweight8(bad) < chip->badblockbits;
384 ofs += mtd->writesize;
385 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
386 i++;
387 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return res;
390}
391
392/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700393 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700394 * @mtd: MTD device structure
395 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700397 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700398 * specific driver. It provides the details for writing a bad block marker to a
399 * block.
400 */
401static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
402{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100403 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700404 struct mtd_oob_ops ops;
405 uint8_t buf[2] = { 0, 0 };
406 int ret = 0, res, i = 0;
407
Brian Norris0ec56dc2015-02-28 02:02:30 -0800408 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700409 ops.oobbuf = buf;
410 ops.ooboffs = chip->badblockpos;
411 if (chip->options & NAND_BUSWIDTH_16) {
412 ops.ooboffs &= ~0x01;
413 ops.len = ops.ooblen = 2;
414 } else {
415 ops.len = ops.ooblen = 1;
416 }
417 ops.mode = MTD_OPS_PLACE_OOB;
418
419 /* Write to first/last page(s) if necessary */
420 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
421 ofs += mtd->erasesize - mtd->writesize;
422 do {
423 res = nand_do_write_oob(mtd, ofs, &ops);
424 if (!ret)
425 ret = res;
426
427 i++;
428 ofs += mtd->writesize;
429 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
430
431 return ret;
432}
433
434/**
435 * nand_block_markbad_lowlevel - mark a block bad
436 * @mtd: MTD device structure
437 * @ofs: offset from device start
438 *
439 * This function performs the generic NAND bad block marking steps (i.e., bad
440 * block table(s) and/or marker(s)). We only allow the hardware driver to
441 * specify how to write bad block markers to OOB (chip->block_markbad).
442 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700443 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800444 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700445 * (2) write bad block marker to OOB area of affected block (unless flag
446 * NAND_BBT_NO_OOB_BBM is present)
447 * (3) update the BBT
448 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800449 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700451static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100453 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700454 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000455
Brian Norrisb32843b2013-07-30 17:52:59 -0700456 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800457 struct erase_info einfo;
458
459 /* Attempt erase before marking OOB */
460 memset(&einfo, 0, sizeof(einfo));
461 einfo.mtd = mtd;
462 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300463 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800464 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800465
Brian Norrisb32843b2013-07-30 17:52:59 -0700466 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800467 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700468 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300469 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200470 }
Brian Norrise2414f42012-02-06 13:44:00 -0800471
Brian Norrisb32843b2013-07-30 17:52:59 -0700472 /* Mark block bad in BBT */
473 if (chip->bbt) {
474 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800475 if (!ret)
476 ret = res;
477 }
478
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200479 if (!ret)
480 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300481
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000485/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 * Check, if the device is write protected. The function expects, that the
490 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100492static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100494 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200495
Brian Norris8b6e50c2011-05-25 14:59:01 -0700496 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200497 if (chip->options & NAND_BROKEN_XD)
498 return 0;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200501 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
502 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800506 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700507 * @mtd: MTD device structure
508 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300509 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800510 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300511 */
512static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
513{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100514 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300515
516 if (!chip->bbt)
517 return 0;
518 /* Return info from the table */
519 return nand_isreserved_bbt(mtd, ofs);
520}
521
522/**
523 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
524 * @mtd: MTD device structure
525 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700526 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *
528 * Check, if the block is bad. Either by reading the bad block table or
529 * calling of the scan function.
530 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530531static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100533 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000534
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200535 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530536 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100539 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200542/**
543 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700544 * @mtd: MTD device structure
545 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200546 *
547 * Helper function for nand_wait_ready used when needing to wait in interrupt
548 * context.
549 */
550static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
551{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100552 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200553 int i;
554
555 /* Wait for the device to get ready */
556 for (i = 0; i < timeo; i++) {
557 if (chip->dev_ready(mtd))
558 break;
559 touch_softlockup_watchdog();
560 mdelay(1);
561 }
562}
563
Alex Smithb70af9b2015-10-06 14:52:07 +0100564/**
565 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
566 * @mtd: MTD device structure
567 *
568 * Wait for the ready pin after a command, and warn if a timeout occurs.
569 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100570void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000571{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100572 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100573 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000574
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200575 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100576 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200577
Brian Norris7854d3f2011-06-23 14:12:08 -0700578 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100579 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000580 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200581 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300582 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100583 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100585
Brian Norris9ebfdf52016-03-04 17:19:23 -0800586 if (!chip->dev_ready(mtd))
587 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000588}
David Woodhouse4b648b02006-09-25 17:05:24 +0100589EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200592 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
593 * @mtd: MTD device structure
594 * @timeo: Timeout in ms
595 *
596 * Wait for status ready (i.e. command done) or timeout.
597 */
598static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
599{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100600 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200601
602 timeo = jiffies + msecs_to_jiffies(timeo);
603 do {
604 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
605 break;
606 touch_softlockup_watchdog();
607 } while (time_before(jiffies, timeo));
608};
609
610/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700612 * @mtd: MTD device structure
613 * @command: the command to be sent
614 * @column: the column address for this command, -1 if none
615 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700617 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200618 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200620static void nand_command(struct mtd_info *mtd, unsigned int command,
621 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100623 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (command == NAND_CMD_SEQIN) {
628 int readcmd;
629
Joern Engel28318772006-05-22 23:18:05 +0200630 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200632 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 readcmd = NAND_CMD_READOOB;
634 } else if (column < 256) {
635 /* First 256 bytes --> READ0 */
636 readcmd = NAND_CMD_READ0;
637 } else {
638 column -= 256;
639 readcmd = NAND_CMD_READ1;
640 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Brian Norris8b6e50c2011-05-25 14:59:01 -0700646 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
648 /* Serially input address */
649 if (column != -1) {
650 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800651 if (chip->options & NAND_BUSWIDTH_16 &&
652 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200655 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200657 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200658 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200659 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 if (chip->chipsize > (32 << 20))
663 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200664 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200665 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000666
667 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700668 * Program and erase have their own busy handlers status and sequential
669 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case NAND_CMD_PAGEPROG:
674 case NAND_CMD_ERASE1:
675 case NAND_CMD_ERASE2:
676 case NAND_CMD_SEQIN:
677 case NAND_CMD_STATUS:
678 return;
679
680 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200681 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 udelay(chip->chip_delay);
684 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200685 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200686 chip->cmd_ctrl(mtd,
687 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200688 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
689 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691
David Woodhousee0c7d762006-05-13 18:07:53 +0100692 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000694 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * If we don't have access to the busy pin, we apply the given
696 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100697 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200698 if (!chip->dev_ready) {
699 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700703 /*
704 * Apply this short delay always to ensure that we do wait tWB in
705 * any case on any machine.
706 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000708
709 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/**
713 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700714 * @mtd: MTD device structure
715 * @command: the command to be sent
716 * @column: the column address for this command, -1 if none
717 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200719 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700720 * devices. We don't have the separate regions as we have in the small page
721 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
724 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100726 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Emulate NAND_CMD_READOOB */
729 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200730 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 command = NAND_CMD_READ0;
732 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000733
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200734 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400735 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200738 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* Serially input address */
741 if (column != -1) {
742 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800743 if (chip->options & NAND_BUSWIDTH_16 &&
744 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200747 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200748 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200751 chip->cmd_ctrl(mtd, page_addr, ctrl);
752 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200753 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 if (chip->chipsize > (128 << 20))
756 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200757 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200760 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000761
762 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700763 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100764 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 case NAND_CMD_CACHEDPROG:
769 case NAND_CMD_PAGEPROG:
770 case NAND_CMD_ERASE1:
771 case NAND_CMD_ERASE2:
772 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200773 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000775 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200778 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200780 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200781 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
782 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
783 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
784 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200785 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
786 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return;
788
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200789 case NAND_CMD_RNDOUT:
790 /* No ready / busy check necessary */
791 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
792 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
793 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
794 NAND_NCE | NAND_CTRL_CHANGE);
795 return;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200798 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
799 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
800 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
801 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000802
David Woodhousee0c7d762006-05-13 18:07:53 +0100803 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000805 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700807 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100808 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 if (!chip->dev_ready) {
810 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000814
Brian Norris8b6e50c2011-05-25 14:59:01 -0700815 /*
816 * Apply this short delay always to ensure that we do wait tWB in
817 * any case on any machine.
818 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100819 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000820
821 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200825 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700826 * @chip: the nand chip descriptor
827 * @mtd: MTD device structure
828 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200829 *
830 * Used when in panic, no locks are taken.
831 */
832static void panic_nand_get_device(struct nand_chip *chip,
833 struct mtd_info *mtd, int new_state)
834{
Brian Norris7854d3f2011-06-23 14:12:08 -0700835 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200836 chip->controller->active = chip;
837 chip->state = new_state;
838}
839
840/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700842 * @mtd: MTD device structure
843 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *
845 * Get the device and lock it for exclusive access
846 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200847static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800848nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100850 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200851 spinlock_t *lock = &chip->controller->lock;
852 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100853 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200854retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100855 spin_lock(lock);
856
vimal singhb8b3ee92009-07-09 20:41:22 +0530857 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200858 if (!chip->controller->active)
859 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200860
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 if (chip->controller->active == chip && chip->state == FL_READY) {
862 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100863 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100864 return 0;
865 }
866 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800867 if (chip->controller->active->state == FL_PM_SUSPENDED) {
868 chip->state = FL_PM_SUSPENDED;
869 spin_unlock(lock);
870 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800871 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100872 }
873 set_current_state(TASK_UNINTERRUPTIBLE);
874 add_wait_queue(wq, &wait);
875 spin_unlock(lock);
876 schedule();
877 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto retry;
879}
880
881/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700882 * panic_nand_wait - [GENERIC] wait until the command is done
883 * @mtd: MTD device structure
884 * @chip: NAND chip structure
885 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200886 *
887 * Wait for command done. This is a helper function for nand_wait used when
888 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400889 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200890 */
891static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
892 unsigned long timeo)
893{
894 int i;
895 for (i = 0; i < timeo; i++) {
896 if (chip->dev_ready) {
897 if (chip->dev_ready(mtd))
898 break;
899 } else {
900 if (chip->read_byte(mtd) & NAND_STATUS_READY)
901 break;
902 }
903 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200904 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200905}
906
907/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700908 * nand_wait - [DEFAULT] wait until the command is done
909 * @mtd: MTD device structure
910 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100912 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700913 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200914static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916
Alex Smithb70af9b2015-10-06 14:52:07 +0100917 int status;
918 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Brian Norris8b6e50c2011-05-25 14:59:01 -0700920 /*
921 * Apply this short delay always to ensure that we do wait tWB in any
922 * case on any machine.
923 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100924 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200926 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200928 if (in_interrupt() || oops_in_progress)
929 panic_nand_wait(mtd, chip, timeo);
930 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800931 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100932 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200933 if (chip->dev_ready) {
934 if (chip->dev_ready(mtd))
935 break;
936 } else {
937 if (chip->read_byte(mtd) & NAND_STATUS_READY)
938 break;
939 }
940 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100941 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800943
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200944 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100945 /* This can happen if in case of timeout or buggy dev_ready */
946 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return status;
948}
949
950/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200951 * nand_reset_data_interface - Reset data interface and timings
952 * @chip: The NAND chip
953 *
954 * Reset the Data interface and timings to ONFI mode 0.
955 *
956 * Returns 0 for success or negative error code otherwise.
957 */
958static int nand_reset_data_interface(struct nand_chip *chip)
959{
960 struct mtd_info *mtd = nand_to_mtd(chip);
961 const struct nand_data_interface *conf;
962 int ret;
963
964 if (!chip->setup_data_interface)
965 return 0;
966
967 /*
968 * The ONFI specification says:
969 * "
970 * To transition from NV-DDR or NV-DDR2 to the SDR data
971 * interface, the host shall use the Reset (FFh) command
972 * using SDR timing mode 0. A device in any timing mode is
973 * required to recognize Reset (FFh) command issued in SDR
974 * timing mode 0.
975 * "
976 *
977 * Configure the data interface in SDR mode and set the
978 * timings to timing mode 0.
979 */
980
981 conf = nand_get_default_data_interface();
982 ret = chip->setup_data_interface(mtd, conf, false);
983 if (ret)
984 pr_err("Failed to configure data interface to SDR timing mode 0\n");
985
986 return ret;
987}
988
989/**
990 * nand_setup_data_interface - Setup the best data interface and timings
991 * @chip: The NAND chip
992 *
993 * Find and configure the best data interface and NAND timings supported by
994 * the chip and the driver.
995 * First tries to retrieve supported timing modes from ONFI information,
996 * and if the NAND chip does not support ONFI, relies on the
997 * ->onfi_timing_mode_default specified in the nand_ids table.
998 *
999 * Returns 0 for success or negative error code otherwise.
1000 */
1001static int nand_setup_data_interface(struct nand_chip *chip)
1002{
1003 struct mtd_info *mtd = nand_to_mtd(chip);
1004 int ret;
1005
1006 if (!chip->setup_data_interface || !chip->data_interface)
1007 return 0;
1008
1009 /*
1010 * Ensure the timing mode has been changed on the chip side
1011 * before changing timings on the controller side.
1012 */
1013 if (chip->onfi_version) {
1014 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1015 chip->onfi_timing_mode_default,
1016 };
1017
1018 ret = chip->onfi_set_features(mtd, chip,
1019 ONFI_FEATURE_ADDR_TIMING_MODE,
1020 tmode_param);
1021 if (ret)
1022 goto err;
1023 }
1024
1025 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1026err:
1027 return ret;
1028}
1029
1030/**
1031 * nand_init_data_interface - find the best data interface and timings
1032 * @chip: The NAND chip
1033 *
1034 * Find the best data interface and NAND timings supported by the chip
1035 * and the driver.
1036 * First tries to retrieve supported timing modes from ONFI information,
1037 * and if the NAND chip does not support ONFI, relies on the
1038 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1039 * function nand_chip->data_interface is initialized with the best timing mode
1040 * available.
1041 *
1042 * Returns 0 for success or negative error code otherwise.
1043 */
1044static int nand_init_data_interface(struct nand_chip *chip)
1045{
1046 struct mtd_info *mtd = nand_to_mtd(chip);
1047 int modes, mode, ret;
1048
1049 if (!chip->setup_data_interface)
1050 return 0;
1051
1052 /*
1053 * First try to identify the best timings from ONFI parameters and
1054 * if the NAND does not support ONFI, fallback to the default ONFI
1055 * timing mode.
1056 */
1057 modes = onfi_get_async_timing_mode(chip);
1058 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1059 if (!chip->onfi_timing_mode_default)
1060 return 0;
1061
1062 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1063 }
1064
1065 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1066 GFP_KERNEL);
1067 if (!chip->data_interface)
1068 return -ENOMEM;
1069
1070 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1071 ret = onfi_init_data_interface(chip, chip->data_interface,
1072 NAND_SDR_IFACE, mode);
1073 if (ret)
1074 continue;
1075
1076 ret = chip->setup_data_interface(mtd, chip->data_interface,
1077 true);
1078 if (!ret) {
1079 chip->onfi_timing_mode_default = mode;
1080 break;
1081 }
1082 }
1083
1084 return 0;
1085}
1086
1087static void nand_release_data_interface(struct nand_chip *chip)
1088{
1089 kfree(chip->data_interface);
1090}
1091
1092/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001093 * nand_reset - Reset and initialize a NAND device
1094 * @chip: The NAND chip
1095 *
1096 * Returns 0 for success or negative error code otherwise
1097 */
1098int nand_reset(struct nand_chip *chip)
1099{
1100 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001101 int ret;
1102
1103 ret = nand_reset_data_interface(chip);
1104 if (ret)
1105 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001106
1107 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1108
Boris Brezillond8e725d2016-09-15 10:32:50 +02001109 ret = nand_setup_data_interface(chip);
1110 if (ret)
1111 return ret;
1112
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001113 return 0;
1114}
1115
1116/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001117 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001118 * @mtd: mtd info
1119 * @ofs: offset to start unlock from
1120 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001121 * @invert: when = 0, unlock the range of blocks within the lower and
1122 * upper boundary address
1123 * when = 1, unlock the range of blocks outside the boundaries
1124 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301125 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001126 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301127 */
1128static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1129 uint64_t len, int invert)
1130{
1131 int ret = 0;
1132 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001133 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301134
1135 /* Submit address of first page to unlock */
1136 page = ofs >> chip->page_shift;
1137 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1138
1139 /* Submit address of last page to unlock */
1140 page = (ofs + len) >> chip->page_shift;
1141 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1142 (page | invert) & chip->pagemask);
1143
1144 /* Call wait ready function */
1145 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301146 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001147 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001148 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301149 __func__, status);
1150 ret = -EIO;
1151 }
1152
1153 return ret;
1154}
1155
1156/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001157 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001158 * @mtd: mtd info
1159 * @ofs: offset to start unlock from
1160 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301161 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001162 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301163 */
1164int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1165{
1166 int ret = 0;
1167 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001168 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301169
Brian Norris289c0522011-07-19 10:06:09 -07001170 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301171 __func__, (unsigned long long)ofs, len);
1172
1173 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001174 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301175
1176 /* Align to last block address if size addresses end of the device */
1177 if (ofs + len == mtd->size)
1178 len -= mtd->erasesize;
1179
Huang Shijie6a8214a2012-11-19 14:43:30 +08001180 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301181
1182 /* Shift to get chip number */
1183 chipnr = ofs >> chip->chip_shift;
1184
1185 chip->select_chip(mtd, chipnr);
1186
White Ding57d3a9a2014-07-24 00:10:45 +08001187 /*
1188 * Reset the chip.
1189 * If we want to check the WP through READ STATUS and check the bit 7
1190 * we must reset the chip
1191 * some operation can also clear the bit 7 of status register
1192 * eg. erase/program a locked block
1193 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001194 nand_reset(chip);
White Ding57d3a9a2014-07-24 00:10:45 +08001195
Vimal Singh7d70f332010-02-08 15:50:49 +05301196 /* Check, if it is write protected */
1197 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001198 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301199 __func__);
1200 ret = -EIO;
1201 goto out;
1202 }
1203
1204 ret = __nand_unlock(mtd, ofs, len, 0);
1205
1206out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001207 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301208 nand_release_device(mtd);
1209
1210 return ret;
1211}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001212EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301213
1214/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001215 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001216 * @mtd: mtd info
1217 * @ofs: offset to start unlock from
1218 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301219 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001220 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1221 * have this feature, but it allows only to lock all blocks, not for specified
1222 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1223 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301224 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001225 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301226 */
1227int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1228{
1229 int ret = 0;
1230 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001231 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301232
Brian Norris289c0522011-07-19 10:06:09 -07001233 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301234 __func__, (unsigned long long)ofs, len);
1235
1236 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001237 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301238
Huang Shijie6a8214a2012-11-19 14:43:30 +08001239 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301240
1241 /* Shift to get chip number */
1242 chipnr = ofs >> chip->chip_shift;
1243
1244 chip->select_chip(mtd, chipnr);
1245
White Ding57d3a9a2014-07-24 00:10:45 +08001246 /*
1247 * Reset the chip.
1248 * If we want to check the WP through READ STATUS and check the bit 7
1249 * we must reset the chip
1250 * some operation can also clear the bit 7 of status register
1251 * eg. erase/program a locked block
1252 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001253 nand_reset(chip);
White Ding57d3a9a2014-07-24 00:10:45 +08001254
Vimal Singh7d70f332010-02-08 15:50:49 +05301255 /* Check, if it is write protected */
1256 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001257 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301258 __func__);
1259 status = MTD_ERASE_FAILED;
1260 ret = -EIO;
1261 goto out;
1262 }
1263
1264 /* Submit address of first page to lock */
1265 page = ofs >> chip->page_shift;
1266 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1267
1268 /* Call wait ready function */
1269 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301270 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001271 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001272 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301273 __func__, status);
1274 ret = -EIO;
1275 goto out;
1276 }
1277
1278 ret = __nand_unlock(mtd, ofs, len, 0x1);
1279
1280out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001281 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301282 nand_release_device(mtd);
1283
1284 return ret;
1285}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001286EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301287
1288/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001289 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1290 * @buf: buffer to test
1291 * @len: buffer length
1292 * @bitflips_threshold: maximum number of bitflips
1293 *
1294 * Check if a buffer contains only 0xff, which means the underlying region
1295 * has been erased and is ready to be programmed.
1296 * The bitflips_threshold specify the maximum number of bitflips before
1297 * considering the region is not erased.
1298 * Note: The logic of this function has been extracted from the memweight
1299 * implementation, except that nand_check_erased_buf function exit before
1300 * testing the whole buffer if the number of bitflips exceed the
1301 * bitflips_threshold value.
1302 *
1303 * Returns a positive number of bitflips less than or equal to
1304 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1305 * threshold.
1306 */
1307static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1308{
1309 const unsigned char *bitmap = buf;
1310 int bitflips = 0;
1311 int weight;
1312
1313 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1314 len--, bitmap++) {
1315 weight = hweight8(*bitmap);
1316 bitflips += BITS_PER_BYTE - weight;
1317 if (unlikely(bitflips > bitflips_threshold))
1318 return -EBADMSG;
1319 }
1320
1321 for (; len >= sizeof(long);
1322 len -= sizeof(long), bitmap += sizeof(long)) {
1323 weight = hweight_long(*((unsigned long *)bitmap));
1324 bitflips += BITS_PER_LONG - weight;
1325 if (unlikely(bitflips > bitflips_threshold))
1326 return -EBADMSG;
1327 }
1328
1329 for (; len > 0; len--, bitmap++) {
1330 weight = hweight8(*bitmap);
1331 bitflips += BITS_PER_BYTE - weight;
1332 if (unlikely(bitflips > bitflips_threshold))
1333 return -EBADMSG;
1334 }
1335
1336 return bitflips;
1337}
1338
1339/**
1340 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1341 * 0xff data
1342 * @data: data buffer to test
1343 * @datalen: data length
1344 * @ecc: ECC buffer
1345 * @ecclen: ECC length
1346 * @extraoob: extra OOB buffer
1347 * @extraooblen: extra OOB length
1348 * @bitflips_threshold: maximum number of bitflips
1349 *
1350 * Check if a data buffer and its associated ECC and OOB data contains only
1351 * 0xff pattern, which means the underlying region has been erased and is
1352 * ready to be programmed.
1353 * The bitflips_threshold specify the maximum number of bitflips before
1354 * considering the region as not erased.
1355 *
1356 * Note:
1357 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1358 * different from the NAND page size. When fixing bitflips, ECC engines will
1359 * report the number of errors per chunk, and the NAND core infrastructure
1360 * expect you to return the maximum number of bitflips for the whole page.
1361 * This is why you should always use this function on a single chunk and
1362 * not on the whole page. After checking each chunk you should update your
1363 * max_bitflips value accordingly.
1364 * 2/ When checking for bitflips in erased pages you should not only check
1365 * the payload data but also their associated ECC data, because a user might
1366 * have programmed almost all bits to 1 but a few. In this case, we
1367 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1368 * this case.
1369 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1370 * data are protected by the ECC engine.
1371 * It could also be used if you support subpages and want to attach some
1372 * extra OOB data to an ECC chunk.
1373 *
1374 * Returns a positive number of bitflips less than or equal to
1375 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1376 * threshold. In case of success, the passed buffers are filled with 0xff.
1377 */
1378int nand_check_erased_ecc_chunk(void *data, int datalen,
1379 void *ecc, int ecclen,
1380 void *extraoob, int extraooblen,
1381 int bitflips_threshold)
1382{
1383 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1384
1385 data_bitflips = nand_check_erased_buf(data, datalen,
1386 bitflips_threshold);
1387 if (data_bitflips < 0)
1388 return data_bitflips;
1389
1390 bitflips_threshold -= data_bitflips;
1391
1392 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1393 if (ecc_bitflips < 0)
1394 return ecc_bitflips;
1395
1396 bitflips_threshold -= ecc_bitflips;
1397
1398 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1399 bitflips_threshold);
1400 if (extraoob_bitflips < 0)
1401 return extraoob_bitflips;
1402
1403 if (data_bitflips)
1404 memset(data, 0xff, datalen);
1405
1406 if (ecc_bitflips)
1407 memset(ecc, 0xff, ecclen);
1408
1409 if (extraoob_bitflips)
1410 memset(extraoob, 0xff, extraooblen);
1411
1412 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1413}
1414EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1415
1416/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001417 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001418 * @mtd: mtd info structure
1419 * @chip: nand chip info structure
1420 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001421 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001422 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001423 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001424 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001425 */
1426static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001427 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001428{
1429 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001430 if (oob_required)
1431 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001432 return 0;
1433}
1434
1435/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001436 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001437 * @mtd: mtd info structure
1438 * @chip: nand chip info structure
1439 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001440 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001441 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001442 *
1443 * We need a special oob layout and handling even when OOB isn't used.
1444 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001445static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001446 struct nand_chip *chip, uint8_t *buf,
1447 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001448{
1449 int eccsize = chip->ecc.size;
1450 int eccbytes = chip->ecc.bytes;
1451 uint8_t *oob = chip->oob_poi;
1452 int steps, size;
1453
1454 for (steps = chip->ecc.steps; steps > 0; steps--) {
1455 chip->read_buf(mtd, buf, eccsize);
1456 buf += eccsize;
1457
1458 if (chip->ecc.prepad) {
1459 chip->read_buf(mtd, oob, chip->ecc.prepad);
1460 oob += chip->ecc.prepad;
1461 }
1462
1463 chip->read_buf(mtd, oob, eccbytes);
1464 oob += eccbytes;
1465
1466 if (chip->ecc.postpad) {
1467 chip->read_buf(mtd, oob, chip->ecc.postpad);
1468 oob += chip->ecc.postpad;
1469 }
1470 }
1471
1472 size = mtd->oobsize - (oob - chip->oob_poi);
1473 if (size)
1474 chip->read_buf(mtd, oob, size);
1475
1476 return 0;
1477}
1478
1479/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001480 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001481 * @mtd: mtd info structure
1482 * @chip: nand chip info structure
1483 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001484 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001485 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001486 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001487static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001488 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Boris Brezillon846031d2016-02-03 20:11:00 +01001490 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001491 int eccbytes = chip->ecc.bytes;
1492 int eccsteps = chip->ecc.steps;
1493 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001494 uint8_t *ecc_calc = chip->buffers->ecccalc;
1495 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001496 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001497
Brian Norris1fbb9382012-05-02 10:14:55 -07001498 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001499
1500 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1501 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1502
Boris Brezillon846031d2016-02-03 20:11:00 +01001503 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1504 chip->ecc.total);
1505 if (ret)
1506 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001507
1508 eccsteps = chip->ecc.steps;
1509 p = buf;
1510
1511 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1512 int stat;
1513
1514 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001515 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001516 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001517 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001518 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001519 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1520 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001521 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001522 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301526 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001527 * @mtd: mtd info structure
1528 * @chip: nand chip info structure
1529 * @data_offs: offset of requested data within the page
1530 * @readlen: data length
1531 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001532 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001533 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001534static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001535 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1536 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001537{
Boris Brezillon846031d2016-02-03 20:11:00 +01001538 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001539 uint8_t *p;
1540 int data_col_addr, i, gaps = 0;
1541 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1542 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001543 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001544 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001545 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001546
Brian Norris7854d3f2011-06-23 14:12:08 -07001547 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001548 start_step = data_offs / chip->ecc.size;
1549 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1550 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301551 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001552
Brian Norris8b6e50c2011-05-25 14:59:01 -07001553 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001554 datafrag_len = num_steps * chip->ecc.size;
1555 eccfrag_len = num_steps * chip->ecc.bytes;
1556
1557 data_col_addr = start_step * chip->ecc.size;
1558 /* If we read not a page aligned data */
1559 if (data_col_addr != 0)
1560 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1561
1562 p = bufpoi + data_col_addr;
1563 chip->read_buf(mtd, p, datafrag_len);
1564
Brian Norris8b6e50c2011-05-25 14:59:01 -07001565 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001566 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1567 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1568
Brian Norris8b6e50c2011-05-25 14:59:01 -07001569 /*
1570 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001571 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001572 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001573 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1574 if (ret)
1575 return ret;
1576
1577 if (oobregion.length < eccfrag_len)
1578 gaps = 1;
1579
Alexey Korolev3d459552008-05-15 17:23:18 +01001580 if (gaps) {
1581 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1582 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1583 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001584 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001585 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001586 * about buswidth alignment in read_buf.
1587 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001588 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001589 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001590 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001591 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001592 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1593 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001594 aligned_len++;
1595
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001596 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001597 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001598 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1599 }
1600
Boris Brezillon846031d2016-02-03 20:11:00 +01001601 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1602 chip->oob_poi, index, eccfrag_len);
1603 if (ret)
1604 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001605
1606 p = bufpoi + data_col_addr;
1607 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1608 int stat;
1609
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001610 stat = chip->ecc.correct(mtd, p,
1611 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001612 if (stat == -EBADMSG &&
1613 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1614 /* check for empty pages with bitflips */
1615 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1616 &chip->buffers->ecccode[i],
1617 chip->ecc.bytes,
1618 NULL, 0,
1619 chip->ecc.strength);
1620 }
1621
Mike Dunn3f91e942012-04-25 12:06:09 -07001622 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001623 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001624 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001625 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001626 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1627 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001628 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001629 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001630}
1631
1632/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001633 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001634 * @mtd: mtd info structure
1635 * @chip: nand chip info structure
1636 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001637 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001638 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001639 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001640 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001641 */
1642static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001643 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001644{
Boris Brezillon846031d2016-02-03 20:11:00 +01001645 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001646 int eccbytes = chip->ecc.bytes;
1647 int eccsteps = chip->ecc.steps;
1648 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001649 uint8_t *ecc_calc = chip->buffers->ecccalc;
1650 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001651 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001652
1653 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1654 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1655 chip->read_buf(mtd, p, eccsize);
1656 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1657 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001658 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001659
Boris Brezillon846031d2016-02-03 20:11:00 +01001660 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1661 chip->ecc.total);
1662 if (ret)
1663 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001664
1665 eccsteps = chip->ecc.steps;
1666 p = buf;
1667
1668 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1669 int stat;
1670
1671 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001672 if (stat == -EBADMSG &&
1673 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1674 /* check for empty pages with bitflips */
1675 stat = nand_check_erased_ecc_chunk(p, eccsize,
1676 &ecc_code[i], eccbytes,
1677 NULL, 0,
1678 chip->ecc.strength);
1679 }
1680
Mike Dunn3f91e942012-04-25 12:06:09 -07001681 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001682 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001683 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001684 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001685 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1686 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001687 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001688 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001689}
1690
1691/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001692 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001693 * @mtd: mtd info structure
1694 * @chip: nand chip info structure
1695 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001696 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001697 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001698 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001699 * Hardware ECC for large page chips, require OOB to be read first. For this
1700 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1701 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1702 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1703 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001704 */
1705static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001706 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001707{
Boris Brezillon846031d2016-02-03 20:11:00 +01001708 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001709 int eccbytes = chip->ecc.bytes;
1710 int eccsteps = chip->ecc.steps;
1711 uint8_t *p = buf;
1712 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001713 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001714 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001715
1716 /* Read the OOB area first */
1717 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1718 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1719 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1720
Boris Brezillon846031d2016-02-03 20:11:00 +01001721 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1722 chip->ecc.total);
1723 if (ret)
1724 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001725
1726 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1727 int stat;
1728
1729 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1730 chip->read_buf(mtd, p, eccsize);
1731 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1732
1733 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001734 if (stat == -EBADMSG &&
1735 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1736 /* check for empty pages with bitflips */
1737 stat = nand_check_erased_ecc_chunk(p, eccsize,
1738 &ecc_code[i], eccbytes,
1739 NULL, 0,
1740 chip->ecc.strength);
1741 }
1742
Mike Dunn3f91e942012-04-25 12:06:09 -07001743 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001744 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001745 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001746 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001747 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1748 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001749 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001750 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001751}
1752
1753/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001754 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001755 * @mtd: mtd info structure
1756 * @chip: nand chip info structure
1757 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001758 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001759 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001760 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001761 * The hw generator calculates the error syndrome automatically. Therefore we
1762 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001763 */
1764static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001765 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001766{
1767 int i, eccsize = chip->ecc.size;
1768 int eccbytes = chip->ecc.bytes;
1769 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001770 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001771 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001772 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001773 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001774
1775 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1776 int stat;
1777
1778 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1779 chip->read_buf(mtd, p, eccsize);
1780
1781 if (chip->ecc.prepad) {
1782 chip->read_buf(mtd, oob, chip->ecc.prepad);
1783 oob += chip->ecc.prepad;
1784 }
1785
1786 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1787 chip->read_buf(mtd, oob, eccbytes);
1788 stat = chip->ecc.correct(mtd, p, oob, NULL);
1789
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001790 oob += eccbytes;
1791
1792 if (chip->ecc.postpad) {
1793 chip->read_buf(mtd, oob, chip->ecc.postpad);
1794 oob += chip->ecc.postpad;
1795 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001796
1797 if (stat == -EBADMSG &&
1798 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1799 /* check for empty pages with bitflips */
1800 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1801 oob - eccpadbytes,
1802 eccpadbytes,
1803 NULL, 0,
1804 chip->ecc.strength);
1805 }
1806
1807 if (stat < 0) {
1808 mtd->ecc_stats.failed++;
1809 } else {
1810 mtd->ecc_stats.corrected += stat;
1811 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1812 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001813 }
1814
1815 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001816 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001817 if (i)
1818 chip->read_buf(mtd, oob, i);
1819
Mike Dunn3f91e942012-04-25 12:06:09 -07001820 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001821}
1822
1823/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001824 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001825 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001826 * @oob: oob destination address
1827 * @ops: oob ops structure
1828 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001829 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001830static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001831 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001832{
Boris Brezillon846031d2016-02-03 20:11:00 +01001833 struct nand_chip *chip = mtd_to_nand(mtd);
1834 int ret;
1835
Florian Fainellif8ac0412010-09-07 13:23:43 +02001836 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001837
Brian Norris0612b9d2011-08-30 18:45:40 -07001838 case MTD_OPS_PLACE_OOB:
1839 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001840 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1841 return oob + len;
1842
Boris Brezillon846031d2016-02-03 20:11:00 +01001843 case MTD_OPS_AUTO_OOB:
1844 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1845 ops->ooboffs, len);
1846 BUG_ON(ret);
1847 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001848
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001849 default:
1850 BUG();
1851 }
1852 return NULL;
1853}
1854
1855/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001856 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1857 * @mtd: MTD device structure
1858 * @retry_mode: the retry mode to use
1859 *
1860 * Some vendors supply a special command to shift the Vt threshold, to be used
1861 * when there are too many bitflips in a page (i.e., ECC error). After setting
1862 * a new threshold, the host should retry reading the page.
1863 */
1864static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1865{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001866 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001867
1868 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1869
1870 if (retry_mode >= chip->read_retries)
1871 return -EINVAL;
1872
1873 if (!chip->setup_read_retry)
1874 return -EOPNOTSUPP;
1875
1876 return chip->setup_read_retry(mtd, retry_mode);
1877}
1878
1879/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001880 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001881 * @mtd: MTD device structure
1882 * @from: offset to read from
1883 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001884 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001885 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001886 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001887static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1888 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001889{
Brian Norrise47f3db2012-05-02 10:14:56 -07001890 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001891 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001892 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001893 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001894 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001895 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001896
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001897 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001898 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001899 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001900 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001901 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001903 chipnr = (int)(from >> chip->chip_shift);
1904 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001906 realpage = (int)(from >> chip->page_shift);
1907 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001909 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001911 buf = ops->datbuf;
1912 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001913 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001914
Florian Fainellif8ac0412010-09-07 13:23:43 +02001915 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001916 unsigned int ecc_failures = mtd->ecc_stats.failed;
1917
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001918 bytes = min(mtd->writesize - col, readlen);
1919 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001920
Kamal Dasu66507c72014-05-01 20:51:19 -04001921 if (!aligned)
1922 use_bufpoi = 1;
1923 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1924 use_bufpoi = !virt_addr_valid(buf);
1925 else
1926 use_bufpoi = 0;
1927
Brian Norris8b6e50c2011-05-25 14:59:01 -07001928 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001929 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001930 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1931
1932 if (use_bufpoi && aligned)
1933 pr_debug("%s: using read bounce buffer for buf@%p\n",
1934 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Brian Norrisba84fb52014-01-03 15:13:33 -08001936read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001937 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Mike Dunnedbc45402012-04-25 12:06:11 -07001939 /*
1940 * Now read the page into the buffer. Absent an error,
1941 * the read methods return max bitflips per ecc step.
1942 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001943 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001944 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001945 oob_required,
1946 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001947 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1948 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001949 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001950 col, bytes, bufpoi,
1951 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001952 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001953 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001954 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001955 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001956 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001957 /* Invalidate page cache */
1958 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001959 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001960 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001961
Mike Dunnedbc45402012-04-25 12:06:11 -07001962 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1963
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001964 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001965 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001966 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001967 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001968 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001969 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001970 chip->pagebuf_bitflips = ret;
1971 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001972 /* Invalidate page cache */
1973 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001974 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001975 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001977
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001978 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001979 int toread = min(oobreadlen, max_oobsize);
1980
1981 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001982 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001983 oob, ops, toread);
1984 oobreadlen -= toread;
1985 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001986 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001987
1988 if (chip->options & NAND_NEED_READRDY) {
1989 /* Apply delay or wait for ready/busy pin */
1990 if (!chip->dev_ready)
1991 udelay(chip->chip_delay);
1992 else
1993 nand_wait_ready(mtd);
1994 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001995
Brian Norrisba84fb52014-01-03 15:13:33 -08001996 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001997 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001998 retry_mode++;
1999 ret = nand_setup_read_retry(mtd,
2000 retry_mode);
2001 if (ret < 0)
2002 break;
2003
2004 /* Reset failures; retry */
2005 mtd->ecc_stats.failed = ecc_failures;
2006 goto read_retry;
2007 } else {
2008 /* No more retry modes; real failure */
2009 ecc_fail = true;
2010 }
2011 }
2012
2013 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002014 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002015 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002016 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002017 max_bitflips = max_t(unsigned int, max_bitflips,
2018 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002021 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002022
Brian Norrisba84fb52014-01-03 15:13:33 -08002023 /* Reset to retry mode 0 */
2024 if (retry_mode) {
2025 ret = nand_setup_read_retry(mtd, 0);
2026 if (ret < 0)
2027 break;
2028 retry_mode = 0;
2029 }
2030
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002031 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Brian Norris8b6e50c2011-05-25 14:59:01 -07002034 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 col = 0;
2036 /* Increment page address */
2037 realpage++;
2038
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002039 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* Check, if we cross a chip boundary */
2041 if (!page) {
2042 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002043 chip->select_chip(mtd, -1);
2044 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002047 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002049 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002050 if (oob)
2051 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Mike Dunn3f91e942012-04-25 12:06:09 -07002053 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002054 return ret;
2055
Brian Norrisb72f3df2013-12-03 11:04:14 -08002056 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002057 return -EBADMSG;
2058
Mike Dunnedbc45402012-04-25 12:06:11 -07002059 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002060}
2061
2062/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002063 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002064 * @mtd: MTD device structure
2065 * @from: offset to read from
2066 * @len: number of bytes to read
2067 * @retlen: pointer to variable to store the number of read bytes
2068 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002069 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002070 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002071 */
2072static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2073 size_t *retlen, uint8_t *buf)
2074{
Brian Norris4a89ff82011-08-30 18:45:45 -07002075 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002076 int ret;
2077
Huang Shijie6a8214a2012-11-19 14:43:30 +08002078 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002079 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002080 ops.len = len;
2081 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002082 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002083 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002084 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002085 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002086 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
2089/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002090 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002091 * @mtd: mtd info structure
2092 * @chip: nand chip info structure
2093 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002094 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002095int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002096{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002097 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002098 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002099 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002100}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002101EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002102
2103/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002104 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002105 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002106 * @mtd: mtd info structure
2107 * @chip: nand chip info structure
2108 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002109 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002110int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2111 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002112{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002113 int length = mtd->oobsize;
2114 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2115 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002116 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002117 int i, toread, sndrnd = 0, pos;
2118
2119 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2120 for (i = 0; i < chip->ecc.steps; i++) {
2121 if (sndrnd) {
2122 pos = eccsize + i * (eccsize + chunk);
2123 if (mtd->writesize > 512)
2124 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2125 else
2126 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2127 } else
2128 sndrnd = 1;
2129 toread = min_t(int, length, chunk);
2130 chip->read_buf(mtd, bufpoi, toread);
2131 bufpoi += toread;
2132 length -= toread;
2133 }
2134 if (length > 0)
2135 chip->read_buf(mtd, bufpoi, length);
2136
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002137 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002138}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002139EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002140
2141/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002142 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002143 * @mtd: mtd info structure
2144 * @chip: nand chip info structure
2145 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002146 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002147int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002148{
2149 int status = 0;
2150 const uint8_t *buf = chip->oob_poi;
2151 int length = mtd->oobsize;
2152
2153 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2154 chip->write_buf(mtd, buf, length);
2155 /* Send command to program the OOB data */
2156 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2157
2158 status = chip->waitfunc(mtd, chip);
2159
Savin Zlobec0d420f92006-06-21 11:51:20 +02002160 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002161}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002162EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002163
2164/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002165 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002166 * with syndrome - only for large page flash
2167 * @mtd: mtd info structure
2168 * @chip: nand chip info structure
2169 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002170 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002171int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2172 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002173{
2174 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2175 int eccsize = chip->ecc.size, length = mtd->oobsize;
2176 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2177 const uint8_t *bufpoi = chip->oob_poi;
2178
2179 /*
2180 * data-ecc-data-ecc ... ecc-oob
2181 * or
2182 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2183 */
2184 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2185 pos = steps * (eccsize + chunk);
2186 steps = 0;
2187 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002188 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002189
2190 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2191 for (i = 0; i < steps; i++) {
2192 if (sndcmd) {
2193 if (mtd->writesize <= 512) {
2194 uint32_t fill = 0xFFFFFFFF;
2195
2196 len = eccsize;
2197 while (len > 0) {
2198 int num = min_t(int, len, 4);
2199 chip->write_buf(mtd, (uint8_t *)&fill,
2200 num);
2201 len -= num;
2202 }
2203 } else {
2204 pos = eccsize + i * (eccsize + chunk);
2205 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2206 }
2207 } else
2208 sndcmd = 1;
2209 len = min_t(int, length, chunk);
2210 chip->write_buf(mtd, bufpoi, len);
2211 bufpoi += len;
2212 length -= len;
2213 }
2214 if (length > 0)
2215 chip->write_buf(mtd, bufpoi, length);
2216
2217 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2218 status = chip->waitfunc(mtd, chip);
2219
2220 return status & NAND_STATUS_FAIL ? -EIO : 0;
2221}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002222EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002223
2224/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002225 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002226 * @mtd: MTD device structure
2227 * @from: offset to read from
2228 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002230 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002232static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2233 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Brian Norrisc00a0992012-05-01 17:12:54 -07002235 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002236 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002237 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002238 int readlen = ops->ooblen;
2239 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002240 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002241 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Brian Norris289c0522011-07-19 10:06:09 -07002243 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302244 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Brian Norris041e4572011-06-23 16:45:24 -07002246 stats = mtd->ecc_stats;
2247
Boris BREZILLON29f10582016-03-07 10:46:52 +01002248 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002249
2250 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002251 pr_debug("%s: attempt to start read outside oob\n",
2252 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002253 return -EINVAL;
2254 }
2255
2256 /* Do not allow reads past end of device */
2257 if (unlikely(from >= mtd->size ||
2258 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2259 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002260 pr_debug("%s: attempt to read beyond end of device\n",
2261 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002262 return -EINVAL;
2263 }
Vitaly Wool70145682006-11-03 18:20:38 +03002264
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002265 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002266 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002268 /* Shift to get page */
2269 realpage = (int)(from >> chip->page_shift);
2270 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Florian Fainellif8ac0412010-09-07 13:23:43 +02002272 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002273 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002274 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002275 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002276 ret = chip->ecc.read_oob(mtd, chip, page);
2277
2278 if (ret < 0)
2279 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002280
2281 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002282 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002283
Brian Norris5bc7c332013-03-13 09:51:31 -07002284 if (chip->options & NAND_NEED_READRDY) {
2285 /* Apply delay or wait for ready/busy pin */
2286 if (!chip->dev_ready)
2287 udelay(chip->chip_delay);
2288 else
2289 nand_wait_ready(mtd);
2290 }
2291
Vitaly Wool70145682006-11-03 18:20:38 +03002292 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002293 if (!readlen)
2294 break;
2295
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002296 /* Increment page address */
2297 realpage++;
2298
2299 page = realpage & chip->pagemask;
2300 /* Check, if we cross a chip boundary */
2301 if (!page) {
2302 chipnr++;
2303 chip->select_chip(mtd, -1);
2304 chip->select_chip(mtd, chipnr);
2305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002307 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002309 ops->oobretlen = ops->ooblen - readlen;
2310
2311 if (ret < 0)
2312 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002313
2314 if (mtd->ecc_stats.failed - stats.failed)
2315 return -EBADMSG;
2316
2317 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
2320/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002322 * @mtd: MTD device structure
2323 * @from: offset to read from
2324 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002326 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002328static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2329 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002331 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002332
2333 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002336 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002337 pr_debug("%s: attempt to read beyond end of device\n",
2338 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return -EINVAL;
2340 }
2341
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002342 if (ops->mode != MTD_OPS_PLACE_OOB &&
2343 ops->mode != MTD_OPS_AUTO_OOB &&
2344 ops->mode != MTD_OPS_RAW)
2345 return -ENOTSUPP;
2346
Huang Shijie6a8214a2012-11-19 14:43:30 +08002347 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002349 if (!ops->datbuf)
2350 ret = nand_do_read_oob(mtd, from, ops);
2351 else
2352 ret = nand_do_read_ops(mtd, from, ops);
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002355 return ret;
2356}
2357
2358
2359/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002360 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002361 * @mtd: mtd info structure
2362 * @chip: nand chip info structure
2363 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002364 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002365 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002366 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002367 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002368 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002369static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002370 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002371{
2372 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002373 if (oob_required)
2374 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002375
2376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002379/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002380 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002381 * @mtd: mtd info structure
2382 * @chip: nand chip info structure
2383 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002384 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002385 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002386 *
2387 * We need a special oob layout and handling even when ECC isn't checked.
2388 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002389static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002390 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002391 const uint8_t *buf, int oob_required,
2392 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002393{
2394 int eccsize = chip->ecc.size;
2395 int eccbytes = chip->ecc.bytes;
2396 uint8_t *oob = chip->oob_poi;
2397 int steps, size;
2398
2399 for (steps = chip->ecc.steps; steps > 0; steps--) {
2400 chip->write_buf(mtd, buf, eccsize);
2401 buf += eccsize;
2402
2403 if (chip->ecc.prepad) {
2404 chip->write_buf(mtd, oob, chip->ecc.prepad);
2405 oob += chip->ecc.prepad;
2406 }
2407
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002408 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002409 oob += eccbytes;
2410
2411 if (chip->ecc.postpad) {
2412 chip->write_buf(mtd, oob, chip->ecc.postpad);
2413 oob += chip->ecc.postpad;
2414 }
2415 }
2416
2417 size = mtd->oobsize - (oob - chip->oob_poi);
2418 if (size)
2419 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002420
2421 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002422}
2423/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002424 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002425 * @mtd: mtd info structure
2426 * @chip: nand chip info structure
2427 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002428 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002429 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002430 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002431static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002432 const uint8_t *buf, int oob_required,
2433 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002434{
Boris Brezillon846031d2016-02-03 20:11:00 +01002435 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002436 int eccbytes = chip->ecc.bytes;
2437 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002438 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002439 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002440
Brian Norris7854d3f2011-06-23 14:12:08 -07002441 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002442 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2443 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002444
Boris Brezillon846031d2016-02-03 20:11:00 +01002445 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2446 chip->ecc.total);
2447 if (ret)
2448 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002449
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002450 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002451}
2452
2453/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002454 * nand_write_page_hwecc - [REPLACEABLE] hardware 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_hwecc(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
2471 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2472 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002473 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002474 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2475 }
2476
Boris Brezillon846031d2016-02-03 20:11:00 +01002477 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2478 chip->ecc.total);
2479 if (ret)
2480 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002481
2482 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002483
2484 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002485}
2486
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302487
2488/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002489 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302490 * @mtd: mtd info structure
2491 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002492 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302493 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002494 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302495 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002496 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302497 */
2498static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2499 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002500 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002501 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302502{
2503 uint8_t *oob_buf = chip->oob_poi;
2504 uint8_t *ecc_calc = chip->buffers->ecccalc;
2505 int ecc_size = chip->ecc.size;
2506 int ecc_bytes = chip->ecc.bytes;
2507 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302508 uint32_t start_step = offset / ecc_size;
2509 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2510 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002511 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302512
2513 for (step = 0; step < ecc_steps; step++) {
2514 /* configure controller for WRITE access */
2515 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2516
2517 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002518 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302519
2520 /* mask ECC of un-touched subpages by padding 0xFF */
2521 if ((step < start_step) || (step > end_step))
2522 memset(ecc_calc, 0xff, ecc_bytes);
2523 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002524 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302525
2526 /* mask OOB of un-touched subpages by padding 0xFF */
2527 /* if oob_required, preserve OOB metadata of written subpage */
2528 if (!oob_required || (step < start_step) || (step > end_step))
2529 memset(oob_buf, 0xff, oob_bytes);
2530
Brian Norrisd6a950802013-08-08 17:16:36 -07002531 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302532 ecc_calc += ecc_bytes;
2533 oob_buf += oob_bytes;
2534 }
2535
2536 /* copy calculated ECC for whole page to chip->buffer->oob */
2537 /* this include masked-value(0xFF) for unwritten subpages */
2538 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002539 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2540 chip->ecc.total);
2541 if (ret)
2542 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302543
2544 /* write OOB buffer to NAND device */
2545 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2546
2547 return 0;
2548}
2549
2550
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002551/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002552 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002553 * @mtd: mtd info structure
2554 * @chip: nand chip info structure
2555 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002556 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002557 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002558 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002559 * The hw generator calculates the error syndrome automatically. Therefore we
2560 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002561 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002562static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002563 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002564 const uint8_t *buf, int oob_required,
2565 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002566{
2567 int i, eccsize = chip->ecc.size;
2568 int eccbytes = chip->ecc.bytes;
2569 int eccsteps = chip->ecc.steps;
2570 const uint8_t *p = buf;
2571 uint8_t *oob = chip->oob_poi;
2572
2573 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2574
2575 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2576 chip->write_buf(mtd, p, eccsize);
2577
2578 if (chip->ecc.prepad) {
2579 chip->write_buf(mtd, oob, chip->ecc.prepad);
2580 oob += chip->ecc.prepad;
2581 }
2582
2583 chip->ecc.calculate(mtd, p, oob);
2584 chip->write_buf(mtd, oob, eccbytes);
2585 oob += eccbytes;
2586
2587 if (chip->ecc.postpad) {
2588 chip->write_buf(mtd, oob, chip->ecc.postpad);
2589 oob += chip->ecc.postpad;
2590 }
2591 }
2592
2593 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002594 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002595 if (i)
2596 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002597
2598 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002599}
2600
2601/**
David Woodhouse956e9442006-09-25 17:12:39 +01002602 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002603 * @mtd: MTD device structure
2604 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302605 * @offset: address offset within the page
2606 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002607 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002608 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002609 * @page: page number to write
2610 * @cached: cached programming
2611 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002612 */
2613static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302614 uint32_t offset, int data_len, const uint8_t *buf,
2615 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002616{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302617 int status, subpage;
2618
2619 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2620 chip->ecc.write_subpage)
2621 subpage = offset || (data_len < mtd->writesize);
2622 else
2623 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002624
2625 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2626
David Woodhouse956e9442006-09-25 17:12:39 +01002627 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302628 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002629 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302630 else if (subpage)
2631 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002632 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002633 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002634 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2635 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002636
2637 if (status < 0)
2638 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002639
2640 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002641 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002642 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002643 */
2644 cached = 0;
2645
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002646 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002647
2648 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002649 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002650 /*
2651 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002652 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002653 */
2654 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2655 status = chip->errstat(mtd, chip, FL_WRITING, status,
2656 page);
2657
2658 if (status & NAND_STATUS_FAIL)
2659 return -EIO;
2660 } else {
2661 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002662 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002663 }
2664
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002665 return 0;
2666}
2667
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002668/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002669 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002670 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002671 * @oob: oob data buffer
2672 * @len: oob data write length
2673 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002674 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002675static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2676 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002677{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002678 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002679 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002680
2681 /*
2682 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2683 * data from a previous OOB read.
2684 */
2685 memset(chip->oob_poi, 0xff, mtd->oobsize);
2686
Florian Fainellif8ac0412010-09-07 13:23:43 +02002687 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002688
Brian Norris0612b9d2011-08-30 18:45:40 -07002689 case MTD_OPS_PLACE_OOB:
2690 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002691 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2692 return oob + len;
2693
Boris Brezillon846031d2016-02-03 20:11:00 +01002694 case MTD_OPS_AUTO_OOB:
2695 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2696 ops->ooboffs, len);
2697 BUG_ON(ret);
2698 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002699
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002700 default:
2701 BUG();
2702 }
2703 return NULL;
2704}
2705
Florian Fainellif8ac0412010-09-07 13:23:43 +02002706#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002707
2708/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002709 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002710 * @mtd: MTD device structure
2711 * @to: offset to write to
2712 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002713 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002714 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002715 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002716static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2717 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002718{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002719 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002720 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002721 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002722
2723 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002724 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002725
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002726 uint8_t *oob = ops->oobbuf;
2727 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302728 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002729 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002730
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002731 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002732 if (!writelen)
2733 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002734
Brian Norris8b6e50c2011-05-25 14:59:01 -07002735 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002736 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002737 pr_notice("%s: attempt to write non page aligned data\n",
2738 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002739 return -EINVAL;
2740 }
2741
Thomas Gleixner29072b92006-09-28 15:38:36 +02002742 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002743
Thomas Gleixner6a930962006-06-28 00:11:45 +02002744 chipnr = (int)(to >> chip->chip_shift);
2745 chip->select_chip(mtd, chipnr);
2746
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002747 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002748 if (nand_check_wp(mtd)) {
2749 ret = -EIO;
2750 goto err_out;
2751 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002752
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002753 realpage = (int)(to >> chip->page_shift);
2754 page = realpage & chip->pagemask;
2755 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2756
2757 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002758 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2759 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002760 chip->pagebuf = -1;
2761
Maxim Levitsky782ce792010-02-22 20:39:36 +02002762 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002763 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2764 ret = -EINVAL;
2765 goto err_out;
2766 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002767
Florian Fainellif8ac0412010-09-07 13:23:43 +02002768 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002769 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002770 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002771 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002772 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002773 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002774
Kamal Dasu66507c72014-05-01 20:51:19 -04002775 if (part_pagewr)
2776 use_bufpoi = 1;
2777 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2778 use_bufpoi = !virt_addr_valid(buf);
2779 else
2780 use_bufpoi = 0;
2781
2782 /* Partial page write?, or need to use bounce buffer */
2783 if (use_bufpoi) {
2784 pr_debug("%s: using write bounce buffer for buf@%p\n",
2785 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002786 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002787 if (part_pagewr)
2788 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002789 chip->pagebuf = -1;
2790 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2791 memcpy(&chip->buffers->databuf[column], buf, bytes);
2792 wbuf = chip->buffers->databuf;
2793 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002794
Maxim Levitsky782ce792010-02-22 20:39:36 +02002795 if (unlikely(oob)) {
2796 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002797 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002798 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002799 } else {
2800 /* We still need to erase leftover OOB data */
2801 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002802 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302803 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2804 oob_required, page, cached,
2805 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002806 if (ret)
2807 break;
2808
2809 writelen -= bytes;
2810 if (!writelen)
2811 break;
2812
Thomas Gleixner29072b92006-09-28 15:38:36 +02002813 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002814 buf += bytes;
2815 realpage++;
2816
2817 page = realpage & chip->pagemask;
2818 /* Check, if we cross a chip boundary */
2819 if (!page) {
2820 chipnr++;
2821 chip->select_chip(mtd, -1);
2822 chip->select_chip(mtd, chipnr);
2823 }
2824 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002825
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002826 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002827 if (unlikely(oob))
2828 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002829
2830err_out:
2831 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002832 return ret;
2833}
2834
2835/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002836 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002837 * @mtd: MTD device structure
2838 * @to: offset to write to
2839 * @len: number of bytes to write
2840 * @retlen: pointer to variable to store the number of written bytes
2841 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002842 *
2843 * NAND write with ECC. Used when performing writes in interrupt context, this
2844 * may for example be called by mtdoops when writing an oops while in panic.
2845 */
2846static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2847 size_t *retlen, const uint8_t *buf)
2848{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002849 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002850 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002851 int ret;
2852
Brian Norris8b6e50c2011-05-25 14:59:01 -07002853 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002854 panic_nand_wait(mtd, chip, 400);
2855
Brian Norris8b6e50c2011-05-25 14:59:01 -07002856 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002857 panic_nand_get_device(chip, mtd, FL_WRITING);
2858
Brian Norris0ec56dc2015-02-28 02:02:30 -08002859 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002860 ops.len = len;
2861 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002862 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002863
Brian Norris4a89ff82011-08-30 18:45:45 -07002864 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002865
Brian Norris4a89ff82011-08-30 18:45:45 -07002866 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002867 return ret;
2868}
2869
2870/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002871 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002872 * @mtd: MTD device structure
2873 * @to: offset to write to
2874 * @len: number of bytes to write
2875 * @retlen: pointer to variable to store the number of written bytes
2876 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002878 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002880static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002881 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Brian Norris4a89ff82011-08-30 18:45:45 -07002883 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002884 int ret;
2885
Huang Shijie6a8214a2012-11-19 14:43:30 +08002886 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002887 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002888 ops.len = len;
2889 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002890 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002891 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002892 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002893 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002894 return ret;
2895}
2896
2897/**
2898 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002899 * @mtd: MTD device structure
2900 * @to: offset to write to
2901 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002902 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002903 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002904 */
2905static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2906 struct mtd_oob_ops *ops)
2907{
Adrian Hunter03736152007-01-31 17:58:29 +02002908 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002909 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Brian Norris289c0522011-07-19 10:06:09 -07002911 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302912 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Boris BREZILLON29f10582016-03-07 10:46:52 +01002914 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002917 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002918 pr_debug("%s: attempt to write past end of page\n",
2919 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return -EINVAL;
2921 }
2922
Adrian Hunter03736152007-01-31 17:58:29 +02002923 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002924 pr_debug("%s: attempt to start write outside oob\n",
2925 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002926 return -EINVAL;
2927 }
2928
Jason Liu775adc3d42011-02-25 13:06:18 +08002929 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002930 if (unlikely(to >= mtd->size ||
2931 ops->ooboffs + ops->ooblen >
2932 ((mtd->size >> chip->page_shift) -
2933 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002934 pr_debug("%s: attempt to write beyond end of device\n",
2935 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002936 return -EINVAL;
2937 }
2938
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002939 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002940 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002942 /* Shift to get page */
2943 page = (int)(to >> chip->page_shift);
2944
2945 /*
2946 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2947 * of my DiskOnChip 2000 test units) will clear the whole data page too
2948 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2949 * it in the doc2000 driver in August 1999. dwmw2.
2950 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002951 nand_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002954 if (nand_check_wp(mtd)) {
2955 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002956 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002957 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002960 if (page == chip->pagebuf)
2961 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002963 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002964
Brian Norris0612b9d2011-08-30 18:45:40 -07002965 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002966 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2967 else
2968 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002969
Huang Shijieb0bb6902012-11-19 14:43:29 +08002970 chip->select_chip(mtd, -1);
2971
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002972 if (status)
2973 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Vitaly Wool70145682006-11-03 18:20:38 +03002975 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002977 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002978}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002980/**
2981 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002982 * @mtd: MTD device structure
2983 * @to: offset to write to
2984 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002985 */
2986static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2987 struct mtd_oob_ops *ops)
2988{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002989 int ret = -ENOTSUPP;
2990
2991 ops->retlen = 0;
2992
2993 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002994 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002995 pr_debug("%s: attempt to write beyond end of device\n",
2996 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002997 return -EINVAL;
2998 }
2999
Huang Shijie6a8214a2012-11-19 14:43:30 +08003000 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003001
Florian Fainellif8ac0412010-09-07 13:23:43 +02003002 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003003 case MTD_OPS_PLACE_OOB:
3004 case MTD_OPS_AUTO_OOB:
3005 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003006 break;
3007
3008 default:
3009 goto out;
3010 }
3011
3012 if (!ops->datbuf)
3013 ret = nand_do_write_oob(mtd, to, ops);
3014 else
3015 ret = nand_do_write_ops(mtd, to, ops);
3016
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003017out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003018 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return ret;
3020}
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022/**
Brian Norris49c50b92014-05-06 16:02:19 -07003023 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003024 * @mtd: MTD device structure
3025 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 *
Brian Norris49c50b92014-05-06 16:02:19 -07003027 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 */
Brian Norris49c50b92014-05-06 16:02:19 -07003029static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003031 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003033 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3034 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003035
3036 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003041 * @mtd: MTD device structure
3042 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003044 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003046static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
David Woodhousee0c7d762006-05-13 18:07:53 +01003048 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003052 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003053 * @mtd: MTD device structure
3054 * @instr: erase instruction
3055 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003057 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003059int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3060 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Adrian Hunter69423d92008-12-10 13:37:21 +00003062 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003063 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003064 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Brian Norris289c0522011-07-19 10:06:09 -07003066 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3067 __func__, (unsigned long long)instr->addr,
3068 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303070 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003074 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003077 page = (int)(instr->addr >> chip->page_shift);
3078 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003081 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
3083 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003084 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 /* Check, if it is write protected */
3087 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003088 pr_debug("%s: device is write protected!\n",
3089 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 instr->state = MTD_ERASE_FAILED;
3091 goto erase_exit;
3092 }
3093
3094 /* Loop through the pages */
3095 len = instr->len;
3096
3097 instr->state = MTD_ERASING;
3098
3099 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003100 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003101 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303102 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003103 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3104 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 instr->state = MTD_ERASE_FAILED;
3106 goto erase_exit;
3107 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003108
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003109 /*
3110 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003111 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003112 */
3113 if (page <= chip->pagebuf && chip->pagebuf <
3114 (page + pages_per_block))
3115 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Brian Norris49c50b92014-05-06 16:02:19 -07003117 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003119 /*
3120 * See if operation failed and additional status checks are
3121 * available
3122 */
3123 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3124 status = chip->errstat(mtd, chip, FL_ERASING,
3125 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003128 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003129 pr_debug("%s: failed erase, page 0x%08x\n",
3130 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003132 instr->fail_addr =
3133 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 goto erase_exit;
3135 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003138 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 page += pages_per_block;
3140
3141 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003142 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003144 chip->select_chip(mtd, -1);
3145 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
3147 }
3148 instr->state = MTD_ERASE_DONE;
3149
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003150erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003155 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 nand_release_device(mtd);
3157
David Woodhouse49defc02007-10-06 15:01:59 -04003158 /* Do call back function */
3159 if (!ret)
3160 mtd_erase_callback(instr);
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 /* Return more or less happy */
3163 return ret;
3164}
3165
3166/**
3167 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003170 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003172static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Brian Norris289c0522011-07-19 10:06:09 -07003174 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003177 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003179 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003183 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003184 * @mtd: MTD device structure
3185 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003187static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303189 struct nand_chip *chip = mtd_to_nand(mtd);
3190 int chipnr = (int)(offs >> chip->chip_shift);
3191 int ret;
3192
3193 /* Select the NAND device */
3194 nand_get_device(mtd, FL_READING);
3195 chip->select_chip(mtd, chipnr);
3196
3197 ret = nand_block_checkbad(mtd, offs, 0);
3198
3199 chip->select_chip(mtd, -1);
3200 nand_release_device(mtd);
3201
3202 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
3205/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003206 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003207 * @mtd: MTD device structure
3208 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003210static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 int ret;
3213
Florian Fainellif8ac0412010-09-07 13:23:43 +02003214 ret = nand_block_isbad(mtd, ofs);
3215 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003216 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (ret > 0)
3218 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003219 return ret;
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Brian Norris5a0edb22013-07-30 17:52:58 -07003222 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
3225/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003226 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3227 * @mtd: MTD device structure
3228 * @chip: nand chip info structure
3229 * @addr: feature address.
3230 * @subfeature_param: the subfeature parameters, a four bytes array.
3231 */
3232static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3233 int addr, uint8_t *subfeature_param)
3234{
3235 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003236 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003237
David Mosbergerd914c932013-05-29 15:30:13 +03003238 if (!chip->onfi_version ||
3239 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3240 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003241 return -EINVAL;
3242
3243 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003244 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3245 chip->write_byte(mtd, subfeature_param[i]);
3246
Huang Shijie7db03ec2012-09-13 14:57:52 +08003247 status = chip->waitfunc(mtd, chip);
3248 if (status & NAND_STATUS_FAIL)
3249 return -EIO;
3250 return 0;
3251}
3252
3253/**
3254 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3255 * @mtd: MTD device structure
3256 * @chip: nand chip info structure
3257 * @addr: feature address.
3258 * @subfeature_param: the subfeature parameters, a four bytes array.
3259 */
3260static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3261 int addr, uint8_t *subfeature_param)
3262{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003263 int i;
3264
David Mosbergerd914c932013-05-29 15:30:13 +03003265 if (!chip->onfi_version ||
3266 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3267 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003268 return -EINVAL;
3269
Huang Shijie7db03ec2012-09-13 14:57:52 +08003270 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003271 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3272 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003273 return 0;
3274}
3275
3276/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003277 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003278 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003279 */
3280static int nand_suspend(struct mtd_info *mtd)
3281{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003282 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003283}
3284
3285/**
3286 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003287 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003288 */
3289static void nand_resume(struct mtd_info *mtd)
3290{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003291 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003292
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003293 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003294 nand_release_device(mtd);
3295 else
Brian Norrisd0370212011-07-19 10:06:08 -07003296 pr_err("%s called for a chip which is not in suspended state\n",
3297 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003298}
3299
Scott Branden72ea4032014-11-20 11:18:05 -08003300/**
3301 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3302 * prevent further operations
3303 * @mtd: MTD device structure
3304 */
3305static void nand_shutdown(struct mtd_info *mtd)
3306{
Brian Norris9ca641b2015-11-09 16:37:28 -08003307 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003308}
3309
Brian Norris8b6e50c2011-05-25 14:59:01 -07003310/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003311static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003314 if (!chip->chip_delay)
3315 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003318 if (chip->cmdfunc == NULL)
3319 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003322 if (chip->waitfunc == NULL)
3323 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003325 if (!chip->select_chip)
3326 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003327
Huang Shijie4204ccc2013-08-16 10:10:07 +08003328 /* set for ONFI nand */
3329 if (!chip->onfi_set_features)
3330 chip->onfi_set_features = nand_onfi_set_features;
3331 if (!chip->onfi_get_features)
3332 chip->onfi_get_features = nand_onfi_get_features;
3333
Brian Norris68e80782013-07-18 01:17:02 -07003334 /* If called twice, pointers that depend on busw may need to be reset */
3335 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003336 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3337 if (!chip->read_word)
3338 chip->read_word = nand_read_word;
3339 if (!chip->block_bad)
3340 chip->block_bad = nand_block_bad;
3341 if (!chip->block_markbad)
3342 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003343 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003344 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003345 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3346 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003347 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003348 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003349 if (!chip->scan_bbt)
3350 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003351
3352 if (!chip->controller) {
3353 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003354 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003355 }
3356
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003357}
3358
Brian Norris8b6e50c2011-05-25 14:59:01 -07003359/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003360static void sanitize_string(uint8_t *s, size_t len)
3361{
3362 ssize_t i;
3363
Brian Norris8b6e50c2011-05-25 14:59:01 -07003364 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003365 s[len - 1] = 0;
3366
Brian Norris8b6e50c2011-05-25 14:59:01 -07003367 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003368 for (i = 0; i < len - 1; i++) {
3369 if (s[i] < ' ' || s[i] > 127)
3370 s[i] = '?';
3371 }
3372
Brian Norris8b6e50c2011-05-25 14:59:01 -07003373 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003374 strim(s);
3375}
3376
3377static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3378{
3379 int i;
3380 while (len--) {
3381 crc ^= *p++ << 8;
3382 for (i = 0; i < 8; i++)
3383 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3384 }
3385
3386 return crc;
3387}
3388
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003389/* Parse the Extended Parameter Page. */
3390static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3391 struct nand_chip *chip, struct nand_onfi_params *p)
3392{
3393 struct onfi_ext_param_page *ep;
3394 struct onfi_ext_section *s;
3395 struct onfi_ext_ecc_info *ecc;
3396 uint8_t *cursor;
3397 int ret = -EINVAL;
3398 int len;
3399 int i;
3400
3401 len = le16_to_cpu(p->ext_param_page_length) * 16;
3402 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003403 if (!ep)
3404 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003405
3406 /* Send our own NAND_CMD_PARAM. */
3407 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3408
3409 /* Use the Change Read Column command to skip the ONFI param pages. */
3410 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3411 sizeof(*p) * p->num_of_param_pages , -1);
3412
3413 /* Read out the Extended Parameter Page. */
3414 chip->read_buf(mtd, (uint8_t *)ep, len);
3415 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3416 != le16_to_cpu(ep->crc))) {
3417 pr_debug("fail in the CRC.\n");
3418 goto ext_out;
3419 }
3420
3421 /*
3422 * Check the signature.
3423 * Do not strictly follow the ONFI spec, maybe changed in future.
3424 */
3425 if (strncmp(ep->sig, "EPPS", 4)) {
3426 pr_debug("The signature is invalid.\n");
3427 goto ext_out;
3428 }
3429
3430 /* find the ECC section. */
3431 cursor = (uint8_t *)(ep + 1);
3432 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3433 s = ep->sections + i;
3434 if (s->type == ONFI_SECTION_TYPE_2)
3435 break;
3436 cursor += s->length * 16;
3437 }
3438 if (i == ONFI_EXT_SECTION_MAX) {
3439 pr_debug("We can not find the ECC section.\n");
3440 goto ext_out;
3441 }
3442
3443 /* get the info we want. */
3444 ecc = (struct onfi_ext_ecc_info *)cursor;
3445
Brian Norris4ae7d222013-09-16 18:20:21 -07003446 if (!ecc->codeword_size) {
3447 pr_debug("Invalid codeword size\n");
3448 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003449 }
3450
Brian Norris4ae7d222013-09-16 18:20:21 -07003451 chip->ecc_strength_ds = ecc->ecc_bits;
3452 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003453 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003454
3455ext_out:
3456 kfree(ep);
3457 return ret;
3458}
3459
Brian Norris8429bb32013-12-03 15:51:09 -08003460static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3461{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003462 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003463 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3464
3465 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3466 feature);
3467}
3468
3469/*
3470 * Configure chip properties from Micron vendor-specific ONFI table
3471 */
3472static void nand_onfi_detect_micron(struct nand_chip *chip,
3473 struct nand_onfi_params *p)
3474{
3475 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3476
3477 if (le16_to_cpu(p->vendor_revision) < 1)
3478 return;
3479
3480 chip->read_retries = micron->read_retry_options;
3481 chip->setup_read_retry = nand_setup_read_retry_micron;
3482}
3483
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003484/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003485 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003486 */
3487static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003488 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003489{
3490 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003491 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003492 int val;
3493
Brian Norris7854d3f2011-06-23 14:12:08 -07003494 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003495 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3496 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3497 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3498 return 0;
3499
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003500 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3501 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003502 for (j = 0; j < sizeof(*p); j++)
3503 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003504 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3505 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003506 break;
3507 }
3508 }
3509
Brian Norrisc7f23a72013-08-13 10:51:55 -07003510 if (i == 3) {
3511 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003512 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003513 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003514
Brian Norris8b6e50c2011-05-25 14:59:01 -07003515 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003516 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003517 if (val & (1 << 5))
3518 chip->onfi_version = 23;
3519 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003520 chip->onfi_version = 22;
3521 else if (val & (1 << 3))
3522 chip->onfi_version = 21;
3523 else if (val & (1 << 2))
3524 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003525 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003526 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003527
3528 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003529 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003530 return 0;
3531 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003532
3533 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3534 sanitize_string(p->model, sizeof(p->model));
3535 if (!mtd->name)
3536 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003537
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003538 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003539
3540 /*
3541 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3542 * (don't ask me who thought of this...). MTD assumes that these
3543 * dimensions will be power-of-2, so just truncate the remaining area.
3544 */
3545 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3546 mtd->erasesize *= mtd->writesize;
3547
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003548 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003549
3550 /* See erasesize comment */
3551 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003552 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003553 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003554
3555 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003556 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003557 else
3558 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003559
Huang Shijie10c86ba2013-05-17 11:17:26 +08003560 if (p->ecc_bits != 0xff) {
3561 chip->ecc_strength_ds = p->ecc_bits;
3562 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003563 } else if (chip->onfi_version >= 21 &&
3564 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3565
3566 /*
3567 * The nand_flash_detect_ext_param_page() uses the
3568 * Change Read Column command which maybe not supported
3569 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3570 * now. We do not replace user supplied command function.
3571 */
3572 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3573 chip->cmdfunc = nand_command_lp;
3574
3575 /* The Extended Parameter Page is supported since ONFI 2.1. */
3576 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003577 pr_warn("Failed to detect ONFI extended param page\n");
3578 } else {
3579 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003580 }
3581
Brian Norris8429bb32013-12-03 15:51:09 -08003582 if (p->jedec_id == NAND_MFR_MICRON)
3583 nand_onfi_detect_micron(chip, p);
3584
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003585 return 1;
3586}
3587
3588/*
Huang Shijie91361812014-02-21 13:39:40 +08003589 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3590 */
3591static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3592 int *busw)
3593{
3594 struct nand_jedec_params *p = &chip->jedec_params;
3595 struct jedec_ecc_info *ecc;
3596 int val;
3597 int i, j;
3598
3599 /* Try JEDEC for unknown chip or LP */
3600 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3601 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3602 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3603 chip->read_byte(mtd) != 'C')
3604 return 0;
3605
3606 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3607 for (i = 0; i < 3; i++) {
3608 for (j = 0; j < sizeof(*p); j++)
3609 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3610
3611 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3612 le16_to_cpu(p->crc))
3613 break;
3614 }
3615
3616 if (i == 3) {
3617 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3618 return 0;
3619 }
3620
3621 /* Check version */
3622 val = le16_to_cpu(p->revision);
3623 if (val & (1 << 2))
3624 chip->jedec_version = 10;
3625 else if (val & (1 << 1))
3626 chip->jedec_version = 1; /* vendor specific version */
3627
3628 if (!chip->jedec_version) {
3629 pr_info("unsupported JEDEC version: %d\n", val);
3630 return 0;
3631 }
3632
3633 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3634 sanitize_string(p->model, sizeof(p->model));
3635 if (!mtd->name)
3636 mtd->name = p->model;
3637
3638 mtd->writesize = le32_to_cpu(p->byte_per_page);
3639
3640 /* Please reference to the comment for nand_flash_detect_onfi. */
3641 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3642 mtd->erasesize *= mtd->writesize;
3643
3644 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3645
3646 /* Please reference to the comment for nand_flash_detect_onfi. */
3647 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3648 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3649 chip->bits_per_cell = p->bits_per_cell;
3650
3651 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3652 *busw = NAND_BUSWIDTH_16;
3653 else
3654 *busw = 0;
3655
3656 /* ECC info */
3657 ecc = &p->ecc_info[0];
3658
3659 if (ecc->codeword_size >= 9) {
3660 chip->ecc_strength_ds = ecc->ecc_bits;
3661 chip->ecc_step_ds = 1 << ecc->codeword_size;
3662 } else {
3663 pr_warn("Invalid codeword size\n");
3664 }
3665
3666 return 1;
3667}
3668
3669/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003670 * nand_id_has_period - Check if an ID string has a given wraparound period
3671 * @id_data: the ID string
3672 * @arrlen: the length of the @id_data array
3673 * @period: the period of repitition
3674 *
3675 * Check if an ID string is repeated within a given sequence of bytes at
3676 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003677 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003678 * if the repetition has a period of @period; otherwise, returns zero.
3679 */
3680static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3681{
3682 int i, j;
3683 for (i = 0; i < period; i++)
3684 for (j = i + period; j < arrlen; j += period)
3685 if (id_data[i] != id_data[j])
3686 return 0;
3687 return 1;
3688}
3689
3690/*
3691 * nand_id_len - Get the length of an ID string returned by CMD_READID
3692 * @id_data: the ID string
3693 * @arrlen: the length of the @id_data array
3694
3695 * Returns the length of the ID string, according to known wraparound/trailing
3696 * zero patterns. If no pattern exists, returns the length of the array.
3697 */
3698static int nand_id_len(u8 *id_data, int arrlen)
3699{
3700 int last_nonzero, period;
3701
3702 /* Find last non-zero byte */
3703 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3704 if (id_data[last_nonzero])
3705 break;
3706
3707 /* All zeros */
3708 if (last_nonzero < 0)
3709 return 0;
3710
3711 /* Calculate wraparound period */
3712 for (period = 1; period < arrlen; period++)
3713 if (nand_id_has_period(id_data, arrlen, period))
3714 break;
3715
3716 /* There's a repeated pattern */
3717 if (period < arrlen)
3718 return period;
3719
3720 /* There are trailing zeros */
3721 if (last_nonzero < arrlen - 1)
3722 return last_nonzero + 1;
3723
3724 /* No pattern detected */
3725 return arrlen;
3726}
3727
Huang Shijie7db906b2013-09-25 14:58:11 +08003728/* Extract the bits of per cell from the 3rd byte of the extended ID */
3729static int nand_get_bits_per_cell(u8 cellinfo)
3730{
3731 int bits;
3732
3733 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3734 bits >>= NAND_CI_CELLTYPE_SHIFT;
3735 return bits + 1;
3736}
3737
Brian Norrise3b88bd2012-09-24 20:40:52 -07003738/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003739 * Many new NAND share similar device ID codes, which represent the size of the
3740 * chip. The rest of the parameters must be decoded according to generic or
3741 * manufacturer-specific "extended ID" decoding patterns.
3742 */
3743static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3744 u8 id_data[8], int *busw)
3745{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003746 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003747 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003748 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003749 /* The 4th id byte is the important one */
3750 extid = id_data[3];
3751
Brian Norrise3b88bd2012-09-24 20:40:52 -07003752 id_len = nand_id_len(id_data, 8);
3753
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003754 /*
3755 * Field definitions are in the following datasheets:
3756 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003757 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003758 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003759 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003760 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3761 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003762 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003763 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003764 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003765 /* Calc pagesize */
3766 mtd->writesize = 2048 << (extid & 0x03);
3767 extid >>= 2;
3768 /* Calc oobsize */
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003769 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003770 case 1:
3771 mtd->oobsize = 128;
3772 break;
3773 case 2:
3774 mtd->oobsize = 218;
3775 break;
3776 case 3:
3777 mtd->oobsize = 400;
3778 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003779 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003780 mtd->oobsize = 436;
3781 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003782 case 5:
3783 mtd->oobsize = 512;
3784 break;
3785 case 6:
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003786 mtd->oobsize = 640;
3787 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003788 case 7:
3789 default: /* Other cases are "reserved" (unknown) */
3790 mtd->oobsize = 1024;
3791 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003792 }
3793 extid >>= 2;
3794 /* Calc blocksize */
3795 mtd->erasesize = (128 * 1024) <<
3796 (((extid >> 1) & 0x04) | (extid & 0x03));
3797 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003798 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003799 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003800 unsigned int tmp;
3801
3802 /* Calc pagesize */
3803 mtd->writesize = 2048 << (extid & 0x03);
3804 extid >>= 2;
3805 /* Calc oobsize */
3806 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3807 case 0:
3808 mtd->oobsize = 128;
3809 break;
3810 case 1:
3811 mtd->oobsize = 224;
3812 break;
3813 case 2:
3814 mtd->oobsize = 448;
3815 break;
3816 case 3:
3817 mtd->oobsize = 64;
3818 break;
3819 case 4:
3820 mtd->oobsize = 32;
3821 break;
3822 case 5:
3823 mtd->oobsize = 16;
3824 break;
3825 default:
3826 mtd->oobsize = 640;
3827 break;
3828 }
3829 extid >>= 2;
3830 /* Calc blocksize */
3831 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3832 if (tmp < 0x03)
3833 mtd->erasesize = (128 * 1024) << tmp;
3834 else if (tmp == 0x03)
3835 mtd->erasesize = 768 * 1024;
3836 else
3837 mtd->erasesize = (64 * 1024) << tmp;
3838 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003839 } else {
3840 /* Calc pagesize */
3841 mtd->writesize = 1024 << (extid & 0x03);
3842 extid >>= 2;
3843 /* Calc oobsize */
3844 mtd->oobsize = (8 << (extid & 0x01)) *
3845 (mtd->writesize >> 9);
3846 extid >>= 2;
3847 /* Calc blocksize. Blocksize is multiples of 64KiB */
3848 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3849 extid >>= 2;
3850 /* Get buswidth information */
3851 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003852
3853 /*
3854 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3855 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3856 * follows:
3857 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3858 * 110b -> 24nm
3859 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3860 */
3861 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003862 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003863 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3864 !(id_data[4] & 0x80) /* !BENAND */) {
3865 mtd->oobsize = 32 * mtd->writesize >> 9;
3866 }
3867
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003868 }
3869}
3870
3871/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003872 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3873 * decodes a matching ID table entry and assigns the MTD size parameters for
3874 * the chip.
3875 */
3876static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3877 struct nand_flash_dev *type, u8 id_data[8],
3878 int *busw)
3879{
3880 int maf_id = id_data[0];
3881
3882 mtd->erasesize = type->erasesize;
3883 mtd->writesize = type->pagesize;
3884 mtd->oobsize = mtd->writesize / 32;
3885 *busw = type->options & NAND_BUSWIDTH_16;
3886
Huang Shijie1c195e92013-09-25 14:58:12 +08003887 /* All legacy ID NAND are small-page, SLC */
3888 chip->bits_per_cell = 1;
3889
Brian Norrisf23a4812012-09-24 20:40:51 -07003890 /*
3891 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3892 * some Spansion chips have erasesize that conflicts with size
3893 * listed in nand_ids table.
3894 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3895 */
3896 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3897 && id_data[6] == 0x00 && id_data[7] == 0x00
3898 && mtd->writesize == 512) {
3899 mtd->erasesize = 128 * 1024;
3900 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3901 }
3902}
3903
3904/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003905 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3906 * heuristic patterns using various detected parameters (e.g., manufacturer,
3907 * page size, cell-type information).
3908 */
3909static void nand_decode_bbm_options(struct mtd_info *mtd,
3910 struct nand_chip *chip, u8 id_data[8])
3911{
3912 int maf_id = id_data[0];
3913
3914 /* Set the bad block position */
3915 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3916 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3917 else
3918 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3919
3920 /*
3921 * Bad block marker is stored in the last page of each block on Samsung
3922 * and Hynix MLC devices; stored in first two pages of each block on
3923 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3924 * AMD/Spansion, and Macronix. All others scan only the first page.
3925 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003926 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003927 (maf_id == NAND_MFR_SAMSUNG ||
3928 maf_id == NAND_MFR_HYNIX))
3929 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003930 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003931 (maf_id == NAND_MFR_SAMSUNG ||
3932 maf_id == NAND_MFR_HYNIX ||
3933 maf_id == NAND_MFR_TOSHIBA ||
3934 maf_id == NAND_MFR_AMD ||
3935 maf_id == NAND_MFR_MACRONIX)) ||
3936 (mtd->writesize == 2048 &&
3937 maf_id == NAND_MFR_MICRON))
3938 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3939}
3940
Huang Shijieec6e87e2013-03-15 11:01:00 +08003941static inline bool is_full_id_nand(struct nand_flash_dev *type)
3942{
3943 return type->id_len;
3944}
3945
3946static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3947 struct nand_flash_dev *type, u8 *id_data, int *busw)
3948{
3949 if (!strncmp(type->id, id_data, type->id_len)) {
3950 mtd->writesize = type->pagesize;
3951 mtd->erasesize = type->erasesize;
3952 mtd->oobsize = type->oobsize;
3953
Huang Shijie7db906b2013-09-25 14:58:11 +08003954 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003955 chip->chipsize = (uint64_t)type->chipsize << 20;
3956 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003957 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3958 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003959 chip->onfi_timing_mode_default =
3960 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003961
3962 *busw = type->options & NAND_BUSWIDTH_16;
3963
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003964 if (!mtd->name)
3965 mtd->name = type->name;
3966
Huang Shijieec6e87e2013-03-15 11:01:00 +08003967 return true;
3968 }
3969 return false;
3970}
3971
Brian Norris7e74c2d2012-09-24 20:40:49 -07003972/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003973 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003974 */
3975static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003976 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003977 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003978 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003979{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003980 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003981 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003982 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003985 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Karl Beldanef89a882008-09-15 14:37:29 +02003987 /*
3988 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003989 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003990 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02003991 nand_reset(chip);
Karl Beldanef89a882008-09-15 14:37:29 +02003992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003994 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003997 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003998 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Brian Norris8b6e50c2011-05-25 14:59:01 -07004000 /*
4001 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004002 * interface concerns can cause random data which looks like a
4003 * possibly credible NAND flash to appear. If the two results do
4004 * not match, ignore the device completely.
4005 */
4006
4007 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4008
Brian Norris4aef9b72012-09-24 20:40:48 -07004009 /* Read entire ID string */
4010 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004011 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004012
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004013 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004014 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004015 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01004016 return ERR_PTR(-ENODEV);
4017 }
4018
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004019 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004020 type = nand_flash_ids;
4021
Huang Shijieec6e87e2013-03-15 11:01:00 +08004022 for (; type->name != NULL; type++) {
4023 if (is_full_id_nand(type)) {
4024 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4025 goto ident_done;
4026 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004027 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004028 }
4029 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004030
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004031 chip->onfi_version = 0;
4032 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004033 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07004034 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004035 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004036
4037 /* Check if the chip is JEDEC compliant */
4038 if (nand_flash_detect_jedec(mtd, chip, &busw))
4039 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004040 }
4041
David Woodhouse5e81e882010-02-26 18:32:56 +00004042 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004043 return ERR_PTR(-ENODEV);
4044
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004045 if (!mtd->name)
4046 mtd->name = type->name;
4047
Adrian Hunter69423d92008-12-10 13:37:21 +00004048 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004049
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02004050 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004051 /* Decode parameters from extended ID */
4052 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004053 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07004054 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004055 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004056 /* Get chip options */
4057 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004058
Brian Norris8b6e50c2011-05-25 14:59:01 -07004059 /*
4060 * Check if chip is not a Samsung device. Do not clear the
4061 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004062 */
4063 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4064 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4065ident_done:
4066
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004067 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01004068 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004069 if (nand_manuf_ids[maf_idx].id == *maf_id)
4070 break;
4071 }
4072
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004073 if (chip->options & NAND_BUSWIDTH_AUTO) {
4074 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4075 chip->options |= busw;
4076 nand_set_defaults(chip, busw);
4077 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4078 /*
4079 * Check, if buswidth is correct. Hardware drivers should set
4080 * chip correct!
4081 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004082 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4083 *maf_id, *dev_id);
4084 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4085 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004086 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4087 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004088 return ERR_PTR(-EINVAL);
4089 }
4090
Brian Norris7e74c2d2012-09-24 20:40:49 -07004091 nand_decode_bbm_options(mtd, chip, id_data);
4092
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004093 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004094 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004095 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004096 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004097
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004098 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004099 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004100 if (chip->chipsize & 0xffffffff)
4101 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004102 else {
4103 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4104 chip->chip_shift += 32 - 1;
4105 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004106
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004107 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004108 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004109
Brian Norris8b6e50c2011-05-25 14:59:01 -07004110 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004111 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4112 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004113
Ezequiel Garcia20171642013-11-25 08:30:31 -03004114 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4115 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004116
4117 if (chip->onfi_version)
4118 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4119 chip->onfi_params.model);
4120 else if (chip->jedec_version)
4121 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4122 chip->jedec_params.model);
4123 else
4124 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4125 type->name);
4126
Rafał Miłecki3755a992014-10-21 00:01:04 +02004127 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004128 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004129 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004130 return type;
4131}
4132
Boris Brezillond48f62b2016-04-01 14:54:32 +02004133static const char * const nand_ecc_modes[] = {
4134 [NAND_ECC_NONE] = "none",
4135 [NAND_ECC_SOFT] = "soft",
4136 [NAND_ECC_HW] = "hw",
4137 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4138 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004139};
4140
4141static int of_get_nand_ecc_mode(struct device_node *np)
4142{
4143 const char *pm;
4144 int err, i;
4145
4146 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4147 if (err < 0)
4148 return err;
4149
4150 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4151 if (!strcasecmp(pm, nand_ecc_modes[i]))
4152 return i;
4153
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004154 /*
4155 * For backward compatibility we support few obsoleted values that don't
4156 * have their mappings into nand_ecc_modes_t anymore (they were merged
4157 * with other enums).
4158 */
4159 if (!strcasecmp(pm, "soft_bch"))
4160 return NAND_ECC_SOFT;
4161
Boris Brezillond48f62b2016-04-01 14:54:32 +02004162 return -ENODEV;
4163}
4164
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004165static const char * const nand_ecc_algos[] = {
4166 [NAND_ECC_HAMMING] = "hamming",
4167 [NAND_ECC_BCH] = "bch",
4168};
4169
Boris Brezillond48f62b2016-04-01 14:54:32 +02004170static int of_get_nand_ecc_algo(struct device_node *np)
4171{
4172 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004173 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004174
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004175 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4176 if (!err) {
4177 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4178 if (!strcasecmp(pm, nand_ecc_algos[i]))
4179 return i;
4180 return -ENODEV;
4181 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004182
4183 /*
4184 * For backward compatibility we also read "nand-ecc-mode" checking
4185 * for some obsoleted values that were specifying ECC algorithm.
4186 */
4187 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4188 if (err < 0)
4189 return err;
4190
4191 if (!strcasecmp(pm, "soft"))
4192 return NAND_ECC_HAMMING;
4193 else if (!strcasecmp(pm, "soft_bch"))
4194 return NAND_ECC_BCH;
4195
4196 return -ENODEV;
4197}
4198
4199static int of_get_nand_ecc_step_size(struct device_node *np)
4200{
4201 int ret;
4202 u32 val;
4203
4204 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4205 return ret ? ret : val;
4206}
4207
4208static int of_get_nand_ecc_strength(struct device_node *np)
4209{
4210 int ret;
4211 u32 val;
4212
4213 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4214 return ret ? ret : val;
4215}
4216
4217static int of_get_nand_bus_width(struct device_node *np)
4218{
4219 u32 val;
4220
4221 if (of_property_read_u32(np, "nand-bus-width", &val))
4222 return 8;
4223
4224 switch (val) {
4225 case 8:
4226 case 16:
4227 return val;
4228 default:
4229 return -EIO;
4230 }
4231}
4232
4233static bool of_get_nand_on_flash_bbt(struct device_node *np)
4234{
4235 return of_property_read_bool(np, "nand-on-flash-bbt");
4236}
4237
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004238static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004239{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004240 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004241 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004242
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004243 if (!dn)
4244 return 0;
4245
Brian Norris5844fee2015-01-23 00:22:27 -08004246 if (of_get_nand_bus_width(dn) == 16)
4247 chip->options |= NAND_BUSWIDTH_16;
4248
4249 if (of_get_nand_on_flash_bbt(dn))
4250 chip->bbt_options |= NAND_BBT_USE_FLASH;
4251
4252 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004253 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004254 ecc_strength = of_get_nand_ecc_strength(dn);
4255 ecc_step = of_get_nand_ecc_step_size(dn);
4256
4257 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4258 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4259 pr_err("must set both strength and step size in DT\n");
4260 return -EINVAL;
4261 }
4262
4263 if (ecc_mode >= 0)
4264 chip->ecc.mode = ecc_mode;
4265
Rafał Miłecki79082452016-03-23 11:19:02 +01004266 if (ecc_algo >= 0)
4267 chip->ecc.algo = ecc_algo;
4268
Brian Norris5844fee2015-01-23 00:22:27 -08004269 if (ecc_strength >= 0)
4270 chip->ecc.strength = ecc_strength;
4271
4272 if (ecc_step > 0)
4273 chip->ecc.size = ecc_step;
4274
Boris Brezillonba78ee02016-06-08 17:04:22 +02004275 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4276 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4277
Brian Norris5844fee2015-01-23 00:22:27 -08004278 return 0;
4279}
4280
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004281/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004282 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004283 * @mtd: MTD device structure
4284 * @maxchips: number of chips to scan for
4285 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004286 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004287 * This is the first phase of the normal nand_scan() function. It reads the
4288 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004289 *
4290 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004291int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4292 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004293{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004294 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004295 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004296 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004297 int ret;
4298
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004299 ret = nand_dt_init(chip);
4300 if (ret)
4301 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004302
Brian Norrisf7a8e382016-01-05 10:39:45 -08004303 if (!mtd->name && mtd->dev.parent)
4304 mtd->name = dev_name(mtd->dev.parent);
4305
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004306 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4307 /*
4308 * Default functions assigned for chip_select() and
4309 * cmdfunc() both expect cmd_ctrl() to be populated,
4310 * so we need to check that that's the case
4311 */
4312 pr_err("chip.cmd_ctrl() callback is not provided");
4313 return -EINVAL;
4314 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004315 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004316 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004317
4318 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004319 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4320 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004321
4322 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004323 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004324 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004325 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004326 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
4328
Boris Brezillond8e725d2016-09-15 10:32:50 +02004329 ret = nand_init_data_interface(chip);
4330 if (ret)
4331 return ret;
4332
Huang Shijie07300162012-11-09 16:23:45 +08004333 chip->select_chip(mtd, -1);
4334
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004335 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004336 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004337 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004338 /* See comment in nand_get_flash_type for reset */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02004339 nand_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004341 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004343 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004344 nand_dev_id != chip->read_byte(mtd)) {
4345 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 break;
Huang Shijie07300162012-11-09 16:23:45 +08004347 }
4348 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 }
4350 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004351 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004354 chip->numchips = i;
4355 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
David Woodhouse3b85c322006-09-25 17:06:53 +01004357 return 0;
4358}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004359EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004360
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004361static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4362{
4363 struct nand_chip *chip = mtd_to_nand(mtd);
4364 struct nand_ecc_ctrl *ecc = &chip->ecc;
4365
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004366 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004367 return -EINVAL;
4368
4369 switch (ecc->algo) {
4370 case NAND_ECC_HAMMING:
4371 ecc->calculate = nand_calculate_ecc;
4372 ecc->correct = nand_correct_data;
4373 ecc->read_page = nand_read_page_swecc;
4374 ecc->read_subpage = nand_read_subpage;
4375 ecc->write_page = nand_write_page_swecc;
4376 ecc->read_page_raw = nand_read_page_raw;
4377 ecc->write_page_raw = nand_write_page_raw;
4378 ecc->read_oob = nand_read_oob_std;
4379 ecc->write_oob = nand_write_oob_std;
4380 if (!ecc->size)
4381 ecc->size = 256;
4382 ecc->bytes = 3;
4383 ecc->strength = 1;
4384 return 0;
4385 case NAND_ECC_BCH:
4386 if (!mtd_nand_has_bch()) {
4387 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4388 return -EINVAL;
4389 }
4390 ecc->calculate = nand_bch_calculate_ecc;
4391 ecc->correct = nand_bch_correct_data;
4392 ecc->read_page = nand_read_page_swecc;
4393 ecc->read_subpage = nand_read_subpage;
4394 ecc->write_page = nand_write_page_swecc;
4395 ecc->read_page_raw = nand_read_page_raw;
4396 ecc->write_page_raw = nand_write_page_raw;
4397 ecc->read_oob = nand_read_oob_std;
4398 ecc->write_oob = nand_write_oob_std;
4399 /*
4400 * Board driver should supply ecc.size and ecc.strength
4401 * values to select how many bits are correctable.
4402 * Otherwise, default to 4 bits for large page devices.
4403 */
4404 if (!ecc->size && (mtd->oobsize >= 64)) {
4405 ecc->size = 512;
4406 ecc->strength = 4;
4407 }
4408
4409 /*
4410 * if no ecc placement scheme was provided pickup the default
4411 * large page one.
4412 */
4413 if (!mtd->ooblayout) {
4414 /* handle large page devices only */
4415 if (mtd->oobsize < 64) {
4416 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4417 return -EINVAL;
4418 }
4419
4420 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
4421 }
4422
4423 /* See nand_bch_init() for details. */
4424 ecc->bytes = 0;
4425 ecc->priv = nand_bch_init(mtd);
4426 if (!ecc->priv) {
4427 WARN(1, "BCH ECC initialization failed!\n");
4428 return -EINVAL;
4429 }
4430 return 0;
4431 default:
4432 WARN(1, "Unsupported ECC algorithm!\n");
4433 return -EINVAL;
4434 }
4435}
4436
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004437/*
4438 * Check if the chip configuration meet the datasheet requirements.
4439
4440 * If our configuration corrects A bits per B bytes and the minimum
4441 * required correction level is X bits per Y bytes, then we must ensure
4442 * both of the following are true:
4443 *
4444 * (1) A / B >= X / Y
4445 * (2) A >= X
4446 *
4447 * Requirement (1) ensures we can correct for the required bitflip density.
4448 * Requirement (2) ensures we can correct even when all bitflips are clumped
4449 * in the same sector.
4450 */
4451static bool nand_ecc_strength_good(struct mtd_info *mtd)
4452{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004453 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004454 struct nand_ecc_ctrl *ecc = &chip->ecc;
4455 int corr, ds_corr;
4456
4457 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4458 /* Not enough information */
4459 return true;
4460
4461 /*
4462 * We get the number of corrected bits per page to compare
4463 * the correction density.
4464 */
4465 corr = (mtd->writesize * ecc->strength) / ecc->size;
4466 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4467
4468 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4469}
David Woodhouse3b85c322006-09-25 17:06:53 +01004470
4471/**
4472 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004473 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004474 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004475 * This is the second phase of the normal nand_scan() function. It fills out
4476 * all the uninitialized function pointers with the defaults and scans for a
4477 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004478 */
4479int nand_scan_tail(struct mtd_info *mtd)
4480{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004481 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004482 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004483 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004484 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004485
Brian Norrise2414f42012-02-06 13:44:00 -08004486 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004487 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4488 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4489 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004490
Huang Shijief02ea4e2014-01-13 14:27:12 +08004491 if (!(chip->options & NAND_OWN_BUFFERS)) {
4492 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4493 + mtd->oobsize * 3, GFP_KERNEL);
4494 if (!nbuf)
4495 return -ENOMEM;
4496 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4497 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4498 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4499
4500 chip->buffers = nbuf;
4501 } else {
4502 if (!chip->buffers)
4503 return -ENOMEM;
4504 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004505
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004506 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004507 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004508
4509 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004510 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004511 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004512 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004513 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004514 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004517 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 break;
4519 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004520 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004521 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004524 WARN(1, "No oob scheme defined for oobsize %d\n",
4525 mtd->oobsize);
4526 ret = -EINVAL;
4527 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
4529 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004530
David Woodhouse956e9442006-09-25 17:12:39 +01004531 if (!chip->write_page)
4532 chip->write_page = nand_write_page;
4533
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004534 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004535 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004536 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004537 */
David Woodhouse956e9442006-09-25 17:12:39 +01004538
Huang Shijie97de79e02013-10-18 14:20:53 +08004539 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004540 case NAND_ECC_HW_OOB_FIRST:
4541 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004542 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004543 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4544 ret = -EINVAL;
4545 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004546 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004547 if (!ecc->read_page)
4548 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004549
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004550 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004551 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004552 if (!ecc->read_page)
4553 ecc->read_page = nand_read_page_hwecc;
4554 if (!ecc->write_page)
4555 ecc->write_page = nand_write_page_hwecc;
4556 if (!ecc->read_page_raw)
4557 ecc->read_page_raw = nand_read_page_raw;
4558 if (!ecc->write_page_raw)
4559 ecc->write_page_raw = nand_write_page_raw;
4560 if (!ecc->read_oob)
4561 ecc->read_oob = nand_read_oob_std;
4562 if (!ecc->write_oob)
4563 ecc->write_oob = nand_write_oob_std;
4564 if (!ecc->read_subpage)
4565 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004566 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004567 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004568
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004569 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004570 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4571 (!ecc->read_page ||
4572 ecc->read_page == nand_read_page_hwecc ||
4573 !ecc->write_page ||
4574 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004575 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4576 ret = -EINVAL;
4577 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004578 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004579 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004580 if (!ecc->read_page)
4581 ecc->read_page = nand_read_page_syndrome;
4582 if (!ecc->write_page)
4583 ecc->write_page = nand_write_page_syndrome;
4584 if (!ecc->read_page_raw)
4585 ecc->read_page_raw = nand_read_page_raw_syndrome;
4586 if (!ecc->write_page_raw)
4587 ecc->write_page_raw = nand_write_page_raw_syndrome;
4588 if (!ecc->read_oob)
4589 ecc->read_oob = nand_read_oob_syndrome;
4590 if (!ecc->write_oob)
4591 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004592
Huang Shijie97de79e02013-10-18 14:20:53 +08004593 if (mtd->writesize >= ecc->size) {
4594 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004595 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4596 ret = -EINVAL;
4597 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004598 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004599 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004600 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004601 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4602 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004603 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004604 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004606 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004607 ret = nand_set_ecc_soft_ops(mtd);
4608 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004609 ret = -EINVAL;
4610 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004611 }
4612 break;
4613
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004614 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004615 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004616 ecc->read_page = nand_read_page_raw;
4617 ecc->write_page = nand_write_page_raw;
4618 ecc->read_oob = nand_read_oob_std;
4619 ecc->read_page_raw = nand_read_page_raw;
4620 ecc->write_page_raw = nand_write_page_raw;
4621 ecc->write_oob = nand_write_oob_std;
4622 ecc->size = mtd->writesize;
4623 ecc->bytes = 0;
4624 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004628 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4629 ret = -EINVAL;
4630 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Brian Norris9ce244b2011-08-30 18:45:37 -07004633 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004634 if (!ecc->read_oob_raw)
4635 ecc->read_oob_raw = ecc->read_oob;
4636 if (!ecc->write_oob_raw)
4637 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004638
Boris Brezillon846031d2016-02-03 20:11:00 +01004639 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004640 mtd->ecc_strength = ecc->strength;
4641 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004642
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004643 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004644 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004645 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004646 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004647 ecc->steps = mtd->writesize / ecc->size;
4648 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004649 WARN(1, "Invalid ECC parameters\n");
4650 ret = -EINVAL;
4651 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004653 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004654
Boris Brezillon846031d2016-02-03 20:11:00 +01004655 /*
4656 * The number of bytes available for a client to place data into
4657 * the out of band area.
4658 */
4659 ret = mtd_ooblayout_count_freebytes(mtd);
4660 if (ret < 0)
4661 ret = 0;
4662
4663 mtd->oobavail = ret;
4664
4665 /* ECC sanity check: warn if it's too weak */
4666 if (!nand_ecc_strength_good(mtd))
4667 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4668 mtd->name);
4669
Brian Norris8b6e50c2011-05-25 14:59:01 -07004670 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004671 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004672 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004673 case 2:
4674 mtd->subpage_sft = 1;
4675 break;
4676 case 4:
4677 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004678 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004679 mtd->subpage_sft = 2;
4680 break;
4681 }
4682 }
4683 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4684
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004685 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004686 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004689 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004691 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304692 switch (ecc->mode) {
4693 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304694 if (chip->page_shift > 9)
4695 chip->options |= NAND_SUBPAGE_READ;
4696 break;
4697
4698 default:
4699 break;
4700 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004703 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004704 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4705 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004706 mtd->_erase = nand_erase;
4707 mtd->_point = NULL;
4708 mtd->_unpoint = NULL;
4709 mtd->_read = nand_read;
4710 mtd->_write = nand_write;
4711 mtd->_panic_write = panic_nand_write;
4712 mtd->_read_oob = nand_read_oob;
4713 mtd->_write_oob = nand_write_oob;
4714 mtd->_sync = nand_sync;
4715 mtd->_lock = NULL;
4716 mtd->_unlock = NULL;
4717 mtd->_suspend = nand_suspend;
4718 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004719 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004720 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004721 mtd->_block_isbad = nand_block_isbad;
4722 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004723 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004725 /*
4726 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4727 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4728 * properly set.
4729 */
4730 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004731 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004733 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004734 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004738 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004739err_free:
4740 if (!(chip->options & NAND_OWN_BUFFERS))
4741 kfree(chip->buffers);
4742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004744EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Brian Norris8b6e50c2011-05-25 14:59:01 -07004746/*
4747 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004748 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004749 * to call us from in-kernel code if the core NAND support is modular.
4750 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004751#ifdef MODULE
4752#define caller_is_module() (1)
4753#else
4754#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004755 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004756#endif
4757
4758/**
4759 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004760 * @mtd: MTD device structure
4761 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004762 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004763 * This fills out all the uninitialized function pointers with the defaults.
4764 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004765 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004766 */
4767int nand_scan(struct mtd_info *mtd, int maxchips)
4768{
4769 int ret;
4770
David Woodhouse5e81e882010-02-26 18:32:56 +00004771 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004772 if (!ret)
4773 ret = nand_scan_tail(mtd);
4774 return ret;
4775}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004776EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004779 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004780 * @mtd: MTD device structure
4781 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004782void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004784 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004786 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004787 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004788 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4789
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004790 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Boris Brezillond8e725d2016-09-15 10:32:50 +02004792 nand_release_data_interface(chip);
4793
Jesper Juhlfa671642005-11-07 01:01:27 -08004794 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004795 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004796 if (!(chip->options & NAND_OWN_BUFFERS))
4797 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004798
4799 /* Free bad block descriptor memory */
4800 if (chip->badblock_pattern && chip->badblock_pattern->options
4801 & NAND_BBT_DYNAMICSTRUCT)
4802 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
David Woodhousee0c7d762006-05-13 18:07:53 +01004804EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004805
David Woodhousee0c7d762006-05-13 18:07:53 +01004806MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004807MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4808MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004809MODULE_DESCRIPTION("Generic NAND flash driver code");