blob: 1a6310573ab486daaf1c72141f9d81e97abea86e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020048#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Huang Shijie6a8214a2012-11-19 14:43:30 +080050static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020052static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
53 struct mtd_oob_ops *ops);
54
Boris Brezillon41b207a2016-02-03 19:06:15 +010055/* Define default oob placement schemes for large and small page devices */
56static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
57 struct mtd_oob_region *oobregion)
58{
59 struct nand_chip *chip = mtd_to_nand(mtd);
60 struct nand_ecc_ctrl *ecc = &chip->ecc;
61
62 if (section > 1)
63 return -ERANGE;
64
65 if (!section) {
66 oobregion->offset = 0;
67 oobregion->length = 4;
68 } else {
69 oobregion->offset = 6;
70 oobregion->length = ecc->total - 4;
71 }
72
73 return 0;
74}
75
76static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
77 struct mtd_oob_region *oobregion)
78{
79 if (section > 1)
80 return -ERANGE;
81
82 if (mtd->oobsize == 16) {
83 if (section)
84 return -ERANGE;
85
86 oobregion->length = 8;
87 oobregion->offset = 8;
88 } else {
89 oobregion->length = 2;
90 if (!section)
91 oobregion->offset = 3;
92 else
93 oobregion->offset = 6;
94 }
95
96 return 0;
97}
98
99const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
100 .ecc = nand_ooblayout_ecc_sp,
101 .free = nand_ooblayout_free_sp,
102};
103EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
104
105static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
106 struct mtd_oob_region *oobregion)
107{
108 struct nand_chip *chip = mtd_to_nand(mtd);
109 struct nand_ecc_ctrl *ecc = &chip->ecc;
110
111 if (section)
112 return -ERANGE;
113
114 oobregion->length = ecc->total;
115 oobregion->offset = mtd->oobsize - oobregion->length;
116
117 return 0;
118}
119
120static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
121 struct mtd_oob_region *oobregion)
122{
123 struct nand_chip *chip = mtd_to_nand(mtd);
124 struct nand_ecc_ctrl *ecc = &chip->ecc;
125
126 if (section)
127 return -ERANGE;
128
129 oobregion->length = mtd->oobsize - ecc->total - 2;
130 oobregion->offset = 2;
131
132 return 0;
133}
134
135const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
136 .ecc = nand_ooblayout_ecc_lp,
137 .free = nand_ooblayout_free_lp,
138};
139EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200140
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530141static int check_offs_len(struct mtd_info *mtd,
142 loff_t ofs, uint64_t len)
143{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100144 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530145 int ret = 0;
146
147 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300148 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700149 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530150 ret = -EINVAL;
151 }
152
153 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300154 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700155 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530156 ret = -EINVAL;
157 }
158
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530159 return ret;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700164 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000165 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800166 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100168static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100170 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200172 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200173 spin_lock(&chip->controller->lock);
174 chip->controller->active = NULL;
175 chip->state = FL_READY;
176 wake_up(&chip->controller->wq);
177 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/**
181 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700182 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700184 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200186static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100188 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/**
Masanari Iida064a7692012-11-09 23:20:58 +0900193 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700194 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700196 * Default read function for 16bit buswidth with endianness conversion.
197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200199static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100201 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200202 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700207 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700209 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
211static u16 nand_read_word(struct mtd_info *mtd)
212{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100213 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200214 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700219 * @mtd: MTD device structure
220 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
222 * Default select function for 1 chip devices.
223 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100226 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200227
228 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200230 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234
235 default:
236 BUG();
237 }
238}
239
240/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100241 * nand_write_byte - [DEFAULT] write single byte to chip
242 * @mtd: MTD device structure
243 * @byte: value to write
244 *
245 * Default function to write a byte to I/O[7:0]
246 */
247static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
248{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100249 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100250
251 chip->write_buf(mtd, &byte, 1);
252}
253
254/**
255 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
256 * @mtd: MTD device structure
257 * @byte: value to write
258 *
259 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
260 */
261static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100264 uint16_t word = byte;
265
266 /*
267 * It's not entirely clear what should happen to I/O[15:8] when writing
268 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
269 *
270 * When the host supports a 16-bit bus width, only data is
271 * transferred at the 16-bit width. All address and command line
272 * transfers shall use only the lower 8-bits of the data bus. During
273 * command transfers, the host may place any value on the upper
274 * 8-bits of the data bus. During address transfers, the host shall
275 * set the upper 8-bits of the data bus to 00h.
276 *
277 * One user of the write_byte callback is nand_onfi_set_features. The
278 * four parameters are specified to be written to I/O[7:0], but this is
279 * neither an address nor a command transfer. Let's assume a 0 on the
280 * upper I/O lines is OK.
281 */
282 chip->write_buf(mtd, (uint8_t *)&word, 2);
283}
284
285/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700287 * @mtd: MTD device structure
288 * @buf: data buffer
289 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700291 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200293static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100295 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Alexander Shiyan76413832013-04-13 09:32:13 +0400297 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000301 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700302 * @mtd: MTD device structure
303 * @buf: buffer to store date
304 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700306 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200308static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100310 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Alexander Shiyan76413832013-04-13 09:32:13 +0400312 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700317 * @mtd: MTD device structure
318 * @buf: data buffer
319 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700321 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200323static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100325 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000327
Alexander Shiyan76413832013-04-13 09:32:13 +0400328 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000332 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700333 * @mtd: MTD device structure
334 * @buf: buffer to store date
335 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700337 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200339static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100341 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Alexander Shiyan76413832013-04-13 09:32:13 +0400344 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700349 * @mtd: MTD device structure
350 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000352 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530354static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Archit Taneja9f3e0422016-02-03 14:29:49 +0530356 int page, res = 0, i = 0;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100357 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 u16 bad;
359
Brian Norris5fb15492011-05-31 16:31:21 -0700360 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700361 ofs += mtd->erasesize - mtd->writesize;
362
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100363 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
364
Brian Norriscdbec052012-01-13 18:11:48 -0800365 do {
366 if (chip->options & NAND_BUSWIDTH_16) {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB,
368 chip->badblockpos & 0xFE, page);
369 bad = cpu_to_le16(chip->read_word(mtd));
370 if (chip->badblockpos & 0x1)
371 bad >>= 8;
372 else
373 bad &= 0xFF;
374 } else {
375 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
376 page);
377 bad = chip->read_byte(mtd);
378 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379
Brian Norriscdbec052012-01-13 18:11:48 -0800380 if (likely(chip->badblockbits == 8))
381 res = bad != 0xFF;
382 else
383 res = hweight8(bad) < chip->badblockbits;
384 ofs += mtd->writesize;
385 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
386 i++;
387 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return res;
390}
391
392/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700393 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700394 * @mtd: MTD device structure
395 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700397 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700398 * specific driver. It provides the details for writing a bad block marker to a
399 * block.
400 */
401static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
402{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100403 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700404 struct mtd_oob_ops ops;
405 uint8_t buf[2] = { 0, 0 };
406 int ret = 0, res, i = 0;
407
Brian Norris0ec56dc2015-02-28 02:02:30 -0800408 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700409 ops.oobbuf = buf;
410 ops.ooboffs = chip->badblockpos;
411 if (chip->options & NAND_BUSWIDTH_16) {
412 ops.ooboffs &= ~0x01;
413 ops.len = ops.ooblen = 2;
414 } else {
415 ops.len = ops.ooblen = 1;
416 }
417 ops.mode = MTD_OPS_PLACE_OOB;
418
419 /* Write to first/last page(s) if necessary */
420 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
421 ofs += mtd->erasesize - mtd->writesize;
422 do {
423 res = nand_do_write_oob(mtd, ofs, &ops);
424 if (!ret)
425 ret = res;
426
427 i++;
428 ofs += mtd->writesize;
429 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
430
431 return ret;
432}
433
434/**
435 * nand_block_markbad_lowlevel - mark a block bad
436 * @mtd: MTD device structure
437 * @ofs: offset from device start
438 *
439 * This function performs the generic NAND bad block marking steps (i.e., bad
440 * block table(s) and/or marker(s)). We only allow the hardware driver to
441 * specify how to write bad block markers to OOB (chip->block_markbad).
442 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700443 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800444 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700445 * (2) write bad block marker to OOB area of affected block (unless flag
446 * NAND_BBT_NO_OOB_BBM is present)
447 * (3) update the BBT
448 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800449 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700451static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100453 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700454 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000455
Brian Norrisb32843b2013-07-30 17:52:59 -0700456 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800457 struct erase_info einfo;
458
459 /* Attempt erase before marking OOB */
460 memset(&einfo, 0, sizeof(einfo));
461 einfo.mtd = mtd;
462 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300463 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800464 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800465
Brian Norrisb32843b2013-07-30 17:52:59 -0700466 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800467 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700468 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300469 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200470 }
Brian Norrise2414f42012-02-06 13:44:00 -0800471
Brian Norrisb32843b2013-07-30 17:52:59 -0700472 /* Mark block bad in BBT */
473 if (chip->bbt) {
474 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800475 if (!ret)
476 ret = res;
477 }
478
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200479 if (!ret)
480 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300481
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000485/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 * Check, if the device is write protected. The function expects, that the
490 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100492static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100494 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200495
Brian Norris8b6e50c2011-05-25 14:59:01 -0700496 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200497 if (chip->options & NAND_BROKEN_XD)
498 return 0;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200501 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
502 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800506 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700507 * @mtd: MTD device structure
508 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300509 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800510 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300511 */
512static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
513{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100514 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300515
516 if (!chip->bbt)
517 return 0;
518 /* Return info from the table */
519 return nand_isreserved_bbt(mtd, ofs);
520}
521
522/**
523 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
524 * @mtd: MTD device structure
525 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700526 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *
528 * Check, if the block is bad. Either by reading the bad block table or
529 * calling of the scan function.
530 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530531static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100533 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000534
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200535 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530536 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100539 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200542/**
543 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700544 * @mtd: MTD device structure
545 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200546 *
547 * Helper function for nand_wait_ready used when needing to wait in interrupt
548 * context.
549 */
550static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
551{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100552 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200553 int i;
554
555 /* Wait for the device to get ready */
556 for (i = 0; i < timeo; i++) {
557 if (chip->dev_ready(mtd))
558 break;
559 touch_softlockup_watchdog();
560 mdelay(1);
561 }
562}
563
Alex Smithb70af9b2015-10-06 14:52:07 +0100564/**
565 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
566 * @mtd: MTD device structure
567 *
568 * Wait for the ready pin after a command, and warn if a timeout occurs.
569 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100570void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000571{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100572 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100573 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000574
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200575 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100576 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200577
Brian Norris7854d3f2011-06-23 14:12:08 -0700578 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100579 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000580 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200581 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300582 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100583 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100585
Brian Norris9ebfdf52016-03-04 17:19:23 -0800586 if (!chip->dev_ready(mtd))
587 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000588}
David Woodhouse4b648b02006-09-25 17:05:24 +0100589EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200592 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
593 * @mtd: MTD device structure
594 * @timeo: Timeout in ms
595 *
596 * Wait for status ready (i.e. command done) or timeout.
597 */
598static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
599{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100600 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200601
602 timeo = jiffies + msecs_to_jiffies(timeo);
603 do {
604 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
605 break;
606 touch_softlockup_watchdog();
607 } while (time_before(jiffies, timeo));
608};
609
610/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700612 * @mtd: MTD device structure
613 * @command: the command to be sent
614 * @column: the column address for this command, -1 if none
615 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700617 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200618 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200620static void nand_command(struct mtd_info *mtd, unsigned int command,
621 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100623 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (command == NAND_CMD_SEQIN) {
628 int readcmd;
629
Joern Engel28318772006-05-22 23:18:05 +0200630 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200632 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 readcmd = NAND_CMD_READOOB;
634 } else if (column < 256) {
635 /* First 256 bytes --> READ0 */
636 readcmd = NAND_CMD_READ0;
637 } else {
638 column -= 256;
639 readcmd = NAND_CMD_READ1;
640 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Brian Norris8b6e50c2011-05-25 14:59:01 -0700646 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
648 /* Serially input address */
649 if (column != -1) {
650 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800651 if (chip->options & NAND_BUSWIDTH_16 &&
652 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200655 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200657 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200658 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200659 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 if (chip->chipsize > (32 << 20))
663 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200664 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200665 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000666
667 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700668 * Program and erase have their own busy handlers status and sequential
669 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case NAND_CMD_PAGEPROG:
674 case NAND_CMD_ERASE1:
675 case NAND_CMD_ERASE2:
676 case NAND_CMD_SEQIN:
677 case NAND_CMD_STATUS:
678 return;
679
680 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200681 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 udelay(chip->chip_delay);
684 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200685 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200686 chip->cmd_ctrl(mtd,
687 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200688 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
689 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691
David Woodhousee0c7d762006-05-13 18:07:53 +0100692 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000694 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * If we don't have access to the busy pin, we apply the given
696 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100697 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200698 if (!chip->dev_ready) {
699 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700703 /*
704 * Apply this short delay always to ensure that we do wait tWB in
705 * any case on any machine.
706 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000708
709 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/**
713 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700714 * @mtd: MTD device structure
715 * @command: the command to be sent
716 * @column: the column address for this command, -1 if none
717 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200719 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700720 * devices. We don't have the separate regions as we have in the small page
721 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
724 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100726 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Emulate NAND_CMD_READOOB */
729 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200730 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 command = NAND_CMD_READ0;
732 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000733
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200734 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400735 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200738 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* Serially input address */
741 if (column != -1) {
742 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800743 if (chip->options & NAND_BUSWIDTH_16 &&
744 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200747 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200748 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200751 chip->cmd_ctrl(mtd, page_addr, ctrl);
752 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200753 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 if (chip->chipsize > (128 << 20))
756 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200757 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200760 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000761
762 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700763 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100764 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 case NAND_CMD_CACHEDPROG:
769 case NAND_CMD_PAGEPROG:
770 case NAND_CMD_ERASE1:
771 case NAND_CMD_ERASE2:
772 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200773 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000775 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200778 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200780 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200781 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
782 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
783 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
784 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200785 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
786 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return;
788
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200789 case NAND_CMD_RNDOUT:
790 /* No ready / busy check necessary */
791 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
792 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
793 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
794 NAND_NCE | NAND_CTRL_CHANGE);
795 return;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200798 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
799 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
800 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
801 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000802
David Woodhousee0c7d762006-05-13 18:07:53 +0100803 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000805 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700807 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100808 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 if (!chip->dev_ready) {
810 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000814
Brian Norris8b6e50c2011-05-25 14:59:01 -0700815 /*
816 * Apply this short delay always to ensure that we do wait tWB in
817 * any case on any machine.
818 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100819 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000820
821 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200825 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700826 * @chip: the nand chip descriptor
827 * @mtd: MTD device structure
828 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200829 *
830 * Used when in panic, no locks are taken.
831 */
832static void panic_nand_get_device(struct nand_chip *chip,
833 struct mtd_info *mtd, int new_state)
834{
Brian Norris7854d3f2011-06-23 14:12:08 -0700835 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200836 chip->controller->active = chip;
837 chip->state = new_state;
838}
839
840/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700842 * @mtd: MTD device structure
843 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *
845 * Get the device and lock it for exclusive access
846 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200847static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800848nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100850 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200851 spinlock_t *lock = &chip->controller->lock;
852 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100853 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200854retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100855 spin_lock(lock);
856
vimal singhb8b3ee92009-07-09 20:41:22 +0530857 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200858 if (!chip->controller->active)
859 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200860
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 if (chip->controller->active == chip && chip->state == FL_READY) {
862 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100863 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100864 return 0;
865 }
866 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800867 if (chip->controller->active->state == FL_PM_SUSPENDED) {
868 chip->state = FL_PM_SUSPENDED;
869 spin_unlock(lock);
870 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800871 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100872 }
873 set_current_state(TASK_UNINTERRUPTIBLE);
874 add_wait_queue(wq, &wait);
875 spin_unlock(lock);
876 schedule();
877 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto retry;
879}
880
881/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700882 * panic_nand_wait - [GENERIC] wait until the command is done
883 * @mtd: MTD device structure
884 * @chip: NAND chip structure
885 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200886 *
887 * Wait for command done. This is a helper function for nand_wait used when
888 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400889 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200890 */
891static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
892 unsigned long timeo)
893{
894 int i;
895 for (i = 0; i < timeo; i++) {
896 if (chip->dev_ready) {
897 if (chip->dev_ready(mtd))
898 break;
899 } else {
900 if (chip->read_byte(mtd) & NAND_STATUS_READY)
901 break;
902 }
903 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200904 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200905}
906
907/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700908 * nand_wait - [DEFAULT] wait until the command is done
909 * @mtd: MTD device structure
910 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100912 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700913 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200914static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916
Alex Smithb70af9b2015-10-06 14:52:07 +0100917 int status;
918 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Brian Norris8b6e50c2011-05-25 14:59:01 -0700920 /*
921 * Apply this short delay always to ensure that we do wait tWB in any
922 * case on any machine.
923 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100924 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200926 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200928 if (in_interrupt() || oops_in_progress)
929 panic_nand_wait(mtd, chip, timeo);
930 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800931 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100932 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200933 if (chip->dev_ready) {
934 if (chip->dev_ready(mtd))
935 break;
936 } else {
937 if (chip->read_byte(mtd) & NAND_STATUS_READY)
938 break;
939 }
940 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100941 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800943
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200944 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100945 /* This can happen if in case of timeout or buggy dev_ready */
946 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return status;
948}
949
950/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +0200951 * nand_reset - Reset and initialize a NAND device
952 * @chip: The NAND chip
953 *
954 * Returns 0 for success or negative error code otherwise
955 */
956int nand_reset(struct nand_chip *chip)
957{
958 struct mtd_info *mtd = nand_to_mtd(chip);
959
960 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
961
962 return 0;
963}
964
965/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700966 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700967 * @mtd: mtd info
968 * @ofs: offset to start unlock from
969 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700970 * @invert: when = 0, unlock the range of blocks within the lower and
971 * upper boundary address
972 * when = 1, unlock the range of blocks outside the boundaries
973 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530974 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700975 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530976 */
977static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
978 uint64_t len, int invert)
979{
980 int ret = 0;
981 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100982 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +0530983
984 /* Submit address of first page to unlock */
985 page = ofs >> chip->page_shift;
986 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
987
988 /* Submit address of last page to unlock */
989 page = (ofs + len) >> chip->page_shift;
990 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
991 (page | invert) & chip->pagemask);
992
993 /* Call wait ready function */
994 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530995 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400996 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700997 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530998 __func__, status);
999 ret = -EIO;
1000 }
1001
1002 return ret;
1003}
1004
1005/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001006 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001007 * @mtd: mtd info
1008 * @ofs: offset to start unlock from
1009 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301010 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001011 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301012 */
1013int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1014{
1015 int ret = 0;
1016 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001017 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301018
Brian Norris289c0522011-07-19 10:06:09 -07001019 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301020 __func__, (unsigned long long)ofs, len);
1021
1022 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001023 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301024
1025 /* Align to last block address if size addresses end of the device */
1026 if (ofs + len == mtd->size)
1027 len -= mtd->erasesize;
1028
Huang Shijie6a8214a2012-11-19 14:43:30 +08001029 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301030
1031 /* Shift to get chip number */
1032 chipnr = ofs >> chip->chip_shift;
1033
1034 chip->select_chip(mtd, chipnr);
1035
White Ding57d3a9a2014-07-24 00:10:45 +08001036 /*
1037 * Reset the chip.
1038 * If we want to check the WP through READ STATUS and check the bit 7
1039 * we must reset the chip
1040 * some operation can also clear the bit 7 of status register
1041 * eg. erase/program a locked block
1042 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001043 nand_reset(chip);
White Ding57d3a9a2014-07-24 00:10:45 +08001044
Vimal Singh7d70f332010-02-08 15:50:49 +05301045 /* Check, if it is write protected */
1046 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001047 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301048 __func__);
1049 ret = -EIO;
1050 goto out;
1051 }
1052
1053 ret = __nand_unlock(mtd, ofs, len, 0);
1054
1055out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001056 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301057 nand_release_device(mtd);
1058
1059 return ret;
1060}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001061EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301062
1063/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001064 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001065 * @mtd: mtd info
1066 * @ofs: offset to start unlock from
1067 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301068 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001069 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1070 * have this feature, but it allows only to lock all blocks, not for specified
1071 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1072 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301073 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001074 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301075 */
1076int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1077{
1078 int ret = 0;
1079 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001080 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301081
Brian Norris289c0522011-07-19 10:06:09 -07001082 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301083 __func__, (unsigned long long)ofs, len);
1084
1085 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001086 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301087
Huang Shijie6a8214a2012-11-19 14:43:30 +08001088 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301089
1090 /* Shift to get chip number */
1091 chipnr = ofs >> chip->chip_shift;
1092
1093 chip->select_chip(mtd, chipnr);
1094
White Ding57d3a9a2014-07-24 00:10:45 +08001095 /*
1096 * Reset the chip.
1097 * If we want to check the WP through READ STATUS and check the bit 7
1098 * we must reset the chip
1099 * some operation can also clear the bit 7 of status register
1100 * eg. erase/program a locked block
1101 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001102 nand_reset(chip);
White Ding57d3a9a2014-07-24 00:10:45 +08001103
Vimal Singh7d70f332010-02-08 15:50:49 +05301104 /* Check, if it is write protected */
1105 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001106 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301107 __func__);
1108 status = MTD_ERASE_FAILED;
1109 ret = -EIO;
1110 goto out;
1111 }
1112
1113 /* Submit address of first page to lock */
1114 page = ofs >> chip->page_shift;
1115 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1116
1117 /* Call wait ready function */
1118 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301119 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001120 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001121 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301122 __func__, status);
1123 ret = -EIO;
1124 goto out;
1125 }
1126
1127 ret = __nand_unlock(mtd, ofs, len, 0x1);
1128
1129out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001130 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301131 nand_release_device(mtd);
1132
1133 return ret;
1134}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001135EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301136
1137/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001138 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1139 * @buf: buffer to test
1140 * @len: buffer length
1141 * @bitflips_threshold: maximum number of bitflips
1142 *
1143 * Check if a buffer contains only 0xff, which means the underlying region
1144 * has been erased and is ready to be programmed.
1145 * The bitflips_threshold specify the maximum number of bitflips before
1146 * considering the region is not erased.
1147 * Note: The logic of this function has been extracted from the memweight
1148 * implementation, except that nand_check_erased_buf function exit before
1149 * testing the whole buffer if the number of bitflips exceed the
1150 * bitflips_threshold value.
1151 *
1152 * Returns a positive number of bitflips less than or equal to
1153 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1154 * threshold.
1155 */
1156static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1157{
1158 const unsigned char *bitmap = buf;
1159 int bitflips = 0;
1160 int weight;
1161
1162 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1163 len--, bitmap++) {
1164 weight = hweight8(*bitmap);
1165 bitflips += BITS_PER_BYTE - weight;
1166 if (unlikely(bitflips > bitflips_threshold))
1167 return -EBADMSG;
1168 }
1169
1170 for (; len >= sizeof(long);
1171 len -= sizeof(long), bitmap += sizeof(long)) {
1172 weight = hweight_long(*((unsigned long *)bitmap));
1173 bitflips += BITS_PER_LONG - weight;
1174 if (unlikely(bitflips > bitflips_threshold))
1175 return -EBADMSG;
1176 }
1177
1178 for (; len > 0; len--, bitmap++) {
1179 weight = hweight8(*bitmap);
1180 bitflips += BITS_PER_BYTE - weight;
1181 if (unlikely(bitflips > bitflips_threshold))
1182 return -EBADMSG;
1183 }
1184
1185 return bitflips;
1186}
1187
1188/**
1189 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1190 * 0xff data
1191 * @data: data buffer to test
1192 * @datalen: data length
1193 * @ecc: ECC buffer
1194 * @ecclen: ECC length
1195 * @extraoob: extra OOB buffer
1196 * @extraooblen: extra OOB length
1197 * @bitflips_threshold: maximum number of bitflips
1198 *
1199 * Check if a data buffer and its associated ECC and OOB data contains only
1200 * 0xff pattern, which means the underlying region has been erased and is
1201 * ready to be programmed.
1202 * The bitflips_threshold specify the maximum number of bitflips before
1203 * considering the region as not erased.
1204 *
1205 * Note:
1206 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1207 * different from the NAND page size. When fixing bitflips, ECC engines will
1208 * report the number of errors per chunk, and the NAND core infrastructure
1209 * expect you to return the maximum number of bitflips for the whole page.
1210 * This is why you should always use this function on a single chunk and
1211 * not on the whole page. After checking each chunk you should update your
1212 * max_bitflips value accordingly.
1213 * 2/ When checking for bitflips in erased pages you should not only check
1214 * the payload data but also their associated ECC data, because a user might
1215 * have programmed almost all bits to 1 but a few. In this case, we
1216 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1217 * this case.
1218 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1219 * data are protected by the ECC engine.
1220 * It could also be used if you support subpages and want to attach some
1221 * extra OOB data to an ECC chunk.
1222 *
1223 * Returns a positive number of bitflips less than or equal to
1224 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1225 * threshold. In case of success, the passed buffers are filled with 0xff.
1226 */
1227int nand_check_erased_ecc_chunk(void *data, int datalen,
1228 void *ecc, int ecclen,
1229 void *extraoob, int extraooblen,
1230 int bitflips_threshold)
1231{
1232 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1233
1234 data_bitflips = nand_check_erased_buf(data, datalen,
1235 bitflips_threshold);
1236 if (data_bitflips < 0)
1237 return data_bitflips;
1238
1239 bitflips_threshold -= data_bitflips;
1240
1241 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1242 if (ecc_bitflips < 0)
1243 return ecc_bitflips;
1244
1245 bitflips_threshold -= ecc_bitflips;
1246
1247 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1248 bitflips_threshold);
1249 if (extraoob_bitflips < 0)
1250 return extraoob_bitflips;
1251
1252 if (data_bitflips)
1253 memset(data, 0xff, datalen);
1254
1255 if (ecc_bitflips)
1256 memset(ecc, 0xff, ecclen);
1257
1258 if (extraoob_bitflips)
1259 memset(extraoob, 0xff, extraooblen);
1260
1261 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1262}
1263EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1264
1265/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001266 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001267 * @mtd: mtd info structure
1268 * @chip: nand chip info structure
1269 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001270 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001271 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001272 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001273 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001274 */
1275static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001276 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001277{
1278 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001279 if (oob_required)
1280 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001281 return 0;
1282}
1283
1284/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001285 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001286 * @mtd: mtd info structure
1287 * @chip: nand chip info structure
1288 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001289 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001290 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001291 *
1292 * We need a special oob layout and handling even when OOB isn't used.
1293 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001294static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001295 struct nand_chip *chip, uint8_t *buf,
1296 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001297{
1298 int eccsize = chip->ecc.size;
1299 int eccbytes = chip->ecc.bytes;
1300 uint8_t *oob = chip->oob_poi;
1301 int steps, size;
1302
1303 for (steps = chip->ecc.steps; steps > 0; steps--) {
1304 chip->read_buf(mtd, buf, eccsize);
1305 buf += eccsize;
1306
1307 if (chip->ecc.prepad) {
1308 chip->read_buf(mtd, oob, chip->ecc.prepad);
1309 oob += chip->ecc.prepad;
1310 }
1311
1312 chip->read_buf(mtd, oob, eccbytes);
1313 oob += eccbytes;
1314
1315 if (chip->ecc.postpad) {
1316 chip->read_buf(mtd, oob, chip->ecc.postpad);
1317 oob += chip->ecc.postpad;
1318 }
1319 }
1320
1321 size = mtd->oobsize - (oob - chip->oob_poi);
1322 if (size)
1323 chip->read_buf(mtd, oob, size);
1324
1325 return 0;
1326}
1327
1328/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001329 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001330 * @mtd: mtd info structure
1331 * @chip: nand chip info structure
1332 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001333 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001334 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001335 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001336static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001337 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Boris Brezillon846031d2016-02-03 20:11:00 +01001339 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001340 int eccbytes = chip->ecc.bytes;
1341 int eccsteps = chip->ecc.steps;
1342 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001343 uint8_t *ecc_calc = chip->buffers->ecccalc;
1344 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001345 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001346
Brian Norris1fbb9382012-05-02 10:14:55 -07001347 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001348
1349 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1350 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1351
Boris Brezillon846031d2016-02-03 20:11:00 +01001352 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1353 chip->ecc.total);
1354 if (ret)
1355 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001356
1357 eccsteps = chip->ecc.steps;
1358 p = buf;
1359
1360 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1361 int stat;
1362
1363 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001364 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001365 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001366 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001367 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001368 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1369 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001370 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001371 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301375 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001376 * @mtd: mtd info structure
1377 * @chip: nand chip info structure
1378 * @data_offs: offset of requested data within the page
1379 * @readlen: data length
1380 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001381 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001382 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001383static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001384 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1385 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001386{
Boris Brezillon846031d2016-02-03 20:11:00 +01001387 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001388 uint8_t *p;
1389 int data_col_addr, i, gaps = 0;
1390 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1391 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001392 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001393 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001394 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001395
Brian Norris7854d3f2011-06-23 14:12:08 -07001396 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001397 start_step = data_offs / chip->ecc.size;
1398 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1399 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301400 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001401
Brian Norris8b6e50c2011-05-25 14:59:01 -07001402 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001403 datafrag_len = num_steps * chip->ecc.size;
1404 eccfrag_len = num_steps * chip->ecc.bytes;
1405
1406 data_col_addr = start_step * chip->ecc.size;
1407 /* If we read not a page aligned data */
1408 if (data_col_addr != 0)
1409 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1410
1411 p = bufpoi + data_col_addr;
1412 chip->read_buf(mtd, p, datafrag_len);
1413
Brian Norris8b6e50c2011-05-25 14:59:01 -07001414 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001415 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1416 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1417
Brian Norris8b6e50c2011-05-25 14:59:01 -07001418 /*
1419 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001420 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001421 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001422 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1423 if (ret)
1424 return ret;
1425
1426 if (oobregion.length < eccfrag_len)
1427 gaps = 1;
1428
Alexey Korolev3d459552008-05-15 17:23:18 +01001429 if (gaps) {
1430 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1431 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1432 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001433 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001434 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001435 * about buswidth alignment in read_buf.
1436 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001437 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001438 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001439 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001440 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001441 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1442 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001443 aligned_len++;
1444
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001445 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001446 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001447 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1448 }
1449
Boris Brezillon846031d2016-02-03 20:11:00 +01001450 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1451 chip->oob_poi, index, eccfrag_len);
1452 if (ret)
1453 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001454
1455 p = bufpoi + data_col_addr;
1456 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1457 int stat;
1458
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001459 stat = chip->ecc.correct(mtd, p,
1460 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001461 if (stat == -EBADMSG &&
1462 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1463 /* check for empty pages with bitflips */
1464 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1465 &chip->buffers->ecccode[i],
1466 chip->ecc.bytes,
1467 NULL, 0,
1468 chip->ecc.strength);
1469 }
1470
Mike Dunn3f91e942012-04-25 12:06:09 -07001471 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001472 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001473 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001474 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001475 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1476 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001477 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001478 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001479}
1480
1481/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001482 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001483 * @mtd: mtd info structure
1484 * @chip: nand chip info structure
1485 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001486 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001487 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001488 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001489 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001490 */
1491static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001492 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001493{
Boris Brezillon846031d2016-02-03 20:11:00 +01001494 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001495 int eccbytes = chip->ecc.bytes;
1496 int eccsteps = chip->ecc.steps;
1497 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001498 uint8_t *ecc_calc = chip->buffers->ecccalc;
1499 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001500 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001501
1502 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1503 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1504 chip->read_buf(mtd, p, eccsize);
1505 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1506 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001507 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001508
Boris Brezillon846031d2016-02-03 20:11:00 +01001509 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1510 chip->ecc.total);
1511 if (ret)
1512 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001513
1514 eccsteps = chip->ecc.steps;
1515 p = buf;
1516
1517 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1518 int stat;
1519
1520 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001521 if (stat == -EBADMSG &&
1522 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1523 /* check for empty pages with bitflips */
1524 stat = nand_check_erased_ecc_chunk(p, eccsize,
1525 &ecc_code[i], eccbytes,
1526 NULL, 0,
1527 chip->ecc.strength);
1528 }
1529
Mike Dunn3f91e942012-04-25 12:06:09 -07001530 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001531 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001532 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001533 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001534 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1535 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001536 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001537 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001538}
1539
1540/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001541 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001542 * @mtd: mtd info structure
1543 * @chip: nand chip info structure
1544 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001545 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001546 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001547 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001548 * Hardware ECC for large page chips, require OOB to be read first. For this
1549 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1550 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1551 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1552 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001553 */
1554static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001555 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001556{
Boris Brezillon846031d2016-02-03 20:11:00 +01001557 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001558 int eccbytes = chip->ecc.bytes;
1559 int eccsteps = chip->ecc.steps;
1560 uint8_t *p = buf;
1561 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001562 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001563 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001564
1565 /* Read the OOB area first */
1566 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1567 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1568 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1569
Boris Brezillon846031d2016-02-03 20:11:00 +01001570 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1571 chip->ecc.total);
1572 if (ret)
1573 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001574
1575 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1576 int stat;
1577
1578 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1579 chip->read_buf(mtd, p, eccsize);
1580 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1581
1582 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001583 if (stat == -EBADMSG &&
1584 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1585 /* check for empty pages with bitflips */
1586 stat = nand_check_erased_ecc_chunk(p, eccsize,
1587 &ecc_code[i], eccbytes,
1588 NULL, 0,
1589 chip->ecc.strength);
1590 }
1591
Mike Dunn3f91e942012-04-25 12:06:09 -07001592 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001593 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001594 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001595 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001596 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1597 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001598 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001599 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001600}
1601
1602/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001603 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001604 * @mtd: mtd info structure
1605 * @chip: nand chip info structure
1606 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001607 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001608 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001609 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001610 * The hw generator calculates the error syndrome automatically. Therefore we
1611 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001612 */
1613static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001614 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001615{
1616 int i, eccsize = chip->ecc.size;
1617 int eccbytes = chip->ecc.bytes;
1618 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001619 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001620 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001621 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001622 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001623
1624 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1625 int stat;
1626
1627 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1628 chip->read_buf(mtd, p, eccsize);
1629
1630 if (chip->ecc.prepad) {
1631 chip->read_buf(mtd, oob, chip->ecc.prepad);
1632 oob += chip->ecc.prepad;
1633 }
1634
1635 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1636 chip->read_buf(mtd, oob, eccbytes);
1637 stat = chip->ecc.correct(mtd, p, oob, NULL);
1638
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001639 oob += eccbytes;
1640
1641 if (chip->ecc.postpad) {
1642 chip->read_buf(mtd, oob, chip->ecc.postpad);
1643 oob += chip->ecc.postpad;
1644 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001645
1646 if (stat == -EBADMSG &&
1647 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1648 /* check for empty pages with bitflips */
1649 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1650 oob - eccpadbytes,
1651 eccpadbytes,
1652 NULL, 0,
1653 chip->ecc.strength);
1654 }
1655
1656 if (stat < 0) {
1657 mtd->ecc_stats.failed++;
1658 } else {
1659 mtd->ecc_stats.corrected += stat;
1660 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1661 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001662 }
1663
1664 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001665 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001666 if (i)
1667 chip->read_buf(mtd, oob, i);
1668
Mike Dunn3f91e942012-04-25 12:06:09 -07001669 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001670}
1671
1672/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001673 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001674 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001675 * @oob: oob destination address
1676 * @ops: oob ops structure
1677 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001678 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001679static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001680 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001681{
Boris Brezillon846031d2016-02-03 20:11:00 +01001682 struct nand_chip *chip = mtd_to_nand(mtd);
1683 int ret;
1684
Florian Fainellif8ac0412010-09-07 13:23:43 +02001685 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001686
Brian Norris0612b9d2011-08-30 18:45:40 -07001687 case MTD_OPS_PLACE_OOB:
1688 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001689 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1690 return oob + len;
1691
Boris Brezillon846031d2016-02-03 20:11:00 +01001692 case MTD_OPS_AUTO_OOB:
1693 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1694 ops->ooboffs, len);
1695 BUG_ON(ret);
1696 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001697
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001698 default:
1699 BUG();
1700 }
1701 return NULL;
1702}
1703
1704/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001705 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1706 * @mtd: MTD device structure
1707 * @retry_mode: the retry mode to use
1708 *
1709 * Some vendors supply a special command to shift the Vt threshold, to be used
1710 * when there are too many bitflips in a page (i.e., ECC error). After setting
1711 * a new threshold, the host should retry reading the page.
1712 */
1713static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1714{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001715 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001716
1717 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1718
1719 if (retry_mode >= chip->read_retries)
1720 return -EINVAL;
1721
1722 if (!chip->setup_read_retry)
1723 return -EOPNOTSUPP;
1724
1725 return chip->setup_read_retry(mtd, retry_mode);
1726}
1727
1728/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001729 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001730 * @mtd: MTD device structure
1731 * @from: offset to read from
1732 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001733 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001734 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001735 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001736static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1737 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001738{
Brian Norrise47f3db2012-05-02 10:14:56 -07001739 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001740 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001741 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001742 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001743 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001744 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001745
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001746 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001747 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001748 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001749 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001750 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001752 chipnr = (int)(from >> chip->chip_shift);
1753 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001755 realpage = (int)(from >> chip->page_shift);
1756 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001758 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001760 buf = ops->datbuf;
1761 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001762 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001763
Florian Fainellif8ac0412010-09-07 13:23:43 +02001764 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001765 unsigned int ecc_failures = mtd->ecc_stats.failed;
1766
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001767 bytes = min(mtd->writesize - col, readlen);
1768 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001769
Kamal Dasu66507c72014-05-01 20:51:19 -04001770 if (!aligned)
1771 use_bufpoi = 1;
1772 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1773 use_bufpoi = !virt_addr_valid(buf);
1774 else
1775 use_bufpoi = 0;
1776
Brian Norris8b6e50c2011-05-25 14:59:01 -07001777 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001778 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001779 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1780
1781 if (use_bufpoi && aligned)
1782 pr_debug("%s: using read bounce buffer for buf@%p\n",
1783 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Brian Norrisba84fb52014-01-03 15:13:33 -08001785read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001786 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Mike Dunnedbc45402012-04-25 12:06:11 -07001788 /*
1789 * Now read the page into the buffer. Absent an error,
1790 * the read methods return max bitflips per ecc step.
1791 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001792 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001793 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001794 oob_required,
1795 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001796 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1797 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001798 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001799 col, bytes, bufpoi,
1800 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001801 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001802 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001803 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001804 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001805 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001806 /* Invalidate page cache */
1807 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001808 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001809 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001810
Mike Dunnedbc45402012-04-25 12:06:11 -07001811 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1812
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001813 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001814 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001815 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001816 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001817 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001818 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001819 chip->pagebuf_bitflips = ret;
1820 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001821 /* Invalidate page cache */
1822 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001823 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001824 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001826
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001827 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001828 int toread = min(oobreadlen, max_oobsize);
1829
1830 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001831 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001832 oob, ops, toread);
1833 oobreadlen -= toread;
1834 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001835 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001836
1837 if (chip->options & NAND_NEED_READRDY) {
1838 /* Apply delay or wait for ready/busy pin */
1839 if (!chip->dev_ready)
1840 udelay(chip->chip_delay);
1841 else
1842 nand_wait_ready(mtd);
1843 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001844
Brian Norrisba84fb52014-01-03 15:13:33 -08001845 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001846 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001847 retry_mode++;
1848 ret = nand_setup_read_retry(mtd,
1849 retry_mode);
1850 if (ret < 0)
1851 break;
1852
1853 /* Reset failures; retry */
1854 mtd->ecc_stats.failed = ecc_failures;
1855 goto read_retry;
1856 } else {
1857 /* No more retry modes; real failure */
1858 ecc_fail = true;
1859 }
1860 }
1861
1862 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001863 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001864 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001865 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001866 max_bitflips = max_t(unsigned int, max_bitflips,
1867 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001870 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001871
Brian Norrisba84fb52014-01-03 15:13:33 -08001872 /* Reset to retry mode 0 */
1873 if (retry_mode) {
1874 ret = nand_setup_read_retry(mtd, 0);
1875 if (ret < 0)
1876 break;
1877 retry_mode = 0;
1878 }
1879
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001880 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001881 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Brian Norris8b6e50c2011-05-25 14:59:01 -07001883 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 col = 0;
1885 /* Increment page address */
1886 realpage++;
1887
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001888 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* Check, if we cross a chip boundary */
1890 if (!page) {
1891 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001892 chip->select_chip(mtd, -1);
1893 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001896 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001898 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001899 if (oob)
1900 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Mike Dunn3f91e942012-04-25 12:06:09 -07001902 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001903 return ret;
1904
Brian Norrisb72f3df2013-12-03 11:04:14 -08001905 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001906 return -EBADMSG;
1907
Mike Dunnedbc45402012-04-25 12:06:11 -07001908 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001909}
1910
1911/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001912 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001913 * @mtd: MTD device structure
1914 * @from: offset to read from
1915 * @len: number of bytes to read
1916 * @retlen: pointer to variable to store the number of read bytes
1917 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001918 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001919 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001920 */
1921static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1922 size_t *retlen, uint8_t *buf)
1923{
Brian Norris4a89ff82011-08-30 18:45:45 -07001924 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001925 int ret;
1926
Huang Shijie6a8214a2012-11-19 14:43:30 +08001927 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001928 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001929 ops.len = len;
1930 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08001931 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001932 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001933 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001934 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001935 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
1938/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001939 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001940 * @mtd: mtd info structure
1941 * @chip: nand chip info structure
1942 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001943 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001944int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001945{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001946 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001947 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001948 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001949}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001950EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001951
1952/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001953 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001954 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001955 * @mtd: mtd info structure
1956 * @chip: nand chip info structure
1957 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001958 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001959int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1960 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001961{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001962 int length = mtd->oobsize;
1963 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1964 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001965 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001966 int i, toread, sndrnd = 0, pos;
1967
1968 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1969 for (i = 0; i < chip->ecc.steps; i++) {
1970 if (sndrnd) {
1971 pos = eccsize + i * (eccsize + chunk);
1972 if (mtd->writesize > 512)
1973 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1974 else
1975 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1976 } else
1977 sndrnd = 1;
1978 toread = min_t(int, length, chunk);
1979 chip->read_buf(mtd, bufpoi, toread);
1980 bufpoi += toread;
1981 length -= toread;
1982 }
1983 if (length > 0)
1984 chip->read_buf(mtd, bufpoi, length);
1985
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001986 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001987}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001988EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001989
1990/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001991 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001992 * @mtd: mtd info structure
1993 * @chip: nand chip info structure
1994 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001995 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001996int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001997{
1998 int status = 0;
1999 const uint8_t *buf = chip->oob_poi;
2000 int length = mtd->oobsize;
2001
2002 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2003 chip->write_buf(mtd, buf, length);
2004 /* Send command to program the OOB data */
2005 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2006
2007 status = chip->waitfunc(mtd, chip);
2008
Savin Zlobec0d420f92006-06-21 11:51:20 +02002009 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002010}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002011EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002012
2013/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002014 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002015 * with syndrome - only for large page flash
2016 * @mtd: mtd info structure
2017 * @chip: nand chip info structure
2018 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002019 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002020int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2021 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002022{
2023 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2024 int eccsize = chip->ecc.size, length = mtd->oobsize;
2025 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2026 const uint8_t *bufpoi = chip->oob_poi;
2027
2028 /*
2029 * data-ecc-data-ecc ... ecc-oob
2030 * or
2031 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2032 */
2033 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2034 pos = steps * (eccsize + chunk);
2035 steps = 0;
2036 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002037 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002038
2039 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2040 for (i = 0; i < steps; i++) {
2041 if (sndcmd) {
2042 if (mtd->writesize <= 512) {
2043 uint32_t fill = 0xFFFFFFFF;
2044
2045 len = eccsize;
2046 while (len > 0) {
2047 int num = min_t(int, len, 4);
2048 chip->write_buf(mtd, (uint8_t *)&fill,
2049 num);
2050 len -= num;
2051 }
2052 } else {
2053 pos = eccsize + i * (eccsize + chunk);
2054 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2055 }
2056 } else
2057 sndcmd = 1;
2058 len = min_t(int, length, chunk);
2059 chip->write_buf(mtd, bufpoi, len);
2060 bufpoi += len;
2061 length -= len;
2062 }
2063 if (length > 0)
2064 chip->write_buf(mtd, bufpoi, length);
2065
2066 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2067 status = chip->waitfunc(mtd, chip);
2068
2069 return status & NAND_STATUS_FAIL ? -EIO : 0;
2070}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002071EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002072
2073/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002074 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002075 * @mtd: MTD device structure
2076 * @from: offset to read from
2077 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002079 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002081static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2082 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Brian Norrisc00a0992012-05-01 17:12:54 -07002084 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002085 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002086 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002087 int readlen = ops->ooblen;
2088 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002089 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002090 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Brian Norris289c0522011-07-19 10:06:09 -07002092 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302093 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Brian Norris041e4572011-06-23 16:45:24 -07002095 stats = mtd->ecc_stats;
2096
Boris BREZILLON29f10582016-03-07 10:46:52 +01002097 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002098
2099 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002100 pr_debug("%s: attempt to start read outside oob\n",
2101 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002102 return -EINVAL;
2103 }
2104
2105 /* Do not allow reads past end of device */
2106 if (unlikely(from >= mtd->size ||
2107 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2108 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002109 pr_debug("%s: attempt to read beyond end of device\n",
2110 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002111 return -EINVAL;
2112 }
Vitaly Wool70145682006-11-03 18:20:38 +03002113
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002114 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002115 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002117 /* Shift to get page */
2118 realpage = (int)(from >> chip->page_shift);
2119 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Florian Fainellif8ac0412010-09-07 13:23:43 +02002121 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002122 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002123 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002124 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002125 ret = chip->ecc.read_oob(mtd, chip, page);
2126
2127 if (ret < 0)
2128 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002129
2130 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002131 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002132
Brian Norris5bc7c332013-03-13 09:51:31 -07002133 if (chip->options & NAND_NEED_READRDY) {
2134 /* Apply delay or wait for ready/busy pin */
2135 if (!chip->dev_ready)
2136 udelay(chip->chip_delay);
2137 else
2138 nand_wait_ready(mtd);
2139 }
2140
Vitaly Wool70145682006-11-03 18:20:38 +03002141 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002142 if (!readlen)
2143 break;
2144
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002145 /* Increment page address */
2146 realpage++;
2147
2148 page = realpage & chip->pagemask;
2149 /* Check, if we cross a chip boundary */
2150 if (!page) {
2151 chipnr++;
2152 chip->select_chip(mtd, -1);
2153 chip->select_chip(mtd, chipnr);
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002156 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002158 ops->oobretlen = ops->ooblen - readlen;
2159
2160 if (ret < 0)
2161 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002162
2163 if (mtd->ecc_stats.failed - stats.failed)
2164 return -EBADMSG;
2165
2166 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
2169/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002170 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002171 * @mtd: MTD device structure
2172 * @from: offset to read from
2173 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002175 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002177static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2178 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002180 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002181
2182 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002185 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002186 pr_debug("%s: attempt to read beyond end of device\n",
2187 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return -EINVAL;
2189 }
2190
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002191 if (ops->mode != MTD_OPS_PLACE_OOB &&
2192 ops->mode != MTD_OPS_AUTO_OOB &&
2193 ops->mode != MTD_OPS_RAW)
2194 return -ENOTSUPP;
2195
Huang Shijie6a8214a2012-11-19 14:43:30 +08002196 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002198 if (!ops->datbuf)
2199 ret = nand_do_read_oob(mtd, from, ops);
2200 else
2201 ret = nand_do_read_ops(mtd, from, ops);
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002204 return ret;
2205}
2206
2207
2208/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002209 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002210 * @mtd: mtd info structure
2211 * @chip: nand chip info structure
2212 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002213 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002214 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002215 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002216 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002217 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002218static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002219 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002220{
2221 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002222 if (oob_required)
2223 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002224
2225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002228/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002229 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002230 * @mtd: mtd info structure
2231 * @chip: nand chip info structure
2232 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002233 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002234 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002235 *
2236 * We need a special oob layout and handling even when ECC isn't checked.
2237 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002238static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002239 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002240 const uint8_t *buf, int oob_required,
2241 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002242{
2243 int eccsize = chip->ecc.size;
2244 int eccbytes = chip->ecc.bytes;
2245 uint8_t *oob = chip->oob_poi;
2246 int steps, size;
2247
2248 for (steps = chip->ecc.steps; steps > 0; steps--) {
2249 chip->write_buf(mtd, buf, eccsize);
2250 buf += eccsize;
2251
2252 if (chip->ecc.prepad) {
2253 chip->write_buf(mtd, oob, chip->ecc.prepad);
2254 oob += chip->ecc.prepad;
2255 }
2256
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002257 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002258 oob += eccbytes;
2259
2260 if (chip->ecc.postpad) {
2261 chip->write_buf(mtd, oob, chip->ecc.postpad);
2262 oob += chip->ecc.postpad;
2263 }
2264 }
2265
2266 size = mtd->oobsize - (oob - chip->oob_poi);
2267 if (size)
2268 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002269
2270 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002271}
2272/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002273 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002274 * @mtd: mtd info structure
2275 * @chip: nand chip info structure
2276 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002277 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002278 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002279 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002280static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002281 const uint8_t *buf, int oob_required,
2282 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002283{
Boris Brezillon846031d2016-02-03 20:11:00 +01002284 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002285 int eccbytes = chip->ecc.bytes;
2286 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002287 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002288 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002289
Brian Norris7854d3f2011-06-23 14:12:08 -07002290 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002291 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2292 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002293
Boris Brezillon846031d2016-02-03 20:11:00 +01002294 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2295 chip->ecc.total);
2296 if (ret)
2297 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002298
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002299 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002300}
2301
2302/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002303 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002304 * @mtd: mtd info structure
2305 * @chip: nand chip info structure
2306 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002307 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002308 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002309 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002310static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002311 const uint8_t *buf, int oob_required,
2312 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002313{
Boris Brezillon846031d2016-02-03 20:11:00 +01002314 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002315 int eccbytes = chip->ecc.bytes;
2316 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002317 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002318 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002319
2320 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2321 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002322 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002323 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2324 }
2325
Boris Brezillon846031d2016-02-03 20:11:00 +01002326 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2327 chip->ecc.total);
2328 if (ret)
2329 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002330
2331 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002332
2333 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002334}
2335
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302336
2337/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002338 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302339 * @mtd: mtd info structure
2340 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002341 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302342 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002343 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302344 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002345 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302346 */
2347static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2348 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002349 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002350 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302351{
2352 uint8_t *oob_buf = chip->oob_poi;
2353 uint8_t *ecc_calc = chip->buffers->ecccalc;
2354 int ecc_size = chip->ecc.size;
2355 int ecc_bytes = chip->ecc.bytes;
2356 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302357 uint32_t start_step = offset / ecc_size;
2358 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2359 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002360 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302361
2362 for (step = 0; step < ecc_steps; step++) {
2363 /* configure controller for WRITE access */
2364 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2365
2366 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002367 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302368
2369 /* mask ECC of un-touched subpages by padding 0xFF */
2370 if ((step < start_step) || (step > end_step))
2371 memset(ecc_calc, 0xff, ecc_bytes);
2372 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002373 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302374
2375 /* mask OOB of un-touched subpages by padding 0xFF */
2376 /* if oob_required, preserve OOB metadata of written subpage */
2377 if (!oob_required || (step < start_step) || (step > end_step))
2378 memset(oob_buf, 0xff, oob_bytes);
2379
Brian Norrisd6a950802013-08-08 17:16:36 -07002380 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302381 ecc_calc += ecc_bytes;
2382 oob_buf += oob_bytes;
2383 }
2384
2385 /* copy calculated ECC for whole page to chip->buffer->oob */
2386 /* this include masked-value(0xFF) for unwritten subpages */
2387 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002388 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2389 chip->ecc.total);
2390 if (ret)
2391 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302392
2393 /* write OOB buffer to NAND device */
2394 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2395
2396 return 0;
2397}
2398
2399
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002400/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002401 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002402 * @mtd: mtd info structure
2403 * @chip: nand chip info structure
2404 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002405 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002406 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002407 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002408 * The hw generator calculates the error syndrome automatically. Therefore we
2409 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002410 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002411static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002412 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002413 const uint8_t *buf, int oob_required,
2414 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002415{
2416 int i, eccsize = chip->ecc.size;
2417 int eccbytes = chip->ecc.bytes;
2418 int eccsteps = chip->ecc.steps;
2419 const uint8_t *p = buf;
2420 uint8_t *oob = chip->oob_poi;
2421
2422 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2423
2424 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2425 chip->write_buf(mtd, p, eccsize);
2426
2427 if (chip->ecc.prepad) {
2428 chip->write_buf(mtd, oob, chip->ecc.prepad);
2429 oob += chip->ecc.prepad;
2430 }
2431
2432 chip->ecc.calculate(mtd, p, oob);
2433 chip->write_buf(mtd, oob, eccbytes);
2434 oob += eccbytes;
2435
2436 if (chip->ecc.postpad) {
2437 chip->write_buf(mtd, oob, chip->ecc.postpad);
2438 oob += chip->ecc.postpad;
2439 }
2440 }
2441
2442 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002443 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002444 if (i)
2445 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002446
2447 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002448}
2449
2450/**
David Woodhouse956e9442006-09-25 17:12:39 +01002451 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002452 * @mtd: MTD device structure
2453 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302454 * @offset: address offset within the page
2455 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002456 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002457 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002458 * @page: page number to write
2459 * @cached: cached programming
2460 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002461 */
2462static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302463 uint32_t offset, int data_len, const uint8_t *buf,
2464 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002465{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302466 int status, subpage;
2467
2468 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2469 chip->ecc.write_subpage)
2470 subpage = offset || (data_len < mtd->writesize);
2471 else
2472 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002473
2474 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2475
David Woodhouse956e9442006-09-25 17:12:39 +01002476 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302477 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002478 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302479 else if (subpage)
2480 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002481 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002482 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002483 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2484 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002485
2486 if (status < 0)
2487 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002488
2489 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002490 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002491 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002492 */
2493 cached = 0;
2494
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002495 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002496
2497 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002498 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002499 /*
2500 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002501 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002502 */
2503 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2504 status = chip->errstat(mtd, chip, FL_WRITING, status,
2505 page);
2506
2507 if (status & NAND_STATUS_FAIL)
2508 return -EIO;
2509 } else {
2510 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002511 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002512 }
2513
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002514 return 0;
2515}
2516
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002517/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002518 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002519 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002520 * @oob: oob data buffer
2521 * @len: oob data write length
2522 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002523 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002524static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2525 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002526{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002527 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002528 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002529
2530 /*
2531 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2532 * data from a previous OOB read.
2533 */
2534 memset(chip->oob_poi, 0xff, mtd->oobsize);
2535
Florian Fainellif8ac0412010-09-07 13:23:43 +02002536 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002537
Brian Norris0612b9d2011-08-30 18:45:40 -07002538 case MTD_OPS_PLACE_OOB:
2539 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002540 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2541 return oob + len;
2542
Boris Brezillon846031d2016-02-03 20:11:00 +01002543 case MTD_OPS_AUTO_OOB:
2544 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2545 ops->ooboffs, len);
2546 BUG_ON(ret);
2547 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002548
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002549 default:
2550 BUG();
2551 }
2552 return NULL;
2553}
2554
Florian Fainellif8ac0412010-09-07 13:23:43 +02002555#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002556
2557/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002558 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002559 * @mtd: MTD device structure
2560 * @to: offset to write to
2561 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002562 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002563 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002564 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002565static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2566 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002567{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002568 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002569 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002570 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002571
2572 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002573 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002574
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002575 uint8_t *oob = ops->oobbuf;
2576 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302577 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002578 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002579
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002580 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002581 if (!writelen)
2582 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002583
Brian Norris8b6e50c2011-05-25 14:59:01 -07002584 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002585 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002586 pr_notice("%s: attempt to write non page aligned data\n",
2587 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002588 return -EINVAL;
2589 }
2590
Thomas Gleixner29072b92006-09-28 15:38:36 +02002591 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002592
Thomas Gleixner6a930962006-06-28 00:11:45 +02002593 chipnr = (int)(to >> chip->chip_shift);
2594 chip->select_chip(mtd, chipnr);
2595
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002596 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002597 if (nand_check_wp(mtd)) {
2598 ret = -EIO;
2599 goto err_out;
2600 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002601
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002602 realpage = (int)(to >> chip->page_shift);
2603 page = realpage & chip->pagemask;
2604 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2605
2606 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002607 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2608 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002609 chip->pagebuf = -1;
2610
Maxim Levitsky782ce792010-02-22 20:39:36 +02002611 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002612 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2613 ret = -EINVAL;
2614 goto err_out;
2615 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002616
Florian Fainellif8ac0412010-09-07 13:23:43 +02002617 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002618 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002619 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002620 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002621 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002622 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002623
Kamal Dasu66507c72014-05-01 20:51:19 -04002624 if (part_pagewr)
2625 use_bufpoi = 1;
2626 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2627 use_bufpoi = !virt_addr_valid(buf);
2628 else
2629 use_bufpoi = 0;
2630
2631 /* Partial page write?, or need to use bounce buffer */
2632 if (use_bufpoi) {
2633 pr_debug("%s: using write bounce buffer for buf@%p\n",
2634 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002635 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002636 if (part_pagewr)
2637 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002638 chip->pagebuf = -1;
2639 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2640 memcpy(&chip->buffers->databuf[column], buf, bytes);
2641 wbuf = chip->buffers->databuf;
2642 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002643
Maxim Levitsky782ce792010-02-22 20:39:36 +02002644 if (unlikely(oob)) {
2645 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002646 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002647 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002648 } else {
2649 /* We still need to erase leftover OOB data */
2650 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002651 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302652 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2653 oob_required, page, cached,
2654 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002655 if (ret)
2656 break;
2657
2658 writelen -= bytes;
2659 if (!writelen)
2660 break;
2661
Thomas Gleixner29072b92006-09-28 15:38:36 +02002662 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002663 buf += bytes;
2664 realpage++;
2665
2666 page = realpage & chip->pagemask;
2667 /* Check, if we cross a chip boundary */
2668 if (!page) {
2669 chipnr++;
2670 chip->select_chip(mtd, -1);
2671 chip->select_chip(mtd, chipnr);
2672 }
2673 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002674
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002675 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002676 if (unlikely(oob))
2677 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002678
2679err_out:
2680 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002681 return ret;
2682}
2683
2684/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002685 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002686 * @mtd: MTD device structure
2687 * @to: offset to write to
2688 * @len: number of bytes to write
2689 * @retlen: pointer to variable to store the number of written bytes
2690 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002691 *
2692 * NAND write with ECC. Used when performing writes in interrupt context, this
2693 * may for example be called by mtdoops when writing an oops while in panic.
2694 */
2695static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2696 size_t *retlen, const uint8_t *buf)
2697{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002698 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002699 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002700 int ret;
2701
Brian Norris8b6e50c2011-05-25 14:59:01 -07002702 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002703 panic_nand_wait(mtd, chip, 400);
2704
Brian Norris8b6e50c2011-05-25 14:59:01 -07002705 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002706 panic_nand_get_device(chip, mtd, FL_WRITING);
2707
Brian Norris0ec56dc2015-02-28 02:02:30 -08002708 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002709 ops.len = len;
2710 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002711 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002712
Brian Norris4a89ff82011-08-30 18:45:45 -07002713 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002714
Brian Norris4a89ff82011-08-30 18:45:45 -07002715 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002716 return ret;
2717}
2718
2719/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002720 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002721 * @mtd: MTD device structure
2722 * @to: offset to write to
2723 * @len: number of bytes to write
2724 * @retlen: pointer to variable to store the number of written bytes
2725 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002727 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002729static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002730 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
Brian Norris4a89ff82011-08-30 18:45:45 -07002732 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002733 int ret;
2734
Huang Shijie6a8214a2012-11-19 14:43:30 +08002735 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002736 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002737 ops.len = len;
2738 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002739 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002740 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002741 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002742 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002743 return ret;
2744}
2745
2746/**
2747 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002748 * @mtd: MTD device structure
2749 * @to: offset to write to
2750 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002751 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002752 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002753 */
2754static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2755 struct mtd_oob_ops *ops)
2756{
Adrian Hunter03736152007-01-31 17:58:29 +02002757 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002758 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Brian Norris289c0522011-07-19 10:06:09 -07002760 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302761 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Boris BREZILLON29f10582016-03-07 10:46:52 +01002763 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002766 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002767 pr_debug("%s: attempt to write past end of page\n",
2768 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return -EINVAL;
2770 }
2771
Adrian Hunter03736152007-01-31 17:58:29 +02002772 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002773 pr_debug("%s: attempt to start write outside oob\n",
2774 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002775 return -EINVAL;
2776 }
2777
Jason Liu775adc3d42011-02-25 13:06:18 +08002778 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002779 if (unlikely(to >= mtd->size ||
2780 ops->ooboffs + ops->ooblen >
2781 ((mtd->size >> chip->page_shift) -
2782 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002783 pr_debug("%s: attempt to write beyond end of device\n",
2784 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002785 return -EINVAL;
2786 }
2787
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002788 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002789 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002791 /* Shift to get page */
2792 page = (int)(to >> chip->page_shift);
2793
2794 /*
2795 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2796 * of my DiskOnChip 2000 test units) will clear the whole data page too
2797 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2798 * it in the doc2000 driver in August 1999. dwmw2.
2799 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002800 nand_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002803 if (nand_check_wp(mtd)) {
2804 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002805 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002806 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002809 if (page == chip->pagebuf)
2810 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002812 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002813
Brian Norris0612b9d2011-08-30 18:45:40 -07002814 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002815 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2816 else
2817 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002818
Huang Shijieb0bb6902012-11-19 14:43:29 +08002819 chip->select_chip(mtd, -1);
2820
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002821 if (status)
2822 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Vitaly Wool70145682006-11-03 18:20:38 +03002824 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002826 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002827}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002829/**
2830 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002831 * @mtd: MTD device structure
2832 * @to: offset to write to
2833 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002834 */
2835static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2836 struct mtd_oob_ops *ops)
2837{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002838 int ret = -ENOTSUPP;
2839
2840 ops->retlen = 0;
2841
2842 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002843 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002844 pr_debug("%s: attempt to write beyond end of device\n",
2845 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002846 return -EINVAL;
2847 }
2848
Huang Shijie6a8214a2012-11-19 14:43:30 +08002849 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002850
Florian Fainellif8ac0412010-09-07 13:23:43 +02002851 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002852 case MTD_OPS_PLACE_OOB:
2853 case MTD_OPS_AUTO_OOB:
2854 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002855 break;
2856
2857 default:
2858 goto out;
2859 }
2860
2861 if (!ops->datbuf)
2862 ret = nand_do_write_oob(mtd, to, ops);
2863 else
2864 ret = nand_do_write_ops(mtd, to, ops);
2865
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002866out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002867 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return ret;
2869}
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871/**
Brian Norris49c50b92014-05-06 16:02:19 -07002872 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002873 * @mtd: MTD device structure
2874 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 *
Brian Norris49c50b92014-05-06 16:02:19 -07002876 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 */
Brian Norris49c50b92014-05-06 16:02:19 -07002878static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002880 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002882 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2883 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002884
2885 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
2888/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002890 * @mtd: MTD device structure
2891 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002893 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002895static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
David Woodhousee0c7d762006-05-13 18:07:53 +01002897 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002901 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002902 * @mtd: MTD device structure
2903 * @instr: erase instruction
2904 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002906 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002908int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2909 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Adrian Hunter69423d92008-12-10 13:37:21 +00002911 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002912 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00002913 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Brian Norris289c0522011-07-19 10:06:09 -07002915 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2916 __func__, (unsigned long long)instr->addr,
2917 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302919 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002923 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002926 page = (int)(instr->addr >> chip->page_shift);
2927 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002930 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002933 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 /* Check, if it is write protected */
2936 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002937 pr_debug("%s: device is write protected!\n",
2938 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 instr->state = MTD_ERASE_FAILED;
2940 goto erase_exit;
2941 }
2942
2943 /* Loop through the pages */
2944 len = instr->len;
2945
2946 instr->state = MTD_ERASING;
2947
2948 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002949 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002950 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05302951 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002952 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2953 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 instr->state = MTD_ERASE_FAILED;
2955 goto erase_exit;
2956 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002957
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002958 /*
2959 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002960 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002961 */
2962 if (page <= chip->pagebuf && chip->pagebuf <
2963 (page + pages_per_block))
2964 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Brian Norris49c50b92014-05-06 16:02:19 -07002966 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002968 /*
2969 * See if operation failed and additional status checks are
2970 * available
2971 */
2972 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2973 status = chip->errstat(mtd, chip, FL_ERASING,
2974 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002977 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002978 pr_debug("%s: failed erase, page 0x%08x\n",
2979 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002981 instr->fail_addr =
2982 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 goto erase_exit;
2984 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002987 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 page += pages_per_block;
2989
2990 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002991 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002993 chip->select_chip(mtd, -1);
2994 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996 }
2997 instr->state = MTD_ERASE_DONE;
2998
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002999erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
3001 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003004 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 nand_release_device(mtd);
3006
David Woodhouse49defc02007-10-06 15:01:59 -04003007 /* Do call back function */
3008 if (!ret)
3009 mtd_erase_callback(instr);
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 /* Return more or less happy */
3012 return ret;
3013}
3014
3015/**
3016 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003017 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003019 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003021static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Brian Norris289c0522011-07-19 10:06:09 -07003023 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003026 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003028 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003032 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003033 * @mtd: MTD device structure
3034 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003036static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303038 struct nand_chip *chip = mtd_to_nand(mtd);
3039 int chipnr = (int)(offs >> chip->chip_shift);
3040 int ret;
3041
3042 /* Select the NAND device */
3043 nand_get_device(mtd, FL_READING);
3044 chip->select_chip(mtd, chipnr);
3045
3046 ret = nand_block_checkbad(mtd, offs, 0);
3047
3048 chip->select_chip(mtd, -1);
3049 nand_release_device(mtd);
3050
3051 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
3054/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003055 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003056 * @mtd: MTD device structure
3057 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003059static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 int ret;
3062
Florian Fainellif8ac0412010-09-07 13:23:43 +02003063 ret = nand_block_isbad(mtd, ofs);
3064 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003065 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (ret > 0)
3067 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003068 return ret;
3069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Brian Norris5a0edb22013-07-30 17:52:58 -07003071 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
3074/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003075 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3076 * @mtd: MTD device structure
3077 * @chip: nand chip info structure
3078 * @addr: feature address.
3079 * @subfeature_param: the subfeature parameters, a four bytes array.
3080 */
3081static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3082 int addr, uint8_t *subfeature_param)
3083{
3084 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003085 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003086
David Mosbergerd914c932013-05-29 15:30:13 +03003087 if (!chip->onfi_version ||
3088 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3089 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003090 return -EINVAL;
3091
3092 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003093 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3094 chip->write_byte(mtd, subfeature_param[i]);
3095
Huang Shijie7db03ec2012-09-13 14:57:52 +08003096 status = chip->waitfunc(mtd, chip);
3097 if (status & NAND_STATUS_FAIL)
3098 return -EIO;
3099 return 0;
3100}
3101
3102/**
3103 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3104 * @mtd: MTD device structure
3105 * @chip: nand chip info structure
3106 * @addr: feature address.
3107 * @subfeature_param: the subfeature parameters, a four bytes array.
3108 */
3109static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3110 int addr, uint8_t *subfeature_param)
3111{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003112 int i;
3113
David Mosbergerd914c932013-05-29 15:30:13 +03003114 if (!chip->onfi_version ||
3115 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3116 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003117 return -EINVAL;
3118
Huang Shijie7db03ec2012-09-13 14:57:52 +08003119 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003120 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3121 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003122 return 0;
3123}
3124
3125/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003126 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003127 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003128 */
3129static int nand_suspend(struct mtd_info *mtd)
3130{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003131 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003132}
3133
3134/**
3135 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003136 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003137 */
3138static void nand_resume(struct mtd_info *mtd)
3139{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003140 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003141
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003142 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003143 nand_release_device(mtd);
3144 else
Brian Norrisd0370212011-07-19 10:06:08 -07003145 pr_err("%s called for a chip which is not in suspended state\n",
3146 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003147}
3148
Scott Branden72ea4032014-11-20 11:18:05 -08003149/**
3150 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3151 * prevent further operations
3152 * @mtd: MTD device structure
3153 */
3154static void nand_shutdown(struct mtd_info *mtd)
3155{
Brian Norris9ca641b2015-11-09 16:37:28 -08003156 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003157}
3158
Brian Norris8b6e50c2011-05-25 14:59:01 -07003159/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003160static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003163 if (!chip->chip_delay)
3164 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003167 if (chip->cmdfunc == NULL)
3168 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003171 if (chip->waitfunc == NULL)
3172 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003174 if (!chip->select_chip)
3175 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003176
Huang Shijie4204ccc2013-08-16 10:10:07 +08003177 /* set for ONFI nand */
3178 if (!chip->onfi_set_features)
3179 chip->onfi_set_features = nand_onfi_set_features;
3180 if (!chip->onfi_get_features)
3181 chip->onfi_get_features = nand_onfi_get_features;
3182
Brian Norris68e80782013-07-18 01:17:02 -07003183 /* If called twice, pointers that depend on busw may need to be reset */
3184 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003185 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3186 if (!chip->read_word)
3187 chip->read_word = nand_read_word;
3188 if (!chip->block_bad)
3189 chip->block_bad = nand_block_bad;
3190 if (!chip->block_markbad)
3191 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003192 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003193 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003194 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3195 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003196 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003197 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003198 if (!chip->scan_bbt)
3199 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003200
3201 if (!chip->controller) {
3202 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003203 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003204 }
3205
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003206}
3207
Brian Norris8b6e50c2011-05-25 14:59:01 -07003208/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003209static void sanitize_string(uint8_t *s, size_t len)
3210{
3211 ssize_t i;
3212
Brian Norris8b6e50c2011-05-25 14:59:01 -07003213 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003214 s[len - 1] = 0;
3215
Brian Norris8b6e50c2011-05-25 14:59:01 -07003216 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003217 for (i = 0; i < len - 1; i++) {
3218 if (s[i] < ' ' || s[i] > 127)
3219 s[i] = '?';
3220 }
3221
Brian Norris8b6e50c2011-05-25 14:59:01 -07003222 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003223 strim(s);
3224}
3225
3226static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3227{
3228 int i;
3229 while (len--) {
3230 crc ^= *p++ << 8;
3231 for (i = 0; i < 8; i++)
3232 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3233 }
3234
3235 return crc;
3236}
3237
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003238/* Parse the Extended Parameter Page. */
3239static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3240 struct nand_chip *chip, struct nand_onfi_params *p)
3241{
3242 struct onfi_ext_param_page *ep;
3243 struct onfi_ext_section *s;
3244 struct onfi_ext_ecc_info *ecc;
3245 uint8_t *cursor;
3246 int ret = -EINVAL;
3247 int len;
3248 int i;
3249
3250 len = le16_to_cpu(p->ext_param_page_length) * 16;
3251 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003252 if (!ep)
3253 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003254
3255 /* Send our own NAND_CMD_PARAM. */
3256 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3257
3258 /* Use the Change Read Column command to skip the ONFI param pages. */
3259 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3260 sizeof(*p) * p->num_of_param_pages , -1);
3261
3262 /* Read out the Extended Parameter Page. */
3263 chip->read_buf(mtd, (uint8_t *)ep, len);
3264 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3265 != le16_to_cpu(ep->crc))) {
3266 pr_debug("fail in the CRC.\n");
3267 goto ext_out;
3268 }
3269
3270 /*
3271 * Check the signature.
3272 * Do not strictly follow the ONFI spec, maybe changed in future.
3273 */
3274 if (strncmp(ep->sig, "EPPS", 4)) {
3275 pr_debug("The signature is invalid.\n");
3276 goto ext_out;
3277 }
3278
3279 /* find the ECC section. */
3280 cursor = (uint8_t *)(ep + 1);
3281 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3282 s = ep->sections + i;
3283 if (s->type == ONFI_SECTION_TYPE_2)
3284 break;
3285 cursor += s->length * 16;
3286 }
3287 if (i == ONFI_EXT_SECTION_MAX) {
3288 pr_debug("We can not find the ECC section.\n");
3289 goto ext_out;
3290 }
3291
3292 /* get the info we want. */
3293 ecc = (struct onfi_ext_ecc_info *)cursor;
3294
Brian Norris4ae7d222013-09-16 18:20:21 -07003295 if (!ecc->codeword_size) {
3296 pr_debug("Invalid codeword size\n");
3297 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003298 }
3299
Brian Norris4ae7d222013-09-16 18:20:21 -07003300 chip->ecc_strength_ds = ecc->ecc_bits;
3301 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003302 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003303
3304ext_out:
3305 kfree(ep);
3306 return ret;
3307}
3308
Brian Norris8429bb32013-12-03 15:51:09 -08003309static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3310{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003311 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003312 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3313
3314 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3315 feature);
3316}
3317
3318/*
3319 * Configure chip properties from Micron vendor-specific ONFI table
3320 */
3321static void nand_onfi_detect_micron(struct nand_chip *chip,
3322 struct nand_onfi_params *p)
3323{
3324 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3325
3326 if (le16_to_cpu(p->vendor_revision) < 1)
3327 return;
3328
3329 chip->read_retries = micron->read_retry_options;
3330 chip->setup_read_retry = nand_setup_read_retry_micron;
3331}
3332
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003333/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003334 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003335 */
3336static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003337 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003338{
3339 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003340 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003341 int val;
3342
Brian Norris7854d3f2011-06-23 14:12:08 -07003343 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003344 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3345 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3346 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3347 return 0;
3348
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003349 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3350 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003351 for (j = 0; j < sizeof(*p); j++)
3352 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003353 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3354 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003355 break;
3356 }
3357 }
3358
Brian Norrisc7f23a72013-08-13 10:51:55 -07003359 if (i == 3) {
3360 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003361 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003362 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003363
Brian Norris8b6e50c2011-05-25 14:59:01 -07003364 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003365 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003366 if (val & (1 << 5))
3367 chip->onfi_version = 23;
3368 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003369 chip->onfi_version = 22;
3370 else if (val & (1 << 3))
3371 chip->onfi_version = 21;
3372 else if (val & (1 << 2))
3373 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003374 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003375 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003376
3377 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003378 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003379 return 0;
3380 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003381
3382 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3383 sanitize_string(p->model, sizeof(p->model));
3384 if (!mtd->name)
3385 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003386
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003387 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003388
3389 /*
3390 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3391 * (don't ask me who thought of this...). MTD assumes that these
3392 * dimensions will be power-of-2, so just truncate the remaining area.
3393 */
3394 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3395 mtd->erasesize *= mtd->writesize;
3396
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003397 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003398
3399 /* See erasesize comment */
3400 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003401 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003402 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003403
3404 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003405 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003406 else
3407 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003408
Huang Shijie10c86ba2013-05-17 11:17:26 +08003409 if (p->ecc_bits != 0xff) {
3410 chip->ecc_strength_ds = p->ecc_bits;
3411 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003412 } else if (chip->onfi_version >= 21 &&
3413 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3414
3415 /*
3416 * The nand_flash_detect_ext_param_page() uses the
3417 * Change Read Column command which maybe not supported
3418 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3419 * now. We do not replace user supplied command function.
3420 */
3421 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3422 chip->cmdfunc = nand_command_lp;
3423
3424 /* The Extended Parameter Page is supported since ONFI 2.1. */
3425 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003426 pr_warn("Failed to detect ONFI extended param page\n");
3427 } else {
3428 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003429 }
3430
Brian Norris8429bb32013-12-03 15:51:09 -08003431 if (p->jedec_id == NAND_MFR_MICRON)
3432 nand_onfi_detect_micron(chip, p);
3433
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003434 return 1;
3435}
3436
3437/*
Huang Shijie91361812014-02-21 13:39:40 +08003438 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3439 */
3440static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3441 int *busw)
3442{
3443 struct nand_jedec_params *p = &chip->jedec_params;
3444 struct jedec_ecc_info *ecc;
3445 int val;
3446 int i, j;
3447
3448 /* Try JEDEC for unknown chip or LP */
3449 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3450 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3451 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3452 chip->read_byte(mtd) != 'C')
3453 return 0;
3454
3455 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3456 for (i = 0; i < 3; i++) {
3457 for (j = 0; j < sizeof(*p); j++)
3458 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3459
3460 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3461 le16_to_cpu(p->crc))
3462 break;
3463 }
3464
3465 if (i == 3) {
3466 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3467 return 0;
3468 }
3469
3470 /* Check version */
3471 val = le16_to_cpu(p->revision);
3472 if (val & (1 << 2))
3473 chip->jedec_version = 10;
3474 else if (val & (1 << 1))
3475 chip->jedec_version = 1; /* vendor specific version */
3476
3477 if (!chip->jedec_version) {
3478 pr_info("unsupported JEDEC version: %d\n", val);
3479 return 0;
3480 }
3481
3482 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3483 sanitize_string(p->model, sizeof(p->model));
3484 if (!mtd->name)
3485 mtd->name = p->model;
3486
3487 mtd->writesize = le32_to_cpu(p->byte_per_page);
3488
3489 /* Please reference to the comment for nand_flash_detect_onfi. */
3490 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3491 mtd->erasesize *= mtd->writesize;
3492
3493 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3494
3495 /* Please reference to the comment for nand_flash_detect_onfi. */
3496 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3497 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3498 chip->bits_per_cell = p->bits_per_cell;
3499
3500 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3501 *busw = NAND_BUSWIDTH_16;
3502 else
3503 *busw = 0;
3504
3505 /* ECC info */
3506 ecc = &p->ecc_info[0];
3507
3508 if (ecc->codeword_size >= 9) {
3509 chip->ecc_strength_ds = ecc->ecc_bits;
3510 chip->ecc_step_ds = 1 << ecc->codeword_size;
3511 } else {
3512 pr_warn("Invalid codeword size\n");
3513 }
3514
3515 return 1;
3516}
3517
3518/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003519 * nand_id_has_period - Check if an ID string has a given wraparound period
3520 * @id_data: the ID string
3521 * @arrlen: the length of the @id_data array
3522 * @period: the period of repitition
3523 *
3524 * Check if an ID string is repeated within a given sequence of bytes at
3525 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003526 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003527 * if the repetition has a period of @period; otherwise, returns zero.
3528 */
3529static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3530{
3531 int i, j;
3532 for (i = 0; i < period; i++)
3533 for (j = i + period; j < arrlen; j += period)
3534 if (id_data[i] != id_data[j])
3535 return 0;
3536 return 1;
3537}
3538
3539/*
3540 * nand_id_len - Get the length of an ID string returned by CMD_READID
3541 * @id_data: the ID string
3542 * @arrlen: the length of the @id_data array
3543
3544 * Returns the length of the ID string, according to known wraparound/trailing
3545 * zero patterns. If no pattern exists, returns the length of the array.
3546 */
3547static int nand_id_len(u8 *id_data, int arrlen)
3548{
3549 int last_nonzero, period;
3550
3551 /* Find last non-zero byte */
3552 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3553 if (id_data[last_nonzero])
3554 break;
3555
3556 /* All zeros */
3557 if (last_nonzero < 0)
3558 return 0;
3559
3560 /* Calculate wraparound period */
3561 for (period = 1; period < arrlen; period++)
3562 if (nand_id_has_period(id_data, arrlen, period))
3563 break;
3564
3565 /* There's a repeated pattern */
3566 if (period < arrlen)
3567 return period;
3568
3569 /* There are trailing zeros */
3570 if (last_nonzero < arrlen - 1)
3571 return last_nonzero + 1;
3572
3573 /* No pattern detected */
3574 return arrlen;
3575}
3576
Huang Shijie7db906b2013-09-25 14:58:11 +08003577/* Extract the bits of per cell from the 3rd byte of the extended ID */
3578static int nand_get_bits_per_cell(u8 cellinfo)
3579{
3580 int bits;
3581
3582 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3583 bits >>= NAND_CI_CELLTYPE_SHIFT;
3584 return bits + 1;
3585}
3586
Brian Norrise3b88bd2012-09-24 20:40:52 -07003587/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003588 * Many new NAND share similar device ID codes, which represent the size of the
3589 * chip. The rest of the parameters must be decoded according to generic or
3590 * manufacturer-specific "extended ID" decoding patterns.
3591 */
3592static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3593 u8 id_data[8], int *busw)
3594{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003595 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003596 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003597 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003598 /* The 4th id byte is the important one */
3599 extid = id_data[3];
3600
Brian Norrise3b88bd2012-09-24 20:40:52 -07003601 id_len = nand_id_len(id_data, 8);
3602
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003603 /*
3604 * Field definitions are in the following datasheets:
3605 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003606 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003607 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003608 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003609 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3610 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003611 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003612 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003613 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003614 /* Calc pagesize */
3615 mtd->writesize = 2048 << (extid & 0x03);
3616 extid >>= 2;
3617 /* Calc oobsize */
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003618 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003619 case 1:
3620 mtd->oobsize = 128;
3621 break;
3622 case 2:
3623 mtd->oobsize = 218;
3624 break;
3625 case 3:
3626 mtd->oobsize = 400;
3627 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003628 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003629 mtd->oobsize = 436;
3630 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003631 case 5:
3632 mtd->oobsize = 512;
3633 break;
3634 case 6:
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003635 mtd->oobsize = 640;
3636 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003637 case 7:
3638 default: /* Other cases are "reserved" (unknown) */
3639 mtd->oobsize = 1024;
3640 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003641 }
3642 extid >>= 2;
3643 /* Calc blocksize */
3644 mtd->erasesize = (128 * 1024) <<
3645 (((extid >> 1) & 0x04) | (extid & 0x03));
3646 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003647 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003648 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003649 unsigned int tmp;
3650
3651 /* Calc pagesize */
3652 mtd->writesize = 2048 << (extid & 0x03);
3653 extid >>= 2;
3654 /* Calc oobsize */
3655 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3656 case 0:
3657 mtd->oobsize = 128;
3658 break;
3659 case 1:
3660 mtd->oobsize = 224;
3661 break;
3662 case 2:
3663 mtd->oobsize = 448;
3664 break;
3665 case 3:
3666 mtd->oobsize = 64;
3667 break;
3668 case 4:
3669 mtd->oobsize = 32;
3670 break;
3671 case 5:
3672 mtd->oobsize = 16;
3673 break;
3674 default:
3675 mtd->oobsize = 640;
3676 break;
3677 }
3678 extid >>= 2;
3679 /* Calc blocksize */
3680 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3681 if (tmp < 0x03)
3682 mtd->erasesize = (128 * 1024) << tmp;
3683 else if (tmp == 0x03)
3684 mtd->erasesize = 768 * 1024;
3685 else
3686 mtd->erasesize = (64 * 1024) << tmp;
3687 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003688 } else {
3689 /* Calc pagesize */
3690 mtd->writesize = 1024 << (extid & 0x03);
3691 extid >>= 2;
3692 /* Calc oobsize */
3693 mtd->oobsize = (8 << (extid & 0x01)) *
3694 (mtd->writesize >> 9);
3695 extid >>= 2;
3696 /* Calc blocksize. Blocksize is multiples of 64KiB */
3697 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3698 extid >>= 2;
3699 /* Get buswidth information */
3700 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003701
3702 /*
3703 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3704 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3705 * follows:
3706 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3707 * 110b -> 24nm
3708 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3709 */
3710 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003711 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003712 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3713 !(id_data[4] & 0x80) /* !BENAND */) {
3714 mtd->oobsize = 32 * mtd->writesize >> 9;
3715 }
3716
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003717 }
3718}
3719
3720/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003721 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3722 * decodes a matching ID table entry and assigns the MTD size parameters for
3723 * the chip.
3724 */
3725static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3726 struct nand_flash_dev *type, u8 id_data[8],
3727 int *busw)
3728{
3729 int maf_id = id_data[0];
3730
3731 mtd->erasesize = type->erasesize;
3732 mtd->writesize = type->pagesize;
3733 mtd->oobsize = mtd->writesize / 32;
3734 *busw = type->options & NAND_BUSWIDTH_16;
3735
Huang Shijie1c195e92013-09-25 14:58:12 +08003736 /* All legacy ID NAND are small-page, SLC */
3737 chip->bits_per_cell = 1;
3738
Brian Norrisf23a4812012-09-24 20:40:51 -07003739 /*
3740 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3741 * some Spansion chips have erasesize that conflicts with size
3742 * listed in nand_ids table.
3743 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3744 */
3745 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3746 && id_data[6] == 0x00 && id_data[7] == 0x00
3747 && mtd->writesize == 512) {
3748 mtd->erasesize = 128 * 1024;
3749 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3750 }
3751}
3752
3753/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003754 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3755 * heuristic patterns using various detected parameters (e.g., manufacturer,
3756 * page size, cell-type information).
3757 */
3758static void nand_decode_bbm_options(struct mtd_info *mtd,
3759 struct nand_chip *chip, u8 id_data[8])
3760{
3761 int maf_id = id_data[0];
3762
3763 /* Set the bad block position */
3764 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3765 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3766 else
3767 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3768
3769 /*
3770 * Bad block marker is stored in the last page of each block on Samsung
3771 * and Hynix MLC devices; stored in first two pages of each block on
3772 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3773 * AMD/Spansion, and Macronix. All others scan only the first page.
3774 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003775 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003776 (maf_id == NAND_MFR_SAMSUNG ||
3777 maf_id == NAND_MFR_HYNIX))
3778 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003779 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003780 (maf_id == NAND_MFR_SAMSUNG ||
3781 maf_id == NAND_MFR_HYNIX ||
3782 maf_id == NAND_MFR_TOSHIBA ||
3783 maf_id == NAND_MFR_AMD ||
3784 maf_id == NAND_MFR_MACRONIX)) ||
3785 (mtd->writesize == 2048 &&
3786 maf_id == NAND_MFR_MICRON))
3787 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3788}
3789
Huang Shijieec6e87e2013-03-15 11:01:00 +08003790static inline bool is_full_id_nand(struct nand_flash_dev *type)
3791{
3792 return type->id_len;
3793}
3794
3795static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3796 struct nand_flash_dev *type, u8 *id_data, int *busw)
3797{
3798 if (!strncmp(type->id, id_data, type->id_len)) {
3799 mtd->writesize = type->pagesize;
3800 mtd->erasesize = type->erasesize;
3801 mtd->oobsize = type->oobsize;
3802
Huang Shijie7db906b2013-09-25 14:58:11 +08003803 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003804 chip->chipsize = (uint64_t)type->chipsize << 20;
3805 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003806 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3807 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003808 chip->onfi_timing_mode_default =
3809 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003810
3811 *busw = type->options & NAND_BUSWIDTH_16;
3812
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003813 if (!mtd->name)
3814 mtd->name = type->name;
3815
Huang Shijieec6e87e2013-03-15 11:01:00 +08003816 return true;
3817 }
3818 return false;
3819}
3820
Brian Norris7e74c2d2012-09-24 20:40:49 -07003821/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003822 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003823 */
3824static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003825 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003826 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003827 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003828{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003829 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003830 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003831 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003834 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Karl Beldanef89a882008-09-15 14:37:29 +02003836 /*
3837 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003838 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003839 */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02003840 nand_reset(chip);
Karl Beldanef89a882008-09-15 14:37:29 +02003841
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003843 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
3845 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003846 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003847 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Brian Norris8b6e50c2011-05-25 14:59:01 -07003849 /*
3850 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003851 * interface concerns can cause random data which looks like a
3852 * possibly credible NAND flash to appear. If the two results do
3853 * not match, ignore the device completely.
3854 */
3855
3856 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3857
Brian Norris4aef9b72012-09-24 20:40:48 -07003858 /* Read entire ID string */
3859 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003860 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003861
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003862 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003863 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003864 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003865 return ERR_PTR(-ENODEV);
3866 }
3867
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003868 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003869 type = nand_flash_ids;
3870
Huang Shijieec6e87e2013-03-15 11:01:00 +08003871 for (; type->name != NULL; type++) {
3872 if (is_full_id_nand(type)) {
3873 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3874 goto ident_done;
3875 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003876 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003877 }
3878 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003879
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003880 chip->onfi_version = 0;
3881 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003882 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003883 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003884 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003885
3886 /* Check if the chip is JEDEC compliant */
3887 if (nand_flash_detect_jedec(mtd, chip, &busw))
3888 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003889 }
3890
David Woodhouse5e81e882010-02-26 18:32:56 +00003891 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003892 return ERR_PTR(-ENODEV);
3893
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02003894 if (!mtd->name)
3895 mtd->name = type->name;
3896
Adrian Hunter69423d92008-12-10 13:37:21 +00003897 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003898
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02003899 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003900 /* Decode parameters from extended ID */
3901 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003902 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003903 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003904 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003905 /* Get chip options */
3906 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003907
Brian Norris8b6e50c2011-05-25 14:59:01 -07003908 /*
3909 * Check if chip is not a Samsung device. Do not clear the
3910 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003911 */
3912 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3913 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3914ident_done:
3915
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003916 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003917 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003918 if (nand_manuf_ids[maf_idx].id == *maf_id)
3919 break;
3920 }
3921
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003922 if (chip->options & NAND_BUSWIDTH_AUTO) {
3923 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3924 chip->options |= busw;
3925 nand_set_defaults(chip, busw);
3926 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3927 /*
3928 * Check, if buswidth is correct. Hardware drivers should set
3929 * chip correct!
3930 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003931 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3932 *maf_id, *dev_id);
3933 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3934 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003935 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3936 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003937 return ERR_PTR(-EINVAL);
3938 }
3939
Brian Norris7e74c2d2012-09-24 20:40:49 -07003940 nand_decode_bbm_options(mtd, chip, id_data);
3941
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003942 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003943 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003944 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003945 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003946
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003947 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003948 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003949 if (chip->chipsize & 0xffffffff)
3950 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003951 else {
3952 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3953 chip->chip_shift += 32 - 1;
3954 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003955
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003956 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003957 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003958
Brian Norris8b6e50c2011-05-25 14:59:01 -07003959 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003960 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3961 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003962
Ezequiel Garcia20171642013-11-25 08:30:31 -03003963 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3964 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003965
3966 if (chip->onfi_version)
3967 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3968 chip->onfi_params.model);
3969 else if (chip->jedec_version)
3970 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3971 chip->jedec_params.model);
3972 else
3973 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3974 type->name);
3975
Rafał Miłecki3755a992014-10-21 00:01:04 +02003976 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003977 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003978 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003979 return type;
3980}
3981
Boris Brezillond48f62b2016-04-01 14:54:32 +02003982static const char * const nand_ecc_modes[] = {
3983 [NAND_ECC_NONE] = "none",
3984 [NAND_ECC_SOFT] = "soft",
3985 [NAND_ECC_HW] = "hw",
3986 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
3987 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02003988};
3989
3990static int of_get_nand_ecc_mode(struct device_node *np)
3991{
3992 const char *pm;
3993 int err, i;
3994
3995 err = of_property_read_string(np, "nand-ecc-mode", &pm);
3996 if (err < 0)
3997 return err;
3998
3999 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4000 if (!strcasecmp(pm, nand_ecc_modes[i]))
4001 return i;
4002
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004003 /*
4004 * For backward compatibility we support few obsoleted values that don't
4005 * have their mappings into nand_ecc_modes_t anymore (they were merged
4006 * with other enums).
4007 */
4008 if (!strcasecmp(pm, "soft_bch"))
4009 return NAND_ECC_SOFT;
4010
Boris Brezillond48f62b2016-04-01 14:54:32 +02004011 return -ENODEV;
4012}
4013
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004014static const char * const nand_ecc_algos[] = {
4015 [NAND_ECC_HAMMING] = "hamming",
4016 [NAND_ECC_BCH] = "bch",
4017};
4018
Boris Brezillond48f62b2016-04-01 14:54:32 +02004019static int of_get_nand_ecc_algo(struct device_node *np)
4020{
4021 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004022 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004023
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004024 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4025 if (!err) {
4026 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4027 if (!strcasecmp(pm, nand_ecc_algos[i]))
4028 return i;
4029 return -ENODEV;
4030 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004031
4032 /*
4033 * For backward compatibility we also read "nand-ecc-mode" checking
4034 * for some obsoleted values that were specifying ECC algorithm.
4035 */
4036 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4037 if (err < 0)
4038 return err;
4039
4040 if (!strcasecmp(pm, "soft"))
4041 return NAND_ECC_HAMMING;
4042 else if (!strcasecmp(pm, "soft_bch"))
4043 return NAND_ECC_BCH;
4044
4045 return -ENODEV;
4046}
4047
4048static int of_get_nand_ecc_step_size(struct device_node *np)
4049{
4050 int ret;
4051 u32 val;
4052
4053 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4054 return ret ? ret : val;
4055}
4056
4057static int of_get_nand_ecc_strength(struct device_node *np)
4058{
4059 int ret;
4060 u32 val;
4061
4062 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4063 return ret ? ret : val;
4064}
4065
4066static int of_get_nand_bus_width(struct device_node *np)
4067{
4068 u32 val;
4069
4070 if (of_property_read_u32(np, "nand-bus-width", &val))
4071 return 8;
4072
4073 switch (val) {
4074 case 8:
4075 case 16:
4076 return val;
4077 default:
4078 return -EIO;
4079 }
4080}
4081
4082static bool of_get_nand_on_flash_bbt(struct device_node *np)
4083{
4084 return of_property_read_bool(np, "nand-on-flash-bbt");
4085}
4086
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004087static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004088{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004089 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004090 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004091
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004092 if (!dn)
4093 return 0;
4094
Brian Norris5844fee2015-01-23 00:22:27 -08004095 if (of_get_nand_bus_width(dn) == 16)
4096 chip->options |= NAND_BUSWIDTH_16;
4097
4098 if (of_get_nand_on_flash_bbt(dn))
4099 chip->bbt_options |= NAND_BBT_USE_FLASH;
4100
4101 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004102 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004103 ecc_strength = of_get_nand_ecc_strength(dn);
4104 ecc_step = of_get_nand_ecc_step_size(dn);
4105
4106 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4107 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4108 pr_err("must set both strength and step size in DT\n");
4109 return -EINVAL;
4110 }
4111
4112 if (ecc_mode >= 0)
4113 chip->ecc.mode = ecc_mode;
4114
Rafał Miłecki79082452016-03-23 11:19:02 +01004115 if (ecc_algo >= 0)
4116 chip->ecc.algo = ecc_algo;
4117
Brian Norris5844fee2015-01-23 00:22:27 -08004118 if (ecc_strength >= 0)
4119 chip->ecc.strength = ecc_strength;
4120
4121 if (ecc_step > 0)
4122 chip->ecc.size = ecc_step;
4123
4124 return 0;
4125}
4126
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004127/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004128 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004129 * @mtd: MTD device structure
4130 * @maxchips: number of chips to scan for
4131 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004132 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004133 * This is the first phase of the normal nand_scan() function. It reads the
4134 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004135 *
4136 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004137int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4138 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004139{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004140 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004141 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004142 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004143 int ret;
4144
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004145 ret = nand_dt_init(chip);
4146 if (ret)
4147 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004148
Brian Norrisf7a8e382016-01-05 10:39:45 -08004149 if (!mtd->name && mtd->dev.parent)
4150 mtd->name = dev_name(mtd->dev.parent);
4151
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004152 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4153 /*
4154 * Default functions assigned for chip_select() and
4155 * cmdfunc() both expect cmd_ctrl() to be populated,
4156 * so we need to check that that's the case
4157 */
4158 pr_err("chip.cmd_ctrl() callback is not provided");
4159 return -EINVAL;
4160 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004161 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004162 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004163
4164 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004165 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4166 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004167
4168 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004169 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004170 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004171 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004172 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 }
4174
Huang Shijie07300162012-11-09 16:23:45 +08004175 chip->select_chip(mtd, -1);
4176
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004177 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004178 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004179 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004180 /* See comment in nand_get_flash_type for reset */
Sascha Hauer2f94abf2016-09-15 10:32:45 +02004181 nand_reset(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004183 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004185 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004186 nand_dev_id != chip->read_byte(mtd)) {
4187 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 break;
Huang Shijie07300162012-11-09 16:23:45 +08004189 }
4190 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
4192 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004193 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004196 chip->numchips = i;
4197 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
David Woodhouse3b85c322006-09-25 17:06:53 +01004199 return 0;
4200}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004201EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004202
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004203static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4204{
4205 struct nand_chip *chip = mtd_to_nand(mtd);
4206 struct nand_ecc_ctrl *ecc = &chip->ecc;
4207
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004208 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004209 return -EINVAL;
4210
4211 switch (ecc->algo) {
4212 case NAND_ECC_HAMMING:
4213 ecc->calculate = nand_calculate_ecc;
4214 ecc->correct = nand_correct_data;
4215 ecc->read_page = nand_read_page_swecc;
4216 ecc->read_subpage = nand_read_subpage;
4217 ecc->write_page = nand_write_page_swecc;
4218 ecc->read_page_raw = nand_read_page_raw;
4219 ecc->write_page_raw = nand_write_page_raw;
4220 ecc->read_oob = nand_read_oob_std;
4221 ecc->write_oob = nand_write_oob_std;
4222 if (!ecc->size)
4223 ecc->size = 256;
4224 ecc->bytes = 3;
4225 ecc->strength = 1;
4226 return 0;
4227 case NAND_ECC_BCH:
4228 if (!mtd_nand_has_bch()) {
4229 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4230 return -EINVAL;
4231 }
4232 ecc->calculate = nand_bch_calculate_ecc;
4233 ecc->correct = nand_bch_correct_data;
4234 ecc->read_page = nand_read_page_swecc;
4235 ecc->read_subpage = nand_read_subpage;
4236 ecc->write_page = nand_write_page_swecc;
4237 ecc->read_page_raw = nand_read_page_raw;
4238 ecc->write_page_raw = nand_write_page_raw;
4239 ecc->read_oob = nand_read_oob_std;
4240 ecc->write_oob = nand_write_oob_std;
4241 /*
4242 * Board driver should supply ecc.size and ecc.strength
4243 * values to select how many bits are correctable.
4244 * Otherwise, default to 4 bits for large page devices.
4245 */
4246 if (!ecc->size && (mtd->oobsize >= 64)) {
4247 ecc->size = 512;
4248 ecc->strength = 4;
4249 }
4250
4251 /*
4252 * if no ecc placement scheme was provided pickup the default
4253 * large page one.
4254 */
4255 if (!mtd->ooblayout) {
4256 /* handle large page devices only */
4257 if (mtd->oobsize < 64) {
4258 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4259 return -EINVAL;
4260 }
4261
4262 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
4263 }
4264
4265 /* See nand_bch_init() for details. */
4266 ecc->bytes = 0;
4267 ecc->priv = nand_bch_init(mtd);
4268 if (!ecc->priv) {
4269 WARN(1, "BCH ECC initialization failed!\n");
4270 return -EINVAL;
4271 }
4272 return 0;
4273 default:
4274 WARN(1, "Unsupported ECC algorithm!\n");
4275 return -EINVAL;
4276 }
4277}
4278
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004279/*
4280 * Check if the chip configuration meet the datasheet requirements.
4281
4282 * If our configuration corrects A bits per B bytes and the minimum
4283 * required correction level is X bits per Y bytes, then we must ensure
4284 * both of the following are true:
4285 *
4286 * (1) A / B >= X / Y
4287 * (2) A >= X
4288 *
4289 * Requirement (1) ensures we can correct for the required bitflip density.
4290 * Requirement (2) ensures we can correct even when all bitflips are clumped
4291 * in the same sector.
4292 */
4293static bool nand_ecc_strength_good(struct mtd_info *mtd)
4294{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004295 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004296 struct nand_ecc_ctrl *ecc = &chip->ecc;
4297 int corr, ds_corr;
4298
4299 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4300 /* Not enough information */
4301 return true;
4302
4303 /*
4304 * We get the number of corrected bits per page to compare
4305 * the correction density.
4306 */
4307 corr = (mtd->writesize * ecc->strength) / ecc->size;
4308 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4309
4310 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4311}
David Woodhouse3b85c322006-09-25 17:06:53 +01004312
4313/**
4314 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004315 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004316 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004317 * This is the second phase of the normal nand_scan() function. It fills out
4318 * all the uninitialized function pointers with the defaults and scans for a
4319 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004320 */
4321int nand_scan_tail(struct mtd_info *mtd)
4322{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004323 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004324 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004325 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004326 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004327
Brian Norrise2414f42012-02-06 13:44:00 -08004328 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004329 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4330 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4331 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004332
Huang Shijief02ea4e2014-01-13 14:27:12 +08004333 if (!(chip->options & NAND_OWN_BUFFERS)) {
4334 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4335 + mtd->oobsize * 3, GFP_KERNEL);
4336 if (!nbuf)
4337 return -ENOMEM;
4338 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4339 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4340 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4341
4342 chip->buffers = nbuf;
4343 } else {
4344 if (!chip->buffers)
4345 return -ENOMEM;
4346 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004347
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004348 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004349 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004350
4351 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004352 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004353 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004354 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004355 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004356 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004359 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 break;
4361 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004362 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004363 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004364 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004366 WARN(1, "No oob scheme defined for oobsize %d\n",
4367 mtd->oobsize);
4368 ret = -EINVAL;
4369 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
4371 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004372
David Woodhouse956e9442006-09-25 17:12:39 +01004373 if (!chip->write_page)
4374 chip->write_page = nand_write_page;
4375
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004376 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004377 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004378 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004379 */
David Woodhouse956e9442006-09-25 17:12:39 +01004380
Huang Shijie97de79e02013-10-18 14:20:53 +08004381 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004382 case NAND_ECC_HW_OOB_FIRST:
4383 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004384 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004385 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4386 ret = -EINVAL;
4387 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004388 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004389 if (!ecc->read_page)
4390 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004391
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004392 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004393 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004394 if (!ecc->read_page)
4395 ecc->read_page = nand_read_page_hwecc;
4396 if (!ecc->write_page)
4397 ecc->write_page = nand_write_page_hwecc;
4398 if (!ecc->read_page_raw)
4399 ecc->read_page_raw = nand_read_page_raw;
4400 if (!ecc->write_page_raw)
4401 ecc->write_page_raw = nand_write_page_raw;
4402 if (!ecc->read_oob)
4403 ecc->read_oob = nand_read_oob_std;
4404 if (!ecc->write_oob)
4405 ecc->write_oob = nand_write_oob_std;
4406 if (!ecc->read_subpage)
4407 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004408 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004409 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004410
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004411 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004412 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4413 (!ecc->read_page ||
4414 ecc->read_page == nand_read_page_hwecc ||
4415 !ecc->write_page ||
4416 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004417 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4418 ret = -EINVAL;
4419 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004420 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004421 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004422 if (!ecc->read_page)
4423 ecc->read_page = nand_read_page_syndrome;
4424 if (!ecc->write_page)
4425 ecc->write_page = nand_write_page_syndrome;
4426 if (!ecc->read_page_raw)
4427 ecc->read_page_raw = nand_read_page_raw_syndrome;
4428 if (!ecc->write_page_raw)
4429 ecc->write_page_raw = nand_write_page_raw_syndrome;
4430 if (!ecc->read_oob)
4431 ecc->read_oob = nand_read_oob_syndrome;
4432 if (!ecc->write_oob)
4433 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004434
Huang Shijie97de79e02013-10-18 14:20:53 +08004435 if (mtd->writesize >= ecc->size) {
4436 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004437 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4438 ret = -EINVAL;
4439 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004440 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004441 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004442 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004443 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4444 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004445 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004446 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004448 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004449 ret = nand_set_ecc_soft_ops(mtd);
4450 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004451 ret = -EINVAL;
4452 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004453 }
4454 break;
4455
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004456 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004457 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004458 ecc->read_page = nand_read_page_raw;
4459 ecc->write_page = nand_write_page_raw;
4460 ecc->read_oob = nand_read_oob_std;
4461 ecc->read_page_raw = nand_read_page_raw;
4462 ecc->write_page_raw = nand_write_page_raw;
4463 ecc->write_oob = nand_write_oob_std;
4464 ecc->size = mtd->writesize;
4465 ecc->bytes = 0;
4466 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004470 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4471 ret = -EINVAL;
4472 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Brian Norris9ce244b2011-08-30 18:45:37 -07004475 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004476 if (!ecc->read_oob_raw)
4477 ecc->read_oob_raw = ecc->read_oob;
4478 if (!ecc->write_oob_raw)
4479 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004480
Boris Brezillon846031d2016-02-03 20:11:00 +01004481 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004482 mtd->ecc_strength = ecc->strength;
4483 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004484
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004485 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004486 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004487 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004488 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004489 ecc->steps = mtd->writesize / ecc->size;
4490 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004491 WARN(1, "Invalid ECC parameters\n");
4492 ret = -EINVAL;
4493 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004495 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004496
Boris Brezillon846031d2016-02-03 20:11:00 +01004497 /*
4498 * The number of bytes available for a client to place data into
4499 * the out of band area.
4500 */
4501 ret = mtd_ooblayout_count_freebytes(mtd);
4502 if (ret < 0)
4503 ret = 0;
4504
4505 mtd->oobavail = ret;
4506
4507 /* ECC sanity check: warn if it's too weak */
4508 if (!nand_ecc_strength_good(mtd))
4509 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4510 mtd->name);
4511
Brian Norris8b6e50c2011-05-25 14:59:01 -07004512 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004513 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004514 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004515 case 2:
4516 mtd->subpage_sft = 1;
4517 break;
4518 case 4:
4519 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004520 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004521 mtd->subpage_sft = 2;
4522 break;
4523 }
4524 }
4525 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4526
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004527 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004528 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004531 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004533 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304534 switch (ecc->mode) {
4535 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304536 if (chip->page_shift > 9)
4537 chip->options |= NAND_SUBPAGE_READ;
4538 break;
4539
4540 default:
4541 break;
4542 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004545 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004546 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4547 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004548 mtd->_erase = nand_erase;
4549 mtd->_point = NULL;
4550 mtd->_unpoint = NULL;
4551 mtd->_read = nand_read;
4552 mtd->_write = nand_write;
4553 mtd->_panic_write = panic_nand_write;
4554 mtd->_read_oob = nand_read_oob;
4555 mtd->_write_oob = nand_write_oob;
4556 mtd->_sync = nand_sync;
4557 mtd->_lock = NULL;
4558 mtd->_unlock = NULL;
4559 mtd->_suspend = nand_suspend;
4560 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004561 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004562 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004563 mtd->_block_isbad = nand_block_isbad;
4564 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004565 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004567 /*
4568 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4569 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4570 * properly set.
4571 */
4572 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004573 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004575 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004576 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004580 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004581err_free:
4582 if (!(chip->options & NAND_OWN_BUFFERS))
4583 kfree(chip->buffers);
4584 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004586EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Brian Norris8b6e50c2011-05-25 14:59:01 -07004588/*
4589 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004590 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004591 * to call us from in-kernel code if the core NAND support is modular.
4592 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004593#ifdef MODULE
4594#define caller_is_module() (1)
4595#else
4596#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004597 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004598#endif
4599
4600/**
4601 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004602 * @mtd: MTD device structure
4603 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004604 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004605 * This fills out all the uninitialized function pointers with the defaults.
4606 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004607 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004608 */
4609int nand_scan(struct mtd_info *mtd, int maxchips)
4610{
4611 int ret;
4612
David Woodhouse5e81e882010-02-26 18:32:56 +00004613 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004614 if (!ret)
4615 ret = nand_scan_tail(mtd);
4616 return ret;
4617}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004618EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004621 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004622 * @mtd: MTD device structure
4623 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004624void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004626 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004628 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004629 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004630 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4631
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004632 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Jesper Juhlfa671642005-11-07 01:01:27 -08004634 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004635 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004636 if (!(chip->options & NAND_OWN_BUFFERS))
4637 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004638
4639 /* Free bad block descriptor memory */
4640 if (chip->badblock_pattern && chip->badblock_pattern->options
4641 & NAND_BBT_DYNAMICSTRUCT)
4642 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643}
David Woodhousee0c7d762006-05-13 18:07:53 +01004644EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004645
David Woodhousee0c7d762006-05-13 18:07:53 +01004646MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004647MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4648MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004649MODULE_DESCRIPTION("Generic NAND flash driver code");