blob: 3bde96a3f7bfd5b8f066fc56af91fb1983279cdf [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;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200748
Brian Norrisf5b88de2016-10-03 09:49:35 -0700749 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200750 if (!nand_opcode_8bits(command))
751 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754 chip->cmd_ctrl(mtd, page_addr, ctrl);
755 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200756 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200758 if (chip->chipsize > (128 << 20))
759 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200760 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200763 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764
765 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700766 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100767 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 case NAND_CMD_CACHEDPROG:
772 case NAND_CMD_PAGEPROG:
773 case NAND_CMD_ERASE1:
774 case NAND_CMD_ERASE2:
775 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200776 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000778 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200781 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200783 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200784 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
785 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
786 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
787 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200788 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
789 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return;
791
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200792 case NAND_CMD_RNDOUT:
793 /* No ready / busy check necessary */
794 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
795 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
796 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
797 NAND_NCE | NAND_CTRL_CHANGE);
798 return;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200801 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
802 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
803 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
804 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000805
David Woodhousee0c7d762006-05-13 18:07:53 +0100806 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000808 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700810 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200812 if (!chip->dev_ready) {
813 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000817
Brian Norris8b6e50c2011-05-25 14:59:01 -0700818 /*
819 * Apply this short delay always to ensure that we do wait tWB in
820 * any case on any machine.
821 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100822 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000823
824 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200828 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700829 * @chip: the nand chip descriptor
830 * @mtd: MTD device structure
831 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200832 *
833 * Used when in panic, no locks are taken.
834 */
835static void panic_nand_get_device(struct nand_chip *chip,
836 struct mtd_info *mtd, int new_state)
837{
Brian Norris7854d3f2011-06-23 14:12:08 -0700838 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200839 chip->controller->active = chip;
840 chip->state = new_state;
841}
842
843/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700845 * @mtd: MTD device structure
846 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 *
848 * Get the device and lock it for exclusive access
849 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200850static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800851nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100853 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200854 spinlock_t *lock = &chip->controller->lock;
855 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100856 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200857retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100858 spin_lock(lock);
859
vimal singhb8b3ee92009-07-09 20:41:22 +0530860 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 if (!chip->controller->active)
862 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200863
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200864 if (chip->controller->active == chip && chip->state == FL_READY) {
865 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100866 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100867 return 0;
868 }
869 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800870 if (chip->controller->active->state == FL_PM_SUSPENDED) {
871 chip->state = FL_PM_SUSPENDED;
872 spin_unlock(lock);
873 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800874 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100875 }
876 set_current_state(TASK_UNINTERRUPTIBLE);
877 add_wait_queue(wq, &wait);
878 spin_unlock(lock);
879 schedule();
880 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto retry;
882}
883
884/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700885 * panic_nand_wait - [GENERIC] wait until the command is done
886 * @mtd: MTD device structure
887 * @chip: NAND chip structure
888 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200889 *
890 * Wait for command done. This is a helper function for nand_wait used when
891 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400892 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200893 */
894static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
895 unsigned long timeo)
896{
897 int i;
898 for (i = 0; i < timeo; i++) {
899 if (chip->dev_ready) {
900 if (chip->dev_ready(mtd))
901 break;
902 } else {
903 if (chip->read_byte(mtd) & NAND_STATUS_READY)
904 break;
905 }
906 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200907 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200908}
909
910/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700911 * nand_wait - [DEFAULT] wait until the command is done
912 * @mtd: MTD device structure
913 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100915 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700916 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200917static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919
Alex Smithb70af9b2015-10-06 14:52:07 +0100920 int status;
921 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Brian Norris8b6e50c2011-05-25 14:59:01 -0700923 /*
924 * Apply this short delay always to ensure that we do wait tWB in any
925 * case on any machine.
926 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100927 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200929 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200931 if (in_interrupt() || oops_in_progress)
932 panic_nand_wait(mtd, chip, timeo);
933 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800934 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100935 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200936 if (chip->dev_ready) {
937 if (chip->dev_ready(mtd))
938 break;
939 } else {
940 if (chip->read_byte(mtd) & NAND_STATUS_READY)
941 break;
942 }
943 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100944 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800946
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200947 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100948 /* This can happen if in case of timeout or buggy dev_ready */
949 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return status;
951}
952
953/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200954 * nand_reset_data_interface - Reset data interface and timings
955 * @chip: The NAND chip
956 *
957 * Reset the Data interface and timings to ONFI mode 0.
958 *
959 * Returns 0 for success or negative error code otherwise.
960 */
961static int nand_reset_data_interface(struct nand_chip *chip)
962{
963 struct mtd_info *mtd = nand_to_mtd(chip);
964 const struct nand_data_interface *conf;
965 int ret;
966
967 if (!chip->setup_data_interface)
968 return 0;
969
970 /*
971 * The ONFI specification says:
972 * "
973 * To transition from NV-DDR or NV-DDR2 to the SDR data
974 * interface, the host shall use the Reset (FFh) command
975 * using SDR timing mode 0. A device in any timing mode is
976 * required to recognize Reset (FFh) command issued in SDR
977 * timing mode 0.
978 * "
979 *
980 * Configure the data interface in SDR mode and set the
981 * timings to timing mode 0.
982 */
983
984 conf = nand_get_default_data_interface();
985 ret = chip->setup_data_interface(mtd, conf, false);
986 if (ret)
987 pr_err("Failed to configure data interface to SDR timing mode 0\n");
988
989 return ret;
990}
991
992/**
993 * nand_setup_data_interface - Setup the best data interface and timings
994 * @chip: The NAND chip
995 *
996 * Find and configure the best data interface and NAND timings supported by
997 * the chip and the driver.
998 * First tries to retrieve supported timing modes from ONFI information,
999 * and if the NAND chip does not support ONFI, relies on the
1000 * ->onfi_timing_mode_default specified in the nand_ids table.
1001 *
1002 * Returns 0 for success or negative error code otherwise.
1003 */
1004static int nand_setup_data_interface(struct nand_chip *chip)
1005{
1006 struct mtd_info *mtd = nand_to_mtd(chip);
1007 int ret;
1008
1009 if (!chip->setup_data_interface || !chip->data_interface)
1010 return 0;
1011
1012 /*
1013 * Ensure the timing mode has been changed on the chip side
1014 * before changing timings on the controller side.
1015 */
1016 if (chip->onfi_version) {
1017 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1018 chip->onfi_timing_mode_default,
1019 };
1020
1021 ret = chip->onfi_set_features(mtd, chip,
1022 ONFI_FEATURE_ADDR_TIMING_MODE,
1023 tmode_param);
1024 if (ret)
1025 goto err;
1026 }
1027
1028 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1029err:
1030 return ret;
1031}
1032
1033/**
1034 * nand_init_data_interface - find the best data interface and timings
1035 * @chip: The NAND chip
1036 *
1037 * Find the best data interface and NAND timings supported by the chip
1038 * and the driver.
1039 * First tries to retrieve supported timing modes from ONFI information,
1040 * and if the NAND chip does not support ONFI, relies on the
1041 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1042 * function nand_chip->data_interface is initialized with the best timing mode
1043 * available.
1044 *
1045 * Returns 0 for success or negative error code otherwise.
1046 */
1047static int nand_init_data_interface(struct nand_chip *chip)
1048{
1049 struct mtd_info *mtd = nand_to_mtd(chip);
1050 int modes, mode, ret;
1051
1052 if (!chip->setup_data_interface)
1053 return 0;
1054
1055 /*
1056 * First try to identify the best timings from ONFI parameters and
1057 * if the NAND does not support ONFI, fallback to the default ONFI
1058 * timing mode.
1059 */
1060 modes = onfi_get_async_timing_mode(chip);
1061 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1062 if (!chip->onfi_timing_mode_default)
1063 return 0;
1064
1065 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1066 }
1067
1068 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1069 GFP_KERNEL);
1070 if (!chip->data_interface)
1071 return -ENOMEM;
1072
1073 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1074 ret = onfi_init_data_interface(chip, chip->data_interface,
1075 NAND_SDR_IFACE, mode);
1076 if (ret)
1077 continue;
1078
1079 ret = chip->setup_data_interface(mtd, chip->data_interface,
1080 true);
1081 if (!ret) {
1082 chip->onfi_timing_mode_default = mode;
1083 break;
1084 }
1085 }
1086
1087 return 0;
1088}
1089
1090static void nand_release_data_interface(struct nand_chip *chip)
1091{
1092 kfree(chip->data_interface);
1093}
1094
1095/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001096 * nand_reset - Reset and initialize a NAND device
1097 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001098 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001099 *
1100 * Returns 0 for success or negative error code otherwise
1101 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001102int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001103{
1104 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001105 int ret;
1106
1107 ret = nand_reset_data_interface(chip);
1108 if (ret)
1109 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001110
Boris Brezillon73f907f2016-10-24 16:46:20 +02001111 /*
1112 * The CS line has to be released before we can apply the new NAND
1113 * interface settings, hence this weird ->select_chip() dance.
1114 */
1115 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001116 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001117 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001118
Boris Brezillon73f907f2016-10-24 16:46:20 +02001119 chip->select_chip(mtd, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001120 ret = nand_setup_data_interface(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001121 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001122 if (ret)
1123 return ret;
1124
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001125 return 0;
1126}
1127
1128/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001129 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001130 * @mtd: mtd info
1131 * @ofs: offset to start unlock from
1132 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001133 * @invert: when = 0, unlock the range of blocks within the lower and
1134 * upper boundary address
1135 * when = 1, unlock the range of blocks outside the boundaries
1136 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301137 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001138 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301139 */
1140static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1141 uint64_t len, int invert)
1142{
1143 int ret = 0;
1144 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001145 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301146
1147 /* Submit address of first page to unlock */
1148 page = ofs >> chip->page_shift;
1149 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1150
1151 /* Submit address of last page to unlock */
1152 page = (ofs + len) >> chip->page_shift;
1153 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1154 (page | invert) & chip->pagemask);
1155
1156 /* Call wait ready function */
1157 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301158 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001159 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001160 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301161 __func__, status);
1162 ret = -EIO;
1163 }
1164
1165 return ret;
1166}
1167
1168/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001169 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001170 * @mtd: mtd info
1171 * @ofs: offset to start unlock from
1172 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301173 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001174 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301175 */
1176int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1177{
1178 int ret = 0;
1179 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001180 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301181
Brian Norris289c0522011-07-19 10:06:09 -07001182 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301183 __func__, (unsigned long long)ofs, len);
1184
1185 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001186 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301187
1188 /* Align to last block address if size addresses end of the device */
1189 if (ofs + len == mtd->size)
1190 len -= mtd->erasesize;
1191
Huang Shijie6a8214a2012-11-19 14:43:30 +08001192 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301193
1194 /* Shift to get chip number */
1195 chipnr = ofs >> chip->chip_shift;
1196
White Ding57d3a9a2014-07-24 00:10:45 +08001197 /*
1198 * Reset the chip.
1199 * If we want to check the WP through READ STATUS and check the bit 7
1200 * we must reset the chip
1201 * some operation can also clear the bit 7 of status register
1202 * eg. erase/program a locked block
1203 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001204 nand_reset(chip, chipnr);
1205
1206 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001207
Vimal Singh7d70f332010-02-08 15:50:49 +05301208 /* Check, if it is write protected */
1209 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001210 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301211 __func__);
1212 ret = -EIO;
1213 goto out;
1214 }
1215
1216 ret = __nand_unlock(mtd, ofs, len, 0);
1217
1218out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001219 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301220 nand_release_device(mtd);
1221
1222 return ret;
1223}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001224EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301225
1226/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001227 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001228 * @mtd: mtd info
1229 * @ofs: offset to start unlock from
1230 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301231 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001232 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1233 * have this feature, but it allows only to lock all blocks, not for specified
1234 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1235 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301236 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001237 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301238 */
1239int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1240{
1241 int ret = 0;
1242 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001243 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301244
Brian Norris289c0522011-07-19 10:06:09 -07001245 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301246 __func__, (unsigned long long)ofs, len);
1247
1248 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001249 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301250
Huang Shijie6a8214a2012-11-19 14:43:30 +08001251 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301252
1253 /* Shift to get chip number */
1254 chipnr = ofs >> chip->chip_shift;
1255
White Ding57d3a9a2014-07-24 00:10:45 +08001256 /*
1257 * Reset the chip.
1258 * If we want to check the WP through READ STATUS and check the bit 7
1259 * we must reset the chip
1260 * some operation can also clear the bit 7 of status register
1261 * eg. erase/program a locked block
1262 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001263 nand_reset(chip, chipnr);
1264
1265 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001266
Vimal Singh7d70f332010-02-08 15:50:49 +05301267 /* Check, if it is write protected */
1268 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001269 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301270 __func__);
1271 status = MTD_ERASE_FAILED;
1272 ret = -EIO;
1273 goto out;
1274 }
1275
1276 /* Submit address of first page to lock */
1277 page = ofs >> chip->page_shift;
1278 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1279
1280 /* Call wait ready function */
1281 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301282 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001283 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001284 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301285 __func__, status);
1286 ret = -EIO;
1287 goto out;
1288 }
1289
1290 ret = __nand_unlock(mtd, ofs, len, 0x1);
1291
1292out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001293 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301294 nand_release_device(mtd);
1295
1296 return ret;
1297}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001298EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301299
1300/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001301 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1302 * @buf: buffer to test
1303 * @len: buffer length
1304 * @bitflips_threshold: maximum number of bitflips
1305 *
1306 * Check if a buffer contains only 0xff, which means the underlying region
1307 * has been erased and is ready to be programmed.
1308 * The bitflips_threshold specify the maximum number of bitflips before
1309 * considering the region is not erased.
1310 * Note: The logic of this function has been extracted from the memweight
1311 * implementation, except that nand_check_erased_buf function exit before
1312 * testing the whole buffer if the number of bitflips exceed the
1313 * bitflips_threshold value.
1314 *
1315 * Returns a positive number of bitflips less than or equal to
1316 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1317 * threshold.
1318 */
1319static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1320{
1321 const unsigned char *bitmap = buf;
1322 int bitflips = 0;
1323 int weight;
1324
1325 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1326 len--, bitmap++) {
1327 weight = hweight8(*bitmap);
1328 bitflips += BITS_PER_BYTE - weight;
1329 if (unlikely(bitflips > bitflips_threshold))
1330 return -EBADMSG;
1331 }
1332
1333 for (; len >= sizeof(long);
1334 len -= sizeof(long), bitmap += sizeof(long)) {
1335 weight = hweight_long(*((unsigned long *)bitmap));
1336 bitflips += BITS_PER_LONG - weight;
1337 if (unlikely(bitflips > bitflips_threshold))
1338 return -EBADMSG;
1339 }
1340
1341 for (; len > 0; len--, bitmap++) {
1342 weight = hweight8(*bitmap);
1343 bitflips += BITS_PER_BYTE - weight;
1344 if (unlikely(bitflips > bitflips_threshold))
1345 return -EBADMSG;
1346 }
1347
1348 return bitflips;
1349}
1350
1351/**
1352 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1353 * 0xff data
1354 * @data: data buffer to test
1355 * @datalen: data length
1356 * @ecc: ECC buffer
1357 * @ecclen: ECC length
1358 * @extraoob: extra OOB buffer
1359 * @extraooblen: extra OOB length
1360 * @bitflips_threshold: maximum number of bitflips
1361 *
1362 * Check if a data buffer and its associated ECC and OOB data contains only
1363 * 0xff pattern, which means the underlying region has been erased and is
1364 * ready to be programmed.
1365 * The bitflips_threshold specify the maximum number of bitflips before
1366 * considering the region as not erased.
1367 *
1368 * Note:
1369 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1370 * different from the NAND page size. When fixing bitflips, ECC engines will
1371 * report the number of errors per chunk, and the NAND core infrastructure
1372 * expect you to return the maximum number of bitflips for the whole page.
1373 * This is why you should always use this function on a single chunk and
1374 * not on the whole page. After checking each chunk you should update your
1375 * max_bitflips value accordingly.
1376 * 2/ When checking for bitflips in erased pages you should not only check
1377 * the payload data but also their associated ECC data, because a user might
1378 * have programmed almost all bits to 1 but a few. In this case, we
1379 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1380 * this case.
1381 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1382 * data are protected by the ECC engine.
1383 * It could also be used if you support subpages and want to attach some
1384 * extra OOB data to an ECC chunk.
1385 *
1386 * Returns a positive number of bitflips less than or equal to
1387 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1388 * threshold. In case of success, the passed buffers are filled with 0xff.
1389 */
1390int nand_check_erased_ecc_chunk(void *data, int datalen,
1391 void *ecc, int ecclen,
1392 void *extraoob, int extraooblen,
1393 int bitflips_threshold)
1394{
1395 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1396
1397 data_bitflips = nand_check_erased_buf(data, datalen,
1398 bitflips_threshold);
1399 if (data_bitflips < 0)
1400 return data_bitflips;
1401
1402 bitflips_threshold -= data_bitflips;
1403
1404 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1405 if (ecc_bitflips < 0)
1406 return ecc_bitflips;
1407
1408 bitflips_threshold -= ecc_bitflips;
1409
1410 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1411 bitflips_threshold);
1412 if (extraoob_bitflips < 0)
1413 return extraoob_bitflips;
1414
1415 if (data_bitflips)
1416 memset(data, 0xff, datalen);
1417
1418 if (ecc_bitflips)
1419 memset(ecc, 0xff, ecclen);
1420
1421 if (extraoob_bitflips)
1422 memset(extraoob, 0xff, extraooblen);
1423
1424 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1425}
1426EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1427
1428/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001429 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001430 * @mtd: mtd info structure
1431 * @chip: nand chip info structure
1432 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001433 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001434 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001435 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001436 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001437 */
1438static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001439 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001440{
1441 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001442 if (oob_required)
1443 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001444 return 0;
1445}
1446
1447/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001448 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001449 * @mtd: mtd info structure
1450 * @chip: nand chip info structure
1451 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001452 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001453 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001454 *
1455 * We need a special oob layout and handling even when OOB isn't used.
1456 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001457static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001458 struct nand_chip *chip, uint8_t *buf,
1459 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001460{
1461 int eccsize = chip->ecc.size;
1462 int eccbytes = chip->ecc.bytes;
1463 uint8_t *oob = chip->oob_poi;
1464 int steps, size;
1465
1466 for (steps = chip->ecc.steps; steps > 0; steps--) {
1467 chip->read_buf(mtd, buf, eccsize);
1468 buf += eccsize;
1469
1470 if (chip->ecc.prepad) {
1471 chip->read_buf(mtd, oob, chip->ecc.prepad);
1472 oob += chip->ecc.prepad;
1473 }
1474
1475 chip->read_buf(mtd, oob, eccbytes);
1476 oob += eccbytes;
1477
1478 if (chip->ecc.postpad) {
1479 chip->read_buf(mtd, oob, chip->ecc.postpad);
1480 oob += chip->ecc.postpad;
1481 }
1482 }
1483
1484 size = mtd->oobsize - (oob - chip->oob_poi);
1485 if (size)
1486 chip->read_buf(mtd, oob, size);
1487
1488 return 0;
1489}
1490
1491/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001492 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001493 * @mtd: mtd info structure
1494 * @chip: nand chip info structure
1495 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001496 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001497 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001498 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001499static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001500 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Boris Brezillon846031d2016-02-03 20:11:00 +01001502 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001503 int eccbytes = chip->ecc.bytes;
1504 int eccsteps = chip->ecc.steps;
1505 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001506 uint8_t *ecc_calc = chip->buffers->ecccalc;
1507 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001508 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001509
Brian Norris1fbb9382012-05-02 10:14:55 -07001510 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001511
1512 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1513 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1514
Boris Brezillon846031d2016-02-03 20:11:00 +01001515 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1516 chip->ecc.total);
1517 if (ret)
1518 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001519
1520 eccsteps = chip->ecc.steps;
1521 p = buf;
1522
1523 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1524 int stat;
1525
1526 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001527 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001528 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001529 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001530 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001531 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1532 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001533 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001534 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301538 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001539 * @mtd: mtd info structure
1540 * @chip: nand chip info structure
1541 * @data_offs: offset of requested data within the page
1542 * @readlen: data length
1543 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001544 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001545 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001546static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001547 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1548 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001549{
Boris Brezillon846031d2016-02-03 20:11:00 +01001550 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001551 uint8_t *p;
1552 int data_col_addr, i, gaps = 0;
1553 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1554 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001555 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001556 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001557 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001558
Brian Norris7854d3f2011-06-23 14:12:08 -07001559 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001560 start_step = data_offs / chip->ecc.size;
1561 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1562 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301563 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001564
Brian Norris8b6e50c2011-05-25 14:59:01 -07001565 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001566 datafrag_len = num_steps * chip->ecc.size;
1567 eccfrag_len = num_steps * chip->ecc.bytes;
1568
1569 data_col_addr = start_step * chip->ecc.size;
1570 /* If we read not a page aligned data */
1571 if (data_col_addr != 0)
1572 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1573
1574 p = bufpoi + data_col_addr;
1575 chip->read_buf(mtd, p, datafrag_len);
1576
Brian Norris8b6e50c2011-05-25 14:59:01 -07001577 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001578 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1579 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1580
Brian Norris8b6e50c2011-05-25 14:59:01 -07001581 /*
1582 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001583 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001584 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001585 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1586 if (ret)
1587 return ret;
1588
1589 if (oobregion.length < eccfrag_len)
1590 gaps = 1;
1591
Alexey Korolev3d459552008-05-15 17:23:18 +01001592 if (gaps) {
1593 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1594 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1595 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001596 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001597 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001598 * about buswidth alignment in read_buf.
1599 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001600 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001601 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001602 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001603 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001604 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1605 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001606 aligned_len++;
1607
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001608 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001609 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001610 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1611 }
1612
Boris Brezillon846031d2016-02-03 20:11:00 +01001613 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1614 chip->oob_poi, index, eccfrag_len);
1615 if (ret)
1616 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001617
1618 p = bufpoi + data_col_addr;
1619 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1620 int stat;
1621
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001622 stat = chip->ecc.correct(mtd, p,
1623 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001624 if (stat == -EBADMSG &&
1625 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1626 /* check for empty pages with bitflips */
1627 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1628 &chip->buffers->ecccode[i],
1629 chip->ecc.bytes,
1630 NULL, 0,
1631 chip->ecc.strength);
1632 }
1633
Mike Dunn3f91e942012-04-25 12:06:09 -07001634 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001635 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001636 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001637 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001638 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1639 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001640 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001641 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001642}
1643
1644/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001645 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001646 * @mtd: mtd info structure
1647 * @chip: nand chip info structure
1648 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001649 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001650 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001651 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001652 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001653 */
1654static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001655 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001656{
Boris Brezillon846031d2016-02-03 20:11:00 +01001657 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001658 int eccbytes = chip->ecc.bytes;
1659 int eccsteps = chip->ecc.steps;
1660 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001661 uint8_t *ecc_calc = chip->buffers->ecccalc;
1662 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001663 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001664
1665 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1666 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1667 chip->read_buf(mtd, p, eccsize);
1668 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1669 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001670 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001671
Boris Brezillon846031d2016-02-03 20:11:00 +01001672 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1673 chip->ecc.total);
1674 if (ret)
1675 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001676
1677 eccsteps = chip->ecc.steps;
1678 p = buf;
1679
1680 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1681 int stat;
1682
1683 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001684 if (stat == -EBADMSG &&
1685 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1686 /* check for empty pages with bitflips */
1687 stat = nand_check_erased_ecc_chunk(p, eccsize,
1688 &ecc_code[i], eccbytes,
1689 NULL, 0,
1690 chip->ecc.strength);
1691 }
1692
Mike Dunn3f91e942012-04-25 12:06:09 -07001693 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001694 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001695 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001696 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001697 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1698 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001699 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001700 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001701}
1702
1703/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001704 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001705 * @mtd: mtd info structure
1706 * @chip: nand chip info structure
1707 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001708 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001709 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001710 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001711 * Hardware ECC for large page chips, require OOB to be read first. For this
1712 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1713 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1714 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1715 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001716 */
1717static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001718 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001719{
Boris Brezillon846031d2016-02-03 20:11:00 +01001720 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001721 int eccbytes = chip->ecc.bytes;
1722 int eccsteps = chip->ecc.steps;
1723 uint8_t *p = buf;
1724 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001725 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001726 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001727
1728 /* Read the OOB area first */
1729 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1730 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1731 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1732
Boris Brezillon846031d2016-02-03 20:11:00 +01001733 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1734 chip->ecc.total);
1735 if (ret)
1736 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001737
1738 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1739 int stat;
1740
1741 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1742 chip->read_buf(mtd, p, eccsize);
1743 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1744
1745 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001746 if (stat == -EBADMSG &&
1747 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1748 /* check for empty pages with bitflips */
1749 stat = nand_check_erased_ecc_chunk(p, eccsize,
1750 &ecc_code[i], eccbytes,
1751 NULL, 0,
1752 chip->ecc.strength);
1753 }
1754
Mike Dunn3f91e942012-04-25 12:06:09 -07001755 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001756 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001757 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001758 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001759 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1760 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001761 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001762 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001763}
1764
1765/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001766 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001767 * @mtd: mtd info structure
1768 * @chip: nand chip info structure
1769 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001770 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001771 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001772 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001773 * The hw generator calculates the error syndrome automatically. Therefore we
1774 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001775 */
1776static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001777 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001778{
1779 int i, eccsize = chip->ecc.size;
1780 int eccbytes = chip->ecc.bytes;
1781 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001782 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001783 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001784 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001785 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001786
1787 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1788 int stat;
1789
1790 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1791 chip->read_buf(mtd, p, eccsize);
1792
1793 if (chip->ecc.prepad) {
1794 chip->read_buf(mtd, oob, chip->ecc.prepad);
1795 oob += chip->ecc.prepad;
1796 }
1797
1798 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1799 chip->read_buf(mtd, oob, eccbytes);
1800 stat = chip->ecc.correct(mtd, p, oob, NULL);
1801
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001802 oob += eccbytes;
1803
1804 if (chip->ecc.postpad) {
1805 chip->read_buf(mtd, oob, chip->ecc.postpad);
1806 oob += chip->ecc.postpad;
1807 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001808
1809 if (stat == -EBADMSG &&
1810 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1811 /* check for empty pages with bitflips */
1812 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1813 oob - eccpadbytes,
1814 eccpadbytes,
1815 NULL, 0,
1816 chip->ecc.strength);
1817 }
1818
1819 if (stat < 0) {
1820 mtd->ecc_stats.failed++;
1821 } else {
1822 mtd->ecc_stats.corrected += stat;
1823 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1824 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001825 }
1826
1827 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001828 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001829 if (i)
1830 chip->read_buf(mtd, oob, i);
1831
Mike Dunn3f91e942012-04-25 12:06:09 -07001832 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001833}
1834
1835/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001836 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001837 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001838 * @oob: oob destination address
1839 * @ops: oob ops structure
1840 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001841 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001842static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001843 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001844{
Boris Brezillon846031d2016-02-03 20:11:00 +01001845 struct nand_chip *chip = mtd_to_nand(mtd);
1846 int ret;
1847
Florian Fainellif8ac0412010-09-07 13:23:43 +02001848 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001849
Brian Norris0612b9d2011-08-30 18:45:40 -07001850 case MTD_OPS_PLACE_OOB:
1851 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001852 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1853 return oob + len;
1854
Boris Brezillon846031d2016-02-03 20:11:00 +01001855 case MTD_OPS_AUTO_OOB:
1856 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1857 ops->ooboffs, len);
1858 BUG_ON(ret);
1859 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001860
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001861 default:
1862 BUG();
1863 }
1864 return NULL;
1865}
1866
1867/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001868 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1869 * @mtd: MTD device structure
1870 * @retry_mode: the retry mode to use
1871 *
1872 * Some vendors supply a special command to shift the Vt threshold, to be used
1873 * when there are too many bitflips in a page (i.e., ECC error). After setting
1874 * a new threshold, the host should retry reading the page.
1875 */
1876static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1877{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001878 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001879
1880 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1881
1882 if (retry_mode >= chip->read_retries)
1883 return -EINVAL;
1884
1885 if (!chip->setup_read_retry)
1886 return -EOPNOTSUPP;
1887
1888 return chip->setup_read_retry(mtd, retry_mode);
1889}
1890
1891/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001892 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001893 * @mtd: MTD device structure
1894 * @from: offset to read from
1895 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001896 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001897 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001898 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001899static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1900 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001901{
Brian Norrise47f3db2012-05-02 10:14:56 -07001902 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001903 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001904 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001905 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001906 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001907 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001908
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001909 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001910 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001911 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001912 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001913 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001915 chipnr = (int)(from >> chip->chip_shift);
1916 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001918 realpage = (int)(from >> chip->page_shift);
1919 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001921 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001923 buf = ops->datbuf;
1924 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001925 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001926
Florian Fainellif8ac0412010-09-07 13:23:43 +02001927 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001928 unsigned int ecc_failures = mtd->ecc_stats.failed;
1929
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001930 bytes = min(mtd->writesize - col, readlen);
1931 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001932
Kamal Dasu66507c72014-05-01 20:51:19 -04001933 if (!aligned)
1934 use_bufpoi = 1;
1935 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1936 use_bufpoi = !virt_addr_valid(buf);
1937 else
1938 use_bufpoi = 0;
1939
Brian Norris8b6e50c2011-05-25 14:59:01 -07001940 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001941 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001942 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1943
1944 if (use_bufpoi && aligned)
1945 pr_debug("%s: using read bounce buffer for buf@%p\n",
1946 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Brian Norrisba84fb52014-01-03 15:13:33 -08001948read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001949 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Mike Dunnedbc45402012-04-25 12:06:11 -07001951 /*
1952 * Now read the page into the buffer. Absent an error,
1953 * the read methods return max bitflips per ecc step.
1954 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001955 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001956 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001957 oob_required,
1958 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001959 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1960 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001961 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001962 col, bytes, bufpoi,
1963 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001964 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001965 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001966 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001967 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001968 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001969 /* Invalidate page cache */
1970 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001971 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001972 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001973
Mike Dunnedbc45402012-04-25 12:06:11 -07001974 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1975
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001976 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001977 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001978 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001979 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001980 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001981 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001982 chip->pagebuf_bitflips = ret;
1983 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001984 /* Invalidate page cache */
1985 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001986 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001987 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001989
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001990 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001991 int toread = min(oobreadlen, max_oobsize);
1992
1993 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001994 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001995 oob, ops, toread);
1996 oobreadlen -= toread;
1997 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001998 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001999
2000 if (chip->options & NAND_NEED_READRDY) {
2001 /* Apply delay or wait for ready/busy pin */
2002 if (!chip->dev_ready)
2003 udelay(chip->chip_delay);
2004 else
2005 nand_wait_ready(mtd);
2006 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002007
Brian Norrisba84fb52014-01-03 15:13:33 -08002008 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002009 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002010 retry_mode++;
2011 ret = nand_setup_read_retry(mtd,
2012 retry_mode);
2013 if (ret < 0)
2014 break;
2015
2016 /* Reset failures; retry */
2017 mtd->ecc_stats.failed = ecc_failures;
2018 goto read_retry;
2019 } else {
2020 /* No more retry modes; real failure */
2021 ecc_fail = true;
2022 }
2023 }
2024
2025 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002026 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002027 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002028 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002029 max_bitflips = max_t(unsigned int, max_bitflips,
2030 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002033 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002034
Brian Norrisba84fb52014-01-03 15:13:33 -08002035 /* Reset to retry mode 0 */
2036 if (retry_mode) {
2037 ret = nand_setup_read_retry(mtd, 0);
2038 if (ret < 0)
2039 break;
2040 retry_mode = 0;
2041 }
2042
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002043 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Brian Norris8b6e50c2011-05-25 14:59:01 -07002046 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 col = 0;
2048 /* Increment page address */
2049 realpage++;
2050
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002051 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* Check, if we cross a chip boundary */
2053 if (!page) {
2054 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002055 chip->select_chip(mtd, -1);
2056 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002059 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002061 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002062 if (oob)
2063 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Mike Dunn3f91e942012-04-25 12:06:09 -07002065 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002066 return ret;
2067
Brian Norrisb72f3df2013-12-03 11:04:14 -08002068 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002069 return -EBADMSG;
2070
Mike Dunnedbc45402012-04-25 12:06:11 -07002071 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002072}
2073
2074/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002075 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002076 * @mtd: MTD device structure
2077 * @from: offset to read from
2078 * @len: number of bytes to read
2079 * @retlen: pointer to variable to store the number of read bytes
2080 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002081 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002082 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002083 */
2084static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2085 size_t *retlen, uint8_t *buf)
2086{
Brian Norris4a89ff82011-08-30 18:45:45 -07002087 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002088 int ret;
2089
Huang Shijie6a8214a2012-11-19 14:43:30 +08002090 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002091 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002092 ops.len = len;
2093 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002094 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002095 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002096 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002097 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
2101/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002102 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002103 * @mtd: mtd info structure
2104 * @chip: nand chip info structure
2105 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002106 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002107int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002108{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002109 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002110 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002111 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002112}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002113EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002114
2115/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002116 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002117 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002118 * @mtd: mtd info structure
2119 * @chip: nand chip info structure
2120 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002121 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002122int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2123 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002124{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002125 int length = mtd->oobsize;
2126 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2127 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002128 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002129 int i, toread, sndrnd = 0, pos;
2130
2131 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2132 for (i = 0; i < chip->ecc.steps; i++) {
2133 if (sndrnd) {
2134 pos = eccsize + i * (eccsize + chunk);
2135 if (mtd->writesize > 512)
2136 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2137 else
2138 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2139 } else
2140 sndrnd = 1;
2141 toread = min_t(int, length, chunk);
2142 chip->read_buf(mtd, bufpoi, toread);
2143 bufpoi += toread;
2144 length -= toread;
2145 }
2146 if (length > 0)
2147 chip->read_buf(mtd, bufpoi, length);
2148
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002149 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002150}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002151EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002152
2153/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002154 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002155 * @mtd: mtd info structure
2156 * @chip: nand chip info structure
2157 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002158 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002159int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002160{
2161 int status = 0;
2162 const uint8_t *buf = chip->oob_poi;
2163 int length = mtd->oobsize;
2164
2165 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2166 chip->write_buf(mtd, buf, length);
2167 /* Send command to program the OOB data */
2168 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2169
2170 status = chip->waitfunc(mtd, chip);
2171
Savin Zlobec0d420f92006-06-21 11:51:20 +02002172 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002173}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002174EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002175
2176/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002177 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002178 * with syndrome - only for large page flash
2179 * @mtd: mtd info structure
2180 * @chip: nand chip info structure
2181 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002182 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002183int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2184 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002185{
2186 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2187 int eccsize = chip->ecc.size, length = mtd->oobsize;
2188 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2189 const uint8_t *bufpoi = chip->oob_poi;
2190
2191 /*
2192 * data-ecc-data-ecc ... ecc-oob
2193 * or
2194 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2195 */
2196 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2197 pos = steps * (eccsize + chunk);
2198 steps = 0;
2199 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002200 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002201
2202 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2203 for (i = 0; i < steps; i++) {
2204 if (sndcmd) {
2205 if (mtd->writesize <= 512) {
2206 uint32_t fill = 0xFFFFFFFF;
2207
2208 len = eccsize;
2209 while (len > 0) {
2210 int num = min_t(int, len, 4);
2211 chip->write_buf(mtd, (uint8_t *)&fill,
2212 num);
2213 len -= num;
2214 }
2215 } else {
2216 pos = eccsize + i * (eccsize + chunk);
2217 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2218 }
2219 } else
2220 sndcmd = 1;
2221 len = min_t(int, length, chunk);
2222 chip->write_buf(mtd, bufpoi, len);
2223 bufpoi += len;
2224 length -= len;
2225 }
2226 if (length > 0)
2227 chip->write_buf(mtd, bufpoi, length);
2228
2229 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2230 status = chip->waitfunc(mtd, chip);
2231
2232 return status & NAND_STATUS_FAIL ? -EIO : 0;
2233}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002234EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002235
2236/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002237 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002238 * @mtd: MTD device structure
2239 * @from: offset to read from
2240 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002242 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002244static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2245 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Brian Norrisc00a0992012-05-01 17:12:54 -07002247 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002248 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002249 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002250 int readlen = ops->ooblen;
2251 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002252 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002253 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Brian Norris289c0522011-07-19 10:06:09 -07002255 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302256 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Brian Norris041e4572011-06-23 16:45:24 -07002258 stats = mtd->ecc_stats;
2259
Boris BREZILLON29f10582016-03-07 10:46:52 +01002260 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002261
2262 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002263 pr_debug("%s: attempt to start read outside oob\n",
2264 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002265 return -EINVAL;
2266 }
2267
2268 /* Do not allow reads past end of device */
2269 if (unlikely(from >= mtd->size ||
2270 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2271 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002272 pr_debug("%s: attempt to read beyond end of device\n",
2273 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002274 return -EINVAL;
2275 }
Vitaly Wool70145682006-11-03 18:20:38 +03002276
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002277 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002278 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002280 /* Shift to get page */
2281 realpage = (int)(from >> chip->page_shift);
2282 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Florian Fainellif8ac0412010-09-07 13:23:43 +02002284 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002285 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002286 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002287 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002288 ret = chip->ecc.read_oob(mtd, chip, page);
2289
2290 if (ret < 0)
2291 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002292
2293 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002294 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002295
Brian Norris5bc7c332013-03-13 09:51:31 -07002296 if (chip->options & NAND_NEED_READRDY) {
2297 /* Apply delay or wait for ready/busy pin */
2298 if (!chip->dev_ready)
2299 udelay(chip->chip_delay);
2300 else
2301 nand_wait_ready(mtd);
2302 }
2303
Vitaly Wool70145682006-11-03 18:20:38 +03002304 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002305 if (!readlen)
2306 break;
2307
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002308 /* Increment page address */
2309 realpage++;
2310
2311 page = realpage & chip->pagemask;
2312 /* Check, if we cross a chip boundary */
2313 if (!page) {
2314 chipnr++;
2315 chip->select_chip(mtd, -1);
2316 chip->select_chip(mtd, chipnr);
2317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002319 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002321 ops->oobretlen = ops->ooblen - readlen;
2322
2323 if (ret < 0)
2324 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002325
2326 if (mtd->ecc_stats.failed - stats.failed)
2327 return -EBADMSG;
2328
2329 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002333 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002334 * @mtd: MTD device structure
2335 * @from: offset to read from
2336 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002338 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002340static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2341 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002343 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002344
2345 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002348 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002349 pr_debug("%s: attempt to read beyond end of device\n",
2350 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return -EINVAL;
2352 }
2353
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002354 if (ops->mode != MTD_OPS_PLACE_OOB &&
2355 ops->mode != MTD_OPS_AUTO_OOB &&
2356 ops->mode != MTD_OPS_RAW)
2357 return -ENOTSUPP;
2358
Huang Shijie6a8214a2012-11-19 14:43:30 +08002359 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002361 if (!ops->datbuf)
2362 ret = nand_do_read_oob(mtd, from, ops);
2363 else
2364 ret = nand_do_read_ops(mtd, from, ops);
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002367 return ret;
2368}
2369
2370
2371/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002372 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002373 * @mtd: mtd info structure
2374 * @chip: nand chip info structure
2375 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002376 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002377 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002378 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002379 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002380 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002381static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002382 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002383{
2384 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002385 if (oob_required)
2386 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002387
2388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002391/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002392 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002393 * @mtd: mtd info structure
2394 * @chip: nand chip info structure
2395 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002396 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002397 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002398 *
2399 * We need a special oob layout and handling even when ECC isn't checked.
2400 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002401static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002402 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002403 const uint8_t *buf, int oob_required,
2404 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002405{
2406 int eccsize = chip->ecc.size;
2407 int eccbytes = chip->ecc.bytes;
2408 uint8_t *oob = chip->oob_poi;
2409 int steps, size;
2410
2411 for (steps = chip->ecc.steps; steps > 0; steps--) {
2412 chip->write_buf(mtd, buf, eccsize);
2413 buf += eccsize;
2414
2415 if (chip->ecc.prepad) {
2416 chip->write_buf(mtd, oob, chip->ecc.prepad);
2417 oob += chip->ecc.prepad;
2418 }
2419
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002420 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002421 oob += eccbytes;
2422
2423 if (chip->ecc.postpad) {
2424 chip->write_buf(mtd, oob, chip->ecc.postpad);
2425 oob += chip->ecc.postpad;
2426 }
2427 }
2428
2429 size = mtd->oobsize - (oob - chip->oob_poi);
2430 if (size)
2431 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002432
2433 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002434}
2435/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002436 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002437 * @mtd: mtd info structure
2438 * @chip: nand chip info structure
2439 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002440 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002441 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002443static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002444 const uint8_t *buf, int oob_required,
2445 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002446{
Boris Brezillon846031d2016-02-03 20:11:00 +01002447 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002448 int eccbytes = chip->ecc.bytes;
2449 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002450 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002451 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002452
Brian Norris7854d3f2011-06-23 14:12:08 -07002453 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002454 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2455 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002456
Boris Brezillon846031d2016-02-03 20:11:00 +01002457 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2458 chip->ecc.total);
2459 if (ret)
2460 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002461
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002462 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002463}
2464
2465/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002466 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002467 * @mtd: mtd info structure
2468 * @chip: nand chip info structure
2469 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002470 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002471 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002472 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002473static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002474 const uint8_t *buf, int oob_required,
2475 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002476{
Boris Brezillon846031d2016-02-03 20:11:00 +01002477 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002478 int eccbytes = chip->ecc.bytes;
2479 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002480 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002481 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002482
2483 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2484 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002485 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002486 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2487 }
2488
Boris Brezillon846031d2016-02-03 20:11:00 +01002489 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2490 chip->ecc.total);
2491 if (ret)
2492 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002493
2494 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002495
2496 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002497}
2498
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302499
2500/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002501 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302502 * @mtd: mtd info structure
2503 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002504 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302505 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002506 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302507 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002508 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302509 */
2510static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2511 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002512 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002513 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302514{
2515 uint8_t *oob_buf = chip->oob_poi;
2516 uint8_t *ecc_calc = chip->buffers->ecccalc;
2517 int ecc_size = chip->ecc.size;
2518 int ecc_bytes = chip->ecc.bytes;
2519 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302520 uint32_t start_step = offset / ecc_size;
2521 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2522 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002523 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302524
2525 for (step = 0; step < ecc_steps; step++) {
2526 /* configure controller for WRITE access */
2527 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2528
2529 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002530 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302531
2532 /* mask ECC of un-touched subpages by padding 0xFF */
2533 if ((step < start_step) || (step > end_step))
2534 memset(ecc_calc, 0xff, ecc_bytes);
2535 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002536 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302537
2538 /* mask OOB of un-touched subpages by padding 0xFF */
2539 /* if oob_required, preserve OOB metadata of written subpage */
2540 if (!oob_required || (step < start_step) || (step > end_step))
2541 memset(oob_buf, 0xff, oob_bytes);
2542
Brian Norrisd6a950802013-08-08 17:16:36 -07002543 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302544 ecc_calc += ecc_bytes;
2545 oob_buf += oob_bytes;
2546 }
2547
2548 /* copy calculated ECC for whole page to chip->buffer->oob */
2549 /* this include masked-value(0xFF) for unwritten subpages */
2550 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002551 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2552 chip->ecc.total);
2553 if (ret)
2554 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302555
2556 /* write OOB buffer to NAND device */
2557 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2558
2559 return 0;
2560}
2561
2562
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002563/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002564 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002565 * @mtd: mtd info structure
2566 * @chip: nand chip info structure
2567 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002568 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002569 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002570 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002571 * The hw generator calculates the error syndrome automatically. Therefore we
2572 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002573 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002574static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002575 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002576 const uint8_t *buf, int oob_required,
2577 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002578{
2579 int i, eccsize = chip->ecc.size;
2580 int eccbytes = chip->ecc.bytes;
2581 int eccsteps = chip->ecc.steps;
2582 const uint8_t *p = buf;
2583 uint8_t *oob = chip->oob_poi;
2584
2585 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2586
2587 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2588 chip->write_buf(mtd, p, eccsize);
2589
2590 if (chip->ecc.prepad) {
2591 chip->write_buf(mtd, oob, chip->ecc.prepad);
2592 oob += chip->ecc.prepad;
2593 }
2594
2595 chip->ecc.calculate(mtd, p, oob);
2596 chip->write_buf(mtd, oob, eccbytes);
2597 oob += eccbytes;
2598
2599 if (chip->ecc.postpad) {
2600 chip->write_buf(mtd, oob, chip->ecc.postpad);
2601 oob += chip->ecc.postpad;
2602 }
2603 }
2604
2605 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002606 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002607 if (i)
2608 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002609
2610 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002611}
2612
2613/**
David Woodhouse956e9442006-09-25 17:12:39 +01002614 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002615 * @mtd: MTD device structure
2616 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302617 * @offset: address offset within the page
2618 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002619 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002620 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002621 * @page: page number to write
2622 * @cached: cached programming
2623 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002624 */
2625static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302626 uint32_t offset, int data_len, const uint8_t *buf,
2627 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002628{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302629 int status, subpage;
2630
2631 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2632 chip->ecc.write_subpage)
2633 subpage = offset || (data_len < mtd->writesize);
2634 else
2635 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002636
2637 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2638
David Woodhouse956e9442006-09-25 17:12:39 +01002639 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302640 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002641 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302642 else if (subpage)
2643 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002644 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002645 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002646 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2647 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002648
2649 if (status < 0)
2650 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002651
2652 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002653 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002654 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002655 */
2656 cached = 0;
2657
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002658 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002659
2660 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002661 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002662 /*
2663 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002664 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002665 */
2666 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2667 status = chip->errstat(mtd, chip, FL_WRITING, status,
2668 page);
2669
2670 if (status & NAND_STATUS_FAIL)
2671 return -EIO;
2672 } else {
2673 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002674 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002675 }
2676
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002677 return 0;
2678}
2679
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002680/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002681 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002682 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002683 * @oob: oob data buffer
2684 * @len: oob data write length
2685 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002686 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002687static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2688 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002689{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002690 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002691 int ret;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002692
2693 /*
2694 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2695 * data from a previous OOB read.
2696 */
2697 memset(chip->oob_poi, 0xff, mtd->oobsize);
2698
Florian Fainellif8ac0412010-09-07 13:23:43 +02002699 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002700
Brian Norris0612b9d2011-08-30 18:45:40 -07002701 case MTD_OPS_PLACE_OOB:
2702 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002703 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2704 return oob + len;
2705
Boris Brezillon846031d2016-02-03 20:11:00 +01002706 case MTD_OPS_AUTO_OOB:
2707 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2708 ops->ooboffs, len);
2709 BUG_ON(ret);
2710 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002711
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002712 default:
2713 BUG();
2714 }
2715 return NULL;
2716}
2717
Florian Fainellif8ac0412010-09-07 13:23:43 +02002718#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002719
2720/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002721 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002722 * @mtd: MTD device structure
2723 * @to: offset to write to
2724 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002725 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002726 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002727 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002728static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2729 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002730{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002731 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002732 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002733 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002734
2735 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002736 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002737
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002738 uint8_t *oob = ops->oobbuf;
2739 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302740 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002741 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002742
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002743 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002744 if (!writelen)
2745 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002746
Brian Norris8b6e50c2011-05-25 14:59:01 -07002747 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002748 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002749 pr_notice("%s: attempt to write non page aligned data\n",
2750 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002751 return -EINVAL;
2752 }
2753
Thomas Gleixner29072b92006-09-28 15:38:36 +02002754 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002755
Thomas Gleixner6a930962006-06-28 00:11:45 +02002756 chipnr = (int)(to >> chip->chip_shift);
2757 chip->select_chip(mtd, chipnr);
2758
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002759 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002760 if (nand_check_wp(mtd)) {
2761 ret = -EIO;
2762 goto err_out;
2763 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002764
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002765 realpage = (int)(to >> chip->page_shift);
2766 page = realpage & chip->pagemask;
2767 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2768
2769 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002770 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2771 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002772 chip->pagebuf = -1;
2773
Maxim Levitsky782ce792010-02-22 20:39:36 +02002774 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002775 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2776 ret = -EINVAL;
2777 goto err_out;
2778 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002779
Florian Fainellif8ac0412010-09-07 13:23:43 +02002780 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002781 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002782 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002783 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002784 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002785 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002786
Kamal Dasu66507c72014-05-01 20:51:19 -04002787 if (part_pagewr)
2788 use_bufpoi = 1;
2789 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2790 use_bufpoi = !virt_addr_valid(buf);
2791 else
2792 use_bufpoi = 0;
2793
2794 /* Partial page write?, or need to use bounce buffer */
2795 if (use_bufpoi) {
2796 pr_debug("%s: using write bounce buffer for buf@%p\n",
2797 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002798 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002799 if (part_pagewr)
2800 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002801 chip->pagebuf = -1;
2802 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2803 memcpy(&chip->buffers->databuf[column], buf, bytes);
2804 wbuf = chip->buffers->databuf;
2805 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002806
Maxim Levitsky782ce792010-02-22 20:39:36 +02002807 if (unlikely(oob)) {
2808 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002809 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002810 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002811 } else {
2812 /* We still need to erase leftover OOB data */
2813 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002814 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302815 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2816 oob_required, page, cached,
2817 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002818 if (ret)
2819 break;
2820
2821 writelen -= bytes;
2822 if (!writelen)
2823 break;
2824
Thomas Gleixner29072b92006-09-28 15:38:36 +02002825 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002826 buf += bytes;
2827 realpage++;
2828
2829 page = realpage & chip->pagemask;
2830 /* Check, if we cross a chip boundary */
2831 if (!page) {
2832 chipnr++;
2833 chip->select_chip(mtd, -1);
2834 chip->select_chip(mtd, chipnr);
2835 }
2836 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002837
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002838 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002839 if (unlikely(oob))
2840 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002841
2842err_out:
2843 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002844 return ret;
2845}
2846
2847/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002848 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002849 * @mtd: MTD device structure
2850 * @to: offset to write to
2851 * @len: number of bytes to write
2852 * @retlen: pointer to variable to store the number of written bytes
2853 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002854 *
2855 * NAND write with ECC. Used when performing writes in interrupt context, this
2856 * may for example be called by mtdoops when writing an oops while in panic.
2857 */
2858static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2859 size_t *retlen, const uint8_t *buf)
2860{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002861 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002862 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002863 int ret;
2864
Brian Norris8b6e50c2011-05-25 14:59:01 -07002865 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002866 panic_nand_wait(mtd, chip, 400);
2867
Brian Norris8b6e50c2011-05-25 14:59:01 -07002868 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002869 panic_nand_get_device(chip, mtd, FL_WRITING);
2870
Brian Norris0ec56dc2015-02-28 02:02:30 -08002871 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002872 ops.len = len;
2873 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002874 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002875
Brian Norris4a89ff82011-08-30 18:45:45 -07002876 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002877
Brian Norris4a89ff82011-08-30 18:45:45 -07002878 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002879 return ret;
2880}
2881
2882/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002883 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002884 * @mtd: MTD device structure
2885 * @to: offset to write to
2886 * @len: number of bytes to write
2887 * @retlen: pointer to variable to store the number of written bytes
2888 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002890 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002892static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002893 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Brian Norris4a89ff82011-08-30 18:45:45 -07002895 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002896 int ret;
2897
Huang Shijie6a8214a2012-11-19 14:43:30 +08002898 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002899 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002900 ops.len = len;
2901 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002902 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002903 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002904 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002905 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002906 return ret;
2907}
2908
2909/**
2910 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002911 * @mtd: MTD device structure
2912 * @to: offset to write to
2913 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002914 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002915 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002916 */
2917static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2918 struct mtd_oob_ops *ops)
2919{
Adrian Hunter03736152007-01-31 17:58:29 +02002920 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002921 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Brian Norris289c0522011-07-19 10:06:09 -07002923 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302924 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Boris BREZILLON29f10582016-03-07 10:46:52 +01002926 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002929 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002930 pr_debug("%s: attempt to write past end of page\n",
2931 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return -EINVAL;
2933 }
2934
Adrian Hunter03736152007-01-31 17:58:29 +02002935 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002936 pr_debug("%s: attempt to start write outside oob\n",
2937 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002938 return -EINVAL;
2939 }
2940
Jason Liu775adc3d42011-02-25 13:06:18 +08002941 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002942 if (unlikely(to >= mtd->size ||
2943 ops->ooboffs + ops->ooblen >
2944 ((mtd->size >> chip->page_shift) -
2945 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002946 pr_debug("%s: attempt to write beyond end of device\n",
2947 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002948 return -EINVAL;
2949 }
2950
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002951 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002952
2953 /*
2954 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2955 * of my DiskOnChip 2000 test units) will clear the whole data page too
2956 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2957 * it in the doc2000 driver in August 1999. dwmw2.
2958 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002959 nand_reset(chip, chipnr);
2960
2961 chip->select_chip(mtd, chipnr);
2962
2963 /* Shift to get page */
2964 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002967 if (nand_check_wp(mtd)) {
2968 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002969 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002970 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002973 if (page == chip->pagebuf)
2974 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002976 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002977
Brian Norris0612b9d2011-08-30 18:45:40 -07002978 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002979 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2980 else
2981 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002982
Huang Shijieb0bb6902012-11-19 14:43:29 +08002983 chip->select_chip(mtd, -1);
2984
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002985 if (status)
2986 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Vitaly Wool70145682006-11-03 18:20:38 +03002988 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002990 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002993/**
2994 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002995 * @mtd: MTD device structure
2996 * @to: offset to write to
2997 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002998 */
2999static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3000 struct mtd_oob_ops *ops)
3001{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003002 int ret = -ENOTSUPP;
3003
3004 ops->retlen = 0;
3005
3006 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003007 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003008 pr_debug("%s: attempt to write beyond end of device\n",
3009 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003010 return -EINVAL;
3011 }
3012
Huang Shijie6a8214a2012-11-19 14:43:30 +08003013 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003014
Florian Fainellif8ac0412010-09-07 13:23:43 +02003015 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003016 case MTD_OPS_PLACE_OOB:
3017 case MTD_OPS_AUTO_OOB:
3018 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003019 break;
3020
3021 default:
3022 goto out;
3023 }
3024
3025 if (!ops->datbuf)
3026 ret = nand_do_write_oob(mtd, to, ops);
3027 else
3028 ret = nand_do_write_ops(mtd, to, ops);
3029
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003030out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003031 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return ret;
3033}
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035/**
Brian Norris49c50b92014-05-06 16:02:19 -07003036 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003037 * @mtd: MTD device structure
3038 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 *
Brian Norris49c50b92014-05-06 16:02:19 -07003040 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 */
Brian Norris49c50b92014-05-06 16:02:19 -07003042static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003044 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003046 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3047 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003048
3049 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
3052/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003054 * @mtd: MTD device structure
3055 * @instr: erase instruction
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 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003059static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
David Woodhousee0c7d762006-05-13 18:07:53 +01003061 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003065 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003066 * @mtd: MTD device structure
3067 * @instr: erase instruction
3068 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003070 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003072int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3073 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Adrian Hunter69423d92008-12-10 13:37:21 +00003075 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003076 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003077 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Brian Norris289c0522011-07-19 10:06:09 -07003079 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3080 __func__, (unsigned long long)instr->addr,
3081 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303083 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003087 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003090 page = (int)(instr->addr >> chip->page_shift);
3091 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003094 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003097 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 /* Check, if it is write protected */
3100 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003101 pr_debug("%s: device is write protected!\n",
3102 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 instr->state = MTD_ERASE_FAILED;
3104 goto erase_exit;
3105 }
3106
3107 /* Loop through the pages */
3108 len = instr->len;
3109
3110 instr->state = MTD_ERASING;
3111
3112 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003113 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003114 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303115 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003116 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3117 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 instr->state = MTD_ERASE_FAILED;
3119 goto erase_exit;
3120 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003121
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003122 /*
3123 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003124 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003125 */
3126 if (page <= chip->pagebuf && chip->pagebuf <
3127 (page + pages_per_block))
3128 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Brian Norris49c50b92014-05-06 16:02:19 -07003130 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003132 /*
3133 * See if operation failed and additional status checks are
3134 * available
3135 */
3136 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3137 status = chip->errstat(mtd, chip, FL_ERASING,
3138 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003141 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003142 pr_debug("%s: failed erase, page 0x%08x\n",
3143 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003145 instr->fail_addr =
3146 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 goto erase_exit;
3148 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003151 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 page += pages_per_block;
3153
3154 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003155 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003157 chip->select_chip(mtd, -1);
3158 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160 }
3161 instr->state = MTD_ERASE_DONE;
3162
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003163erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
3167 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003168 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 nand_release_device(mtd);
3170
David Woodhouse49defc02007-10-06 15:01:59 -04003171 /* Do call back function */
3172 if (!ret)
3173 mtd_erase_callback(instr);
3174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 /* Return more or less happy */
3176 return ret;
3177}
3178
3179/**
3180 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003181 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003183 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003185static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
Brian Norris289c0522011-07-19 10:06:09 -07003187 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003190 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003192 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003196 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003197 * @mtd: MTD device structure
3198 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003200static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303202 struct nand_chip *chip = mtd_to_nand(mtd);
3203 int chipnr = (int)(offs >> chip->chip_shift);
3204 int ret;
3205
3206 /* Select the NAND device */
3207 nand_get_device(mtd, FL_READING);
3208 chip->select_chip(mtd, chipnr);
3209
3210 ret = nand_block_checkbad(mtd, offs, 0);
3211
3212 chip->select_chip(mtd, -1);
3213 nand_release_device(mtd);
3214
3215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
3218/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003219 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003220 * @mtd: MTD device structure
3221 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003223static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 int ret;
3226
Florian Fainellif8ac0412010-09-07 13:23:43 +02003227 ret = nand_block_isbad(mtd, ofs);
3228 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003229 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (ret > 0)
3231 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003232 return ret;
3233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Brian Norris5a0edb22013-07-30 17:52:58 -07003235 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
3238/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003239 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3240 * @mtd: MTD device structure
3241 * @chip: nand chip info structure
3242 * @addr: feature address.
3243 * @subfeature_param: the subfeature parameters, a four bytes array.
3244 */
3245static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3246 int addr, uint8_t *subfeature_param)
3247{
3248 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003249 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003250
David Mosbergerd914c932013-05-29 15:30:13 +03003251 if (!chip->onfi_version ||
3252 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3253 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003254 return -EINVAL;
3255
3256 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003257 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3258 chip->write_byte(mtd, subfeature_param[i]);
3259
Huang Shijie7db03ec2012-09-13 14:57:52 +08003260 status = chip->waitfunc(mtd, chip);
3261 if (status & NAND_STATUS_FAIL)
3262 return -EIO;
3263 return 0;
3264}
3265
3266/**
3267 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3268 * @mtd: MTD device structure
3269 * @chip: nand chip info structure
3270 * @addr: feature address.
3271 * @subfeature_param: the subfeature parameters, a four bytes array.
3272 */
3273static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3274 int addr, uint8_t *subfeature_param)
3275{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003276 int i;
3277
David Mosbergerd914c932013-05-29 15:30:13 +03003278 if (!chip->onfi_version ||
3279 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3280 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003281 return -EINVAL;
3282
Huang Shijie7db03ec2012-09-13 14:57:52 +08003283 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003284 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3285 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003286 return 0;
3287}
3288
3289/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003290 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003291 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003292 */
3293static int nand_suspend(struct mtd_info *mtd)
3294{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003295 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003296}
3297
3298/**
3299 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003300 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003301 */
3302static void nand_resume(struct mtd_info *mtd)
3303{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003304 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003305
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003306 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003307 nand_release_device(mtd);
3308 else
Brian Norrisd0370212011-07-19 10:06:08 -07003309 pr_err("%s called for a chip which is not in suspended state\n",
3310 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003311}
3312
Scott Branden72ea4032014-11-20 11:18:05 -08003313/**
3314 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3315 * prevent further operations
3316 * @mtd: MTD device structure
3317 */
3318static void nand_shutdown(struct mtd_info *mtd)
3319{
Brian Norris9ca641b2015-11-09 16:37:28 -08003320 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003321}
3322
Brian Norris8b6e50c2011-05-25 14:59:01 -07003323/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003324static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003327 if (!chip->chip_delay)
3328 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003331 if (chip->cmdfunc == NULL)
3332 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003335 if (chip->waitfunc == NULL)
3336 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003338 if (!chip->select_chip)
3339 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003340
Huang Shijie4204ccc2013-08-16 10:10:07 +08003341 /* set for ONFI nand */
3342 if (!chip->onfi_set_features)
3343 chip->onfi_set_features = nand_onfi_set_features;
3344 if (!chip->onfi_get_features)
3345 chip->onfi_get_features = nand_onfi_get_features;
3346
Brian Norris68e80782013-07-18 01:17:02 -07003347 /* If called twice, pointers that depend on busw may need to be reset */
3348 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003349 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3350 if (!chip->read_word)
3351 chip->read_word = nand_read_word;
3352 if (!chip->block_bad)
3353 chip->block_bad = nand_block_bad;
3354 if (!chip->block_markbad)
3355 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003356 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003357 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003358 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3359 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003360 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003361 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003362 if (!chip->scan_bbt)
3363 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003364
3365 if (!chip->controller) {
3366 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003367 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003368 }
3369
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003370}
3371
Brian Norris8b6e50c2011-05-25 14:59:01 -07003372/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003373static void sanitize_string(uint8_t *s, size_t len)
3374{
3375 ssize_t i;
3376
Brian Norris8b6e50c2011-05-25 14:59:01 -07003377 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003378 s[len - 1] = 0;
3379
Brian Norris8b6e50c2011-05-25 14:59:01 -07003380 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003381 for (i = 0; i < len - 1; i++) {
3382 if (s[i] < ' ' || s[i] > 127)
3383 s[i] = '?';
3384 }
3385
Brian Norris8b6e50c2011-05-25 14:59:01 -07003386 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003387 strim(s);
3388}
3389
3390static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3391{
3392 int i;
3393 while (len--) {
3394 crc ^= *p++ << 8;
3395 for (i = 0; i < 8; i++)
3396 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3397 }
3398
3399 return crc;
3400}
3401
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003402/* Parse the Extended Parameter Page. */
3403static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3404 struct nand_chip *chip, struct nand_onfi_params *p)
3405{
3406 struct onfi_ext_param_page *ep;
3407 struct onfi_ext_section *s;
3408 struct onfi_ext_ecc_info *ecc;
3409 uint8_t *cursor;
3410 int ret = -EINVAL;
3411 int len;
3412 int i;
3413
3414 len = le16_to_cpu(p->ext_param_page_length) * 16;
3415 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003416 if (!ep)
3417 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003418
3419 /* Send our own NAND_CMD_PARAM. */
3420 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3421
3422 /* Use the Change Read Column command to skip the ONFI param pages. */
3423 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3424 sizeof(*p) * p->num_of_param_pages , -1);
3425
3426 /* Read out the Extended Parameter Page. */
3427 chip->read_buf(mtd, (uint8_t *)ep, len);
3428 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3429 != le16_to_cpu(ep->crc))) {
3430 pr_debug("fail in the CRC.\n");
3431 goto ext_out;
3432 }
3433
3434 /*
3435 * Check the signature.
3436 * Do not strictly follow the ONFI spec, maybe changed in future.
3437 */
3438 if (strncmp(ep->sig, "EPPS", 4)) {
3439 pr_debug("The signature is invalid.\n");
3440 goto ext_out;
3441 }
3442
3443 /* find the ECC section. */
3444 cursor = (uint8_t *)(ep + 1);
3445 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3446 s = ep->sections + i;
3447 if (s->type == ONFI_SECTION_TYPE_2)
3448 break;
3449 cursor += s->length * 16;
3450 }
3451 if (i == ONFI_EXT_SECTION_MAX) {
3452 pr_debug("We can not find the ECC section.\n");
3453 goto ext_out;
3454 }
3455
3456 /* get the info we want. */
3457 ecc = (struct onfi_ext_ecc_info *)cursor;
3458
Brian Norris4ae7d222013-09-16 18:20:21 -07003459 if (!ecc->codeword_size) {
3460 pr_debug("Invalid codeword size\n");
3461 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003462 }
3463
Brian Norris4ae7d222013-09-16 18:20:21 -07003464 chip->ecc_strength_ds = ecc->ecc_bits;
3465 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003466 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003467
3468ext_out:
3469 kfree(ep);
3470 return ret;
3471}
3472
Brian Norris8429bb32013-12-03 15:51:09 -08003473static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3474{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003475 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003476 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3477
3478 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3479 feature);
3480}
3481
3482/*
3483 * Configure chip properties from Micron vendor-specific ONFI table
3484 */
3485static void nand_onfi_detect_micron(struct nand_chip *chip,
3486 struct nand_onfi_params *p)
3487{
3488 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3489
3490 if (le16_to_cpu(p->vendor_revision) < 1)
3491 return;
3492
3493 chip->read_retries = micron->read_retry_options;
3494 chip->setup_read_retry = nand_setup_read_retry_micron;
3495}
3496
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003497/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003498 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003499 */
3500static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003501 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003502{
3503 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003504 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003505 int val;
3506
Brian Norris7854d3f2011-06-23 14:12:08 -07003507 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003508 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3509 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3510 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3511 return 0;
3512
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003513 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3514 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003515 for (j = 0; j < sizeof(*p); j++)
3516 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003517 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3518 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003519 break;
3520 }
3521 }
3522
Brian Norrisc7f23a72013-08-13 10:51:55 -07003523 if (i == 3) {
3524 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003525 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003526 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003527
Brian Norris8b6e50c2011-05-25 14:59:01 -07003528 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003529 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003530 if (val & (1 << 5))
3531 chip->onfi_version = 23;
3532 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003533 chip->onfi_version = 22;
3534 else if (val & (1 << 3))
3535 chip->onfi_version = 21;
3536 else if (val & (1 << 2))
3537 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003538 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003539 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003540
3541 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003542 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003543 return 0;
3544 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003545
3546 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3547 sanitize_string(p->model, sizeof(p->model));
3548 if (!mtd->name)
3549 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003550
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003551 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003552
3553 /*
3554 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3555 * (don't ask me who thought of this...). MTD assumes that these
3556 * dimensions will be power-of-2, so just truncate the remaining area.
3557 */
3558 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3559 mtd->erasesize *= mtd->writesize;
3560
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003561 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003562
3563 /* See erasesize comment */
3564 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003565 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003566 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003567
3568 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003569 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003570 else
3571 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003572
Huang Shijie10c86ba2013-05-17 11:17:26 +08003573 if (p->ecc_bits != 0xff) {
3574 chip->ecc_strength_ds = p->ecc_bits;
3575 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003576 } else if (chip->onfi_version >= 21 &&
3577 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3578
3579 /*
3580 * The nand_flash_detect_ext_param_page() uses the
3581 * Change Read Column command which maybe not supported
3582 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3583 * now. We do not replace user supplied command function.
3584 */
3585 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3586 chip->cmdfunc = nand_command_lp;
3587
3588 /* The Extended Parameter Page is supported since ONFI 2.1. */
3589 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003590 pr_warn("Failed to detect ONFI extended param page\n");
3591 } else {
3592 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003593 }
3594
Brian Norris8429bb32013-12-03 15:51:09 -08003595 if (p->jedec_id == NAND_MFR_MICRON)
3596 nand_onfi_detect_micron(chip, p);
3597
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003598 return 1;
3599}
3600
3601/*
Huang Shijie91361812014-02-21 13:39:40 +08003602 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3603 */
3604static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3605 int *busw)
3606{
3607 struct nand_jedec_params *p = &chip->jedec_params;
3608 struct jedec_ecc_info *ecc;
3609 int val;
3610 int i, j;
3611
3612 /* Try JEDEC for unknown chip or LP */
3613 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3614 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3615 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3616 chip->read_byte(mtd) != 'C')
3617 return 0;
3618
3619 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3620 for (i = 0; i < 3; i++) {
3621 for (j = 0; j < sizeof(*p); j++)
3622 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3623
3624 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3625 le16_to_cpu(p->crc))
3626 break;
3627 }
3628
3629 if (i == 3) {
3630 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3631 return 0;
3632 }
3633
3634 /* Check version */
3635 val = le16_to_cpu(p->revision);
3636 if (val & (1 << 2))
3637 chip->jedec_version = 10;
3638 else if (val & (1 << 1))
3639 chip->jedec_version = 1; /* vendor specific version */
3640
3641 if (!chip->jedec_version) {
3642 pr_info("unsupported JEDEC version: %d\n", val);
3643 return 0;
3644 }
3645
3646 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3647 sanitize_string(p->model, sizeof(p->model));
3648 if (!mtd->name)
3649 mtd->name = p->model;
3650
3651 mtd->writesize = le32_to_cpu(p->byte_per_page);
3652
3653 /* Please reference to the comment for nand_flash_detect_onfi. */
3654 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3655 mtd->erasesize *= mtd->writesize;
3656
3657 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3658
3659 /* Please reference to the comment for nand_flash_detect_onfi. */
3660 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3661 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3662 chip->bits_per_cell = p->bits_per_cell;
3663
3664 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3665 *busw = NAND_BUSWIDTH_16;
3666 else
3667 *busw = 0;
3668
3669 /* ECC info */
3670 ecc = &p->ecc_info[0];
3671
3672 if (ecc->codeword_size >= 9) {
3673 chip->ecc_strength_ds = ecc->ecc_bits;
3674 chip->ecc_step_ds = 1 << ecc->codeword_size;
3675 } else {
3676 pr_warn("Invalid codeword size\n");
3677 }
3678
3679 return 1;
3680}
3681
3682/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003683 * nand_id_has_period - Check if an ID string has a given wraparound period
3684 * @id_data: the ID string
3685 * @arrlen: the length of the @id_data array
3686 * @period: the period of repitition
3687 *
3688 * Check if an ID string is repeated within a given sequence of bytes at
3689 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003690 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003691 * if the repetition has a period of @period; otherwise, returns zero.
3692 */
3693static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3694{
3695 int i, j;
3696 for (i = 0; i < period; i++)
3697 for (j = i + period; j < arrlen; j += period)
3698 if (id_data[i] != id_data[j])
3699 return 0;
3700 return 1;
3701}
3702
3703/*
3704 * nand_id_len - Get the length of an ID string returned by CMD_READID
3705 * @id_data: the ID string
3706 * @arrlen: the length of the @id_data array
3707
3708 * Returns the length of the ID string, according to known wraparound/trailing
3709 * zero patterns. If no pattern exists, returns the length of the array.
3710 */
3711static int nand_id_len(u8 *id_data, int arrlen)
3712{
3713 int last_nonzero, period;
3714
3715 /* Find last non-zero byte */
3716 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3717 if (id_data[last_nonzero])
3718 break;
3719
3720 /* All zeros */
3721 if (last_nonzero < 0)
3722 return 0;
3723
3724 /* Calculate wraparound period */
3725 for (period = 1; period < arrlen; period++)
3726 if (nand_id_has_period(id_data, arrlen, period))
3727 break;
3728
3729 /* There's a repeated pattern */
3730 if (period < arrlen)
3731 return period;
3732
3733 /* There are trailing zeros */
3734 if (last_nonzero < arrlen - 1)
3735 return last_nonzero + 1;
3736
3737 /* No pattern detected */
3738 return arrlen;
3739}
3740
Huang Shijie7db906b2013-09-25 14:58:11 +08003741/* Extract the bits of per cell from the 3rd byte of the extended ID */
3742static int nand_get_bits_per_cell(u8 cellinfo)
3743{
3744 int bits;
3745
3746 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3747 bits >>= NAND_CI_CELLTYPE_SHIFT;
3748 return bits + 1;
3749}
3750
Brian Norrise3b88bd2012-09-24 20:40:52 -07003751/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003752 * Many new NAND share similar device ID codes, which represent the size of the
3753 * chip. The rest of the parameters must be decoded according to generic or
3754 * manufacturer-specific "extended ID" decoding patterns.
3755 */
3756static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3757 u8 id_data[8], int *busw)
3758{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003759 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003760 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003761 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003762 /* The 4th id byte is the important one */
3763 extid = id_data[3];
3764
Brian Norrise3b88bd2012-09-24 20:40:52 -07003765 id_len = nand_id_len(id_data, 8);
3766
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003767 /*
3768 * Field definitions are in the following datasheets:
3769 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003770 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003771 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003772 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003773 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3774 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003775 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003776 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003777 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003778 /* Calc pagesize */
3779 mtd->writesize = 2048 << (extid & 0x03);
3780 extid >>= 2;
3781 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003782 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003783 case 1:
3784 mtd->oobsize = 128;
3785 break;
3786 case 2:
3787 mtd->oobsize = 218;
3788 break;
3789 case 3:
3790 mtd->oobsize = 400;
3791 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003792 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003793 mtd->oobsize = 436;
3794 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003795 case 5:
3796 mtd->oobsize = 512;
3797 break;
3798 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003799 mtd->oobsize = 640;
3800 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003801 case 7:
3802 default: /* Other cases are "reserved" (unknown) */
3803 mtd->oobsize = 1024;
3804 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003805 }
3806 extid >>= 2;
3807 /* Calc blocksize */
3808 mtd->erasesize = (128 * 1024) <<
3809 (((extid >> 1) & 0x04) | (extid & 0x03));
3810 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003811 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003812 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003813 unsigned int tmp;
3814
3815 /* Calc pagesize */
3816 mtd->writesize = 2048 << (extid & 0x03);
3817 extid >>= 2;
3818 /* Calc oobsize */
3819 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3820 case 0:
3821 mtd->oobsize = 128;
3822 break;
3823 case 1:
3824 mtd->oobsize = 224;
3825 break;
3826 case 2:
3827 mtd->oobsize = 448;
3828 break;
3829 case 3:
3830 mtd->oobsize = 64;
3831 break;
3832 case 4:
3833 mtd->oobsize = 32;
3834 break;
3835 case 5:
3836 mtd->oobsize = 16;
3837 break;
3838 default:
3839 mtd->oobsize = 640;
3840 break;
3841 }
3842 extid >>= 2;
3843 /* Calc blocksize */
3844 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3845 if (tmp < 0x03)
3846 mtd->erasesize = (128 * 1024) << tmp;
3847 else if (tmp == 0x03)
3848 mtd->erasesize = 768 * 1024;
3849 else
3850 mtd->erasesize = (64 * 1024) << tmp;
3851 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003852 } else {
3853 /* Calc pagesize */
3854 mtd->writesize = 1024 << (extid & 0x03);
3855 extid >>= 2;
3856 /* Calc oobsize */
3857 mtd->oobsize = (8 << (extid & 0x01)) *
3858 (mtd->writesize >> 9);
3859 extid >>= 2;
3860 /* Calc blocksize. Blocksize is multiples of 64KiB */
3861 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3862 extid >>= 2;
3863 /* Get buswidth information */
3864 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003865
3866 /*
3867 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3868 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3869 * follows:
3870 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3871 * 110b -> 24nm
3872 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3873 */
3874 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003875 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003876 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3877 !(id_data[4] & 0x80) /* !BENAND */) {
3878 mtd->oobsize = 32 * mtd->writesize >> 9;
3879 }
3880
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003881 }
3882}
3883
3884/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003885 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3886 * decodes a matching ID table entry and assigns the MTD size parameters for
3887 * the chip.
3888 */
3889static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3890 struct nand_flash_dev *type, u8 id_data[8],
3891 int *busw)
3892{
3893 int maf_id = id_data[0];
3894
3895 mtd->erasesize = type->erasesize;
3896 mtd->writesize = type->pagesize;
3897 mtd->oobsize = mtd->writesize / 32;
3898 *busw = type->options & NAND_BUSWIDTH_16;
3899
Huang Shijie1c195e92013-09-25 14:58:12 +08003900 /* All legacy ID NAND are small-page, SLC */
3901 chip->bits_per_cell = 1;
3902
Brian Norrisf23a4812012-09-24 20:40:51 -07003903 /*
3904 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3905 * some Spansion chips have erasesize that conflicts with size
3906 * listed in nand_ids table.
3907 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3908 */
3909 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3910 && id_data[6] == 0x00 && id_data[7] == 0x00
3911 && mtd->writesize == 512) {
3912 mtd->erasesize = 128 * 1024;
3913 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3914 }
3915}
3916
3917/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003918 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3919 * heuristic patterns using various detected parameters (e.g., manufacturer,
3920 * page size, cell-type information).
3921 */
3922static void nand_decode_bbm_options(struct mtd_info *mtd,
3923 struct nand_chip *chip, u8 id_data[8])
3924{
3925 int maf_id = id_data[0];
3926
3927 /* Set the bad block position */
3928 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3929 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3930 else
3931 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3932
3933 /*
3934 * Bad block marker is stored in the last page of each block on Samsung
3935 * and Hynix MLC devices; stored in first two pages of each block on
3936 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3937 * AMD/Spansion, and Macronix. All others scan only the first page.
3938 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003939 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003940 (maf_id == NAND_MFR_SAMSUNG ||
3941 maf_id == NAND_MFR_HYNIX))
3942 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003943 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003944 (maf_id == NAND_MFR_SAMSUNG ||
3945 maf_id == NAND_MFR_HYNIX ||
3946 maf_id == NAND_MFR_TOSHIBA ||
3947 maf_id == NAND_MFR_AMD ||
3948 maf_id == NAND_MFR_MACRONIX)) ||
3949 (mtd->writesize == 2048 &&
3950 maf_id == NAND_MFR_MICRON))
3951 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3952}
3953
Huang Shijieec6e87e2013-03-15 11:01:00 +08003954static inline bool is_full_id_nand(struct nand_flash_dev *type)
3955{
3956 return type->id_len;
3957}
3958
3959static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3960 struct nand_flash_dev *type, u8 *id_data, int *busw)
3961{
3962 if (!strncmp(type->id, id_data, type->id_len)) {
3963 mtd->writesize = type->pagesize;
3964 mtd->erasesize = type->erasesize;
3965 mtd->oobsize = type->oobsize;
3966
Huang Shijie7db906b2013-09-25 14:58:11 +08003967 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003968 chip->chipsize = (uint64_t)type->chipsize << 20;
3969 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003970 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3971 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003972 chip->onfi_timing_mode_default =
3973 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003974
3975 *busw = type->options & NAND_BUSWIDTH_16;
3976
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003977 if (!mtd->name)
3978 mtd->name = type->name;
3979
Huang Shijieec6e87e2013-03-15 11:01:00 +08003980 return true;
3981 }
3982 return false;
3983}
3984
Brian Norris7e74c2d2012-09-24 20:40:49 -07003985/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003986 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003987 */
3988static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003989 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003990 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003991 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003992{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003993 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003994 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003995 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Karl Beldanef89a882008-09-15 14:37:29 +02003997 /*
3998 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003999 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004000 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004001 nand_reset(chip, 0);
4002
4003 /* Select the device */
4004 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004007 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
4009 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004010 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004011 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Brian Norris8b6e50c2011-05-25 14:59:01 -07004013 /*
4014 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004015 * interface concerns can cause random data which looks like a
4016 * possibly credible NAND flash to appear. If the two results do
4017 * not match, ignore the device completely.
4018 */
4019
4020 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4021
Brian Norris4aef9b72012-09-24 20:40:48 -07004022 /* Read entire ID string */
4023 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004024 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004025
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004026 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004027 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004028 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01004029 return ERR_PTR(-ENODEV);
4030 }
4031
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004032 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004033 type = nand_flash_ids;
4034
Huang Shijieec6e87e2013-03-15 11:01:00 +08004035 for (; type->name != NULL; type++) {
4036 if (is_full_id_nand(type)) {
4037 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4038 goto ident_done;
4039 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004040 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004041 }
4042 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004043
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004044 chip->onfi_version = 0;
4045 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004046 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07004047 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004048 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004049
4050 /* Check if the chip is JEDEC compliant */
4051 if (nand_flash_detect_jedec(mtd, chip, &busw))
4052 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004053 }
4054
David Woodhouse5e81e882010-02-26 18:32:56 +00004055 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004056 return ERR_PTR(-ENODEV);
4057
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02004058 if (!mtd->name)
4059 mtd->name = type->name;
4060
Adrian Hunter69423d92008-12-10 13:37:21 +00004061 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004062
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02004063 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004064 /* Decode parameters from extended ID */
4065 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004066 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07004067 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004068 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004069 /* Get chip options */
4070 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004071
Brian Norris8b6e50c2011-05-25 14:59:01 -07004072 /*
4073 * Check if chip is not a Samsung device. Do not clear the
4074 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004075 */
4076 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4077 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4078ident_done:
4079
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004080 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01004081 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004082 if (nand_manuf_ids[maf_idx].id == *maf_id)
4083 break;
4084 }
4085
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004086 if (chip->options & NAND_BUSWIDTH_AUTO) {
4087 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4088 chip->options |= busw;
4089 nand_set_defaults(chip, busw);
4090 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4091 /*
4092 * Check, if buswidth is correct. Hardware drivers should set
4093 * chip correct!
4094 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004095 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4096 *maf_id, *dev_id);
4097 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4098 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004099 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4100 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004101 return ERR_PTR(-EINVAL);
4102 }
4103
Brian Norris7e74c2d2012-09-24 20:40:49 -07004104 nand_decode_bbm_options(mtd, chip, id_data);
4105
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004106 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004107 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004108 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004109 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004110
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004111 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004112 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004113 if (chip->chipsize & 0xffffffff)
4114 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004115 else {
4116 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4117 chip->chip_shift += 32 - 1;
4118 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004119
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004120 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004121 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004122
Brian Norris8b6e50c2011-05-25 14:59:01 -07004123 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004124 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4125 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004126
Ezequiel Garcia20171642013-11-25 08:30:31 -03004127 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4128 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004129
4130 if (chip->onfi_version)
4131 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4132 chip->onfi_params.model);
4133 else if (chip->jedec_version)
4134 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4135 chip->jedec_params.model);
4136 else
4137 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4138 type->name);
4139
Rafał Miłecki3755a992014-10-21 00:01:04 +02004140 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004141 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004142 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004143 return type;
4144}
4145
Boris Brezillond48f62b2016-04-01 14:54:32 +02004146static const char * const nand_ecc_modes[] = {
4147 [NAND_ECC_NONE] = "none",
4148 [NAND_ECC_SOFT] = "soft",
4149 [NAND_ECC_HW] = "hw",
4150 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4151 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004152};
4153
4154static int of_get_nand_ecc_mode(struct device_node *np)
4155{
4156 const char *pm;
4157 int err, i;
4158
4159 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4160 if (err < 0)
4161 return err;
4162
4163 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4164 if (!strcasecmp(pm, nand_ecc_modes[i]))
4165 return i;
4166
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004167 /*
4168 * For backward compatibility we support few obsoleted values that don't
4169 * have their mappings into nand_ecc_modes_t anymore (they were merged
4170 * with other enums).
4171 */
4172 if (!strcasecmp(pm, "soft_bch"))
4173 return NAND_ECC_SOFT;
4174
Boris Brezillond48f62b2016-04-01 14:54:32 +02004175 return -ENODEV;
4176}
4177
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004178static const char * const nand_ecc_algos[] = {
4179 [NAND_ECC_HAMMING] = "hamming",
4180 [NAND_ECC_BCH] = "bch",
4181};
4182
Boris Brezillond48f62b2016-04-01 14:54:32 +02004183static int of_get_nand_ecc_algo(struct device_node *np)
4184{
4185 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004186 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004187
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004188 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4189 if (!err) {
4190 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4191 if (!strcasecmp(pm, nand_ecc_algos[i]))
4192 return i;
4193 return -ENODEV;
4194 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004195
4196 /*
4197 * For backward compatibility we also read "nand-ecc-mode" checking
4198 * for some obsoleted values that were specifying ECC algorithm.
4199 */
4200 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4201 if (err < 0)
4202 return err;
4203
4204 if (!strcasecmp(pm, "soft"))
4205 return NAND_ECC_HAMMING;
4206 else if (!strcasecmp(pm, "soft_bch"))
4207 return NAND_ECC_BCH;
4208
4209 return -ENODEV;
4210}
4211
4212static int of_get_nand_ecc_step_size(struct device_node *np)
4213{
4214 int ret;
4215 u32 val;
4216
4217 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4218 return ret ? ret : val;
4219}
4220
4221static int of_get_nand_ecc_strength(struct device_node *np)
4222{
4223 int ret;
4224 u32 val;
4225
4226 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4227 return ret ? ret : val;
4228}
4229
4230static int of_get_nand_bus_width(struct device_node *np)
4231{
4232 u32 val;
4233
4234 if (of_property_read_u32(np, "nand-bus-width", &val))
4235 return 8;
4236
4237 switch (val) {
4238 case 8:
4239 case 16:
4240 return val;
4241 default:
4242 return -EIO;
4243 }
4244}
4245
4246static bool of_get_nand_on_flash_bbt(struct device_node *np)
4247{
4248 return of_property_read_bool(np, "nand-on-flash-bbt");
4249}
4250
Boris BREZILLON7194a292015-12-10 09:00:37 +01004251static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004252{
Boris BREZILLON7194a292015-12-10 09:00:37 +01004253 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004254 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004255
Boris BREZILLON7194a292015-12-10 09:00:37 +01004256 if (!dn)
4257 return 0;
4258
Brian Norris5844fee2015-01-23 00:22:27 -08004259 if (of_get_nand_bus_width(dn) == 16)
4260 chip->options |= NAND_BUSWIDTH_16;
4261
4262 if (of_get_nand_on_flash_bbt(dn))
4263 chip->bbt_options |= NAND_BBT_USE_FLASH;
4264
4265 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004266 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004267 ecc_strength = of_get_nand_ecc_strength(dn);
4268 ecc_step = of_get_nand_ecc_step_size(dn);
4269
4270 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4271 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4272 pr_err("must set both strength and step size in DT\n");
4273 return -EINVAL;
4274 }
4275
4276 if (ecc_mode >= 0)
4277 chip->ecc.mode = ecc_mode;
4278
Rafał Miłecki79082452016-03-23 11:19:02 +01004279 if (ecc_algo >= 0)
4280 chip->ecc.algo = ecc_algo;
4281
Brian Norris5844fee2015-01-23 00:22:27 -08004282 if (ecc_strength >= 0)
4283 chip->ecc.strength = ecc_strength;
4284
4285 if (ecc_step > 0)
4286 chip->ecc.size = ecc_step;
4287
Boris Brezillonba78ee02016-06-08 17:04:22 +02004288 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4289 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4290
Brian Norris5844fee2015-01-23 00:22:27 -08004291 return 0;
4292}
4293
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004294/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004295 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004296 * @mtd: MTD device structure
4297 * @maxchips: number of chips to scan for
4298 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004299 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004300 * This is the first phase of the normal nand_scan() function. It reads the
4301 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004302 *
4303 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004304int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4305 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004306{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004307 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004308 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004309 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004310 int ret;
4311
Boris BREZILLON7194a292015-12-10 09:00:37 +01004312 ret = nand_dt_init(chip);
4313 if (ret)
4314 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004315
Brian Norrisf7a8e382016-01-05 10:39:45 -08004316 if (!mtd->name && mtd->dev.parent)
4317 mtd->name = dev_name(mtd->dev.parent);
4318
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004319 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4320 /*
4321 * Default functions assigned for chip_select() and
4322 * cmdfunc() both expect cmd_ctrl() to be populated,
4323 * so we need to check that that's the case
4324 */
4325 pr_err("chip.cmd_ctrl() callback is not provided");
4326 return -EINVAL;
4327 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004328 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004329 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004330
4331 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004332 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4333 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004334
4335 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004336 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004337 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004338 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004339 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
4341
Boris Brezillon73f907f2016-10-24 16:46:20 +02004342 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004343 ret = nand_init_data_interface(chip);
4344 if (ret)
4345 return ret;
4346
Boris Brezillon73f907f2016-10-24 16:46:20 +02004347 /*
4348 * Setup the data interface correctly on the chip and controller side.
4349 * This explicit call to nand_setup_data_interface() is only required
4350 * for the first die, because nand_reset() has been called before
4351 * ->data_interface and ->default_onfi_timing_mode were set.
4352 * For the other dies, nand_reset() will automatically switch to the
4353 * best mode for us.
4354 */
4355 ret = nand_setup_data_interface(chip);
4356 if (ret)
4357 return ret;
4358
Huang Shijie07300162012-11-09 16:23:45 +08004359 chip->select_chip(mtd, -1);
4360
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004361 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004362 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004363 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004364 nand_reset(chip, i);
4365
4366 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004368 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004370 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004371 nand_dev_id != chip->read_byte(mtd)) {
4372 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 break;
Huang Shijie07300162012-11-09 16:23:45 +08004374 }
4375 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
4377 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004378 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004381 chip->numchips = i;
4382 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
David Woodhouse3b85c322006-09-25 17:06:53 +01004384 return 0;
4385}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004386EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004387
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004388static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4389{
4390 struct nand_chip *chip = mtd_to_nand(mtd);
4391 struct nand_ecc_ctrl *ecc = &chip->ecc;
4392
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004393 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004394 return -EINVAL;
4395
4396 switch (ecc->algo) {
4397 case NAND_ECC_HAMMING:
4398 ecc->calculate = nand_calculate_ecc;
4399 ecc->correct = nand_correct_data;
4400 ecc->read_page = nand_read_page_swecc;
4401 ecc->read_subpage = nand_read_subpage;
4402 ecc->write_page = nand_write_page_swecc;
4403 ecc->read_page_raw = nand_read_page_raw;
4404 ecc->write_page_raw = nand_write_page_raw;
4405 ecc->read_oob = nand_read_oob_std;
4406 ecc->write_oob = nand_write_oob_std;
4407 if (!ecc->size)
4408 ecc->size = 256;
4409 ecc->bytes = 3;
4410 ecc->strength = 1;
4411 return 0;
4412 case NAND_ECC_BCH:
4413 if (!mtd_nand_has_bch()) {
4414 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4415 return -EINVAL;
4416 }
4417 ecc->calculate = nand_bch_calculate_ecc;
4418 ecc->correct = nand_bch_correct_data;
4419 ecc->read_page = nand_read_page_swecc;
4420 ecc->read_subpage = nand_read_subpage;
4421 ecc->write_page = nand_write_page_swecc;
4422 ecc->read_page_raw = nand_read_page_raw;
4423 ecc->write_page_raw = nand_write_page_raw;
4424 ecc->read_oob = nand_read_oob_std;
4425 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004426
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004427 /*
4428 * Board driver should supply ecc.size and ecc.strength
4429 * values to select how many bits are correctable.
4430 * Otherwise, default to 4 bits for large page devices.
4431 */
4432 if (!ecc->size && (mtd->oobsize >= 64)) {
4433 ecc->size = 512;
4434 ecc->strength = 4;
4435 }
4436
4437 /*
4438 * if no ecc placement scheme was provided pickup the default
4439 * large page one.
4440 */
4441 if (!mtd->ooblayout) {
4442 /* handle large page devices only */
4443 if (mtd->oobsize < 64) {
4444 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4445 return -EINVAL;
4446 }
4447
4448 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004449
4450 }
4451
4452 /*
4453 * We can only maximize ECC config when the default layout is
4454 * used, otherwise we don't know how many bytes can really be
4455 * used.
4456 */
4457 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4458 ecc->options & NAND_ECC_MAXIMIZE) {
4459 int steps, bytes;
4460
4461 /* Always prefer 1k blocks over 512bytes ones */
4462 ecc->size = 1024;
4463 steps = mtd->writesize / ecc->size;
4464
4465 /* Reserve 2 bytes for the BBM */
4466 bytes = (mtd->oobsize - 2) / steps;
4467 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004468 }
4469
4470 /* See nand_bch_init() for details. */
4471 ecc->bytes = 0;
4472 ecc->priv = nand_bch_init(mtd);
4473 if (!ecc->priv) {
4474 WARN(1, "BCH ECC initialization failed!\n");
4475 return -EINVAL;
4476 }
4477 return 0;
4478 default:
4479 WARN(1, "Unsupported ECC algorithm!\n");
4480 return -EINVAL;
4481 }
4482}
4483
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004484/*
4485 * Check if the chip configuration meet the datasheet requirements.
4486
4487 * If our configuration corrects A bits per B bytes and the minimum
4488 * required correction level is X bits per Y bytes, then we must ensure
4489 * both of the following are true:
4490 *
4491 * (1) A / B >= X / Y
4492 * (2) A >= X
4493 *
4494 * Requirement (1) ensures we can correct for the required bitflip density.
4495 * Requirement (2) ensures we can correct even when all bitflips are clumped
4496 * in the same sector.
4497 */
4498static bool nand_ecc_strength_good(struct mtd_info *mtd)
4499{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004500 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004501 struct nand_ecc_ctrl *ecc = &chip->ecc;
4502 int corr, ds_corr;
4503
4504 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4505 /* Not enough information */
4506 return true;
4507
4508 /*
4509 * We get the number of corrected bits per page to compare
4510 * the correction density.
4511 */
4512 corr = (mtd->writesize * ecc->strength) / ecc->size;
4513 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4514
4515 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4516}
David Woodhouse3b85c322006-09-25 17:06:53 +01004517
4518/**
4519 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004520 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004521 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004522 * This is the second phase of the normal nand_scan() function. It fills out
4523 * all the uninitialized function pointers with the defaults and scans for a
4524 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004525 */
4526int nand_scan_tail(struct mtd_info *mtd)
4527{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004528 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004529 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004530 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004531 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004532
Brian Norrise2414f42012-02-06 13:44:00 -08004533 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004534 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4535 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4536 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004537
Huang Shijief02ea4e2014-01-13 14:27:12 +08004538 if (!(chip->options & NAND_OWN_BUFFERS)) {
4539 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4540 + mtd->oobsize * 3, GFP_KERNEL);
4541 if (!nbuf)
4542 return -ENOMEM;
4543 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4544 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4545 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4546
4547 chip->buffers = nbuf;
4548 } else {
4549 if (!chip->buffers)
4550 return -ENOMEM;
4551 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004552
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004553 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004554 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004555
4556 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004557 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004558 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004559 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004560 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004561 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004564 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 break;
4566 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004567 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004568 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004571 WARN(1, "No oob scheme defined for oobsize %d\n",
4572 mtd->oobsize);
4573 ret = -EINVAL;
4574 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 }
4576 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004577
David Woodhouse956e9442006-09-25 17:12:39 +01004578 if (!chip->write_page)
4579 chip->write_page = nand_write_page;
4580
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004581 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004582 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004583 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004584 */
David Woodhouse956e9442006-09-25 17:12:39 +01004585
Huang Shijie97de79e02013-10-18 14:20:53 +08004586 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004587 case NAND_ECC_HW_OOB_FIRST:
4588 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004589 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004590 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4591 ret = -EINVAL;
4592 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004593 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004594 if (!ecc->read_page)
4595 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004596
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004597 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004598 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004599 if (!ecc->read_page)
4600 ecc->read_page = nand_read_page_hwecc;
4601 if (!ecc->write_page)
4602 ecc->write_page = nand_write_page_hwecc;
4603 if (!ecc->read_page_raw)
4604 ecc->read_page_raw = nand_read_page_raw;
4605 if (!ecc->write_page_raw)
4606 ecc->write_page_raw = nand_write_page_raw;
4607 if (!ecc->read_oob)
4608 ecc->read_oob = nand_read_oob_std;
4609 if (!ecc->write_oob)
4610 ecc->write_oob = nand_write_oob_std;
4611 if (!ecc->read_subpage)
4612 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004613 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004614 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004615
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004616 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004617 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4618 (!ecc->read_page ||
4619 ecc->read_page == nand_read_page_hwecc ||
4620 !ecc->write_page ||
4621 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004622 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4623 ret = -EINVAL;
4624 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004625 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004626 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004627 if (!ecc->read_page)
4628 ecc->read_page = nand_read_page_syndrome;
4629 if (!ecc->write_page)
4630 ecc->write_page = nand_write_page_syndrome;
4631 if (!ecc->read_page_raw)
4632 ecc->read_page_raw = nand_read_page_raw_syndrome;
4633 if (!ecc->write_page_raw)
4634 ecc->write_page_raw = nand_write_page_raw_syndrome;
4635 if (!ecc->read_oob)
4636 ecc->read_oob = nand_read_oob_syndrome;
4637 if (!ecc->write_oob)
4638 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004639
Huang Shijie97de79e02013-10-18 14:20:53 +08004640 if (mtd->writesize >= ecc->size) {
4641 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004642 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4643 ret = -EINVAL;
4644 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004645 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004646 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004647 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004648 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4649 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004650 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004651 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004653 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004654 ret = nand_set_ecc_soft_ops(mtd);
4655 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004656 ret = -EINVAL;
4657 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004658 }
4659 break;
4660
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004661 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004662 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004663 ecc->read_page = nand_read_page_raw;
4664 ecc->write_page = nand_write_page_raw;
4665 ecc->read_oob = nand_read_oob_std;
4666 ecc->read_page_raw = nand_read_page_raw;
4667 ecc->write_page_raw = nand_write_page_raw;
4668 ecc->write_oob = nand_write_oob_std;
4669 ecc->size = mtd->writesize;
4670 ecc->bytes = 0;
4671 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004675 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4676 ret = -EINVAL;
4677 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
Brian Norris9ce244b2011-08-30 18:45:37 -07004680 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004681 if (!ecc->read_oob_raw)
4682 ecc->read_oob_raw = ecc->read_oob;
4683 if (!ecc->write_oob_raw)
4684 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004685
Boris Brezillon846031d2016-02-03 20:11:00 +01004686 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004687 mtd->ecc_strength = ecc->strength;
4688 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004689
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004690 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004691 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004692 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004693 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004694 ecc->steps = mtd->writesize / ecc->size;
4695 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004696 WARN(1, "Invalid ECC parameters\n");
4697 ret = -EINVAL;
4698 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004700 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004701
Boris Brezillon846031d2016-02-03 20:11:00 +01004702 /*
4703 * The number of bytes available for a client to place data into
4704 * the out of band area.
4705 */
4706 ret = mtd_ooblayout_count_freebytes(mtd);
4707 if (ret < 0)
4708 ret = 0;
4709
4710 mtd->oobavail = ret;
4711
4712 /* ECC sanity check: warn if it's too weak */
4713 if (!nand_ecc_strength_good(mtd))
4714 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4715 mtd->name);
4716
Brian Norris8b6e50c2011-05-25 14:59:01 -07004717 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004718 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004719 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004720 case 2:
4721 mtd->subpage_sft = 1;
4722 break;
4723 case 4:
4724 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004725 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004726 mtd->subpage_sft = 2;
4727 break;
4728 }
4729 }
4730 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4731
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004732 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004733 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004736 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004738 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304739 switch (ecc->mode) {
4740 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304741 if (chip->page_shift > 9)
4742 chip->options |= NAND_SUBPAGE_READ;
4743 break;
4744
4745 default:
4746 break;
4747 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004750 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004751 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4752 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004753 mtd->_erase = nand_erase;
4754 mtd->_point = NULL;
4755 mtd->_unpoint = NULL;
4756 mtd->_read = nand_read;
4757 mtd->_write = nand_write;
4758 mtd->_panic_write = panic_nand_write;
4759 mtd->_read_oob = nand_read_oob;
4760 mtd->_write_oob = nand_write_oob;
4761 mtd->_sync = nand_sync;
4762 mtd->_lock = NULL;
4763 mtd->_unlock = NULL;
4764 mtd->_suspend = nand_suspend;
4765 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004766 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004767 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004768 mtd->_block_isbad = nand_block_isbad;
4769 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004770 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004772 /*
4773 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4774 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4775 * properly set.
4776 */
4777 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004778 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004780 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004781 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
4784 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004785 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004786err_free:
4787 if (!(chip->options & NAND_OWN_BUFFERS))
4788 kfree(chip->buffers);
4789 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004791EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Brian Norris8b6e50c2011-05-25 14:59:01 -07004793/*
4794 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004795 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004796 * to call us from in-kernel code if the core NAND support is modular.
4797 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004798#ifdef MODULE
4799#define caller_is_module() (1)
4800#else
4801#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004802 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004803#endif
4804
4805/**
4806 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004807 * @mtd: MTD device structure
4808 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004809 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004810 * This fills out all the uninitialized function pointers with the defaults.
4811 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004812 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004813 */
4814int nand_scan(struct mtd_info *mtd, int maxchips)
4815{
4816 int ret;
4817
David Woodhouse5e81e882010-02-26 18:32:56 +00004818 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004819 if (!ret)
4820 ret = nand_scan_tail(mtd);
4821 return ret;
4822}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004823EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004826 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4827 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004828 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004829void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004831 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004832 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004833 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4834
Boris Brezillond8e725d2016-09-15 10:32:50 +02004835 nand_release_data_interface(chip);
4836
Jesper Juhlfa671642005-11-07 01:01:27 -08004837 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004838 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004839 if (!(chip->options & NAND_OWN_BUFFERS))
4840 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004841
4842 /* Free bad block descriptor memory */
4843 if (chip->badblock_pattern && chip->badblock_pattern->options
4844 & NAND_BBT_DYNAMICSTRUCT)
4845 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004847EXPORT_SYMBOL_GPL(nand_cleanup);
4848
4849/**
4850 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4851 * held by the NAND device
4852 * @mtd: MTD device structure
4853 */
4854void nand_release(struct mtd_info *mtd)
4855{
4856 mtd_device_unregister(mtd);
4857 nand_cleanup(mtd_to_nand(mtd));
4858}
David Woodhousee0c7d762006-05-13 18:07:53 +01004859EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004860
David Woodhousee0c7d762006-05-13 18:07:53 +01004861MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004862MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4863MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004864MODULE_DESCRIPTION("Generic NAND flash driver code");