blob: f7616985d95e44e33acc6e6f6b8e744e28b02ee2 [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002 * 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.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02006 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -05007 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020011 *
William Juul52c07962007-10-31 13:53:06 +010012 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020013 * David Woodhouse for adding multichip support
14 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
William Juul52c07962007-10-31 13:53:06 +010018 * TODO:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapin3a38a552013-01-14 03:46:50 +000021 * if we have HW ECC support.
Scott Wood3628f002008-10-24 16:20:43 -050022 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020023 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020024 * 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
Heiko Schocherf5895d12014-06-24 10:10:04 +020030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060032#include <log.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020033#include <malloc.h>
34#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070035#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060036#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060037#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060038#include <linux/delay.h>
William Juul52c07962007-10-31 13:53:06 +010039#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000040#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020041#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090042#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020043#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020044#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020045#ifdef CONFIG_MTD_PARTITIONS
46#include <linux/mtd/partitions.h>
47#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020048#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090049#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020050
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051/* Define default oob placement schemes for large and small page devices */
Gregory CLEMENTe5b96312019-04-17 11:22:05 +020052#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
William Juul52c07962007-10-31 13:53:06 +010053static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020054 .eccbytes = 3,
55 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010056 .oobfree = {
57 {.offset = 3,
58 .length = 2},
59 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020060 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020061};
62
William Juul52c07962007-10-31 13:53:06 +010063static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020064 .eccbytes = 6,
65 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010066 .oobfree = {
67 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020068 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020069};
70
William Juul52c07962007-10-31 13:53:06 +010071static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020072 .eccbytes = 24,
73 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010074 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
77 .oobfree = {
78 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020079 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020080};
81
William Juul52c07962007-10-31 13:53:06 +010082static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020083 .eccbytes = 48,
84 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020085 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010088 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
91 .oobfree = {
92 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020093 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094};
Stefan Agnerbd186142018-12-06 14:57:09 +010095#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020096
Heiko Schocherf5895d12014-06-24 10:10:04 +020097static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010098
99static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
100 struct mtd_oob_ops *ops);
101
Heiko Schocherf5895d12014-06-24 10:10:04 +0200102/*
103 * For devices which display every fart in the system on a separate LED. Is
104 * compiled away when LED support is disabled.
105 */
106DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200107
Christian Hitzb8a6b372011-10-12 09:32:02 +0200108static int check_offs_len(struct mtd_info *mtd,
109 loff_t ofs, uint64_t len)
110{
Scott Wood17fed142016-05-30 13:57:56 -0500111 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200112 int ret = 0;
113
114 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200115 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
116 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200117 ret = -EINVAL;
118 }
119
120 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200121 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
122 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200123 ret = -EINVAL;
124 }
125
Christian Hitzb8a6b372011-10-12 09:32:02 +0200126 return ret;
127}
128
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200129/**
130 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000131 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200132 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200133 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200134 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200135static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100136{
Scott Wood17fed142016-05-30 13:57:56 -0500137 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200138
139 /* De-select the NAND device */
140 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100141}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200142
143/**
144 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000145 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200147 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200148 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000149uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200150{
Scott Wood17fed142016-05-30 13:57:56 -0500151 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100152 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200153}
154
155/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200156 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000157 * @mtd: MTD device structure
158 *
159 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200161 */
William Juul52c07962007-10-31 13:53:06 +0100162static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200163{
Scott Wood17fed142016-05-30 13:57:56 -0500164 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100165 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200166}
167
168/**
169 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000170 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200171 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000172 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200173 */
174static u16 nand_read_word(struct mtd_info *mtd)
175{
Scott Wood17fed142016-05-30 13:57:56 -0500176 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100177 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200178}
179
180/**
181 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000182 * @mtd: MTD device structure
183 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200184 *
185 * Default select function for 1 chip devices.
186 */
William Juul52c07962007-10-31 13:53:06 +0100187static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200188{
Scott Wood17fed142016-05-30 13:57:56 -0500189 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100190
191 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192 case -1:
William Juul52c07962007-10-31 13:53:06 +0100193 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200194 break;
195 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200196 break;
197
198 default:
199 BUG();
200 }
201}
202
203/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200204 * nand_write_byte - [DEFAULT] write single byte to chip
205 * @mtd: MTD device structure
206 * @byte: value to write
207 *
208 * Default function to write a byte to I/O[7:0]
209 */
210static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
211{
Scott Wood17fed142016-05-30 13:57:56 -0500212 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200213
214 chip->write_buf(mtd, &byte, 1);
215}
216
217/**
218 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
219 * @mtd: MTD device structure
220 * @byte: value to write
221 *
222 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
223 */
224static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
225{
Scott Wood17fed142016-05-30 13:57:56 -0500226 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200227 uint16_t word = byte;
228
229 /*
230 * It's not entirely clear what should happen to I/O[15:8] when writing
231 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
232 *
233 * When the host supports a 16-bit bus width, only data is
234 * transferred at the 16-bit width. All address and command line
235 * transfers shall use only the lower 8-bits of the data bus. During
236 * command transfers, the host may place any value on the upper
237 * 8-bits of the data bus. During address transfers, the host shall
238 * set the upper 8-bits of the data bus to 00h.
239 *
240 * One user of the write_byte callback is nand_onfi_set_features. The
241 * four parameters are specified to be written to I/O[7:0], but this is
242 * neither an address nor a command transfer. Let's assume a 0 on the
243 * upper I/O lines is OK.
244 */
245 chip->write_buf(mtd, (uint8_t *)&word, 2);
246}
247
Heiko Schocherf5895d12014-06-24 10:10:04 +0200248static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
249{
250 int i;
251
252 for (i = 0; i < len; i++)
253 writeb(buf[i], addr);
254}
255static void ioread8_rep(void *addr, uint8_t *buf, int len)
256{
257 int i;
258
259 for (i = 0; i < len; i++)
260 buf[i] = readb(addr);
261}
262
263static void ioread16_rep(void *addr, void *buf, int len)
264{
265 int i;
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200266 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200267
Heiko Schocherf5895d12014-06-24 10:10:04 +0200268 for (i = 0; i < len; i++)
269 p[i] = readw(addr);
270}
271
272static void iowrite16_rep(void *addr, void *buf, int len)
273{
274 int i;
275 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200276
277 for (i = 0; i < len; i++)
278 writew(p[i], addr);
279}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200280
281/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200282 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000283 * @mtd: MTD device structure
284 * @buf: data buffer
285 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000287 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000289void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200290{
Scott Wood17fed142016-05-30 13:57:56 -0500291 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200292
Heiko Schocherf5895d12014-06-24 10:10:04 +0200293 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200294}
295
296/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200297 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000298 * @mtd: MTD device structure
299 * @buf: buffer to store date
300 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200301 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000302 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200303 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400304void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305{
Scott Wood17fed142016-05-30 13:57:56 -0500306 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307
Heiko Schocherf5895d12014-06-24 10:10:04 +0200308 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200309}
310
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200311/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200312 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000313 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200314 * @buf: data buffer
315 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200317 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200318 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200319void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200320{
Scott Wood17fed142016-05-30 13:57:56 -0500321 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200322 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200323
Heiko Schocherf5895d12014-06-24 10:10:04 +0200324 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200325}
326
327/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200328 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000329 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200330 * @buf: buffer to store date
331 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200333 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200335void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200336{
Scott Wood17fed142016-05-30 13:57:56 -0500337 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200338 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200339
Heiko Schocherf5895d12014-06-24 10:10:04 +0200340 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200341}
342
343/**
344 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000345 * @mtd: MTD device structure
346 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200347 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200348 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200349 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500350static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500352 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500353 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200354 u16 bad;
355
Sergey Lapin3a38a552013-01-14 03:46:50 +0000356 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200357 ofs += mtd->erasesize - mtd->writesize;
358
William Juul52c07962007-10-31 13:53:06 +0100359 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200360
Sergey Lapin3a38a552013-01-14 03:46:50 +0000361 do {
362 if (chip->options & NAND_BUSWIDTH_16) {
363 chip->cmdfunc(mtd, NAND_CMD_READOOB,
364 chip->badblockpos & 0xFE, page);
365 bad = cpu_to_le16(chip->read_word(mtd));
366 if (chip->badblockpos & 0x1)
367 bad >>= 8;
368 else
369 bad &= 0xFF;
370 } else {
371 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
372 page);
373 bad = chip->read_byte(mtd);
374 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200375
Sergey Lapin3a38a552013-01-14 03:46:50 +0000376 if (likely(chip->badblockbits == 8))
377 res = bad != 0xFF;
378 else
379 res = hweight8(bad) < chip->badblockbits;
380 ofs += mtd->writesize;
381 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
382 i++;
383 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200384
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200385 return res;
386}
387
388/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200389 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000390 * @mtd: MTD device structure
391 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200392 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000393 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200394 * specific driver. It provides the details for writing a bad block marker to a
395 * block.
396 */
397static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398{
Scott Wood17fed142016-05-30 13:57:56 -0500399 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200400 struct mtd_oob_ops ops;
401 uint8_t buf[2] = { 0, 0 };
402 int ret = 0, res, i = 0;
403
Scott Wood3ea94ed2015-06-26 19:03:26 -0500404 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200405 ops.oobbuf = buf;
406 ops.ooboffs = chip->badblockpos;
407 if (chip->options & NAND_BUSWIDTH_16) {
408 ops.ooboffs &= ~0x01;
409 ops.len = ops.ooblen = 2;
410 } else {
411 ops.len = ops.ooblen = 1;
412 }
413 ops.mode = MTD_OPS_PLACE_OOB;
414
415 /* Write to first/last page(s) if necessary */
416 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
417 ofs += mtd->erasesize - mtd->writesize;
418 do {
419 res = nand_do_write_oob(mtd, ofs, &ops);
420 if (!ret)
421 ret = res;
422
423 i++;
424 ofs += mtd->writesize;
425 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
426
427 return ret;
428}
429
430/**
431 * nand_block_markbad_lowlevel - mark a block bad
432 * @mtd: MTD device structure
433 * @ofs: offset from device start
434 *
435 * This function performs the generic NAND bad block marking steps (i.e., bad
436 * block table(s) and/or marker(s)). We only allow the hardware driver to
437 * specify how to write bad block markers to OOB (chip->block_markbad).
438 *
439 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000440 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200441 * (2) write bad block marker to OOB area of affected block (unless flag
442 * NAND_BBT_NO_OOB_BBM is present)
443 * (3) update the BBT
444 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000445 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200446*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200447static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200448{
Scott Wood17fed142016-05-30 13:57:56 -0500449 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200450 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200451
Heiko Schocherf5895d12014-06-24 10:10:04 +0200452 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000453 struct erase_info einfo;
454
455 /* Attempt erase before marking OOB */
456 memset(&einfo, 0, sizeof(einfo));
457 einfo.mtd = mtd;
458 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200459 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000460 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200461
Heiko Schocherf5895d12014-06-24 10:10:04 +0200462 /* Write bad block marker to OOB */
463 nand_get_device(mtd, FL_WRITING);
464 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500465 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100466 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000467
Heiko Schocherf5895d12014-06-24 10:10:04 +0200468 /* Mark block bad in BBT */
469 if (chip->bbt) {
470 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000471 if (!ret)
472 ret = res;
473 }
474
William Juul52c07962007-10-31 13:53:06 +0100475 if (!ret)
476 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500477
William Juul52c07962007-10-31 13:53:06 +0100478 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200479}
480
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200481/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200482 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000483 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200484 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000485 * Check, if the device is write protected. The function expects, that the
486 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200487 */
William Juul52c07962007-10-31 13:53:06 +0100488static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200489{
Scott Wood17fed142016-05-30 13:57:56 -0500490 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100491 u8 status;
492 int ret;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200493
Sergey Lapin3a38a552013-01-14 03:46:50 +0000494 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200495 if (chip->options & NAND_BROKEN_XD)
496 return 0;
497
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200498 /* Check the WP bit */
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100499 ret = nand_status_op(chip, &status);
500 if (ret)
501 return ret;
502
503 return status & NAND_STATUS_WP ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200504}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100505
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200506/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500507 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000508 * @mtd: MTD device structure
509 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300510 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500511 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300512 */
513static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
514{
Scott Wood17fed142016-05-30 13:57:56 -0500515 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300516
517 if (!chip->bbt)
518 return 0;
519 /* Return info from the table */
520 return nand_isreserved_bbt(mtd, ofs);
521}
522
523/**
524 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
525 * @mtd: MTD device structure
526 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000527 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200528 *
529 * Check, if the block is bad. Either by reading the bad block table or
530 * calling of the scan function.
531 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500532static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200533{
Scott Wood17fed142016-05-30 13:57:56 -0500534 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200535
Masahiro Yamada8d100542014-12-26 22:20:58 +0900536 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
537 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200538 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900539 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200540 }
541
William Juul52c07962007-10-31 13:53:06 +0100542 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500543 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200544
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200545 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100546 return nand_isbad_bbt(mtd, ofs, allowbbt);
547}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200548
Scott Wood52ab7ce2016-05-30 13:57:58 -0500549/**
550 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
551 * @mtd: MTD device structure
552 *
553 * Wait for the ready pin after a command, and warn if a timeout occurs.
554 */
William Juul52c07962007-10-31 13:53:06 +0100555void nand_wait_ready(struct mtd_info *mtd)
556{
Scott Wood17fed142016-05-30 13:57:56 -0500557 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500558 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000559 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100560
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000561 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000562 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000563 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100564 if (chip->dev_ready)
565 if (chip->dev_ready(mtd))
566 break;
567 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500568
569 if (!chip->dev_ready(mtd))
570 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200571}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200572EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200573
574/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500575 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
576 * @mtd: MTD device structure
577 * @timeo: Timeout in ms
578 *
579 * Wait for status ready (i.e. command done) or timeout.
580 */
581static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
582{
Scott Wood17fed142016-05-30 13:57:56 -0500583 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500584 u32 time_start;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100585 int ret;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500586
587 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
588 time_start = get_timer(0);
589 while (get_timer(time_start) < timeo) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100590 u8 status;
591
592 ret = nand_read_data_op(chip, &status, sizeof(status), true);
593 if (ret)
594 return;
595
596 if (status & NAND_STATUS_READY)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500597 break;
598 WATCHDOG_RESET();
599 }
600};
601
602/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000604 * @mtd: MTD device structure
605 * @command: the command to be sent
606 * @column: the column address for this command, -1 if none
607 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200608 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000609 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200610 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 */
William Juul52c07962007-10-31 13:53:06 +0100612static void nand_command(struct mtd_info *mtd, unsigned int command,
613 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200614{
Scott Wood17fed142016-05-30 13:57:56 -0500615 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100616 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617
Sergey Lapin3a38a552013-01-14 03:46:50 +0000618 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200619 if (command == NAND_CMD_SEQIN) {
620 int readcmd;
621
William Juul52c07962007-10-31 13:53:06 +0100622 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100624 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200625 readcmd = NAND_CMD_READOOB;
626 } else if (column < 256) {
627 /* First 256 bytes --> READ0 */
628 readcmd = NAND_CMD_READ0;
629 } else {
630 column -= 256;
631 readcmd = NAND_CMD_READ1;
632 }
William Juul52c07962007-10-31 13:53:06 +0100633 chip->cmd_ctrl(mtd, readcmd, ctrl);
634 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200635 }
William Juul52c07962007-10-31 13:53:06 +0100636 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200637
Sergey Lapin3a38a552013-01-14 03:46:50 +0000638 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100639 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
640 /* Serially input address */
641 if (column != -1) {
642 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200643 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530644 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100645 column >>= 1;
646 chip->cmd_ctrl(mtd, column, ctrl);
647 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200648 }
William Juul52c07962007-10-31 13:53:06 +0100649 if (page_addr != -1) {
650 chip->cmd_ctrl(mtd, page_addr, ctrl);
651 ctrl &= ~NAND_CTRL_CHANGE;
652 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900653 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100654 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
655 }
656 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200657
658 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000659 * Program and erase have their own busy handlers status and sequential
660 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100661 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200662 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200663
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200664 case NAND_CMD_PAGEPROG:
665 case NAND_CMD_ERASE1:
666 case NAND_CMD_ERASE2:
667 case NAND_CMD_SEQIN:
668 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900669 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900670 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200671 return;
672
673 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100674 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200675 break;
William Juul52c07962007-10-31 13:53:06 +0100676 udelay(chip->chip_delay);
677 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
678 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
679 chip->cmd_ctrl(mtd,
680 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500681 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
682 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200683 return;
684
William Juul52c07962007-10-31 13:53:06 +0100685 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200687 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200688 * If we don't have access to the busy pin, we apply the given
689 * command delay
William Juul52c07962007-10-31 13:53:06 +0100690 */
691 if (!chip->dev_ready) {
692 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200693 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200694 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200695 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000696 /*
697 * Apply this short delay always to ensure that we do wait tWB in
698 * any case on any machine.
699 */
William Juul52c07962007-10-31 13:53:06 +0100700 ndelay(100);
701
702 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200703}
704
705/**
706 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000707 * @mtd: MTD device structure
708 * @command: the command to be sent
709 * @column: the column address for this command, -1 if none
710 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200711 *
William Juul52c07962007-10-31 13:53:06 +0100712 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000713 * devices. We don't have the separate regions as we have in the small page
714 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715 */
William Juul52c07962007-10-31 13:53:06 +0100716static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
717 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200718{
Scott Wood17fed142016-05-30 13:57:56 -0500719 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200720
721 /* Emulate NAND_CMD_READOOB */
722 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100723 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 command = NAND_CMD_READ0;
725 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200726
William Juul52c07962007-10-31 13:53:06 +0100727 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200728 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200729
730 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100731 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732
733 /* Serially input address */
734 if (column != -1) {
735 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200736 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530737 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200738 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100739 chip->cmd_ctrl(mtd, column, ctrl);
740 ctrl &= ~NAND_CTRL_CHANGE;
741 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200742 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200743 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100744 chip->cmd_ctrl(mtd, page_addr, ctrl);
745 chip->cmd_ctrl(mtd, page_addr >> 8,
746 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900747 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100748 chip->cmd_ctrl(mtd, page_addr >> 16,
749 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200751 }
William Juul52c07962007-10-31 13:53:06 +0100752 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200753
754 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000755 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500756 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100757 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200758 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200759
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200760 case NAND_CMD_CACHEDPROG:
761 case NAND_CMD_PAGEPROG:
762 case NAND_CMD_ERASE1:
763 case NAND_CMD_ERASE2:
764 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100765 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200766 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900767 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900768 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200769 return;
770
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200771 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100772 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200773 break;
William Juul52c07962007-10-31 13:53:06 +0100774 udelay(chip->chip_delay);
775 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
776 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
777 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
778 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500779 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
780 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100781 return;
782
783 case NAND_CMD_RNDOUT:
784 /* No ready / busy check necessary */
785 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
786 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
787 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
788 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200789 return;
790
791 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100792 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
793 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
794 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
795 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200796
William Juul52c07962007-10-31 13:53:06 +0100797 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200798 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200799 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200800 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000801 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100802 */
803 if (!chip->dev_ready) {
804 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200805 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200806 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200807 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200808
Sergey Lapin3a38a552013-01-14 03:46:50 +0000809 /*
810 * Apply this short delay always to ensure that we do wait tWB in
811 * any case on any machine.
812 */
William Juul52c07962007-10-31 13:53:06 +0100813 ndelay(100);
814
815 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200816}
817
818/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200819 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000820 * @chip: the nand chip descriptor
821 * @mtd: MTD device structure
822 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200823 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200824 * Used when in panic, no locks are taken.
825 */
826static void panic_nand_get_device(struct nand_chip *chip,
827 struct mtd_info *mtd, int new_state)
828{
829 /* Hardware controller shared among independent devices */
830 chip->controller->active = chip;
831 chip->state = new_state;
832}
833
834/**
835 * nand_get_device - [GENERIC] Get chip for selected access
836 * @mtd: MTD device structure
837 * @new_state: the state which is requested
838 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200839 * Get the device and lock it for exclusive access
840 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200841static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200842nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100843{
Scott Wood17fed142016-05-30 13:57:56 -0500844 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200845 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100846 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200847}
848
849/**
850 * panic_nand_wait - [GENERIC] wait until the command is done
851 * @mtd: MTD device structure
852 * @chip: NAND chip structure
853 * @timeo: timeout
854 *
855 * Wait for command done. This is a helper function for nand_wait used when
856 * we are in interrupt context. May happen when in panic and trying to write
857 * an oops through mtdoops.
858 */
859static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
860 unsigned long timeo)
861{
862 int i;
863 for (i = 0; i < timeo; i++) {
864 if (chip->dev_ready) {
865 if (chip->dev_ready(mtd))
866 break;
867 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100868 int ret;
869 u8 status;
870
871 ret = nand_read_data_op(chip, &status, sizeof(status),
872 true);
873 if (ret)
874 return;
875
876 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200877 break;
878 }
879 mdelay(1);
880 }
William Juul52c07962007-10-31 13:53:06 +0100881}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200882
883/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000884 * nand_wait - [DEFAULT] wait until the command is done
885 * @mtd: MTD device structure
886 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200887 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500888 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100889 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200890static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200891{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500892 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100893 u8 status;
894 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100895
Heiko Schocherf5895d12014-06-24 10:10:04 +0200896 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100897
Heiko Schocherf5895d12014-06-24 10:10:04 +0200898 /*
899 * Apply this short delay always to ensure that we do wait tWB in any
900 * case on any machine.
901 */
902 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100903
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100904 ret = nand_status_op(chip, NULL);
905 if (ret)
906 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100907
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200908 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
909 u32 time_start;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200910
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200911 time_start = get_timer(0);
912 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200913 if (chip->dev_ready) {
914 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100915 break;
916 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100917 ret = nand_read_data_op(chip, &status,
918 sizeof(status), true);
919 if (ret)
920 return ret;
921
922 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100923 break;
924 }
925 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200926 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100927
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100928 ret = nand_read_data_op(chip, &status, sizeof(status), true);
929 if (ret)
930 return ret;
931
Heiko Schocherf5895d12014-06-24 10:10:04 +0200932 /* This can happen if in case of timeout or buggy dev_ready */
933 WARN_ON(!(status & NAND_STATUS_READY));
934 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200935}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500936
Scott Wood52ab7ce2016-05-30 13:57:58 -0500937/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900938 * nand_reset_data_interface - Reset data interface and timings
939 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900940 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900941 *
942 * Reset the Data interface and timings to ONFI mode 0.
943 *
944 * Returns 0 for success or negative error code otherwise.
945 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900946static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900947{
948 struct mtd_info *mtd = nand_to_mtd(chip);
949 const struct nand_data_interface *conf;
950 int ret;
951
952 if (!chip->setup_data_interface)
953 return 0;
954
955 /*
956 * The ONFI specification says:
957 * "
958 * To transition from NV-DDR or NV-DDR2 to the SDR data
959 * interface, the host shall use the Reset (FFh) command
960 * using SDR timing mode 0. A device in any timing mode is
961 * required to recognize Reset (FFh) command issued in SDR
962 * timing mode 0.
963 * "
964 *
965 * Configure the data interface in SDR mode and set the
966 * timings to timing mode 0.
967 */
968
969 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900970 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900971 if (ret)
972 pr_err("Failed to configure data interface to SDR timing mode 0\n");
973
974 return ret;
975}
976
977/**
978 * nand_setup_data_interface - Setup the best data interface and timings
979 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900980 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900981 *
982 * Find and configure the best data interface and NAND timings supported by
983 * the chip and the driver.
984 * First tries to retrieve supported timing modes from ONFI information,
985 * and if the NAND chip does not support ONFI, relies on the
986 * ->onfi_timing_mode_default specified in the nand_ids table.
987 *
988 * Returns 0 for success or negative error code otherwise.
989 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900990static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900991{
992 struct mtd_info *mtd = nand_to_mtd(chip);
993 int ret;
994
995 if (!chip->setup_data_interface || !chip->data_interface)
996 return 0;
997
998 /*
999 * Ensure the timing mode has been changed on the chip side
1000 * before changing timings on the controller side.
1001 */
1002 if (chip->onfi_version) {
1003 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1004 chip->onfi_timing_mode_default,
1005 };
1006
1007 ret = chip->onfi_set_features(mtd, chip,
1008 ONFI_FEATURE_ADDR_TIMING_MODE,
1009 tmode_param);
1010 if (ret)
1011 goto err;
1012 }
1013
Boris Brezillon32935f42017-11-22 02:38:28 +09001014 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001015err:
1016 return ret;
1017}
1018
1019/**
1020 * nand_init_data_interface - find the best data interface and timings
1021 * @chip: The NAND chip
1022 *
1023 * Find the best data interface and NAND timings supported by the chip
1024 * and the driver.
1025 * First tries to retrieve supported timing modes from ONFI information,
1026 * and if the NAND chip does not support ONFI, relies on the
1027 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1028 * function nand_chip->data_interface is initialized with the best timing mode
1029 * available.
1030 *
1031 * Returns 0 for success or negative error code otherwise.
1032 */
1033static int nand_init_data_interface(struct nand_chip *chip)
1034{
1035 struct mtd_info *mtd = nand_to_mtd(chip);
1036 int modes, mode, ret;
1037
1038 if (!chip->setup_data_interface)
1039 return 0;
1040
1041 /*
1042 * First try to identify the best timings from ONFI parameters and
1043 * if the NAND does not support ONFI, fallback to the default ONFI
1044 * timing mode.
1045 */
1046 modes = onfi_get_async_timing_mode(chip);
1047 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1048 if (!chip->onfi_timing_mode_default)
1049 return 0;
1050
1051 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1052 }
1053
1054 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1055 GFP_KERNEL);
1056 if (!chip->data_interface)
1057 return -ENOMEM;
1058
1059 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1060 ret = onfi_init_data_interface(chip, chip->data_interface,
1061 NAND_SDR_IFACE, mode);
1062 if (ret)
1063 continue;
1064
Boris Brezillon32935f42017-11-22 02:38:28 +09001065 /* Pass -1 to only */
1066 ret = chip->setup_data_interface(mtd,
1067 NAND_DATA_IFACE_CHECK_ONLY,
1068 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001069 if (!ret) {
1070 chip->onfi_timing_mode_default = mode;
1071 break;
1072 }
1073 }
1074
1075 return 0;
1076}
1077
1078static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1079{
1080 kfree(chip->data_interface);
1081}
1082
1083/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001084 * nand_read_page_op - Do a READ PAGE operation
1085 * @chip: The NAND chip
1086 * @page: page to read
1087 * @offset_in_page: offset within the page
1088 * @buf: buffer used to store the data
1089 * @len: length of the buffer
1090 *
1091 * This function issues a READ PAGE operation.
1092 * This function does not select/unselect the CS line.
1093 *
1094 * Returns 0 on success, a negative error code otherwise.
1095 */
1096int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1097 unsigned int offset_in_page, void *buf, unsigned int len)
1098{
1099 struct mtd_info *mtd = nand_to_mtd(chip);
1100
1101 if (len && !buf)
1102 return -EINVAL;
1103
1104 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1105 return -EINVAL;
1106
1107 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1108 if (len)
1109 chip->read_buf(mtd, buf, len);
1110
1111 return 0;
1112}
1113EXPORT_SYMBOL_GPL(nand_read_page_op);
1114
1115/**
1116 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1117 * @chip: The NAND chip
1118 * @page: parameter page to read
1119 * @buf: buffer used to store the data
1120 * @len: length of the buffer
1121 *
1122 * This function issues a READ PARAMETER PAGE operation.
1123 * This function does not select/unselect the CS line.
1124 *
1125 * Returns 0 on success, a negative error code otherwise.
1126 */
1127static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1128 unsigned int len)
1129{
1130 struct mtd_info *mtd = nand_to_mtd(chip);
1131 unsigned int i;
1132 u8 *p = buf;
1133
1134 if (len && !buf)
1135 return -EINVAL;
1136
1137 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1138 for (i = 0; i < len; i++)
1139 p[i] = chip->read_byte(mtd);
1140
1141 return 0;
1142}
1143
1144/**
1145 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1146 * @chip: The NAND chip
1147 * @offset_in_page: offset within the page
1148 * @buf: buffer used to store the data
1149 * @len: length of the buffer
1150 * @force_8bit: force 8-bit bus access
1151 *
1152 * This function issues a CHANGE READ COLUMN operation.
1153 * This function does not select/unselect the CS line.
1154 *
1155 * Returns 0 on success, a negative error code otherwise.
1156 */
1157int nand_change_read_column_op(struct nand_chip *chip,
1158 unsigned int offset_in_page, void *buf,
1159 unsigned int len, bool force_8bit)
1160{
1161 struct mtd_info *mtd = nand_to_mtd(chip);
1162
1163 if (len && !buf)
1164 return -EINVAL;
1165
1166 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1167 return -EINVAL;
1168
1169 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1170 if (len)
1171 chip->read_buf(mtd, buf, len);
1172
1173 return 0;
1174}
1175EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1176
1177/**
1178 * nand_read_oob_op - Do a READ OOB operation
1179 * @chip: The NAND chip
1180 * @page: page to read
1181 * @offset_in_oob: offset within the OOB area
1182 * @buf: buffer used to store the data
1183 * @len: length of the buffer
1184 *
1185 * This function issues a READ OOB operation.
1186 * This function does not select/unselect the CS line.
1187 *
1188 * Returns 0 on success, a negative error code otherwise.
1189 */
1190int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1191 unsigned int offset_in_oob, void *buf, unsigned int len)
1192{
1193 struct mtd_info *mtd = nand_to_mtd(chip);
1194
1195 if (len && !buf)
1196 return -EINVAL;
1197
1198 if (offset_in_oob + len > mtd->oobsize)
1199 return -EINVAL;
1200
1201 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1202 if (len)
1203 chip->read_buf(mtd, buf, len);
1204
1205 return 0;
1206}
1207EXPORT_SYMBOL_GPL(nand_read_oob_op);
1208
1209/**
1210 * nand_prog_page_begin_op - starts a PROG PAGE operation
1211 * @chip: The NAND chip
1212 * @page: page to write
1213 * @offset_in_page: offset within the page
1214 * @buf: buffer containing the data to write to the page
1215 * @len: length of the buffer
1216 *
1217 * This function issues the first half of a PROG PAGE operation.
1218 * This function does not select/unselect the CS line.
1219 *
1220 * Returns 0 on success, a negative error code otherwise.
1221 */
1222int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1223 unsigned int offset_in_page, const void *buf,
1224 unsigned int len)
1225{
1226 struct mtd_info *mtd = nand_to_mtd(chip);
1227
1228 if (len && !buf)
1229 return -EINVAL;
1230
1231 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1232 return -EINVAL;
1233
1234 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1235
1236 if (buf)
1237 chip->write_buf(mtd, buf, len);
1238
1239 return 0;
1240}
1241EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1242
1243/**
1244 * nand_prog_page_end_op - ends a PROG PAGE operation
1245 * @chip: The NAND chip
1246 *
1247 * This function issues the second half of a PROG PAGE operation.
1248 * This function does not select/unselect the CS line.
1249 *
1250 * Returns 0 on success, a negative error code otherwise.
1251 */
1252int nand_prog_page_end_op(struct nand_chip *chip)
1253{
1254 struct mtd_info *mtd = nand_to_mtd(chip);
1255 int status;
1256
1257 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1258
1259 status = chip->waitfunc(mtd, chip);
1260 if (status & NAND_STATUS_FAIL)
1261 return -EIO;
1262
1263 return 0;
1264}
1265EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1266
1267/**
1268 * nand_prog_page_op - Do a full PROG PAGE operation
1269 * @chip: The NAND chip
1270 * @page: page to write
1271 * @offset_in_page: offset within the page
1272 * @buf: buffer containing the data to write to the page
1273 * @len: length of the buffer
1274 *
1275 * This function issues a full PROG PAGE operation.
1276 * This function does not select/unselect the CS line.
1277 *
1278 * Returns 0 on success, a negative error code otherwise.
1279 */
1280int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1281 unsigned int offset_in_page, const void *buf,
1282 unsigned int len)
1283{
1284 struct mtd_info *mtd = nand_to_mtd(chip);
1285 int status;
1286
1287 if (!len || !buf)
1288 return -EINVAL;
1289
1290 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1291 return -EINVAL;
1292
1293 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1294 chip->write_buf(mtd, buf, len);
1295 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1296
1297 status = chip->waitfunc(mtd, chip);
1298 if (status & NAND_STATUS_FAIL)
1299 return -EIO;
1300
1301 return 0;
1302}
1303EXPORT_SYMBOL_GPL(nand_prog_page_op);
1304
1305/**
1306 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1307 * @chip: The NAND chip
1308 * @offset_in_page: offset within the page
1309 * @buf: buffer containing the data to send to the NAND
1310 * @len: length of the buffer
1311 * @force_8bit: force 8-bit bus access
1312 *
1313 * This function issues a CHANGE WRITE COLUMN operation.
1314 * This function does not select/unselect the CS line.
1315 *
1316 * Returns 0 on success, a negative error code otherwise.
1317 */
1318int nand_change_write_column_op(struct nand_chip *chip,
1319 unsigned int offset_in_page,
1320 const void *buf, unsigned int len,
1321 bool force_8bit)
1322{
1323 struct mtd_info *mtd = nand_to_mtd(chip);
1324
1325 if (len && !buf)
1326 return -EINVAL;
1327
1328 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1329 return -EINVAL;
1330
1331 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1332 if (len)
1333 chip->write_buf(mtd, buf, len);
1334
1335 return 0;
1336}
1337EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1338
1339/**
1340 * nand_readid_op - Do a READID operation
1341 * @chip: The NAND chip
1342 * @addr: address cycle to pass after the READID command
1343 * @buf: buffer used to store the ID
1344 * @len: length of the buffer
1345 *
1346 * This function sends a READID command and reads back the ID returned by the
1347 * NAND.
1348 * This function does not select/unselect the CS line.
1349 *
1350 * Returns 0 on success, a negative error code otherwise.
1351 */
1352int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1353 unsigned int len)
1354{
1355 struct mtd_info *mtd = nand_to_mtd(chip);
1356 unsigned int i;
1357 u8 *id = buf;
1358
1359 if (len && !buf)
1360 return -EINVAL;
1361
1362 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1363
1364 for (i = 0; i < len; i++)
1365 id[i] = chip->read_byte(mtd);
1366
1367 return 0;
1368}
1369EXPORT_SYMBOL_GPL(nand_readid_op);
1370
1371/**
1372 * nand_status_op - Do a STATUS operation
1373 * @chip: The NAND chip
1374 * @status: out variable to store the NAND status
1375 *
1376 * This function sends a STATUS command and reads back the status returned by
1377 * the NAND.
1378 * This function does not select/unselect the CS line.
1379 *
1380 * Returns 0 on success, a negative error code otherwise.
1381 */
1382int nand_status_op(struct nand_chip *chip, u8 *status)
1383{
1384 struct mtd_info *mtd = nand_to_mtd(chip);
1385
1386 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1387 if (status)
1388 *status = chip->read_byte(mtd);
1389
1390 return 0;
1391}
1392EXPORT_SYMBOL_GPL(nand_status_op);
1393
1394/**
1395 * nand_exit_status_op - Exit a STATUS operation
1396 * @chip: The NAND chip
1397 *
1398 * This function sends a READ0 command to cancel the effect of the STATUS
1399 * command to avoid reading only the status until a new read command is sent.
1400 *
1401 * This function does not select/unselect the CS line.
1402 *
1403 * Returns 0 on success, a negative error code otherwise.
1404 */
1405int nand_exit_status_op(struct nand_chip *chip)
1406{
1407 struct mtd_info *mtd = nand_to_mtd(chip);
1408
1409 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1410
1411 return 0;
1412}
1413EXPORT_SYMBOL_GPL(nand_exit_status_op);
1414
1415/**
1416 * nand_erase_op - Do an erase operation
1417 * @chip: The NAND chip
1418 * @eraseblock: block to erase
1419 *
1420 * This function sends an ERASE command and waits for the NAND to be ready
1421 * before returning.
1422 * This function does not select/unselect the CS line.
1423 *
1424 * Returns 0 on success, a negative error code otherwise.
1425 */
1426int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1427{
1428 struct mtd_info *mtd = nand_to_mtd(chip);
1429 unsigned int page = eraseblock <<
1430 (chip->phys_erase_shift - chip->page_shift);
1431 int status;
1432
1433 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1434 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1435
1436 status = chip->waitfunc(mtd, chip);
1437 if (status < 0)
1438 return status;
1439
1440 if (status & NAND_STATUS_FAIL)
1441 return -EIO;
1442
1443 return 0;
1444}
1445EXPORT_SYMBOL_GPL(nand_erase_op);
1446
1447/**
1448 * nand_set_features_op - Do a SET FEATURES operation
1449 * @chip: The NAND chip
1450 * @feature: feature id
1451 * @data: 4 bytes of data
1452 *
1453 * This function sends a SET FEATURES command and waits for the NAND to be
1454 * ready before returning.
1455 * This function does not select/unselect the CS line.
1456 *
1457 * Returns 0 on success, a negative error code otherwise.
1458 */
1459static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1460 const void *data)
1461{
1462 struct mtd_info *mtd = nand_to_mtd(chip);
1463 const u8 *params = data;
1464 int i, status;
1465
1466 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1467 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1468 chip->write_byte(mtd, params[i]);
1469
1470 status = chip->waitfunc(mtd, chip);
1471 if (status & NAND_STATUS_FAIL)
1472 return -EIO;
1473
1474 return 0;
1475}
1476
1477/**
1478 * nand_get_features_op - Do a GET FEATURES operation
1479 * @chip: The NAND chip
1480 * @feature: feature id
1481 * @data: 4 bytes of data
1482 *
1483 * This function sends a GET FEATURES command and waits for the NAND to be
1484 * ready before returning.
1485 * This function does not select/unselect the CS line.
1486 *
1487 * Returns 0 on success, a negative error code otherwise.
1488 */
1489static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1490 void *data)
1491{
1492 struct mtd_info *mtd = nand_to_mtd(chip);
1493 u8 *params = data;
1494 int i;
1495
1496 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1497 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1498 params[i] = chip->read_byte(mtd);
1499
1500 return 0;
1501}
1502
1503/**
1504 * nand_reset_op - Do a reset operation
1505 * @chip: The NAND chip
1506 *
1507 * This function sends a RESET command and waits for the NAND to be ready
1508 * before returning.
1509 * This function does not select/unselect the CS line.
1510 *
1511 * Returns 0 on success, a negative error code otherwise.
1512 */
1513int nand_reset_op(struct nand_chip *chip)
1514{
1515 struct mtd_info *mtd = nand_to_mtd(chip);
1516
1517 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1518
1519 return 0;
1520}
1521EXPORT_SYMBOL_GPL(nand_reset_op);
1522
1523/**
1524 * nand_read_data_op - Read data from the NAND
1525 * @chip: The NAND chip
1526 * @buf: buffer used to store the data
1527 * @len: length of the buffer
1528 * @force_8bit: force 8-bit bus access
1529 *
1530 * This function does a raw data read on the bus. Usually used after launching
1531 * another NAND operation like nand_read_page_op().
1532 * This function does not select/unselect the CS line.
1533 *
1534 * Returns 0 on success, a negative error code otherwise.
1535 */
1536int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1537 bool force_8bit)
1538{
1539 struct mtd_info *mtd = nand_to_mtd(chip);
1540
1541 if (!len || !buf)
1542 return -EINVAL;
1543
1544 if (force_8bit) {
1545 u8 *p = buf;
1546 unsigned int i;
1547
1548 for (i = 0; i < len; i++)
1549 p[i] = chip->read_byte(mtd);
1550 } else {
1551 chip->read_buf(mtd, buf, len);
1552 }
1553
1554 return 0;
1555}
1556EXPORT_SYMBOL_GPL(nand_read_data_op);
1557
1558/**
1559 * nand_write_data_op - Write data from the NAND
1560 * @chip: The NAND chip
1561 * @buf: buffer containing the data to send on the bus
1562 * @len: length of the buffer
1563 * @force_8bit: force 8-bit bus access
1564 *
1565 * This function does a raw data write on the bus. Usually used after launching
1566 * another NAND operation like nand_write_page_begin_op().
1567 * This function does not select/unselect the CS line.
1568 *
1569 * Returns 0 on success, a negative error code otherwise.
1570 */
1571int nand_write_data_op(struct nand_chip *chip, const void *buf,
1572 unsigned int len, bool force_8bit)
1573{
1574 struct mtd_info *mtd = nand_to_mtd(chip);
1575
1576 if (!len || !buf)
1577 return -EINVAL;
1578
1579 if (force_8bit) {
1580 const u8 *p = buf;
1581 unsigned int i;
1582
1583 for (i = 0; i < len; i++)
1584 chip->write_byte(mtd, p[i]);
1585 } else {
1586 chip->write_buf(mtd, buf, len);
1587 }
1588
1589 return 0;
1590}
1591EXPORT_SYMBOL_GPL(nand_write_data_op);
1592
1593/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001594 * nand_reset - Reset and initialize a NAND device
1595 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001596 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001597 *
1598 * Returns 0 for success or negative error code otherwise
1599 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001600int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001601{
1602 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001603 int ret;
1604
Boris Brezillon32935f42017-11-22 02:38:28 +09001605 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001606 if (ret)
1607 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001608
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001609 /*
1610 * The CS line has to be released before we can apply the new NAND
1611 * interface settings, hence this weird ->select_chip() dance.
1612 */
1613 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001614 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001615 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001616 if (ret)
1617 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001618
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001619 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001620 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001621 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001622 if (ret)
1623 return ret;
1624
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001625 return 0;
1626}
1627
1628/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001629 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1630 * @buf: buffer to test
1631 * @len: buffer length
1632 * @bitflips_threshold: maximum number of bitflips
1633 *
1634 * Check if a buffer contains only 0xff, which means the underlying region
1635 * has been erased and is ready to be programmed.
1636 * The bitflips_threshold specify the maximum number of bitflips before
1637 * considering the region is not erased.
1638 * Note: The logic of this function has been extracted from the memweight
1639 * implementation, except that nand_check_erased_buf function exit before
1640 * testing the whole buffer if the number of bitflips exceed the
1641 * bitflips_threshold value.
1642 *
1643 * Returns a positive number of bitflips less than or equal to
1644 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1645 * threshold.
1646 */
1647static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1648{
1649 const unsigned char *bitmap = buf;
1650 int bitflips = 0;
1651 int weight;
1652
1653 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1654 len--, bitmap++) {
1655 weight = hweight8(*bitmap);
1656 bitflips += BITS_PER_BYTE - weight;
1657 if (unlikely(bitflips > bitflips_threshold))
1658 return -EBADMSG;
1659 }
1660
1661 for (; len >= 4; len -= 4, bitmap += 4) {
1662 weight = hweight32(*((u32 *)bitmap));
1663 bitflips += 32 - weight;
1664 if (unlikely(bitflips > bitflips_threshold))
1665 return -EBADMSG;
1666 }
1667
1668 for (; len > 0; len--, bitmap++) {
1669 weight = hweight8(*bitmap);
1670 bitflips += BITS_PER_BYTE - weight;
1671 if (unlikely(bitflips > bitflips_threshold))
1672 return -EBADMSG;
1673 }
1674
1675 return bitflips;
1676}
1677
1678/**
1679 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1680 * 0xff data
1681 * @data: data buffer to test
1682 * @datalen: data length
1683 * @ecc: ECC buffer
1684 * @ecclen: ECC length
1685 * @extraoob: extra OOB buffer
1686 * @extraooblen: extra OOB length
1687 * @bitflips_threshold: maximum number of bitflips
1688 *
1689 * Check if a data buffer and its associated ECC and OOB data contains only
1690 * 0xff pattern, which means the underlying region has been erased and is
1691 * ready to be programmed.
1692 * The bitflips_threshold specify the maximum number of bitflips before
1693 * considering the region as not erased.
1694 *
1695 * Note:
1696 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1697 * different from the NAND page size. When fixing bitflips, ECC engines will
1698 * report the number of errors per chunk, and the NAND core infrastructure
1699 * expect you to return the maximum number of bitflips for the whole page.
1700 * This is why you should always use this function on a single chunk and
1701 * not on the whole page. After checking each chunk you should update your
1702 * max_bitflips value accordingly.
1703 * 2/ When checking for bitflips in erased pages you should not only check
1704 * the payload data but also their associated ECC data, because a user might
1705 * have programmed almost all bits to 1 but a few. In this case, we
1706 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1707 * this case.
1708 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1709 * data are protected by the ECC engine.
1710 * It could also be used if you support subpages and want to attach some
1711 * extra OOB data to an ECC chunk.
1712 *
1713 * Returns a positive number of bitflips less than or equal to
1714 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1715 * threshold. In case of success, the passed buffers are filled with 0xff.
1716 */
1717int nand_check_erased_ecc_chunk(void *data, int datalen,
1718 void *ecc, int ecclen,
1719 void *extraoob, int extraooblen,
1720 int bitflips_threshold)
1721{
1722 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1723
1724 data_bitflips = nand_check_erased_buf(data, datalen,
1725 bitflips_threshold);
1726 if (data_bitflips < 0)
1727 return data_bitflips;
1728
1729 bitflips_threshold -= data_bitflips;
1730
1731 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1732 if (ecc_bitflips < 0)
1733 return ecc_bitflips;
1734
1735 bitflips_threshold -= ecc_bitflips;
1736
1737 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1738 bitflips_threshold);
1739 if (extraoob_bitflips < 0)
1740 return extraoob_bitflips;
1741
1742 if (data_bitflips)
1743 memset(data, 0xff, datalen);
1744
1745 if (ecc_bitflips)
1746 memset(ecc, 0xff, ecclen);
1747
1748 if (extraoob_bitflips)
1749 memset(extraoob, 0xff, extraooblen);
1750
1751 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1752}
1753EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001754
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001755/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001756 * nand_read_page_raw - [INTERN] read raw page data without ecc
1757 * @mtd: mtd info structure
1758 * @chip: nand chip info structure
1759 * @buf: buffer to store read data
1760 * @oob_required: caller requires OOB data read to chip->oob_poi
1761 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001762 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001763 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001764 */
William Juul52c07962007-10-31 13:53:06 +01001765static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001766 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001767{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001768 int ret;
1769
1770 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1771 if (ret)
1772 return ret;
1773
1774 if (oob_required) {
1775 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1776 false);
1777 if (ret)
1778 return ret;
1779 }
1780
William Juul52c07962007-10-31 13:53:06 +01001781 return 0;
1782}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001783
William Juul52c07962007-10-31 13:53:06 +01001784/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001785 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1786 * @mtd: mtd info structure
1787 * @chip: nand chip info structure
1788 * @buf: buffer to store read data
1789 * @oob_required: caller requires OOB data read to chip->oob_poi
1790 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001791 *
1792 * We need a special oob layout and handling even when OOB isn't used.
1793 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001794static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001795 struct nand_chip *chip, uint8_t *buf,
1796 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001797{
1798 int eccsize = chip->ecc.size;
1799 int eccbytes = chip->ecc.bytes;
1800 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001801 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001802
1803 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001804 ret = nand_read_data_op(chip, buf, eccsize, false);
1805 if (ret)
1806 return ret;
1807
David Brownellee86b8d2009-11-07 16:27:01 -05001808 buf += eccsize;
1809
1810 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001811 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1812 false);
1813 if (ret)
1814 return ret;
1815
David Brownellee86b8d2009-11-07 16:27:01 -05001816 oob += chip->ecc.prepad;
1817 }
1818
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001819 ret = nand_read_data_op(chip, oob, eccbytes, false);
1820 if (ret)
1821 return ret;
1822
David Brownellee86b8d2009-11-07 16:27:01 -05001823 oob += eccbytes;
1824
1825 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001826 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1827 false);
1828 if (ret)
1829 return ret;
1830
David Brownellee86b8d2009-11-07 16:27:01 -05001831 oob += chip->ecc.postpad;
1832 }
1833 }
1834
1835 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001836 if (size) {
1837 ret = nand_read_data_op(chip, oob, size, false);
1838 if (ret)
1839 return ret;
1840 }
David Brownellee86b8d2009-11-07 16:27:01 -05001841
1842 return 0;
1843}
1844
1845/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001846 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1847 * @mtd: mtd info structure
1848 * @chip: nand chip info structure
1849 * @buf: buffer to store read data
1850 * @oob_required: caller requires OOB data read to chip->oob_poi
1851 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001852 */
1853static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001854 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001855{
1856 int i, eccsize = chip->ecc.size;
1857 int eccbytes = chip->ecc.bytes;
1858 int eccsteps = chip->ecc.steps;
1859 uint8_t *p = buf;
1860 uint8_t *ecc_calc = chip->buffers->ecccalc;
1861 uint8_t *ecc_code = chip->buffers->ecccode;
1862 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001863 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001864
Sergey Lapin3a38a552013-01-14 03:46:50 +00001865 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001866
William Juul52c07962007-10-31 13:53:06 +01001867 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1868 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001869
William Juul52c07962007-10-31 13:53:06 +01001870 for (i = 0; i < chip->ecc.total; i++)
1871 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001872
William Juul52c07962007-10-31 13:53:06 +01001873 eccsteps = chip->ecc.steps;
1874 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001875
William Juul52c07962007-10-31 13:53:06 +01001876 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1877 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001878
William Juul52c07962007-10-31 13:53:06 +01001879 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001880 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001881 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001882 } else {
William Juul52c07962007-10-31 13:53:06 +01001883 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001884 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1885 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001886 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001887 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001888}
1889
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001890/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001891 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001892 * @mtd: mtd info structure
1893 * @chip: nand chip info structure
1894 * @data_offs: offset of requested data within the page
1895 * @readlen: data length
1896 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001897 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001898 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001899static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001900 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1901 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001902{
1903 int start_step, end_step, num_steps;
1904 uint32_t *eccpos = chip->ecc.layout->eccpos;
1905 uint8_t *p;
1906 int data_col_addr, i, gaps = 0;
1907 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1908 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001909 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001910 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001911 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001912
Sergey Lapin3a38a552013-01-14 03:46:50 +00001913 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001914 start_step = data_offs / chip->ecc.size;
1915 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1916 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001917 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001918
Sergey Lapin3a38a552013-01-14 03:46:50 +00001919 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001920 datafrag_len = num_steps * chip->ecc.size;
1921 eccfrag_len = num_steps * chip->ecc.bytes;
1922
1923 data_col_addr = start_step * chip->ecc.size;
1924 /* If we read not a page aligned data */
1925 if (data_col_addr != 0)
1926 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1927
1928 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001929 ret = nand_read_data_op(chip, p, datafrag_len, false);
1930 if (ret)
1931 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001932
Sergey Lapin3a38a552013-01-14 03:46:50 +00001933 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001934 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1935 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1936
Sergey Lapin3a38a552013-01-14 03:46:50 +00001937 /*
1938 * The performance is faster if we position offsets according to
1939 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1940 */
Scott Wood3628f002008-10-24 16:20:43 -05001941 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001942 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001943 gaps = 1;
1944 break;
1945 }
1946 }
1947 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001948 ret = nand_change_read_column_op(chip, mtd->writesize,
1949 chip->oob_poi, mtd->oobsize,
1950 false);
1951 if (ret)
1952 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001953 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001954 /*
1955 * Send the command to read the particular ECC bytes take care
1956 * about buswidth alignment in read_buf.
1957 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001958 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001959 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001960 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001961 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001962 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001963 aligned_len++;
1964
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001965 ret = nand_change_read_column_op(chip,
1966 mtd->writesize + aligned_pos,
1967 &chip->oob_poi[aligned_pos],
1968 aligned_len, false);
1969 if (ret)
1970 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001971 }
1972
1973 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001974 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001975
1976 p = bufpoi + data_col_addr;
1977 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1978 int stat;
1979
Christian Hitzb8a6b372011-10-12 09:32:02 +02001980 stat = chip->ecc.correct(mtd, p,
1981 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001982 if (stat == -EBADMSG &&
1983 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1984 /* check for empty pages with bitflips */
1985 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1986 &chip->buffers->ecccode[i],
1987 chip->ecc.bytes,
1988 NULL, 0,
1989 chip->ecc.strength);
1990 }
1991
Heiko Schocherf5895d12014-06-24 10:10:04 +02001992 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001993 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001994 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001995 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001996 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1997 }
Scott Wood3628f002008-10-24 16:20:43 -05001998 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001999 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05002000}
2001
2002/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002003 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
2004 * @mtd: mtd info structure
2005 * @chip: nand chip info structure
2006 * @buf: buffer to store read data
2007 * @oob_required: caller requires OOB data read to chip->oob_poi
2008 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002009 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002010 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002011 */
William Juul52c07962007-10-31 13:53:06 +01002012static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002013 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002014{
William Juul52c07962007-10-31 13:53:06 +01002015 int i, eccsize = chip->ecc.size;
2016 int eccbytes = chip->ecc.bytes;
2017 int eccsteps = chip->ecc.steps;
2018 uint8_t *p = buf;
2019 uint8_t *ecc_calc = chip->buffers->ecccalc;
2020 uint8_t *ecc_code = chip->buffers->ecccode;
2021 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002022 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002023 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002024
William Juul52c07962007-10-31 13:53:06 +01002025 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2026 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002027
2028 ret = nand_read_data_op(chip, p, eccsize, false);
2029 if (ret)
2030 return ret;
2031
William Juul52c07962007-10-31 13:53:06 +01002032 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2033 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002034
2035 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2036 if (ret)
2037 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002038
William Juul52c07962007-10-31 13:53:06 +01002039 for (i = 0; i < chip->ecc.total; i++)
2040 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002041
William Juul52c07962007-10-31 13:53:06 +01002042 eccsteps = chip->ecc.steps;
2043 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002044
William Juul52c07962007-10-31 13:53:06 +01002045 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2046 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002047
William Juul52c07962007-10-31 13:53:06 +01002048 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002049 if (stat == -EBADMSG &&
2050 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2051 /* check for empty pages with bitflips */
2052 stat = nand_check_erased_ecc_chunk(p, eccsize,
2053 &ecc_code[i], eccbytes,
2054 NULL, 0,
2055 chip->ecc.strength);
2056 }
2057
Heiko Schocherf5895d12014-06-24 10:10:04 +02002058 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002059 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002060 } else {
William Juul52c07962007-10-31 13:53:06 +01002061 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002062 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2063 }
William Juul52c07962007-10-31 13:53:06 +01002064 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002065 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002066}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002067
William Juul52c07962007-10-31 13:53:06 +01002068/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002069 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2070 * @mtd: mtd info structure
2071 * @chip: nand chip info structure
2072 * @buf: buffer to store read data
2073 * @oob_required: caller requires OOB data read to chip->oob_poi
2074 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002075 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002076 * Hardware ECC for large page chips, require OOB to be read first. For this
2077 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2078 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2079 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2080 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002081 */
2082static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002083 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002084{
2085 int i, eccsize = chip->ecc.size;
2086 int eccbytes = chip->ecc.bytes;
2087 int eccsteps = chip->ecc.steps;
2088 uint8_t *p = buf;
2089 uint8_t *ecc_code = chip->buffers->ecccode;
2090 uint32_t *eccpos = chip->ecc.layout->eccpos;
2091 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002092 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002093 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002094
2095 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002096 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2097 if (ret)
2098 return ret;
2099
2100 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2101 if (ret)
2102 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002103
2104 for (i = 0; i < chip->ecc.total; i++)
2105 ecc_code[i] = chip->oob_poi[eccpos[i]];
2106
2107 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2108 int stat;
2109
2110 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002111
2112 ret = nand_read_data_op(chip, p, eccsize, false);
2113 if (ret)
2114 return ret;
2115
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002116 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2117
2118 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002119 if (stat == -EBADMSG &&
2120 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2121 /* check for empty pages with bitflips */
2122 stat = nand_check_erased_ecc_chunk(p, eccsize,
2123 &ecc_code[i], eccbytes,
2124 NULL, 0,
2125 chip->ecc.strength);
2126 }
2127
Heiko Schocherf5895d12014-06-24 10:10:04 +02002128 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002129 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002130 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002131 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002132 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2133 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002134 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002135 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002136}
2137
2138/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002139 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2140 * @mtd: mtd info structure
2141 * @chip: nand chip info structure
2142 * @buf: buffer to store read data
2143 * @oob_required: caller requires OOB data read to chip->oob_poi
2144 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002145 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002146 * The hw generator calculates the error syndrome automatically. Therefore we
2147 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002148 */
2149static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002150 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002151{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002152 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002153 int eccbytes = chip->ecc.bytes;
2154 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002155 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002156 uint8_t *p = buf;
2157 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002158 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002159
2160 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2161 int stat;
2162
2163 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002164
2165 ret = nand_read_data_op(chip, p, eccsize, false);
2166 if (ret)
2167 return ret;
William Juul52c07962007-10-31 13:53:06 +01002168
2169 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002170 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2171 false);
2172 if (ret)
2173 return ret;
2174
William Juul52c07962007-10-31 13:53:06 +01002175 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002176 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002177
William Juul52c07962007-10-31 13:53:06 +01002178 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002179
2180 ret = nand_read_data_op(chip, oob, eccbytes, false);
2181 if (ret)
2182 return ret;
2183
William Juul52c07962007-10-31 13:53:06 +01002184 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002185
William Juul52c07962007-10-31 13:53:06 +01002186 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002187
William Juul52c07962007-10-31 13:53:06 +01002188 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002189 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2190 false);
2191 if (ret)
2192 return ret;
2193
William Juul52c07962007-10-31 13:53:06 +01002194 oob += chip->ecc.postpad;
2195 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002196
2197 if (stat == -EBADMSG &&
2198 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2199 /* check for empty pages with bitflips */
2200 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2201 oob - eccpadbytes,
2202 eccpadbytes,
2203 NULL, 0,
2204 chip->ecc.strength);
2205 }
2206
2207 if (stat < 0) {
2208 mtd->ecc_stats.failed++;
2209 } else {
2210 mtd->ecc_stats.corrected += stat;
2211 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2212 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002213 }
William Juul52c07962007-10-31 13:53:06 +01002214
2215 /* Calculate remaining oob bytes */
2216 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002217 if (i) {
2218 ret = nand_read_data_op(chip, oob, i, false);
2219 if (ret)
2220 return ret;
2221 }
William Juul52c07962007-10-31 13:53:06 +01002222
Heiko Schocherf5895d12014-06-24 10:10:04 +02002223 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002224}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002225
2226/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002227 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2228 * @chip: nand chip structure
2229 * @oob: oob destination address
2230 * @ops: oob ops structure
2231 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002232 */
2233static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2234 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002235{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002236 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002237
Sergey Lapin3a38a552013-01-14 03:46:50 +00002238 case MTD_OPS_PLACE_OOB:
2239 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002240 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2241 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002242
Sergey Lapin3a38a552013-01-14 03:46:50 +00002243 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002244 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2245 uint32_t boffs = 0, roffs = ops->ooboffs;
2246 size_t bytes = 0;
2247
Christian Hitz13fc0e22011-10-12 09:32:01 +02002248 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002249 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002250 if (unlikely(roffs)) {
2251 if (roffs >= free->length) {
2252 roffs -= free->length;
2253 continue;
2254 }
2255 boffs = free->offset + roffs;
2256 bytes = min_t(size_t, len,
2257 (free->length - roffs));
2258 roffs = 0;
2259 } else {
2260 bytes = min_t(size_t, len, free->length);
2261 boffs = free->offset;
2262 }
2263 memcpy(oob, chip->oob_poi + boffs, bytes);
2264 oob += bytes;
2265 }
2266 return oob;
2267 }
2268 default:
2269 BUG();
2270 }
2271 return NULL;
2272}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002273
2274/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002275 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2276 * @mtd: MTD device structure
2277 * @retry_mode: the retry mode to use
2278 *
2279 * Some vendors supply a special command to shift the Vt threshold, to be used
2280 * when there are too many bitflips in a page (i.e., ECC error). After setting
2281 * a new threshold, the host should retry reading the page.
2282 */
2283static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2284{
Scott Wood17fed142016-05-30 13:57:56 -05002285 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002286
2287 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2288
2289 if (retry_mode >= chip->read_retries)
2290 return -EINVAL;
2291
2292 if (!chip->setup_read_retry)
2293 return -EOPNOTSUPP;
2294
2295 return chip->setup_read_retry(mtd, retry_mode);
2296}
2297
2298/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002299 * nand_do_read_ops - [INTERN] Read data with ECC
2300 * @mtd: MTD device structure
2301 * @from: offset to read from
2302 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002303 *
William Juul52c07962007-10-31 13:53:06 +01002304 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002305 */
William Juul52c07962007-10-31 13:53:06 +01002306static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2307 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002308{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002309 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002310 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002311 int ret = 0;
2312 uint32_t readlen = ops->len;
2313 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002314 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002315
William Juul52c07962007-10-31 13:53:06 +01002316 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002317 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002318 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002319 int retry_mode = 0;
2320 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002321
William Juul52c07962007-10-31 13:53:06 +01002322 chipnr = (int)(from >> chip->chip_shift);
2323 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002324
William Juul52c07962007-10-31 13:53:06 +01002325 realpage = (int)(from >> chip->page_shift);
2326 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002327
William Juul52c07962007-10-31 13:53:06 +01002328 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002329
William Juul52c07962007-10-31 13:53:06 +01002330 buf = ops->datbuf;
2331 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002332 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002333
Christian Hitz13fc0e22011-10-12 09:32:01 +02002334 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002335 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002336
Heiko Schocherf5895d12014-06-24 10:10:04 +02002337 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01002338 bytes = min(mtd->writesize - col, readlen);
2339 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002340
Scott Wood3ea94ed2015-06-26 19:03:26 -05002341 if (!aligned)
2342 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002343 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2344 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2345 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002346 else
2347 use_bufpoi = 0;
2348
Sergey Lapin3a38a552013-01-14 03:46:50 +00002349 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002350 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002351 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2352
2353 if (use_bufpoi && aligned)
2354 pr_debug("%s: using read bounce buffer for buf@%p\n",
2355 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002356
Heiko Schocherf5895d12014-06-24 10:10:04 +02002357read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002358 if (nand_standard_page_accessors(&chip->ecc)) {
2359 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2360 if (ret)
2361 break;
2362 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002363
Paul Burton700a76c2013-09-04 15:16:56 +01002364 /*
2365 * Now read the page into the buffer. Absent an error,
2366 * the read methods return max bitflips per ecc step.
2367 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002368 if (unlikely(ops->mode == MTD_OPS_RAW))
2369 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2370 oob_required,
2371 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002372 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002373 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002374 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002375 col, bytes, bufpoi,
2376 page);
William Juul52c07962007-10-31 13:53:06 +01002377 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002378 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002379 oob_required, page);
2380 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002381 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002382 /* Invalidate page cache */
2383 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002384 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002385 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002386
Paul Burton700a76c2013-09-04 15:16:56 +01002387 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2388
William Juul52c07962007-10-31 13:53:06 +01002389 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002390 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002391 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002392 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002393 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002394 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002395 chip->pagebuf_bitflips = ret;
2396 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002397 /* Invalidate page cache */
2398 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002399 }
William Juul52c07962007-10-31 13:53:06 +01002400 memcpy(buf, chip->buffers->databuf + col, bytes);
2401 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002402
William Juul52c07962007-10-31 13:53:06 +01002403 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002404 int toread = min(oobreadlen, max_oobsize);
2405
2406 if (toread) {
2407 oob = nand_transfer_oob(chip,
2408 oob, ops, toread);
2409 oobreadlen -= toread;
2410 }
William Juul52c07962007-10-31 13:53:06 +01002411 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002412
2413 if (chip->options & NAND_NEED_READRDY) {
2414 /* Apply delay or wait for ready/busy pin */
2415 if (!chip->dev_ready)
2416 udelay(chip->chip_delay);
2417 else
2418 nand_wait_ready(mtd);
2419 }
2420
2421 if (mtd->ecc_stats.failed - ecc_failures) {
2422 if (retry_mode + 1 < chip->read_retries) {
2423 retry_mode++;
2424 ret = nand_setup_read_retry(mtd,
2425 retry_mode);
2426 if (ret < 0)
2427 break;
2428
2429 /* Reset failures; retry */
2430 mtd->ecc_stats.failed = ecc_failures;
2431 goto read_retry;
2432 } else {
2433 /* No more retry modes; real failure */
2434 ecc_fail = true;
2435 }
2436 }
2437
2438 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002439 } else {
2440 memcpy(buf, chip->buffers->databuf + col, bytes);
2441 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002442 max_bitflips = max_t(unsigned int, max_bitflips,
2443 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002444 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002445
William Juul52c07962007-10-31 13:53:06 +01002446 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002447
Heiko Schocherf5895d12014-06-24 10:10:04 +02002448 /* Reset to retry mode 0 */
2449 if (retry_mode) {
2450 ret = nand_setup_read_retry(mtd, 0);
2451 if (ret < 0)
2452 break;
2453 retry_mode = 0;
2454 }
2455
William Juul52c07962007-10-31 13:53:06 +01002456 if (!readlen)
2457 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002458
Sergey Lapin3a38a552013-01-14 03:46:50 +00002459 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002460 col = 0;
2461 /* Increment page address */
2462 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002463
William Juul52c07962007-10-31 13:53:06 +01002464 page = realpage & chip->pagemask;
2465 /* Check, if we cross a chip boundary */
2466 if (!page) {
2467 chipnr++;
2468 chip->select_chip(mtd, -1);
2469 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002470 }
William Juul52c07962007-10-31 13:53:06 +01002471 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002472 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002473
William Juul52c07962007-10-31 13:53:06 +01002474 ops->retlen = ops->len - (size_t) readlen;
2475 if (oob)
2476 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002477
Heiko Schocherf5895d12014-06-24 10:10:04 +02002478 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002479 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002480
Heiko Schocherf5895d12014-06-24 10:10:04 +02002481 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002482 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002483
Paul Burton700a76c2013-09-04 15:16:56 +01002484 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002485}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002486
William Juul52c07962007-10-31 13:53:06 +01002487/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002488 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2489 * @mtd: mtd info structure
2490 * @chip: nand chip info structure
2491 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002492 */
2493static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002494 int page)
William Juul52c07962007-10-31 13:53:06 +01002495{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002496 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002497}
2498
2499/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002500 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002501 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002502 * @mtd: mtd info structure
2503 * @chip: nand chip info structure
2504 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002505 */
2506static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002507 int page)
William Juul52c07962007-10-31 13:53:06 +01002508{
William Juul52c07962007-10-31 13:53:06 +01002509 int length = mtd->oobsize;
2510 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2511 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002512 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002513 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002514
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002515 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2516 if (ret)
2517 return ret;
2518
William Juul52c07962007-10-31 13:53:06 +01002519 for (i = 0; i < chip->ecc.steps; i++) {
2520 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002521 int ret;
2522
William Juul52c07962007-10-31 13:53:06 +01002523 pos = eccsize + i * (eccsize + chunk);
2524 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002525 ret = nand_change_read_column_op(chip, pos,
2526 NULL, 0,
2527 false);
William Juul52c07962007-10-31 13:53:06 +01002528 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002529 ret = nand_read_page_op(chip, page, pos, NULL,
2530 0);
2531
2532 if (ret)
2533 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002534 } else
William Juul52c07962007-10-31 13:53:06 +01002535 sndrnd = 1;
2536 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002537
2538 ret = nand_read_data_op(chip, bufpoi, toread, false);
2539 if (ret)
2540 return ret;
2541
William Juul52c07962007-10-31 13:53:06 +01002542 bufpoi += toread;
2543 length -= toread;
2544 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002545 if (length > 0) {
2546 ret = nand_read_data_op(chip, bufpoi, length, false);
2547 if (ret)
2548 return ret;
2549 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002550
Sergey Lapin3a38a552013-01-14 03:46:50 +00002551 return 0;
William Juul52c07962007-10-31 13:53:06 +01002552}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002553
William Juul52c07962007-10-31 13:53:06 +01002554/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002555 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2556 * @mtd: mtd info structure
2557 * @chip: nand chip info structure
2558 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002559 */
2560static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2561 int page)
2562{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002563 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2564 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002565}
2566
2567/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002568 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2569 * with syndrome - only for large page flash
2570 * @mtd: mtd info structure
2571 * @chip: nand chip info structure
2572 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002573 */
2574static int nand_write_oob_syndrome(struct mtd_info *mtd,
2575 struct nand_chip *chip, int page)
2576{
2577 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2578 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002579 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002580 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002581
2582 /*
William Juul52c07962007-10-31 13:53:06 +01002583 * data-ecc-data-ecc ... ecc-oob
2584 * or
2585 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002586 */
William Juul52c07962007-10-31 13:53:06 +01002587 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2588 pos = steps * (eccsize + chunk);
2589 steps = 0;
2590 } else
2591 pos = eccsize;
2592
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002593 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2594 if (ret)
2595 return ret;
2596
William Juul52c07962007-10-31 13:53:06 +01002597 for (i = 0; i < steps; i++) {
2598 if (sndcmd) {
2599 if (mtd->writesize <= 512) {
2600 uint32_t fill = 0xFFFFFFFF;
2601
2602 len = eccsize;
2603 while (len > 0) {
2604 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002605
2606 ret = nand_write_data_op(chip, &fill,
2607 num, false);
2608 if (ret)
2609 return ret;
2610
William Juul52c07962007-10-31 13:53:06 +01002611 len -= num;
2612 }
2613 } else {
2614 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002615 ret = nand_change_write_column_op(chip, pos,
2616 NULL, 0,
2617 false);
2618 if (ret)
2619 return ret;
William Juul52c07962007-10-31 13:53:06 +01002620 }
2621 } else
2622 sndcmd = 1;
2623 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002624
2625 ret = nand_write_data_op(chip, bufpoi, len, false);
2626 if (ret)
2627 return ret;
2628
William Juul52c07962007-10-31 13:53:06 +01002629 bufpoi += len;
2630 length -= len;
2631 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002632 if (length > 0) {
2633 ret = nand_write_data_op(chip, bufpoi, length, false);
2634 if (ret)
2635 return ret;
2636 }
William Juul52c07962007-10-31 13:53:06 +01002637
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002638 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002639}
2640
2641/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002642 * nand_do_read_oob - [INTERN] NAND read out-of-band
2643 * @mtd: MTD device structure
2644 * @from: offset to read from
2645 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002646 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002647 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002648 */
William Juul52c07962007-10-31 13:53:06 +01002649static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2650 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002651{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002652 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002653 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002654 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002655 int readlen = ops->ooblen;
2656 int len;
2657 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002658 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002659
Heiko Schocherf5895d12014-06-24 10:10:04 +02002660 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002661 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002662
Sergey Lapin3a38a552013-01-14 03:46:50 +00002663 stats = mtd->ecc_stats;
2664
Scott Wood52ab7ce2016-05-30 13:57:58 -05002665 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002666
William Juul52c07962007-10-31 13:53:06 +01002667 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002668 pr_debug("%s: attempt to start read outside oob\n",
2669 __func__);
William Juul52c07962007-10-31 13:53:06 +01002670 return -EINVAL;
2671 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002672
2673 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002674 if (unlikely(from >= mtd->size ||
2675 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2676 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002677 pr_debug("%s: attempt to read beyond end of device\n",
2678 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002679 return -EINVAL;
2680 }
2681
William Juul52c07962007-10-31 13:53:06 +01002682 chipnr = (int)(from >> chip->chip_shift);
2683 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002684
William Juul52c07962007-10-31 13:53:06 +01002685 /* Shift to get page */
2686 realpage = (int)(from >> chip->page_shift);
2687 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002688
Christian Hitz13fc0e22011-10-12 09:32:01 +02002689 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002690 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002691
Sergey Lapin3a38a552013-01-14 03:46:50 +00002692 if (ops->mode == MTD_OPS_RAW)
2693 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2694 else
2695 ret = chip->ecc.read_oob(mtd, chip, page);
2696
2697 if (ret < 0)
2698 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002699
William Juul52c07962007-10-31 13:53:06 +01002700 len = min(len, readlen);
2701 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002702
Heiko Schocherf5895d12014-06-24 10:10:04 +02002703 if (chip->options & NAND_NEED_READRDY) {
2704 /* Apply delay or wait for ready/busy pin */
2705 if (!chip->dev_ready)
2706 udelay(chip->chip_delay);
2707 else
2708 nand_wait_ready(mtd);
2709 }
2710
William Juul52c07962007-10-31 13:53:06 +01002711 readlen -= len;
2712 if (!readlen)
2713 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002714
William Juul52c07962007-10-31 13:53:06 +01002715 /* Increment page address */
2716 realpage++;
2717
2718 page = realpage & chip->pagemask;
2719 /* Check, if we cross a chip boundary */
2720 if (!page) {
2721 chipnr++;
2722 chip->select_chip(mtd, -1);
2723 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002724 }
William Juul52c07962007-10-31 13:53:06 +01002725 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002726 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002727
Sergey Lapin3a38a552013-01-14 03:46:50 +00002728 ops->oobretlen = ops->ooblen - readlen;
2729
2730 if (ret < 0)
2731 return ret;
2732
2733 if (mtd->ecc_stats.failed - stats.failed)
2734 return -EBADMSG;
2735
2736 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002737}
2738
2739/**
William Juul52c07962007-10-31 13:53:06 +01002740 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002741 * @mtd: MTD device structure
2742 * @from: offset to read from
2743 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002744 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002745 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002746 */
William Juul52c07962007-10-31 13:53:06 +01002747static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2748 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002749{
William Juul52c07962007-10-31 13:53:06 +01002750 int ret = -ENOTSUPP;
2751
2752 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002753
2754 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002755 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002756 pr_debug("%s: attempt to read beyond end of device\n",
2757 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002758 return -EINVAL;
2759 }
2760
Heiko Schocherf5895d12014-06-24 10:10:04 +02002761 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002762
Christian Hitz13fc0e22011-10-12 09:32:01 +02002763 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002764 case MTD_OPS_PLACE_OOB:
2765 case MTD_OPS_AUTO_OOB:
2766 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002767 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002768
William Juul52c07962007-10-31 13:53:06 +01002769 default:
2770 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002771 }
2772
William Juul52c07962007-10-31 13:53:06 +01002773 if (!ops->datbuf)
2774 ret = nand_do_read_oob(mtd, from, ops);
2775 else
2776 ret = nand_do_read_ops(mtd, from, ops);
2777
Christian Hitz13fc0e22011-10-12 09:32:01 +02002778out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002780 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002781}
2782
2783
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002784/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002785 * nand_write_page_raw - [INTERN] raw page write function
2786 * @mtd: mtd info structure
2787 * @chip: nand chip info structure
2788 * @buf: data buffer
2789 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002790 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002791 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002792 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002793 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002794static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002795 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002796{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002797 int ret;
2798
2799 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2800 if (ret)
2801 return ret;
2802
2803 if (oob_required) {
2804 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2805 false);
2806 if (ret)
2807 return ret;
2808 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002809
2810 return 0;
William Juul52c07962007-10-31 13:53:06 +01002811}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002812
William Juul52c07962007-10-31 13:53:06 +01002813/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002814 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2815 * @mtd: mtd info structure
2816 * @chip: nand chip info structure
2817 * @buf: data buffer
2818 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002819 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002820 *
2821 * We need a special oob layout and handling even when ECC isn't checked.
2822 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002823static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002824 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002825 const uint8_t *buf, int oob_required,
2826 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002827{
2828 int eccsize = chip->ecc.size;
2829 int eccbytes = chip->ecc.bytes;
2830 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002831 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002832
2833 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002834 ret = nand_write_data_op(chip, buf, eccsize, false);
2835 if (ret)
2836 return ret;
2837
David Brownellee86b8d2009-11-07 16:27:01 -05002838 buf += eccsize;
2839
2840 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002841 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2842 false);
2843 if (ret)
2844 return ret;
2845
David Brownellee86b8d2009-11-07 16:27:01 -05002846 oob += chip->ecc.prepad;
2847 }
2848
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002849 ret = nand_write_data_op(chip, oob, eccbytes, false);
2850 if (ret)
2851 return ret;
2852
David Brownellee86b8d2009-11-07 16:27:01 -05002853 oob += eccbytes;
2854
2855 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002856 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2857 false);
2858 if (ret)
2859 return ret;
2860
David Brownellee86b8d2009-11-07 16:27:01 -05002861 oob += chip->ecc.postpad;
2862 }
2863 }
2864
2865 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002866 if (size) {
2867 ret = nand_write_data_op(chip, oob, size, false);
2868 if (ret)
2869 return ret;
2870 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002871
2872 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002873}
2874/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002875 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2876 * @mtd: mtd info structure
2877 * @chip: nand chip info structure
2878 * @buf: data buffer
2879 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002880 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002881 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002882static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002883 const uint8_t *buf, int oob_required,
2884 int page)
William Juul52c07962007-10-31 13:53:06 +01002885{
2886 int i, eccsize = chip->ecc.size;
2887 int eccbytes = chip->ecc.bytes;
2888 int eccsteps = chip->ecc.steps;
2889 uint8_t *ecc_calc = chip->buffers->ecccalc;
2890 const uint8_t *p = buf;
2891 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002892
Sergey Lapin3a38a552013-01-14 03:46:50 +00002893 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002894 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2895 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002896
William Juul52c07962007-10-31 13:53:06 +01002897 for (i = 0; i < chip->ecc.total; i++)
2898 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002899
Scott Wood46e13102016-05-30 13:57:57 -05002900 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002901}
2902
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002903/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002904 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2905 * @mtd: mtd info structure
2906 * @chip: nand chip info structure
2907 * @buf: data buffer
2908 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002909 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002910 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002911static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002912 const uint8_t *buf, int oob_required,
2913 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002914{
William Juul52c07962007-10-31 13:53:06 +01002915 int i, eccsize = chip->ecc.size;
2916 int eccbytes = chip->ecc.bytes;
2917 int eccsteps = chip->ecc.steps;
2918 uint8_t *ecc_calc = chip->buffers->ecccalc;
2919 const uint8_t *p = buf;
2920 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002921 int ret;
William Juul52c07962007-10-31 13:53:06 +01002922
2923 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2924 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002925
2926 ret = nand_write_data_op(chip, p, eccsize, false);
2927 if (ret)
2928 return ret;
2929
William Juul52c07962007-10-31 13:53:06 +01002930 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2931 }
2932
2933 for (i = 0; i < chip->ecc.total; i++)
2934 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2935
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002936 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2937 if (ret)
2938 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002939
2940 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002941}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002942
Heiko Schocherf5895d12014-06-24 10:10:04 +02002943
2944/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002945 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002946 * @mtd: mtd info structure
2947 * @chip: nand chip info structure
2948 * @offset: column address of subpage within the page
2949 * @data_len: data length
2950 * @buf: data buffer
2951 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002952 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002953 */
2954static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2955 struct nand_chip *chip, uint32_t offset,
2956 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002957 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002958{
2959 uint8_t *oob_buf = chip->oob_poi;
2960 uint8_t *ecc_calc = chip->buffers->ecccalc;
2961 int ecc_size = chip->ecc.size;
2962 int ecc_bytes = chip->ecc.bytes;
2963 int ecc_steps = chip->ecc.steps;
2964 uint32_t *eccpos = chip->ecc.layout->eccpos;
2965 uint32_t start_step = offset / ecc_size;
2966 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2967 int oob_bytes = mtd->oobsize / ecc_steps;
2968 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002969 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002970
2971 for (step = 0; step < ecc_steps; step++) {
2972 /* configure controller for WRITE access */
2973 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2974
2975 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002976 ret = nand_write_data_op(chip, buf, ecc_size, false);
2977 if (ret)
2978 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002979
2980 /* mask ECC of un-touched subpages by padding 0xFF */
2981 if ((step < start_step) || (step > end_step))
2982 memset(ecc_calc, 0xff, ecc_bytes);
2983 else
2984 chip->ecc.calculate(mtd, buf, ecc_calc);
2985
2986 /* mask OOB of un-touched subpages by padding 0xFF */
2987 /* if oob_required, preserve OOB metadata of written subpage */
2988 if (!oob_required || (step < start_step) || (step > end_step))
2989 memset(oob_buf, 0xff, oob_bytes);
2990
2991 buf += ecc_size;
2992 ecc_calc += ecc_bytes;
2993 oob_buf += oob_bytes;
2994 }
2995
2996 /* copy calculated ECC for whole page to chip->buffer->oob */
2997 /* this include masked-value(0xFF) for unwritten subpages */
2998 ecc_calc = chip->buffers->ecccalc;
2999 for (i = 0; i < chip->ecc.total; i++)
3000 chip->oob_poi[eccpos[i]] = ecc_calc[i];
3001
3002 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003003 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3004 if (ret)
3005 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003006
3007 return 0;
3008}
3009
3010
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003011/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003012 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
3013 * @mtd: mtd info structure
3014 * @chip: nand chip info structure
3015 * @buf: data buffer
3016 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003017 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003018 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003019 * The hw generator calculates the error syndrome automatically. Therefore we
3020 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003021 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003022static int nand_write_page_syndrome(struct mtd_info *mtd,
3023 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003024 const uint8_t *buf, int oob_required,
3025 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003026{
William Juul52c07962007-10-31 13:53:06 +01003027 int i, eccsize = chip->ecc.size;
3028 int eccbytes = chip->ecc.bytes;
3029 int eccsteps = chip->ecc.steps;
3030 const uint8_t *p = buf;
3031 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003032 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003033
William Juul52c07962007-10-31 13:53:06 +01003034 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003035 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003036
3037 ret = nand_write_data_op(chip, p, eccsize, false);
3038 if (ret)
3039 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003040
William Juul52c07962007-10-31 13:53:06 +01003041 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003042 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3043 false);
3044 if (ret)
3045 return ret;
3046
William Juul52c07962007-10-31 13:53:06 +01003047 oob += chip->ecc.prepad;
3048 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003049
William Juul52c07962007-10-31 13:53:06 +01003050 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003051
3052 ret = nand_write_data_op(chip, oob, eccbytes, false);
3053 if (ret)
3054 return ret;
3055
William Juul52c07962007-10-31 13:53:06 +01003056 oob += eccbytes;
3057
3058 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003059 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3060 false);
3061 if (ret)
3062 return ret;
3063
William Juul52c07962007-10-31 13:53:06 +01003064 oob += chip->ecc.postpad;
3065 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003066 }
3067
William Juul52c07962007-10-31 13:53:06 +01003068 /* Calculate remaining oob bytes */
3069 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003070 if (i) {
3071 ret = nand_write_data_op(chip, oob, i, false);
3072 if (ret)
3073 return ret;
3074 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003075
3076 return 0;
William Juul52c07962007-10-31 13:53:06 +01003077}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003078
William Juul52c07962007-10-31 13:53:06 +01003079/**
3080 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003081 * @mtd: MTD device structure
3082 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003083 * @offset: address offset within the page
3084 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003085 * @buf: the data to write
3086 * @oob_required: must write chip->oob_poi to OOB
3087 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003088 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003089 */
3090static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003091 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003092 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003093{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003094 int status, subpage;
3095
3096 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3097 chip->ecc.write_subpage)
3098 subpage = offset || (data_len < mtd->writesize);
3099 else
3100 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003101
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003102 if (nand_standard_page_accessors(&chip->ecc)) {
3103 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3104 if (status)
3105 return status;
3106 }
William Juul52c07962007-10-31 13:53:06 +01003107
3108 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003109 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003110 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003111 else if (subpage)
3112 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003113 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003114 else
Scott Wood46e13102016-05-30 13:57:57 -05003115 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3116 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003117
3118 if (status < 0)
3119 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003120
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003121 if (nand_standard_page_accessors(&chip->ecc))
3122 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003123
William Juul52c07962007-10-31 13:53:06 +01003124 return 0;
3125}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003126
William Juul52c07962007-10-31 13:53:06 +01003127/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003128 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3129 * @mtd: MTD device structure
3130 * @oob: oob data buffer
3131 * @len: oob data write length
3132 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003133 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003134static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3135 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003136{
Scott Wood17fed142016-05-30 13:57:56 -05003137 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003138
3139 /*
3140 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3141 * data from a previous OOB read.
3142 */
3143 memset(chip->oob_poi, 0xff, mtd->oobsize);
3144
Christian Hitz13fc0e22011-10-12 09:32:01 +02003145 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003146
Sergey Lapin3a38a552013-01-14 03:46:50 +00003147 case MTD_OPS_PLACE_OOB:
3148 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003149 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3150 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003151
Sergey Lapin3a38a552013-01-14 03:46:50 +00003152 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003153 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3154 uint32_t boffs = 0, woffs = ops->ooboffs;
3155 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003156
Christian Hitz13fc0e22011-10-12 09:32:01 +02003157 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003158 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003159 if (unlikely(woffs)) {
3160 if (woffs >= free->length) {
3161 woffs -= free->length;
3162 continue;
3163 }
3164 boffs = free->offset + woffs;
3165 bytes = min_t(size_t, len,
3166 (free->length - woffs));
3167 woffs = 0;
3168 } else {
3169 bytes = min_t(size_t, len, free->length);
3170 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003171 }
William Juul52c07962007-10-31 13:53:06 +01003172 memcpy(chip->oob_poi + boffs, oob, bytes);
3173 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003174 }
William Juul52c07962007-10-31 13:53:06 +01003175 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003176 }
William Juul52c07962007-10-31 13:53:06 +01003177 default:
3178 BUG();
3179 }
3180 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003181}
3182
Christian Hitzb8a6b372011-10-12 09:32:02 +02003183#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003184
3185/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003186 * nand_do_write_ops - [INTERN] NAND write with ECC
3187 * @mtd: MTD device structure
3188 * @to: offset to write to
3189 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003190 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003191 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003192 */
William Juul52c07962007-10-31 13:53:06 +01003193static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3194 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003195{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003196 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003197 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003198 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003199
3200 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003201 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003202
William Juul52c07962007-10-31 13:53:06 +01003203 uint8_t *oob = ops->oobbuf;
3204 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003205 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003206 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003207
William Juul52c07962007-10-31 13:53:06 +01003208 ops->retlen = 0;
3209 if (!writelen)
3210 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003211
Heiko Schocherf5895d12014-06-24 10:10:04 +02003212 /* Reject writes, which are not page aligned */
3213 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003214 pr_notice("%s: attempt to write non page aligned data\n",
3215 __func__);
William Juul52c07962007-10-31 13:53:06 +01003216 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003217 }
3218
3219 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003220
William Juul52c07962007-10-31 13:53:06 +01003221 chipnr = (int)(to >> chip->chip_shift);
3222 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003223
3224 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003225 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003226 ret = -EIO;
3227 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003228 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003229
William Juul52c07962007-10-31 13:53:06 +01003230 realpage = (int)(to >> chip->page_shift);
3231 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003232
William Juul52c07962007-10-31 13:53:06 +01003233 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003234 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3235 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003236 chip->pagebuf = -1;
3237
Christian Hitzb8a6b372011-10-12 09:32:02 +02003238 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003239 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3240 ret = -EINVAL;
3241 goto err_out;
3242 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003243
Christian Hitz13fc0e22011-10-12 09:32:01 +02003244 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003245 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003246 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003247 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003248 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003249
3250 if (part_pagewr)
3251 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003252 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3253 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3254 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003255 else
3256 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003257
Heiko Schocherf5895d12014-06-24 10:10:04 +02003258 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003259 /* Partial page write?, or need to use bounce buffer */
3260 if (use_bufpoi) {
3261 pr_debug("%s: using write bounce buffer for buf@%p\n",
3262 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003263 if (part_pagewr)
3264 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003265 chip->pagebuf = -1;
3266 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3267 memcpy(&chip->buffers->databuf[column], buf, bytes);
3268 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003269 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003270
Christian Hitzb8a6b372011-10-12 09:32:02 +02003271 if (unlikely(oob)) {
3272 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003273 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003274 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003275 } else {
3276 /* We still need to erase leftover OOB data */
3277 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003278 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003279 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003280 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003281 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003282 if (ret)
3283 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003284
William Juul52c07962007-10-31 13:53:06 +01003285 writelen -= bytes;
3286 if (!writelen)
3287 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003288
Heiko Schocherf5895d12014-06-24 10:10:04 +02003289 column = 0;
3290 buf += bytes;
3291 realpage++;
3292
3293 page = realpage & chip->pagemask;
3294 /* Check, if we cross a chip boundary */
3295 if (!page) {
3296 chipnr++;
3297 chip->select_chip(mtd, -1);
3298 chip->select_chip(mtd, chipnr);
3299 }
3300 }
3301
3302 ops->retlen = ops->len - writelen;
3303 if (unlikely(oob))
3304 ops->oobretlen = ops->ooblen;
3305
3306err_out:
3307 chip->select_chip(mtd, -1);
3308 return ret;
3309}
3310
3311/**
3312 * panic_nand_write - [MTD Interface] NAND write with ECC
3313 * @mtd: MTD device structure
3314 * @to: offset to write to
3315 * @len: number of bytes to write
3316 * @retlen: pointer to variable to store the number of written bytes
3317 * @buf: the data to write
3318 *
3319 * NAND write with ECC. Used when performing writes in interrupt context, this
3320 * may for example be called by mtdoops when writing an oops while in panic.
3321 */
3322static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3323 size_t *retlen, const uint8_t *buf)
3324{
Scott Wood17fed142016-05-30 13:57:56 -05003325 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003326 struct mtd_oob_ops ops;
3327 int ret;
3328
3329 /* Wait for the device to get ready */
3330 panic_nand_wait(mtd, chip, 400);
3331
3332 /* Grab the device */
3333 panic_nand_get_device(chip, mtd, FL_WRITING);
3334
Scott Wood3ea94ed2015-06-26 19:03:26 -05003335 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003336 ops.len = len;
3337 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003338 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003339
Heiko Schocherf5895d12014-06-24 10:10:04 +02003340 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003341
Sergey Lapin3a38a552013-01-14 03:46:50 +00003342 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003343 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003344}
3345
3346/**
William Juul52c07962007-10-31 13:53:06 +01003347 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003348 * @mtd: MTD device structure
3349 * @to: offset to write to
3350 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003351 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003352 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003353 */
William Juul52c07962007-10-31 13:53:06 +01003354static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3355 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003356{
William Juul52c07962007-10-31 13:53:06 +01003357 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003358 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003359
Heiko Schocherf5895d12014-06-24 10:10:04 +02003360 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003361 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003362
Scott Wood52ab7ce2016-05-30 13:57:58 -05003363 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003364
3365 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003366 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003367 pr_debug("%s: attempt to write past end of page\n",
3368 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003369 return -EINVAL;
3370 }
3371
William Juul52c07962007-10-31 13:53:06 +01003372 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003373 pr_debug("%s: attempt to start write outside oob\n",
3374 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003375 return -EINVAL;
3376 }
3377
Christian Hitz13fc0e22011-10-12 09:32:01 +02003378 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003379 if (unlikely(to >= mtd->size ||
3380 ops->ooboffs + ops->ooblen >
3381 ((mtd->size >> chip->page_shift) -
3382 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003383 pr_debug("%s: attempt to write beyond end of device\n",
3384 __func__);
William Juul52c07962007-10-31 13:53:06 +01003385 return -EINVAL;
3386 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003387
William Juul52c07962007-10-31 13:53:06 +01003388 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003389
3390 /*
3391 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3392 * of my DiskOnChip 2000 test units) will clear the whole data page too
3393 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3394 * it in the doc2000 driver in August 1999. dwmw2.
3395 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003396 nand_reset(chip, chipnr);
3397
3398 chip->select_chip(mtd, chipnr);
3399
3400 /* Shift to get page */
3401 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003402
3403 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003404 if (nand_check_wp(mtd)) {
3405 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003406 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003407 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003408
William Juul52c07962007-10-31 13:53:06 +01003409 /* Invalidate the page cache, if we write to the cached page */
3410 if (page == chip->pagebuf)
3411 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003412
Sergey Lapin3a38a552013-01-14 03:46:50 +00003413 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3414
3415 if (ops->mode == MTD_OPS_RAW)
3416 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3417 else
3418 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003419
Heiko Schocherf5895d12014-06-24 10:10:04 +02003420 chip->select_chip(mtd, -1);
3421
William Juul52c07962007-10-31 13:53:06 +01003422 if (status)
3423 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003424
William Juul52c07962007-10-31 13:53:06 +01003425 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003426
William Juul52c07962007-10-31 13:53:06 +01003427 return 0;
3428}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003429
William Juul52c07962007-10-31 13:53:06 +01003430/**
3431 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003432 * @mtd: MTD device structure
3433 * @to: offset to write to
3434 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003435 */
3436static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3437 struct mtd_oob_ops *ops)
3438{
William Juul52c07962007-10-31 13:53:06 +01003439 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003440
William Juul52c07962007-10-31 13:53:06 +01003441 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003442
William Juul52c07962007-10-31 13:53:06 +01003443 /* Do not allow writes past end of device */
3444 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003445 pr_debug("%s: attempt to write beyond end of device\n",
3446 __func__);
William Juul52c07962007-10-31 13:53:06 +01003447 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003448 }
William Juul52c07962007-10-31 13:53:06 +01003449
Heiko Schocherf5895d12014-06-24 10:10:04 +02003450 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003451
Christian Hitz13fc0e22011-10-12 09:32:01 +02003452 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003453 case MTD_OPS_PLACE_OOB:
3454 case MTD_OPS_AUTO_OOB:
3455 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003456 break;
3457
3458 default:
3459 goto out;
3460 }
3461
3462 if (!ops->datbuf)
3463 ret = nand_do_write_oob(mtd, to, ops);
3464 else
3465 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003466
Christian Hitz13fc0e22011-10-12 09:32:01 +02003467out:
William Juul52c07962007-10-31 13:53:06 +01003468 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003469 return ret;
3470}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003471
3472/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003473 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003474 * @mtd: MTD device structure
3475 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003476 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003477 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003478 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003479static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003480{
Scott Wood17fed142016-05-30 13:57:56 -05003481 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003482 unsigned int eraseblock;
3483
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003484 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003485 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003486
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003487 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003488}
3489
3490/**
3491 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003492 * @mtd: MTD device structure
3493 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003494 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003495 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003496 */
William Juul52c07962007-10-31 13:53:06 +01003497static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003498{
William Juul52c07962007-10-31 13:53:06 +01003499 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003500}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003501
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003502/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003503 * nand_erase_nand - [INTERN] erase block(s)
3504 * @mtd: MTD device structure
3505 * @instr: erase instruction
3506 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003507 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003508 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003509 */
William Juul52c07962007-10-31 13:53:06 +01003510int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3511 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003512{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003513 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003514 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003515 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003516
Heiko Schocherf5895d12014-06-24 10:10:04 +02003517 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3518 __func__, (unsigned long long)instr->addr,
3519 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003520
Christian Hitzb8a6b372011-10-12 09:32:02 +02003521 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003522 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003523
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003524 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003525 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003526
3527 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003528 page = (int)(instr->addr >> chip->page_shift);
3529 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003530
3531 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003532 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003533
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003534 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003535 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003536
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003537 /* Check, if it is write protected */
3538 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003539 pr_debug("%s: device is write protected!\n",
3540 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003541 instr->state = MTD_ERASE_FAILED;
3542 goto erase_exit;
3543 }
3544
3545 /* Loop through the pages */
3546 len = instr->len;
3547
3548 instr->state = MTD_ERASING;
3549
3550 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06003551 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003552
Sergey Lapin3a38a552013-01-14 03:46:50 +00003553 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003554 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003555 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003556 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003557 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003558 instr->state = MTD_ERASE_FAILED;
Farhan Ali7c053192021-02-24 15:25:53 -08003559 instr->fail_addr =
3560 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003561 goto erase_exit;
3562 }
William Juul52c07962007-10-31 13:53:06 +01003563
3564 /*
3565 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003566 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003567 */
3568 if (page <= chip->pagebuf && chip->pagebuf <
3569 (page + pages_per_block))
3570 chip->pagebuf = -1;
3571
Scott Wood3ea94ed2015-06-26 19:03:26 -05003572 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003573
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003574 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003575 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003576 pr_debug("%s: failed erase, page 0x%08x\n",
3577 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003578 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003579 instr->fail_addr =
3580 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003581 goto erase_exit;
3582 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003583
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003584 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003585 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003586 page += pages_per_block;
3587
3588 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003589 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003590 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003591 chip->select_chip(mtd, -1);
3592 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003593 }
3594 }
3595 instr->state = MTD_ERASE_DONE;
3596
Christian Hitz13fc0e22011-10-12 09:32:01 +02003597erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003598
3599 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003600
3601 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003602 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003603 nand_release_device(mtd);
3604
3605 /* Return more or less happy */
3606 return ret;
3607}
3608
3609/**
3610 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003611 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003612 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003613 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003614 */
William Juul52c07962007-10-31 13:53:06 +01003615static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003616{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003617 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003618
3619 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003620 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003621 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003622 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003623}
3624
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003625/**
William Juul52c07962007-10-31 13:53:06 +01003626 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003627 * @mtd: MTD device structure
3628 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003629 */
William Juul52c07962007-10-31 13:53:06 +01003630static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003631{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003632 struct nand_chip *chip = mtd_to_nand(mtd);
3633 int chipnr = (int)(offs >> chip->chip_shift);
3634 int ret;
3635
3636 /* Select the NAND device */
3637 nand_get_device(mtd, FL_READING);
3638 chip->select_chip(mtd, chipnr);
3639
3640 ret = nand_block_checkbad(mtd, offs, 0);
3641
3642 chip->select_chip(mtd, -1);
3643 nand_release_device(mtd);
3644
3645 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003646}
3647
3648/**
William Juul52c07962007-10-31 13:53:06 +01003649 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003650 * @mtd: MTD device structure
3651 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003652 */
William Juul52c07962007-10-31 13:53:06 +01003653static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003654{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003655 int ret;
3656
Christian Hitzb8a6b372011-10-12 09:32:02 +02003657 ret = nand_block_isbad(mtd, ofs);
3658 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003659 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003660 if (ret > 0)
3661 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003662 return ret;
3663 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003664
Heiko Schocherf5895d12014-06-24 10:10:04 +02003665 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003666}
3667
Heiko Schocherf5895d12014-06-24 10:10:04 +02003668/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003669 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3670 * @mtd: MTD device structure
3671 * @chip: nand chip info structure
3672 * @addr: feature address.
3673 * @subfeature_param: the subfeature parameters, a four bytes array.
3674 */
3675static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3676 int addr, uint8_t *subfeature_param)
3677{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003678#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3679 if (!chip->onfi_version ||
3680 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3681 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003682 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003683#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003684
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003685 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003686}
3687
3688/**
3689 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3690 * @mtd: MTD device structure
3691 * @chip: nand chip info structure
3692 * @addr: feature address.
3693 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003694 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003695static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3696 int addr, uint8_t *subfeature_param)
3697{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003698#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3699 if (!chip->onfi_version ||
3700 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3701 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003702 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003703#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003704
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003705 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003706}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003707
Sergey Lapin3a38a552013-01-14 03:46:50 +00003708/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003709static void nand_set_defaults(struct nand_chip *chip, int busw)
3710{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003711 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003712 if (!chip->chip_delay)
3713 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003714
3715 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003716 if (chip->cmdfunc == NULL)
3717 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003718
3719 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003720 if (chip->waitfunc == NULL)
3721 chip->waitfunc = nand_wait;
3722
3723 if (!chip->select_chip)
3724 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003725
3726 /* set for ONFI nand */
3727 if (!chip->onfi_set_features)
3728 chip->onfi_set_features = nand_onfi_set_features;
3729 if (!chip->onfi_get_features)
3730 chip->onfi_get_features = nand_onfi_get_features;
3731
3732 /* If called twice, pointers that depend on busw may need to be reset */
3733 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003734 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3735 if (!chip->read_word)
3736 chip->read_word = nand_read_word;
3737 if (!chip->block_bad)
3738 chip->block_bad = nand_block_bad;
3739 if (!chip->block_markbad)
3740 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003741 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003742 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003743 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3744 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3745 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003746 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003747 if (!chip->scan_bbt)
3748 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003749
3750 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003751 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003752 spin_lock_init(&chip->controller->lock);
3753 init_waitqueue_head(&chip->controller->wq);
3754 }
3755
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003756 if (!chip->buf_align)
3757 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003758}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003759
Sergey Lapin3a38a552013-01-14 03:46:50 +00003760/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003761static void sanitize_string(char *s, size_t len)
3762{
3763 ssize_t i;
3764
Sergey Lapin3a38a552013-01-14 03:46:50 +00003765 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003766 s[len - 1] = 0;
3767
Sergey Lapin3a38a552013-01-14 03:46:50 +00003768 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003769 for (i = 0; i < len - 1; i++) {
3770 if (s[i] < ' ' || s[i] > 127)
3771 s[i] = '?';
3772 }
3773
Sergey Lapin3a38a552013-01-14 03:46:50 +00003774 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003775 strim(s);
3776}
3777
Florian Fainellic98a9352011-02-25 00:01:34 +00003778static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3779{
3780 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003781 while (len--) {
3782 crc ^= *p++ << 8;
3783 for (i = 0; i < 8; i++)
3784 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3785 }
3786
3787 return crc;
3788}
3789
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003790#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003791/* Parse the Extended Parameter Page. */
3792static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3793 struct nand_chip *chip, struct nand_onfi_params *p)
3794{
3795 struct onfi_ext_param_page *ep;
3796 struct onfi_ext_section *s;
3797 struct onfi_ext_ecc_info *ecc;
3798 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003799 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003800 int len;
3801 int i;
3802
3803 len = le16_to_cpu(p->ext_param_page_length) * 16;
3804 ep = kmalloc(len, GFP_KERNEL);
3805 if (!ep)
3806 return -ENOMEM;
3807
3808 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003809 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3810 if (ret)
3811 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003812
3813 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003814 ret = nand_change_read_column_op(chip,
3815 sizeof(*p) * p->num_of_param_pages,
3816 ep, len, true);
3817 if (ret)
3818 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003819
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003820 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003821 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3822 != le16_to_cpu(ep->crc))) {
3823 pr_debug("fail in the CRC.\n");
3824 goto ext_out;
3825 }
3826
3827 /*
3828 * Check the signature.
3829 * Do not strictly follow the ONFI spec, maybe changed in future.
3830 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003831 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003832 pr_debug("The signature is invalid.\n");
3833 goto ext_out;
3834 }
3835
3836 /* find the ECC section. */
3837 cursor = (uint8_t *)(ep + 1);
3838 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3839 s = ep->sections + i;
3840 if (s->type == ONFI_SECTION_TYPE_2)
3841 break;
3842 cursor += s->length * 16;
3843 }
3844 if (i == ONFI_EXT_SECTION_MAX) {
3845 pr_debug("We can not find the ECC section.\n");
3846 goto ext_out;
3847 }
3848
3849 /* get the info we want. */
3850 ecc = (struct onfi_ext_ecc_info *)cursor;
3851
3852 if (!ecc->codeword_size) {
3853 pr_debug("Invalid codeword size\n");
3854 goto ext_out;
3855 }
3856
3857 chip->ecc_strength_ds = ecc->ecc_bits;
3858 chip->ecc_step_ds = 1 << ecc->codeword_size;
3859 ret = 0;
3860
3861ext_out:
3862 kfree(ep);
3863 return ret;
3864}
3865
3866static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3867{
Scott Wood17fed142016-05-30 13:57:56 -05003868 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003869 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3870
3871 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3872 feature);
3873}
3874
3875/*
3876 * Configure chip properties from Micron vendor-specific ONFI table
3877 */
3878static void nand_onfi_detect_micron(struct nand_chip *chip,
3879 struct nand_onfi_params *p)
3880{
3881 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3882
3883 if (le16_to_cpu(p->vendor_revision) < 1)
3884 return;
3885
3886 chip->read_retries = micron->read_retry_options;
3887 chip->setup_read_retry = nand_setup_read_retry_micron;
3888}
3889
Florian Fainellic98a9352011-02-25 00:01:34 +00003890/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003891 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003892 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003893static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003894 int *busw)
3895{
3896 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003897 char id[4];
3898 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003899
Sergey Lapin3a38a552013-01-14 03:46:50 +00003900 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003901 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3902 if (ret || strncmp(id, "ONFI", 4))
3903 return 0;
3904
3905 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3906 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003907 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003908
Florian Fainellic98a9352011-02-25 00:01:34 +00003909 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003910 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3911 if (ret)
3912 return 0;
3913
Florian Fainellic98a9352011-02-25 00:01:34 +00003914 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003915 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003916 break;
3917 }
3918 }
3919
Heiko Schocherf5895d12014-06-24 10:10:04 +02003920 if (i == 3) {
3921 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003922 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003923 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003924
Sergey Lapin3a38a552013-01-14 03:46:50 +00003925 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003926 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003927 if (val & (1 << 5))
3928 chip->onfi_version = 23;
3929 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003930 chip->onfi_version = 22;
3931 else if (val & (1 << 3))
3932 chip->onfi_version = 21;
3933 else if (val & (1 << 2))
3934 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003935 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003936 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003937
3938 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003939 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003940 return 0;
3941 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003942
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003943 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3944 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003945 if (!mtd->name)
3946 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003947
Florian Fainellic98a9352011-02-25 00:01:34 +00003948 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003949
3950 /*
3951 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3952 * (don't ask me who thought of this...). MTD assumes that these
3953 * dimensions will be power-of-2, so just truncate the remaining area.
3954 */
3955 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3956 mtd->erasesize *= mtd->writesize;
3957
Florian Fainellic98a9352011-02-25 00:01:34 +00003958 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003959
3960 /* See erasesize comment */
3961 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003962 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003963 chip->bits_per_cell = p->bits_per_cell;
3964
3965 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003966 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003967 else
3968 *busw = 0;
3969
3970 if (p->ecc_bits != 0xff) {
3971 chip->ecc_strength_ds = p->ecc_bits;
3972 chip->ecc_step_ds = 512;
3973 } else if (chip->onfi_version >= 21 &&
3974 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3975
3976 /*
3977 * The nand_flash_detect_ext_param_page() uses the
3978 * Change Read Column command which maybe not supported
3979 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3980 * now. We do not replace user supplied command function.
3981 */
3982 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3983 chip->cmdfunc = nand_command_lp;
3984
3985 /* The Extended Parameter Page is supported since ONFI 2.1. */
3986 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3987 pr_warn("Failed to detect ONFI extended param page\n");
3988 } else {
3989 pr_warn("Could not retrieve ONFI ECC requirements\n");
3990 }
3991
3992 if (p->jedec_id == NAND_MFR_MICRON)
3993 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003994
3995 return 1;
3996}
3997#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02003998static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003999 int *busw)
4000{
4001 return 0;
4002}
4003#endif
4004
William Juul52c07962007-10-31 13:53:06 +01004005/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004006 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4007 */
4008static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
4009 int *busw)
4010{
4011 struct nand_jedec_params *p = &chip->jedec_params;
4012 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004013 char id[5];
4014 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004015
4016 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004017 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4018 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004019 return 0;
4020
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004021 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4022 if (ret)
4023 return 0;
4024
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004025 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004026 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4027 if (ret)
4028 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004029
4030 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4031 le16_to_cpu(p->crc))
4032 break;
4033 }
4034
4035 if (i == 3) {
4036 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4037 return 0;
4038 }
4039
4040 /* Check version */
4041 val = le16_to_cpu(p->revision);
4042 if (val & (1 << 2))
4043 chip->jedec_version = 10;
4044 else if (val & (1 << 1))
4045 chip->jedec_version = 1; /* vendor specific version */
4046
4047 if (!chip->jedec_version) {
4048 pr_info("unsupported JEDEC version: %d\n", val);
4049 return 0;
4050 }
4051
4052 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4053 sanitize_string(p->model, sizeof(p->model));
4054 if (!mtd->name)
4055 mtd->name = p->model;
4056
4057 mtd->writesize = le32_to_cpu(p->byte_per_page);
4058
4059 /* Please reference to the comment for nand_flash_detect_onfi. */
4060 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4061 mtd->erasesize *= mtd->writesize;
4062
4063 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4064
4065 /* Please reference to the comment for nand_flash_detect_onfi. */
4066 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4067 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4068 chip->bits_per_cell = p->bits_per_cell;
4069
4070 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
4071 *busw = NAND_BUSWIDTH_16;
4072 else
4073 *busw = 0;
4074
4075 /* ECC info */
4076 ecc = &p->ecc_info[0];
4077
4078 if (ecc->codeword_size >= 9) {
4079 chip->ecc_strength_ds = ecc->ecc_bits;
4080 chip->ecc_step_ds = 1 << ecc->codeword_size;
4081 } else {
4082 pr_warn("Invalid codeword size\n");
4083 }
4084
4085 return 1;
4086}
4087
4088/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004089 * nand_id_has_period - Check if an ID string has a given wraparound period
4090 * @id_data: the ID string
4091 * @arrlen: the length of the @id_data array
4092 * @period: the period of repitition
4093 *
4094 * Check if an ID string is repeated within a given sequence of bytes at
4095 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02004096 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00004097 * if the repetition has a period of @period; otherwise, returns zero.
4098 */
4099static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4100{
4101 int i, j;
4102 for (i = 0; i < period; i++)
4103 for (j = i + period; j < arrlen; j += period)
4104 if (id_data[i] != id_data[j])
4105 return 0;
4106 return 1;
4107}
4108
4109/*
4110 * nand_id_len - Get the length of an ID string returned by CMD_READID
4111 * @id_data: the ID string
4112 * @arrlen: the length of the @id_data array
4113
4114 * Returns the length of the ID string, according to known wraparound/trailing
4115 * zero patterns. If no pattern exists, returns the length of the array.
4116 */
4117static int nand_id_len(u8 *id_data, int arrlen)
4118{
4119 int last_nonzero, period;
4120
4121 /* Find last non-zero byte */
4122 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4123 if (id_data[last_nonzero])
4124 break;
4125
4126 /* All zeros */
4127 if (last_nonzero < 0)
4128 return 0;
4129
4130 /* Calculate wraparound period */
4131 for (period = 1; period < arrlen; period++)
4132 if (nand_id_has_period(id_data, arrlen, period))
4133 break;
4134
4135 /* There's a repeated pattern */
4136 if (period < arrlen)
4137 return period;
4138
4139 /* There are trailing zeros */
4140 if (last_nonzero < arrlen - 1)
4141 return last_nonzero + 1;
4142
4143 /* No pattern detected */
4144 return arrlen;
4145}
4146
Heiko Schocherf5895d12014-06-24 10:10:04 +02004147/* Extract the bits of per cell from the 3rd byte of the extended ID */
4148static int nand_get_bits_per_cell(u8 cellinfo)
4149{
4150 int bits;
4151
4152 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4153 bits >>= NAND_CI_CELLTYPE_SHIFT;
4154 return bits + 1;
4155}
4156
Sergey Lapin3a38a552013-01-14 03:46:50 +00004157/*
4158 * Many new NAND share similar device ID codes, which represent the size of the
4159 * chip. The rest of the parameters must be decoded according to generic or
4160 * manufacturer-specific "extended ID" decoding patterns.
4161 */
4162static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
4163 u8 id_data[8], int *busw)
4164{
4165 int extid, id_len;
4166 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004167 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004168 /* The 4th id byte is the important one */
4169 extid = id_data[3];
4170
4171 id_len = nand_id_len(id_data, 8);
4172
4173 /*
4174 * Field definitions are in the following datasheets:
4175 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
4176 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
4177 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
4178 *
4179 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
4180 * ID to decide what to do.
4181 */
4182 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02004183 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004184 /* Calc pagesize */
4185 mtd->writesize = 2048 << (extid & 0x03);
4186 extid >>= 2;
4187 /* Calc oobsize */
4188 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4189 case 1:
4190 mtd->oobsize = 128;
4191 break;
4192 case 2:
4193 mtd->oobsize = 218;
4194 break;
4195 case 3:
4196 mtd->oobsize = 400;
4197 break;
4198 case 4:
4199 mtd->oobsize = 436;
4200 break;
4201 case 5:
4202 mtd->oobsize = 512;
4203 break;
4204 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004205 mtd->oobsize = 640;
4206 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004207 case 7:
4208 default: /* Other cases are "reserved" (unknown) */
4209 mtd->oobsize = 1024;
4210 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004211 }
4212 extid >>= 2;
4213 /* Calc blocksize */
4214 mtd->erasesize = (128 * 1024) <<
4215 (((extid >> 1) & 0x04) | (extid & 0x03));
4216 *busw = 0;
4217 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02004218 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004219 unsigned int tmp;
4220
4221 /* Calc pagesize */
4222 mtd->writesize = 2048 << (extid & 0x03);
4223 extid >>= 2;
4224 /* Calc oobsize */
4225 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4226 case 0:
4227 mtd->oobsize = 128;
4228 break;
4229 case 1:
4230 mtd->oobsize = 224;
4231 break;
4232 case 2:
4233 mtd->oobsize = 448;
4234 break;
4235 case 3:
4236 mtd->oobsize = 64;
4237 break;
4238 case 4:
4239 mtd->oobsize = 32;
4240 break;
4241 case 5:
4242 mtd->oobsize = 16;
4243 break;
4244 default:
4245 mtd->oobsize = 640;
4246 break;
4247 }
4248 extid >>= 2;
4249 /* Calc blocksize */
4250 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
4251 if (tmp < 0x03)
4252 mtd->erasesize = (128 * 1024) << tmp;
4253 else if (tmp == 0x03)
4254 mtd->erasesize = 768 * 1024;
4255 else
4256 mtd->erasesize = (64 * 1024) << tmp;
4257 *busw = 0;
4258 } else {
4259 /* Calc pagesize */
4260 mtd->writesize = 1024 << (extid & 0x03);
4261 extid >>= 2;
4262 /* Calc oobsize */
4263 mtd->oobsize = (8 << (extid & 0x01)) *
4264 (mtd->writesize >> 9);
4265 extid >>= 2;
4266 /* Calc blocksize. Blocksize is multiples of 64KiB */
4267 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4268 extid >>= 2;
4269 /* Get buswidth information */
4270 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004271
4272 /*
4273 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
4274 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
4275 * follows:
4276 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
4277 * 110b -> 24nm
4278 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
4279 */
4280 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
4281 nand_is_slc(chip) &&
4282 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
4283 !(id_data[4] & 0x80) /* !BENAND */) {
4284 mtd->oobsize = 32 * mtd->writesize >> 9;
4285 }
4286
Sergey Lapin3a38a552013-01-14 03:46:50 +00004287 }
4288}
4289
Heiko Schocherf5895d12014-06-24 10:10:04 +02004290/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004291 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4292 * decodes a matching ID table entry and assigns the MTD size parameters for
4293 * the chip.
4294 */
4295static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004296 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00004297 int *busw)
4298{
4299 int maf_id = id_data[0];
4300
4301 mtd->erasesize = type->erasesize;
4302 mtd->writesize = type->pagesize;
4303 mtd->oobsize = mtd->writesize / 32;
4304 *busw = type->options & NAND_BUSWIDTH_16;
4305
Heiko Schocherf5895d12014-06-24 10:10:04 +02004306 /* All legacy ID NAND are small-page, SLC */
4307 chip->bits_per_cell = 1;
4308
Sergey Lapin3a38a552013-01-14 03:46:50 +00004309 /*
4310 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
4311 * some Spansion chips have erasesize that conflicts with size
4312 * listed in nand_ids table.
4313 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
4314 */
4315 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
4316 && id_data[6] == 0x00 && id_data[7] == 0x00
4317 && mtd->writesize == 512) {
4318 mtd->erasesize = 128 * 1024;
4319 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
4320 }
4321}
4322
Heiko Schocherf5895d12014-06-24 10:10:04 +02004323/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004324 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4325 * heuristic patterns using various detected parameters (e.g., manufacturer,
4326 * page size, cell-type information).
4327 */
4328static void nand_decode_bbm_options(struct mtd_info *mtd,
4329 struct nand_chip *chip, u8 id_data[8])
4330{
4331 int maf_id = id_data[0];
4332
4333 /* Set the bad block position */
4334 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4335 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4336 else
4337 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4338
4339 /*
4340 * Bad block marker is stored in the last page of each block on Samsung
4341 * and Hynix MLC devices; stored in first two pages of each block on
4342 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4343 * AMD/Spansion, and Macronix. All others scan only the first page.
4344 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004345 if (!nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00004346 (maf_id == NAND_MFR_SAMSUNG ||
4347 maf_id == NAND_MFR_HYNIX))
4348 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004349 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00004350 (maf_id == NAND_MFR_SAMSUNG ||
4351 maf_id == NAND_MFR_HYNIX ||
4352 maf_id == NAND_MFR_TOSHIBA ||
4353 maf_id == NAND_MFR_AMD ||
4354 maf_id == NAND_MFR_MACRONIX)) ||
4355 (mtd->writesize == 2048 &&
4356 maf_id == NAND_MFR_MICRON))
4357 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4358}
4359
Heiko Schocherf5895d12014-06-24 10:10:04 +02004360static inline bool is_full_id_nand(struct nand_flash_dev *type)
4361{
4362 return type->id_len;
4363}
4364
4365static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
4366 struct nand_flash_dev *type, u8 *id_data, int *busw)
4367{
Heiko Schocherf5895d12014-06-24 10:10:04 +02004368 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004369 mtd->writesize = type->pagesize;
4370 mtd->erasesize = type->erasesize;
4371 mtd->oobsize = type->oobsize;
4372
4373 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
4374 chip->chipsize = (uint64_t)type->chipsize << 20;
4375 chip->options |= type->options;
4376 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4377 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004378 chip->onfi_timing_mode_default =
4379 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004380
4381 *busw = type->options & NAND_BUSWIDTH_16;
4382
4383 if (!mtd->name)
4384 mtd->name = type->name;
4385
4386 return true;
4387 }
4388 return false;
4389}
4390
Sergey Lapin3a38a552013-01-14 03:46:50 +00004391/*
4392 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004393 */
Jörg Krause929fb442018-01-14 19:26:37 +01004394struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01004395 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00004396 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004397 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004398{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004399 int busw, ret;
4400 int maf_idx;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004401 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004402
Karl Beldanb6322fc2008-09-15 16:08:03 +02004403 /*
4404 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004405 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004406 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004407 ret = nand_reset(chip, 0);
4408 if (ret)
4409 return ERR_PTR(ret);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004410
4411 /* Select the device */
4412 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004413
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004414 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004415 ret = nand_readid_op(chip, 0, id_data, 2);
4416 if (ret)
4417 return ERR_PTR(ret);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004418
4419 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004420 *maf_id = id_data[0];
4421 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004422
Sergey Lapin3a38a552013-01-14 03:46:50 +00004423 /*
4424 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004425 * interface concerns can cause random data which looks like a
4426 * possibly credible NAND flash to appear. If the two results do
4427 * not match, ignore the device completely.
4428 */
4429
Sergey Lapin3a38a552013-01-14 03:46:50 +00004430 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004431 ret = nand_readid_op(chip, 0, id_data, 8);
4432 if (ret)
4433 return ERR_PTR(ret);
Scott Wood3628f002008-10-24 16:20:43 -05004434
Christian Hitzb8a6b372011-10-12 09:32:02 +02004435 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004436 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004437 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05004438 return ERR_PTR(-ENODEV);
4439 }
4440
Lei Wen75bde942011-01-06 09:48:18 +08004441 if (!type)
4442 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004443
Heiko Schocherf5895d12014-06-24 10:10:04 +02004444 for (; type->name != NULL; type++) {
4445 if (is_full_id_nand(type)) {
4446 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4447 goto ident_done;
4448 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004449 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004450 }
4451 }
Lei Wen75bde942011-01-06 09:48:18 +08004452
Christian Hitzb8a6b372011-10-12 09:32:02 +02004453 chip->onfi_version = 0;
4454 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004455 /* Check if the chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00004456 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004457 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004458
4459 /* Check if the chip is JEDEC compliant */
4460 if (nand_flash_detect_jedec(mtd, chip, &busw))
4461 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004462 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004463
Christian Hitzb8a6b372011-10-12 09:32:02 +02004464 if (!type->name)
4465 return ERR_PTR(-ENODEV);
4466
William Juul52c07962007-10-31 13:53:06 +01004467 if (!mtd->name)
4468 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004469
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004470 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004471
Scott Wood52ab7ce2016-05-30 13:57:58 -05004472 if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004473 /* Decode parameters from extended ID */
4474 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004475 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004476 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004477 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004478 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004479 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004480
Sergey Lapin3a38a552013-01-14 03:46:50 +00004481 /*
4482 * Check if chip is not a Samsung device. Do not clear the
4483 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02004484 */
4485 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4486 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4487ident_done:
4488
William Juul52c07962007-10-31 13:53:06 +01004489 /* Try to identify manufacturer */
4490 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
4491 if (nand_manuf_ids[maf_idx].id == *maf_id)
4492 break;
4493 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004494
Heiko Schocherf5895d12014-06-24 10:10:04 +02004495 if (chip->options & NAND_BUSWIDTH_AUTO) {
4496 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4497 chip->options |= busw;
4498 nand_set_defaults(chip, busw);
4499 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4500 /*
4501 * Check, if buswidth is correct. Hardware drivers should set
4502 * chip correct!
4503 */
4504 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4505 *maf_id, *dev_id);
4506 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4507 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004508 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4509 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01004510 return ERR_PTR(-EINVAL);
4511 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004512
Sergey Lapin3a38a552013-01-14 03:46:50 +00004513 nand_decode_bbm_options(mtd, chip, id_data);
4514
William Juul52c07962007-10-31 13:53:06 +01004515 /* Calculate the address shift from the page size */
4516 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004517 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004518 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004519
William Juul52c07962007-10-31 13:53:06 +01004520 chip->bbt_erase_shift = chip->phys_erase_shift =
4521 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004522 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004523 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004524 else {
4525 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4526 chip->chip_shift += 32 - 1;
4527 }
4528
Masahiro Yamada984926b2017-11-22 02:38:31 +09004529 if (chip->chip_shift - chip->page_shift > 16)
4530 chip->options |= NAND_ROW_ADDR_3;
4531
Christian Hitzb8a6b372011-10-12 09:32:02 +02004532 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004533 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004534
Sergey Lapin3a38a552013-01-14 03:46:50 +00004535 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004536 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4537 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004538
Heiko Schocherf5895d12014-06-24 10:10:04 +02004539 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4540 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004541
Christian Hitzb8a6b372011-10-12 09:32:02 +02004542#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004543 if (chip->onfi_version)
4544 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4545 chip->onfi_params.model);
4546 else if (chip->jedec_version)
4547 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4548 chip->jedec_params.model);
4549 else
4550 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4551 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004552#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004553 if (chip->jedec_version)
4554 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4555 chip->jedec_params.model);
4556 else
4557 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4558 type->name);
4559
4560 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4561 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004562#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004563
Scott Wood3ea94ed2015-06-26 19:03:26 -05004564 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004565 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004566 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01004567 return type;
4568}
Jörg Krause929fb442018-01-14 19:26:37 +01004569EXPORT_SYMBOL(nand_get_flash_type);
William Juul52c07962007-10-31 13:53:06 +01004570
Brian Norrisba6463d2016-06-15 21:09:22 +02004571#if CONFIG_IS_ENABLED(OF_CONTROL)
Brian Norrisba6463d2016-06-15 21:09:22 +02004572
Patrice Chotardbc77af52021-09-13 16:25:53 +02004573static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004574{
4575 int ret, ecc_mode = -1, ecc_strength, ecc_step;
Brian Norrisba6463d2016-06-15 21:09:22 +02004576 const char *str;
4577
Patrice Chotardbc77af52021-09-13 16:25:53 +02004578 ret = ofnode_read_s32_default(node, "nand-bus-width", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004579 if (ret == 16)
4580 chip->options |= NAND_BUSWIDTH_16;
4581
Patrice Chotardbc77af52021-09-13 16:25:53 +02004582 if (ofnode_read_bool(node, "nand-on-flash-bbt"))
Brian Norrisba6463d2016-06-15 21:09:22 +02004583 chip->bbt_options |= NAND_BBT_USE_FLASH;
4584
Patrice Chotardbc77af52021-09-13 16:25:53 +02004585 str = ofnode_read_string(node, "nand-ecc-mode");
Brian Norrisba6463d2016-06-15 21:09:22 +02004586 if (str) {
4587 if (!strcmp(str, "none"))
4588 ecc_mode = NAND_ECC_NONE;
4589 else if (!strcmp(str, "soft"))
4590 ecc_mode = NAND_ECC_SOFT;
4591 else if (!strcmp(str, "hw"))
4592 ecc_mode = NAND_ECC_HW;
4593 else if (!strcmp(str, "hw_syndrome"))
4594 ecc_mode = NAND_ECC_HW_SYNDROME;
4595 else if (!strcmp(str, "hw_oob_first"))
4596 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4597 else if (!strcmp(str, "soft_bch"))
4598 ecc_mode = NAND_ECC_SOFT_BCH;
4599 }
4600
Patrice Chotardbc77af52021-09-13 16:25:53 +02004601 ecc_strength = ofnode_read_s32_default(node,
4602 "nand-ecc-strength", -1);
4603 ecc_step = ofnode_read_s32_default(node,
4604 "nand-ecc-step-size", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004605
4606 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4607 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4608 pr_err("must set both strength and step size in DT\n");
4609 return -EINVAL;
4610 }
4611
4612 if (ecc_mode >= 0)
4613 chip->ecc.mode = ecc_mode;
4614
4615 if (ecc_strength >= 0)
4616 chip->ecc.strength = ecc_strength;
4617
4618 if (ecc_step > 0)
4619 chip->ecc.size = ecc_step;
4620
Patrice Chotardbc77af52021-09-13 16:25:53 +02004621 if (ofnode_read_bool(node, "nand-ecc-maximize"))
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004622 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4623
Brian Norrisba6463d2016-06-15 21:09:22 +02004624 return 0;
4625}
4626#else
Patrice Chotardbc77af52021-09-13 16:25:53 +02004627static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004628{
4629 return 0;
4630}
4631#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4632
William Juul52c07962007-10-31 13:53:06 +01004633/**
4634 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004635 * @mtd: MTD device structure
4636 * @maxchips: number of chips to scan for
4637 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004638 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004639 * This is the first phase of the normal nand_scan() function. It reads the
4640 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004641 *
William Juul52c07962007-10-31 13:53:06 +01004642 */
Lei Wen75bde942011-01-06 09:48:18 +08004643int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004644 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004645{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004646 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004647 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004648 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02004649 int ret;
4650
Patrice Chotardbc77af52021-09-13 16:25:53 +02004651 if (ofnode_valid(chip->flash_node)) {
Brian Norrisba6463d2016-06-15 21:09:22 +02004652 ret = nand_dt_init(mtd, chip, chip->flash_node);
4653 if (ret)
4654 return ret;
4655 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004656
William Juul52c07962007-10-31 13:53:06 +01004657 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004658 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004659
4660 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004661 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4662 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004663
4664 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004665 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4666 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004667 chip->select_chip(mtd, -1);
4668 return PTR_ERR(type);
4669 }
4670
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004671 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004672 ret = nand_init_data_interface(chip);
4673 if (ret)
4674 return ret;
4675
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004676 /*
4677 * Setup the data interface correctly on the chip and controller side.
4678 * This explicit call to nand_setup_data_interface() is only required
4679 * for the first die, because nand_reset() has been called before
4680 * ->data_interface and ->default_onfi_timing_mode were set.
4681 * For the other dies, nand_reset() will automatically switch to the
4682 * best mode for us.
4683 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004684 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004685 if (ret)
4686 return ret;
4687
Heiko Schocherf5895d12014-06-24 10:10:04 +02004688 chip->select_chip(mtd, -1);
4689
William Juul52c07962007-10-31 13:53:06 +01004690 /* Check for a chip array */
4691 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004692 u8 id[2];
4693
Karl Beldanb6322fc2008-09-15 16:08:03 +02004694 /* See comment in nand_get_flash_type for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004695 nand_reset(chip, i);
4696
4697 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004698 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004699 nand_readid_op(chip, 0, id, sizeof(id));
4700
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004701 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004702 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004703 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004704 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004705 }
4706 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004707 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004708
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004709#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004710 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004711 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004712#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004713
William Juul52c07962007-10-31 13:53:06 +01004714 /* Store the number of chips and calc total size for mtd */
4715 chip->numchips = i;
4716 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004717
William Juul52c07962007-10-31 13:53:06 +01004718 return 0;
4719}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004720EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004721
Masahiro Yamada820eb482017-11-22 02:38:29 +09004722/**
4723 * nand_check_ecc_caps - check the sanity of preset ECC settings
4724 * @chip: nand chip info structure
4725 * @caps: ECC caps info structure
4726 * @oobavail: OOB size that the ECC engine can use
4727 *
4728 * When ECC step size and strength are already set, check if they are supported
4729 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4730 * On success, the calculated ECC bytes is set.
4731 */
4732int nand_check_ecc_caps(struct nand_chip *chip,
4733 const struct nand_ecc_caps *caps, int oobavail)
4734{
4735 struct mtd_info *mtd = nand_to_mtd(chip);
4736 const struct nand_ecc_step_info *stepinfo;
4737 int preset_step = chip->ecc.size;
4738 int preset_strength = chip->ecc.strength;
4739 int nsteps, ecc_bytes;
4740 int i, j;
4741
4742 if (WARN_ON(oobavail < 0))
4743 return -EINVAL;
4744
4745 if (!preset_step || !preset_strength)
4746 return -ENODATA;
4747
4748 nsteps = mtd->writesize / preset_step;
4749
4750 for (i = 0; i < caps->nstepinfos; i++) {
4751 stepinfo = &caps->stepinfos[i];
4752
4753 if (stepinfo->stepsize != preset_step)
4754 continue;
4755
4756 for (j = 0; j < stepinfo->nstrengths; j++) {
4757 if (stepinfo->strengths[j] != preset_strength)
4758 continue;
4759
4760 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4761 preset_strength);
4762 if (WARN_ON_ONCE(ecc_bytes < 0))
4763 return ecc_bytes;
4764
4765 if (ecc_bytes * nsteps > oobavail) {
4766 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4767 preset_step, preset_strength);
4768 return -ENOSPC;
4769 }
4770
4771 chip->ecc.bytes = ecc_bytes;
4772
4773 return 0;
4774 }
4775 }
4776
4777 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4778 preset_step, preset_strength);
4779
4780 return -ENOTSUPP;
4781}
4782EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4783
4784/**
4785 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4786 * @chip: nand chip info structure
4787 * @caps: ECC engine caps info structure
4788 * @oobavail: OOB size that the ECC engine can use
4789 *
4790 * If a chip's ECC requirement is provided, try to meet it with the least
4791 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4792 * On success, the chosen ECC settings are set.
4793 */
4794int nand_match_ecc_req(struct nand_chip *chip,
4795 const struct nand_ecc_caps *caps, int oobavail)
4796{
4797 struct mtd_info *mtd = nand_to_mtd(chip);
4798 const struct nand_ecc_step_info *stepinfo;
4799 int req_step = chip->ecc_step_ds;
4800 int req_strength = chip->ecc_strength_ds;
4801 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4802 int best_step, best_strength, best_ecc_bytes;
4803 int best_ecc_bytes_total = INT_MAX;
4804 int i, j;
4805
4806 if (WARN_ON(oobavail < 0))
4807 return -EINVAL;
4808
4809 /* No information provided by the NAND chip */
4810 if (!req_step || !req_strength)
4811 return -ENOTSUPP;
4812
4813 /* number of correctable bits the chip requires in a page */
4814 req_corr = mtd->writesize / req_step * req_strength;
4815
4816 for (i = 0; i < caps->nstepinfos; i++) {
4817 stepinfo = &caps->stepinfos[i];
4818 step_size = stepinfo->stepsize;
4819
4820 for (j = 0; j < stepinfo->nstrengths; j++) {
4821 strength = stepinfo->strengths[j];
4822
4823 /*
4824 * If both step size and strength are smaller than the
4825 * chip's requirement, it is not easy to compare the
4826 * resulted reliability.
4827 */
4828 if (step_size < req_step && strength < req_strength)
4829 continue;
4830
4831 if (mtd->writesize % step_size)
4832 continue;
4833
4834 nsteps = mtd->writesize / step_size;
4835
4836 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4837 if (WARN_ON_ONCE(ecc_bytes < 0))
4838 continue;
4839 ecc_bytes_total = ecc_bytes * nsteps;
4840
4841 if (ecc_bytes_total > oobavail ||
4842 strength * nsteps < req_corr)
4843 continue;
4844
4845 /*
4846 * We assume the best is to meet the chip's requrement
4847 * with the least number of ECC bytes.
4848 */
4849 if (ecc_bytes_total < best_ecc_bytes_total) {
4850 best_ecc_bytes_total = ecc_bytes_total;
4851 best_step = step_size;
4852 best_strength = strength;
4853 best_ecc_bytes = ecc_bytes;
4854 }
4855 }
4856 }
4857
4858 if (best_ecc_bytes_total == INT_MAX)
4859 return -ENOTSUPP;
4860
4861 chip->ecc.size = best_step;
4862 chip->ecc.strength = best_strength;
4863 chip->ecc.bytes = best_ecc_bytes;
4864
4865 return 0;
4866}
4867EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4868
4869/**
4870 * nand_maximize_ecc - choose the max ECC strength available
4871 * @chip: nand chip info structure
4872 * @caps: ECC engine caps info structure
4873 * @oobavail: OOB size that the ECC engine can use
4874 *
4875 * Choose the max ECC strength that is supported on the controller, and can fit
4876 * within the chip's OOB. On success, the chosen ECC settings are set.
4877 */
4878int nand_maximize_ecc(struct nand_chip *chip,
4879 const struct nand_ecc_caps *caps, int oobavail)
4880{
4881 struct mtd_info *mtd = nand_to_mtd(chip);
4882 const struct nand_ecc_step_info *stepinfo;
4883 int step_size, strength, nsteps, ecc_bytes, corr;
4884 int best_corr = 0;
4885 int best_step = 0;
4886 int best_strength, best_ecc_bytes;
4887 int i, j;
4888
4889 if (WARN_ON(oobavail < 0))
4890 return -EINVAL;
4891
4892 for (i = 0; i < caps->nstepinfos; i++) {
4893 stepinfo = &caps->stepinfos[i];
4894 step_size = stepinfo->stepsize;
4895
4896 /* If chip->ecc.size is already set, respect it */
4897 if (chip->ecc.size && step_size != chip->ecc.size)
4898 continue;
4899
4900 for (j = 0; j < stepinfo->nstrengths; j++) {
4901 strength = stepinfo->strengths[j];
4902
4903 if (mtd->writesize % step_size)
4904 continue;
4905
4906 nsteps = mtd->writesize / step_size;
4907
4908 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4909 if (WARN_ON_ONCE(ecc_bytes < 0))
4910 continue;
4911
4912 if (ecc_bytes * nsteps > oobavail)
4913 continue;
4914
4915 corr = strength * nsteps;
4916
4917 /*
4918 * If the number of correctable bits is the same,
4919 * bigger step_size has more reliability.
4920 */
4921 if (corr > best_corr ||
4922 (corr == best_corr && step_size > best_step)) {
4923 best_corr = corr;
4924 best_step = step_size;
4925 best_strength = strength;
4926 best_ecc_bytes = ecc_bytes;
4927 }
4928 }
4929 }
4930
4931 if (!best_corr)
4932 return -ENOTSUPP;
4933
4934 chip->ecc.size = best_step;
4935 chip->ecc.strength = best_strength;
4936 chip->ecc.bytes = best_ecc_bytes;
4937
4938 return 0;
4939}
4940EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4941
Scott Wood3ea94ed2015-06-26 19:03:26 -05004942/*
4943 * Check if the chip configuration meet the datasheet requirements.
4944
4945 * If our configuration corrects A bits per B bytes and the minimum
4946 * required correction level is X bits per Y bytes, then we must ensure
4947 * both of the following are true:
4948 *
4949 * (1) A / B >= X / Y
4950 * (2) A >= X
4951 *
4952 * Requirement (1) ensures we can correct for the required bitflip density.
4953 * Requirement (2) ensures we can correct even when all bitflips are clumped
4954 * in the same sector.
4955 */
4956static bool nand_ecc_strength_good(struct mtd_info *mtd)
4957{
Scott Wood17fed142016-05-30 13:57:56 -05004958 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004959 struct nand_ecc_ctrl *ecc = &chip->ecc;
4960 int corr, ds_corr;
4961
4962 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4963 /* Not enough information */
4964 return true;
4965
4966 /*
4967 * We get the number of corrected bits per page to compare
4968 * the correction density.
4969 */
4970 corr = (mtd->writesize * ecc->strength) / ecc->size;
4971 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4972
4973 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4974}
William Juul52c07962007-10-31 13:53:06 +01004975
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004976static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4977{
4978 struct nand_ecc_ctrl *ecc = &chip->ecc;
4979
4980 if (nand_standard_page_accessors(ecc))
4981 return false;
4982
4983 /*
4984 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4985 * controller driver implements all the page accessors because
4986 * default helpers are not suitable when the core does not
4987 * send the READ0/PAGEPROG commands.
4988 */
4989 return (!ecc->read_page || !ecc->write_page ||
4990 !ecc->read_page_raw || !ecc->write_page_raw ||
4991 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4992 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4993 ecc->hwctl && ecc->calculate));
4994}
4995
William Juul52c07962007-10-31 13:53:06 +01004996/**
4997 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004998 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004999 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005000 * This is the second phase of the normal nand_scan() function. It fills out
5001 * all the uninitialized function pointers with the defaults and scans for a
5002 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01005003 */
5004int nand_scan_tail(struct mtd_info *mtd)
5005{
5006 int i;
Scott Wood17fed142016-05-30 13:57:56 -05005007 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005008 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005009 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005010
Sergey Lapin3a38a552013-01-14 03:46:50 +00005011 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
5012 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
5013 !(chip->bbt_options & NAND_BBT_USE_FLASH));
5014
Marc Gonzalezc3a29852017-11-22 02:38:22 +09005015 if (invalid_ecc_page_accessors(chip)) {
5016 pr_err("Invalid ECC page accessors setup\n");
5017 return -EINVAL;
5018 }
5019
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005020 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005021 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005022 chip->buffers = nbuf;
5023 } else {
5024 if (!chip->buffers)
5025 return -ENOMEM;
5026 }
William Juul52c07962007-10-31 13:53:06 +01005027
5028 /* Set the internal oob buffer location, just after the page data */
5029 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
5030
5031 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005032 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01005033 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005034 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005035 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02005036#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005037 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005038 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005039 break;
5040 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005041 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005042 break;
5043 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005044 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005045 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005046 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005047 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005048 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01005049#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005050 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005051 pr_warn("No oob scheme defined for oobsize %d\n",
5052 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005053 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005054 }
5055 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005056
William Juul52c07962007-10-31 13:53:06 +01005057 if (!chip->write_page)
5058 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005059
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005060 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005061 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01005062 * selected and we have 256 byte pagesize fallback to software ECC
5063 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005064
Heiko Schocherf5895d12014-06-24 10:10:04 +02005065 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005066 case NAND_ECC_HW_OOB_FIRST:
5067 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005068 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005069 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005070 BUG();
5071 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005072 if (!ecc->read_page)
5073 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005074
William Juul52c07962007-10-31 13:53:06 +01005075 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005076 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005077 if (!ecc->read_page)
5078 ecc->read_page = nand_read_page_hwecc;
5079 if (!ecc->write_page)
5080 ecc->write_page = nand_write_page_hwecc;
5081 if (!ecc->read_page_raw)
5082 ecc->read_page_raw = nand_read_page_raw;
5083 if (!ecc->write_page_raw)
5084 ecc->write_page_raw = nand_write_page_raw;
5085 if (!ecc->read_oob)
5086 ecc->read_oob = nand_read_oob_std;
5087 if (!ecc->write_oob)
5088 ecc->write_oob = nand_write_oob_std;
5089 if (!ecc->read_subpage)
5090 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05005091 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02005092 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005093
William Juul52c07962007-10-31 13:53:06 +01005094 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005095 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5096 (!ecc->read_page ||
5097 ecc->read_page == nand_read_page_hwecc ||
5098 !ecc->write_page ||
5099 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005100 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005101 BUG();
5102 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005103 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005104 if (!ecc->read_page)
5105 ecc->read_page = nand_read_page_syndrome;
5106 if (!ecc->write_page)
5107 ecc->write_page = nand_write_page_syndrome;
5108 if (!ecc->read_page_raw)
5109 ecc->read_page_raw = nand_read_page_raw_syndrome;
5110 if (!ecc->write_page_raw)
5111 ecc->write_page_raw = nand_write_page_raw_syndrome;
5112 if (!ecc->read_oob)
5113 ecc->read_oob = nand_read_oob_syndrome;
5114 if (!ecc->write_oob)
5115 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005116
Heiko Schocherf5895d12014-06-24 10:10:04 +02005117 if (mtd->writesize >= ecc->size) {
5118 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005119 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5120 BUG();
5121 }
William Juul52c07962007-10-31 13:53:06 +01005122 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005123 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005124 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5125 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005126 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005127
William Juul52c07962007-10-31 13:53:06 +01005128 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005129 ecc->calculate = nand_calculate_ecc;
5130 ecc->correct = nand_correct_data;
5131 ecc->read_page = nand_read_page_swecc;
5132 ecc->read_subpage = nand_read_subpage;
5133 ecc->write_page = nand_write_page_swecc;
5134 ecc->read_page_raw = nand_read_page_raw;
5135 ecc->write_page_raw = nand_write_page_raw;
5136 ecc->read_oob = nand_read_oob_std;
5137 ecc->write_oob = nand_write_oob_std;
5138 if (!ecc->size)
5139 ecc->size = 256;
5140 ecc->bytes = 3;
5141 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005142 break;
5143
Christian Hitz55f7bca2011-10-12 09:31:59 +02005144 case NAND_ECC_SOFT_BCH:
5145 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005146 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005147 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005148 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005149 ecc->calculate = nand_bch_calculate_ecc;
5150 ecc->correct = nand_bch_correct_data;
5151 ecc->read_page = nand_read_page_swecc;
5152 ecc->read_subpage = nand_read_subpage;
5153 ecc->write_page = nand_write_page_swecc;
5154 ecc->read_page_raw = nand_read_page_raw;
5155 ecc->write_page_raw = nand_write_page_raw;
5156 ecc->read_oob = nand_read_oob_std;
5157 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005158 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005159 * Board driver should supply ecc.size and ecc.strength values
5160 * to select how many bits are correctable. Otherwise, default
5161 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005162 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005163 if (!ecc->size && (mtd->oobsize >= 64)) {
5164 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005165 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005166 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005167
5168 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005169 ecc->bytes = 0;
5170 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005171 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005172 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005173 BUG();
5174 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005175 break;
5176
William Juul52c07962007-10-31 13:53:06 +01005177 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005178 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005179 ecc->read_page = nand_read_page_raw;
5180 ecc->write_page = nand_write_page_raw;
5181 ecc->read_oob = nand_read_oob_std;
5182 ecc->read_page_raw = nand_read_page_raw;
5183 ecc->write_page_raw = nand_write_page_raw;
5184 ecc->write_oob = nand_write_oob_std;
5185 ecc->size = mtd->writesize;
5186 ecc->bytes = 0;
5187 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005188 break;
5189
5190 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005191 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005192 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005193 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005194
Sergey Lapin3a38a552013-01-14 03:46:50 +00005195 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005196 if (!ecc->read_oob_raw)
5197 ecc->read_oob_raw = ecc->read_oob;
5198 if (!ecc->write_oob_raw)
5199 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005200
William Juul52c07962007-10-31 13:53:06 +01005201 /*
5202 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005203 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005204 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005205 mtd->oobavail = 0;
5206 if (ecc->layout) {
5207 for (i = 0; ecc->layout->oobfree[i].length; i++)
5208 mtd->oobavail += ecc->layout->oobfree[i].length;
5209 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005210
Scott Wood3ea94ed2015-06-26 19:03:26 -05005211 /* ECC sanity check: warn if it's too weak */
5212 if (!nand_ecc_strength_good(mtd))
5213 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5214 mtd->name);
5215
William Juul52c07962007-10-31 13:53:06 +01005216 /*
5217 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005218 * mode.
William Juul52c07962007-10-31 13:53:06 +01005219 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005220 ecc->steps = mtd->writesize / ecc->size;
5221 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005222 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005223 BUG();
5224 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005225 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005226
Sergey Lapin3a38a552013-01-14 03:46:50 +00005227 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005228 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5229 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005230 case 2:
5231 mtd->subpage_sft = 1;
5232 break;
5233 case 4:
5234 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005235 case 16:
William Juul52c07962007-10-31 13:53:06 +01005236 mtd->subpage_sft = 2;
5237 break;
5238 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005239 }
William Juul52c07962007-10-31 13:53:06 +01005240 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005241
William Juul52c07962007-10-31 13:53:06 +01005242 /* Initialize state */
5243 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005244
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005245 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005246 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005247
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005248 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005249 switch (ecc->mode) {
5250 case NAND_ECC_SOFT:
5251 case NAND_ECC_SOFT_BCH:
5252 if (chip->page_shift > 9)
5253 chip->options |= NAND_SUBPAGE_READ;
5254 break;
5255
5256 default:
5257 break;
5258 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005259
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005260 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005261 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005262 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5263 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005264 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005265 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005266 mtd->_read_oob = nand_read_oob;
5267 mtd->_write_oob = nand_write_oob;
5268 mtd->_sync = nand_sync;
5269 mtd->_lock = NULL;
5270 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005271 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005272 mtd->_block_isbad = nand_block_isbad;
5273 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005274 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005275
Sergey Lapin3a38a552013-01-14 03:46:50 +00005276 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005277 mtd->ecclayout = ecc->layout;
5278 mtd->ecc_strength = ecc->strength;
5279 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005280 /*
5281 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5282 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5283 * properly set.
5284 */
5285 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005286 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005287
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005288 return 0;
William Juul52c07962007-10-31 13:53:06 +01005289}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005290EXPORT_SYMBOL(nand_scan_tail);
5291
William Juul52c07962007-10-31 13:53:06 +01005292/**
5293 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005294 * @mtd: MTD device structure
5295 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005296 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005297 * This fills out all the uninitialized function pointers with the defaults.
5298 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005299 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005300 */
5301int nand_scan(struct mtd_info *mtd, int maxchips)
5302{
5303 int ret;
5304
Lei Wen75bde942011-01-06 09:48:18 +08005305 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005306 if (!ret)
5307 ret = nand_scan_tail(mtd);
5308 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005309}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005310EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005311
Heiko Schocherf5895d12014-06-24 10:10:04 +02005312MODULE_LICENSE("GPL");
5313MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5314MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5315MODULE_DESCRIPTION("Generic NAND flash driver code");