blob: 1492c12906f6bd3f83bedc05c14f8b7ee7d80963 [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
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200712static void nand_ccs_delay(struct nand_chip *chip)
713{
714 /*
715 * The controller already takes care of waiting for tCCS when the RNDIN
716 * or RNDOUT command is sent, return directly.
717 */
718 if (!(chip->options & NAND_WAIT_TCCS))
719 return;
720
721 /*
722 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
723 * (which should be safe for all NANDs).
724 */
725 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
726 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
727 else
728 ndelay(500);
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/**
732 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700733 * @mtd: MTD device structure
734 * @command: the command to be sent
735 * @column: the column address for this command, -1 if none
736 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200738 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700739 * devices. We don't have the separate regions as we have in the small page
740 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200742static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
743 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100745 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* Emulate NAND_CMD_READOOB */
748 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200749 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 command = NAND_CMD_READ0;
751 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000752
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200753 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400754 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200757 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Serially input address */
760 if (column != -1) {
761 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800762 if (chip->options & NAND_BUSWIDTH_16 &&
763 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200765 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200766 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200767
Brian Norrisf5b88de2016-10-03 09:49:35 -0700768 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200769 if (!nand_opcode_8bits(command))
770 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200773 chip->cmd_ctrl(mtd, page_addr, ctrl);
774 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200775 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200777 if (chip->chipsize > (128 << 20))
778 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200779 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200782 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000783
784 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700785 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100786 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000787 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case NAND_CMD_CACHEDPROG:
791 case NAND_CMD_PAGEPROG:
792 case NAND_CMD_ERASE1:
793 case NAND_CMD_ERASE2:
794 case NAND_CMD_SEQIN:
795 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000796 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200798 case NAND_CMD_RNDIN:
799 nand_ccs_delay(chip);
800 return;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200803 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200805 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200806 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
807 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
808 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
809 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200810 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
811 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return;
813
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200814 case NAND_CMD_RNDOUT:
815 /* No ready / busy check necessary */
816 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
817 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
818 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
819 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200820
821 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200822 return;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200825 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
826 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
827 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
828 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000829
David Woodhousee0c7d762006-05-13 18:07:53 +0100830 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000832 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700834 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100835 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200836 if (!chip->dev_ready) {
837 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000841
Brian Norris8b6e50c2011-05-25 14:59:01 -0700842 /*
843 * Apply this short delay always to ensure that we do wait tWB in
844 * any case on any machine.
845 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100846 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000847
848 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200852 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700853 * @chip: the nand chip descriptor
854 * @mtd: MTD device structure
855 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200856 *
857 * Used when in panic, no locks are taken.
858 */
859static void panic_nand_get_device(struct nand_chip *chip,
860 struct mtd_info *mtd, int new_state)
861{
Brian Norris7854d3f2011-06-23 14:12:08 -0700862 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200863 chip->controller->active = chip;
864 chip->state = new_state;
865}
866
867/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700869 * @mtd: MTD device structure
870 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 *
872 * Get the device and lock it for exclusive access
873 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200874static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800875nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100877 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200878 spinlock_t *lock = &chip->controller->lock;
879 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100880 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200881retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100882 spin_lock(lock);
883
vimal singhb8b3ee92009-07-09 20:41:22 +0530884 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200885 if (!chip->controller->active)
886 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200887
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200888 if (chip->controller->active == chip && chip->state == FL_READY) {
889 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100890 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100891 return 0;
892 }
893 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800894 if (chip->controller->active->state == FL_PM_SUSPENDED) {
895 chip->state = FL_PM_SUSPENDED;
896 spin_unlock(lock);
897 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800898 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100899 }
900 set_current_state(TASK_UNINTERRUPTIBLE);
901 add_wait_queue(wq, &wait);
902 spin_unlock(lock);
903 schedule();
904 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto retry;
906}
907
908/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700909 * panic_nand_wait - [GENERIC] wait until the command is done
910 * @mtd: MTD device structure
911 * @chip: NAND chip structure
912 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200913 *
914 * Wait for command done. This is a helper function for nand_wait used when
915 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400916 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200917 */
918static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
919 unsigned long timeo)
920{
921 int i;
922 for (i = 0; i < timeo; i++) {
923 if (chip->dev_ready) {
924 if (chip->dev_ready(mtd))
925 break;
926 } else {
927 if (chip->read_byte(mtd) & NAND_STATUS_READY)
928 break;
929 }
930 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200931 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200932}
933
934/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700935 * nand_wait - [DEFAULT] wait until the command is done
936 * @mtd: MTD device structure
937 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100939 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700940 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200941static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943
Alex Smithb70af9b2015-10-06 14:52:07 +0100944 int status;
945 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Brian Norris8b6e50c2011-05-25 14:59:01 -0700947 /*
948 * Apply this short delay always to ensure that we do wait tWB in any
949 * case on any machine.
950 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100951 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200953 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200955 if (in_interrupt() || oops_in_progress)
956 panic_nand_wait(mtd, chip, timeo);
957 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800958 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100959 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200960 if (chip->dev_ready) {
961 if (chip->dev_ready(mtd))
962 break;
963 } else {
964 if (chip->read_byte(mtd) & NAND_STATUS_READY)
965 break;
966 }
967 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100968 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800970
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200971 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100972 /* This can happen if in case of timeout or buggy dev_ready */
973 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return status;
975}
976
977/**
Boris Brezillond8e725d2016-09-15 10:32:50 +0200978 * nand_reset_data_interface - Reset data interface and timings
979 * @chip: The NAND chip
980 *
981 * Reset the Data interface and timings to ONFI mode 0.
982 *
983 * Returns 0 for success or negative error code otherwise.
984 */
985static int nand_reset_data_interface(struct nand_chip *chip)
986{
987 struct mtd_info *mtd = nand_to_mtd(chip);
988 const struct nand_data_interface *conf;
989 int ret;
990
991 if (!chip->setup_data_interface)
992 return 0;
993
994 /*
995 * The ONFI specification says:
996 * "
997 * To transition from NV-DDR or NV-DDR2 to the SDR data
998 * interface, the host shall use the Reset (FFh) command
999 * using SDR timing mode 0. A device in any timing mode is
1000 * required to recognize Reset (FFh) command issued in SDR
1001 * timing mode 0.
1002 * "
1003 *
1004 * Configure the data interface in SDR mode and set the
1005 * timings to timing mode 0.
1006 */
1007
1008 conf = nand_get_default_data_interface();
1009 ret = chip->setup_data_interface(mtd, conf, false);
1010 if (ret)
1011 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1012
1013 return ret;
1014}
1015
1016/**
1017 * nand_setup_data_interface - Setup the best data interface and timings
1018 * @chip: The NAND chip
1019 *
1020 * Find and configure the best data interface and NAND timings supported by
1021 * the chip and the driver.
1022 * First tries to retrieve supported timing modes from ONFI information,
1023 * and if the NAND chip does not support ONFI, relies on the
1024 * ->onfi_timing_mode_default specified in the nand_ids table.
1025 *
1026 * Returns 0 for success or negative error code otherwise.
1027 */
1028static int nand_setup_data_interface(struct nand_chip *chip)
1029{
1030 struct mtd_info *mtd = nand_to_mtd(chip);
1031 int ret;
1032
1033 if (!chip->setup_data_interface || !chip->data_interface)
1034 return 0;
1035
1036 /*
1037 * Ensure the timing mode has been changed on the chip side
1038 * before changing timings on the controller side.
1039 */
1040 if (chip->onfi_version) {
1041 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1042 chip->onfi_timing_mode_default,
1043 };
1044
1045 ret = chip->onfi_set_features(mtd, chip,
1046 ONFI_FEATURE_ADDR_TIMING_MODE,
1047 tmode_param);
1048 if (ret)
1049 goto err;
1050 }
1051
1052 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1053err:
1054 return ret;
1055}
1056
1057/**
1058 * nand_init_data_interface - find the best data interface and timings
1059 * @chip: The NAND chip
1060 *
1061 * Find the best data interface and NAND timings supported by the chip
1062 * and the driver.
1063 * First tries to retrieve supported timing modes from ONFI information,
1064 * and if the NAND chip does not support ONFI, relies on the
1065 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1066 * function nand_chip->data_interface is initialized with the best timing mode
1067 * available.
1068 *
1069 * Returns 0 for success or negative error code otherwise.
1070 */
1071static int nand_init_data_interface(struct nand_chip *chip)
1072{
1073 struct mtd_info *mtd = nand_to_mtd(chip);
1074 int modes, mode, ret;
1075
1076 if (!chip->setup_data_interface)
1077 return 0;
1078
1079 /*
1080 * First try to identify the best timings from ONFI parameters and
1081 * if the NAND does not support ONFI, fallback to the default ONFI
1082 * timing mode.
1083 */
1084 modes = onfi_get_async_timing_mode(chip);
1085 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1086 if (!chip->onfi_timing_mode_default)
1087 return 0;
1088
1089 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1090 }
1091
1092 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1093 GFP_KERNEL);
1094 if (!chip->data_interface)
1095 return -ENOMEM;
1096
1097 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1098 ret = onfi_init_data_interface(chip, chip->data_interface,
1099 NAND_SDR_IFACE, mode);
1100 if (ret)
1101 continue;
1102
1103 ret = chip->setup_data_interface(mtd, chip->data_interface,
1104 true);
1105 if (!ret) {
1106 chip->onfi_timing_mode_default = mode;
1107 break;
1108 }
1109 }
1110
1111 return 0;
1112}
1113
1114static void nand_release_data_interface(struct nand_chip *chip)
1115{
1116 kfree(chip->data_interface);
1117}
1118
1119/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001120 * nand_reset - Reset and initialize a NAND device
1121 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001122 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001123 *
1124 * Returns 0 for success or negative error code otherwise
1125 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001126int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001127{
1128 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001129 int ret;
1130
1131 ret = nand_reset_data_interface(chip);
1132 if (ret)
1133 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001134
Boris Brezillon73f907f2016-10-24 16:46:20 +02001135 /*
1136 * The CS line has to be released before we can apply the new NAND
1137 * interface settings, hence this weird ->select_chip() dance.
1138 */
1139 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001140 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001141 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001142
Boris Brezillon73f907f2016-10-24 16:46:20 +02001143 chip->select_chip(mtd, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001144 ret = nand_setup_data_interface(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001145 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001146 if (ret)
1147 return ret;
1148
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001149 return 0;
1150}
1151
1152/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001153 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001154 * @mtd: mtd info
1155 * @ofs: offset to start unlock from
1156 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001157 * @invert: when = 0, unlock the range of blocks within the lower and
1158 * upper boundary address
1159 * when = 1, unlock the range of blocks outside the boundaries
1160 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301161 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001162 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301163 */
1164static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1165 uint64_t len, int invert)
1166{
1167 int ret = 0;
1168 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001169 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301170
1171 /* Submit address of first page to unlock */
1172 page = ofs >> chip->page_shift;
1173 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1174
1175 /* Submit address of last page to unlock */
1176 page = (ofs + len) >> chip->page_shift;
1177 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1178 (page | invert) & chip->pagemask);
1179
1180 /* Call wait ready function */
1181 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301182 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001183 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001184 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301185 __func__, status);
1186 ret = -EIO;
1187 }
1188
1189 return ret;
1190}
1191
1192/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001193 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001194 * @mtd: mtd info
1195 * @ofs: offset to start unlock from
1196 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301197 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001198 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301199 */
1200int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1201{
1202 int ret = 0;
1203 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001204 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301205
Brian Norris289c0522011-07-19 10:06:09 -07001206 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301207 __func__, (unsigned long long)ofs, len);
1208
1209 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001210 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301211
1212 /* Align to last block address if size addresses end of the device */
1213 if (ofs + len == mtd->size)
1214 len -= mtd->erasesize;
1215
Huang Shijie6a8214a2012-11-19 14:43:30 +08001216 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301217
1218 /* Shift to get chip number */
1219 chipnr = ofs >> chip->chip_shift;
1220
White Ding57d3a9a2014-07-24 00:10:45 +08001221 /*
1222 * Reset the chip.
1223 * If we want to check the WP through READ STATUS and check the bit 7
1224 * we must reset the chip
1225 * some operation can also clear the bit 7 of status register
1226 * eg. erase/program a locked block
1227 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001228 nand_reset(chip, chipnr);
1229
1230 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001231
Vimal Singh7d70f332010-02-08 15:50:49 +05301232 /* Check, if it is write protected */
1233 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001234 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301235 __func__);
1236 ret = -EIO;
1237 goto out;
1238 }
1239
1240 ret = __nand_unlock(mtd, ofs, len, 0);
1241
1242out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001243 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301244 nand_release_device(mtd);
1245
1246 return ret;
1247}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001248EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301249
1250/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001251 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001252 * @mtd: mtd info
1253 * @ofs: offset to start unlock from
1254 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301255 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001256 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1257 * have this feature, but it allows only to lock all blocks, not for specified
1258 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1259 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301260 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001261 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301262 */
1263int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1264{
1265 int ret = 0;
1266 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001267 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301268
Brian Norris289c0522011-07-19 10:06:09 -07001269 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301270 __func__, (unsigned long long)ofs, len);
1271
1272 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001273 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301274
Huang Shijie6a8214a2012-11-19 14:43:30 +08001275 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301276
1277 /* Shift to get chip number */
1278 chipnr = ofs >> chip->chip_shift;
1279
White Ding57d3a9a2014-07-24 00:10:45 +08001280 /*
1281 * Reset the chip.
1282 * If we want to check the WP through READ STATUS and check the bit 7
1283 * we must reset the chip
1284 * some operation can also clear the bit 7 of status register
1285 * eg. erase/program a locked block
1286 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001287 nand_reset(chip, chipnr);
1288
1289 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001290
Vimal Singh7d70f332010-02-08 15:50:49 +05301291 /* Check, if it is write protected */
1292 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001293 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301294 __func__);
1295 status = MTD_ERASE_FAILED;
1296 ret = -EIO;
1297 goto out;
1298 }
1299
1300 /* Submit address of first page to lock */
1301 page = ofs >> chip->page_shift;
1302 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1303
1304 /* Call wait ready function */
1305 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301306 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001307 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001308 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301309 __func__, status);
1310 ret = -EIO;
1311 goto out;
1312 }
1313
1314 ret = __nand_unlock(mtd, ofs, len, 0x1);
1315
1316out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001317 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301318 nand_release_device(mtd);
1319
1320 return ret;
1321}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001322EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301323
1324/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001325 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1326 * @buf: buffer to test
1327 * @len: buffer length
1328 * @bitflips_threshold: maximum number of bitflips
1329 *
1330 * Check if a buffer contains only 0xff, which means the underlying region
1331 * has been erased and is ready to be programmed.
1332 * The bitflips_threshold specify the maximum number of bitflips before
1333 * considering the region is not erased.
1334 * Note: The logic of this function has been extracted from the memweight
1335 * implementation, except that nand_check_erased_buf function exit before
1336 * testing the whole buffer if the number of bitflips exceed the
1337 * bitflips_threshold value.
1338 *
1339 * Returns a positive number of bitflips less than or equal to
1340 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1341 * threshold.
1342 */
1343static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1344{
1345 const unsigned char *bitmap = buf;
1346 int bitflips = 0;
1347 int weight;
1348
1349 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1350 len--, bitmap++) {
1351 weight = hweight8(*bitmap);
1352 bitflips += BITS_PER_BYTE - weight;
1353 if (unlikely(bitflips > bitflips_threshold))
1354 return -EBADMSG;
1355 }
1356
1357 for (; len >= sizeof(long);
1358 len -= sizeof(long), bitmap += sizeof(long)) {
1359 weight = hweight_long(*((unsigned long *)bitmap));
1360 bitflips += BITS_PER_LONG - weight;
1361 if (unlikely(bitflips > bitflips_threshold))
1362 return -EBADMSG;
1363 }
1364
1365 for (; len > 0; len--, bitmap++) {
1366 weight = hweight8(*bitmap);
1367 bitflips += BITS_PER_BYTE - weight;
1368 if (unlikely(bitflips > bitflips_threshold))
1369 return -EBADMSG;
1370 }
1371
1372 return bitflips;
1373}
1374
1375/**
1376 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1377 * 0xff data
1378 * @data: data buffer to test
1379 * @datalen: data length
1380 * @ecc: ECC buffer
1381 * @ecclen: ECC length
1382 * @extraoob: extra OOB buffer
1383 * @extraooblen: extra OOB length
1384 * @bitflips_threshold: maximum number of bitflips
1385 *
1386 * Check if a data buffer and its associated ECC and OOB data contains only
1387 * 0xff pattern, which means the underlying region has been erased and is
1388 * ready to be programmed.
1389 * The bitflips_threshold specify the maximum number of bitflips before
1390 * considering the region as not erased.
1391 *
1392 * Note:
1393 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1394 * different from the NAND page size. When fixing bitflips, ECC engines will
1395 * report the number of errors per chunk, and the NAND core infrastructure
1396 * expect you to return the maximum number of bitflips for the whole page.
1397 * This is why you should always use this function on a single chunk and
1398 * not on the whole page. After checking each chunk you should update your
1399 * max_bitflips value accordingly.
1400 * 2/ When checking for bitflips in erased pages you should not only check
1401 * the payload data but also their associated ECC data, because a user might
1402 * have programmed almost all bits to 1 but a few. In this case, we
1403 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1404 * this case.
1405 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1406 * data are protected by the ECC engine.
1407 * It could also be used if you support subpages and want to attach some
1408 * extra OOB data to an ECC chunk.
1409 *
1410 * Returns a positive number of bitflips less than or equal to
1411 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1412 * threshold. In case of success, the passed buffers are filled with 0xff.
1413 */
1414int nand_check_erased_ecc_chunk(void *data, int datalen,
1415 void *ecc, int ecclen,
1416 void *extraoob, int extraooblen,
1417 int bitflips_threshold)
1418{
1419 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1420
1421 data_bitflips = nand_check_erased_buf(data, datalen,
1422 bitflips_threshold);
1423 if (data_bitflips < 0)
1424 return data_bitflips;
1425
1426 bitflips_threshold -= data_bitflips;
1427
1428 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1429 if (ecc_bitflips < 0)
1430 return ecc_bitflips;
1431
1432 bitflips_threshold -= ecc_bitflips;
1433
1434 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1435 bitflips_threshold);
1436 if (extraoob_bitflips < 0)
1437 return extraoob_bitflips;
1438
1439 if (data_bitflips)
1440 memset(data, 0xff, datalen);
1441
1442 if (ecc_bitflips)
1443 memset(ecc, 0xff, ecclen);
1444
1445 if (extraoob_bitflips)
1446 memset(extraoob, 0xff, extraooblen);
1447
1448 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1449}
1450EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1451
1452/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001453 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001454 * @mtd: mtd info structure
1455 * @chip: nand chip info structure
1456 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001457 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001458 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001459 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001460 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001461 */
1462static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001463 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001464{
1465 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001466 if (oob_required)
1467 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001468 return 0;
1469}
1470
1471/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001472 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001473 * @mtd: mtd info structure
1474 * @chip: nand chip info structure
1475 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001476 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001477 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001478 *
1479 * We need a special oob layout and handling even when OOB isn't used.
1480 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001481static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001482 struct nand_chip *chip, uint8_t *buf,
1483 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001484{
1485 int eccsize = chip->ecc.size;
1486 int eccbytes = chip->ecc.bytes;
1487 uint8_t *oob = chip->oob_poi;
1488 int steps, size;
1489
1490 for (steps = chip->ecc.steps; steps > 0; steps--) {
1491 chip->read_buf(mtd, buf, eccsize);
1492 buf += eccsize;
1493
1494 if (chip->ecc.prepad) {
1495 chip->read_buf(mtd, oob, chip->ecc.prepad);
1496 oob += chip->ecc.prepad;
1497 }
1498
1499 chip->read_buf(mtd, oob, eccbytes);
1500 oob += eccbytes;
1501
1502 if (chip->ecc.postpad) {
1503 chip->read_buf(mtd, oob, chip->ecc.postpad);
1504 oob += chip->ecc.postpad;
1505 }
1506 }
1507
1508 size = mtd->oobsize - (oob - chip->oob_poi);
1509 if (size)
1510 chip->read_buf(mtd, oob, size);
1511
1512 return 0;
1513}
1514
1515/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001516 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001517 * @mtd: mtd info structure
1518 * @chip: nand chip info structure
1519 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001520 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001521 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001522 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001523static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001524 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Boris Brezillon846031d2016-02-03 20:11:00 +01001526 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001527 int eccbytes = chip->ecc.bytes;
1528 int eccsteps = chip->ecc.steps;
1529 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001530 uint8_t *ecc_calc = chip->buffers->ecccalc;
1531 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001532 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001533
Brian Norris1fbb9382012-05-02 10:14:55 -07001534 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001535
1536 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1537 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1538
Boris Brezillon846031d2016-02-03 20:11:00 +01001539 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1540 chip->ecc.total);
1541 if (ret)
1542 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001543
1544 eccsteps = chip->ecc.steps;
1545 p = buf;
1546
1547 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1548 int stat;
1549
1550 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001551 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001552 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001553 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001554 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001555 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1556 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001557 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001558 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301562 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001563 * @mtd: mtd info structure
1564 * @chip: nand chip info structure
1565 * @data_offs: offset of requested data within the page
1566 * @readlen: data length
1567 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001568 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001569 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001570static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001571 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1572 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001573{
Boris Brezillon846031d2016-02-03 20:11:00 +01001574 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001575 uint8_t *p;
1576 int data_col_addr, i, gaps = 0;
1577 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1578 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001579 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001580 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001581 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001582
Brian Norris7854d3f2011-06-23 14:12:08 -07001583 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001584 start_step = data_offs / chip->ecc.size;
1585 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1586 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301587 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001588
Brian Norris8b6e50c2011-05-25 14:59:01 -07001589 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001590 datafrag_len = num_steps * chip->ecc.size;
1591 eccfrag_len = num_steps * chip->ecc.bytes;
1592
1593 data_col_addr = start_step * chip->ecc.size;
1594 /* If we read not a page aligned data */
1595 if (data_col_addr != 0)
1596 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1597
1598 p = bufpoi + data_col_addr;
1599 chip->read_buf(mtd, p, datafrag_len);
1600
Brian Norris8b6e50c2011-05-25 14:59:01 -07001601 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001602 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1603 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1604
Brian Norris8b6e50c2011-05-25 14:59:01 -07001605 /*
1606 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001607 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001608 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001609 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1610 if (ret)
1611 return ret;
1612
1613 if (oobregion.length < eccfrag_len)
1614 gaps = 1;
1615
Alexey Korolev3d459552008-05-15 17:23:18 +01001616 if (gaps) {
1617 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1618 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1619 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001620 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001621 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001622 * about buswidth alignment in read_buf.
1623 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001624 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001625 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001626 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001627 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001628 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1629 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001630 aligned_len++;
1631
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001632 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001633 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001634 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1635 }
1636
Boris Brezillon846031d2016-02-03 20:11:00 +01001637 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1638 chip->oob_poi, index, eccfrag_len);
1639 if (ret)
1640 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001641
1642 p = bufpoi + data_col_addr;
1643 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1644 int stat;
1645
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001646 stat = chip->ecc.correct(mtd, p,
1647 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001648 if (stat == -EBADMSG &&
1649 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1650 /* check for empty pages with bitflips */
1651 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1652 &chip->buffers->ecccode[i],
1653 chip->ecc.bytes,
1654 NULL, 0,
1655 chip->ecc.strength);
1656 }
1657
Mike Dunn3f91e942012-04-25 12:06:09 -07001658 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001659 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001660 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001661 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001662 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1663 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001664 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001665 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001666}
1667
1668/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001669 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001670 * @mtd: mtd info structure
1671 * @chip: nand chip info structure
1672 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001673 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001674 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001675 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001676 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001677 */
1678static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001679 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001680{
Boris Brezillon846031d2016-02-03 20:11:00 +01001681 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001682 int eccbytes = chip->ecc.bytes;
1683 int eccsteps = chip->ecc.steps;
1684 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001685 uint8_t *ecc_calc = chip->buffers->ecccalc;
1686 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001687 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001688
1689 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1690 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1691 chip->read_buf(mtd, p, eccsize);
1692 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1693 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001694 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001695
Boris Brezillon846031d2016-02-03 20:11:00 +01001696 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1697 chip->ecc.total);
1698 if (ret)
1699 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001700
1701 eccsteps = chip->ecc.steps;
1702 p = buf;
1703
1704 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1705 int stat;
1706
1707 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001708 if (stat == -EBADMSG &&
1709 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1710 /* check for empty pages with bitflips */
1711 stat = nand_check_erased_ecc_chunk(p, eccsize,
1712 &ecc_code[i], eccbytes,
1713 NULL, 0,
1714 chip->ecc.strength);
1715 }
1716
Mike Dunn3f91e942012-04-25 12:06:09 -07001717 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001718 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001719 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001720 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001721 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1722 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001723 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001724 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001725}
1726
1727/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001728 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001729 * @mtd: mtd info structure
1730 * @chip: nand chip info structure
1731 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001732 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001733 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001734 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001735 * Hardware ECC for large page chips, require OOB to be read first. For this
1736 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1737 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1738 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1739 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001740 */
1741static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001742 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001743{
Boris Brezillon846031d2016-02-03 20:11:00 +01001744 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001745 int eccbytes = chip->ecc.bytes;
1746 int eccsteps = chip->ecc.steps;
1747 uint8_t *p = buf;
1748 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001749 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001750 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001751
1752 /* Read the OOB area first */
1753 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1754 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1755 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1756
Boris Brezillon846031d2016-02-03 20:11:00 +01001757 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1758 chip->ecc.total);
1759 if (ret)
1760 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001761
1762 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1763 int stat;
1764
1765 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1766 chip->read_buf(mtd, p, eccsize);
1767 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1768
1769 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001770 if (stat == -EBADMSG &&
1771 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1772 /* check for empty pages with bitflips */
1773 stat = nand_check_erased_ecc_chunk(p, eccsize,
1774 &ecc_code[i], eccbytes,
1775 NULL, 0,
1776 chip->ecc.strength);
1777 }
1778
Mike Dunn3f91e942012-04-25 12:06:09 -07001779 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001780 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001781 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001782 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001783 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1784 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001785 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001786 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001787}
1788
1789/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001790 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001791 * @mtd: mtd info structure
1792 * @chip: nand chip info structure
1793 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001794 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001795 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001796 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001797 * The hw generator calculates the error syndrome automatically. Therefore we
1798 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001799 */
1800static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001801 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001802{
1803 int i, eccsize = chip->ecc.size;
1804 int eccbytes = chip->ecc.bytes;
1805 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001806 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001807 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001808 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001809 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001810
1811 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1812 int stat;
1813
1814 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1815 chip->read_buf(mtd, p, eccsize);
1816
1817 if (chip->ecc.prepad) {
1818 chip->read_buf(mtd, oob, chip->ecc.prepad);
1819 oob += chip->ecc.prepad;
1820 }
1821
1822 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1823 chip->read_buf(mtd, oob, eccbytes);
1824 stat = chip->ecc.correct(mtd, p, oob, NULL);
1825
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001826 oob += eccbytes;
1827
1828 if (chip->ecc.postpad) {
1829 chip->read_buf(mtd, oob, chip->ecc.postpad);
1830 oob += chip->ecc.postpad;
1831 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001832
1833 if (stat == -EBADMSG &&
1834 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1835 /* check for empty pages with bitflips */
1836 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1837 oob - eccpadbytes,
1838 eccpadbytes,
1839 NULL, 0,
1840 chip->ecc.strength);
1841 }
1842
1843 if (stat < 0) {
1844 mtd->ecc_stats.failed++;
1845 } else {
1846 mtd->ecc_stats.corrected += stat;
1847 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1848 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001849 }
1850
1851 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001852 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001853 if (i)
1854 chip->read_buf(mtd, oob, i);
1855
Mike Dunn3f91e942012-04-25 12:06:09 -07001856 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001857}
1858
1859/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001860 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001861 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001862 * @oob: oob destination address
1863 * @ops: oob ops structure
1864 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001865 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001866static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001867 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868{
Boris Brezillon846031d2016-02-03 20:11:00 +01001869 struct nand_chip *chip = mtd_to_nand(mtd);
1870 int ret;
1871
Florian Fainellif8ac0412010-09-07 13:23:43 +02001872 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001873
Brian Norris0612b9d2011-08-30 18:45:40 -07001874 case MTD_OPS_PLACE_OOB:
1875 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001876 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1877 return oob + len;
1878
Boris Brezillon846031d2016-02-03 20:11:00 +01001879 case MTD_OPS_AUTO_OOB:
1880 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1881 ops->ooboffs, len);
1882 BUG_ON(ret);
1883 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001884
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001885 default:
1886 BUG();
1887 }
1888 return NULL;
1889}
1890
1891/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001892 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1893 * @mtd: MTD device structure
1894 * @retry_mode: the retry mode to use
1895 *
1896 * Some vendors supply a special command to shift the Vt threshold, to be used
1897 * when there are too many bitflips in a page (i.e., ECC error). After setting
1898 * a new threshold, the host should retry reading the page.
1899 */
1900static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1901{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001902 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001903
1904 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1905
1906 if (retry_mode >= chip->read_retries)
1907 return -EINVAL;
1908
1909 if (!chip->setup_read_retry)
1910 return -EOPNOTSUPP;
1911
1912 return chip->setup_read_retry(mtd, retry_mode);
1913}
1914
1915/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001916 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001917 * @mtd: MTD device structure
1918 * @from: offset to read from
1919 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001920 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001921 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001922 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001923static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1924 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001925{
Brian Norrise47f3db2012-05-02 10:14:56 -07001926 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001927 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001928 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001929 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001930 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001931 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001932
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001933 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001934 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001935 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001936 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001937 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001939 chipnr = (int)(from >> chip->chip_shift);
1940 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001942 realpage = (int)(from >> chip->page_shift);
1943 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001945 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001947 buf = ops->datbuf;
1948 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001949 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001950
Florian Fainellif8ac0412010-09-07 13:23:43 +02001951 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001952 unsigned int ecc_failures = mtd->ecc_stats.failed;
1953
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001954 bytes = min(mtd->writesize - col, readlen);
1955 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001956
Kamal Dasu66507c72014-05-01 20:51:19 -04001957 if (!aligned)
1958 use_bufpoi = 1;
1959 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1960 use_bufpoi = !virt_addr_valid(buf);
1961 else
1962 use_bufpoi = 0;
1963
Brian Norris8b6e50c2011-05-25 14:59:01 -07001964 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001965 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001966 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1967
1968 if (use_bufpoi && aligned)
1969 pr_debug("%s: using read bounce buffer for buf@%p\n",
1970 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Brian Norrisba84fb52014-01-03 15:13:33 -08001972read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001973 if (nand_standard_page_accessors(&chip->ecc))
1974 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Mike Dunnedbc45402012-04-25 12:06:11 -07001976 /*
1977 * Now read the page into the buffer. Absent an error,
1978 * the read methods return max bitflips per ecc step.
1979 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001980 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001981 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001982 oob_required,
1983 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001984 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1985 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001986 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001987 col, bytes, bufpoi,
1988 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001989 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001990 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001991 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001992 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001993 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001994 /* Invalidate page cache */
1995 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001996 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001997 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001998
Mike Dunnedbc45402012-04-25 12:06:11 -07001999 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2000
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002001 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002002 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002003 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002004 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002005 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002006 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002007 chip->pagebuf_bitflips = ret;
2008 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002009 /* Invalidate page cache */
2010 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002011 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002012 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002014
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002015 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002016 int toread = min(oobreadlen, max_oobsize);
2017
2018 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002019 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002020 oob, ops, toread);
2021 oobreadlen -= toread;
2022 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002023 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002024
2025 if (chip->options & NAND_NEED_READRDY) {
2026 /* Apply delay or wait for ready/busy pin */
2027 if (!chip->dev_ready)
2028 udelay(chip->chip_delay);
2029 else
2030 nand_wait_ready(mtd);
2031 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002032
Brian Norrisba84fb52014-01-03 15:13:33 -08002033 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002034 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002035 retry_mode++;
2036 ret = nand_setup_read_retry(mtd,
2037 retry_mode);
2038 if (ret < 0)
2039 break;
2040
2041 /* Reset failures; retry */
2042 mtd->ecc_stats.failed = ecc_failures;
2043 goto read_retry;
2044 } else {
2045 /* No more retry modes; real failure */
2046 ecc_fail = true;
2047 }
2048 }
2049
2050 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002051 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002052 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002053 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002054 max_bitflips = max_t(unsigned int, max_bitflips,
2055 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002058 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002059
Brian Norrisba84fb52014-01-03 15:13:33 -08002060 /* Reset to retry mode 0 */
2061 if (retry_mode) {
2062 ret = nand_setup_read_retry(mtd, 0);
2063 if (ret < 0)
2064 break;
2065 retry_mode = 0;
2066 }
2067
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002068 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Brian Norris8b6e50c2011-05-25 14:59:01 -07002071 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 col = 0;
2073 /* Increment page address */
2074 realpage++;
2075
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002076 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /* Check, if we cross a chip boundary */
2078 if (!page) {
2079 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002080 chip->select_chip(mtd, -1);
2081 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002084 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002086 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002087 if (oob)
2088 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Mike Dunn3f91e942012-04-25 12:06:09 -07002090 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002091 return ret;
2092
Brian Norrisb72f3df2013-12-03 11:04:14 -08002093 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002094 return -EBADMSG;
2095
Mike Dunnedbc45402012-04-25 12:06:11 -07002096 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002097}
2098
2099/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002100 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002101 * @mtd: MTD device structure
2102 * @from: offset to read from
2103 * @len: number of bytes to read
2104 * @retlen: pointer to variable to store the number of read bytes
2105 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002106 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002107 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002108 */
2109static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2110 size_t *retlen, uint8_t *buf)
2111{
Brian Norris4a89ff82011-08-30 18:45:45 -07002112 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002113 int ret;
2114
Huang Shijie6a8214a2012-11-19 14:43:30 +08002115 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002116 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002117 ops.len = len;
2118 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002119 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002120 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002121 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002122 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002123 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002127 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002128 * @mtd: mtd info structure
2129 * @chip: nand chip info structure
2130 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002131 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002132int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002133{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002134 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002135 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002136 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002137}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002138EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002139
2140/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002141 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002142 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002143 * @mtd: mtd info structure
2144 * @chip: nand chip info structure
2145 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002146 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002147int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2148 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002149{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002150 int length = mtd->oobsize;
2151 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2152 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002153 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002154 int i, toread, sndrnd = 0, pos;
2155
2156 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2157 for (i = 0; i < chip->ecc.steps; i++) {
2158 if (sndrnd) {
2159 pos = eccsize + i * (eccsize + chunk);
2160 if (mtd->writesize > 512)
2161 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2162 else
2163 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2164 } else
2165 sndrnd = 1;
2166 toread = min_t(int, length, chunk);
2167 chip->read_buf(mtd, bufpoi, toread);
2168 bufpoi += toread;
2169 length -= toread;
2170 }
2171 if (length > 0)
2172 chip->read_buf(mtd, bufpoi, length);
2173
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002174 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002175}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002176EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002177
2178/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002179 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002180 * @mtd: mtd info structure
2181 * @chip: nand chip info structure
2182 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002183 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002184int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002185{
2186 int status = 0;
2187 const uint8_t *buf = chip->oob_poi;
2188 int length = mtd->oobsize;
2189
2190 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2191 chip->write_buf(mtd, buf, length);
2192 /* Send command to program the OOB data */
2193 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2194
2195 status = chip->waitfunc(mtd, chip);
2196
Savin Zlobec0d420f92006-06-21 11:51:20 +02002197 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002198}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002199EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002200
2201/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002202 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002203 * with syndrome - only for large page flash
2204 * @mtd: mtd info structure
2205 * @chip: nand chip info structure
2206 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002207 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002208int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2209 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002210{
2211 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2212 int eccsize = chip->ecc.size, length = mtd->oobsize;
2213 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2214 const uint8_t *bufpoi = chip->oob_poi;
2215
2216 /*
2217 * data-ecc-data-ecc ... ecc-oob
2218 * or
2219 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2220 */
2221 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2222 pos = steps * (eccsize + chunk);
2223 steps = 0;
2224 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002225 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002226
2227 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2228 for (i = 0; i < steps; i++) {
2229 if (sndcmd) {
2230 if (mtd->writesize <= 512) {
2231 uint32_t fill = 0xFFFFFFFF;
2232
2233 len = eccsize;
2234 while (len > 0) {
2235 int num = min_t(int, len, 4);
2236 chip->write_buf(mtd, (uint8_t *)&fill,
2237 num);
2238 len -= num;
2239 }
2240 } else {
2241 pos = eccsize + i * (eccsize + chunk);
2242 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2243 }
2244 } else
2245 sndcmd = 1;
2246 len = min_t(int, length, chunk);
2247 chip->write_buf(mtd, bufpoi, len);
2248 bufpoi += len;
2249 length -= len;
2250 }
2251 if (length > 0)
2252 chip->write_buf(mtd, bufpoi, length);
2253
2254 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2255 status = chip->waitfunc(mtd, chip);
2256
2257 return status & NAND_STATUS_FAIL ? -EIO : 0;
2258}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002259EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002260
2261/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002262 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002263 * @mtd: MTD device structure
2264 * @from: offset to read from
2265 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002267 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002269static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2270 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Brian Norrisc00a0992012-05-01 17:12:54 -07002272 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002273 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002274 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002275 int readlen = ops->ooblen;
2276 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002277 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002278 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Brian Norris289c0522011-07-19 10:06:09 -07002280 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302281 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Brian Norris041e4572011-06-23 16:45:24 -07002283 stats = mtd->ecc_stats;
2284
Boris BREZILLON29f10582016-03-07 10:46:52 +01002285 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002286
2287 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002288 pr_debug("%s: attempt to start read outside oob\n",
2289 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002290 return -EINVAL;
2291 }
2292
2293 /* Do not allow reads past end of device */
2294 if (unlikely(from >= mtd->size ||
2295 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2296 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002297 pr_debug("%s: attempt to read beyond end of device\n",
2298 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002299 return -EINVAL;
2300 }
Vitaly Wool70145682006-11-03 18:20:38 +03002301
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002302 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002303 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002305 /* Shift to get page */
2306 realpage = (int)(from >> chip->page_shift);
2307 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Florian Fainellif8ac0412010-09-07 13:23:43 +02002309 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002310 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002311 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002312 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002313 ret = chip->ecc.read_oob(mtd, chip, page);
2314
2315 if (ret < 0)
2316 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002317
2318 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002319 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002320
Brian Norris5bc7c332013-03-13 09:51:31 -07002321 if (chip->options & NAND_NEED_READRDY) {
2322 /* Apply delay or wait for ready/busy pin */
2323 if (!chip->dev_ready)
2324 udelay(chip->chip_delay);
2325 else
2326 nand_wait_ready(mtd);
2327 }
2328
Vitaly Wool70145682006-11-03 18:20:38 +03002329 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002330 if (!readlen)
2331 break;
2332
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002333 /* Increment page address */
2334 realpage++;
2335
2336 page = realpage & chip->pagemask;
2337 /* Check, if we cross a chip boundary */
2338 if (!page) {
2339 chipnr++;
2340 chip->select_chip(mtd, -1);
2341 chip->select_chip(mtd, chipnr);
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002344 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002346 ops->oobretlen = ops->ooblen - readlen;
2347
2348 if (ret < 0)
2349 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002350
2351 if (mtd->ecc_stats.failed - stats.failed)
2352 return -EBADMSG;
2353
2354 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355}
2356
2357/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002358 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002359 * @mtd: MTD device structure
2360 * @from: offset to read from
2361 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002363 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002365static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2366 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002368 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002369
2370 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002373 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002374 pr_debug("%s: attempt to read beyond end of device\n",
2375 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return -EINVAL;
2377 }
2378
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002379 if (ops->mode != MTD_OPS_PLACE_OOB &&
2380 ops->mode != MTD_OPS_AUTO_OOB &&
2381 ops->mode != MTD_OPS_RAW)
2382 return -ENOTSUPP;
2383
Huang Shijie6a8214a2012-11-19 14:43:30 +08002384 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002386 if (!ops->datbuf)
2387 ret = nand_do_read_oob(mtd, from, ops);
2388 else
2389 ret = nand_do_read_ops(mtd, from, ops);
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002392 return ret;
2393}
2394
2395
2396/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002397 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002398 * @mtd: mtd info structure
2399 * @chip: nand chip info structure
2400 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002401 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002402 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002403 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002404 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002405 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002406static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002407 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002408{
2409 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002410 if (oob_required)
2411 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002412
2413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002416/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002417 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002418 * @mtd: mtd info structure
2419 * @chip: nand chip info structure
2420 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002421 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002422 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002423 *
2424 * We need a special oob layout and handling even when ECC isn't checked.
2425 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002426static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002427 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002428 const uint8_t *buf, int oob_required,
2429 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002430{
2431 int eccsize = chip->ecc.size;
2432 int eccbytes = chip->ecc.bytes;
2433 uint8_t *oob = chip->oob_poi;
2434 int steps, size;
2435
2436 for (steps = chip->ecc.steps; steps > 0; steps--) {
2437 chip->write_buf(mtd, buf, eccsize);
2438 buf += eccsize;
2439
2440 if (chip->ecc.prepad) {
2441 chip->write_buf(mtd, oob, chip->ecc.prepad);
2442 oob += chip->ecc.prepad;
2443 }
2444
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002445 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002446 oob += eccbytes;
2447
2448 if (chip->ecc.postpad) {
2449 chip->write_buf(mtd, oob, chip->ecc.postpad);
2450 oob += chip->ecc.postpad;
2451 }
2452 }
2453
2454 size = mtd->oobsize - (oob - chip->oob_poi);
2455 if (size)
2456 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002457
2458 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002459}
2460/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002461 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002462 * @mtd: mtd info structure
2463 * @chip: nand chip info structure
2464 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002465 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002466 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002467 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002468static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002469 const uint8_t *buf, int oob_required,
2470 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002471{
Boris Brezillon846031d2016-02-03 20:11:00 +01002472 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002473 int eccbytes = chip->ecc.bytes;
2474 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002475 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002476 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002477
Brian Norris7854d3f2011-06-23 14:12:08 -07002478 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002479 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2480 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002481
Boris Brezillon846031d2016-02-03 20:11:00 +01002482 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2483 chip->ecc.total);
2484 if (ret)
2485 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002486
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002487 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002488}
2489
2490/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002491 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002492 * @mtd: mtd info structure
2493 * @chip: nand chip info structure
2494 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002495 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002496 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002497 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002498static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002499 const uint8_t *buf, int oob_required,
2500 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002501{
Boris Brezillon846031d2016-02-03 20:11:00 +01002502 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002503 int eccbytes = chip->ecc.bytes;
2504 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002505 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002506 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002507
2508 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2509 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002510 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002511 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2512 }
2513
Boris Brezillon846031d2016-02-03 20:11:00 +01002514 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2515 chip->ecc.total);
2516 if (ret)
2517 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002518
2519 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002520
2521 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002522}
2523
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302524
2525/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002526 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302527 * @mtd: mtd info structure
2528 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002529 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302530 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002531 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302532 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002533 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302534 */
2535static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2536 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002537 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002538 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302539{
2540 uint8_t *oob_buf = chip->oob_poi;
2541 uint8_t *ecc_calc = chip->buffers->ecccalc;
2542 int ecc_size = chip->ecc.size;
2543 int ecc_bytes = chip->ecc.bytes;
2544 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302545 uint32_t start_step = offset / ecc_size;
2546 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2547 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002548 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302549
2550 for (step = 0; step < ecc_steps; step++) {
2551 /* configure controller for WRITE access */
2552 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2553
2554 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002555 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302556
2557 /* mask ECC of un-touched subpages by padding 0xFF */
2558 if ((step < start_step) || (step > end_step))
2559 memset(ecc_calc, 0xff, ecc_bytes);
2560 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002561 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302562
2563 /* mask OOB of un-touched subpages by padding 0xFF */
2564 /* if oob_required, preserve OOB metadata of written subpage */
2565 if (!oob_required || (step < start_step) || (step > end_step))
2566 memset(oob_buf, 0xff, oob_bytes);
2567
Brian Norrisd6a950802013-08-08 17:16:36 -07002568 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302569 ecc_calc += ecc_bytes;
2570 oob_buf += oob_bytes;
2571 }
2572
2573 /* copy calculated ECC for whole page to chip->buffer->oob */
2574 /* this include masked-value(0xFF) for unwritten subpages */
2575 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002576 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2577 chip->ecc.total);
2578 if (ret)
2579 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302580
2581 /* write OOB buffer to NAND device */
2582 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2583
2584 return 0;
2585}
2586
2587
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002588/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002589 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002590 * @mtd: mtd info structure
2591 * @chip: nand chip info structure
2592 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002593 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002594 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002595 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002596 * The hw generator calculates the error syndrome automatically. Therefore we
2597 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002598 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002599static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002600 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002601 const uint8_t *buf, int oob_required,
2602 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002603{
2604 int i, eccsize = chip->ecc.size;
2605 int eccbytes = chip->ecc.bytes;
2606 int eccsteps = chip->ecc.steps;
2607 const uint8_t *p = buf;
2608 uint8_t *oob = chip->oob_poi;
2609
2610 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2611
2612 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2613 chip->write_buf(mtd, p, eccsize);
2614
2615 if (chip->ecc.prepad) {
2616 chip->write_buf(mtd, oob, chip->ecc.prepad);
2617 oob += chip->ecc.prepad;
2618 }
2619
2620 chip->ecc.calculate(mtd, p, oob);
2621 chip->write_buf(mtd, oob, eccbytes);
2622 oob += eccbytes;
2623
2624 if (chip->ecc.postpad) {
2625 chip->write_buf(mtd, oob, chip->ecc.postpad);
2626 oob += chip->ecc.postpad;
2627 }
2628 }
2629
2630 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002631 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002632 if (i)
2633 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002634
2635 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002636}
2637
2638/**
David Woodhouse956e9442006-09-25 17:12:39 +01002639 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002640 * @mtd: MTD device structure
2641 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302642 * @offset: address offset within the page
2643 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002644 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002645 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002646 * @page: page number to write
2647 * @cached: cached programming
2648 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002649 */
2650static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302651 uint32_t offset, int data_len, const uint8_t *buf,
2652 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002653{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302654 int status, subpage;
2655
2656 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2657 chip->ecc.write_subpage)
2658 subpage = offset || (data_len < mtd->writesize);
2659 else
2660 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002661
Marc Gonzalez3371d662016-11-15 10:56:20 +01002662 if (nand_standard_page_accessors(&chip->ecc))
2663 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002664
David Woodhouse956e9442006-09-25 17:12:39 +01002665 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302666 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002667 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302668 else if (subpage)
2669 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002670 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002671 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002672 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2673 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002674
2675 if (status < 0)
2676 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002677
2678 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002679 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002680 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002681 */
2682 cached = 0;
2683
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002684 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002685
Marc Gonzalez3371d662016-11-15 10:56:20 +01002686 if (nand_standard_page_accessors(&chip->ecc))
2687 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002688 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002689 /*
2690 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002691 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002692 */
2693 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2694 status = chip->errstat(mtd, chip, FL_WRITING, status,
2695 page);
2696
2697 if (status & NAND_STATUS_FAIL)
2698 return -EIO;
2699 } else {
2700 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002701 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002702 }
2703
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002704 return 0;
2705}
2706
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002707/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002708 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002709 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002710 * @oob: oob data buffer
2711 * @len: oob data write length
2712 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002713 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002714static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2715 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002716{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002717 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002718 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002719
2720 /*
2721 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2722 * data from a previous OOB read.
2723 */
2724 memset(chip->oob_poi, 0xff, mtd->oobsize);
2725
Florian Fainellif8ac0412010-09-07 13:23:43 +02002726 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002727
Brian Norris0612b9d2011-08-30 18:45:40 -07002728 case MTD_OPS_PLACE_OOB:
2729 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002730 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2731 return oob + len;
2732
Boris Brezillon846031d2016-02-03 20:11:00 +01002733 case MTD_OPS_AUTO_OOB:
2734 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2735 ops->ooboffs, len);
2736 BUG_ON(ret);
2737 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002738
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002739 default:
2740 BUG();
2741 }
2742 return NULL;
2743}
2744
Florian Fainellif8ac0412010-09-07 13:23:43 +02002745#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002746
2747/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002748 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002749 * @mtd: MTD device structure
2750 * @to: offset to write to
2751 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002752 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002753 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002754 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002755static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2756 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002757{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002758 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002759 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002760 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002761
2762 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002763 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002764
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002765 uint8_t *oob = ops->oobbuf;
2766 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302767 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002768 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002769
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002770 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002771 if (!writelen)
2772 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002773
Brian Norris8b6e50c2011-05-25 14:59:01 -07002774 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002775 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002776 pr_notice("%s: attempt to write non page aligned data\n",
2777 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002778 return -EINVAL;
2779 }
2780
Thomas Gleixner29072b92006-09-28 15:38:36 +02002781 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002782
Thomas Gleixner6a930962006-06-28 00:11:45 +02002783 chipnr = (int)(to >> chip->chip_shift);
2784 chip->select_chip(mtd, chipnr);
2785
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002786 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002787 if (nand_check_wp(mtd)) {
2788 ret = -EIO;
2789 goto err_out;
2790 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002791
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002792 realpage = (int)(to >> chip->page_shift);
2793 page = realpage & chip->pagemask;
2794 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2795
2796 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002797 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2798 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002799 chip->pagebuf = -1;
2800
Maxim Levitsky782ce792010-02-22 20:39:36 +02002801 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002802 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2803 ret = -EINVAL;
2804 goto err_out;
2805 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002806
Florian Fainellif8ac0412010-09-07 13:23:43 +02002807 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002808 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002809 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002810 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002811 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002812 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002813
Kamal Dasu66507c72014-05-01 20:51:19 -04002814 if (part_pagewr)
2815 use_bufpoi = 1;
2816 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2817 use_bufpoi = !virt_addr_valid(buf);
2818 else
2819 use_bufpoi = 0;
2820
2821 /* Partial page write?, or need to use bounce buffer */
2822 if (use_bufpoi) {
2823 pr_debug("%s: using write bounce buffer for buf@%p\n",
2824 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002825 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002826 if (part_pagewr)
2827 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002828 chip->pagebuf = -1;
2829 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2830 memcpy(&chip->buffers->databuf[column], buf, bytes);
2831 wbuf = chip->buffers->databuf;
2832 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002833
Maxim Levitsky782ce792010-02-22 20:39:36 +02002834 if (unlikely(oob)) {
2835 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002836 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002837 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002838 } else {
2839 /* We still need to erase leftover OOB data */
2840 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002841 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302842 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2843 oob_required, page, cached,
2844 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002845 if (ret)
2846 break;
2847
2848 writelen -= bytes;
2849 if (!writelen)
2850 break;
2851
Thomas Gleixner29072b92006-09-28 15:38:36 +02002852 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002853 buf += bytes;
2854 realpage++;
2855
2856 page = realpage & chip->pagemask;
2857 /* Check, if we cross a chip boundary */
2858 if (!page) {
2859 chipnr++;
2860 chip->select_chip(mtd, -1);
2861 chip->select_chip(mtd, chipnr);
2862 }
2863 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002864
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002865 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002866 if (unlikely(oob))
2867 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002868
2869err_out:
2870 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002871 return ret;
2872}
2873
2874/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002875 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002876 * @mtd: MTD device structure
2877 * @to: offset to write to
2878 * @len: number of bytes to write
2879 * @retlen: pointer to variable to store the number of written bytes
2880 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002881 *
2882 * NAND write with ECC. Used when performing writes in interrupt context, this
2883 * may for example be called by mtdoops when writing an oops while in panic.
2884 */
2885static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2886 size_t *retlen, const uint8_t *buf)
2887{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002888 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002889 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002890 int ret;
2891
Brian Norris8b6e50c2011-05-25 14:59:01 -07002892 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002893 panic_nand_wait(mtd, chip, 400);
2894
Brian Norris8b6e50c2011-05-25 14:59:01 -07002895 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002896 panic_nand_get_device(chip, mtd, FL_WRITING);
2897
Brian Norris0ec56dc2015-02-28 02:02:30 -08002898 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002899 ops.len = len;
2900 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002901 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002902
Brian Norris4a89ff82011-08-30 18:45:45 -07002903 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002904
Brian Norris4a89ff82011-08-30 18:45:45 -07002905 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002906 return ret;
2907}
2908
2909/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002910 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002911 * @mtd: MTD device structure
2912 * @to: offset to write to
2913 * @len: number of bytes to write
2914 * @retlen: pointer to variable to store the number of written bytes
2915 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002917 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002919static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002920 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
Brian Norris4a89ff82011-08-30 18:45:45 -07002922 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002923 int ret;
2924
Huang Shijie6a8214a2012-11-19 14:43:30 +08002925 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002926 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002927 ops.len = len;
2928 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002929 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002930 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002931 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002932 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002933 return ret;
2934}
2935
2936/**
2937 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002938 * @mtd: MTD device structure
2939 * @to: offset to write to
2940 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002941 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002942 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002943 */
2944static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2945 struct mtd_oob_ops *ops)
2946{
Adrian Hunter03736152007-01-31 17:58:29 +02002947 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002948 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Brian Norris289c0522011-07-19 10:06:09 -07002950 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302951 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Boris BREZILLON29f10582016-03-07 10:46:52 +01002953 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002956 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002957 pr_debug("%s: attempt to write past end of page\n",
2958 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return -EINVAL;
2960 }
2961
Adrian Hunter03736152007-01-31 17:58:29 +02002962 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002963 pr_debug("%s: attempt to start write outside oob\n",
2964 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002965 return -EINVAL;
2966 }
2967
Jason Liu775adc3d42011-02-25 13:06:18 +08002968 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002969 if (unlikely(to >= mtd->size ||
2970 ops->ooboffs + ops->ooblen >
2971 ((mtd->size >> chip->page_shift) -
2972 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002973 pr_debug("%s: attempt to write beyond end of device\n",
2974 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002975 return -EINVAL;
2976 }
2977
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002978 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002979
2980 /*
2981 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2982 * of my DiskOnChip 2000 test units) will clear the whole data page too
2983 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2984 * it in the doc2000 driver in August 1999. dwmw2.
2985 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002986 nand_reset(chip, chipnr);
2987
2988 chip->select_chip(mtd, chipnr);
2989
2990 /* Shift to get page */
2991 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002994 if (nand_check_wp(mtd)) {
2995 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002996 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002997 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003000 if (page == chip->pagebuf)
3001 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003003 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003004
Brian Norris0612b9d2011-08-30 18:45:40 -07003005 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003006 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3007 else
3008 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003009
Huang Shijieb0bb6902012-11-19 14:43:29 +08003010 chip->select_chip(mtd, -1);
3011
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003012 if (status)
3013 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Vitaly Wool70145682006-11-03 18:20:38 +03003015 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003017 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003020/**
3021 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003022 * @mtd: MTD device structure
3023 * @to: offset to write to
3024 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003025 */
3026static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3027 struct mtd_oob_ops *ops)
3028{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003029 int ret = -ENOTSUPP;
3030
3031 ops->retlen = 0;
3032
3033 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003034 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003035 pr_debug("%s: attempt to write beyond end of device\n",
3036 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003037 return -EINVAL;
3038 }
3039
Huang Shijie6a8214a2012-11-19 14:43:30 +08003040 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003041
Florian Fainellif8ac0412010-09-07 13:23:43 +02003042 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003043 case MTD_OPS_PLACE_OOB:
3044 case MTD_OPS_AUTO_OOB:
3045 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003046 break;
3047
3048 default:
3049 goto out;
3050 }
3051
3052 if (!ops->datbuf)
3053 ret = nand_do_write_oob(mtd, to, ops);
3054 else
3055 ret = nand_do_write_ops(mtd, to, ops);
3056
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003057out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003058 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return ret;
3060}
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062/**
Brian Norris49c50b92014-05-06 16:02:19 -07003063 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003064 * @mtd: MTD device structure
3065 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 *
Brian Norris49c50b92014-05-06 16:02:19 -07003067 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 */
Brian Norris49c50b92014-05-06 16:02:19 -07003069static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003071 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003073 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3074 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003075
3076 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
3079/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003081 * @mtd: MTD device structure
3082 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003084 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003086static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
David Woodhousee0c7d762006-05-13 18:07:53 +01003088 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003092 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003093 * @mtd: MTD device structure
3094 * @instr: erase instruction
3095 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003097 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003099int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3100 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Adrian Hunter69423d92008-12-10 13:37:21 +00003102 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003103 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003104 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Brian Norris289c0522011-07-19 10:06:09 -07003106 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3107 __func__, (unsigned long long)instr->addr,
3108 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303110 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003114 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003117 page = (int)(instr->addr >> chip->page_shift);
3118 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003121 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003124 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 /* Check, if it is write protected */
3127 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003128 pr_debug("%s: device is write protected!\n",
3129 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 instr->state = MTD_ERASE_FAILED;
3131 goto erase_exit;
3132 }
3133
3134 /* Loop through the pages */
3135 len = instr->len;
3136
3137 instr->state = MTD_ERASING;
3138
3139 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003140 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003141 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303142 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003143 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3144 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 instr->state = MTD_ERASE_FAILED;
3146 goto erase_exit;
3147 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003148
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003149 /*
3150 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003151 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003152 */
3153 if (page <= chip->pagebuf && chip->pagebuf <
3154 (page + pages_per_block))
3155 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Brian Norris49c50b92014-05-06 16:02:19 -07003157 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003159 /*
3160 * See if operation failed and additional status checks are
3161 * available
3162 */
3163 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3164 status = chip->errstat(mtd, chip, FL_ERASING,
3165 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003168 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003169 pr_debug("%s: failed erase, page 0x%08x\n",
3170 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003172 instr->fail_addr =
3173 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 goto erase_exit;
3175 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003178 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 page += pages_per_block;
3180
3181 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003182 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003184 chip->select_chip(mtd, -1);
3185 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187 }
3188 instr->state = MTD_ERASE_DONE;
3189
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003190erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003195 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 nand_release_device(mtd);
3197
David Woodhouse49defc02007-10-06 15:01:59 -04003198 /* Do call back function */
3199 if (!ret)
3200 mtd_erase_callback(instr);
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 /* Return more or less happy */
3203 return ret;
3204}
3205
3206/**
3207 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003208 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003210 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003212static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
Brian Norris289c0522011-07-19 10:06:09 -07003214 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003217 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003219 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003223 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003224 * @mtd: MTD device structure
3225 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003227static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303229 struct nand_chip *chip = mtd_to_nand(mtd);
3230 int chipnr = (int)(offs >> chip->chip_shift);
3231 int ret;
3232
3233 /* Select the NAND device */
3234 nand_get_device(mtd, FL_READING);
3235 chip->select_chip(mtd, chipnr);
3236
3237 ret = nand_block_checkbad(mtd, offs, 0);
3238
3239 chip->select_chip(mtd, -1);
3240 nand_release_device(mtd);
3241
3242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
3245/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003246 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003247 * @mtd: MTD device structure
3248 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003250static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 int ret;
3253
Florian Fainellif8ac0412010-09-07 13:23:43 +02003254 ret = nand_block_isbad(mtd, ofs);
3255 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003256 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (ret > 0)
3258 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003259 return ret;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Brian Norris5a0edb22013-07-30 17:52:58 -07003262 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
3265/**
Zach Brown56718422017-01-10 13:30:20 -06003266 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3267 * @mtd: MTD device structure
3268 * @ofs: offset relative to mtd start
3269 * @len: length of mtd
3270 */
3271static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3272{
3273 struct nand_chip *chip = mtd_to_nand(mtd);
3274 u32 part_start_block;
3275 u32 part_end_block;
3276 u32 part_start_die;
3277 u32 part_end_die;
3278
3279 /*
3280 * max_bb_per_die and blocks_per_die used to determine
3281 * the maximum bad block count.
3282 */
3283 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3284 return -ENOTSUPP;
3285
3286 /* Get the start and end of the partition in erase blocks. */
3287 part_start_block = mtd_div_by_eb(ofs, mtd);
3288 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3289
3290 /* Get the start and end LUNs of the partition. */
3291 part_start_die = part_start_block / chip->blocks_per_die;
3292 part_end_die = part_end_block / chip->blocks_per_die;
3293
3294 /*
3295 * Look up the bad blocks per unit and multiply by the number of units
3296 * that the partition spans.
3297 */
3298 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3299}
3300
3301/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003302 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3303 * @mtd: MTD device structure
3304 * @chip: nand chip info structure
3305 * @addr: feature address.
3306 * @subfeature_param: the subfeature parameters, a four bytes array.
3307 */
3308static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3309 int addr, uint8_t *subfeature_param)
3310{
3311 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003312 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003313
David Mosbergerd914c932013-05-29 15:30:13 +03003314 if (!chip->onfi_version ||
3315 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3316 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003317 return -EINVAL;
3318
3319 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003320 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3321 chip->write_byte(mtd, subfeature_param[i]);
3322
Huang Shijie7db03ec2012-09-13 14:57:52 +08003323 status = chip->waitfunc(mtd, chip);
3324 if (status & NAND_STATUS_FAIL)
3325 return -EIO;
3326 return 0;
3327}
3328
3329/**
3330 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3331 * @mtd: MTD device structure
3332 * @chip: nand chip info structure
3333 * @addr: feature address.
3334 * @subfeature_param: the subfeature parameters, a four bytes array.
3335 */
3336static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3337 int addr, uint8_t *subfeature_param)
3338{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003339 int i;
3340
David Mosbergerd914c932013-05-29 15:30:13 +03003341 if (!chip->onfi_version ||
3342 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3343 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003344 return -EINVAL;
3345
Huang Shijie7db03ec2012-09-13 14:57:52 +08003346 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003347 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3348 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003349 return 0;
3350}
3351
3352/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003353 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003354 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003355 */
3356static int nand_suspend(struct mtd_info *mtd)
3357{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003358 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003359}
3360
3361/**
3362 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003363 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003364 */
3365static void nand_resume(struct mtd_info *mtd)
3366{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003367 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003368
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003369 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003370 nand_release_device(mtd);
3371 else
Brian Norrisd0370212011-07-19 10:06:08 -07003372 pr_err("%s called for a chip which is not in suspended state\n",
3373 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003374}
3375
Scott Branden72ea4032014-11-20 11:18:05 -08003376/**
3377 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3378 * prevent further operations
3379 * @mtd: MTD device structure
3380 */
3381static void nand_shutdown(struct mtd_info *mtd)
3382{
Brian Norris9ca641b2015-11-09 16:37:28 -08003383 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003384}
3385
Brian Norris8b6e50c2011-05-25 14:59:01 -07003386/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003387static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003390 if (!chip->chip_delay)
3391 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003394 if (chip->cmdfunc == NULL)
3395 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003398 if (chip->waitfunc == NULL)
3399 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003401 if (!chip->select_chip)
3402 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003403
Huang Shijie4204ccc2013-08-16 10:10:07 +08003404 /* set for ONFI nand */
3405 if (!chip->onfi_set_features)
3406 chip->onfi_set_features = nand_onfi_set_features;
3407 if (!chip->onfi_get_features)
3408 chip->onfi_get_features = nand_onfi_get_features;
3409
Brian Norris68e80782013-07-18 01:17:02 -07003410 /* If called twice, pointers that depend on busw may need to be reset */
3411 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003412 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3413 if (!chip->read_word)
3414 chip->read_word = nand_read_word;
3415 if (!chip->block_bad)
3416 chip->block_bad = nand_block_bad;
3417 if (!chip->block_markbad)
3418 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003419 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003420 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003421 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3422 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003423 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003424 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003425 if (!chip->scan_bbt)
3426 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003427
3428 if (!chip->controller) {
3429 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003430 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003431 }
3432
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003433}
3434
Brian Norris8b6e50c2011-05-25 14:59:01 -07003435/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003436static void sanitize_string(uint8_t *s, size_t len)
3437{
3438 ssize_t i;
3439
Brian Norris8b6e50c2011-05-25 14:59:01 -07003440 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003441 s[len - 1] = 0;
3442
Brian Norris8b6e50c2011-05-25 14:59:01 -07003443 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003444 for (i = 0; i < len - 1; i++) {
3445 if (s[i] < ' ' || s[i] > 127)
3446 s[i] = '?';
3447 }
3448
Brian Norris8b6e50c2011-05-25 14:59:01 -07003449 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003450 strim(s);
3451}
3452
3453static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3454{
3455 int i;
3456 while (len--) {
3457 crc ^= *p++ << 8;
3458 for (i = 0; i < 8; i++)
3459 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3460 }
3461
3462 return crc;
3463}
3464
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003465/* Parse the Extended Parameter Page. */
3466static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3467 struct nand_chip *chip, struct nand_onfi_params *p)
3468{
3469 struct onfi_ext_param_page *ep;
3470 struct onfi_ext_section *s;
3471 struct onfi_ext_ecc_info *ecc;
3472 uint8_t *cursor;
3473 int ret = -EINVAL;
3474 int len;
3475 int i;
3476
3477 len = le16_to_cpu(p->ext_param_page_length) * 16;
3478 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003479 if (!ep)
3480 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003481
3482 /* Send our own NAND_CMD_PARAM. */
3483 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3484
3485 /* Use the Change Read Column command to skip the ONFI param pages. */
3486 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3487 sizeof(*p) * p->num_of_param_pages , -1);
3488
3489 /* Read out the Extended Parameter Page. */
3490 chip->read_buf(mtd, (uint8_t *)ep, len);
3491 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3492 != le16_to_cpu(ep->crc))) {
3493 pr_debug("fail in the CRC.\n");
3494 goto ext_out;
3495 }
3496
3497 /*
3498 * Check the signature.
3499 * Do not strictly follow the ONFI spec, maybe changed in future.
3500 */
3501 if (strncmp(ep->sig, "EPPS", 4)) {
3502 pr_debug("The signature is invalid.\n");
3503 goto ext_out;
3504 }
3505
3506 /* find the ECC section. */
3507 cursor = (uint8_t *)(ep + 1);
3508 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3509 s = ep->sections + i;
3510 if (s->type == ONFI_SECTION_TYPE_2)
3511 break;
3512 cursor += s->length * 16;
3513 }
3514 if (i == ONFI_EXT_SECTION_MAX) {
3515 pr_debug("We can not find the ECC section.\n");
3516 goto ext_out;
3517 }
3518
3519 /* get the info we want. */
3520 ecc = (struct onfi_ext_ecc_info *)cursor;
3521
Brian Norris4ae7d222013-09-16 18:20:21 -07003522 if (!ecc->codeword_size) {
3523 pr_debug("Invalid codeword size\n");
3524 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003525 }
3526
Brian Norris4ae7d222013-09-16 18:20:21 -07003527 chip->ecc_strength_ds = ecc->ecc_bits;
3528 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003529 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003530
3531ext_out:
3532 kfree(ep);
3533 return ret;
3534}
3535
Brian Norris8429bb32013-12-03 15:51:09 -08003536static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3537{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003538 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003539 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3540
3541 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3542 feature);
3543}
3544
3545/*
3546 * Configure chip properties from Micron vendor-specific ONFI table
3547 */
3548static void nand_onfi_detect_micron(struct nand_chip *chip,
3549 struct nand_onfi_params *p)
3550{
3551 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3552
3553 if (le16_to_cpu(p->vendor_revision) < 1)
3554 return;
3555
3556 chip->read_retries = micron->read_retry_options;
3557 chip->setup_read_retry = nand_setup_read_retry_micron;
3558}
3559
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003560/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003561 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003562 */
3563static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003564 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003565{
3566 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003567 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003568 int val;
3569
Brian Norris7854d3f2011-06-23 14:12:08 -07003570 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003571 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3572 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3573 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3574 return 0;
3575
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003576 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3577 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003578 for (j = 0; j < sizeof(*p); j++)
3579 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003580 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3581 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003582 break;
3583 }
3584 }
3585
Brian Norrisc7f23a72013-08-13 10:51:55 -07003586 if (i == 3) {
3587 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003588 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003589 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003590
Brian Norris8b6e50c2011-05-25 14:59:01 -07003591 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003592 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003593 if (val & (1 << 5))
3594 chip->onfi_version = 23;
3595 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003596 chip->onfi_version = 22;
3597 else if (val & (1 << 3))
3598 chip->onfi_version = 21;
3599 else if (val & (1 << 2))
3600 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003601 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003602 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003603
3604 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003605 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003606 return 0;
3607 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003608
3609 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3610 sanitize_string(p->model, sizeof(p->model));
3611 if (!mtd->name)
3612 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003613
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003614 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003615
3616 /*
3617 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3618 * (don't ask me who thought of this...). MTD assumes that these
3619 * dimensions will be power-of-2, so just truncate the remaining area.
3620 */
3621 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3622 mtd->erasesize *= mtd->writesize;
3623
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003624 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003625
3626 /* See erasesize comment */
3627 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003628 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003629 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003630
Zach Brown34da5f52017-01-10 13:30:21 -06003631 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3632 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3633
Huang Shijiee2985fc2013-05-17 11:17:30 +08003634 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003635 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003636 else
3637 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003638
Huang Shijie10c86ba2013-05-17 11:17:26 +08003639 if (p->ecc_bits != 0xff) {
3640 chip->ecc_strength_ds = p->ecc_bits;
3641 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003642 } else if (chip->onfi_version >= 21 &&
3643 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3644
3645 /*
3646 * The nand_flash_detect_ext_param_page() uses the
3647 * Change Read Column command which maybe not supported
3648 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3649 * now. We do not replace user supplied command function.
3650 */
3651 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3652 chip->cmdfunc = nand_command_lp;
3653
3654 /* The Extended Parameter Page is supported since ONFI 2.1. */
3655 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003656 pr_warn("Failed to detect ONFI extended param page\n");
3657 } else {
3658 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003659 }
3660
Brian Norris8429bb32013-12-03 15:51:09 -08003661 if (p->jedec_id == NAND_MFR_MICRON)
3662 nand_onfi_detect_micron(chip, p);
3663
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003664 return 1;
3665}
3666
3667/*
Huang Shijie91361812014-02-21 13:39:40 +08003668 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3669 */
3670static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3671 int *busw)
3672{
3673 struct nand_jedec_params *p = &chip->jedec_params;
3674 struct jedec_ecc_info *ecc;
3675 int val;
3676 int i, j;
3677
3678 /* Try JEDEC for unknown chip or LP */
3679 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3680 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3681 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3682 chip->read_byte(mtd) != 'C')
3683 return 0;
3684
3685 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3686 for (i = 0; i < 3; i++) {
3687 for (j = 0; j < sizeof(*p); j++)
3688 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3689
3690 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3691 le16_to_cpu(p->crc))
3692 break;
3693 }
3694
3695 if (i == 3) {
3696 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3697 return 0;
3698 }
3699
3700 /* Check version */
3701 val = le16_to_cpu(p->revision);
3702 if (val & (1 << 2))
3703 chip->jedec_version = 10;
3704 else if (val & (1 << 1))
3705 chip->jedec_version = 1; /* vendor specific version */
3706
3707 if (!chip->jedec_version) {
3708 pr_info("unsupported JEDEC version: %d\n", val);
3709 return 0;
3710 }
3711
3712 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3713 sanitize_string(p->model, sizeof(p->model));
3714 if (!mtd->name)
3715 mtd->name = p->model;
3716
3717 mtd->writesize = le32_to_cpu(p->byte_per_page);
3718
3719 /* Please reference to the comment for nand_flash_detect_onfi. */
3720 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3721 mtd->erasesize *= mtd->writesize;
3722
3723 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3724
3725 /* Please reference to the comment for nand_flash_detect_onfi. */
3726 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3727 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3728 chip->bits_per_cell = p->bits_per_cell;
3729
3730 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3731 *busw = NAND_BUSWIDTH_16;
3732 else
3733 *busw = 0;
3734
3735 /* ECC info */
3736 ecc = &p->ecc_info[0];
3737
3738 if (ecc->codeword_size >= 9) {
3739 chip->ecc_strength_ds = ecc->ecc_bits;
3740 chip->ecc_step_ds = 1 << ecc->codeword_size;
3741 } else {
3742 pr_warn("Invalid codeword size\n");
3743 }
3744
3745 return 1;
3746}
3747
3748/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003749 * nand_id_has_period - Check if an ID string has a given wraparound period
3750 * @id_data: the ID string
3751 * @arrlen: the length of the @id_data array
3752 * @period: the period of repitition
3753 *
3754 * Check if an ID string is repeated within a given sequence of bytes at
3755 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003756 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003757 * if the repetition has a period of @period; otherwise, returns zero.
3758 */
3759static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3760{
3761 int i, j;
3762 for (i = 0; i < period; i++)
3763 for (j = i + period; j < arrlen; j += period)
3764 if (id_data[i] != id_data[j])
3765 return 0;
3766 return 1;
3767}
3768
3769/*
3770 * nand_id_len - Get the length of an ID string returned by CMD_READID
3771 * @id_data: the ID string
3772 * @arrlen: the length of the @id_data array
3773
3774 * Returns the length of the ID string, according to known wraparound/trailing
3775 * zero patterns. If no pattern exists, returns the length of the array.
3776 */
3777static int nand_id_len(u8 *id_data, int arrlen)
3778{
3779 int last_nonzero, period;
3780
3781 /* Find last non-zero byte */
3782 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3783 if (id_data[last_nonzero])
3784 break;
3785
3786 /* All zeros */
3787 if (last_nonzero < 0)
3788 return 0;
3789
3790 /* Calculate wraparound period */
3791 for (period = 1; period < arrlen; period++)
3792 if (nand_id_has_period(id_data, arrlen, period))
3793 break;
3794
3795 /* There's a repeated pattern */
3796 if (period < arrlen)
3797 return period;
3798
3799 /* There are trailing zeros */
3800 if (last_nonzero < arrlen - 1)
3801 return last_nonzero + 1;
3802
3803 /* No pattern detected */
3804 return arrlen;
3805}
3806
Huang Shijie7db906b2013-09-25 14:58:11 +08003807/* Extract the bits of per cell from the 3rd byte of the extended ID */
3808static int nand_get_bits_per_cell(u8 cellinfo)
3809{
3810 int bits;
3811
3812 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3813 bits >>= NAND_CI_CELLTYPE_SHIFT;
3814 return bits + 1;
3815}
3816
Brian Norrise3b88bd2012-09-24 20:40:52 -07003817/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003818 * Many new NAND share similar device ID codes, which represent the size of the
3819 * chip. The rest of the parameters must be decoded according to generic or
3820 * manufacturer-specific "extended ID" decoding patterns.
3821 */
3822static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3823 u8 id_data[8], int *busw)
3824{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003825 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003826 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003827 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003828 /* The 4th id byte is the important one */
3829 extid = id_data[3];
3830
Brian Norrise3b88bd2012-09-24 20:40:52 -07003831 id_len = nand_id_len(id_data, 8);
3832
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003833 /*
3834 * Field definitions are in the following datasheets:
3835 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003836 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003837 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003838 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003839 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3840 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003841 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003842 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003843 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003844 /* Calc pagesize */
3845 mtd->writesize = 2048 << (extid & 0x03);
3846 extid >>= 2;
3847 /* Calc oobsize */
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003848 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003849 case 1:
3850 mtd->oobsize = 128;
3851 break;
3852 case 2:
3853 mtd->oobsize = 218;
3854 break;
3855 case 3:
3856 mtd->oobsize = 400;
3857 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003858 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003859 mtd->oobsize = 436;
3860 break;
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003861 case 5:
3862 mtd->oobsize = 512;
3863 break;
3864 case 6:
Brian Norrise2d3a35e2012-09-24 20:40:55 -07003865 mtd->oobsize = 640;
3866 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003867 case 7:
3868 default: /* Other cases are "reserved" (unknown) */
3869 mtd->oobsize = 1024;
3870 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003871 }
3872 extid >>= 2;
3873 /* Calc blocksize */
3874 mtd->erasesize = (128 * 1024) <<
3875 (((extid >> 1) & 0x04) | (extid & 0x03));
3876 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003877 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003878 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003879 unsigned int tmp;
3880
3881 /* Calc pagesize */
3882 mtd->writesize = 2048 << (extid & 0x03);
3883 extid >>= 2;
3884 /* Calc oobsize */
3885 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3886 case 0:
3887 mtd->oobsize = 128;
3888 break;
3889 case 1:
3890 mtd->oobsize = 224;
3891 break;
3892 case 2:
3893 mtd->oobsize = 448;
3894 break;
3895 case 3:
3896 mtd->oobsize = 64;
3897 break;
3898 case 4:
3899 mtd->oobsize = 32;
3900 break;
3901 case 5:
3902 mtd->oobsize = 16;
3903 break;
3904 default:
3905 mtd->oobsize = 640;
3906 break;
3907 }
3908 extid >>= 2;
3909 /* Calc blocksize */
3910 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3911 if (tmp < 0x03)
3912 mtd->erasesize = (128 * 1024) << tmp;
3913 else if (tmp == 0x03)
3914 mtd->erasesize = 768 * 1024;
3915 else
3916 mtd->erasesize = (64 * 1024) << tmp;
3917 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003918 } else {
3919 /* Calc pagesize */
3920 mtd->writesize = 1024 << (extid & 0x03);
3921 extid >>= 2;
3922 /* Calc oobsize */
3923 mtd->oobsize = (8 << (extid & 0x01)) *
3924 (mtd->writesize >> 9);
3925 extid >>= 2;
3926 /* Calc blocksize. Blocksize is multiples of 64KiB */
3927 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3928 extid >>= 2;
3929 /* Get buswidth information */
3930 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003931
3932 /*
3933 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3934 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3935 * follows:
3936 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3937 * 110b -> 24nm
3938 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3939 */
3940 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003941 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003942 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3943 !(id_data[4] & 0x80) /* !BENAND */) {
3944 mtd->oobsize = 32 * mtd->writesize >> 9;
3945 }
3946
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003947 }
3948}
3949
3950/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003951 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3952 * decodes a matching ID table entry and assigns the MTD size parameters for
3953 * the chip.
3954 */
3955static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3956 struct nand_flash_dev *type, u8 id_data[8],
3957 int *busw)
3958{
3959 int maf_id = id_data[0];
3960
3961 mtd->erasesize = type->erasesize;
3962 mtd->writesize = type->pagesize;
3963 mtd->oobsize = mtd->writesize / 32;
3964 *busw = type->options & NAND_BUSWIDTH_16;
3965
Huang Shijie1c195e92013-09-25 14:58:12 +08003966 /* All legacy ID NAND are small-page, SLC */
3967 chip->bits_per_cell = 1;
3968
Brian Norrisf23a4812012-09-24 20:40:51 -07003969 /*
3970 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3971 * some Spansion chips have erasesize that conflicts with size
3972 * listed in nand_ids table.
3973 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3974 */
3975 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3976 && id_data[6] == 0x00 && id_data[7] == 0x00
3977 && mtd->writesize == 512) {
3978 mtd->erasesize = 128 * 1024;
3979 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3980 }
3981}
3982
3983/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003984 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3985 * heuristic patterns using various detected parameters (e.g., manufacturer,
3986 * page size, cell-type information).
3987 */
3988static void nand_decode_bbm_options(struct mtd_info *mtd,
3989 struct nand_chip *chip, u8 id_data[8])
3990{
3991 int maf_id = id_data[0];
3992
3993 /* Set the bad block position */
3994 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3995 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3996 else
3997 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3998
3999 /*
4000 * Bad block marker is stored in the last page of each block on Samsung
4001 * and Hynix MLC devices; stored in first two pages of each block on
4002 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4003 * AMD/Spansion, and Macronix. All others scan only the first page.
4004 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004005 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07004006 (maf_id == NAND_MFR_SAMSUNG ||
4007 maf_id == NAND_MFR_HYNIX))
4008 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08004009 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07004010 (maf_id == NAND_MFR_SAMSUNG ||
4011 maf_id == NAND_MFR_HYNIX ||
4012 maf_id == NAND_MFR_TOSHIBA ||
4013 maf_id == NAND_MFR_AMD ||
4014 maf_id == NAND_MFR_MACRONIX)) ||
4015 (mtd->writesize == 2048 &&
4016 maf_id == NAND_MFR_MICRON))
4017 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4018}
4019
Huang Shijieec6e87e2013-03-15 11:01:00 +08004020static inline bool is_full_id_nand(struct nand_flash_dev *type)
4021{
4022 return type->id_len;
4023}
4024
4025static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
4026 struct nand_flash_dev *type, u8 *id_data, int *busw)
4027{
4028 if (!strncmp(type->id, id_data, type->id_len)) {
4029 mtd->writesize = type->pagesize;
4030 mtd->erasesize = type->erasesize;
4031 mtd->oobsize = type->oobsize;
4032
Huang Shijie7db906b2013-09-25 14:58:11 +08004033 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004034 chip->chipsize = (uint64_t)type->chipsize << 20;
4035 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004036 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4037 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004038 chip->onfi_timing_mode_default =
4039 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004040
4041 *busw = type->options & NAND_BUSWIDTH_16;
4042
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004043 if (!mtd->name)
4044 mtd->name = type->name;
4045
Huang Shijieec6e87e2013-03-15 11:01:00 +08004046 return true;
4047 }
4048 return false;
4049}
4050
Brian Norris7e74c2d2012-09-24 20:40:49 -07004051/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004052 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004053 */
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004054static int nand_get_flash_type(struct mtd_info *mtd, struct nand_chip *chip,
4055 int *maf_id, int *dev_id,
4056 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004057{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004058 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004059 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07004060 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Karl Beldanef89a882008-09-15 14:37:29 +02004062 /*
4063 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004064 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004065 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004066 nand_reset(chip, 0);
4067
4068 /* Select the device */
4069 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004072 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
4074 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004075 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004076 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Brian Norris8b6e50c2011-05-25 14:59:01 -07004078 /*
4079 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004080 * interface concerns can cause random data which looks like a
4081 * possibly credible NAND flash to appear. If the two results do
4082 * not match, ignore the device completely.
4083 */
4084
4085 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4086
Brian Norris4aef9b72012-09-24 20:40:48 -07004087 /* Read entire ID string */
4088 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004089 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004090
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004091 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004092 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004093 *maf_id, *dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004094 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004095 }
4096
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004097 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004098 type = nand_flash_ids;
4099
Huang Shijieec6e87e2013-03-15 11:01:00 +08004100 for (; type->name != NULL; type++) {
4101 if (is_full_id_nand(type)) {
4102 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4103 goto ident_done;
4104 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004105 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004106 }
4107 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004108
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004109 chip->onfi_version = 0;
4110 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004111 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07004112 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004113 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004114
4115 /* Check if the chip is JEDEC compliant */
4116 if (nand_flash_detect_jedec(mtd, chip, &busw))
4117 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004118 }
4119
David Woodhouse5e81e882010-02-26 18:32:56 +00004120 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004121 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004122
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004123 if (!mtd->name)
4124 mtd->name = type->name;
4125
Adrian Hunter69423d92008-12-10 13:37:21 +00004126 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004127
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02004128 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004129 /* Decode parameters from extended ID */
4130 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004131 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07004132 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004133 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004134 /* Get chip options */
4135 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004136
Brian Norris8b6e50c2011-05-25 14:59:01 -07004137 /*
4138 * Check if chip is not a Samsung device. Do not clear the
4139 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004140 */
4141 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4142 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4143ident_done:
4144
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004145 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01004146 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004147 if (nand_manuf_ids[maf_idx].id == *maf_id)
4148 break;
4149 }
4150
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004151 if (chip->options & NAND_BUSWIDTH_AUTO) {
4152 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4153 chip->options |= busw;
4154 nand_set_defaults(chip, busw);
4155 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4156 /*
4157 * Check, if buswidth is correct. Hardware drivers should set
4158 * chip correct!
4159 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004160 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4161 *maf_id, *dev_id);
4162 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4163 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004164 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4165 busw ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004166 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004167 }
4168
Brian Norris7e74c2d2012-09-24 20:40:49 -07004169 nand_decode_bbm_options(mtd, chip, id_data);
4170
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004171 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004172 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004173 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004174 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004175
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004176 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004177 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004178 if (chip->chipsize & 0xffffffff)
4179 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004180 else {
4181 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4182 chip->chip_shift += 32 - 1;
4183 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004184
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004185 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004186 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004187
Brian Norris8b6e50c2011-05-25 14:59:01 -07004188 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004189 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4190 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004191
Ezequiel Garcia20171642013-11-25 08:30:31 -03004192 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4193 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004194
4195 if (chip->onfi_version)
4196 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4197 chip->onfi_params.model);
4198 else if (chip->jedec_version)
4199 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4200 chip->jedec_params.model);
4201 else
4202 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4203 type->name);
4204
Rafał Miłecki3755a992014-10-21 00:01:04 +02004205 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004206 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004207 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004208 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004209}
4210
Boris Brezillond48f62b2016-04-01 14:54:32 +02004211static const char * const nand_ecc_modes[] = {
4212 [NAND_ECC_NONE] = "none",
4213 [NAND_ECC_SOFT] = "soft",
4214 [NAND_ECC_HW] = "hw",
4215 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4216 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004217};
4218
4219static int of_get_nand_ecc_mode(struct device_node *np)
4220{
4221 const char *pm;
4222 int err, i;
4223
4224 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4225 if (err < 0)
4226 return err;
4227
4228 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4229 if (!strcasecmp(pm, nand_ecc_modes[i]))
4230 return i;
4231
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004232 /*
4233 * For backward compatibility we support few obsoleted values that don't
4234 * have their mappings into nand_ecc_modes_t anymore (they were merged
4235 * with other enums).
4236 */
4237 if (!strcasecmp(pm, "soft_bch"))
4238 return NAND_ECC_SOFT;
4239
Boris Brezillond48f62b2016-04-01 14:54:32 +02004240 return -ENODEV;
4241}
4242
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004243static const char * const nand_ecc_algos[] = {
4244 [NAND_ECC_HAMMING] = "hamming",
4245 [NAND_ECC_BCH] = "bch",
4246};
4247
Boris Brezillond48f62b2016-04-01 14:54:32 +02004248static int of_get_nand_ecc_algo(struct device_node *np)
4249{
4250 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004251 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004252
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004253 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4254 if (!err) {
4255 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4256 if (!strcasecmp(pm, nand_ecc_algos[i]))
4257 return i;
4258 return -ENODEV;
4259 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004260
4261 /*
4262 * For backward compatibility we also read "nand-ecc-mode" checking
4263 * for some obsoleted values that were specifying ECC algorithm.
4264 */
4265 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4266 if (err < 0)
4267 return err;
4268
4269 if (!strcasecmp(pm, "soft"))
4270 return NAND_ECC_HAMMING;
4271 else if (!strcasecmp(pm, "soft_bch"))
4272 return NAND_ECC_BCH;
4273
4274 return -ENODEV;
4275}
4276
4277static int of_get_nand_ecc_step_size(struct device_node *np)
4278{
4279 int ret;
4280 u32 val;
4281
4282 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4283 return ret ? ret : val;
4284}
4285
4286static int of_get_nand_ecc_strength(struct device_node *np)
4287{
4288 int ret;
4289 u32 val;
4290
4291 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4292 return ret ? ret : val;
4293}
4294
4295static int of_get_nand_bus_width(struct device_node *np)
4296{
4297 u32 val;
4298
4299 if (of_property_read_u32(np, "nand-bus-width", &val))
4300 return 8;
4301
4302 switch (val) {
4303 case 8:
4304 case 16:
4305 return val;
4306 default:
4307 return -EIO;
4308 }
4309}
4310
4311static bool of_get_nand_on_flash_bbt(struct device_node *np)
4312{
4313 return of_property_read_bool(np, "nand-on-flash-bbt");
4314}
4315
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004316static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004317{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004318 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004319 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004320
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004321 if (!dn)
4322 return 0;
4323
Brian Norris5844fee2015-01-23 00:22:27 -08004324 if (of_get_nand_bus_width(dn) == 16)
4325 chip->options |= NAND_BUSWIDTH_16;
4326
4327 if (of_get_nand_on_flash_bbt(dn))
4328 chip->bbt_options |= NAND_BBT_USE_FLASH;
4329
4330 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004331 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004332 ecc_strength = of_get_nand_ecc_strength(dn);
4333 ecc_step = of_get_nand_ecc_step_size(dn);
4334
4335 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4336 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4337 pr_err("must set both strength and step size in DT\n");
4338 return -EINVAL;
4339 }
4340
4341 if (ecc_mode >= 0)
4342 chip->ecc.mode = ecc_mode;
4343
Rafał Miłecki79082452016-03-23 11:19:02 +01004344 if (ecc_algo >= 0)
4345 chip->ecc.algo = ecc_algo;
4346
Brian Norris5844fee2015-01-23 00:22:27 -08004347 if (ecc_strength >= 0)
4348 chip->ecc.strength = ecc_strength;
4349
4350 if (ecc_step > 0)
4351 chip->ecc.size = ecc_step;
4352
Boris Brezillonba78ee02016-06-08 17:04:22 +02004353 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4354 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4355
Brian Norris5844fee2015-01-23 00:22:27 -08004356 return 0;
4357}
4358
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004359/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004360 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004361 * @mtd: MTD device structure
4362 * @maxchips: number of chips to scan for
4363 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004364 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004365 * This is the first phase of the normal nand_scan() function. It reads the
4366 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004367 *
4368 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004369int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4370 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004371{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004372 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004373 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004374 int ret;
4375
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004376 ret = nand_dt_init(chip);
4377 if (ret)
4378 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004379
Brian Norrisf7a8e382016-01-05 10:39:45 -08004380 if (!mtd->name && mtd->dev.parent)
4381 mtd->name = dev_name(mtd->dev.parent);
4382
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004383 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4384 /*
4385 * Default functions assigned for chip_select() and
4386 * cmdfunc() both expect cmd_ctrl() to be populated,
4387 * so we need to check that that's the case
4388 */
4389 pr_err("chip.cmd_ctrl() callback is not provided");
4390 return -EINVAL;
4391 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004392 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004393 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004394
4395 /* Read the flash type */
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004396 ret = nand_get_flash_type(mtd, chip, &nand_maf_id, &nand_dev_id, table);
4397 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004398 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004399 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004400 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004401 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
4403
Boris Brezillon73f907f2016-10-24 16:46:20 +02004404 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004405 ret = nand_init_data_interface(chip);
4406 if (ret)
4407 return ret;
4408
Boris Brezillon73f907f2016-10-24 16:46:20 +02004409 /*
4410 * Setup the data interface correctly on the chip and controller side.
4411 * This explicit call to nand_setup_data_interface() is only required
4412 * for the first die, because nand_reset() has been called before
4413 * ->data_interface and ->default_onfi_timing_mode were set.
4414 * For the other dies, nand_reset() will automatically switch to the
4415 * best mode for us.
4416 */
4417 ret = nand_setup_data_interface(chip);
4418 if (ret)
4419 return ret;
4420
Huang Shijie07300162012-11-09 16:23:45 +08004421 chip->select_chip(mtd, -1);
4422
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004423 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004424 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004425 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004426 nand_reset(chip, i);
4427
4428 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004430 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004432 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004433 nand_dev_id != chip->read_byte(mtd)) {
4434 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 break;
Huang Shijie07300162012-11-09 16:23:45 +08004436 }
4437 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 }
4439 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004440 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004443 chip->numchips = i;
4444 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
David Woodhouse3b85c322006-09-25 17:06:53 +01004446 return 0;
4447}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004448EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004449
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004450static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4451{
4452 struct nand_chip *chip = mtd_to_nand(mtd);
4453 struct nand_ecc_ctrl *ecc = &chip->ecc;
4454
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004455 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004456 return -EINVAL;
4457
4458 switch (ecc->algo) {
4459 case NAND_ECC_HAMMING:
4460 ecc->calculate = nand_calculate_ecc;
4461 ecc->correct = nand_correct_data;
4462 ecc->read_page = nand_read_page_swecc;
4463 ecc->read_subpage = nand_read_subpage;
4464 ecc->write_page = nand_write_page_swecc;
4465 ecc->read_page_raw = nand_read_page_raw;
4466 ecc->write_page_raw = nand_write_page_raw;
4467 ecc->read_oob = nand_read_oob_std;
4468 ecc->write_oob = nand_write_oob_std;
4469 if (!ecc->size)
4470 ecc->size = 256;
4471 ecc->bytes = 3;
4472 ecc->strength = 1;
4473 return 0;
4474 case NAND_ECC_BCH:
4475 if (!mtd_nand_has_bch()) {
4476 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4477 return -EINVAL;
4478 }
4479 ecc->calculate = nand_bch_calculate_ecc;
4480 ecc->correct = nand_bch_correct_data;
4481 ecc->read_page = nand_read_page_swecc;
4482 ecc->read_subpage = nand_read_subpage;
4483 ecc->write_page = nand_write_page_swecc;
4484 ecc->read_page_raw = nand_read_page_raw;
4485 ecc->write_page_raw = nand_write_page_raw;
4486 ecc->read_oob = nand_read_oob_std;
4487 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004488
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004489 /*
4490 * Board driver should supply ecc.size and ecc.strength
4491 * values to select how many bits are correctable.
4492 * Otherwise, default to 4 bits for large page devices.
4493 */
4494 if (!ecc->size && (mtd->oobsize >= 64)) {
4495 ecc->size = 512;
4496 ecc->strength = 4;
4497 }
4498
4499 /*
4500 * if no ecc placement scheme was provided pickup the default
4501 * large page one.
4502 */
4503 if (!mtd->ooblayout) {
4504 /* handle large page devices only */
4505 if (mtd->oobsize < 64) {
4506 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4507 return -EINVAL;
4508 }
4509
4510 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004511
4512 }
4513
4514 /*
4515 * We can only maximize ECC config when the default layout is
4516 * used, otherwise we don't know how many bytes can really be
4517 * used.
4518 */
4519 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4520 ecc->options & NAND_ECC_MAXIMIZE) {
4521 int steps, bytes;
4522
4523 /* Always prefer 1k blocks over 512bytes ones */
4524 ecc->size = 1024;
4525 steps = mtd->writesize / ecc->size;
4526
4527 /* Reserve 2 bytes for the BBM */
4528 bytes = (mtd->oobsize - 2) / steps;
4529 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004530 }
4531
4532 /* See nand_bch_init() for details. */
4533 ecc->bytes = 0;
4534 ecc->priv = nand_bch_init(mtd);
4535 if (!ecc->priv) {
4536 WARN(1, "BCH ECC initialization failed!\n");
4537 return -EINVAL;
4538 }
4539 return 0;
4540 default:
4541 WARN(1, "Unsupported ECC algorithm!\n");
4542 return -EINVAL;
4543 }
4544}
4545
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004546/*
4547 * Check if the chip configuration meet the datasheet requirements.
4548
4549 * If our configuration corrects A bits per B bytes and the minimum
4550 * required correction level is X bits per Y bytes, then we must ensure
4551 * both of the following are true:
4552 *
4553 * (1) A / B >= X / Y
4554 * (2) A >= X
4555 *
4556 * Requirement (1) ensures we can correct for the required bitflip density.
4557 * Requirement (2) ensures we can correct even when all bitflips are clumped
4558 * in the same sector.
4559 */
4560static bool nand_ecc_strength_good(struct mtd_info *mtd)
4561{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004562 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004563 struct nand_ecc_ctrl *ecc = &chip->ecc;
4564 int corr, ds_corr;
4565
4566 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4567 /* Not enough information */
4568 return true;
4569
4570 /*
4571 * We get the number of corrected bits per page to compare
4572 * the correction density.
4573 */
4574 corr = (mtd->writesize * ecc->strength) / ecc->size;
4575 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4576
4577 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4578}
David Woodhouse3b85c322006-09-25 17:06:53 +01004579
Marc Gonzalez3371d662016-11-15 10:56:20 +01004580static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4581{
4582 struct nand_ecc_ctrl *ecc = &chip->ecc;
4583
4584 if (nand_standard_page_accessors(ecc))
4585 return false;
4586
4587 /*
4588 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4589 * controller driver implements all the page accessors because
4590 * default helpers are not suitable when the core does not
4591 * send the READ0/PAGEPROG commands.
4592 */
4593 return (!ecc->read_page || !ecc->write_page ||
4594 !ecc->read_page_raw || !ecc->write_page_raw ||
4595 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4596 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4597 ecc->hwctl && ecc->calculate));
4598}
4599
David Woodhouse3b85c322006-09-25 17:06:53 +01004600/**
4601 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004602 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004603 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004604 * This is the second phase of the normal nand_scan() function. It fills out
4605 * all the uninitialized function pointers with the defaults and scans for a
4606 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004607 */
4608int nand_scan_tail(struct mtd_info *mtd)
4609{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004610 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004611 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004612 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004613 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004614
Brian Norrise2414f42012-02-06 13:44:00 -08004615 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004616 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4617 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4618 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004619
Marc Gonzalez3371d662016-11-15 10:56:20 +01004620 if (invalid_ecc_page_accessors(chip)) {
4621 pr_err("Invalid ECC page accessors setup\n");
4622 return -EINVAL;
4623 }
4624
Huang Shijief02ea4e2014-01-13 14:27:12 +08004625 if (!(chip->options & NAND_OWN_BUFFERS)) {
4626 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4627 + mtd->oobsize * 3, GFP_KERNEL);
4628 if (!nbuf)
4629 return -ENOMEM;
4630 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4631 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4632 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4633
4634 chip->buffers = nbuf;
4635 } else {
4636 if (!chip->buffers)
4637 return -ENOMEM;
4638 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004639
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004640 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004641 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004642
4643 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004644 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004645 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004646 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004647 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004648 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004651 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 break;
4653 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004654 case 128:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004655 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004658 WARN(1, "No oob scheme defined for oobsize %d\n",
4659 mtd->oobsize);
4660 ret = -EINVAL;
4661 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
4663 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004664
David Woodhouse956e9442006-09-25 17:12:39 +01004665 if (!chip->write_page)
4666 chip->write_page = nand_write_page;
4667
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004668 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004669 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004670 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004671 */
David Woodhouse956e9442006-09-25 17:12:39 +01004672
Huang Shijie97de79e02013-10-18 14:20:53 +08004673 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004674 case NAND_ECC_HW_OOB_FIRST:
4675 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004676 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004677 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4678 ret = -EINVAL;
4679 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004680 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004681 if (!ecc->read_page)
4682 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004683
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004684 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004685 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004686 if (!ecc->read_page)
4687 ecc->read_page = nand_read_page_hwecc;
4688 if (!ecc->write_page)
4689 ecc->write_page = nand_write_page_hwecc;
4690 if (!ecc->read_page_raw)
4691 ecc->read_page_raw = nand_read_page_raw;
4692 if (!ecc->write_page_raw)
4693 ecc->write_page_raw = nand_write_page_raw;
4694 if (!ecc->read_oob)
4695 ecc->read_oob = nand_read_oob_std;
4696 if (!ecc->write_oob)
4697 ecc->write_oob = nand_write_oob_std;
4698 if (!ecc->read_subpage)
4699 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004700 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004701 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004702
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004703 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004704 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4705 (!ecc->read_page ||
4706 ecc->read_page == nand_read_page_hwecc ||
4707 !ecc->write_page ||
4708 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004709 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4710 ret = -EINVAL;
4711 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004712 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004713 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004714 if (!ecc->read_page)
4715 ecc->read_page = nand_read_page_syndrome;
4716 if (!ecc->write_page)
4717 ecc->write_page = nand_write_page_syndrome;
4718 if (!ecc->read_page_raw)
4719 ecc->read_page_raw = nand_read_page_raw_syndrome;
4720 if (!ecc->write_page_raw)
4721 ecc->write_page_raw = nand_write_page_raw_syndrome;
4722 if (!ecc->read_oob)
4723 ecc->read_oob = nand_read_oob_syndrome;
4724 if (!ecc->write_oob)
4725 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004726
Huang Shijie97de79e02013-10-18 14:20:53 +08004727 if (mtd->writesize >= ecc->size) {
4728 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004729 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4730 ret = -EINVAL;
4731 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004732 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004733 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004734 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004735 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4736 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004737 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004738 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004740 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004741 ret = nand_set_ecc_soft_ops(mtd);
4742 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004743 ret = -EINVAL;
4744 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004745 }
4746 break;
4747
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004748 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004749 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004750 ecc->read_page = nand_read_page_raw;
4751 ecc->write_page = nand_write_page_raw;
4752 ecc->read_oob = nand_read_oob_std;
4753 ecc->read_page_raw = nand_read_page_raw;
4754 ecc->write_page_raw = nand_write_page_raw;
4755 ecc->write_oob = nand_write_oob_std;
4756 ecc->size = mtd->writesize;
4757 ecc->bytes = 0;
4758 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004762 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4763 ret = -EINVAL;
4764 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Brian Norris9ce244b2011-08-30 18:45:37 -07004767 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004768 if (!ecc->read_oob_raw)
4769 ecc->read_oob_raw = ecc->read_oob;
4770 if (!ecc->write_oob_raw)
4771 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004772
Boris Brezillon846031d2016-02-03 20:11:00 +01004773 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004774 mtd->ecc_strength = ecc->strength;
4775 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004776
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004777 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004778 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004779 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004780 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004781 ecc->steps = mtd->writesize / ecc->size;
4782 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004783 WARN(1, "Invalid ECC parameters\n");
4784 ret = -EINVAL;
4785 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004787 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004788
Boris Brezillon846031d2016-02-03 20:11:00 +01004789 /*
4790 * The number of bytes available for a client to place data into
4791 * the out of band area.
4792 */
4793 ret = mtd_ooblayout_count_freebytes(mtd);
4794 if (ret < 0)
4795 ret = 0;
4796
4797 mtd->oobavail = ret;
4798
4799 /* ECC sanity check: warn if it's too weak */
4800 if (!nand_ecc_strength_good(mtd))
4801 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4802 mtd->name);
4803
Brian Norris8b6e50c2011-05-25 14:59:01 -07004804 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004805 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004806 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004807 case 2:
4808 mtd->subpage_sft = 1;
4809 break;
4810 case 4:
4811 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004812 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004813 mtd->subpage_sft = 2;
4814 break;
4815 }
4816 }
4817 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4818
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004819 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004820 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004823 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004825 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304826 switch (ecc->mode) {
4827 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304828 if (chip->page_shift > 9)
4829 chip->options |= NAND_SUBPAGE_READ;
4830 break;
4831
4832 default:
4833 break;
4834 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004837 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004838 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4839 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004840 mtd->_erase = nand_erase;
4841 mtd->_point = NULL;
4842 mtd->_unpoint = NULL;
4843 mtd->_read = nand_read;
4844 mtd->_write = nand_write;
4845 mtd->_panic_write = panic_nand_write;
4846 mtd->_read_oob = nand_read_oob;
4847 mtd->_write_oob = nand_write_oob;
4848 mtd->_sync = nand_sync;
4849 mtd->_lock = NULL;
4850 mtd->_unlock = NULL;
4851 mtd->_suspend = nand_suspend;
4852 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004853 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004854 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004855 mtd->_block_isbad = nand_block_isbad;
4856 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004857 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004858 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004860 /*
4861 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4862 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4863 * properly set.
4864 */
4865 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004866 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004868 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004869 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004873 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004874err_free:
4875 if (!(chip->options & NAND_OWN_BUFFERS))
4876 kfree(chip->buffers);
4877 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004879EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Brian Norris8b6e50c2011-05-25 14:59:01 -07004881/*
4882 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004883 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004884 * to call us from in-kernel code if the core NAND support is modular.
4885 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004886#ifdef MODULE
4887#define caller_is_module() (1)
4888#else
4889#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004890 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004891#endif
4892
4893/**
4894 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004895 * @mtd: MTD device structure
4896 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004897 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004898 * This fills out all the uninitialized function pointers with the defaults.
4899 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004900 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004901 */
4902int nand_scan(struct mtd_info *mtd, int maxchips)
4903{
4904 int ret;
4905
David Woodhouse5e81e882010-02-26 18:32:56 +00004906 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004907 if (!ret)
4908 ret = nand_scan_tail(mtd);
4909 return ret;
4910}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004911EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004914 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4915 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004916 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004917void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004919 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004920 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004921 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4922
Boris Brezillond8e725d2016-09-15 10:32:50 +02004923 nand_release_data_interface(chip);
4924
Jesper Juhlfa671642005-11-07 01:01:27 -08004925 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004926 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004927 if (!(chip->options & NAND_OWN_BUFFERS))
4928 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004929
4930 /* Free bad block descriptor memory */
4931 if (chip->badblock_pattern && chip->badblock_pattern->options
4932 & NAND_BBT_DYNAMICSTRUCT)
4933 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004935EXPORT_SYMBOL_GPL(nand_cleanup);
4936
4937/**
4938 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4939 * held by the NAND device
4940 * @mtd: MTD device structure
4941 */
4942void nand_release(struct mtd_info *mtd)
4943{
4944 mtd_device_unregister(mtd);
4945 nand_cleanup(mtd_to_nand(mtd));
4946}
David Woodhousee0c7d762006-05-13 18:07:53 +01004947EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004948
David Woodhousee0c7d762006-05-13 18:07:53 +01004949MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004950MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4951MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004952MODULE_DESCRIPTION("Generic NAND flash driver code");