blob: 1d74b802aa0a5e4c271113cb15710f18384199cb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
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);
140
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/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700951 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700952 * @mtd: mtd info
953 * @ofs: offset to start unlock from
954 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700955 * @invert: when = 0, unlock the range of blocks within the lower and
956 * upper boundary address
957 * when = 1, unlock the range of blocks outside the boundaries
958 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530959 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700960 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530961 */
962static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
963 uint64_t len, int invert)
964{
965 int ret = 0;
966 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100967 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +0530968
969 /* Submit address of first page to unlock */
970 page = ofs >> chip->page_shift;
971 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
972
973 /* Submit address of last page to unlock */
974 page = (ofs + len) >> chip->page_shift;
975 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
976 (page | invert) & chip->pagemask);
977
978 /* Call wait ready function */
979 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530980 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400981 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700982 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530983 __func__, status);
984 ret = -EIO;
985 }
986
987 return ret;
988}
989
990/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700991 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700992 * @mtd: mtd info
993 * @ofs: offset to start unlock from
994 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530995 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700996 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530997 */
998int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
999{
1000 int ret = 0;
1001 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001002 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301003
Brian Norris289c0522011-07-19 10:06:09 -07001004 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301005 __func__, (unsigned long long)ofs, len);
1006
1007 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001008 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301009
1010 /* Align to last block address if size addresses end of the device */
1011 if (ofs + len == mtd->size)
1012 len -= mtd->erasesize;
1013
Huang Shijie6a8214a2012-11-19 14:43:30 +08001014 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301015
1016 /* Shift to get chip number */
1017 chipnr = ofs >> chip->chip_shift;
1018
1019 chip->select_chip(mtd, chipnr);
1020
White Ding57d3a9a2014-07-24 00:10:45 +08001021 /*
1022 * Reset the chip.
1023 * If we want to check the WP through READ STATUS and check the bit 7
1024 * we must reset the chip
1025 * some operation can also clear the bit 7 of status register
1026 * eg. erase/program a locked block
1027 */
1028 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1029
Vimal Singh7d70f332010-02-08 15:50:49 +05301030 /* Check, if it is write protected */
1031 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001032 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301033 __func__);
1034 ret = -EIO;
1035 goto out;
1036 }
1037
1038 ret = __nand_unlock(mtd, ofs, len, 0);
1039
1040out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001041 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301042 nand_release_device(mtd);
1043
1044 return ret;
1045}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001046EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301047
1048/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001049 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001050 * @mtd: mtd info
1051 * @ofs: offset to start unlock from
1052 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301053 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001054 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1055 * have this feature, but it allows only to lock all blocks, not for specified
1056 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1057 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301058 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001059 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301060 */
1061int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1062{
1063 int ret = 0;
1064 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001065 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301066
Brian Norris289c0522011-07-19 10:06:09 -07001067 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301068 __func__, (unsigned long long)ofs, len);
1069
1070 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001071 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301072
Huang Shijie6a8214a2012-11-19 14:43:30 +08001073 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301074
1075 /* Shift to get chip number */
1076 chipnr = ofs >> chip->chip_shift;
1077
1078 chip->select_chip(mtd, chipnr);
1079
White Ding57d3a9a2014-07-24 00:10:45 +08001080 /*
1081 * Reset the chip.
1082 * If we want to check the WP through READ STATUS and check the bit 7
1083 * we must reset the chip
1084 * some operation can also clear the bit 7 of status register
1085 * eg. erase/program a locked block
1086 */
1087 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1088
Vimal Singh7d70f332010-02-08 15:50:49 +05301089 /* Check, if it is write protected */
1090 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001091 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301092 __func__);
1093 status = MTD_ERASE_FAILED;
1094 ret = -EIO;
1095 goto out;
1096 }
1097
1098 /* Submit address of first page to lock */
1099 page = ofs >> chip->page_shift;
1100 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1101
1102 /* Call wait ready function */
1103 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301104 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001105 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001106 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301107 __func__, status);
1108 ret = -EIO;
1109 goto out;
1110 }
1111
1112 ret = __nand_unlock(mtd, ofs, len, 0x1);
1113
1114out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001115 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301116 nand_release_device(mtd);
1117
1118 return ret;
1119}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001120EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301121
1122/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001123 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1124 * @buf: buffer to test
1125 * @len: buffer length
1126 * @bitflips_threshold: maximum number of bitflips
1127 *
1128 * Check if a buffer contains only 0xff, which means the underlying region
1129 * has been erased and is ready to be programmed.
1130 * The bitflips_threshold specify the maximum number of bitflips before
1131 * considering the region is not erased.
1132 * Note: The logic of this function has been extracted from the memweight
1133 * implementation, except that nand_check_erased_buf function exit before
1134 * testing the whole buffer if the number of bitflips exceed the
1135 * bitflips_threshold value.
1136 *
1137 * Returns a positive number of bitflips less than or equal to
1138 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1139 * threshold.
1140 */
1141static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1142{
1143 const unsigned char *bitmap = buf;
1144 int bitflips = 0;
1145 int weight;
1146
1147 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1148 len--, bitmap++) {
1149 weight = hweight8(*bitmap);
1150 bitflips += BITS_PER_BYTE - weight;
1151 if (unlikely(bitflips > bitflips_threshold))
1152 return -EBADMSG;
1153 }
1154
1155 for (; len >= sizeof(long);
1156 len -= sizeof(long), bitmap += sizeof(long)) {
1157 weight = hweight_long(*((unsigned long *)bitmap));
1158 bitflips += BITS_PER_LONG - weight;
1159 if (unlikely(bitflips > bitflips_threshold))
1160 return -EBADMSG;
1161 }
1162
1163 for (; len > 0; len--, bitmap++) {
1164 weight = hweight8(*bitmap);
1165 bitflips += BITS_PER_BYTE - weight;
1166 if (unlikely(bitflips > bitflips_threshold))
1167 return -EBADMSG;
1168 }
1169
1170 return bitflips;
1171}
1172
1173/**
1174 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1175 * 0xff data
1176 * @data: data buffer to test
1177 * @datalen: data length
1178 * @ecc: ECC buffer
1179 * @ecclen: ECC length
1180 * @extraoob: extra OOB buffer
1181 * @extraooblen: extra OOB length
1182 * @bitflips_threshold: maximum number of bitflips
1183 *
1184 * Check if a data buffer and its associated ECC and OOB data contains only
1185 * 0xff pattern, which means the underlying region has been erased and is
1186 * ready to be programmed.
1187 * The bitflips_threshold specify the maximum number of bitflips before
1188 * considering the region as not erased.
1189 *
1190 * Note:
1191 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1192 * different from the NAND page size. When fixing bitflips, ECC engines will
1193 * report the number of errors per chunk, and the NAND core infrastructure
1194 * expect you to return the maximum number of bitflips for the whole page.
1195 * This is why you should always use this function on a single chunk and
1196 * not on the whole page. After checking each chunk you should update your
1197 * max_bitflips value accordingly.
1198 * 2/ When checking for bitflips in erased pages you should not only check
1199 * the payload data but also their associated ECC data, because a user might
1200 * have programmed almost all bits to 1 but a few. In this case, we
1201 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1202 * this case.
1203 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1204 * data are protected by the ECC engine.
1205 * It could also be used if you support subpages and want to attach some
1206 * extra OOB data to an ECC chunk.
1207 *
1208 * Returns a positive number of bitflips less than or equal to
1209 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1210 * threshold. In case of success, the passed buffers are filled with 0xff.
1211 */
1212int nand_check_erased_ecc_chunk(void *data, int datalen,
1213 void *ecc, int ecclen,
1214 void *extraoob, int extraooblen,
1215 int bitflips_threshold)
1216{
1217 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1218
1219 data_bitflips = nand_check_erased_buf(data, datalen,
1220 bitflips_threshold);
1221 if (data_bitflips < 0)
1222 return data_bitflips;
1223
1224 bitflips_threshold -= data_bitflips;
1225
1226 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1227 if (ecc_bitflips < 0)
1228 return ecc_bitflips;
1229
1230 bitflips_threshold -= ecc_bitflips;
1231
1232 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1233 bitflips_threshold);
1234 if (extraoob_bitflips < 0)
1235 return extraoob_bitflips;
1236
1237 if (data_bitflips)
1238 memset(data, 0xff, datalen);
1239
1240 if (ecc_bitflips)
1241 memset(ecc, 0xff, ecclen);
1242
1243 if (extraoob_bitflips)
1244 memset(extraoob, 0xff, extraooblen);
1245
1246 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1247}
1248EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1249
1250/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001251 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001252 * @mtd: mtd info structure
1253 * @chip: nand chip info structure
1254 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001255 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001256 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001257 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001258 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001259 */
1260static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001261 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001262{
1263 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001264 if (oob_required)
1265 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001266 return 0;
1267}
1268
1269/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001270 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001271 * @mtd: mtd info structure
1272 * @chip: nand chip info structure
1273 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001274 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001275 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001276 *
1277 * We need a special oob layout and handling even when OOB isn't used.
1278 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001279static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001280 struct nand_chip *chip, uint8_t *buf,
1281 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001282{
1283 int eccsize = chip->ecc.size;
1284 int eccbytes = chip->ecc.bytes;
1285 uint8_t *oob = chip->oob_poi;
1286 int steps, size;
1287
1288 for (steps = chip->ecc.steps; steps > 0; steps--) {
1289 chip->read_buf(mtd, buf, eccsize);
1290 buf += eccsize;
1291
1292 if (chip->ecc.prepad) {
1293 chip->read_buf(mtd, oob, chip->ecc.prepad);
1294 oob += chip->ecc.prepad;
1295 }
1296
1297 chip->read_buf(mtd, oob, eccbytes);
1298 oob += eccbytes;
1299
1300 if (chip->ecc.postpad) {
1301 chip->read_buf(mtd, oob, chip->ecc.postpad);
1302 oob += chip->ecc.postpad;
1303 }
1304 }
1305
1306 size = mtd->oobsize - (oob - chip->oob_poi);
1307 if (size)
1308 chip->read_buf(mtd, oob, size);
1309
1310 return 0;
1311}
1312
1313/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001314 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001315 * @mtd: mtd info structure
1316 * @chip: nand chip info structure
1317 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001318 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001319 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001320 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001321static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001322 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Boris Brezillon846031d2016-02-03 20:11:00 +01001324 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001325 int eccbytes = chip->ecc.bytes;
1326 int eccsteps = chip->ecc.steps;
1327 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001328 uint8_t *ecc_calc = chip->buffers->ecccalc;
1329 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001330 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001331
Brian Norris1fbb9382012-05-02 10:14:55 -07001332 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001333
1334 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1335 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1336
Boris Brezillon846031d2016-02-03 20:11:00 +01001337 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1338 chip->ecc.total);
1339 if (ret)
1340 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001341
1342 eccsteps = chip->ecc.steps;
1343 p = buf;
1344
1345 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1346 int stat;
1347
1348 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001349 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001350 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001351 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001352 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001353 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1354 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001355 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001356 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001357}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301360 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001361 * @mtd: mtd info structure
1362 * @chip: nand chip info structure
1363 * @data_offs: offset of requested data within the page
1364 * @readlen: data length
1365 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001366 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001367 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001368static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001369 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1370 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001371{
Boris Brezillon846031d2016-02-03 20:11:00 +01001372 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001373 uint8_t *p;
1374 int data_col_addr, i, gaps = 0;
1375 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1376 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001377 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001378 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001379 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001380
Brian Norris7854d3f2011-06-23 14:12:08 -07001381 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001382 start_step = data_offs / chip->ecc.size;
1383 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1384 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301385 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001386
Brian Norris8b6e50c2011-05-25 14:59:01 -07001387 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001388 datafrag_len = num_steps * chip->ecc.size;
1389 eccfrag_len = num_steps * chip->ecc.bytes;
1390
1391 data_col_addr = start_step * chip->ecc.size;
1392 /* If we read not a page aligned data */
1393 if (data_col_addr != 0)
1394 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1395
1396 p = bufpoi + data_col_addr;
1397 chip->read_buf(mtd, p, datafrag_len);
1398
Brian Norris8b6e50c2011-05-25 14:59:01 -07001399 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001400 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1401 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1402
Brian Norris8b6e50c2011-05-25 14:59:01 -07001403 /*
1404 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001405 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001406 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001407 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1408 if (ret)
1409 return ret;
1410
1411 if (oobregion.length < eccfrag_len)
1412 gaps = 1;
1413
Alexey Korolev3d459552008-05-15 17:23:18 +01001414 if (gaps) {
1415 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1416 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1417 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001418 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001419 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001420 * about buswidth alignment in read_buf.
1421 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001422 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001423 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001424 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001425 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001426 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1427 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001428 aligned_len++;
1429
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001430 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001431 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001432 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1433 }
1434
Boris Brezillon846031d2016-02-03 20:11:00 +01001435 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1436 chip->oob_poi, index, eccfrag_len);
1437 if (ret)
1438 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001439
1440 p = bufpoi + data_col_addr;
1441 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1442 int stat;
1443
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001444 stat = chip->ecc.correct(mtd, p,
1445 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001446 if (stat == -EBADMSG &&
1447 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1448 /* check for empty pages with bitflips */
1449 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1450 &chip->buffers->ecccode[i],
1451 chip->ecc.bytes,
1452 NULL, 0,
1453 chip->ecc.strength);
1454 }
1455
Mike Dunn3f91e942012-04-25 12:06:09 -07001456 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001457 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001458 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001459 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001460 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1461 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001462 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001463 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001464}
1465
1466/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001467 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001468 * @mtd: mtd info structure
1469 * @chip: nand chip info structure
1470 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001471 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001472 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001473 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001474 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001475 */
1476static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001477 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001478{
Boris Brezillon846031d2016-02-03 20:11:00 +01001479 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001480 int eccbytes = chip->ecc.bytes;
1481 int eccsteps = chip->ecc.steps;
1482 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001483 uint8_t *ecc_calc = chip->buffers->ecccalc;
1484 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001485 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001486
1487 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1488 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1489 chip->read_buf(mtd, p, eccsize);
1490 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1491 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001492 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001493
Boris Brezillon846031d2016-02-03 20:11:00 +01001494 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1495 chip->ecc.total);
1496 if (ret)
1497 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001498
1499 eccsteps = chip->ecc.steps;
1500 p = buf;
1501
1502 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1503 int stat;
1504
1505 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001506 if (stat == -EBADMSG &&
1507 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1508 /* check for empty pages with bitflips */
1509 stat = nand_check_erased_ecc_chunk(p, eccsize,
1510 &ecc_code[i], eccbytes,
1511 NULL, 0,
1512 chip->ecc.strength);
1513 }
1514
Mike Dunn3f91e942012-04-25 12:06:09 -07001515 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001516 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001517 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001518 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001519 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1520 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001521 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001522 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001523}
1524
1525/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001526 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001527 * @mtd: mtd info structure
1528 * @chip: nand chip info structure
1529 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001530 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001531 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001532 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001533 * Hardware ECC for large page chips, require OOB to be read first. For this
1534 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1535 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1536 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1537 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001538 */
1539static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001540 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001541{
Boris Brezillon846031d2016-02-03 20:11:00 +01001542 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001543 int eccbytes = chip->ecc.bytes;
1544 int eccsteps = chip->ecc.steps;
1545 uint8_t *p = buf;
1546 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001547 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001548 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001549
1550 /* Read the OOB area first */
1551 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1552 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1553 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1554
Boris Brezillon846031d2016-02-03 20:11:00 +01001555 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1556 chip->ecc.total);
1557 if (ret)
1558 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001559
1560 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1561 int stat;
1562
1563 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1564 chip->read_buf(mtd, p, eccsize);
1565 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1566
1567 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001568 if (stat == -EBADMSG &&
1569 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1570 /* check for empty pages with bitflips */
1571 stat = nand_check_erased_ecc_chunk(p, eccsize,
1572 &ecc_code[i], eccbytes,
1573 NULL, 0,
1574 chip->ecc.strength);
1575 }
1576
Mike Dunn3f91e942012-04-25 12:06:09 -07001577 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001578 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001579 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001580 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001581 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1582 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001583 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001584 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001585}
1586
1587/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001588 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001589 * @mtd: mtd info structure
1590 * @chip: nand chip info structure
1591 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001592 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001593 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001594 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001595 * The hw generator calculates the error syndrome automatically. Therefore we
1596 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001597 */
1598static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001599 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001600{
1601 int i, eccsize = chip->ecc.size;
1602 int eccbytes = chip->ecc.bytes;
1603 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001604 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001605 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001606 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001607 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001608
1609 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1610 int stat;
1611
1612 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1613 chip->read_buf(mtd, p, eccsize);
1614
1615 if (chip->ecc.prepad) {
1616 chip->read_buf(mtd, oob, chip->ecc.prepad);
1617 oob += chip->ecc.prepad;
1618 }
1619
1620 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1621 chip->read_buf(mtd, oob, eccbytes);
1622 stat = chip->ecc.correct(mtd, p, oob, NULL);
1623
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001624 oob += eccbytes;
1625
1626 if (chip->ecc.postpad) {
1627 chip->read_buf(mtd, oob, chip->ecc.postpad);
1628 oob += chip->ecc.postpad;
1629 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001630
1631 if (stat == -EBADMSG &&
1632 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1633 /* check for empty pages with bitflips */
1634 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1635 oob - eccpadbytes,
1636 eccpadbytes,
1637 NULL, 0,
1638 chip->ecc.strength);
1639 }
1640
1641 if (stat < 0) {
1642 mtd->ecc_stats.failed++;
1643 } else {
1644 mtd->ecc_stats.corrected += stat;
1645 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1646 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001647 }
1648
1649 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001650 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001651 if (i)
1652 chip->read_buf(mtd, oob, i);
1653
Mike Dunn3f91e942012-04-25 12:06:09 -07001654 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001655}
1656
1657/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001658 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001659 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001660 * @oob: oob destination address
1661 * @ops: oob ops structure
1662 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001663 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001664static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001665 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001666{
Boris Brezillon846031d2016-02-03 20:11:00 +01001667 struct nand_chip *chip = mtd_to_nand(mtd);
1668 int ret;
1669
Florian Fainellif8ac0412010-09-07 13:23:43 +02001670 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001671
Brian Norris0612b9d2011-08-30 18:45:40 -07001672 case MTD_OPS_PLACE_OOB:
1673 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001674 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1675 return oob + len;
1676
Boris Brezillon846031d2016-02-03 20:11:00 +01001677 case MTD_OPS_AUTO_OOB:
1678 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1679 ops->ooboffs, len);
1680 BUG_ON(ret);
1681 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001682
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001683 default:
1684 BUG();
1685 }
1686 return NULL;
1687}
1688
1689/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001690 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1691 * @mtd: MTD device structure
1692 * @retry_mode: the retry mode to use
1693 *
1694 * Some vendors supply a special command to shift the Vt threshold, to be used
1695 * when there are too many bitflips in a page (i.e., ECC error). After setting
1696 * a new threshold, the host should retry reading the page.
1697 */
1698static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1699{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001700 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001701
1702 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1703
1704 if (retry_mode >= chip->read_retries)
1705 return -EINVAL;
1706
1707 if (!chip->setup_read_retry)
1708 return -EOPNOTSUPP;
1709
1710 return chip->setup_read_retry(mtd, retry_mode);
1711}
1712
1713/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001714 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001715 * @mtd: MTD device structure
1716 * @from: offset to read from
1717 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001718 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001719 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001720 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001721static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1722 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001723{
Brian Norrise47f3db2012-05-02 10:14:56 -07001724 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001725 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001726 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001727 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001728 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001729 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001730
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001731 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001732 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001733 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001734 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001735 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001737 chipnr = (int)(from >> chip->chip_shift);
1738 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001740 realpage = (int)(from >> chip->page_shift);
1741 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001743 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001745 buf = ops->datbuf;
1746 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001747 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001748
Florian Fainellif8ac0412010-09-07 13:23:43 +02001749 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001750 unsigned int ecc_failures = mtd->ecc_stats.failed;
1751
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001752 bytes = min(mtd->writesize - col, readlen);
1753 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001754
Kamal Dasu66507c72014-05-01 20:51:19 -04001755 if (!aligned)
1756 use_bufpoi = 1;
1757 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1758 use_bufpoi = !virt_addr_valid(buf);
1759 else
1760 use_bufpoi = 0;
1761
Brian Norris8b6e50c2011-05-25 14:59:01 -07001762 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001763 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001764 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1765
1766 if (use_bufpoi && aligned)
1767 pr_debug("%s: using read bounce buffer for buf@%p\n",
1768 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Brian Norrisba84fb52014-01-03 15:13:33 -08001770read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001771 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Mike Dunnedbc45402012-04-25 12:06:11 -07001773 /*
1774 * Now read the page into the buffer. Absent an error,
1775 * the read methods return max bitflips per ecc step.
1776 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001777 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001778 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001779 oob_required,
1780 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001781 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1782 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001783 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001784 col, bytes, bufpoi,
1785 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001786 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001787 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001788 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001789 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001790 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001791 /* Invalidate page cache */
1792 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001793 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001794 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001795
Mike Dunnedbc45402012-04-25 12:06:11 -07001796 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1797
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001798 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001799 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001800 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001801 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001802 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001803 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001804 chip->pagebuf_bitflips = ret;
1805 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001806 /* Invalidate page cache */
1807 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001808 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001809 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001811
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001812 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001813 int toread = min(oobreadlen, max_oobsize);
1814
1815 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001816 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001817 oob, ops, toread);
1818 oobreadlen -= toread;
1819 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001820 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001821
1822 if (chip->options & NAND_NEED_READRDY) {
1823 /* Apply delay or wait for ready/busy pin */
1824 if (!chip->dev_ready)
1825 udelay(chip->chip_delay);
1826 else
1827 nand_wait_ready(mtd);
1828 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001829
Brian Norrisba84fb52014-01-03 15:13:33 -08001830 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001831 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001832 retry_mode++;
1833 ret = nand_setup_read_retry(mtd,
1834 retry_mode);
1835 if (ret < 0)
1836 break;
1837
1838 /* Reset failures; retry */
1839 mtd->ecc_stats.failed = ecc_failures;
1840 goto read_retry;
1841 } else {
1842 /* No more retry modes; real failure */
1843 ecc_fail = true;
1844 }
1845 }
1846
1847 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001848 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001849 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001850 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001851 max_bitflips = max_t(unsigned int, max_bitflips,
1852 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001855 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001856
Brian Norrisba84fb52014-01-03 15:13:33 -08001857 /* Reset to retry mode 0 */
1858 if (retry_mode) {
1859 ret = nand_setup_read_retry(mtd, 0);
1860 if (ret < 0)
1861 break;
1862 retry_mode = 0;
1863 }
1864
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001865 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001866 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Brian Norris8b6e50c2011-05-25 14:59:01 -07001868 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 col = 0;
1870 /* Increment page address */
1871 realpage++;
1872
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001873 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /* Check, if we cross a chip boundary */
1875 if (!page) {
1876 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001877 chip->select_chip(mtd, -1);
1878 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001881 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001883 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001884 if (oob)
1885 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Mike Dunn3f91e942012-04-25 12:06:09 -07001887 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001888 return ret;
1889
Brian Norrisb72f3df2013-12-03 11:04:14 -08001890 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001891 return -EBADMSG;
1892
Mike Dunnedbc45402012-04-25 12:06:11 -07001893 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001894}
1895
1896/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001897 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001898 * @mtd: MTD device structure
1899 * @from: offset to read from
1900 * @len: number of bytes to read
1901 * @retlen: pointer to variable to store the number of read bytes
1902 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001903 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001904 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001905 */
1906static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1907 size_t *retlen, uint8_t *buf)
1908{
Brian Norris4a89ff82011-08-30 18:45:45 -07001909 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001910 int ret;
1911
Huang Shijie6a8214a2012-11-19 14:43:30 +08001912 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001913 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001914 ops.len = len;
1915 ops.datbuf = buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08001916 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001917 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001918 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001919 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001920 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
1923/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001924 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001925 * @mtd: mtd info structure
1926 * @chip: nand chip info structure
1927 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001928 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001929int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001930{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001931 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001932 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001933 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001934}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001935EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001936
1937/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001938 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001939 * with syndromes
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_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1945 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001946{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001947 int length = mtd->oobsize;
1948 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1949 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001950 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001951 int i, toread, sndrnd = 0, pos;
1952
1953 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1954 for (i = 0; i < chip->ecc.steps; i++) {
1955 if (sndrnd) {
1956 pos = eccsize + i * (eccsize + chunk);
1957 if (mtd->writesize > 512)
1958 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1959 else
1960 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1961 } else
1962 sndrnd = 1;
1963 toread = min_t(int, length, chunk);
1964 chip->read_buf(mtd, bufpoi, toread);
1965 bufpoi += toread;
1966 length -= toread;
1967 }
1968 if (length > 0)
1969 chip->read_buf(mtd, bufpoi, length);
1970
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001971 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001972}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001973EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001974
1975/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001976 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001977 * @mtd: mtd info structure
1978 * @chip: nand chip info structure
1979 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001980 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001981int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001982{
1983 int status = 0;
1984 const uint8_t *buf = chip->oob_poi;
1985 int length = mtd->oobsize;
1986
1987 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1988 chip->write_buf(mtd, buf, length);
1989 /* Send command to program the OOB data */
1990 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1991
1992 status = chip->waitfunc(mtd, chip);
1993
Savin Zlobec0d420f92006-06-21 11:51:20 +02001994 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001995}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001996EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001997
1998/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001999 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002000 * with syndrome - only for large page flash
2001 * @mtd: mtd info structure
2002 * @chip: nand chip info structure
2003 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002004 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002005int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2006 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002007{
2008 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2009 int eccsize = chip->ecc.size, length = mtd->oobsize;
2010 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2011 const uint8_t *bufpoi = chip->oob_poi;
2012
2013 /*
2014 * data-ecc-data-ecc ... ecc-oob
2015 * or
2016 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2017 */
2018 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2019 pos = steps * (eccsize + chunk);
2020 steps = 0;
2021 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002022 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002023
2024 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2025 for (i = 0; i < steps; i++) {
2026 if (sndcmd) {
2027 if (mtd->writesize <= 512) {
2028 uint32_t fill = 0xFFFFFFFF;
2029
2030 len = eccsize;
2031 while (len > 0) {
2032 int num = min_t(int, len, 4);
2033 chip->write_buf(mtd, (uint8_t *)&fill,
2034 num);
2035 len -= num;
2036 }
2037 } else {
2038 pos = eccsize + i * (eccsize + chunk);
2039 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2040 }
2041 } else
2042 sndcmd = 1;
2043 len = min_t(int, length, chunk);
2044 chip->write_buf(mtd, bufpoi, len);
2045 bufpoi += len;
2046 length -= len;
2047 }
2048 if (length > 0)
2049 chip->write_buf(mtd, bufpoi, length);
2050
2051 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2052 status = chip->waitfunc(mtd, chip);
2053
2054 return status & NAND_STATUS_FAIL ? -EIO : 0;
2055}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002056EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002057
2058/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002059 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002060 * @mtd: MTD device structure
2061 * @from: offset to read from
2062 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002064 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002066static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2067 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Brian Norrisc00a0992012-05-01 17:12:54 -07002069 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002070 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002071 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002072 int readlen = ops->ooblen;
2073 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002074 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002075 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Brian Norris289c0522011-07-19 10:06:09 -07002077 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302078 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Brian Norris041e4572011-06-23 16:45:24 -07002080 stats = mtd->ecc_stats;
2081
Boris BREZILLON29f10582016-03-07 10:46:52 +01002082 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002083
2084 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002085 pr_debug("%s: attempt to start read outside oob\n",
2086 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002087 return -EINVAL;
2088 }
2089
2090 /* Do not allow reads past end of device */
2091 if (unlikely(from >= mtd->size ||
2092 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2093 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002094 pr_debug("%s: attempt to read beyond end of device\n",
2095 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002096 return -EINVAL;
2097 }
Vitaly Wool70145682006-11-03 18:20:38 +03002098
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002099 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002100 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002102 /* Shift to get page */
2103 realpage = (int)(from >> chip->page_shift);
2104 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Florian Fainellif8ac0412010-09-07 13:23:43 +02002106 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002107 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002108 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002109 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002110 ret = chip->ecc.read_oob(mtd, chip, page);
2111
2112 if (ret < 0)
2113 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002114
2115 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002116 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002117
Brian Norris5bc7c332013-03-13 09:51:31 -07002118 if (chip->options & NAND_NEED_READRDY) {
2119 /* Apply delay or wait for ready/busy pin */
2120 if (!chip->dev_ready)
2121 udelay(chip->chip_delay);
2122 else
2123 nand_wait_ready(mtd);
2124 }
2125
Vitaly Wool70145682006-11-03 18:20:38 +03002126 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002127 if (!readlen)
2128 break;
2129
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002130 /* Increment page address */
2131 realpage++;
2132
2133 page = realpage & chip->pagemask;
2134 /* Check, if we cross a chip boundary */
2135 if (!page) {
2136 chipnr++;
2137 chip->select_chip(mtd, -1);
2138 chip->select_chip(mtd, chipnr);
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002141 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002143 ops->oobretlen = ops->ooblen - readlen;
2144
2145 if (ret < 0)
2146 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002147
2148 if (mtd->ecc_stats.failed - stats.failed)
2149 return -EBADMSG;
2150
2151 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
2154/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002155 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002156 * @mtd: MTD device structure
2157 * @from: offset to read from
2158 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002160 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002162static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2163 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002165 int ret = -ENOTSUPP;
2166
2167 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002170 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002171 pr_debug("%s: attempt to read beyond end of device\n",
2172 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return -EINVAL;
2174 }
2175
Huang Shijie6a8214a2012-11-19 14:43:30 +08002176 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Florian Fainellif8ac0412010-09-07 13:23:43 +02002178 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002179 case MTD_OPS_PLACE_OOB:
2180 case MTD_OPS_AUTO_OOB:
2181 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002182 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002183
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002184 default:
2185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002188 if (!ops->datbuf)
2189 ret = nand_do_read_oob(mtd, from, ops);
2190 else
2191 ret = nand_do_read_ops(mtd, from, ops);
2192
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002193out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002195 return ret;
2196}
2197
2198
2199/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002200 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002201 * @mtd: mtd info structure
2202 * @chip: nand chip info structure
2203 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002204 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002205 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002206 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002207 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002208 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002209static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002210 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002211{
2212 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002213 if (oob_required)
2214 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002215
2216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002219/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002220 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002221 * @mtd: mtd info structure
2222 * @chip: nand chip info structure
2223 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002224 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002225 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002226 *
2227 * We need a special oob layout and handling even when ECC isn't checked.
2228 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002229static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002230 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002231 const uint8_t *buf, int oob_required,
2232 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002233{
2234 int eccsize = chip->ecc.size;
2235 int eccbytes = chip->ecc.bytes;
2236 uint8_t *oob = chip->oob_poi;
2237 int steps, size;
2238
2239 for (steps = chip->ecc.steps; steps > 0; steps--) {
2240 chip->write_buf(mtd, buf, eccsize);
2241 buf += eccsize;
2242
2243 if (chip->ecc.prepad) {
2244 chip->write_buf(mtd, oob, chip->ecc.prepad);
2245 oob += chip->ecc.prepad;
2246 }
2247
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002248 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002249 oob += eccbytes;
2250
2251 if (chip->ecc.postpad) {
2252 chip->write_buf(mtd, oob, chip->ecc.postpad);
2253 oob += chip->ecc.postpad;
2254 }
2255 }
2256
2257 size = mtd->oobsize - (oob - chip->oob_poi);
2258 if (size)
2259 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002260
2261 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002262}
2263/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002264 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002265 * @mtd: mtd info structure
2266 * @chip: nand chip info structure
2267 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002268 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002269 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002270 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002271static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002272 const uint8_t *buf, int oob_required,
2273 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002274{
Boris Brezillon846031d2016-02-03 20:11:00 +01002275 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002276 int eccbytes = chip->ecc.bytes;
2277 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002278 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002279 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002280
Brian Norris7854d3f2011-06-23 14:12:08 -07002281 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002282 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2283 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002284
Boris Brezillon846031d2016-02-03 20:11:00 +01002285 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2286 chip->ecc.total);
2287 if (ret)
2288 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002289
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002290 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002291}
2292
2293/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002294 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002295 * @mtd: mtd info structure
2296 * @chip: nand chip info structure
2297 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002298 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002299 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002300 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002301static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002302 const uint8_t *buf, int oob_required,
2303 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002304{
Boris Brezillon846031d2016-02-03 20:11:00 +01002305 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002306 int eccbytes = chip->ecc.bytes;
2307 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002308 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002309 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002310
2311 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2312 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002313 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002314 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2315 }
2316
Boris Brezillon846031d2016-02-03 20:11:00 +01002317 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2318 chip->ecc.total);
2319 if (ret)
2320 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002321
2322 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002323
2324 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002325}
2326
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302327
2328/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002329 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302330 * @mtd: mtd info structure
2331 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002332 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302333 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002334 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302335 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002336 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302337 */
2338static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2339 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002340 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002341 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302342{
2343 uint8_t *oob_buf = chip->oob_poi;
2344 uint8_t *ecc_calc = chip->buffers->ecccalc;
2345 int ecc_size = chip->ecc.size;
2346 int ecc_bytes = chip->ecc.bytes;
2347 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302348 uint32_t start_step = offset / ecc_size;
2349 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2350 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002351 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302352
2353 for (step = 0; step < ecc_steps; step++) {
2354 /* configure controller for WRITE access */
2355 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2356
2357 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002358 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302359
2360 /* mask ECC of un-touched subpages by padding 0xFF */
2361 if ((step < start_step) || (step > end_step))
2362 memset(ecc_calc, 0xff, ecc_bytes);
2363 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002364 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302365
2366 /* mask OOB of un-touched subpages by padding 0xFF */
2367 /* if oob_required, preserve OOB metadata of written subpage */
2368 if (!oob_required || (step < start_step) || (step > end_step))
2369 memset(oob_buf, 0xff, oob_bytes);
2370
Brian Norrisd6a950802013-08-08 17:16:36 -07002371 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302372 ecc_calc += ecc_bytes;
2373 oob_buf += oob_bytes;
2374 }
2375
2376 /* copy calculated ECC for whole page to chip->buffer->oob */
2377 /* this include masked-value(0xFF) for unwritten subpages */
2378 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002379 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2380 chip->ecc.total);
2381 if (ret)
2382 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302383
2384 /* write OOB buffer to NAND device */
2385 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2386
2387 return 0;
2388}
2389
2390
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002391/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002392 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002393 * @mtd: mtd info structure
2394 * @chip: nand chip info structure
2395 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002396 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002397 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002398 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002399 * The hw generator calculates the error syndrome automatically. Therefore we
2400 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002401 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002402static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002403 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002404 const uint8_t *buf, int oob_required,
2405 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002406{
2407 int i, eccsize = chip->ecc.size;
2408 int eccbytes = chip->ecc.bytes;
2409 int eccsteps = chip->ecc.steps;
2410 const uint8_t *p = buf;
2411 uint8_t *oob = chip->oob_poi;
2412
2413 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2414
2415 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2416 chip->write_buf(mtd, p, eccsize);
2417
2418 if (chip->ecc.prepad) {
2419 chip->write_buf(mtd, oob, chip->ecc.prepad);
2420 oob += chip->ecc.prepad;
2421 }
2422
2423 chip->ecc.calculate(mtd, p, oob);
2424 chip->write_buf(mtd, oob, eccbytes);
2425 oob += eccbytes;
2426
2427 if (chip->ecc.postpad) {
2428 chip->write_buf(mtd, oob, chip->ecc.postpad);
2429 oob += chip->ecc.postpad;
2430 }
2431 }
2432
2433 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002434 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002435 if (i)
2436 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002437
2438 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002439}
2440
2441/**
David Woodhouse956e9442006-09-25 17:12:39 +01002442 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002443 * @mtd: MTD device structure
2444 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302445 * @offset: address offset within the page
2446 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002447 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002448 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002449 * @page: page number to write
2450 * @cached: cached programming
2451 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002452 */
2453static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302454 uint32_t offset, int data_len, const uint8_t *buf,
2455 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002456{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302457 int status, subpage;
2458
2459 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2460 chip->ecc.write_subpage)
2461 subpage = offset || (data_len < mtd->writesize);
2462 else
2463 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002464
2465 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2466
David Woodhouse956e9442006-09-25 17:12:39 +01002467 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302468 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002469 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302470 else if (subpage)
2471 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002472 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002473 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002474 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2475 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002476
2477 if (status < 0)
2478 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002479
2480 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002481 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002482 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002483 */
2484 cached = 0;
2485
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002486 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002487
2488 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002489 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002490 /*
2491 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002492 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002493 */
2494 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2495 status = chip->errstat(mtd, chip, FL_WRITING, status,
2496 page);
2497
2498 if (status & NAND_STATUS_FAIL)
2499 return -EIO;
2500 } else {
2501 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002502 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002503 }
2504
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002505 return 0;
2506}
2507
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002508/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002509 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002510 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002511 * @oob: oob data buffer
2512 * @len: oob data write length
2513 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002514 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002515static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2516 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002517{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002518 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002519 int ret;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002520
2521 /*
2522 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2523 * data from a previous OOB read.
2524 */
2525 memset(chip->oob_poi, 0xff, mtd->oobsize);
2526
Florian Fainellif8ac0412010-09-07 13:23:43 +02002527 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002528
Brian Norris0612b9d2011-08-30 18:45:40 -07002529 case MTD_OPS_PLACE_OOB:
2530 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002531 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2532 return oob + len;
2533
Boris Brezillon846031d2016-02-03 20:11:00 +01002534 case MTD_OPS_AUTO_OOB:
2535 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2536 ops->ooboffs, len);
2537 BUG_ON(ret);
2538 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002539
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002540 default:
2541 BUG();
2542 }
2543 return NULL;
2544}
2545
Florian Fainellif8ac0412010-09-07 13:23:43 +02002546#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002547
2548/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002549 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002550 * @mtd: MTD device structure
2551 * @to: offset to write to
2552 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002553 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002554 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002555 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002556static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2557 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002558{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002559 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002560 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002561 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002562
2563 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002564 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002565
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002566 uint8_t *oob = ops->oobbuf;
2567 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302568 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002569 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002570
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002571 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002572 if (!writelen)
2573 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002574
Brian Norris8b6e50c2011-05-25 14:59:01 -07002575 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002576 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002577 pr_notice("%s: attempt to write non page aligned data\n",
2578 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002579 return -EINVAL;
2580 }
2581
Thomas Gleixner29072b92006-09-28 15:38:36 +02002582 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002583
Thomas Gleixner6a930962006-06-28 00:11:45 +02002584 chipnr = (int)(to >> chip->chip_shift);
2585 chip->select_chip(mtd, chipnr);
2586
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002587 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002588 if (nand_check_wp(mtd)) {
2589 ret = -EIO;
2590 goto err_out;
2591 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002592
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002593 realpage = (int)(to >> chip->page_shift);
2594 page = realpage & chip->pagemask;
2595 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2596
2597 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002598 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2599 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002600 chip->pagebuf = -1;
2601
Maxim Levitsky782ce792010-02-22 20:39:36 +02002602 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002603 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2604 ret = -EINVAL;
2605 goto err_out;
2606 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002607
Florian Fainellif8ac0412010-09-07 13:23:43 +02002608 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002609 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002610 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002611 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002612 int use_bufpoi;
2613 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002614
Kamal Dasu66507c72014-05-01 20:51:19 -04002615 if (part_pagewr)
2616 use_bufpoi = 1;
2617 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2618 use_bufpoi = !virt_addr_valid(buf);
2619 else
2620 use_bufpoi = 0;
2621
2622 /* Partial page write?, or need to use bounce buffer */
2623 if (use_bufpoi) {
2624 pr_debug("%s: using write bounce buffer for buf@%p\n",
2625 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002626 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002627 if (part_pagewr)
2628 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002629 chip->pagebuf = -1;
2630 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2631 memcpy(&chip->buffers->databuf[column], buf, bytes);
2632 wbuf = chip->buffers->databuf;
2633 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002634
Maxim Levitsky782ce792010-02-22 20:39:36 +02002635 if (unlikely(oob)) {
2636 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002637 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002638 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002639 } else {
2640 /* We still need to erase leftover OOB data */
2641 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002642 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302643 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2644 oob_required, page, cached,
2645 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002646 if (ret)
2647 break;
2648
2649 writelen -= bytes;
2650 if (!writelen)
2651 break;
2652
Thomas Gleixner29072b92006-09-28 15:38:36 +02002653 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002654 buf += bytes;
2655 realpage++;
2656
2657 page = realpage & chip->pagemask;
2658 /* Check, if we cross a chip boundary */
2659 if (!page) {
2660 chipnr++;
2661 chip->select_chip(mtd, -1);
2662 chip->select_chip(mtd, chipnr);
2663 }
2664 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002665
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002666 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002667 if (unlikely(oob))
2668 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002669
2670err_out:
2671 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002672 return ret;
2673}
2674
2675/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002676 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002677 * @mtd: MTD device structure
2678 * @to: offset to write to
2679 * @len: number of bytes to write
2680 * @retlen: pointer to variable to store the number of written bytes
2681 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002682 *
2683 * NAND write with ECC. Used when performing writes in interrupt context, this
2684 * may for example be called by mtdoops when writing an oops while in panic.
2685 */
2686static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2687 size_t *retlen, const uint8_t *buf)
2688{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002689 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002690 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002691 int ret;
2692
Brian Norris8b6e50c2011-05-25 14:59:01 -07002693 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002694 panic_nand_wait(mtd, chip, 400);
2695
Brian Norris8b6e50c2011-05-25 14:59:01 -07002696 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002697 panic_nand_get_device(chip, mtd, FL_WRITING);
2698
Brian Norris0ec56dc2015-02-28 02:02:30 -08002699 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002700 ops.len = len;
2701 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002702 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002703
Brian Norris4a89ff82011-08-30 18:45:45 -07002704 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002705
Brian Norris4a89ff82011-08-30 18:45:45 -07002706 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002707 return ret;
2708}
2709
2710/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002711 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002712 * @mtd: MTD device structure
2713 * @to: offset to write to
2714 * @len: number of bytes to write
2715 * @retlen: pointer to variable to store the number of written bytes
2716 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002718 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002720static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002721 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Brian Norris4a89ff82011-08-30 18:45:45 -07002723 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002724 int ret;
2725
Huang Shijie6a8214a2012-11-19 14:43:30 +08002726 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002727 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002728 ops.len = len;
2729 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002730 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002731 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002732 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002733 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002734 return ret;
2735}
2736
2737/**
2738 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002739 * @mtd: MTD device structure
2740 * @to: offset to write to
2741 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002742 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002743 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002744 */
2745static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2746 struct mtd_oob_ops *ops)
2747{
Adrian Hunter03736152007-01-31 17:58:29 +02002748 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002749 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Brian Norris289c0522011-07-19 10:06:09 -07002751 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302752 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Boris BREZILLON29f10582016-03-07 10:46:52 +01002754 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002757 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002758 pr_debug("%s: attempt to write past end of page\n",
2759 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return -EINVAL;
2761 }
2762
Adrian Hunter03736152007-01-31 17:58:29 +02002763 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002764 pr_debug("%s: attempt to start write outside oob\n",
2765 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002766 return -EINVAL;
2767 }
2768
Jason Liu775adc32011-02-25 13:06:18 +08002769 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002770 if (unlikely(to >= mtd->size ||
2771 ops->ooboffs + ops->ooblen >
2772 ((mtd->size >> chip->page_shift) -
2773 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002774 pr_debug("%s: attempt to write beyond end of device\n",
2775 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002776 return -EINVAL;
2777 }
2778
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002779 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002780 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002782 /* Shift to get page */
2783 page = (int)(to >> chip->page_shift);
2784
2785 /*
2786 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2787 * of my DiskOnChip 2000 test units) will clear the whole data page too
2788 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2789 * it in the doc2000 driver in August 1999. dwmw2.
2790 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002791 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002794 if (nand_check_wp(mtd)) {
2795 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002796 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002797 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002800 if (page == chip->pagebuf)
2801 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002803 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002804
Brian Norris0612b9d2011-08-30 18:45:40 -07002805 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002806 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2807 else
2808 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002809
Huang Shijieb0bb6902012-11-19 14:43:29 +08002810 chip->select_chip(mtd, -1);
2811
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002812 if (status)
2813 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Vitaly Wool70145682006-11-03 18:20:38 +03002815 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002817 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002818}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002820/**
2821 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002822 * @mtd: MTD device structure
2823 * @to: offset to write to
2824 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002825 */
2826static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2827 struct mtd_oob_ops *ops)
2828{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002829 int ret = -ENOTSUPP;
2830
2831 ops->retlen = 0;
2832
2833 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002834 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002835 pr_debug("%s: attempt to write beyond end of device\n",
2836 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002837 return -EINVAL;
2838 }
2839
Huang Shijie6a8214a2012-11-19 14:43:30 +08002840 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002841
Florian Fainellif8ac0412010-09-07 13:23:43 +02002842 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002843 case MTD_OPS_PLACE_OOB:
2844 case MTD_OPS_AUTO_OOB:
2845 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002846 break;
2847
2848 default:
2849 goto out;
2850 }
2851
2852 if (!ops->datbuf)
2853 ret = nand_do_write_oob(mtd, to, ops);
2854 else
2855 ret = nand_do_write_ops(mtd, to, ops);
2856
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002857out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002858 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return ret;
2860}
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862/**
Brian Norris49c50b92014-05-06 16:02:19 -07002863 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002864 * @mtd: MTD device structure
2865 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 *
Brian Norris49c50b92014-05-06 16:02:19 -07002867 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 */
Brian Norris49c50b92014-05-06 16:02:19 -07002869static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002871 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002873 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2874 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002875
2876 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
2879/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002881 * @mtd: MTD device structure
2882 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002884 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002886static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
David Woodhousee0c7d762006-05-13 18:07:53 +01002888 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002892 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002893 * @mtd: MTD device structure
2894 * @instr: erase instruction
2895 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002897 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002899int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2900 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Adrian Hunter69423d92008-12-10 13:37:21 +00002902 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002903 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00002904 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Brian Norris289c0522011-07-19 10:06:09 -07002906 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2907 __func__, (unsigned long long)instr->addr,
2908 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302910 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002914 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002917 page = (int)(instr->addr >> chip->page_shift);
2918 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002921 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002924 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 /* Check, if it is write protected */
2927 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002928 pr_debug("%s: device is write protected!\n",
2929 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 instr->state = MTD_ERASE_FAILED;
2931 goto erase_exit;
2932 }
2933
2934 /* Loop through the pages */
2935 len = instr->len;
2936
2937 instr->state = MTD_ERASING;
2938
2939 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002940 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002941 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05302942 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002943 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2944 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 instr->state = MTD_ERASE_FAILED;
2946 goto erase_exit;
2947 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002948
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002949 /*
2950 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002951 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002952 */
2953 if (page <= chip->pagebuf && chip->pagebuf <
2954 (page + pages_per_block))
2955 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Brian Norris49c50b92014-05-06 16:02:19 -07002957 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002959 /*
2960 * See if operation failed and additional status checks are
2961 * available
2962 */
2963 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2964 status = chip->errstat(mtd, chip, FL_ERASING,
2965 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002968 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002969 pr_debug("%s: failed erase, page 0x%08x\n",
2970 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002972 instr->fail_addr =
2973 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 goto erase_exit;
2975 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002978 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 page += pages_per_block;
2980
2981 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002982 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002984 chip->select_chip(mtd, -1);
2985 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987 }
2988 instr->state = MTD_ERASE_DONE;
2989
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002990erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002995 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 nand_release_device(mtd);
2997
David Woodhouse49defc02007-10-06 15:01:59 -04002998 /* Do call back function */
2999 if (!ret)
3000 mtd_erase_callback(instr);
3001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 /* Return more or less happy */
3003 return ret;
3004}
3005
3006/**
3007 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003008 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003010 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003012static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Brian Norris289c0522011-07-19 10:06:09 -07003014 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003017 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003019 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003023 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003024 * @mtd: MTD device structure
3025 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003027static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303029 struct nand_chip *chip = mtd_to_nand(mtd);
3030 int chipnr = (int)(offs >> chip->chip_shift);
3031 int ret;
3032
3033 /* Select the NAND device */
3034 nand_get_device(mtd, FL_READING);
3035 chip->select_chip(mtd, chipnr);
3036
3037 ret = nand_block_checkbad(mtd, offs, 0);
3038
3039 chip->select_chip(mtd, -1);
3040 nand_release_device(mtd);
3041
3042 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
3045/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003046 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003047 * @mtd: MTD device structure
3048 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003050static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 int ret;
3053
Florian Fainellif8ac0412010-09-07 13:23:43 +02003054 ret = nand_block_isbad(mtd, ofs);
3055 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003056 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (ret > 0)
3058 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003059 return ret;
3060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Brian Norris5a0edb22013-07-30 17:52:58 -07003062 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
3065/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003066 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3067 * @mtd: MTD device structure
3068 * @chip: nand chip info structure
3069 * @addr: feature address.
3070 * @subfeature_param: the subfeature parameters, a four bytes array.
3071 */
3072static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3073 int addr, uint8_t *subfeature_param)
3074{
3075 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003076 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003077
David Mosbergerd914c932013-05-29 15:30:13 +03003078 if (!chip->onfi_version ||
3079 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3080 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003081 return -EINVAL;
3082
3083 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003084 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3085 chip->write_byte(mtd, subfeature_param[i]);
3086
Huang Shijie7db03ec2012-09-13 14:57:52 +08003087 status = chip->waitfunc(mtd, chip);
3088 if (status & NAND_STATUS_FAIL)
3089 return -EIO;
3090 return 0;
3091}
3092
3093/**
3094 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3095 * @mtd: MTD device structure
3096 * @chip: nand chip info structure
3097 * @addr: feature address.
3098 * @subfeature_param: the subfeature parameters, a four bytes array.
3099 */
3100static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3101 int addr, uint8_t *subfeature_param)
3102{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003103 int i;
3104
David Mosbergerd914c932013-05-29 15:30:13 +03003105 if (!chip->onfi_version ||
3106 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3107 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003108 return -EINVAL;
3109
Huang Shijie7db03ec2012-09-13 14:57:52 +08003110 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003111 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3112 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003113 return 0;
3114}
3115
3116/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003117 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003118 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003119 */
3120static int nand_suspend(struct mtd_info *mtd)
3121{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003122 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003123}
3124
3125/**
3126 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003127 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003128 */
3129static void nand_resume(struct mtd_info *mtd)
3130{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003131 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003132
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003133 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003134 nand_release_device(mtd);
3135 else
Brian Norrisd0370212011-07-19 10:06:08 -07003136 pr_err("%s called for a chip which is not in suspended state\n",
3137 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003138}
3139
Scott Branden72ea4032014-11-20 11:18:05 -08003140/**
3141 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3142 * prevent further operations
3143 * @mtd: MTD device structure
3144 */
3145static void nand_shutdown(struct mtd_info *mtd)
3146{
Brian Norris9ca641b2015-11-09 16:37:28 -08003147 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003148}
3149
Brian Norris8b6e50c2011-05-25 14:59:01 -07003150/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003151static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003152{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003154 if (!chip->chip_delay)
3155 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003158 if (chip->cmdfunc == NULL)
3159 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003162 if (chip->waitfunc == NULL)
3163 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003165 if (!chip->select_chip)
3166 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003167
Huang Shijie4204ccc2013-08-16 10:10:07 +08003168 /* set for ONFI nand */
3169 if (!chip->onfi_set_features)
3170 chip->onfi_set_features = nand_onfi_set_features;
3171 if (!chip->onfi_get_features)
3172 chip->onfi_get_features = nand_onfi_get_features;
3173
Brian Norris68e80782013-07-18 01:17:02 -07003174 /* If called twice, pointers that depend on busw may need to be reset */
3175 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003176 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3177 if (!chip->read_word)
3178 chip->read_word = nand_read_word;
3179 if (!chip->block_bad)
3180 chip->block_bad = nand_block_bad;
3181 if (!chip->block_markbad)
3182 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003183 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003184 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003185 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3186 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003187 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003188 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003189 if (!chip->scan_bbt)
3190 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003191
3192 if (!chip->controller) {
3193 chip->controller = &chip->hwcontrol;
3194 spin_lock_init(&chip->controller->lock);
3195 init_waitqueue_head(&chip->controller->wq);
3196 }
3197
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003198}
3199
Brian Norris8b6e50c2011-05-25 14:59:01 -07003200/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003201static void sanitize_string(uint8_t *s, size_t len)
3202{
3203 ssize_t i;
3204
Brian Norris8b6e50c2011-05-25 14:59:01 -07003205 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003206 s[len - 1] = 0;
3207
Brian Norris8b6e50c2011-05-25 14:59:01 -07003208 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003209 for (i = 0; i < len - 1; i++) {
3210 if (s[i] < ' ' || s[i] > 127)
3211 s[i] = '?';
3212 }
3213
Brian Norris8b6e50c2011-05-25 14:59:01 -07003214 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003215 strim(s);
3216}
3217
3218static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3219{
3220 int i;
3221 while (len--) {
3222 crc ^= *p++ << 8;
3223 for (i = 0; i < 8; i++)
3224 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3225 }
3226
3227 return crc;
3228}
3229
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003230/* Parse the Extended Parameter Page. */
3231static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3232 struct nand_chip *chip, struct nand_onfi_params *p)
3233{
3234 struct onfi_ext_param_page *ep;
3235 struct onfi_ext_section *s;
3236 struct onfi_ext_ecc_info *ecc;
3237 uint8_t *cursor;
3238 int ret = -EINVAL;
3239 int len;
3240 int i;
3241
3242 len = le16_to_cpu(p->ext_param_page_length) * 16;
3243 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003244 if (!ep)
3245 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003246
3247 /* Send our own NAND_CMD_PARAM. */
3248 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3249
3250 /* Use the Change Read Column command to skip the ONFI param pages. */
3251 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3252 sizeof(*p) * p->num_of_param_pages , -1);
3253
3254 /* Read out the Extended Parameter Page. */
3255 chip->read_buf(mtd, (uint8_t *)ep, len);
3256 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3257 != le16_to_cpu(ep->crc))) {
3258 pr_debug("fail in the CRC.\n");
3259 goto ext_out;
3260 }
3261
3262 /*
3263 * Check the signature.
3264 * Do not strictly follow the ONFI spec, maybe changed in future.
3265 */
3266 if (strncmp(ep->sig, "EPPS", 4)) {
3267 pr_debug("The signature is invalid.\n");
3268 goto ext_out;
3269 }
3270
3271 /* find the ECC section. */
3272 cursor = (uint8_t *)(ep + 1);
3273 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3274 s = ep->sections + i;
3275 if (s->type == ONFI_SECTION_TYPE_2)
3276 break;
3277 cursor += s->length * 16;
3278 }
3279 if (i == ONFI_EXT_SECTION_MAX) {
3280 pr_debug("We can not find the ECC section.\n");
3281 goto ext_out;
3282 }
3283
3284 /* get the info we want. */
3285 ecc = (struct onfi_ext_ecc_info *)cursor;
3286
Brian Norris4ae7d222013-09-16 18:20:21 -07003287 if (!ecc->codeword_size) {
3288 pr_debug("Invalid codeword size\n");
3289 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003290 }
3291
Brian Norris4ae7d222013-09-16 18:20:21 -07003292 chip->ecc_strength_ds = ecc->ecc_bits;
3293 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003294 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003295
3296ext_out:
3297 kfree(ep);
3298 return ret;
3299}
3300
Brian Norris8429bb32013-12-03 15:51:09 -08003301static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3302{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003303 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003304 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3305
3306 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3307 feature);
3308}
3309
3310/*
3311 * Configure chip properties from Micron vendor-specific ONFI table
3312 */
3313static void nand_onfi_detect_micron(struct nand_chip *chip,
3314 struct nand_onfi_params *p)
3315{
3316 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3317
3318 if (le16_to_cpu(p->vendor_revision) < 1)
3319 return;
3320
3321 chip->read_retries = micron->read_retry_options;
3322 chip->setup_read_retry = nand_setup_read_retry_micron;
3323}
3324
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003325/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003326 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003327 */
3328static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003329 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003330{
3331 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003332 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003333 int val;
3334
Brian Norris7854d3f2011-06-23 14:12:08 -07003335 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003336 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3337 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3338 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3339 return 0;
3340
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003341 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3342 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003343 for (j = 0; j < sizeof(*p); j++)
3344 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003345 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3346 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003347 break;
3348 }
3349 }
3350
Brian Norrisc7f23a72013-08-13 10:51:55 -07003351 if (i == 3) {
3352 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003353 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003354 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003355
Brian Norris8b6e50c2011-05-25 14:59:01 -07003356 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003357 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003358 if (val & (1 << 5))
3359 chip->onfi_version = 23;
3360 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003361 chip->onfi_version = 22;
3362 else if (val & (1 << 3))
3363 chip->onfi_version = 21;
3364 else if (val & (1 << 2))
3365 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003366 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003367 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003368
3369 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003370 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003371 return 0;
3372 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003373
3374 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3375 sanitize_string(p->model, sizeof(p->model));
3376 if (!mtd->name)
3377 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003378
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003379 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003380
3381 /*
3382 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3383 * (don't ask me who thought of this...). MTD assumes that these
3384 * dimensions will be power-of-2, so just truncate the remaining area.
3385 */
3386 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3387 mtd->erasesize *= mtd->writesize;
3388
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003389 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003390
3391 /* See erasesize comment */
3392 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003393 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003394 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003395
3396 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003397 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003398 else
3399 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003400
Huang Shijie10c86ba2013-05-17 11:17:26 +08003401 if (p->ecc_bits != 0xff) {
3402 chip->ecc_strength_ds = p->ecc_bits;
3403 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003404 } else if (chip->onfi_version >= 21 &&
3405 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3406
3407 /*
3408 * The nand_flash_detect_ext_param_page() uses the
3409 * Change Read Column command which maybe not supported
3410 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3411 * now. We do not replace user supplied command function.
3412 */
3413 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3414 chip->cmdfunc = nand_command_lp;
3415
3416 /* The Extended Parameter Page is supported since ONFI 2.1. */
3417 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003418 pr_warn("Failed to detect ONFI extended param page\n");
3419 } else {
3420 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003421 }
3422
Brian Norris8429bb32013-12-03 15:51:09 -08003423 if (p->jedec_id == NAND_MFR_MICRON)
3424 nand_onfi_detect_micron(chip, p);
3425
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003426 return 1;
3427}
3428
3429/*
Huang Shijie91361812014-02-21 13:39:40 +08003430 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3431 */
3432static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3433 int *busw)
3434{
3435 struct nand_jedec_params *p = &chip->jedec_params;
3436 struct jedec_ecc_info *ecc;
3437 int val;
3438 int i, j;
3439
3440 /* Try JEDEC for unknown chip or LP */
3441 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3442 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3443 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3444 chip->read_byte(mtd) != 'C')
3445 return 0;
3446
3447 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3448 for (i = 0; i < 3; i++) {
3449 for (j = 0; j < sizeof(*p); j++)
3450 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3451
3452 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3453 le16_to_cpu(p->crc))
3454 break;
3455 }
3456
3457 if (i == 3) {
3458 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3459 return 0;
3460 }
3461
3462 /* Check version */
3463 val = le16_to_cpu(p->revision);
3464 if (val & (1 << 2))
3465 chip->jedec_version = 10;
3466 else if (val & (1 << 1))
3467 chip->jedec_version = 1; /* vendor specific version */
3468
3469 if (!chip->jedec_version) {
3470 pr_info("unsupported JEDEC version: %d\n", val);
3471 return 0;
3472 }
3473
3474 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3475 sanitize_string(p->model, sizeof(p->model));
3476 if (!mtd->name)
3477 mtd->name = p->model;
3478
3479 mtd->writesize = le32_to_cpu(p->byte_per_page);
3480
3481 /* Please reference to the comment for nand_flash_detect_onfi. */
3482 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3483 mtd->erasesize *= mtd->writesize;
3484
3485 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3486
3487 /* Please reference to the comment for nand_flash_detect_onfi. */
3488 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3489 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3490 chip->bits_per_cell = p->bits_per_cell;
3491
3492 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3493 *busw = NAND_BUSWIDTH_16;
3494 else
3495 *busw = 0;
3496
3497 /* ECC info */
3498 ecc = &p->ecc_info[0];
3499
3500 if (ecc->codeword_size >= 9) {
3501 chip->ecc_strength_ds = ecc->ecc_bits;
3502 chip->ecc_step_ds = 1 << ecc->codeword_size;
3503 } else {
3504 pr_warn("Invalid codeword size\n");
3505 }
3506
3507 return 1;
3508}
3509
3510/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003511 * nand_id_has_period - Check if an ID string has a given wraparound period
3512 * @id_data: the ID string
3513 * @arrlen: the length of the @id_data array
3514 * @period: the period of repitition
3515 *
3516 * Check if an ID string is repeated within a given sequence of bytes at
3517 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003518 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003519 * if the repetition has a period of @period; otherwise, returns zero.
3520 */
3521static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3522{
3523 int i, j;
3524 for (i = 0; i < period; i++)
3525 for (j = i + period; j < arrlen; j += period)
3526 if (id_data[i] != id_data[j])
3527 return 0;
3528 return 1;
3529}
3530
3531/*
3532 * nand_id_len - Get the length of an ID string returned by CMD_READID
3533 * @id_data: the ID string
3534 * @arrlen: the length of the @id_data array
3535
3536 * Returns the length of the ID string, according to known wraparound/trailing
3537 * zero patterns. If no pattern exists, returns the length of the array.
3538 */
3539static int nand_id_len(u8 *id_data, int arrlen)
3540{
3541 int last_nonzero, period;
3542
3543 /* Find last non-zero byte */
3544 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3545 if (id_data[last_nonzero])
3546 break;
3547
3548 /* All zeros */
3549 if (last_nonzero < 0)
3550 return 0;
3551
3552 /* Calculate wraparound period */
3553 for (period = 1; period < arrlen; period++)
3554 if (nand_id_has_period(id_data, arrlen, period))
3555 break;
3556
3557 /* There's a repeated pattern */
3558 if (period < arrlen)
3559 return period;
3560
3561 /* There are trailing zeros */
3562 if (last_nonzero < arrlen - 1)
3563 return last_nonzero + 1;
3564
3565 /* No pattern detected */
3566 return arrlen;
3567}
3568
Huang Shijie7db906b2013-09-25 14:58:11 +08003569/* Extract the bits of per cell from the 3rd byte of the extended ID */
3570static int nand_get_bits_per_cell(u8 cellinfo)
3571{
3572 int bits;
3573
3574 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3575 bits >>= NAND_CI_CELLTYPE_SHIFT;
3576 return bits + 1;
3577}
3578
Brian Norrise3b88bd2012-09-24 20:40:52 -07003579/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003580 * Many new NAND share similar device ID codes, which represent the size of the
3581 * chip. The rest of the parameters must be decoded according to generic or
3582 * manufacturer-specific "extended ID" decoding patterns.
3583 */
3584static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3585 u8 id_data[8], int *busw)
3586{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003587 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003588 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003589 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003590 /* The 4th id byte is the important one */
3591 extid = id_data[3];
3592
Brian Norrise3b88bd2012-09-24 20:40:52 -07003593 id_len = nand_id_len(id_data, 8);
3594
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003595 /*
3596 * Field definitions are in the following datasheets:
3597 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003598 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003599 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003600 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003601 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3602 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003603 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003604 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003605 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003606 /* Calc pagesize */
3607 mtd->writesize = 2048 << (extid & 0x03);
3608 extid >>= 2;
3609 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003610 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003611 case 1:
3612 mtd->oobsize = 128;
3613 break;
3614 case 2:
3615 mtd->oobsize = 218;
3616 break;
3617 case 3:
3618 mtd->oobsize = 400;
3619 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003620 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003621 mtd->oobsize = 436;
3622 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003623 case 5:
3624 mtd->oobsize = 512;
3625 break;
3626 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003627 mtd->oobsize = 640;
3628 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003629 case 7:
3630 default: /* Other cases are "reserved" (unknown) */
3631 mtd->oobsize = 1024;
3632 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003633 }
3634 extid >>= 2;
3635 /* Calc blocksize */
3636 mtd->erasesize = (128 * 1024) <<
3637 (((extid >> 1) & 0x04) | (extid & 0x03));
3638 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003639 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003640 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003641 unsigned int tmp;
3642
3643 /* Calc pagesize */
3644 mtd->writesize = 2048 << (extid & 0x03);
3645 extid >>= 2;
3646 /* Calc oobsize */
3647 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3648 case 0:
3649 mtd->oobsize = 128;
3650 break;
3651 case 1:
3652 mtd->oobsize = 224;
3653 break;
3654 case 2:
3655 mtd->oobsize = 448;
3656 break;
3657 case 3:
3658 mtd->oobsize = 64;
3659 break;
3660 case 4:
3661 mtd->oobsize = 32;
3662 break;
3663 case 5:
3664 mtd->oobsize = 16;
3665 break;
3666 default:
3667 mtd->oobsize = 640;
3668 break;
3669 }
3670 extid >>= 2;
3671 /* Calc blocksize */
3672 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3673 if (tmp < 0x03)
3674 mtd->erasesize = (128 * 1024) << tmp;
3675 else if (tmp == 0x03)
3676 mtd->erasesize = 768 * 1024;
3677 else
3678 mtd->erasesize = (64 * 1024) << tmp;
3679 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003680 } else {
3681 /* Calc pagesize */
3682 mtd->writesize = 1024 << (extid & 0x03);
3683 extid >>= 2;
3684 /* Calc oobsize */
3685 mtd->oobsize = (8 << (extid & 0x01)) *
3686 (mtd->writesize >> 9);
3687 extid >>= 2;
3688 /* Calc blocksize. Blocksize is multiples of 64KiB */
3689 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3690 extid >>= 2;
3691 /* Get buswidth information */
3692 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003693
3694 /*
3695 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3696 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3697 * follows:
3698 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3699 * 110b -> 24nm
3700 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3701 */
3702 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003703 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003704 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3705 !(id_data[4] & 0x80) /* !BENAND */) {
3706 mtd->oobsize = 32 * mtd->writesize >> 9;
3707 }
3708
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003709 }
3710}
3711
3712/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003713 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3714 * decodes a matching ID table entry and assigns the MTD size parameters for
3715 * the chip.
3716 */
3717static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3718 struct nand_flash_dev *type, u8 id_data[8],
3719 int *busw)
3720{
3721 int maf_id = id_data[0];
3722
3723 mtd->erasesize = type->erasesize;
3724 mtd->writesize = type->pagesize;
3725 mtd->oobsize = mtd->writesize / 32;
3726 *busw = type->options & NAND_BUSWIDTH_16;
3727
Huang Shijie1c195e92013-09-25 14:58:12 +08003728 /* All legacy ID NAND are small-page, SLC */
3729 chip->bits_per_cell = 1;
3730
Brian Norrisf23a4812012-09-24 20:40:51 -07003731 /*
3732 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3733 * some Spansion chips have erasesize that conflicts with size
3734 * listed in nand_ids table.
3735 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3736 */
3737 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3738 && id_data[6] == 0x00 && id_data[7] == 0x00
3739 && mtd->writesize == 512) {
3740 mtd->erasesize = 128 * 1024;
3741 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3742 }
3743}
3744
3745/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003746 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3747 * heuristic patterns using various detected parameters (e.g., manufacturer,
3748 * page size, cell-type information).
3749 */
3750static void nand_decode_bbm_options(struct mtd_info *mtd,
3751 struct nand_chip *chip, u8 id_data[8])
3752{
3753 int maf_id = id_data[0];
3754
3755 /* Set the bad block position */
3756 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3757 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3758 else
3759 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3760
3761 /*
3762 * Bad block marker is stored in the last page of each block on Samsung
3763 * and Hynix MLC devices; stored in first two pages of each block on
3764 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3765 * AMD/Spansion, and Macronix. All others scan only the first page.
3766 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003767 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003768 (maf_id == NAND_MFR_SAMSUNG ||
3769 maf_id == NAND_MFR_HYNIX))
3770 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003771 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003772 (maf_id == NAND_MFR_SAMSUNG ||
3773 maf_id == NAND_MFR_HYNIX ||
3774 maf_id == NAND_MFR_TOSHIBA ||
3775 maf_id == NAND_MFR_AMD ||
3776 maf_id == NAND_MFR_MACRONIX)) ||
3777 (mtd->writesize == 2048 &&
3778 maf_id == NAND_MFR_MICRON))
3779 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3780}
3781
Huang Shijieec6e87e2013-03-15 11:01:00 +08003782static inline bool is_full_id_nand(struct nand_flash_dev *type)
3783{
3784 return type->id_len;
3785}
3786
3787static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3788 struct nand_flash_dev *type, u8 *id_data, int *busw)
3789{
3790 if (!strncmp(type->id, id_data, type->id_len)) {
3791 mtd->writesize = type->pagesize;
3792 mtd->erasesize = type->erasesize;
3793 mtd->oobsize = type->oobsize;
3794
Huang Shijie7db906b2013-09-25 14:58:11 +08003795 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003796 chip->chipsize = (uint64_t)type->chipsize << 20;
3797 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003798 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3799 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003800 chip->onfi_timing_mode_default =
3801 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003802
3803 *busw = type->options & NAND_BUSWIDTH_16;
3804
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003805 if (!mtd->name)
3806 mtd->name = type->name;
3807
Huang Shijieec6e87e2013-03-15 11:01:00 +08003808 return true;
3809 }
3810 return false;
3811}
3812
Brian Norris7e74c2d2012-09-24 20:40:49 -07003813/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003814 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003815 */
3816static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003817 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003818 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003819 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003820{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003821 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003822 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003823 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
3825 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003826 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Karl Beldanef89a882008-09-15 14:37:29 +02003828 /*
3829 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003830 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003831 */
3832 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003835 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
3837 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003838 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003839 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Brian Norris8b6e50c2011-05-25 14:59:01 -07003841 /*
3842 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003843 * interface concerns can cause random data which looks like a
3844 * possibly credible NAND flash to appear. If the two results do
3845 * not match, ignore the device completely.
3846 */
3847
3848 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3849
Brian Norris4aef9b72012-09-24 20:40:48 -07003850 /* Read entire ID string */
3851 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003852 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003853
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003854 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003855 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003856 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003857 return ERR_PTR(-ENODEV);
3858 }
3859
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003860 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003861 type = nand_flash_ids;
3862
Huang Shijieec6e87e2013-03-15 11:01:00 +08003863 for (; type->name != NULL; type++) {
3864 if (is_full_id_nand(type)) {
3865 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3866 goto ident_done;
3867 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003868 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003869 }
3870 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003871
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003872 chip->onfi_version = 0;
3873 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003874 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003875 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003876 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003877
3878 /* Check if the chip is JEDEC compliant */
3879 if (nand_flash_detect_jedec(mtd, chip, &busw))
3880 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003881 }
3882
David Woodhouse5e81e882010-02-26 18:32:56 +00003883 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003884 return ERR_PTR(-ENODEV);
3885
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003886 if (!mtd->name)
3887 mtd->name = type->name;
3888
Adrian Hunter69423d92008-12-10 13:37:21 +00003889 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003890
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02003891 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003892 /* Decode parameters from extended ID */
3893 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003894 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003895 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003896 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003897 /* Get chip options */
3898 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003899
Brian Norris8b6e50c2011-05-25 14:59:01 -07003900 /*
3901 * Check if chip is not a Samsung device. Do not clear the
3902 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003903 */
3904 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3905 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3906ident_done:
3907
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003908 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003909 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003910 if (nand_manuf_ids[maf_idx].id == *maf_id)
3911 break;
3912 }
3913
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003914 if (chip->options & NAND_BUSWIDTH_AUTO) {
3915 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3916 chip->options |= busw;
3917 nand_set_defaults(chip, busw);
3918 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3919 /*
3920 * Check, if buswidth is correct. Hardware drivers should set
3921 * chip correct!
3922 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003923 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3924 *maf_id, *dev_id);
3925 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3926 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003927 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3928 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003929 return ERR_PTR(-EINVAL);
3930 }
3931
Brian Norris7e74c2d2012-09-24 20:40:49 -07003932 nand_decode_bbm_options(mtd, chip, id_data);
3933
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003934 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003935 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003936 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003937 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003938
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003939 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003940 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003941 if (chip->chipsize & 0xffffffff)
3942 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003943 else {
3944 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3945 chip->chip_shift += 32 - 1;
3946 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003947
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003948 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003949 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003950
Brian Norris8b6e50c2011-05-25 14:59:01 -07003951 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003952 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3953 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003954
Ezequiel Garcia20171642013-11-25 08:30:31 -03003955 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3956 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003957
3958 if (chip->onfi_version)
3959 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3960 chip->onfi_params.model);
3961 else if (chip->jedec_version)
3962 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3963 chip->jedec_params.model);
3964 else
3965 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3966 type->name);
3967
Rafał Miłecki3755a992014-10-21 00:01:04 +02003968 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003969 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003970 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003971 return type;
3972}
3973
Boris Brezillond48f62b2016-04-01 14:54:32 +02003974static const char * const nand_ecc_modes[] = {
3975 [NAND_ECC_NONE] = "none",
3976 [NAND_ECC_SOFT] = "soft",
3977 [NAND_ECC_HW] = "hw",
3978 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
3979 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02003980};
3981
3982static int of_get_nand_ecc_mode(struct device_node *np)
3983{
3984 const char *pm;
3985 int err, i;
3986
3987 err = of_property_read_string(np, "nand-ecc-mode", &pm);
3988 if (err < 0)
3989 return err;
3990
3991 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
3992 if (!strcasecmp(pm, nand_ecc_modes[i]))
3993 return i;
3994
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02003995 /*
3996 * For backward compatibility we support few obsoleted values that don't
3997 * have their mappings into nand_ecc_modes_t anymore (they were merged
3998 * with other enums).
3999 */
4000 if (!strcasecmp(pm, "soft_bch"))
4001 return NAND_ECC_SOFT;
4002
Boris Brezillond48f62b2016-04-01 14:54:32 +02004003 return -ENODEV;
4004}
4005
4006static int of_get_nand_ecc_algo(struct device_node *np)
4007{
4008 const char *pm;
4009 int err;
4010
4011 /*
4012 * TODO: Read ECC algo OF property and map it to enum nand_ecc_algo.
4013 * It's not implemented yet as currently NAND subsystem ignores
4014 * algorithm explicitly set this way. Once it's handled we should
4015 * document & support new property.
4016 */
4017
4018 /*
4019 * For backward compatibility we also read "nand-ecc-mode" checking
4020 * for some obsoleted values that were specifying ECC algorithm.
4021 */
4022 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4023 if (err < 0)
4024 return err;
4025
4026 if (!strcasecmp(pm, "soft"))
4027 return NAND_ECC_HAMMING;
4028 else if (!strcasecmp(pm, "soft_bch"))
4029 return NAND_ECC_BCH;
4030
4031 return -ENODEV;
4032}
4033
4034static int of_get_nand_ecc_step_size(struct device_node *np)
4035{
4036 int ret;
4037 u32 val;
4038
4039 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4040 return ret ? ret : val;
4041}
4042
4043static int of_get_nand_ecc_strength(struct device_node *np)
4044{
4045 int ret;
4046 u32 val;
4047
4048 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4049 return ret ? ret : val;
4050}
4051
4052static int of_get_nand_bus_width(struct device_node *np)
4053{
4054 u32 val;
4055
4056 if (of_property_read_u32(np, "nand-bus-width", &val))
4057 return 8;
4058
4059 switch (val) {
4060 case 8:
4061 case 16:
4062 return val;
4063 default:
4064 return -EIO;
4065 }
4066}
4067
4068static bool of_get_nand_on_flash_bbt(struct device_node *np)
4069{
4070 return of_property_read_bool(np, "nand-on-flash-bbt");
4071}
4072
Boris BREZILLON7194a292015-12-10 09:00:37 +01004073static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004074{
Boris BREZILLON7194a292015-12-10 09:00:37 +01004075 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004076 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004077
Boris BREZILLON7194a292015-12-10 09:00:37 +01004078 if (!dn)
4079 return 0;
4080
Brian Norris5844fee2015-01-23 00:22:27 -08004081 if (of_get_nand_bus_width(dn) == 16)
4082 chip->options |= NAND_BUSWIDTH_16;
4083
4084 if (of_get_nand_on_flash_bbt(dn))
4085 chip->bbt_options |= NAND_BBT_USE_FLASH;
4086
4087 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004088 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004089 ecc_strength = of_get_nand_ecc_strength(dn);
4090 ecc_step = of_get_nand_ecc_step_size(dn);
4091
4092 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4093 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4094 pr_err("must set both strength and step size in DT\n");
4095 return -EINVAL;
4096 }
4097
4098 if (ecc_mode >= 0)
4099 chip->ecc.mode = ecc_mode;
4100
Rafał Miłecki79082452016-03-23 11:19:02 +01004101 if (ecc_algo >= 0)
4102 chip->ecc.algo = ecc_algo;
4103
Brian Norris5844fee2015-01-23 00:22:27 -08004104 if (ecc_strength >= 0)
4105 chip->ecc.strength = ecc_strength;
4106
4107 if (ecc_step > 0)
4108 chip->ecc.size = ecc_step;
4109
4110 return 0;
4111}
4112
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004113/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004114 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004115 * @mtd: MTD device structure
4116 * @maxchips: number of chips to scan for
4117 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004118 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004119 * This is the first phase of the normal nand_scan() function. It reads the
4120 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004121 *
David Woodhouse3b85c322006-09-25 17:06:53 +01004122 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004123 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004124int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4125 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004126{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004127 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004128 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004129 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004130 int ret;
4131
Boris BREZILLON7194a292015-12-10 09:00:37 +01004132 ret = nand_dt_init(chip);
4133 if (ret)
4134 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004135
Brian Norrisf7a8e382016-01-05 10:39:45 -08004136 if (!mtd->name && mtd->dev.parent)
4137 mtd->name = dev_name(mtd->dev.parent);
4138
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004139 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004140 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004141
4142 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004143 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4144 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004145
4146 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004147 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004148 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004149 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004150 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 }
4152
Huang Shijie07300162012-11-09 16:23:45 +08004153 chip->select_chip(mtd, -1);
4154
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004155 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004156 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004157 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004158 /* See comment in nand_get_flash_type for reset */
4159 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004161 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004163 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004164 nand_dev_id != chip->read_byte(mtd)) {
4165 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 break;
Huang Shijie07300162012-11-09 16:23:45 +08004167 }
4168 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
4170 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004171 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004174 chip->numchips = i;
4175 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
David Woodhouse3b85c322006-09-25 17:06:53 +01004177 return 0;
4178}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004179EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004180
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004181static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4182{
4183 struct nand_chip *chip = mtd_to_nand(mtd);
4184 struct nand_ecc_ctrl *ecc = &chip->ecc;
4185
4186 if (WARN_ON(ecc->mode != NAND_ECC_SOFT &&
4187 ecc->mode != NAND_ECC_SOFT_BCH))
4188 return -EINVAL;
4189
4190 switch (ecc->algo) {
4191 case NAND_ECC_HAMMING:
4192 ecc->calculate = nand_calculate_ecc;
4193 ecc->correct = nand_correct_data;
4194 ecc->read_page = nand_read_page_swecc;
4195 ecc->read_subpage = nand_read_subpage;
4196 ecc->write_page = nand_write_page_swecc;
4197 ecc->read_page_raw = nand_read_page_raw;
4198 ecc->write_page_raw = nand_write_page_raw;
4199 ecc->read_oob = nand_read_oob_std;
4200 ecc->write_oob = nand_write_oob_std;
4201 if (!ecc->size)
4202 ecc->size = 256;
4203 ecc->bytes = 3;
4204 ecc->strength = 1;
4205 return 0;
4206 case NAND_ECC_BCH:
4207 if (!mtd_nand_has_bch()) {
4208 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4209 return -EINVAL;
4210 }
4211 ecc->calculate = nand_bch_calculate_ecc;
4212 ecc->correct = nand_bch_correct_data;
4213 ecc->read_page = nand_read_page_swecc;
4214 ecc->read_subpage = nand_read_subpage;
4215 ecc->write_page = nand_write_page_swecc;
4216 ecc->read_page_raw = nand_read_page_raw;
4217 ecc->write_page_raw = nand_write_page_raw;
4218 ecc->read_oob = nand_read_oob_std;
4219 ecc->write_oob = nand_write_oob_std;
4220 /*
4221 * Board driver should supply ecc.size and ecc.strength
4222 * values to select how many bits are correctable.
4223 * Otherwise, default to 4 bits for large page devices.
4224 */
4225 if (!ecc->size && (mtd->oobsize >= 64)) {
4226 ecc->size = 512;
4227 ecc->strength = 4;
4228 }
4229
4230 /*
4231 * if no ecc placement scheme was provided pickup the default
4232 * large page one.
4233 */
4234 if (!mtd->ooblayout) {
4235 /* handle large page devices only */
4236 if (mtd->oobsize < 64) {
4237 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4238 return -EINVAL;
4239 }
4240
4241 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
4242 }
4243
4244 /* See nand_bch_init() for details. */
4245 ecc->bytes = 0;
4246 ecc->priv = nand_bch_init(mtd);
4247 if (!ecc->priv) {
4248 WARN(1, "BCH ECC initialization failed!\n");
4249 return -EINVAL;
4250 }
4251 return 0;
4252 default:
4253 WARN(1, "Unsupported ECC algorithm!\n");
4254 return -EINVAL;
4255 }
4256}
4257
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004258/*
4259 * Check if the chip configuration meet the datasheet requirements.
4260
4261 * If our configuration corrects A bits per B bytes and the minimum
4262 * required correction level is X bits per Y bytes, then we must ensure
4263 * both of the following are true:
4264 *
4265 * (1) A / B >= X / Y
4266 * (2) A >= X
4267 *
4268 * Requirement (1) ensures we can correct for the required bitflip density.
4269 * Requirement (2) ensures we can correct even when all bitflips are clumped
4270 * in the same sector.
4271 */
4272static bool nand_ecc_strength_good(struct mtd_info *mtd)
4273{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004274 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004275 struct nand_ecc_ctrl *ecc = &chip->ecc;
4276 int corr, ds_corr;
4277
4278 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4279 /* Not enough information */
4280 return true;
4281
4282 /*
4283 * We get the number of corrected bits per page to compare
4284 * the correction density.
4285 */
4286 corr = (mtd->writesize * ecc->strength) / ecc->size;
4287 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4288
4289 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4290}
David Woodhouse3b85c322006-09-25 17:06:53 +01004291
4292/**
4293 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004294 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004295 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004296 * This is the second phase of the normal nand_scan() function. It fills out
4297 * all the uninitialized function pointers with the defaults and scans for a
4298 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004299 */
4300int nand_scan_tail(struct mtd_info *mtd)
4301{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004302 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004303 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004304 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004305 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004306
Brian Norrise2414f42012-02-06 13:44:00 -08004307 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004308 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4309 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4310 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004311
Huang Shijief02ea4e2014-01-13 14:27:12 +08004312 if (!(chip->options & NAND_OWN_BUFFERS)) {
4313 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4314 + mtd->oobsize * 3, GFP_KERNEL);
4315 if (!nbuf)
4316 return -ENOMEM;
4317 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4318 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4319 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4320
4321 chip->buffers = nbuf;
4322 } else {
4323 if (!chip->buffers)
4324 return -ENOMEM;
4325 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004326
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004327 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004328 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004329
4330 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004331 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004332 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004333 if (!mtd->ooblayout &&
4334 !((ecc->mode == NAND_ECC_SOFT || ecc->mode == NAND_ECC_SOFT_BCH) &&
4335 ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004336 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004339 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 break;
4341 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004342 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004343 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004346 WARN(1, "No oob scheme defined for oobsize %d\n",
4347 mtd->oobsize);
4348 ret = -EINVAL;
4349 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 }
4351 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004352
David Woodhouse956e9442006-09-25 17:12:39 +01004353 if (!chip->write_page)
4354 chip->write_page = nand_write_page;
4355
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004356 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004357 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004358 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004359 */
David Woodhouse956e9442006-09-25 17:12:39 +01004360
Huang Shijie97de79e02013-10-18 14:20:53 +08004361 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004362 case NAND_ECC_HW_OOB_FIRST:
4363 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004364 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004365 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4366 ret = -EINVAL;
4367 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004368 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004369 if (!ecc->read_page)
4370 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004371
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004372 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004373 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004374 if (!ecc->read_page)
4375 ecc->read_page = nand_read_page_hwecc;
4376 if (!ecc->write_page)
4377 ecc->write_page = nand_write_page_hwecc;
4378 if (!ecc->read_page_raw)
4379 ecc->read_page_raw = nand_read_page_raw;
4380 if (!ecc->write_page_raw)
4381 ecc->write_page_raw = nand_write_page_raw;
4382 if (!ecc->read_oob)
4383 ecc->read_oob = nand_read_oob_std;
4384 if (!ecc->write_oob)
4385 ecc->write_oob = nand_write_oob_std;
4386 if (!ecc->read_subpage)
4387 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004388 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004389 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004390
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004391 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004392 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4393 (!ecc->read_page ||
4394 ecc->read_page == nand_read_page_hwecc ||
4395 !ecc->write_page ||
4396 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004397 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4398 ret = -EINVAL;
4399 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004400 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004401 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004402 if (!ecc->read_page)
4403 ecc->read_page = nand_read_page_syndrome;
4404 if (!ecc->write_page)
4405 ecc->write_page = nand_write_page_syndrome;
4406 if (!ecc->read_page_raw)
4407 ecc->read_page_raw = nand_read_page_raw_syndrome;
4408 if (!ecc->write_page_raw)
4409 ecc->write_page_raw = nand_write_page_raw_syndrome;
4410 if (!ecc->read_oob)
4411 ecc->read_oob = nand_read_oob_syndrome;
4412 if (!ecc->write_oob)
4413 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004414
Huang Shijie97de79e02013-10-18 14:20:53 +08004415 if (mtd->writesize >= ecc->size) {
4416 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004417 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4418 ret = -EINVAL;
4419 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004420 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004421 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004422 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004423 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4424 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004425 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004426 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004428 case NAND_ECC_SOFT:
Ivan Djelic193bd402011-03-11 11:05:33 +01004429 case NAND_ECC_SOFT_BCH:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004430 ret = nand_set_ecc_soft_ops(mtd);
4431 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004432 ret = -EINVAL;
4433 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004434 }
4435 break;
4436
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004437 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004438 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004439 ecc->read_page = nand_read_page_raw;
4440 ecc->write_page = nand_write_page_raw;
4441 ecc->read_oob = nand_read_oob_std;
4442 ecc->read_page_raw = nand_read_page_raw;
4443 ecc->write_page_raw = nand_write_page_raw;
4444 ecc->write_oob = nand_write_oob_std;
4445 ecc->size = mtd->writesize;
4446 ecc->bytes = 0;
4447 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004451 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4452 ret = -EINVAL;
4453 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Brian Norris9ce244b2011-08-30 18:45:37 -07004456 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004457 if (!ecc->read_oob_raw)
4458 ecc->read_oob_raw = ecc->read_oob;
4459 if (!ecc->write_oob_raw)
4460 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004461
Boris Brezillon846031d2016-02-03 20:11:00 +01004462 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004463 mtd->ecc_strength = ecc->strength;
4464 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004465
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004466 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004467 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004468 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004469 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004470 ecc->steps = mtd->writesize / ecc->size;
4471 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004472 WARN(1, "Invalid ECC parameters\n");
4473 ret = -EINVAL;
4474 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004476 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004477
Boris Brezillon846031d2016-02-03 20:11:00 +01004478 /*
4479 * The number of bytes available for a client to place data into
4480 * the out of band area.
4481 */
4482 ret = mtd_ooblayout_count_freebytes(mtd);
4483 if (ret < 0)
4484 ret = 0;
4485
4486 mtd->oobavail = ret;
4487
4488 /* ECC sanity check: warn if it's too weak */
4489 if (!nand_ecc_strength_good(mtd))
4490 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4491 mtd->name);
4492
Brian Norris8b6e50c2011-05-25 14:59:01 -07004493 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004494 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004495 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004496 case 2:
4497 mtd->subpage_sft = 1;
4498 break;
4499 case 4:
4500 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004501 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004502 mtd->subpage_sft = 2;
4503 break;
4504 }
4505 }
4506 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4507
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004508 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004509 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004512 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004514 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304515 switch (ecc->mode) {
4516 case NAND_ECC_SOFT:
4517 case NAND_ECC_SOFT_BCH:
4518 if (chip->page_shift > 9)
4519 chip->options |= NAND_SUBPAGE_READ;
4520 break;
4521
4522 default:
4523 break;
4524 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004525
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004527 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004528 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4529 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004530 mtd->_erase = nand_erase;
4531 mtd->_point = NULL;
4532 mtd->_unpoint = NULL;
4533 mtd->_read = nand_read;
4534 mtd->_write = nand_write;
4535 mtd->_panic_write = panic_nand_write;
4536 mtd->_read_oob = nand_read_oob;
4537 mtd->_write_oob = nand_write_oob;
4538 mtd->_sync = nand_sync;
4539 mtd->_lock = NULL;
4540 mtd->_unlock = NULL;
4541 mtd->_suspend = nand_suspend;
4542 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004543 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004544 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004545 mtd->_block_isbad = nand_block_isbad;
4546 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004547 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004549 /*
4550 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4551 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4552 * properly set.
4553 */
4554 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004555 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004557 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004558 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
4561 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004562 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004563err_free:
4564 if (!(chip->options & NAND_OWN_BUFFERS))
4565 kfree(chip->buffers);
4566 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004568EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Brian Norris8b6e50c2011-05-25 14:59:01 -07004570/*
4571 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004572 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004573 * to call us from in-kernel code if the core NAND support is modular.
4574 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004575#ifdef MODULE
4576#define caller_is_module() (1)
4577#else
4578#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004579 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004580#endif
4581
4582/**
4583 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004584 * @mtd: MTD device structure
4585 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004586 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004587 * This fills out all the uninitialized function pointers with the defaults.
4588 * The flash ID is read and the mtd/chip structures are filled with the
4589 * appropriate values. The mtd->owner field must be set to the module of the
4590 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004591 */
4592int nand_scan(struct mtd_info *mtd, int maxchips)
4593{
4594 int ret;
4595
4596 /* Many callers got this wrong, so check for it for a while... */
4597 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004598 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004599 BUG();
4600 }
4601
David Woodhouse5e81e882010-02-26 18:32:56 +00004602 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004603 if (!ret)
4604 ret = nand_scan_tail(mtd);
4605 return ret;
4606}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004607EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004610 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004611 * @mtd: MTD device structure
4612 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004613void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004615 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004617 if ((chip->ecc.mode == NAND_ECC_SOFT ||
4618 chip->ecc.mode == NAND_ECC_SOFT_BCH) &&
4619 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004620 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4621
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004622 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Jesper Juhlfa671642005-11-07 01:01:27 -08004624 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004625 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004626 if (!(chip->options & NAND_OWN_BUFFERS))
4627 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004628
4629 /* Free bad block descriptor memory */
4630 if (chip->badblock_pattern && chip->badblock_pattern->options
4631 & NAND_BBT_DYNAMICSTRUCT)
4632 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633}
David Woodhousee0c7d762006-05-13 18:07:53 +01004634EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004635
David Woodhousee0c7d762006-05-13 18:07:53 +01004636MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004637MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4638MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004639MODULE_DESCRIPTION("Generic NAND flash driver code");