blob: 108adefcc8cc3f4c5c5860409e2397aab37cdeba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020048#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Huang Shijie6a8214a2012-11-19 14:43:30 +080050static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020052static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
53 struct mtd_oob_ops *ops);
54
Boris Brezillon41b207a2016-02-03 19:06:15 +010055/* Define default oob placement schemes for large and small page devices */
56static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
57 struct mtd_oob_region *oobregion)
58{
59 struct nand_chip *chip = mtd_to_nand(mtd);
60 struct nand_ecc_ctrl *ecc = &chip->ecc;
61
62 if (section > 1)
63 return -ERANGE;
64
65 if (!section) {
66 oobregion->offset = 0;
67 oobregion->length = 4;
68 } else {
69 oobregion->offset = 6;
70 oobregion->length = ecc->total - 4;
71 }
72
73 return 0;
74}
75
76static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
77 struct mtd_oob_region *oobregion)
78{
79 if (section > 1)
80 return -ERANGE;
81
82 if (mtd->oobsize == 16) {
83 if (section)
84 return -ERANGE;
85
86 oobregion->length = 8;
87 oobregion->offset = 8;
88 } else {
89 oobregion->length = 2;
90 if (!section)
91 oobregion->offset = 3;
92 else
93 oobregion->offset = 6;
94 }
95
96 return 0;
97}
98
99const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
100 .ecc = nand_ooblayout_ecc_sp,
101 .free = nand_ooblayout_free_sp,
102};
103EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
104
105static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
106 struct mtd_oob_region *oobregion)
107{
108 struct nand_chip *chip = mtd_to_nand(mtd);
109 struct nand_ecc_ctrl *ecc = &chip->ecc;
110
111 if (section)
112 return -ERANGE;
113
114 oobregion->length = ecc->total;
115 oobregion->offset = mtd->oobsize - oobregion->length;
116
117 return 0;
118}
119
120static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
121 struct mtd_oob_region *oobregion)
122{
123 struct nand_chip *chip = mtd_to_nand(mtd);
124 struct nand_ecc_ctrl *ecc = &chip->ecc;
125
126 if (section)
127 return -ERANGE;
128
129 oobregion->length = mtd->oobsize - ecc->total - 2;
130 oobregion->offset = 2;
131
132 return 0;
133}
134
135const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
136 .ecc = nand_ooblayout_ecc_lp,
137 .free = nand_ooblayout_free_lp,
138};
139EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200140
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530141static int check_offs_len(struct mtd_info *mtd,
142 loff_t ofs, uint64_t len)
143{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100144 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530145 int ret = 0;
146
147 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300148 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700149 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530150 ret = -EINVAL;
151 }
152
153 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300154 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700155 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530156 ret = -EINVAL;
157 }
158
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530159 return ret;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
163 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700164 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000165 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800166 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100168static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100170 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200172 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200173 spin_lock(&chip->controller->lock);
174 chip->controller->active = NULL;
175 chip->state = FL_READY;
176 wake_up(&chip->controller->wq);
177 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/**
181 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700182 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700184 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200186static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100188 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200189 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/**
Masanari Iida064a7692012-11-09 23:20:58 +0900193 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700194 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700196 * Default read function for 16bit buswidth with endianness conversion.
197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200199static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100201 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200202 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700207 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700209 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
211static u16 nand_read_word(struct mtd_info *mtd)
212{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100213 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200214 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700219 * @mtd: MTD device structure
220 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
222 * Default select function for 1 chip devices.
223 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200224static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100226 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200227
228 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200230 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234
235 default:
236 BUG();
237 }
238}
239
240/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100241 * nand_write_byte - [DEFAULT] write single byte to chip
242 * @mtd: MTD device structure
243 * @byte: value to write
244 *
245 * Default function to write a byte to I/O[7:0]
246 */
247static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
248{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100249 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100250
251 chip->write_buf(mtd, &byte, 1);
252}
253
254/**
255 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
256 * @mtd: MTD device structure
257 * @byte: value to write
258 *
259 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
260 */
261static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
262{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100263 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100264 uint16_t word = byte;
265
266 /*
267 * It's not entirely clear what should happen to I/O[15:8] when writing
268 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
269 *
270 * When the host supports a 16-bit bus width, only data is
271 * transferred at the 16-bit width. All address and command line
272 * transfers shall use only the lower 8-bits of the data bus. During
273 * command transfers, the host may place any value on the upper
274 * 8-bits of the data bus. During address transfers, the host shall
275 * set the upper 8-bits of the data bus to 00h.
276 *
277 * One user of the write_byte callback is nand_onfi_set_features. The
278 * four parameters are specified to be written to I/O[7:0], but this is
279 * neither an address nor a command transfer. Let's assume a 0 on the
280 * upper I/O lines is OK.
281 */
282 chip->write_buf(mtd, (uint8_t *)&word, 2);
283}
284
285/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700287 * @mtd: MTD device structure
288 * @buf: data buffer
289 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700291 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200293static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100295 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Alexander Shiyan76413832013-04-13 09:32:13 +0400297 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000301 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700302 * @mtd: MTD device structure
303 * @buf: buffer to store date
304 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700306 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200308static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100310 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Alexander Shiyan76413832013-04-13 09:32:13 +0400312 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700317 * @mtd: MTD device structure
318 * @buf: data buffer
319 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700321 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200323static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100325 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000327
Alexander Shiyan76413832013-04-13 09:32:13 +0400328 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000332 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700333 * @mtd: MTD device structure
334 * @buf: buffer to store date
335 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700337 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200339static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100341 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Alexander Shiyan76413832013-04-13 09:32:13 +0400344 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700349 * @mtd: MTD device structure
350 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000352 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530354static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Archit Taneja9f3e0422016-02-03 14:29:49 +0530356 int page, res = 0, i = 0;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100357 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 u16 bad;
359
Brian Norris5fb15492011-05-31 16:31:21 -0700360 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700361 ofs += mtd->erasesize - mtd->writesize;
362
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100363 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
364
Brian Norriscdbec052012-01-13 18:11:48 -0800365 do {
366 if (chip->options & NAND_BUSWIDTH_16) {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB,
368 chip->badblockpos & 0xFE, page);
369 bad = cpu_to_le16(chip->read_word(mtd));
370 if (chip->badblockpos & 0x1)
371 bad >>= 8;
372 else
373 bad &= 0xFF;
374 } else {
375 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
376 page);
377 bad = chip->read_byte(mtd);
378 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379
Brian Norriscdbec052012-01-13 18:11:48 -0800380 if (likely(chip->badblockbits == 8))
381 res = bad != 0xFF;
382 else
383 res = hweight8(bad) < chip->badblockbits;
384 ofs += mtd->writesize;
385 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
386 i++;
387 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return res;
390}
391
392/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700393 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700394 * @mtd: MTD device structure
395 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700397 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700398 * specific driver. It provides the details for writing a bad block marker to a
399 * block.
400 */
401static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
402{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100403 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700404 struct mtd_oob_ops ops;
405 uint8_t buf[2] = { 0, 0 };
406 int ret = 0, res, i = 0;
407
Brian Norris0ec56dc2015-02-28 02:02:30 -0800408 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700409 ops.oobbuf = buf;
410 ops.ooboffs = chip->badblockpos;
411 if (chip->options & NAND_BUSWIDTH_16) {
412 ops.ooboffs &= ~0x01;
413 ops.len = ops.ooblen = 2;
414 } else {
415 ops.len = ops.ooblen = 1;
416 }
417 ops.mode = MTD_OPS_PLACE_OOB;
418
419 /* Write to first/last page(s) if necessary */
420 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
421 ofs += mtd->erasesize - mtd->writesize;
422 do {
423 res = nand_do_write_oob(mtd, ofs, &ops);
424 if (!ret)
425 ret = res;
426
427 i++;
428 ofs += mtd->writesize;
429 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
430
431 return ret;
432}
433
434/**
435 * nand_block_markbad_lowlevel - mark a block bad
436 * @mtd: MTD device structure
437 * @ofs: offset from device start
438 *
439 * This function performs the generic NAND bad block marking steps (i.e., bad
440 * block table(s) and/or marker(s)). We only allow the hardware driver to
441 * specify how to write bad block markers to OOB (chip->block_markbad).
442 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700443 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800444 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700445 * (2) write bad block marker to OOB area of affected block (unless flag
446 * NAND_BBT_NO_OOB_BBM is present)
447 * (3) update the BBT
448 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800449 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700451static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100453 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700454 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000455
Brian Norrisb32843b2013-07-30 17:52:59 -0700456 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800457 struct erase_info einfo;
458
459 /* Attempt erase before marking OOB */
460 memset(&einfo, 0, sizeof(einfo));
461 einfo.mtd = mtd;
462 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300463 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800464 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800465
Brian Norrisb32843b2013-07-30 17:52:59 -0700466 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800467 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700468 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300469 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200470 }
Brian Norrise2414f42012-02-06 13:44:00 -0800471
Brian Norrisb32843b2013-07-30 17:52:59 -0700472 /* Mark block bad in BBT */
473 if (chip->bbt) {
474 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800475 if (!ret)
476 ret = res;
477 }
478
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200479 if (!ret)
480 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300481
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000485/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 * Check, if the device is write protected. The function expects, that the
490 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100492static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100494 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200495
Brian Norris8b6e50c2011-05-25 14:59:01 -0700496 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200497 if (chip->options & NAND_BROKEN_XD)
498 return 0;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200501 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
502 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800506 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700507 * @mtd: MTD device structure
508 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300509 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800510 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300511 */
512static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
513{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100514 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300515
516 if (!chip->bbt)
517 return 0;
518 /* Return info from the table */
519 return nand_isreserved_bbt(mtd, ofs);
520}
521
522/**
523 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
524 * @mtd: MTD device structure
525 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700526 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *
528 * Check, if the block is bad. Either by reading the bad block table or
529 * calling of the scan function.
530 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530531static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100533 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000534
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200535 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530536 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100539 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200542/**
543 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700544 * @mtd: MTD device structure
545 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200546 *
547 * Helper function for nand_wait_ready used when needing to wait in interrupt
548 * context.
549 */
550static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
551{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100552 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200553 int i;
554
555 /* Wait for the device to get ready */
556 for (i = 0; i < timeo; i++) {
557 if (chip->dev_ready(mtd))
558 break;
559 touch_softlockup_watchdog();
560 mdelay(1);
561 }
562}
563
Alex Smithb70af9b2015-10-06 14:52:07 +0100564/**
565 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
566 * @mtd: MTD device structure
567 *
568 * Wait for the ready pin after a command, and warn if a timeout occurs.
569 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100570void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000571{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100572 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100573 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000574
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200575 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100576 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200577
Brian Norris7854d3f2011-06-23 14:12:08 -0700578 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100579 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000580 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200581 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300582 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100583 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100585
Brian Norris9ebfdf52016-03-04 17:19:23 -0800586 if (!chip->dev_ready(mtd))
587 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000588}
David Woodhouse4b648b02006-09-25 17:05:24 +0100589EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200592 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
593 * @mtd: MTD device structure
594 * @timeo: Timeout in ms
595 *
596 * Wait for status ready (i.e. command done) or timeout.
597 */
598static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
599{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100600 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200601
602 timeo = jiffies + msecs_to_jiffies(timeo);
603 do {
604 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
605 break;
606 touch_softlockup_watchdog();
607 } while (time_before(jiffies, timeo));
608};
609
610/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700612 * @mtd: MTD device structure
613 * @command: the command to be sent
614 * @column: the column address for this command, -1 if none
615 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700617 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200618 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200620static void nand_command(struct mtd_info *mtd, unsigned int command,
621 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100623 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (command == NAND_CMD_SEQIN) {
628 int readcmd;
629
Joern Engel28318772006-05-22 23:18:05 +0200630 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200632 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 readcmd = NAND_CMD_READOOB;
634 } else if (column < 256) {
635 /* First 256 bytes --> READ0 */
636 readcmd = NAND_CMD_READ0;
637 } else {
638 column -= 256;
639 readcmd = NAND_CMD_READ1;
640 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Brian Norris8b6e50c2011-05-25 14:59:01 -0700646 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
648 /* Serially input address */
649 if (column != -1) {
650 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800651 if (chip->options & NAND_BUSWIDTH_16 &&
652 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200655 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200657 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200658 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200659 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 if (chip->chipsize > (32 << 20))
663 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200664 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200665 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000666
667 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700668 * Program and erase have their own busy handlers status and sequential
669 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case NAND_CMD_PAGEPROG:
674 case NAND_CMD_ERASE1:
675 case NAND_CMD_ERASE2:
676 case NAND_CMD_SEQIN:
677 case NAND_CMD_STATUS:
678 return;
679
680 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200681 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 udelay(chip->chip_delay);
684 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200685 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200686 chip->cmd_ctrl(mtd,
687 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200688 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
689 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691
David Woodhousee0c7d762006-05-13 18:07:53 +0100692 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000694 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * If we don't have access to the busy pin, we apply the given
696 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100697 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200698 if (!chip->dev_ready) {
699 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700703 /*
704 * Apply this short delay always to ensure that we do wait tWB in
705 * any case on any machine.
706 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000708
709 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/**
713 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700714 * @mtd: MTD device structure
715 * @command: the command to be sent
716 * @column: the column address for this command, -1 if none
717 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200719 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700720 * devices. We don't have the separate regions as we have in the small page
721 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200723static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
724 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100726 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Emulate NAND_CMD_READOOB */
729 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200730 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 command = NAND_CMD_READ0;
732 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000733
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200734 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400735 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200738 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /* Serially input address */
741 if (column != -1) {
742 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800743 if (chip->options & NAND_BUSWIDTH_16 &&
744 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200747 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200748 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200751 chip->cmd_ctrl(mtd, page_addr, ctrl);
752 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200753 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 if (chip->chipsize > (128 << 20))
756 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200757 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200760 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000761
762 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700763 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100764 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 case NAND_CMD_CACHEDPROG:
769 case NAND_CMD_PAGEPROG:
770 case NAND_CMD_ERASE1:
771 case NAND_CMD_ERASE2:
772 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200773 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000775 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200778 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200780 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200781 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
782 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
783 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
784 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200785 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
786 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return;
788
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200789 case NAND_CMD_RNDOUT:
790 /* No ready / busy check necessary */
791 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
792 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
793 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
794 NAND_NCE | NAND_CTRL_CHANGE);
795 return;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200798 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
799 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
800 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
801 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000802
David Woodhousee0c7d762006-05-13 18:07:53 +0100803 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000805 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700807 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100808 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 if (!chip->dev_ready) {
810 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000814
Brian Norris8b6e50c2011-05-25 14:59:01 -0700815 /*
816 * Apply this short delay always to ensure that we do wait tWB in
817 * any case on any machine.
818 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100819 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000820
821 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200825 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700826 * @chip: the nand chip descriptor
827 * @mtd: MTD device structure
828 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200829 *
830 * Used when in panic, no locks are taken.
831 */
832static void panic_nand_get_device(struct nand_chip *chip,
833 struct mtd_info *mtd, int new_state)
834{
Brian Norris7854d3f2011-06-23 14:12:08 -0700835 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200836 chip->controller->active = chip;
837 chip->state = new_state;
838}
839
840/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700842 * @mtd: MTD device structure
843 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *
845 * Get the device and lock it for exclusive access
846 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200847static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800848nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100850 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200851 spinlock_t *lock = &chip->controller->lock;
852 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100853 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200854retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100855 spin_lock(lock);
856
vimal singhb8b3ee92009-07-09 20:41:22 +0530857 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200858 if (!chip->controller->active)
859 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200860
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 if (chip->controller->active == chip && chip->state == FL_READY) {
862 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100863 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100864 return 0;
865 }
866 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800867 if (chip->controller->active->state == FL_PM_SUSPENDED) {
868 chip->state = FL_PM_SUSPENDED;
869 spin_unlock(lock);
870 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800871 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100872 }
873 set_current_state(TASK_UNINTERRUPTIBLE);
874 add_wait_queue(wq, &wait);
875 spin_unlock(lock);
876 schedule();
877 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto retry;
879}
880
881/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700882 * panic_nand_wait - [GENERIC] wait until the command is done
883 * @mtd: MTD device structure
884 * @chip: NAND chip structure
885 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200886 *
887 * Wait for command done. This is a helper function for nand_wait used when
888 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400889 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200890 */
891static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
892 unsigned long timeo)
893{
894 int i;
895 for (i = 0; i < timeo; i++) {
896 if (chip->dev_ready) {
897 if (chip->dev_ready(mtd))
898 break;
899 } else {
900 if (chip->read_byte(mtd) & NAND_STATUS_READY)
901 break;
902 }
903 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200904 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200905}
906
907/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700908 * nand_wait - [DEFAULT] wait until the command is done
909 * @mtd: MTD device structure
910 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100912 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700913 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200914static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916
Alex Smithb70af9b2015-10-06 14:52:07 +0100917 int status;
918 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Brian Norris8b6e50c2011-05-25 14:59:01 -0700920 /*
921 * Apply this short delay always to ensure that we do wait tWB in any
922 * case on any machine.
923 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100924 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200926 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200928 if (in_interrupt() || oops_in_progress)
929 panic_nand_wait(mtd, chip, timeo);
930 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800931 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100932 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200933 if (chip->dev_ready) {
934 if (chip->dev_ready(mtd))
935 break;
936 } else {
937 if (chip->read_byte(mtd) & NAND_STATUS_READY)
938 break;
939 }
940 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100941 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800943
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200944 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100945 /* This can happen if in case of timeout or buggy dev_ready */
946 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return status;
948}
949
950/**
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;
Ron4a4163ca2014-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 Shijie11041ae62012-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{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002165 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002166
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
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002176 if (ops->mode != MTD_OPS_PLACE_OOB &&
2177 ops->mode != MTD_OPS_AUTO_OOB &&
2178 ops->mode != MTD_OPS_RAW)
2179 return -ENOTSUPP;
2180
Huang Shijie6a8214a2012-11-19 14:43:30 +08002181 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002183 if (!ops->datbuf)
2184 ret = nand_do_read_oob(mtd, from, ops);
2185 else
2186 ret = nand_do_read_ops(mtd, from, ops);
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002189 return ret;
2190}
2191
2192
2193/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002194 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002195 * @mtd: mtd info structure
2196 * @chip: nand chip info structure
2197 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002198 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002199 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002200 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002201 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002202 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002203static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002204 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002205{
2206 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002207 if (oob_required)
2208 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002209
2210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002213/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002214 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002215 * @mtd: mtd info structure
2216 * @chip: nand chip info structure
2217 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002218 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002219 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002220 *
2221 * We need a special oob layout and handling even when ECC isn't checked.
2222 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002223static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002224 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002225 const uint8_t *buf, int oob_required,
2226 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002227{
2228 int eccsize = chip->ecc.size;
2229 int eccbytes = chip->ecc.bytes;
2230 uint8_t *oob = chip->oob_poi;
2231 int steps, size;
2232
2233 for (steps = chip->ecc.steps; steps > 0; steps--) {
2234 chip->write_buf(mtd, buf, eccsize);
2235 buf += eccsize;
2236
2237 if (chip->ecc.prepad) {
2238 chip->write_buf(mtd, oob, chip->ecc.prepad);
2239 oob += chip->ecc.prepad;
2240 }
2241
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002242 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002243 oob += eccbytes;
2244
2245 if (chip->ecc.postpad) {
2246 chip->write_buf(mtd, oob, chip->ecc.postpad);
2247 oob += chip->ecc.postpad;
2248 }
2249 }
2250
2251 size = mtd->oobsize - (oob - chip->oob_poi);
2252 if (size)
2253 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002254
2255 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002256}
2257/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002258 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002259 * @mtd: mtd info structure
2260 * @chip: nand chip info structure
2261 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002262 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002263 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002264 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002265static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002266 const uint8_t *buf, int oob_required,
2267 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002268{
Boris Brezillon846031d2016-02-03 20:11:00 +01002269 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002270 int eccbytes = chip->ecc.bytes;
2271 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002272 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002273 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002274
Brian Norris7854d3f2011-06-23 14:12:08 -07002275 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002276 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2277 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002278
Boris Brezillon846031d2016-02-03 20:11:00 +01002279 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2280 chip->ecc.total);
2281 if (ret)
2282 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002283
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002284 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002285}
2286
2287/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002288 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002289 * @mtd: mtd info structure
2290 * @chip: nand chip info structure
2291 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002292 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002293 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002294 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002295static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002296 const uint8_t *buf, int oob_required,
2297 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002298{
Boris Brezillon846031d2016-02-03 20:11:00 +01002299 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002300 int eccbytes = chip->ecc.bytes;
2301 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002302 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002303 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002304
2305 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2306 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002307 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002308 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2309 }
2310
Boris Brezillon846031d2016-02-03 20:11:00 +01002311 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2312 chip->ecc.total);
2313 if (ret)
2314 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002315
2316 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002317
2318 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002319}
2320
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302321
2322/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002323 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302324 * @mtd: mtd info structure
2325 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002326 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302327 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002328 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302329 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002330 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302331 */
2332static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2333 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002334 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002335 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302336{
2337 uint8_t *oob_buf = chip->oob_poi;
2338 uint8_t *ecc_calc = chip->buffers->ecccalc;
2339 int ecc_size = chip->ecc.size;
2340 int ecc_bytes = chip->ecc.bytes;
2341 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302342 uint32_t start_step = offset / ecc_size;
2343 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2344 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002345 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302346
2347 for (step = 0; step < ecc_steps; step++) {
2348 /* configure controller for WRITE access */
2349 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2350
2351 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002352 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302353
2354 /* mask ECC of un-touched subpages by padding 0xFF */
2355 if ((step < start_step) || (step > end_step))
2356 memset(ecc_calc, 0xff, ecc_bytes);
2357 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002358 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302359
2360 /* mask OOB of un-touched subpages by padding 0xFF */
2361 /* if oob_required, preserve OOB metadata of written subpage */
2362 if (!oob_required || (step < start_step) || (step > end_step))
2363 memset(oob_buf, 0xff, oob_bytes);
2364
Brian Norrisd6a950802013-08-08 17:16:36 -07002365 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302366 ecc_calc += ecc_bytes;
2367 oob_buf += oob_bytes;
2368 }
2369
2370 /* copy calculated ECC for whole page to chip->buffer->oob */
2371 /* this include masked-value(0xFF) for unwritten subpages */
2372 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002373 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2374 chip->ecc.total);
2375 if (ret)
2376 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302377
2378 /* write OOB buffer to NAND device */
2379 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2380
2381 return 0;
2382}
2383
2384
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002385/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002386 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002387 * @mtd: mtd info structure
2388 * @chip: nand chip info structure
2389 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002390 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002391 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002392 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002393 * The hw generator calculates the error syndrome automatically. Therefore we
2394 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002395 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002396static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002397 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002398 const uint8_t *buf, int oob_required,
2399 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002400{
2401 int i, eccsize = chip->ecc.size;
2402 int eccbytes = chip->ecc.bytes;
2403 int eccsteps = chip->ecc.steps;
2404 const uint8_t *p = buf;
2405 uint8_t *oob = chip->oob_poi;
2406
2407 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2408
2409 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2410 chip->write_buf(mtd, p, eccsize);
2411
2412 if (chip->ecc.prepad) {
2413 chip->write_buf(mtd, oob, chip->ecc.prepad);
2414 oob += chip->ecc.prepad;
2415 }
2416
2417 chip->ecc.calculate(mtd, p, oob);
2418 chip->write_buf(mtd, oob, eccbytes);
2419 oob += eccbytes;
2420
2421 if (chip->ecc.postpad) {
2422 chip->write_buf(mtd, oob, chip->ecc.postpad);
2423 oob += chip->ecc.postpad;
2424 }
2425 }
2426
2427 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002428 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002429 if (i)
2430 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002431
2432 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002433}
2434
2435/**
David Woodhouse956e9442006-09-25 17:12:39 +01002436 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002437 * @mtd: MTD device structure
2438 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302439 * @offset: address offset within the page
2440 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002441 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002442 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002443 * @page: page number to write
2444 * @cached: cached programming
2445 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002446 */
2447static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302448 uint32_t offset, int data_len, const uint8_t *buf,
2449 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002450{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302451 int status, subpage;
2452
2453 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2454 chip->ecc.write_subpage)
2455 subpage = offset || (data_len < mtd->writesize);
2456 else
2457 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002458
2459 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2460
David Woodhouse956e9442006-09-25 17:12:39 +01002461 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302462 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002463 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302464 else if (subpage)
2465 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002466 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002467 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002468 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2469 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002470
2471 if (status < 0)
2472 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002473
2474 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002475 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002476 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002477 */
2478 cached = 0;
2479
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002480 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002481
2482 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002483 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002484 /*
2485 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002486 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002487 */
2488 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2489 status = chip->errstat(mtd, chip, FL_WRITING, status,
2490 page);
2491
2492 if (status & NAND_STATUS_FAIL)
2493 return -EIO;
2494 } else {
2495 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002496 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002497 }
2498
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002499 return 0;
2500}
2501
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002502/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002503 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002504 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002505 * @oob: oob data buffer
2506 * @len: oob data write length
2507 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002508 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002509static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2510 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002511{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002512 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002513 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002514
2515 /*
2516 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2517 * data from a previous OOB read.
2518 */
2519 memset(chip->oob_poi, 0xff, mtd->oobsize);
2520
Florian Fainellif8ac0412010-09-07 13:23:43 +02002521 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002522
Brian Norris0612b9d2011-08-30 18:45:40 -07002523 case MTD_OPS_PLACE_OOB:
2524 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002525 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2526 return oob + len;
2527
Boris Brezillon846031d2016-02-03 20:11:00 +01002528 case MTD_OPS_AUTO_OOB:
2529 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2530 ops->ooboffs, len);
2531 BUG_ON(ret);
2532 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002533
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002534 default:
2535 BUG();
2536 }
2537 return NULL;
2538}
2539
Florian Fainellif8ac0412010-09-07 13:23:43 +02002540#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002541
2542/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002543 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002544 * @mtd: MTD device structure
2545 * @to: offset to write to
2546 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002547 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002548 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002549 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002550static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2551 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002552{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002553 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002554 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002555 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002556
2557 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002558 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002559
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002560 uint8_t *oob = ops->oobbuf;
2561 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302562 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002563 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002564
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002565 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002566 if (!writelen)
2567 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002568
Brian Norris8b6e50c2011-05-25 14:59:01 -07002569 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002570 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002571 pr_notice("%s: attempt to write non page aligned data\n",
2572 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002573 return -EINVAL;
2574 }
2575
Thomas Gleixner29072b92006-09-28 15:38:36 +02002576 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002577
Thomas Gleixner6a930962006-06-28 00:11:45 +02002578 chipnr = (int)(to >> chip->chip_shift);
2579 chip->select_chip(mtd, chipnr);
2580
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002581 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002582 if (nand_check_wp(mtd)) {
2583 ret = -EIO;
2584 goto err_out;
2585 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002586
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002587 realpage = (int)(to >> chip->page_shift);
2588 page = realpage & chip->pagemask;
2589 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2590
2591 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002592 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2593 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002594 chip->pagebuf = -1;
2595
Maxim Levitsky782ce792010-02-22 20:39:36 +02002596 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002597 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2598 ret = -EINVAL;
2599 goto err_out;
2600 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002601
Florian Fainellif8ac0412010-09-07 13:23:43 +02002602 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002603 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002604 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002605 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002606 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002607 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002608
Kamal Dasu66507c72014-05-01 20:51:19 -04002609 if (part_pagewr)
2610 use_bufpoi = 1;
2611 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2612 use_bufpoi = !virt_addr_valid(buf);
2613 else
2614 use_bufpoi = 0;
2615
2616 /* Partial page write?, or need to use bounce buffer */
2617 if (use_bufpoi) {
2618 pr_debug("%s: using write bounce buffer for buf@%p\n",
2619 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002620 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002621 if (part_pagewr)
2622 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002623 chip->pagebuf = -1;
2624 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2625 memcpy(&chip->buffers->databuf[column], buf, bytes);
2626 wbuf = chip->buffers->databuf;
2627 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002628
Maxim Levitsky782ce792010-02-22 20:39:36 +02002629 if (unlikely(oob)) {
2630 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002631 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002632 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002633 } else {
2634 /* We still need to erase leftover OOB data */
2635 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002636 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302637 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2638 oob_required, page, cached,
2639 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002640 if (ret)
2641 break;
2642
2643 writelen -= bytes;
2644 if (!writelen)
2645 break;
2646
Thomas Gleixner29072b92006-09-28 15:38:36 +02002647 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002648 buf += bytes;
2649 realpage++;
2650
2651 page = realpage & chip->pagemask;
2652 /* Check, if we cross a chip boundary */
2653 if (!page) {
2654 chipnr++;
2655 chip->select_chip(mtd, -1);
2656 chip->select_chip(mtd, chipnr);
2657 }
2658 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002659
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002660 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002661 if (unlikely(oob))
2662 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002663
2664err_out:
2665 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002666 return ret;
2667}
2668
2669/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002670 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002671 * @mtd: MTD device structure
2672 * @to: offset to write to
2673 * @len: number of bytes to write
2674 * @retlen: pointer to variable to store the number of written bytes
2675 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002676 *
2677 * NAND write with ECC. Used when performing writes in interrupt context, this
2678 * may for example be called by mtdoops when writing an oops while in panic.
2679 */
2680static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2681 size_t *retlen, const uint8_t *buf)
2682{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002683 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002684 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002685 int ret;
2686
Brian Norris8b6e50c2011-05-25 14:59:01 -07002687 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002688 panic_nand_wait(mtd, chip, 400);
2689
Brian Norris8b6e50c2011-05-25 14:59:01 -07002690 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002691 panic_nand_get_device(chip, mtd, FL_WRITING);
2692
Brian Norris0ec56dc2015-02-28 02:02:30 -08002693 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002694 ops.len = len;
2695 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002696 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002697
Brian Norris4a89ff82011-08-30 18:45:45 -07002698 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002699
Brian Norris4a89ff82011-08-30 18:45:45 -07002700 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002701 return ret;
2702}
2703
2704/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002705 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002706 * @mtd: MTD device structure
2707 * @to: offset to write to
2708 * @len: number of bytes to write
2709 * @retlen: pointer to variable to store the number of written bytes
2710 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002712 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002714static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002715 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Brian Norris4a89ff82011-08-30 18:45:45 -07002717 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002718 int ret;
2719
Huang Shijie6a8214a2012-11-19 14:43:30 +08002720 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002721 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002722 ops.len = len;
2723 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002724 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002725 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002726 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002727 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002728 return ret;
2729}
2730
2731/**
2732 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002733 * @mtd: MTD device structure
2734 * @to: offset to write to
2735 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002736 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002737 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002738 */
2739static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2740 struct mtd_oob_ops *ops)
2741{
Adrian Hunter03736152007-01-31 17:58:29 +02002742 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002743 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Brian Norris289c0522011-07-19 10:06:09 -07002745 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302746 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Boris BREZILLON29f10582016-03-07 10:46:52 +01002748 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002751 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002752 pr_debug("%s: attempt to write past end of page\n",
2753 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return -EINVAL;
2755 }
2756
Adrian Hunter03736152007-01-31 17:58:29 +02002757 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002758 pr_debug("%s: attempt to start write outside oob\n",
2759 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002760 return -EINVAL;
2761 }
2762
Jason Liu775adc3d42011-02-25 13:06:18 +08002763 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002764 if (unlikely(to >= mtd->size ||
2765 ops->ooboffs + ops->ooblen >
2766 ((mtd->size >> chip->page_shift) -
2767 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002768 pr_debug("%s: attempt to write beyond end of device\n",
2769 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002770 return -EINVAL;
2771 }
2772
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002773 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002774 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002776 /* Shift to get page */
2777 page = (int)(to >> chip->page_shift);
2778
2779 /*
2780 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2781 * of my DiskOnChip 2000 test units) will clear the whole data page too
2782 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2783 * it in the doc2000 driver in August 1999. dwmw2.
2784 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002785 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002788 if (nand_check_wp(mtd)) {
2789 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002790 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002791 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002794 if (page == chip->pagebuf)
2795 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002797 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002798
Brian Norris0612b9d2011-08-30 18:45:40 -07002799 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002800 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2801 else
2802 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002803
Huang Shijieb0bb6902012-11-19 14:43:29 +08002804 chip->select_chip(mtd, -1);
2805
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002806 if (status)
2807 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Vitaly Wool70145682006-11-03 18:20:38 +03002809 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002811 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002814/**
2815 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002816 * @mtd: MTD device structure
2817 * @to: offset to write to
2818 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002819 */
2820static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2821 struct mtd_oob_ops *ops)
2822{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002823 int ret = -ENOTSUPP;
2824
2825 ops->retlen = 0;
2826
2827 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002828 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002829 pr_debug("%s: attempt to write beyond end of device\n",
2830 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002831 return -EINVAL;
2832 }
2833
Huang Shijie6a8214a2012-11-19 14:43:30 +08002834 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002835
Florian Fainellif8ac0412010-09-07 13:23:43 +02002836 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002837 case MTD_OPS_PLACE_OOB:
2838 case MTD_OPS_AUTO_OOB:
2839 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002840 break;
2841
2842 default:
2843 goto out;
2844 }
2845
2846 if (!ops->datbuf)
2847 ret = nand_do_write_oob(mtd, to, ops);
2848 else
2849 ret = nand_do_write_ops(mtd, to, ops);
2850
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002851out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002852 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return ret;
2854}
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856/**
Brian Norris49c50b92014-05-06 16:02:19 -07002857 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002858 * @mtd: MTD device structure
2859 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 *
Brian Norris49c50b92014-05-06 16:02:19 -07002861 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 */
Brian Norris49c50b92014-05-06 16:02:19 -07002863static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002865 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002867 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2868 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002869
2870 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
2873/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002875 * @mtd: MTD device structure
2876 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002878 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002880static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
David Woodhousee0c7d762006-05-13 18:07:53 +01002882 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002886 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002887 * @mtd: MTD device structure
2888 * @instr: erase instruction
2889 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002891 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002893int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2894 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Adrian Hunter69423d92008-12-10 13:37:21 +00002896 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002897 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00002898 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Brian Norris289c0522011-07-19 10:06:09 -07002900 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2901 __func__, (unsigned long long)instr->addr,
2902 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302904 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002908 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002911 page = (int)(instr->addr >> chip->page_shift);
2912 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002915 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002918 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /* Check, if it is write protected */
2921 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002922 pr_debug("%s: device is write protected!\n",
2923 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 instr->state = MTD_ERASE_FAILED;
2925 goto erase_exit;
2926 }
2927
2928 /* Loop through the pages */
2929 len = instr->len;
2930
2931 instr->state = MTD_ERASING;
2932
2933 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002934 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002935 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05302936 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002937 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2938 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 instr->state = MTD_ERASE_FAILED;
2940 goto erase_exit;
2941 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002942
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002943 /*
2944 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002945 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002946 */
2947 if (page <= chip->pagebuf && chip->pagebuf <
2948 (page + pages_per_block))
2949 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Brian Norris49c50b92014-05-06 16:02:19 -07002951 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002953 /*
2954 * See if operation failed and additional status checks are
2955 * available
2956 */
2957 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2958 status = chip->errstat(mtd, chip, FL_ERASING,
2959 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002962 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002963 pr_debug("%s: failed erase, page 0x%08x\n",
2964 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002966 instr->fail_addr =
2967 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 goto erase_exit;
2969 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002972 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 page += pages_per_block;
2974
2975 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002976 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002978 chip->select_chip(mtd, -1);
2979 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981 }
2982 instr->state = MTD_ERASE_DONE;
2983
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002984erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002989 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 nand_release_device(mtd);
2991
David Woodhouse49defc02007-10-06 15:01:59 -04002992 /* Do call back function */
2993 if (!ret)
2994 mtd_erase_callback(instr);
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 /* Return more or less happy */
2997 return ret;
2998}
2999
3000/**
3001 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003002 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003004 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003006static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
Brian Norris289c0522011-07-19 10:06:09 -07003008 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003011 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003013 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003017 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003018 * @mtd: MTD device structure
3019 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003021static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303023 struct nand_chip *chip = mtd_to_nand(mtd);
3024 int chipnr = (int)(offs >> chip->chip_shift);
3025 int ret;
3026
3027 /* Select the NAND device */
3028 nand_get_device(mtd, FL_READING);
3029 chip->select_chip(mtd, chipnr);
3030
3031 ret = nand_block_checkbad(mtd, offs, 0);
3032
3033 chip->select_chip(mtd, -1);
3034 nand_release_device(mtd);
3035
3036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003040 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003041 * @mtd: MTD device structure
3042 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003044static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 int ret;
3047
Florian Fainellif8ac0412010-09-07 13:23:43 +02003048 ret = nand_block_isbad(mtd, ofs);
3049 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003050 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (ret > 0)
3052 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003053 return ret;
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Brian Norris5a0edb22013-07-30 17:52:58 -07003056 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
3059/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003060 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3061 * @mtd: MTD device structure
3062 * @chip: nand chip info structure
3063 * @addr: feature address.
3064 * @subfeature_param: the subfeature parameters, a four bytes array.
3065 */
3066static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3067 int addr, uint8_t *subfeature_param)
3068{
3069 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003070 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003071
David Mosbergerd914c932013-05-29 15:30:13 +03003072 if (!chip->onfi_version ||
3073 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3074 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003075 return -EINVAL;
3076
3077 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003078 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3079 chip->write_byte(mtd, subfeature_param[i]);
3080
Huang Shijie7db03ec2012-09-13 14:57:52 +08003081 status = chip->waitfunc(mtd, chip);
3082 if (status & NAND_STATUS_FAIL)
3083 return -EIO;
3084 return 0;
3085}
3086
3087/**
3088 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3089 * @mtd: MTD device structure
3090 * @chip: nand chip info structure
3091 * @addr: feature address.
3092 * @subfeature_param: the subfeature parameters, a four bytes array.
3093 */
3094static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3095 int addr, uint8_t *subfeature_param)
3096{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003097 int i;
3098
David Mosbergerd914c932013-05-29 15:30:13 +03003099 if (!chip->onfi_version ||
3100 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3101 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003102 return -EINVAL;
3103
Huang Shijie7db03ec2012-09-13 14:57:52 +08003104 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003105 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3106 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003107 return 0;
3108}
3109
3110/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003111 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003112 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003113 */
3114static int nand_suspend(struct mtd_info *mtd)
3115{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003116 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003117}
3118
3119/**
3120 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003121 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003122 */
3123static void nand_resume(struct mtd_info *mtd)
3124{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003125 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003126
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003127 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003128 nand_release_device(mtd);
3129 else
Brian Norrisd0370212011-07-19 10:06:08 -07003130 pr_err("%s called for a chip which is not in suspended state\n",
3131 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003132}
3133
Scott Branden72ea4032014-11-20 11:18:05 -08003134/**
3135 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3136 * prevent further operations
3137 * @mtd: MTD device structure
3138 */
3139static void nand_shutdown(struct mtd_info *mtd)
3140{
Brian Norris9ca641b2015-11-09 16:37:28 -08003141 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003142}
3143
Brian Norris8b6e50c2011-05-25 14:59:01 -07003144/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003145static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003148 if (!chip->chip_delay)
3149 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
3151 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003152 if (chip->cmdfunc == NULL)
3153 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003156 if (chip->waitfunc == NULL)
3157 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003159 if (!chip->select_chip)
3160 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003161
Huang Shijie4204ccc2013-08-16 10:10:07 +08003162 /* set for ONFI nand */
3163 if (!chip->onfi_set_features)
3164 chip->onfi_set_features = nand_onfi_set_features;
3165 if (!chip->onfi_get_features)
3166 chip->onfi_get_features = nand_onfi_get_features;
3167
Brian Norris68e80782013-07-18 01:17:02 -07003168 /* If called twice, pointers that depend on busw may need to be reset */
3169 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003170 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3171 if (!chip->read_word)
3172 chip->read_word = nand_read_word;
3173 if (!chip->block_bad)
3174 chip->block_bad = nand_block_bad;
3175 if (!chip->block_markbad)
3176 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003177 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003178 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003179 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3180 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003181 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003182 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003183 if (!chip->scan_bbt)
3184 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003185
3186 if (!chip->controller) {
3187 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003188 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003189 }
3190
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003191}
3192
Brian Norris8b6e50c2011-05-25 14:59:01 -07003193/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003194static void sanitize_string(uint8_t *s, size_t len)
3195{
3196 ssize_t i;
3197
Brian Norris8b6e50c2011-05-25 14:59:01 -07003198 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003199 s[len - 1] = 0;
3200
Brian Norris8b6e50c2011-05-25 14:59:01 -07003201 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003202 for (i = 0; i < len - 1; i++) {
3203 if (s[i] < ' ' || s[i] > 127)
3204 s[i] = '?';
3205 }
3206
Brian Norris8b6e50c2011-05-25 14:59:01 -07003207 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003208 strim(s);
3209}
3210
3211static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3212{
3213 int i;
3214 while (len--) {
3215 crc ^= *p++ << 8;
3216 for (i = 0; i < 8; i++)
3217 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3218 }
3219
3220 return crc;
3221}
3222
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003223/* Parse the Extended Parameter Page. */
3224static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3225 struct nand_chip *chip, struct nand_onfi_params *p)
3226{
3227 struct onfi_ext_param_page *ep;
3228 struct onfi_ext_section *s;
3229 struct onfi_ext_ecc_info *ecc;
3230 uint8_t *cursor;
3231 int ret = -EINVAL;
3232 int len;
3233 int i;
3234
3235 len = le16_to_cpu(p->ext_param_page_length) * 16;
3236 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003237 if (!ep)
3238 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003239
3240 /* Send our own NAND_CMD_PARAM. */
3241 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3242
3243 /* Use the Change Read Column command to skip the ONFI param pages. */
3244 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3245 sizeof(*p) * p->num_of_param_pages , -1);
3246
3247 /* Read out the Extended Parameter Page. */
3248 chip->read_buf(mtd, (uint8_t *)ep, len);
3249 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3250 != le16_to_cpu(ep->crc))) {
3251 pr_debug("fail in the CRC.\n");
3252 goto ext_out;
3253 }
3254
3255 /*
3256 * Check the signature.
3257 * Do not strictly follow the ONFI spec, maybe changed in future.
3258 */
3259 if (strncmp(ep->sig, "EPPS", 4)) {
3260 pr_debug("The signature is invalid.\n");
3261 goto ext_out;
3262 }
3263
3264 /* find the ECC section. */
3265 cursor = (uint8_t *)(ep + 1);
3266 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3267 s = ep->sections + i;
3268 if (s->type == ONFI_SECTION_TYPE_2)
3269 break;
3270 cursor += s->length * 16;
3271 }
3272 if (i == ONFI_EXT_SECTION_MAX) {
3273 pr_debug("We can not find the ECC section.\n");
3274 goto ext_out;
3275 }
3276
3277 /* get the info we want. */
3278 ecc = (struct onfi_ext_ecc_info *)cursor;
3279
Brian Norris4ae7d222013-09-16 18:20:21 -07003280 if (!ecc->codeword_size) {
3281 pr_debug("Invalid codeword size\n");
3282 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003283 }
3284
Brian Norris4ae7d222013-09-16 18:20:21 -07003285 chip->ecc_strength_ds = ecc->ecc_bits;
3286 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003287 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003288
3289ext_out:
3290 kfree(ep);
3291 return ret;
3292}
3293
Brian Norris8429bb32013-12-03 15:51:09 -08003294static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3295{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003296 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003297 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3298
3299 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3300 feature);
3301}
3302
3303/*
3304 * Configure chip properties from Micron vendor-specific ONFI table
3305 */
3306static void nand_onfi_detect_micron(struct nand_chip *chip,
3307 struct nand_onfi_params *p)
3308{
3309 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3310
3311 if (le16_to_cpu(p->vendor_revision) < 1)
3312 return;
3313
3314 chip->read_retries = micron->read_retry_options;
3315 chip->setup_read_retry = nand_setup_read_retry_micron;
3316}
3317
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003318/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003319 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003320 */
3321static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003322 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003323{
3324 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003325 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003326 int val;
3327
Brian Norris7854d3f2011-06-23 14:12:08 -07003328 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003329 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3330 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3331 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3332 return 0;
3333
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003334 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3335 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003336 for (j = 0; j < sizeof(*p); j++)
3337 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003338 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3339 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003340 break;
3341 }
3342 }
3343
Brian Norrisc7f23a72013-08-13 10:51:55 -07003344 if (i == 3) {
3345 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003346 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003347 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003348
Brian Norris8b6e50c2011-05-25 14:59:01 -07003349 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003350 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003351 if (val & (1 << 5))
3352 chip->onfi_version = 23;
3353 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003354 chip->onfi_version = 22;
3355 else if (val & (1 << 3))
3356 chip->onfi_version = 21;
3357 else if (val & (1 << 2))
3358 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003359 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003360 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003361
3362 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003363 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003364 return 0;
3365 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003366
3367 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3368 sanitize_string(p->model, sizeof(p->model));
3369 if (!mtd->name)
3370 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003371
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003372 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003373
3374 /*
3375 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3376 * (don't ask me who thought of this...). MTD assumes that these
3377 * dimensions will be power-of-2, so just truncate the remaining area.
3378 */
3379 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3380 mtd->erasesize *= mtd->writesize;
3381
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003382 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003383
3384 /* See erasesize comment */
3385 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003386 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003387 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003388
3389 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003390 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003391 else
3392 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003393
Huang Shijie10c86ba2013-05-17 11:17:26 +08003394 if (p->ecc_bits != 0xff) {
3395 chip->ecc_strength_ds = p->ecc_bits;
3396 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003397 } else if (chip->onfi_version >= 21 &&
3398 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3399
3400 /*
3401 * The nand_flash_detect_ext_param_page() uses the
3402 * Change Read Column command which maybe not supported
3403 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3404 * now. We do not replace user supplied command function.
3405 */
3406 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3407 chip->cmdfunc = nand_command_lp;
3408
3409 /* The Extended Parameter Page is supported since ONFI 2.1. */
3410 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003411 pr_warn("Failed to detect ONFI extended param page\n");
3412 } else {
3413 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003414 }
3415
Brian Norris8429bb32013-12-03 15:51:09 -08003416 if (p->jedec_id == NAND_MFR_MICRON)
3417 nand_onfi_detect_micron(chip, p);
3418
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003419 return 1;
3420}
3421
3422/*
Huang Shijie91361812014-02-21 13:39:40 +08003423 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3424 */
3425static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3426 int *busw)
3427{
3428 struct nand_jedec_params *p = &chip->jedec_params;
3429 struct jedec_ecc_info *ecc;
3430 int val;
3431 int i, j;
3432
3433 /* Try JEDEC for unknown chip or LP */
3434 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3435 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3436 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3437 chip->read_byte(mtd) != 'C')
3438 return 0;
3439
3440 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3441 for (i = 0; i < 3; i++) {
3442 for (j = 0; j < sizeof(*p); j++)
3443 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3444
3445 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3446 le16_to_cpu(p->crc))
3447 break;
3448 }
3449
3450 if (i == 3) {
3451 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3452 return 0;
3453 }
3454
3455 /* Check version */
3456 val = le16_to_cpu(p->revision);
3457 if (val & (1 << 2))
3458 chip->jedec_version = 10;
3459 else if (val & (1 << 1))
3460 chip->jedec_version = 1; /* vendor specific version */
3461
3462 if (!chip->jedec_version) {
3463 pr_info("unsupported JEDEC version: %d\n", val);
3464 return 0;
3465 }
3466
3467 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3468 sanitize_string(p->model, sizeof(p->model));
3469 if (!mtd->name)
3470 mtd->name = p->model;
3471
3472 mtd->writesize = le32_to_cpu(p->byte_per_page);
3473
3474 /* Please reference to the comment for nand_flash_detect_onfi. */
3475 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3476 mtd->erasesize *= mtd->writesize;
3477
3478 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3479
3480 /* Please reference to the comment for nand_flash_detect_onfi. */
3481 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3482 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3483 chip->bits_per_cell = p->bits_per_cell;
3484
3485 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3486 *busw = NAND_BUSWIDTH_16;
3487 else
3488 *busw = 0;
3489
3490 /* ECC info */
3491 ecc = &p->ecc_info[0];
3492
3493 if (ecc->codeword_size >= 9) {
3494 chip->ecc_strength_ds = ecc->ecc_bits;
3495 chip->ecc_step_ds = 1 << ecc->codeword_size;
3496 } else {
3497 pr_warn("Invalid codeword size\n");
3498 }
3499
3500 return 1;
3501}
3502
3503/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003504 * nand_id_has_period - Check if an ID string has a given wraparound period
3505 * @id_data: the ID string
3506 * @arrlen: the length of the @id_data array
3507 * @period: the period of repitition
3508 *
3509 * Check if an ID string is repeated within a given sequence of bytes at
3510 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003511 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003512 * if the repetition has a period of @period; otherwise, returns zero.
3513 */
3514static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3515{
3516 int i, j;
3517 for (i = 0; i < period; i++)
3518 for (j = i + period; j < arrlen; j += period)
3519 if (id_data[i] != id_data[j])
3520 return 0;
3521 return 1;
3522}
3523
3524/*
3525 * nand_id_len - Get the length of an ID string returned by CMD_READID
3526 * @id_data: the ID string
3527 * @arrlen: the length of the @id_data array
3528
3529 * Returns the length of the ID string, according to known wraparound/trailing
3530 * zero patterns. If no pattern exists, returns the length of the array.
3531 */
3532static int nand_id_len(u8 *id_data, int arrlen)
3533{
3534 int last_nonzero, period;
3535
3536 /* Find last non-zero byte */
3537 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3538 if (id_data[last_nonzero])
3539 break;
3540
3541 /* All zeros */
3542 if (last_nonzero < 0)
3543 return 0;
3544
3545 /* Calculate wraparound period */
3546 for (period = 1; period < arrlen; period++)
3547 if (nand_id_has_period(id_data, arrlen, period))
3548 break;
3549
3550 /* There's a repeated pattern */
3551 if (period < arrlen)
3552 return period;
3553
3554 /* There are trailing zeros */
3555 if (last_nonzero < arrlen - 1)
3556 return last_nonzero + 1;
3557
3558 /* No pattern detected */
3559 return arrlen;
3560}
3561
Huang Shijie7db906b2013-09-25 14:58:11 +08003562/* Extract the bits of per cell from the 3rd byte of the extended ID */
3563static int nand_get_bits_per_cell(u8 cellinfo)
3564{
3565 int bits;
3566
3567 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3568 bits >>= NAND_CI_CELLTYPE_SHIFT;
3569 return bits + 1;
3570}
3571
Brian Norrise3b88bd2012-09-24 20:40:52 -07003572/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003573 * Many new NAND share similar device ID codes, which represent the size of the
3574 * chip. The rest of the parameters must be decoded according to generic or
3575 * manufacturer-specific "extended ID" decoding patterns.
3576 */
3577static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3578 u8 id_data[8], int *busw)
3579{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003580 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003581 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003582 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003583 /* The 4th id byte is the important one */
3584 extid = id_data[3];
3585
Brian Norrise3b88bd2012-09-24 20:40:52 -07003586 id_len = nand_id_len(id_data, 8);
3587
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003588 /*
3589 * Field definitions are in the following datasheets:
3590 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003591 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003592 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003593 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003594 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3595 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003596 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003597 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003598 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003599 /* Calc pagesize */
3600 mtd->writesize = 2048 << (extid & 0x03);
3601 extid >>= 2;
3602 /* Calc oobsize */
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003603 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003604 case 1:
3605 mtd->oobsize = 128;
3606 break;
3607 case 2:
3608 mtd->oobsize = 218;
3609 break;
3610 case 3:
3611 mtd->oobsize = 400;
3612 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003613 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003614 mtd->oobsize = 436;
3615 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003616 case 5:
3617 mtd->oobsize = 512;
3618 break;
3619 case 6:
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003620 mtd->oobsize = 640;
3621 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003622 case 7:
3623 default: /* Other cases are "reserved" (unknown) */
3624 mtd->oobsize = 1024;
3625 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003626 }
3627 extid >>= 2;
3628 /* Calc blocksize */
3629 mtd->erasesize = (128 * 1024) <<
3630 (((extid >> 1) & 0x04) | (extid & 0x03));
3631 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003632 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003633 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003634 unsigned int tmp;
3635
3636 /* Calc pagesize */
3637 mtd->writesize = 2048 << (extid & 0x03);
3638 extid >>= 2;
3639 /* Calc oobsize */
3640 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3641 case 0:
3642 mtd->oobsize = 128;
3643 break;
3644 case 1:
3645 mtd->oobsize = 224;
3646 break;
3647 case 2:
3648 mtd->oobsize = 448;
3649 break;
3650 case 3:
3651 mtd->oobsize = 64;
3652 break;
3653 case 4:
3654 mtd->oobsize = 32;
3655 break;
3656 case 5:
3657 mtd->oobsize = 16;
3658 break;
3659 default:
3660 mtd->oobsize = 640;
3661 break;
3662 }
3663 extid >>= 2;
3664 /* Calc blocksize */
3665 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3666 if (tmp < 0x03)
3667 mtd->erasesize = (128 * 1024) << tmp;
3668 else if (tmp == 0x03)
3669 mtd->erasesize = 768 * 1024;
3670 else
3671 mtd->erasesize = (64 * 1024) << tmp;
3672 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003673 } else {
3674 /* Calc pagesize */
3675 mtd->writesize = 1024 << (extid & 0x03);
3676 extid >>= 2;
3677 /* Calc oobsize */
3678 mtd->oobsize = (8 << (extid & 0x01)) *
3679 (mtd->writesize >> 9);
3680 extid >>= 2;
3681 /* Calc blocksize. Blocksize is multiples of 64KiB */
3682 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3683 extid >>= 2;
3684 /* Get buswidth information */
3685 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003686
3687 /*
3688 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3689 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3690 * follows:
3691 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3692 * 110b -> 24nm
3693 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3694 */
3695 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003696 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003697 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3698 !(id_data[4] & 0x80) /* !BENAND */) {
3699 mtd->oobsize = 32 * mtd->writesize >> 9;
3700 }
3701
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003702 }
3703}
3704
3705/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003706 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3707 * decodes a matching ID table entry and assigns the MTD size parameters for
3708 * the chip.
3709 */
3710static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3711 struct nand_flash_dev *type, u8 id_data[8],
3712 int *busw)
3713{
3714 int maf_id = id_data[0];
3715
3716 mtd->erasesize = type->erasesize;
3717 mtd->writesize = type->pagesize;
3718 mtd->oobsize = mtd->writesize / 32;
3719 *busw = type->options & NAND_BUSWIDTH_16;
3720
Huang Shijie1c195e92013-09-25 14:58:12 +08003721 /* All legacy ID NAND are small-page, SLC */
3722 chip->bits_per_cell = 1;
3723
Brian Norrisf23a4812012-09-24 20:40:51 -07003724 /*
3725 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3726 * some Spansion chips have erasesize that conflicts with size
3727 * listed in nand_ids table.
3728 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3729 */
3730 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3731 && id_data[6] == 0x00 && id_data[7] == 0x00
3732 && mtd->writesize == 512) {
3733 mtd->erasesize = 128 * 1024;
3734 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3735 }
3736}
3737
3738/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003739 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3740 * heuristic patterns using various detected parameters (e.g., manufacturer,
3741 * page size, cell-type information).
3742 */
3743static void nand_decode_bbm_options(struct mtd_info *mtd,
3744 struct nand_chip *chip, u8 id_data[8])
3745{
3746 int maf_id = id_data[0];
3747
3748 /* Set the bad block position */
3749 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3750 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3751 else
3752 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3753
3754 /*
3755 * Bad block marker is stored in the last page of each block on Samsung
3756 * and Hynix MLC devices; stored in first two pages of each block on
3757 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3758 * AMD/Spansion, and Macronix. All others scan only the first page.
3759 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003760 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003761 (maf_id == NAND_MFR_SAMSUNG ||
3762 maf_id == NAND_MFR_HYNIX))
3763 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003764 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003765 (maf_id == NAND_MFR_SAMSUNG ||
3766 maf_id == NAND_MFR_HYNIX ||
3767 maf_id == NAND_MFR_TOSHIBA ||
3768 maf_id == NAND_MFR_AMD ||
3769 maf_id == NAND_MFR_MACRONIX)) ||
3770 (mtd->writesize == 2048 &&
3771 maf_id == NAND_MFR_MICRON))
3772 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3773}
3774
Huang Shijieec6e87e2013-03-15 11:01:00 +08003775static inline bool is_full_id_nand(struct nand_flash_dev *type)
3776{
3777 return type->id_len;
3778}
3779
3780static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3781 struct nand_flash_dev *type, u8 *id_data, int *busw)
3782{
3783 if (!strncmp(type->id, id_data, type->id_len)) {
3784 mtd->writesize = type->pagesize;
3785 mtd->erasesize = type->erasesize;
3786 mtd->oobsize = type->oobsize;
3787
Huang Shijie7db906b2013-09-25 14:58:11 +08003788 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003789 chip->chipsize = (uint64_t)type->chipsize << 20;
3790 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003791 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3792 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003793 chip->onfi_timing_mode_default =
3794 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003795
3796 *busw = type->options & NAND_BUSWIDTH_16;
3797
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003798 if (!mtd->name)
3799 mtd->name = type->name;
3800
Huang Shijieec6e87e2013-03-15 11:01:00 +08003801 return true;
3802 }
3803 return false;
3804}
3805
Brian Norris7e74c2d2012-09-24 20:40:49 -07003806/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003807 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003808 */
3809static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003810 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003811 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003812 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003813{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003814 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003815 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003816 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003819 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Karl Beldanef89a882008-09-15 14:37:29 +02003821 /*
3822 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003823 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003824 */
3825 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003828 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003831 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003832 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Brian Norris8b6e50c2011-05-25 14:59:01 -07003834 /*
3835 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003836 * interface concerns can cause random data which looks like a
3837 * possibly credible NAND flash to appear. If the two results do
3838 * not match, ignore the device completely.
3839 */
3840
3841 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3842
Brian Norris4aef9b72012-09-24 20:40:48 -07003843 /* Read entire ID string */
3844 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003845 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003846
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003847 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003848 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003849 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003850 return ERR_PTR(-ENODEV);
3851 }
3852
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003853 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003854 type = nand_flash_ids;
3855
Huang Shijieec6e87e2013-03-15 11:01:00 +08003856 for (; type->name != NULL; type++) {
3857 if (is_full_id_nand(type)) {
3858 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3859 goto ident_done;
3860 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003861 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003862 }
3863 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003864
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003865 chip->onfi_version = 0;
3866 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003867 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003868 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003869 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003870
3871 /* Check if the chip is JEDEC compliant */
3872 if (nand_flash_detect_jedec(mtd, chip, &busw))
3873 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003874 }
3875
David Woodhouse5e81e882010-02-26 18:32:56 +00003876 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003877 return ERR_PTR(-ENODEV);
3878
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02003879 if (!mtd->name)
3880 mtd->name = type->name;
3881
Adrian Hunter69423d92008-12-10 13:37:21 +00003882 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003883
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02003884 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003885 /* Decode parameters from extended ID */
3886 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003887 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003888 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003889 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003890 /* Get chip options */
3891 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003892
Brian Norris8b6e50c2011-05-25 14:59:01 -07003893 /*
3894 * Check if chip is not a Samsung device. Do not clear the
3895 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003896 */
3897 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3898 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3899ident_done:
3900
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003901 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003902 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003903 if (nand_manuf_ids[maf_idx].id == *maf_id)
3904 break;
3905 }
3906
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003907 if (chip->options & NAND_BUSWIDTH_AUTO) {
3908 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3909 chip->options |= busw;
3910 nand_set_defaults(chip, busw);
3911 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3912 /*
3913 * Check, if buswidth is correct. Hardware drivers should set
3914 * chip correct!
3915 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003916 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3917 *maf_id, *dev_id);
3918 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3919 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003920 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3921 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003922 return ERR_PTR(-EINVAL);
3923 }
3924
Brian Norris7e74c2d2012-09-24 20:40:49 -07003925 nand_decode_bbm_options(mtd, chip, id_data);
3926
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003927 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003928 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003929 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003930 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003931
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003932 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003933 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003934 if (chip->chipsize & 0xffffffff)
3935 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003936 else {
3937 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3938 chip->chip_shift += 32 - 1;
3939 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003940
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003941 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003942 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003943
Brian Norris8b6e50c2011-05-25 14:59:01 -07003944 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003945 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3946 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003947
Ezequiel Garcia20171642013-11-25 08:30:31 -03003948 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3949 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003950
3951 if (chip->onfi_version)
3952 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3953 chip->onfi_params.model);
3954 else if (chip->jedec_version)
3955 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3956 chip->jedec_params.model);
3957 else
3958 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3959 type->name);
3960
Rafał Miłecki3755a992014-10-21 00:01:04 +02003961 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003962 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003963 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003964 return type;
3965}
3966
Boris Brezillond48f62b2016-04-01 14:54:32 +02003967static const char * const nand_ecc_modes[] = {
3968 [NAND_ECC_NONE] = "none",
3969 [NAND_ECC_SOFT] = "soft",
3970 [NAND_ECC_HW] = "hw",
3971 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
3972 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02003973};
3974
3975static int of_get_nand_ecc_mode(struct device_node *np)
3976{
3977 const char *pm;
3978 int err, i;
3979
3980 err = of_property_read_string(np, "nand-ecc-mode", &pm);
3981 if (err < 0)
3982 return err;
3983
3984 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
3985 if (!strcasecmp(pm, nand_ecc_modes[i]))
3986 return i;
3987
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02003988 /*
3989 * For backward compatibility we support few obsoleted values that don't
3990 * have their mappings into nand_ecc_modes_t anymore (they were merged
3991 * with other enums).
3992 */
3993 if (!strcasecmp(pm, "soft_bch"))
3994 return NAND_ECC_SOFT;
3995
Boris Brezillond48f62b2016-04-01 14:54:32 +02003996 return -ENODEV;
3997}
3998
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02003999static const char * const nand_ecc_algos[] = {
4000 [NAND_ECC_HAMMING] = "hamming",
4001 [NAND_ECC_BCH] = "bch",
4002};
4003
Boris Brezillond48f62b2016-04-01 14:54:32 +02004004static int of_get_nand_ecc_algo(struct device_node *np)
4005{
4006 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004007 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004008
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004009 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4010 if (!err) {
4011 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4012 if (!strcasecmp(pm, nand_ecc_algos[i]))
4013 return i;
4014 return -ENODEV;
4015 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004016
4017 /*
4018 * For backward compatibility we also read "nand-ecc-mode" checking
4019 * for some obsoleted values that were specifying ECC algorithm.
4020 */
4021 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4022 if (err < 0)
4023 return err;
4024
4025 if (!strcasecmp(pm, "soft"))
4026 return NAND_ECC_HAMMING;
4027 else if (!strcasecmp(pm, "soft_bch"))
4028 return NAND_ECC_BCH;
4029
4030 return -ENODEV;
4031}
4032
4033static int of_get_nand_ecc_step_size(struct device_node *np)
4034{
4035 int ret;
4036 u32 val;
4037
4038 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4039 return ret ? ret : val;
4040}
4041
4042static int of_get_nand_ecc_strength(struct device_node *np)
4043{
4044 int ret;
4045 u32 val;
4046
4047 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4048 return ret ? ret : val;
4049}
4050
4051static int of_get_nand_bus_width(struct device_node *np)
4052{
4053 u32 val;
4054
4055 if (of_property_read_u32(np, "nand-bus-width", &val))
4056 return 8;
4057
4058 switch (val) {
4059 case 8:
4060 case 16:
4061 return val;
4062 default:
4063 return -EIO;
4064 }
4065}
4066
4067static bool of_get_nand_on_flash_bbt(struct device_node *np)
4068{
4069 return of_property_read_bool(np, "nand-on-flash-bbt");
4070}
4071
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004072static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004073{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004074 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004075 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004076
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004077 if (!dn)
4078 return 0;
4079
Brian Norris5844fee2015-01-23 00:22:27 -08004080 if (of_get_nand_bus_width(dn) == 16)
4081 chip->options |= NAND_BUSWIDTH_16;
4082
4083 if (of_get_nand_on_flash_bbt(dn))
4084 chip->bbt_options |= NAND_BBT_USE_FLASH;
4085
4086 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004087 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004088 ecc_strength = of_get_nand_ecc_strength(dn);
4089 ecc_step = of_get_nand_ecc_step_size(dn);
4090
4091 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4092 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4093 pr_err("must set both strength and step size in DT\n");
4094 return -EINVAL;
4095 }
4096
4097 if (ecc_mode >= 0)
4098 chip->ecc.mode = ecc_mode;
4099
Rafał Miłecki79082452016-03-23 11:19:02 +01004100 if (ecc_algo >= 0)
4101 chip->ecc.algo = ecc_algo;
4102
Brian Norris5844fee2015-01-23 00:22:27 -08004103 if (ecc_strength >= 0)
4104 chip->ecc.strength = ecc_strength;
4105
4106 if (ecc_step > 0)
4107 chip->ecc.size = ecc_step;
4108
4109 return 0;
4110}
4111
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004112/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004113 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004114 * @mtd: MTD device structure
4115 * @maxchips: number of chips to scan for
4116 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004117 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004118 * This is the first phase of the normal nand_scan() function. It reads the
4119 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004120 *
4121 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004122int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4123 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004124{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004125 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004126 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004127 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004128 int ret;
4129
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004130 ret = nand_dt_init(chip);
4131 if (ret)
4132 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004133
Brian Norrisf7a8e382016-01-05 10:39:45 -08004134 if (!mtd->name && mtd->dev.parent)
4135 mtd->name = dev_name(mtd->dev.parent);
4136
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004137 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4138 /*
4139 * Default functions assigned for chip_select() and
4140 * cmdfunc() both expect cmd_ctrl() to be populated,
4141 * so we need to check that that's the case
4142 */
4143 pr_err("chip.cmd_ctrl() callback is not provided");
4144 return -EINVAL;
4145 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004146 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004147 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004148
4149 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004150 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4151 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004152
4153 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004154 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004155 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004156 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004157 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
4159
Huang Shijie07300162012-11-09 16:23:45 +08004160 chip->select_chip(mtd, -1);
4161
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004162 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004163 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004164 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004165 /* See comment in nand_get_flash_type for reset */
4166 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004168 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004170 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004171 nand_dev_id != chip->read_byte(mtd)) {
4172 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 break;
Huang Shijie07300162012-11-09 16:23:45 +08004174 }
4175 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
4177 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004178 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004181 chip->numchips = i;
4182 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
David Woodhouse3b85c322006-09-25 17:06:53 +01004184 return 0;
4185}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004186EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004187
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004188static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4189{
4190 struct nand_chip *chip = mtd_to_nand(mtd);
4191 struct nand_ecc_ctrl *ecc = &chip->ecc;
4192
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004193 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004194 return -EINVAL;
4195
4196 switch (ecc->algo) {
4197 case NAND_ECC_HAMMING:
4198 ecc->calculate = nand_calculate_ecc;
4199 ecc->correct = nand_correct_data;
4200 ecc->read_page = nand_read_page_swecc;
4201 ecc->read_subpage = nand_read_subpage;
4202 ecc->write_page = nand_write_page_swecc;
4203 ecc->read_page_raw = nand_read_page_raw;
4204 ecc->write_page_raw = nand_write_page_raw;
4205 ecc->read_oob = nand_read_oob_std;
4206 ecc->write_oob = nand_write_oob_std;
4207 if (!ecc->size)
4208 ecc->size = 256;
4209 ecc->bytes = 3;
4210 ecc->strength = 1;
4211 return 0;
4212 case NAND_ECC_BCH:
4213 if (!mtd_nand_has_bch()) {
4214 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4215 return -EINVAL;
4216 }
4217 ecc->calculate = nand_bch_calculate_ecc;
4218 ecc->correct = nand_bch_correct_data;
4219 ecc->read_page = nand_read_page_swecc;
4220 ecc->read_subpage = nand_read_subpage;
4221 ecc->write_page = nand_write_page_swecc;
4222 ecc->read_page_raw = nand_read_page_raw;
4223 ecc->write_page_raw = nand_write_page_raw;
4224 ecc->read_oob = nand_read_oob_std;
4225 ecc->write_oob = nand_write_oob_std;
4226 /*
4227 * Board driver should supply ecc.size and ecc.strength
4228 * values to select how many bits are correctable.
4229 * Otherwise, default to 4 bits for large page devices.
4230 */
4231 if (!ecc->size && (mtd->oobsize >= 64)) {
4232 ecc->size = 512;
4233 ecc->strength = 4;
4234 }
4235
4236 /*
4237 * if no ecc placement scheme was provided pickup the default
4238 * large page one.
4239 */
4240 if (!mtd->ooblayout) {
4241 /* handle large page devices only */
4242 if (mtd->oobsize < 64) {
4243 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4244 return -EINVAL;
4245 }
4246
4247 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
4248 }
4249
4250 /* See nand_bch_init() for details. */
4251 ecc->bytes = 0;
4252 ecc->priv = nand_bch_init(mtd);
4253 if (!ecc->priv) {
4254 WARN(1, "BCH ECC initialization failed!\n");
4255 return -EINVAL;
4256 }
4257 return 0;
4258 default:
4259 WARN(1, "Unsupported ECC algorithm!\n");
4260 return -EINVAL;
4261 }
4262}
4263
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004264/*
4265 * Check if the chip configuration meet the datasheet requirements.
4266
4267 * If our configuration corrects A bits per B bytes and the minimum
4268 * required correction level is X bits per Y bytes, then we must ensure
4269 * both of the following are true:
4270 *
4271 * (1) A / B >= X / Y
4272 * (2) A >= X
4273 *
4274 * Requirement (1) ensures we can correct for the required bitflip density.
4275 * Requirement (2) ensures we can correct even when all bitflips are clumped
4276 * in the same sector.
4277 */
4278static bool nand_ecc_strength_good(struct mtd_info *mtd)
4279{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004280 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004281 struct nand_ecc_ctrl *ecc = &chip->ecc;
4282 int corr, ds_corr;
4283
4284 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4285 /* Not enough information */
4286 return true;
4287
4288 /*
4289 * We get the number of corrected bits per page to compare
4290 * the correction density.
4291 */
4292 corr = (mtd->writesize * ecc->strength) / ecc->size;
4293 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4294
4295 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4296}
David Woodhouse3b85c322006-09-25 17:06:53 +01004297
4298/**
4299 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004300 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004301 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004302 * This is the second phase of the normal nand_scan() function. It fills out
4303 * all the uninitialized function pointers with the defaults and scans for a
4304 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004305 */
4306int nand_scan_tail(struct mtd_info *mtd)
4307{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004308 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004309 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004310 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004311 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004312
Brian Norrise2414f42012-02-06 13:44:00 -08004313 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004314 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4315 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4316 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004317
Huang Shijief02ea4e2014-01-13 14:27:12 +08004318 if (!(chip->options & NAND_OWN_BUFFERS)) {
4319 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4320 + mtd->oobsize * 3, GFP_KERNEL);
4321 if (!nbuf)
4322 return -ENOMEM;
4323 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4324 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4325 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4326
4327 chip->buffers = nbuf;
4328 } else {
4329 if (!chip->buffers)
4330 return -ENOMEM;
4331 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004332
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004333 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004334 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004335
4336 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004337 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004338 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004339 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004340 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004341 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004344 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 break;
4346 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004347 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004348 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004351 WARN(1, "No oob scheme defined for oobsize %d\n",
4352 mtd->oobsize);
4353 ret = -EINVAL;
4354 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 }
4356 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004357
David Woodhouse956e9442006-09-25 17:12:39 +01004358 if (!chip->write_page)
4359 chip->write_page = nand_write_page;
4360
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004361 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004362 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004363 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004364 */
David Woodhouse956e9442006-09-25 17:12:39 +01004365
Huang Shijie97de79e02013-10-18 14:20:53 +08004366 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004367 case NAND_ECC_HW_OOB_FIRST:
4368 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004369 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004370 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4371 ret = -EINVAL;
4372 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004373 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004374 if (!ecc->read_page)
4375 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004376
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004377 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004378 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004379 if (!ecc->read_page)
4380 ecc->read_page = nand_read_page_hwecc;
4381 if (!ecc->write_page)
4382 ecc->write_page = nand_write_page_hwecc;
4383 if (!ecc->read_page_raw)
4384 ecc->read_page_raw = nand_read_page_raw;
4385 if (!ecc->write_page_raw)
4386 ecc->write_page_raw = nand_write_page_raw;
4387 if (!ecc->read_oob)
4388 ecc->read_oob = nand_read_oob_std;
4389 if (!ecc->write_oob)
4390 ecc->write_oob = nand_write_oob_std;
4391 if (!ecc->read_subpage)
4392 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004393 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004394 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004395
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004396 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004397 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4398 (!ecc->read_page ||
4399 ecc->read_page == nand_read_page_hwecc ||
4400 !ecc->write_page ||
4401 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004402 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4403 ret = -EINVAL;
4404 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004405 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004406 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004407 if (!ecc->read_page)
4408 ecc->read_page = nand_read_page_syndrome;
4409 if (!ecc->write_page)
4410 ecc->write_page = nand_write_page_syndrome;
4411 if (!ecc->read_page_raw)
4412 ecc->read_page_raw = nand_read_page_raw_syndrome;
4413 if (!ecc->write_page_raw)
4414 ecc->write_page_raw = nand_write_page_raw_syndrome;
4415 if (!ecc->read_oob)
4416 ecc->read_oob = nand_read_oob_syndrome;
4417 if (!ecc->write_oob)
4418 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004419
Huang Shijie97de79e02013-10-18 14:20:53 +08004420 if (mtd->writesize >= ecc->size) {
4421 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004422 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4423 ret = -EINVAL;
4424 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004425 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004426 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004427 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004428 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4429 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004430 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004431 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004433 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004434 ret = nand_set_ecc_soft_ops(mtd);
4435 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004436 ret = -EINVAL;
4437 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004438 }
4439 break;
4440
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004441 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004442 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004443 ecc->read_page = nand_read_page_raw;
4444 ecc->write_page = nand_write_page_raw;
4445 ecc->read_oob = nand_read_oob_std;
4446 ecc->read_page_raw = nand_read_page_raw;
4447 ecc->write_page_raw = nand_write_page_raw;
4448 ecc->write_oob = nand_write_oob_std;
4449 ecc->size = mtd->writesize;
4450 ecc->bytes = 0;
4451 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004455 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4456 ret = -EINVAL;
4457 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Brian Norris9ce244b2011-08-30 18:45:37 -07004460 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004461 if (!ecc->read_oob_raw)
4462 ecc->read_oob_raw = ecc->read_oob;
4463 if (!ecc->write_oob_raw)
4464 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004465
Boris Brezillon846031d2016-02-03 20:11:00 +01004466 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004467 mtd->ecc_strength = ecc->strength;
4468 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004469
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004470 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004471 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004472 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004473 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004474 ecc->steps = mtd->writesize / ecc->size;
4475 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004476 WARN(1, "Invalid ECC parameters\n");
4477 ret = -EINVAL;
4478 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004480 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004481
Boris Brezillon846031d2016-02-03 20:11:00 +01004482 /*
4483 * The number of bytes available for a client to place data into
4484 * the out of band area.
4485 */
4486 ret = mtd_ooblayout_count_freebytes(mtd);
4487 if (ret < 0)
4488 ret = 0;
4489
4490 mtd->oobavail = ret;
4491
4492 /* ECC sanity check: warn if it's too weak */
4493 if (!nand_ecc_strength_good(mtd))
4494 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4495 mtd->name);
4496
Brian Norris8b6e50c2011-05-25 14:59:01 -07004497 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004498 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004499 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004500 case 2:
4501 mtd->subpage_sft = 1;
4502 break;
4503 case 4:
4504 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004505 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004506 mtd->subpage_sft = 2;
4507 break;
4508 }
4509 }
4510 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4511
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004512 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004513 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004516 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004518 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304519 switch (ecc->mode) {
4520 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304521 if (chip->page_shift > 9)
4522 chip->options |= NAND_SUBPAGE_READ;
4523 break;
4524
4525 default:
4526 break;
4527 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004530 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004531 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4532 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004533 mtd->_erase = nand_erase;
4534 mtd->_point = NULL;
4535 mtd->_unpoint = NULL;
4536 mtd->_read = nand_read;
4537 mtd->_write = nand_write;
4538 mtd->_panic_write = panic_nand_write;
4539 mtd->_read_oob = nand_read_oob;
4540 mtd->_write_oob = nand_write_oob;
4541 mtd->_sync = nand_sync;
4542 mtd->_lock = NULL;
4543 mtd->_unlock = NULL;
4544 mtd->_suspend = nand_suspend;
4545 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004546 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004547 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004548 mtd->_block_isbad = nand_block_isbad;
4549 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004550 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004552 /*
4553 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4554 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4555 * properly set.
4556 */
4557 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004558 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004560 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004561 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004565 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004566err_free:
4567 if (!(chip->options & NAND_OWN_BUFFERS))
4568 kfree(chip->buffers);
4569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004571EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Brian Norris8b6e50c2011-05-25 14:59:01 -07004573/*
4574 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004575 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004576 * to call us from in-kernel code if the core NAND support is modular.
4577 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004578#ifdef MODULE
4579#define caller_is_module() (1)
4580#else
4581#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004582 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004583#endif
4584
4585/**
4586 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004587 * @mtd: MTD device structure
4588 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004589 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004590 * This fills out all the uninitialized function pointers with the defaults.
4591 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004592 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004593 */
4594int nand_scan(struct mtd_info *mtd, int maxchips)
4595{
4596 int ret;
4597
David Woodhouse5e81e882010-02-26 18:32:56 +00004598 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004599 if (!ret)
4600 ret = nand_scan_tail(mtd);
4601 return ret;
4602}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004603EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004606 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004607 * @mtd: MTD device structure
4608 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004609void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004611 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004613 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004614 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004615 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4616
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004617 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Jesper Juhlfa671642005-11-07 01:01:27 -08004619 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004620 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004621 if (!(chip->options & NAND_OWN_BUFFERS))
4622 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004623
4624 /* Free bad block descriptor memory */
4625 if (chip->badblock_pattern && chip->badblock_pattern->options
4626 & NAND_BBT_DYNAMICSTRUCT)
4627 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
David Woodhousee0c7d762006-05-13 18:07:53 +01004629EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004630
David Woodhousee0c7d762006-05-13 18:07:53 +01004631MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004632MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4633MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004634MODULE_DESCRIPTION("Generic NAND flash driver code");