blob: daf12807c67e4d51ad328301309d781fb8fe2eed [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
Simon Glass0f2af882020-05-10 11:40:05 -060031#include <log.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020032#include <malloc.h>
33#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070034#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060035#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060036#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060037#include <linux/delay.h>
William Juul52c07962007-10-31 13:53:06 +010038#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000039#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020040#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090041#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020042#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020043#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020044#ifdef CONFIG_MTD_PARTITIONS
45#include <linux/mtd/partitions.h>
46#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020047#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090048#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020049
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020050/* Define default oob placement schemes for large and small page devices */
Gregory CLEMENTe5b96312019-04-17 11:22:05 +020051#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
William Juul52c07962007-10-31 13:53:06 +010052static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020053 .eccbytes = 3,
54 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010055 .oobfree = {
56 {.offset = 3,
57 .length = 2},
58 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020059 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020060};
61
William Juul52c07962007-10-31 13:53:06 +010062static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020063 .eccbytes = 6,
64 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010065 .oobfree = {
66 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020067 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020068};
69
William Juul52c07962007-10-31 13:53:06 +010070static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020071 .eccbytes = 24,
72 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010073 40, 41, 42, 43, 44, 45, 46, 47,
74 48, 49, 50, 51, 52, 53, 54, 55,
75 56, 57, 58, 59, 60, 61, 62, 63},
76 .oobfree = {
77 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020078 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020079};
80
William Juul52c07962007-10-31 13:53:06 +010081static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020082 .eccbytes = 48,
83 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020084 80, 81, 82, 83, 84, 85, 86, 87,
85 88, 89, 90, 91, 92, 93, 94, 95,
86 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010087 104, 105, 106, 107, 108, 109, 110, 111,
88 112, 113, 114, 115, 116, 117, 118, 119,
89 120, 121, 122, 123, 124, 125, 126, 127},
90 .oobfree = {
91 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020092 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020093};
Stefan Agnerbd186142018-12-06 14:57:09 +010094#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020095
Heiko Schocherf5895d12014-06-24 10:10:04 +020096static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010097
98static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
99 struct mtd_oob_ops *ops);
100
Heiko Schocherf5895d12014-06-24 10:10:04 +0200101/*
102 * For devices which display every fart in the system on a separate LED. Is
103 * compiled away when LED support is disabled.
104 */
105DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200106
Christian Hitzb8a6b372011-10-12 09:32:02 +0200107static int check_offs_len(struct mtd_info *mtd,
108 loff_t ofs, uint64_t len)
109{
Scott Wood17fed142016-05-30 13:57:56 -0500110 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200111 int ret = 0;
112
113 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200114 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
115 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200116 ret = -EINVAL;
117 }
118
119 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200120 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
121 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200122 ret = -EINVAL;
123 }
124
Christian Hitzb8a6b372011-10-12 09:32:02 +0200125 return ret;
126}
127
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200128/**
129 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000130 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200131 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200132 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200133 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200134static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100135{
Scott Wood17fed142016-05-30 13:57:56 -0500136 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200137
138 /* De-select the NAND device */
139 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100140}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200141
142/**
143 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000144 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200145 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200146 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200147 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000148uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149{
Scott Wood17fed142016-05-30 13:57:56 -0500150 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100151 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200152}
153
154/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200155 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000156 * @mtd: MTD device structure
157 *
158 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200159 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160 */
William Juul52c07962007-10-31 13:53:06 +0100161static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200162{
Scott Wood17fed142016-05-30 13:57:56 -0500163 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100164 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200165}
166
167/**
168 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000169 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200170 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000171 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200172 */
173static u16 nand_read_word(struct mtd_info *mtd)
174{
Scott Wood17fed142016-05-30 13:57:56 -0500175 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100176 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200177}
178
179/**
180 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000181 * @mtd: MTD device structure
182 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200183 *
184 * Default select function for 1 chip devices.
185 */
William Juul52c07962007-10-31 13:53:06 +0100186static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200187{
Scott Wood17fed142016-05-30 13:57:56 -0500188 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100189
190 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200191 case -1:
William Juul52c07962007-10-31 13:53:06 +0100192 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200193 break;
194 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200195 break;
196
197 default:
198 BUG();
199 }
200}
201
202/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200203 * nand_write_byte - [DEFAULT] write single byte to chip
204 * @mtd: MTD device structure
205 * @byte: value to write
206 *
207 * Default function to write a byte to I/O[7:0]
208 */
209static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
210{
Scott Wood17fed142016-05-30 13:57:56 -0500211 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200212
213 chip->write_buf(mtd, &byte, 1);
214}
215
216/**
217 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
218 * @mtd: MTD device structure
219 * @byte: value to write
220 *
221 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
222 */
223static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
224{
Scott Wood17fed142016-05-30 13:57:56 -0500225 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200226 uint16_t word = byte;
227
228 /*
229 * It's not entirely clear what should happen to I/O[15:8] when writing
230 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
231 *
232 * When the host supports a 16-bit bus width, only data is
233 * transferred at the 16-bit width. All address and command line
234 * transfers shall use only the lower 8-bits of the data bus. During
235 * command transfers, the host may place any value on the upper
236 * 8-bits of the data bus. During address transfers, the host shall
237 * set the upper 8-bits of the data bus to 00h.
238 *
239 * One user of the write_byte callback is nand_onfi_set_features. The
240 * four parameters are specified to be written to I/O[7:0], but this is
241 * neither an address nor a command transfer. Let's assume a 0 on the
242 * upper I/O lines is OK.
243 */
244 chip->write_buf(mtd, (uint8_t *)&word, 2);
245}
246
Heiko Schocherf5895d12014-06-24 10:10:04 +0200247/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200248 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000249 * @mtd: MTD device structure
250 * @buf: data buffer
251 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200252 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000253 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200254 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000255void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200256{
Scott Wood17fed142016-05-30 13:57:56 -0500257 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200258
Heiko Schocherf5895d12014-06-24 10:10:04 +0200259 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200260}
261
262/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200263 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000264 * @mtd: MTD device structure
265 * @buf: buffer to store date
266 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200267 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000268 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200269 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400270void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200271{
Scott Wood17fed142016-05-30 13:57:56 -0500272 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200273
Heiko Schocherf5895d12014-06-24 10:10:04 +0200274 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200275}
276
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200277/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200278 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000279 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200280 * @buf: data buffer
281 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200282 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200283 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200284 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200285void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286{
Scott Wood17fed142016-05-30 13:57:56 -0500287 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200289
Heiko Schocherf5895d12014-06-24 10:10:04 +0200290 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200291}
292
293/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200294 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000295 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200296 * @buf: buffer to store date
297 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200298 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200299 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200300 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200301void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200302{
Scott Wood17fed142016-05-30 13:57:56 -0500303 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200304 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305
Heiko Schocherf5895d12014-06-24 10:10:04 +0200306 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307}
308
309/**
310 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000311 * @mtd: MTD device structure
312 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200313 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200314 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200315 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500316static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200317{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500318 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500319 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200320 u16 bad;
321
Sergey Lapin3a38a552013-01-14 03:46:50 +0000322 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200323 ofs += mtd->erasesize - mtd->writesize;
324
William Juul52c07962007-10-31 13:53:06 +0100325 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200326
Sergey Lapin3a38a552013-01-14 03:46:50 +0000327 do {
328 if (chip->options & NAND_BUSWIDTH_16) {
329 chip->cmdfunc(mtd, NAND_CMD_READOOB,
330 chip->badblockpos & 0xFE, page);
331 bad = cpu_to_le16(chip->read_word(mtd));
332 if (chip->badblockpos & 0x1)
333 bad >>= 8;
334 else
335 bad &= 0xFF;
336 } else {
337 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
338 page);
339 bad = chip->read_byte(mtd);
340 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200341
Sergey Lapin3a38a552013-01-14 03:46:50 +0000342 if (likely(chip->badblockbits == 8))
343 res = bad != 0xFF;
344 else
345 res = hweight8(bad) < chip->badblockbits;
346 ofs += mtd->writesize;
347 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
348 i++;
349 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200350
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351 return res;
352}
353
354/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200355 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000356 * @mtd: MTD device structure
357 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200358 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000359 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200360 * specific driver. It provides the details for writing a bad block marker to a
361 * block.
362 */
363static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
364{
Scott Wood17fed142016-05-30 13:57:56 -0500365 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200366 struct mtd_oob_ops ops;
367 uint8_t buf[2] = { 0, 0 };
368 int ret = 0, res, i = 0;
369
Scott Wood3ea94ed2015-06-26 19:03:26 -0500370 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200371 ops.oobbuf = buf;
372 ops.ooboffs = chip->badblockpos;
373 if (chip->options & NAND_BUSWIDTH_16) {
374 ops.ooboffs &= ~0x01;
375 ops.len = ops.ooblen = 2;
376 } else {
377 ops.len = ops.ooblen = 1;
378 }
379 ops.mode = MTD_OPS_PLACE_OOB;
380
381 /* Write to first/last page(s) if necessary */
382 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
383 ofs += mtd->erasesize - mtd->writesize;
384 do {
385 res = nand_do_write_oob(mtd, ofs, &ops);
386 if (!ret)
387 ret = res;
388
389 i++;
390 ofs += mtd->writesize;
391 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
392
393 return ret;
394}
395
396/**
397 * nand_block_markbad_lowlevel - mark a block bad
398 * @mtd: MTD device structure
399 * @ofs: offset from device start
400 *
401 * This function performs the generic NAND bad block marking steps (i.e., bad
402 * block table(s) and/or marker(s)). We only allow the hardware driver to
403 * specify how to write bad block markers to OOB (chip->block_markbad).
404 *
405 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000406 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200407 * (2) write bad block marker to OOB area of affected block (unless flag
408 * NAND_BBT_NO_OOB_BBM is present)
409 * (3) update the BBT
410 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000411 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200412*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200413static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200414{
Scott Wood17fed142016-05-30 13:57:56 -0500415 struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadrosb590f612022-12-20 12:21:57 +0200416 int ret = 0;
Simon Glass7ec24132024-09-29 19:49:48 -0600417#ifndef CONFIG_XPL_BUILD
Roger Quadrosb590f612022-12-20 12:21:57 +0200418 int res;
419#endif
Christian Hitzb8a6b372011-10-12 09:32:02 +0200420
Heiko Schocherf5895d12014-06-24 10:10:04 +0200421 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000422 struct erase_info einfo;
423
424 /* Attempt erase before marking OOB */
425 memset(&einfo, 0, sizeof(einfo));
426 einfo.mtd = mtd;
427 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200428 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000429 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200430
Heiko Schocherf5895d12014-06-24 10:10:04 +0200431 /* Write bad block marker to OOB */
432 nand_get_device(mtd, FL_WRITING);
433 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500434 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100435 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000436
Simon Glass7ec24132024-09-29 19:49:48 -0600437#ifndef CONFIG_XPL_BUILD
Heiko Schocherf5895d12014-06-24 10:10:04 +0200438 /* Mark block bad in BBT */
439 if (chip->bbt) {
440 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000441 if (!ret)
442 ret = res;
443 }
Roger Quadrosb590f612022-12-20 12:21:57 +0200444#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +0000445
William Juul52c07962007-10-31 13:53:06 +0100446 if (!ret)
447 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500448
William Juul52c07962007-10-31 13:53:06 +0100449 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200450}
451
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200452/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200453 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000454 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200455 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000456 * Check, if the device is write protected. The function expects, that the
457 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200458 */
William Juul52c07962007-10-31 13:53:06 +0100459static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200460{
Scott Wood17fed142016-05-30 13:57:56 -0500461 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100462 u8 status;
463 int ret;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200464
Sergey Lapin3a38a552013-01-14 03:46:50 +0000465 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200466 if (chip->options & NAND_BROKEN_XD)
467 return 0;
468
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200469 /* Check the WP bit */
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100470 ret = nand_status_op(chip, &status);
471 if (ret)
472 return ret;
473
474 return status & NAND_STATUS_WP ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200475}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100476
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200477/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500478 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000479 * @mtd: MTD device structure
480 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300481 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500482 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300483 */
484static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
485{
Scott Wood17fed142016-05-30 13:57:56 -0500486 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300487
488 if (!chip->bbt)
489 return 0;
490 /* Return info from the table */
Simon Glass7ec24132024-09-29 19:49:48 -0600491#ifndef CONFIG_XPL_BUILD
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300492 return nand_isreserved_bbt(mtd, ofs);
Roger Quadrosb590f612022-12-20 12:21:57 +0200493#else
494 return 0;
495#endif
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300496}
497
498/**
499 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
500 * @mtd: MTD device structure
501 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000502 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200503 *
504 * Check, if the block is bad. Either by reading the bad block table or
505 * calling of the scan function.
506 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500507static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200508{
Scott Wood17fed142016-05-30 13:57:56 -0500509 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200510
Masahiro Yamada8d100542014-12-26 22:20:58 +0900511 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
512 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200513 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900514 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200515 }
516
William Juul52c07962007-10-31 13:53:06 +0100517 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500518 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200519
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200520 /* Return info from the table */
Simon Glass7ec24132024-09-29 19:49:48 -0600521#ifndef CONFIG_XPL_BUILD
William Juul52c07962007-10-31 13:53:06 +0100522 return nand_isbad_bbt(mtd, ofs, allowbbt);
Roger Quadrosb590f612022-12-20 12:21:57 +0200523#else
524 return 0;
525#endif
William Juul52c07962007-10-31 13:53:06 +0100526}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200527
Scott Wood52ab7ce2016-05-30 13:57:58 -0500528/**
529 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
530 * @mtd: MTD device structure
531 *
532 * Wait for the ready pin after a command, and warn if a timeout occurs.
533 */
William Juul52c07962007-10-31 13:53:06 +0100534void nand_wait_ready(struct mtd_info *mtd)
535{
Scott Wood17fed142016-05-30 13:57:56 -0500536 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500537 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000538 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100539
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000540 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000541 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000542 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100543 if (chip->dev_ready)
544 if (chip->dev_ready(mtd))
545 break;
546 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500547
548 if (!chip->dev_ready(mtd))
549 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200550}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200551EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200552
553/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500554 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
555 * @mtd: MTD device structure
556 * @timeo: Timeout in ms
557 *
558 * Wait for status ready (i.e. command done) or timeout.
559 */
560static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
561{
Scott Wood17fed142016-05-30 13:57:56 -0500562 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500563 u32 time_start;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100564 int ret;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500565
566 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
567 time_start = get_timer(0);
568 while (get_timer(time_start) < timeo) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100569 u8 status;
570
571 ret = nand_read_data_op(chip, &status, sizeof(status), true);
572 if (ret)
573 return;
574
575 if (status & NAND_STATUS_READY)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500576 break;
Stefan Roese80877fa2022-09-02 14:10:46 +0200577 schedule();
Scott Wood3ea94ed2015-06-26 19:03:26 -0500578 }
579};
580
581/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200582 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000583 * @mtd: MTD device structure
584 * @command: the command to be sent
585 * @column: the column address for this command, -1 if none
586 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200587 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000588 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200589 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200590 */
William Juul52c07962007-10-31 13:53:06 +0100591static void nand_command(struct mtd_info *mtd, unsigned int command,
592 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200593{
Scott Wood17fed142016-05-30 13:57:56 -0500594 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100595 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200596
Sergey Lapin3a38a552013-01-14 03:46:50 +0000597 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200598 if (command == NAND_CMD_SEQIN) {
599 int readcmd;
600
William Juul52c07962007-10-31 13:53:06 +0100601 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200602 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100603 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200604 readcmd = NAND_CMD_READOOB;
605 } else if (column < 256) {
606 /* First 256 bytes --> READ0 */
607 readcmd = NAND_CMD_READ0;
608 } else {
609 column -= 256;
610 readcmd = NAND_CMD_READ1;
611 }
William Juul52c07962007-10-31 13:53:06 +0100612 chip->cmd_ctrl(mtd, readcmd, ctrl);
613 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200614 }
William Juul52c07962007-10-31 13:53:06 +0100615 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200616
Sergey Lapin3a38a552013-01-14 03:46:50 +0000617 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100618 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
619 /* Serially input address */
620 if (column != -1) {
621 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200622 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530623 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100624 column >>= 1;
625 chip->cmd_ctrl(mtd, column, ctrl);
626 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200627 }
William Juul52c07962007-10-31 13:53:06 +0100628 if (page_addr != -1) {
629 chip->cmd_ctrl(mtd, page_addr, ctrl);
630 ctrl &= ~NAND_CTRL_CHANGE;
631 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900632 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100633 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
634 }
635 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200636
637 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000638 * Program and erase have their own busy handlers status and sequential
639 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100640 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200641 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200642
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200643 case NAND_CMD_PAGEPROG:
644 case NAND_CMD_ERASE1:
645 case NAND_CMD_ERASE2:
646 case NAND_CMD_SEQIN:
647 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900648 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900649 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200650 return;
651
652 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100653 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200654 break;
William Juul52c07962007-10-31 13:53:06 +0100655 udelay(chip->chip_delay);
656 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
657 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
658 chip->cmd_ctrl(mtd,
659 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500660 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
661 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200662 return;
663
William Juul52c07962007-10-31 13:53:06 +0100664 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200665 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200666 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200667 * If we don't have access to the busy pin, we apply the given
668 * command delay
William Juul52c07962007-10-31 13:53:06 +0100669 */
670 if (!chip->dev_ready) {
671 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200672 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200673 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200674 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000675 /*
676 * Apply this short delay always to ensure that we do wait tWB in
677 * any case on any machine.
678 */
William Juul52c07962007-10-31 13:53:06 +0100679 ndelay(100);
680
681 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200682}
683
684/**
685 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000686 * @mtd: MTD device structure
687 * @command: the command to be sent
688 * @column: the column address for this command, -1 if none
689 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200690 *
William Juul52c07962007-10-31 13:53:06 +0100691 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000692 * devices. We don't have the separate regions as we have in the small page
693 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200694 */
William Juul52c07962007-10-31 13:53:06 +0100695static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
696 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200697{
Scott Wood17fed142016-05-30 13:57:56 -0500698 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200699
700 /* Emulate NAND_CMD_READOOB */
701 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100702 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200703 command = NAND_CMD_READ0;
704 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200705
William Juul52c07962007-10-31 13:53:06 +0100706 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200707 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200708
709 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100710 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200711
712 /* Serially input address */
713 if (column != -1) {
714 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200715 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530716 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200717 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100718 chip->cmd_ctrl(mtd, column, ctrl);
719 ctrl &= ~NAND_CTRL_CHANGE;
720 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200721 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200722 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100723 chip->cmd_ctrl(mtd, page_addr, ctrl);
724 chip->cmd_ctrl(mtd, page_addr >> 8,
725 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900726 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100727 chip->cmd_ctrl(mtd, page_addr >> 16,
728 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200729 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200730 }
William Juul52c07962007-10-31 13:53:06 +0100731 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200732
733 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000734 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500735 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100736 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200737 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200738
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200739 case NAND_CMD_CACHEDPROG:
740 case NAND_CMD_PAGEPROG:
741 case NAND_CMD_ERASE1:
742 case NAND_CMD_ERASE2:
743 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100744 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200745 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900746 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900747 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200748 return;
749
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100751 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200752 break;
William Juul52c07962007-10-31 13:53:06 +0100753 udelay(chip->chip_delay);
754 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
755 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
756 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
757 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500758 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
759 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100760 return;
761
762 case NAND_CMD_RNDOUT:
763 /* No ready / busy check necessary */
764 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
765 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
766 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
767 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200768 return;
769
770 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100771 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
772 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
773 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
774 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200775
William Juul52c07962007-10-31 13:53:06 +0100776 /* This applies to read commands */
Andre Przywaraa50e5c62025-03-27 15:33:10 +0000777 fallthrough;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200778 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200779 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200780 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000781 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100782 */
783 if (!chip->dev_ready) {
784 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200785 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200786 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200787 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200788
Sergey Lapin3a38a552013-01-14 03:46:50 +0000789 /*
790 * Apply this short delay always to ensure that we do wait tWB in
791 * any case on any machine.
792 */
William Juul52c07962007-10-31 13:53:06 +0100793 ndelay(100);
794
795 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200796}
797
798/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200799 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000800 * @chip: the nand chip descriptor
801 * @mtd: MTD device structure
802 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200803 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200804 * Used when in panic, no locks are taken.
805 */
806static void panic_nand_get_device(struct nand_chip *chip,
807 struct mtd_info *mtd, int new_state)
808{
809 /* Hardware controller shared among independent devices */
810 chip->controller->active = chip;
811 chip->state = new_state;
812}
813
814/**
815 * nand_get_device - [GENERIC] Get chip for selected access
816 * @mtd: MTD device structure
817 * @new_state: the state which is requested
818 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200819 * Get the device and lock it for exclusive access
820 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200821static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200822nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100823{
Scott Wood17fed142016-05-30 13:57:56 -0500824 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200825 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100826 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200827}
828
829/**
830 * panic_nand_wait - [GENERIC] wait until the command is done
831 * @mtd: MTD device structure
832 * @chip: NAND chip structure
833 * @timeo: timeout
834 *
835 * Wait for command done. This is a helper function for nand_wait used when
836 * we are in interrupt context. May happen when in panic and trying to write
837 * an oops through mtdoops.
838 */
839static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
840 unsigned long timeo)
841{
842 int i;
843 for (i = 0; i < timeo; i++) {
844 if (chip->dev_ready) {
845 if (chip->dev_ready(mtd))
846 break;
847 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100848 int ret;
849 u8 status;
850
851 ret = nand_read_data_op(chip, &status, sizeof(status),
852 true);
853 if (ret)
854 return;
855
856 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200857 break;
858 }
859 mdelay(1);
860 }
William Juul52c07962007-10-31 13:53:06 +0100861}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200862
863/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000864 * nand_wait - [DEFAULT] wait until the command is done
865 * @mtd: MTD device structure
866 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200867 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500868 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100869 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200870static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200871{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500872 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100873 u8 status;
874 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100875
Heiko Schocherf5895d12014-06-24 10:10:04 +0200876 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100877
Heiko Schocherf5895d12014-06-24 10:10:04 +0200878 /*
879 * Apply this short delay always to ensure that we do wait tWB in any
880 * case on any machine.
881 */
882 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100883
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100884 ret = nand_status_op(chip, NULL);
885 if (ret)
886 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100887
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200888 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
889 u32 time_start;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200890
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200891 time_start = get_timer(0);
892 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200893 if (chip->dev_ready) {
894 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100895 break;
896 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100897 ret = nand_read_data_op(chip, &status,
898 sizeof(status), true);
899 if (ret)
900 return ret;
901
902 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100903 break;
904 }
905 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200906 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100907
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100908 ret = nand_read_data_op(chip, &status, sizeof(status), true);
909 if (ret)
910 return ret;
911
Heiko Schocherf5895d12014-06-24 10:10:04 +0200912 /* This can happen if in case of timeout or buggy dev_ready */
913 WARN_ON(!(status & NAND_STATUS_READY));
914 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200915}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500916
Scott Wood52ab7ce2016-05-30 13:57:58 -0500917/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900918 * nand_reset_data_interface - Reset data interface and timings
919 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900920 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900921 *
922 * Reset the Data interface and timings to ONFI mode 0.
923 *
924 * Returns 0 for success or negative error code otherwise.
925 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900926static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900927{
928 struct mtd_info *mtd = nand_to_mtd(chip);
929 const struct nand_data_interface *conf;
930 int ret;
931
932 if (!chip->setup_data_interface)
933 return 0;
934
935 /*
936 * The ONFI specification says:
937 * "
938 * To transition from NV-DDR or NV-DDR2 to the SDR data
939 * interface, the host shall use the Reset (FFh) command
940 * using SDR timing mode 0. A device in any timing mode is
941 * required to recognize Reset (FFh) command issued in SDR
942 * timing mode 0.
943 * "
944 *
945 * Configure the data interface in SDR mode and set the
946 * timings to timing mode 0.
947 */
948
949 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900950 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900951 if (ret)
952 pr_err("Failed to configure data interface to SDR timing mode 0\n");
953
954 return ret;
955}
956
Kory Maincent0cda0cb2022-06-22 11:11:45 +0200957static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip)
958{
959 if (!chip->onfi_version ||
960 !(le16_to_cpu(chip->onfi_params.opt_cmd)
961 & ONFI_OPT_CMD_SET_GET_FEATURES))
962 return 0;
963
964 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
965 chip->onfi_timing_mode_default,
966 };
967
968 return chip->onfi_set_features(mtd, chip,
969 ONFI_FEATURE_ADDR_TIMING_MODE,
970 tmode_param);
971}
972
Boris Brezillone509cba2017-11-22 02:38:19 +0900973/**
974 * nand_setup_data_interface - Setup the best data interface and timings
975 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900976 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900977 *
978 * Find and configure the best data interface and NAND timings supported by
979 * the chip and the driver.
980 * First tries to retrieve supported timing modes from ONFI information,
981 * and if the NAND chip does not support ONFI, relies on the
982 * ->onfi_timing_mode_default specified in the nand_ids table.
983 *
984 * Returns 0 for success or negative error code otherwise.
985 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900986static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900987{
988 struct mtd_info *mtd = nand_to_mtd(chip);
989 int ret;
990
991 if (!chip->setup_data_interface || !chip->data_interface)
992 return 0;
993
994 /*
995 * Ensure the timing mode has been changed on the chip side
996 * before changing timings on the controller side.
997 */
Kory Maincent0cda0cb2022-06-22 11:11:45 +0200998 ret = nand_onfi_set_timings(mtd, chip);
999 if (ret)
1000 goto err;
Boris Brezillone509cba2017-11-22 02:38:19 +09001001
Boris Brezillon32935f42017-11-22 02:38:28 +09001002 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001003err:
1004 return ret;
1005}
1006
1007/**
1008 * nand_init_data_interface - find the best data interface and timings
1009 * @chip: The NAND chip
1010 *
1011 * Find the best data interface and NAND timings supported by the chip
1012 * and the driver.
1013 * First tries to retrieve supported timing modes from ONFI information,
1014 * and if the NAND chip does not support ONFI, relies on the
1015 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1016 * function nand_chip->data_interface is initialized with the best timing mode
1017 * available.
1018 *
1019 * Returns 0 for success or negative error code otherwise.
1020 */
1021static int nand_init_data_interface(struct nand_chip *chip)
1022{
1023 struct mtd_info *mtd = nand_to_mtd(chip);
1024 int modes, mode, ret;
1025
1026 if (!chip->setup_data_interface)
1027 return 0;
1028
1029 /*
1030 * First try to identify the best timings from ONFI parameters and
1031 * if the NAND does not support ONFI, fallback to the default ONFI
1032 * timing mode.
1033 */
1034 modes = onfi_get_async_timing_mode(chip);
1035 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1036 if (!chip->onfi_timing_mode_default)
1037 return 0;
1038
1039 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1040 }
1041
1042 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1043 GFP_KERNEL);
1044 if (!chip->data_interface)
1045 return -ENOMEM;
1046
1047 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1048 ret = onfi_init_data_interface(chip, chip->data_interface,
1049 NAND_SDR_IFACE, mode);
1050 if (ret)
1051 continue;
1052
Boris Brezillon32935f42017-11-22 02:38:28 +09001053 /* Pass -1 to only */
1054 ret = chip->setup_data_interface(mtd,
1055 NAND_DATA_IFACE_CHECK_ONLY,
1056 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001057 if (!ret) {
1058 chip->onfi_timing_mode_default = mode;
1059 break;
1060 }
1061 }
1062
1063 return 0;
1064}
1065
1066static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1067{
1068 kfree(chip->data_interface);
1069}
1070
1071/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001072 * nand_read_page_op - Do a READ PAGE operation
1073 * @chip: The NAND chip
1074 * @page: page to read
1075 * @offset_in_page: offset within the page
1076 * @buf: buffer used to store the data
1077 * @len: length of the buffer
1078 *
1079 * This function issues a READ PAGE operation.
1080 * This function does not select/unselect the CS line.
1081 *
1082 * Returns 0 on success, a negative error code otherwise.
1083 */
1084int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1085 unsigned int offset_in_page, void *buf, unsigned int len)
1086{
1087 struct mtd_info *mtd = nand_to_mtd(chip);
1088
1089 if (len && !buf)
1090 return -EINVAL;
1091
1092 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1093 return -EINVAL;
1094
1095 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1096 if (len)
1097 chip->read_buf(mtd, buf, len);
1098
1099 return 0;
1100}
1101EXPORT_SYMBOL_GPL(nand_read_page_op);
1102
1103/**
1104 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1105 * @chip: The NAND chip
1106 * @page: parameter page to read
1107 * @buf: buffer used to store the data
1108 * @len: length of the buffer
1109 *
1110 * This function issues a READ PARAMETER PAGE operation.
1111 * This function does not select/unselect the CS line.
1112 *
1113 * Returns 0 on success, a negative error code otherwise.
1114 */
1115static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1116 unsigned int len)
1117{
1118 struct mtd_info *mtd = nand_to_mtd(chip);
1119 unsigned int i;
1120 u8 *p = buf;
1121
1122 if (len && !buf)
1123 return -EINVAL;
1124
1125 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1126 for (i = 0; i < len; i++)
1127 p[i] = chip->read_byte(mtd);
1128
1129 return 0;
1130}
1131
1132/**
1133 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1134 * @chip: The NAND chip
1135 * @offset_in_page: offset within the page
1136 * @buf: buffer used to store the data
1137 * @len: length of the buffer
1138 * @force_8bit: force 8-bit bus access
1139 *
1140 * This function issues a CHANGE READ COLUMN operation.
1141 * This function does not select/unselect the CS line.
1142 *
1143 * Returns 0 on success, a negative error code otherwise.
1144 */
1145int nand_change_read_column_op(struct nand_chip *chip,
1146 unsigned int offset_in_page, void *buf,
1147 unsigned int len, bool force_8bit)
1148{
1149 struct mtd_info *mtd = nand_to_mtd(chip);
1150
1151 if (len && !buf)
1152 return -EINVAL;
1153
1154 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1155 return -EINVAL;
1156
1157 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1158 if (len)
1159 chip->read_buf(mtd, buf, len);
1160
1161 return 0;
1162}
1163EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1164
1165/**
1166 * nand_read_oob_op - Do a READ OOB operation
1167 * @chip: The NAND chip
1168 * @page: page to read
1169 * @offset_in_oob: offset within the OOB area
1170 * @buf: buffer used to store the data
1171 * @len: length of the buffer
1172 *
1173 * This function issues a READ OOB operation.
1174 * This function does not select/unselect the CS line.
1175 *
1176 * Returns 0 on success, a negative error code otherwise.
1177 */
1178int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1179 unsigned int offset_in_oob, void *buf, unsigned int len)
1180{
1181 struct mtd_info *mtd = nand_to_mtd(chip);
1182
1183 if (len && !buf)
1184 return -EINVAL;
1185
1186 if (offset_in_oob + len > mtd->oobsize)
1187 return -EINVAL;
1188
1189 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1190 if (len)
1191 chip->read_buf(mtd, buf, len);
1192
1193 return 0;
1194}
1195EXPORT_SYMBOL_GPL(nand_read_oob_op);
1196
1197/**
1198 * nand_prog_page_begin_op - starts a PROG PAGE operation
1199 * @chip: The NAND chip
1200 * @page: page to write
1201 * @offset_in_page: offset within the page
1202 * @buf: buffer containing the data to write to the page
1203 * @len: length of the buffer
1204 *
1205 * This function issues the first half of a PROG PAGE operation.
1206 * This function does not select/unselect the CS line.
1207 *
1208 * Returns 0 on success, a negative error code otherwise.
1209 */
1210int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1211 unsigned int offset_in_page, const void *buf,
1212 unsigned int len)
1213{
1214 struct mtd_info *mtd = nand_to_mtd(chip);
1215
1216 if (len && !buf)
1217 return -EINVAL;
1218
1219 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1220 return -EINVAL;
1221
1222 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1223
1224 if (buf)
1225 chip->write_buf(mtd, buf, len);
1226
1227 return 0;
1228}
1229EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1230
1231/**
1232 * nand_prog_page_end_op - ends a PROG PAGE operation
1233 * @chip: The NAND chip
1234 *
1235 * This function issues the second half of a PROG PAGE operation.
1236 * This function does not select/unselect the CS line.
1237 *
1238 * Returns 0 on success, a negative error code otherwise.
1239 */
1240int nand_prog_page_end_op(struct nand_chip *chip)
1241{
1242 struct mtd_info *mtd = nand_to_mtd(chip);
1243 int status;
1244
1245 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1246
1247 status = chip->waitfunc(mtd, chip);
1248 if (status & NAND_STATUS_FAIL)
1249 return -EIO;
1250
1251 return 0;
1252}
1253EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1254
1255/**
1256 * nand_prog_page_op - Do a full PROG PAGE operation
1257 * @chip: The NAND chip
1258 * @page: page to write
1259 * @offset_in_page: offset within the page
1260 * @buf: buffer containing the data to write to the page
1261 * @len: length of the buffer
1262 *
1263 * This function issues a full PROG PAGE operation.
1264 * This function does not select/unselect the CS line.
1265 *
1266 * Returns 0 on success, a negative error code otherwise.
1267 */
1268int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1269 unsigned int offset_in_page, const void *buf,
1270 unsigned int len)
1271{
1272 struct mtd_info *mtd = nand_to_mtd(chip);
1273 int status;
1274
1275 if (!len || !buf)
1276 return -EINVAL;
1277
1278 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1279 return -EINVAL;
1280
1281 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1282 chip->write_buf(mtd, buf, len);
1283 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1284
1285 status = chip->waitfunc(mtd, chip);
1286 if (status & NAND_STATUS_FAIL)
1287 return -EIO;
1288
1289 return 0;
1290}
1291EXPORT_SYMBOL_GPL(nand_prog_page_op);
1292
1293/**
1294 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1295 * @chip: The NAND chip
1296 * @offset_in_page: offset within the page
1297 * @buf: buffer containing the data to send to the NAND
1298 * @len: length of the buffer
1299 * @force_8bit: force 8-bit bus access
1300 *
1301 * This function issues a CHANGE WRITE COLUMN operation.
1302 * This function does not select/unselect the CS line.
1303 *
1304 * Returns 0 on success, a negative error code otherwise.
1305 */
1306int nand_change_write_column_op(struct nand_chip *chip,
1307 unsigned int offset_in_page,
1308 const void *buf, unsigned int len,
1309 bool force_8bit)
1310{
1311 struct mtd_info *mtd = nand_to_mtd(chip);
1312
1313 if (len && !buf)
1314 return -EINVAL;
1315
1316 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1317 return -EINVAL;
1318
1319 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1320 if (len)
1321 chip->write_buf(mtd, buf, len);
1322
1323 return 0;
1324}
1325EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1326
1327/**
1328 * nand_readid_op - Do a READID operation
1329 * @chip: The NAND chip
1330 * @addr: address cycle to pass after the READID command
1331 * @buf: buffer used to store the ID
1332 * @len: length of the buffer
1333 *
1334 * This function sends a READID command and reads back the ID returned by the
1335 * NAND.
1336 * This function does not select/unselect the CS line.
1337 *
1338 * Returns 0 on success, a negative error code otherwise.
1339 */
1340int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1341 unsigned int len)
1342{
1343 struct mtd_info *mtd = nand_to_mtd(chip);
1344 unsigned int i;
1345 u8 *id = buf;
1346
1347 if (len && !buf)
1348 return -EINVAL;
1349
1350 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1351
1352 for (i = 0; i < len; i++)
1353 id[i] = chip->read_byte(mtd);
1354
1355 return 0;
1356}
1357EXPORT_SYMBOL_GPL(nand_readid_op);
1358
1359/**
1360 * nand_status_op - Do a STATUS operation
1361 * @chip: The NAND chip
1362 * @status: out variable to store the NAND status
1363 *
1364 * This function sends a STATUS command and reads back the status returned by
1365 * the NAND.
1366 * This function does not select/unselect the CS line.
1367 *
1368 * Returns 0 on success, a negative error code otherwise.
1369 */
1370int nand_status_op(struct nand_chip *chip, u8 *status)
1371{
1372 struct mtd_info *mtd = nand_to_mtd(chip);
1373
1374 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1375 if (status)
1376 *status = chip->read_byte(mtd);
1377
1378 return 0;
1379}
1380EXPORT_SYMBOL_GPL(nand_status_op);
1381
1382/**
1383 * nand_exit_status_op - Exit a STATUS operation
1384 * @chip: The NAND chip
1385 *
1386 * This function sends a READ0 command to cancel the effect of the STATUS
1387 * command to avoid reading only the status until a new read command is sent.
1388 *
1389 * This function does not select/unselect the CS line.
1390 *
1391 * Returns 0 on success, a negative error code otherwise.
1392 */
1393int nand_exit_status_op(struct nand_chip *chip)
1394{
1395 struct mtd_info *mtd = nand_to_mtd(chip);
1396
1397 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1398
1399 return 0;
1400}
1401EXPORT_SYMBOL_GPL(nand_exit_status_op);
1402
1403/**
1404 * nand_erase_op - Do an erase operation
1405 * @chip: The NAND chip
1406 * @eraseblock: block to erase
1407 *
1408 * This function sends an ERASE command and waits for the NAND to be ready
1409 * before returning.
1410 * This function does not select/unselect the CS line.
1411 *
1412 * Returns 0 on success, a negative error code otherwise.
1413 */
1414int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1415{
1416 struct mtd_info *mtd = nand_to_mtd(chip);
1417 unsigned int page = eraseblock <<
1418 (chip->phys_erase_shift - chip->page_shift);
1419 int status;
1420
1421 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1422 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1423
1424 status = chip->waitfunc(mtd, chip);
1425 if (status < 0)
1426 return status;
1427
1428 if (status & NAND_STATUS_FAIL)
1429 return -EIO;
1430
1431 return 0;
1432}
1433EXPORT_SYMBOL_GPL(nand_erase_op);
1434
1435/**
1436 * nand_set_features_op - Do a SET FEATURES operation
1437 * @chip: The NAND chip
1438 * @feature: feature id
1439 * @data: 4 bytes of data
1440 *
1441 * This function sends a SET FEATURES command and waits for the NAND to be
1442 * ready before returning.
1443 * This function does not select/unselect the CS line.
1444 *
1445 * Returns 0 on success, a negative error code otherwise.
1446 */
1447static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1448 const void *data)
1449{
1450 struct mtd_info *mtd = nand_to_mtd(chip);
1451 const u8 *params = data;
1452 int i, status;
1453
1454 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1455 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1456 chip->write_byte(mtd, params[i]);
1457
1458 status = chip->waitfunc(mtd, chip);
1459 if (status & NAND_STATUS_FAIL)
1460 return -EIO;
1461
1462 return 0;
1463}
1464
1465/**
1466 * nand_get_features_op - Do a GET FEATURES operation
1467 * @chip: The NAND chip
1468 * @feature: feature id
1469 * @data: 4 bytes of data
1470 *
1471 * This function sends a GET FEATURES command and waits for the NAND to be
1472 * ready before returning.
1473 * This function does not select/unselect the CS line.
1474 *
1475 * Returns 0 on success, a negative error code otherwise.
1476 */
1477static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1478 void *data)
1479{
1480 struct mtd_info *mtd = nand_to_mtd(chip);
1481 u8 *params = data;
1482 int i;
1483
1484 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1485 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1486 params[i] = chip->read_byte(mtd);
1487
1488 return 0;
1489}
1490
1491/**
1492 * nand_reset_op - Do a reset operation
1493 * @chip: The NAND chip
1494 *
1495 * This function sends a RESET command and waits for the NAND to be ready
1496 * before returning.
1497 * This function does not select/unselect the CS line.
1498 *
1499 * Returns 0 on success, a negative error code otherwise.
1500 */
1501int nand_reset_op(struct nand_chip *chip)
1502{
1503 struct mtd_info *mtd = nand_to_mtd(chip);
1504
1505 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1506
1507 return 0;
1508}
1509EXPORT_SYMBOL_GPL(nand_reset_op);
1510
1511/**
1512 * nand_read_data_op - Read data from the NAND
1513 * @chip: The NAND chip
1514 * @buf: buffer used to store the data
1515 * @len: length of the buffer
1516 * @force_8bit: force 8-bit bus access
1517 *
1518 * This function does a raw data read on the bus. Usually used after launching
1519 * another NAND operation like nand_read_page_op().
1520 * This function does not select/unselect the CS line.
1521 *
1522 * Returns 0 on success, a negative error code otherwise.
1523 */
1524int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1525 bool force_8bit)
1526{
1527 struct mtd_info *mtd = nand_to_mtd(chip);
1528
1529 if (!len || !buf)
1530 return -EINVAL;
1531
1532 if (force_8bit) {
1533 u8 *p = buf;
1534 unsigned int i;
1535
1536 for (i = 0; i < len; i++)
1537 p[i] = chip->read_byte(mtd);
1538 } else {
1539 chip->read_buf(mtd, buf, len);
1540 }
1541
1542 return 0;
1543}
1544EXPORT_SYMBOL_GPL(nand_read_data_op);
1545
1546/**
1547 * nand_write_data_op - Write data from the NAND
1548 * @chip: The NAND chip
1549 * @buf: buffer containing the data to send on the bus
1550 * @len: length of the buffer
1551 * @force_8bit: force 8-bit bus access
1552 *
1553 * This function does a raw data write on the bus. Usually used after launching
1554 * another NAND operation like nand_write_page_begin_op().
1555 * This function does not select/unselect the CS line.
1556 *
1557 * Returns 0 on success, a negative error code otherwise.
1558 */
1559int nand_write_data_op(struct nand_chip *chip, const void *buf,
1560 unsigned int len, bool force_8bit)
1561{
1562 struct mtd_info *mtd = nand_to_mtd(chip);
1563
1564 if (!len || !buf)
1565 return -EINVAL;
1566
1567 if (force_8bit) {
1568 const u8 *p = buf;
1569 unsigned int i;
1570
1571 for (i = 0; i < len; i++)
1572 chip->write_byte(mtd, p[i]);
1573 } else {
1574 chip->write_buf(mtd, buf, len);
1575 }
1576
1577 return 0;
1578}
1579EXPORT_SYMBOL_GPL(nand_write_data_op);
1580
1581/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001582 * nand_reset - Reset and initialize a NAND device
1583 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001584 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001585 *
1586 * Returns 0 for success or negative error code otherwise
1587 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001588int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001589{
1590 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001591 int ret;
1592
Boris Brezillon32935f42017-11-22 02:38:28 +09001593 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001594 if (ret)
1595 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001596
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001597 /*
1598 * The CS line has to be released before we can apply the new NAND
1599 * interface settings, hence this weird ->select_chip() dance.
1600 */
1601 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001602 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001603 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001604 if (ret)
1605 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001606
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001607 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001608 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001609 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001610 if (ret)
1611 return ret;
1612
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001613 return 0;
1614}
1615
1616/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001617 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1618 * @buf: buffer to test
1619 * @len: buffer length
1620 * @bitflips_threshold: maximum number of bitflips
1621 *
1622 * Check if a buffer contains only 0xff, which means the underlying region
1623 * has been erased and is ready to be programmed.
1624 * The bitflips_threshold specify the maximum number of bitflips before
1625 * considering the region is not erased.
1626 * Note: The logic of this function has been extracted from the memweight
1627 * implementation, except that nand_check_erased_buf function exit before
1628 * testing the whole buffer if the number of bitflips exceed the
1629 * bitflips_threshold value.
1630 *
1631 * Returns a positive number of bitflips less than or equal to
1632 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1633 * threshold.
1634 */
1635static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1636{
1637 const unsigned char *bitmap = buf;
1638 int bitflips = 0;
1639 int weight;
1640
1641 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1642 len--, bitmap++) {
1643 weight = hweight8(*bitmap);
1644 bitflips += BITS_PER_BYTE - weight;
1645 if (unlikely(bitflips > bitflips_threshold))
1646 return -EBADMSG;
1647 }
1648
1649 for (; len >= 4; len -= 4, bitmap += 4) {
1650 weight = hweight32(*((u32 *)bitmap));
1651 bitflips += 32 - weight;
1652 if (unlikely(bitflips > bitflips_threshold))
1653 return -EBADMSG;
1654 }
1655
1656 for (; len > 0; len--, bitmap++) {
1657 weight = hweight8(*bitmap);
1658 bitflips += BITS_PER_BYTE - weight;
1659 if (unlikely(bitflips > bitflips_threshold))
1660 return -EBADMSG;
1661 }
1662
1663 return bitflips;
1664}
1665
1666/**
1667 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1668 * 0xff data
1669 * @data: data buffer to test
1670 * @datalen: data length
1671 * @ecc: ECC buffer
1672 * @ecclen: ECC length
1673 * @extraoob: extra OOB buffer
1674 * @extraooblen: extra OOB length
1675 * @bitflips_threshold: maximum number of bitflips
1676 *
1677 * Check if a data buffer and its associated ECC and OOB data contains only
1678 * 0xff pattern, which means the underlying region has been erased and is
1679 * ready to be programmed.
1680 * The bitflips_threshold specify the maximum number of bitflips before
1681 * considering the region as not erased.
1682 *
1683 * Note:
1684 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1685 * different from the NAND page size. When fixing bitflips, ECC engines will
1686 * report the number of errors per chunk, and the NAND core infrastructure
1687 * expect you to return the maximum number of bitflips for the whole page.
1688 * This is why you should always use this function on a single chunk and
1689 * not on the whole page. After checking each chunk you should update your
1690 * max_bitflips value accordingly.
1691 * 2/ When checking for bitflips in erased pages you should not only check
1692 * the payload data but also their associated ECC data, because a user might
1693 * have programmed almost all bits to 1 but a few. In this case, we
1694 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1695 * this case.
1696 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1697 * data are protected by the ECC engine.
1698 * It could also be used if you support subpages and want to attach some
1699 * extra OOB data to an ECC chunk.
1700 *
1701 * Returns a positive number of bitflips less than or equal to
1702 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1703 * threshold. In case of success, the passed buffers are filled with 0xff.
1704 */
1705int nand_check_erased_ecc_chunk(void *data, int datalen,
1706 void *ecc, int ecclen,
1707 void *extraoob, int extraooblen,
1708 int bitflips_threshold)
1709{
1710 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1711
1712 data_bitflips = nand_check_erased_buf(data, datalen,
1713 bitflips_threshold);
1714 if (data_bitflips < 0)
1715 return data_bitflips;
1716
1717 bitflips_threshold -= data_bitflips;
1718
1719 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1720 if (ecc_bitflips < 0)
1721 return ecc_bitflips;
1722
1723 bitflips_threshold -= ecc_bitflips;
1724
1725 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1726 bitflips_threshold);
1727 if (extraoob_bitflips < 0)
1728 return extraoob_bitflips;
1729
1730 if (data_bitflips)
1731 memset(data, 0xff, datalen);
1732
1733 if (ecc_bitflips)
1734 memset(ecc, 0xff, ecclen);
1735
1736 if (extraoob_bitflips)
1737 memset(extraoob, 0xff, extraooblen);
1738
1739 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1740}
1741EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001742
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001743/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001744 * nand_read_page_raw - [INTERN] read raw page data without ecc
1745 * @mtd: mtd info structure
1746 * @chip: nand chip info structure
1747 * @buf: buffer to store read data
1748 * @oob_required: caller requires OOB data read to chip->oob_poi
1749 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001750 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001751 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001752 */
William Juul52c07962007-10-31 13:53:06 +01001753static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001754 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001755{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001756 int ret;
1757
1758 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1759 if (ret)
1760 return ret;
1761
1762 if (oob_required) {
1763 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1764 false);
1765 if (ret)
1766 return ret;
1767 }
1768
William Juul52c07962007-10-31 13:53:06 +01001769 return 0;
1770}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001771
William Juul52c07962007-10-31 13:53:06 +01001772/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001773 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1774 * @mtd: mtd info structure
1775 * @chip: nand chip info structure
1776 * @buf: buffer to store read data
1777 * @oob_required: caller requires OOB data read to chip->oob_poi
1778 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001779 *
1780 * We need a special oob layout and handling even when OOB isn't used.
1781 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001782static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001783 struct nand_chip *chip, uint8_t *buf,
1784 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001785{
1786 int eccsize = chip->ecc.size;
1787 int eccbytes = chip->ecc.bytes;
1788 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001789 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001790
1791 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001792 ret = nand_read_data_op(chip, buf, eccsize, false);
1793 if (ret)
1794 return ret;
1795
David Brownellee86b8d2009-11-07 16:27:01 -05001796 buf += eccsize;
1797
1798 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001799 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1800 false);
1801 if (ret)
1802 return ret;
1803
David Brownellee86b8d2009-11-07 16:27:01 -05001804 oob += chip->ecc.prepad;
1805 }
1806
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001807 ret = nand_read_data_op(chip, oob, eccbytes, false);
1808 if (ret)
1809 return ret;
1810
David Brownellee86b8d2009-11-07 16:27:01 -05001811 oob += eccbytes;
1812
1813 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001814 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1815 false);
1816 if (ret)
1817 return ret;
1818
David Brownellee86b8d2009-11-07 16:27:01 -05001819 oob += chip->ecc.postpad;
1820 }
1821 }
1822
1823 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001824 if (size) {
1825 ret = nand_read_data_op(chip, oob, size, false);
1826 if (ret)
1827 return ret;
1828 }
David Brownellee86b8d2009-11-07 16:27:01 -05001829
1830 return 0;
1831}
1832
1833/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001834 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1835 * @mtd: mtd info structure
1836 * @chip: nand chip info structure
1837 * @buf: buffer to store read data
1838 * @oob_required: caller requires OOB data read to chip->oob_poi
1839 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001840 */
1841static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001842 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001843{
1844 int i, eccsize = chip->ecc.size;
1845 int eccbytes = chip->ecc.bytes;
1846 int eccsteps = chip->ecc.steps;
1847 uint8_t *p = buf;
1848 uint8_t *ecc_calc = chip->buffers->ecccalc;
1849 uint8_t *ecc_code = chip->buffers->ecccode;
1850 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001851 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001852
Sergey Lapin3a38a552013-01-14 03:46:50 +00001853 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001854
William Juul52c07962007-10-31 13:53:06 +01001855 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1856 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001857
William Juul52c07962007-10-31 13:53:06 +01001858 for (i = 0; i < chip->ecc.total; i++)
1859 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001860
William Juul52c07962007-10-31 13:53:06 +01001861 eccsteps = chip->ecc.steps;
1862 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001863
William Juul52c07962007-10-31 13:53:06 +01001864 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1865 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001866
William Juul52c07962007-10-31 13:53:06 +01001867 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001868 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001869 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001870 } else {
William Juul52c07962007-10-31 13:53:06 +01001871 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001872 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1873 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001874 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001875 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001876}
1877
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001878/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001879 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001880 * @mtd: mtd info structure
1881 * @chip: nand chip info structure
1882 * @data_offs: offset of requested data within the page
1883 * @readlen: data length
1884 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001885 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001886 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001887static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001888 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1889 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001890{
1891 int start_step, end_step, num_steps;
1892 uint32_t *eccpos = chip->ecc.layout->eccpos;
1893 uint8_t *p;
1894 int data_col_addr, i, gaps = 0;
1895 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1896 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001897 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001898 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001899 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001900
Sergey Lapin3a38a552013-01-14 03:46:50 +00001901 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001902 start_step = data_offs / chip->ecc.size;
1903 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1904 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001905 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001906
Sergey Lapin3a38a552013-01-14 03:46:50 +00001907 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001908 datafrag_len = num_steps * chip->ecc.size;
1909 eccfrag_len = num_steps * chip->ecc.bytes;
1910
1911 data_col_addr = start_step * chip->ecc.size;
1912 /* If we read not a page aligned data */
1913 if (data_col_addr != 0)
1914 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1915
1916 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001917 ret = nand_read_data_op(chip, p, datafrag_len, false);
1918 if (ret)
1919 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001920
Sergey Lapin3a38a552013-01-14 03:46:50 +00001921 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001922 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1923 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1924
Sergey Lapin3a38a552013-01-14 03:46:50 +00001925 /*
1926 * The performance is faster if we position offsets according to
1927 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1928 */
Scott Wood3628f002008-10-24 16:20:43 -05001929 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001930 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001931 gaps = 1;
1932 break;
1933 }
1934 }
1935 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001936 ret = nand_change_read_column_op(chip, mtd->writesize,
1937 chip->oob_poi, mtd->oobsize,
1938 false);
1939 if (ret)
1940 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001941 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001942 /*
1943 * Send the command to read the particular ECC bytes take care
1944 * about buswidth alignment in read_buf.
1945 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001946 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001947 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001948 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001949 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001950 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001951 aligned_len++;
1952
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001953 ret = nand_change_read_column_op(chip,
1954 mtd->writesize + aligned_pos,
1955 &chip->oob_poi[aligned_pos],
1956 aligned_len, false);
1957 if (ret)
1958 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001959 }
1960
1961 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001962 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001963
1964 p = bufpoi + data_col_addr;
1965 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1966 int stat;
1967
Christian Hitzb8a6b372011-10-12 09:32:02 +02001968 stat = chip->ecc.correct(mtd, p,
1969 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001970 if (stat == -EBADMSG &&
1971 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1972 /* check for empty pages with bitflips */
1973 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1974 &chip->buffers->ecccode[i],
1975 chip->ecc.bytes,
1976 NULL, 0,
1977 chip->ecc.strength);
1978 }
1979
Heiko Schocherf5895d12014-06-24 10:10:04 +02001980 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001981 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001982 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001983 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001984 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1985 }
Scott Wood3628f002008-10-24 16:20:43 -05001986 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001987 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05001988}
1989
1990/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001991 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1992 * @mtd: mtd info structure
1993 * @chip: nand chip info structure
1994 * @buf: buffer to store read data
1995 * @oob_required: caller requires OOB data read to chip->oob_poi
1996 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001997 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001998 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001999 */
William Juul52c07962007-10-31 13:53:06 +01002000static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002001 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002002{
William Juul52c07962007-10-31 13:53:06 +01002003 int i, eccsize = chip->ecc.size;
2004 int eccbytes = chip->ecc.bytes;
2005 int eccsteps = chip->ecc.steps;
2006 uint8_t *p = buf;
2007 uint8_t *ecc_calc = chip->buffers->ecccalc;
2008 uint8_t *ecc_code = chip->buffers->ecccode;
2009 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002010 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002011 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002012
William Juul52c07962007-10-31 13:53:06 +01002013 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2014 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002015
2016 ret = nand_read_data_op(chip, p, eccsize, false);
2017 if (ret)
2018 return ret;
2019
William Juul52c07962007-10-31 13:53:06 +01002020 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2021 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002022
2023 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2024 if (ret)
2025 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002026
William Juul52c07962007-10-31 13:53:06 +01002027 for (i = 0; i < chip->ecc.total; i++)
2028 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002029
William Juul52c07962007-10-31 13:53:06 +01002030 eccsteps = chip->ecc.steps;
2031 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002032
William Juul52c07962007-10-31 13:53:06 +01002033 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2034 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002035
William Juul52c07962007-10-31 13:53:06 +01002036 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002037 if (stat == -EBADMSG &&
2038 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2039 /* check for empty pages with bitflips */
2040 stat = nand_check_erased_ecc_chunk(p, eccsize,
2041 &ecc_code[i], eccbytes,
2042 NULL, 0,
2043 chip->ecc.strength);
2044 }
2045
Heiko Schocherf5895d12014-06-24 10:10:04 +02002046 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002047 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002048 } else {
William Juul52c07962007-10-31 13:53:06 +01002049 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002050 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2051 }
William Juul52c07962007-10-31 13:53:06 +01002052 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002053 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002054}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002055
William Juul52c07962007-10-31 13:53:06 +01002056/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002057 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2058 * @mtd: mtd info structure
2059 * @chip: nand chip info structure
2060 * @buf: buffer to store read data
2061 * @oob_required: caller requires OOB data read to chip->oob_poi
2062 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002063 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002064 * Hardware ECC for large page chips, require OOB to be read first. For this
2065 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2066 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2067 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2068 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002069 */
2070static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002071 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002072{
2073 int i, eccsize = chip->ecc.size;
2074 int eccbytes = chip->ecc.bytes;
2075 int eccsteps = chip->ecc.steps;
2076 uint8_t *p = buf;
2077 uint8_t *ecc_code = chip->buffers->ecccode;
2078 uint32_t *eccpos = chip->ecc.layout->eccpos;
2079 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002080 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002081 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002082
2083 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002084 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2085 if (ret)
2086 return ret;
2087
2088 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2089 if (ret)
2090 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002091
2092 for (i = 0; i < chip->ecc.total; i++)
2093 ecc_code[i] = chip->oob_poi[eccpos[i]];
2094
2095 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2096 int stat;
2097
2098 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002099
2100 ret = nand_read_data_op(chip, p, eccsize, false);
2101 if (ret)
2102 return ret;
2103
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002104 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2105
2106 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002107 if (stat == -EBADMSG &&
2108 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2109 /* check for empty pages with bitflips */
2110 stat = nand_check_erased_ecc_chunk(p, eccsize,
2111 &ecc_code[i], eccbytes,
2112 NULL, 0,
2113 chip->ecc.strength);
2114 }
2115
Heiko Schocherf5895d12014-06-24 10:10:04 +02002116 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002117 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002118 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002119 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002120 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2121 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002122 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002123 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002124}
2125
2126/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002127 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2128 * @mtd: mtd info structure
2129 * @chip: nand chip info structure
2130 * @buf: buffer to store read data
2131 * @oob_required: caller requires OOB data read to chip->oob_poi
2132 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002133 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002134 * The hw generator calculates the error syndrome automatically. Therefore we
2135 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002136 */
2137static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002138 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002139{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002140 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002141 int eccbytes = chip->ecc.bytes;
2142 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002143 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002144 uint8_t *p = buf;
2145 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002146 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002147
2148 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2149 int stat;
2150
2151 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002152
2153 ret = nand_read_data_op(chip, p, eccsize, false);
2154 if (ret)
2155 return ret;
William Juul52c07962007-10-31 13:53:06 +01002156
2157 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002158 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2159 false);
2160 if (ret)
2161 return ret;
2162
William Juul52c07962007-10-31 13:53:06 +01002163 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002164 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002165
William Juul52c07962007-10-31 13:53:06 +01002166 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002167
2168 ret = nand_read_data_op(chip, oob, eccbytes, false);
2169 if (ret)
2170 return ret;
2171
William Juul52c07962007-10-31 13:53:06 +01002172 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002173
William Juul52c07962007-10-31 13:53:06 +01002174 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002175
William Juul52c07962007-10-31 13:53:06 +01002176 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002177 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2178 false);
2179 if (ret)
2180 return ret;
2181
William Juul52c07962007-10-31 13:53:06 +01002182 oob += chip->ecc.postpad;
2183 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002184
2185 if (stat == -EBADMSG &&
2186 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2187 /* check for empty pages with bitflips */
2188 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2189 oob - eccpadbytes,
2190 eccpadbytes,
2191 NULL, 0,
2192 chip->ecc.strength);
2193 }
2194
2195 if (stat < 0) {
2196 mtd->ecc_stats.failed++;
2197 } else {
2198 mtd->ecc_stats.corrected += stat;
2199 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2200 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002201 }
William Juul52c07962007-10-31 13:53:06 +01002202
2203 /* Calculate remaining oob bytes */
2204 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002205 if (i) {
2206 ret = nand_read_data_op(chip, oob, i, false);
2207 if (ret)
2208 return ret;
2209 }
William Juul52c07962007-10-31 13:53:06 +01002210
Heiko Schocherf5895d12014-06-24 10:10:04 +02002211 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002212}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002213
2214/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002215 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2216 * @chip: nand chip structure
2217 * @oob: oob destination address
2218 * @ops: oob ops structure
2219 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002220 */
2221static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2222 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002223{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002224 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002225
Sergey Lapin3a38a552013-01-14 03:46:50 +00002226 case MTD_OPS_PLACE_OOB:
2227 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002228 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2229 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002230
Sergey Lapin3a38a552013-01-14 03:46:50 +00002231 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002232 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2233 uint32_t boffs = 0, roffs = ops->ooboffs;
2234 size_t bytes = 0;
2235
Christian Hitz13fc0e22011-10-12 09:32:01 +02002236 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002237 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002238 if (unlikely(roffs)) {
2239 if (roffs >= free->length) {
2240 roffs -= free->length;
2241 continue;
2242 }
2243 boffs = free->offset + roffs;
2244 bytes = min_t(size_t, len,
2245 (free->length - roffs));
2246 roffs = 0;
2247 } else {
2248 bytes = min_t(size_t, len, free->length);
2249 boffs = free->offset;
2250 }
2251 memcpy(oob, chip->oob_poi + boffs, bytes);
2252 oob += bytes;
2253 }
2254 return oob;
2255 }
2256 default:
2257 BUG();
2258 }
2259 return NULL;
2260}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002261
2262/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002263 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2264 * @mtd: MTD device structure
2265 * @retry_mode: the retry mode to use
2266 *
2267 * Some vendors supply a special command to shift the Vt threshold, to be used
2268 * when there are too many bitflips in a page (i.e., ECC error). After setting
2269 * a new threshold, the host should retry reading the page.
2270 */
2271static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2272{
Scott Wood17fed142016-05-30 13:57:56 -05002273 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002274
2275 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2276
2277 if (retry_mode >= chip->read_retries)
2278 return -EINVAL;
2279
2280 if (!chip->setup_read_retry)
2281 return -EOPNOTSUPP;
2282
2283 return chip->setup_read_retry(mtd, retry_mode);
2284}
2285
2286/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002287 * nand_do_read_ops - [INTERN] Read data with ECC
2288 * @mtd: MTD device structure
2289 * @from: offset to read from
2290 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002291 *
William Juul52c07962007-10-31 13:53:06 +01002292 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002293 */
William Juul52c07962007-10-31 13:53:06 +01002294static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2295 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002296{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002297 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002298 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002299 int ret = 0;
2300 uint32_t readlen = ops->len;
2301 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002302 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002303
William Juul52c07962007-10-31 13:53:06 +01002304 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002305 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002306 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002307 int retry_mode = 0;
2308 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002309
William Juul52c07962007-10-31 13:53:06 +01002310 chipnr = (int)(from >> chip->chip_shift);
2311 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002312
William Juul52c07962007-10-31 13:53:06 +01002313 realpage = (int)(from >> chip->page_shift);
2314 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002315
William Juul52c07962007-10-31 13:53:06 +01002316 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002317
William Juul52c07962007-10-31 13:53:06 +01002318 buf = ops->datbuf;
2319 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002320 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002321
Christian Hitz13fc0e22011-10-12 09:32:01 +02002322 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002323 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002324
Stefan Roese80877fa2022-09-02 14:10:46 +02002325 schedule();
William Juul52c07962007-10-31 13:53:06 +01002326 bytes = min(mtd->writesize - col, readlen);
2327 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002328
Scott Wood3ea94ed2015-06-26 19:03:26 -05002329 if (!aligned)
2330 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002331 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2332 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2333 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002334 else
2335 use_bufpoi = 0;
2336
Sergey Lapin3a38a552013-01-14 03:46:50 +00002337 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002338 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002339 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2340
2341 if (use_bufpoi && aligned)
2342 pr_debug("%s: using read bounce buffer for buf@%p\n",
2343 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002344
Heiko Schocherf5895d12014-06-24 10:10:04 +02002345read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002346 if (nand_standard_page_accessors(&chip->ecc)) {
2347 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2348 if (ret)
2349 break;
2350 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002351
Paul Burton700a76c2013-09-04 15:16:56 +01002352 /*
2353 * Now read the page into the buffer. Absent an error,
2354 * the read methods return max bitflips per ecc step.
2355 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002356 if (unlikely(ops->mode == MTD_OPS_RAW))
2357 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2358 oob_required,
2359 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002360 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002361 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002362 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002363 col, bytes, bufpoi,
2364 page);
William Juul52c07962007-10-31 13:53:06 +01002365 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002366 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002367 oob_required, page);
2368 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002369 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002370 /* Invalidate page cache */
2371 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002372 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002373 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002374
Paul Burton700a76c2013-09-04 15:16:56 +01002375 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2376
William Juul52c07962007-10-31 13:53:06 +01002377 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002378 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002379 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002380 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002381 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002382 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002383 chip->pagebuf_bitflips = ret;
2384 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002385 /* Invalidate page cache */
2386 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002387 }
William Juul52c07962007-10-31 13:53:06 +01002388 memcpy(buf, chip->buffers->databuf + col, bytes);
2389 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002390
William Juul52c07962007-10-31 13:53:06 +01002391 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002392 int toread = min(oobreadlen, max_oobsize);
2393
2394 if (toread) {
2395 oob = nand_transfer_oob(chip,
2396 oob, ops, toread);
2397 oobreadlen -= toread;
2398 }
William Juul52c07962007-10-31 13:53:06 +01002399 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002400
2401 if (chip->options & NAND_NEED_READRDY) {
2402 /* Apply delay or wait for ready/busy pin */
2403 if (!chip->dev_ready)
2404 udelay(chip->chip_delay);
2405 else
2406 nand_wait_ready(mtd);
2407 }
2408
2409 if (mtd->ecc_stats.failed - ecc_failures) {
2410 if (retry_mode + 1 < chip->read_retries) {
2411 retry_mode++;
2412 ret = nand_setup_read_retry(mtd,
2413 retry_mode);
2414 if (ret < 0)
2415 break;
2416
2417 /* Reset failures; retry */
2418 mtd->ecc_stats.failed = ecc_failures;
2419 goto read_retry;
2420 } else {
2421 /* No more retry modes; real failure */
2422 ecc_fail = true;
2423 }
2424 }
2425
2426 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002427 } else {
2428 memcpy(buf, chip->buffers->databuf + col, bytes);
2429 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002430 max_bitflips = max_t(unsigned int, max_bitflips,
2431 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002432 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002433
William Juul52c07962007-10-31 13:53:06 +01002434 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002435
Heiko Schocherf5895d12014-06-24 10:10:04 +02002436 /* Reset to retry mode 0 */
2437 if (retry_mode) {
2438 ret = nand_setup_read_retry(mtd, 0);
2439 if (ret < 0)
2440 break;
2441 retry_mode = 0;
2442 }
2443
William Juul52c07962007-10-31 13:53:06 +01002444 if (!readlen)
2445 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002446
Sergey Lapin3a38a552013-01-14 03:46:50 +00002447 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002448 col = 0;
2449 /* Increment page address */
2450 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002451
William Juul52c07962007-10-31 13:53:06 +01002452 page = realpage & chip->pagemask;
2453 /* Check, if we cross a chip boundary */
2454 if (!page) {
2455 chipnr++;
2456 chip->select_chip(mtd, -1);
2457 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002458 }
William Juul52c07962007-10-31 13:53:06 +01002459 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002460 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002461
William Juul52c07962007-10-31 13:53:06 +01002462 ops->retlen = ops->len - (size_t) readlen;
2463 if (oob)
2464 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002465
Heiko Schocherf5895d12014-06-24 10:10:04 +02002466 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002467 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002468
Heiko Schocherf5895d12014-06-24 10:10:04 +02002469 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002470 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002471
Paul Burton700a76c2013-09-04 15:16:56 +01002472 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002473}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002474
William Juul52c07962007-10-31 13:53:06 +01002475/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002476 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2477 * @mtd: mtd info structure
2478 * @chip: nand chip info structure
2479 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002480 */
2481static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002482 int page)
William Juul52c07962007-10-31 13:53:06 +01002483{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002484 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002485}
2486
2487/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002488 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002489 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002490 * @mtd: mtd info structure
2491 * @chip: nand chip info structure
2492 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002493 */
2494static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002495 int page)
William Juul52c07962007-10-31 13:53:06 +01002496{
William Juul52c07962007-10-31 13:53:06 +01002497 int length = mtd->oobsize;
2498 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2499 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002500 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002501 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002502
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002503 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2504 if (ret)
2505 return ret;
2506
William Juul52c07962007-10-31 13:53:06 +01002507 for (i = 0; i < chip->ecc.steps; i++) {
2508 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002509 int ret;
2510
William Juul52c07962007-10-31 13:53:06 +01002511 pos = eccsize + i * (eccsize + chunk);
2512 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002513 ret = nand_change_read_column_op(chip, pos,
2514 NULL, 0,
2515 false);
William Juul52c07962007-10-31 13:53:06 +01002516 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002517 ret = nand_read_page_op(chip, page, pos, NULL,
2518 0);
2519
2520 if (ret)
2521 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002522 } else
William Juul52c07962007-10-31 13:53:06 +01002523 sndrnd = 1;
2524 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002525
2526 ret = nand_read_data_op(chip, bufpoi, toread, false);
2527 if (ret)
2528 return ret;
2529
William Juul52c07962007-10-31 13:53:06 +01002530 bufpoi += toread;
2531 length -= toread;
2532 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002533 if (length > 0) {
2534 ret = nand_read_data_op(chip, bufpoi, length, false);
2535 if (ret)
2536 return ret;
2537 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002538
Sergey Lapin3a38a552013-01-14 03:46:50 +00002539 return 0;
William Juul52c07962007-10-31 13:53:06 +01002540}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002541
William Juul52c07962007-10-31 13:53:06 +01002542/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002543 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2544 * @mtd: mtd info structure
2545 * @chip: nand chip info structure
2546 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002547 */
2548static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2549 int page)
2550{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002551 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2552 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002553}
2554
2555/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002556 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2557 * with syndrome - only for large page flash
2558 * @mtd: mtd info structure
2559 * @chip: nand chip info structure
2560 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002561 */
2562static int nand_write_oob_syndrome(struct mtd_info *mtd,
2563 struct nand_chip *chip, int page)
2564{
2565 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2566 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002567 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002568 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002569
2570 /*
William Juul52c07962007-10-31 13:53:06 +01002571 * data-ecc-data-ecc ... ecc-oob
2572 * or
2573 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002574 */
William Juul52c07962007-10-31 13:53:06 +01002575 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2576 pos = steps * (eccsize + chunk);
2577 steps = 0;
2578 } else
2579 pos = eccsize;
2580
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002581 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2582 if (ret)
2583 return ret;
2584
William Juul52c07962007-10-31 13:53:06 +01002585 for (i = 0; i < steps; i++) {
2586 if (sndcmd) {
2587 if (mtd->writesize <= 512) {
2588 uint32_t fill = 0xFFFFFFFF;
2589
2590 len = eccsize;
2591 while (len > 0) {
2592 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002593
2594 ret = nand_write_data_op(chip, &fill,
2595 num, false);
2596 if (ret)
2597 return ret;
2598
William Juul52c07962007-10-31 13:53:06 +01002599 len -= num;
2600 }
2601 } else {
2602 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002603 ret = nand_change_write_column_op(chip, pos,
2604 NULL, 0,
2605 false);
2606 if (ret)
2607 return ret;
William Juul52c07962007-10-31 13:53:06 +01002608 }
2609 } else
2610 sndcmd = 1;
2611 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002612
2613 ret = nand_write_data_op(chip, bufpoi, len, false);
2614 if (ret)
2615 return ret;
2616
William Juul52c07962007-10-31 13:53:06 +01002617 bufpoi += len;
2618 length -= len;
2619 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002620 if (length > 0) {
2621 ret = nand_write_data_op(chip, bufpoi, length, false);
2622 if (ret)
2623 return ret;
2624 }
William Juul52c07962007-10-31 13:53:06 +01002625
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002626 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002627}
2628
2629/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002630 * nand_do_read_oob - [INTERN] NAND read out-of-band
2631 * @mtd: MTD device structure
2632 * @from: offset to read from
2633 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002634 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002635 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002636 */
William Juul52c07962007-10-31 13:53:06 +01002637static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2638 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002639{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002640 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002641 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002642 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002643 int readlen = ops->ooblen;
2644 int len;
2645 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002646 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002647
Heiko Schocherf5895d12014-06-24 10:10:04 +02002648 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002649 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002650
Sergey Lapin3a38a552013-01-14 03:46:50 +00002651 stats = mtd->ecc_stats;
2652
Scott Wood52ab7ce2016-05-30 13:57:58 -05002653 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002654
William Juul52c07962007-10-31 13:53:06 +01002655 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002656 pr_debug("%s: attempt to start read outside oob\n",
2657 __func__);
William Juul52c07962007-10-31 13:53:06 +01002658 return -EINVAL;
2659 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002660
2661 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002662 if (unlikely(from >= mtd->size ||
2663 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2664 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002665 pr_debug("%s: attempt to read beyond end of device\n",
2666 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002667 return -EINVAL;
2668 }
2669
William Juul52c07962007-10-31 13:53:06 +01002670 chipnr = (int)(from >> chip->chip_shift);
2671 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002672
William Juul52c07962007-10-31 13:53:06 +01002673 /* Shift to get page */
2674 realpage = (int)(from >> chip->page_shift);
2675 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002676
Christian Hitz13fc0e22011-10-12 09:32:01 +02002677 while (1) {
Stefan Roese80877fa2022-09-02 14:10:46 +02002678 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002679
Sergey Lapin3a38a552013-01-14 03:46:50 +00002680 if (ops->mode == MTD_OPS_RAW)
2681 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2682 else
2683 ret = chip->ecc.read_oob(mtd, chip, page);
2684
2685 if (ret < 0)
2686 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002687
William Juul52c07962007-10-31 13:53:06 +01002688 len = min(len, readlen);
2689 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002690
Heiko Schocherf5895d12014-06-24 10:10:04 +02002691 if (chip->options & NAND_NEED_READRDY) {
2692 /* Apply delay or wait for ready/busy pin */
2693 if (!chip->dev_ready)
2694 udelay(chip->chip_delay);
2695 else
2696 nand_wait_ready(mtd);
2697 }
2698
William Juul52c07962007-10-31 13:53:06 +01002699 readlen -= len;
2700 if (!readlen)
2701 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002702
William Juul52c07962007-10-31 13:53:06 +01002703 /* Increment page address */
2704 realpage++;
2705
2706 page = realpage & chip->pagemask;
2707 /* Check, if we cross a chip boundary */
2708 if (!page) {
2709 chipnr++;
2710 chip->select_chip(mtd, -1);
2711 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002712 }
William Juul52c07962007-10-31 13:53:06 +01002713 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002714 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002715
Sergey Lapin3a38a552013-01-14 03:46:50 +00002716 ops->oobretlen = ops->ooblen - readlen;
2717
2718 if (ret < 0)
2719 return ret;
2720
2721 if (mtd->ecc_stats.failed - stats.failed)
2722 return -EBADMSG;
2723
2724 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002725}
2726
2727/**
William Juul52c07962007-10-31 13:53:06 +01002728 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002729 * @mtd: MTD device structure
2730 * @from: offset to read from
2731 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002732 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002733 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002734 */
William Juul52c07962007-10-31 13:53:06 +01002735static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2736 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002737{
William Juul52c07962007-10-31 13:53:06 +01002738 int ret = -ENOTSUPP;
2739
2740 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002741
2742 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002743 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002744 pr_debug("%s: attempt to read beyond end of device\n",
2745 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002746 return -EINVAL;
2747 }
2748
Heiko Schocherf5895d12014-06-24 10:10:04 +02002749 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002750
Christian Hitz13fc0e22011-10-12 09:32:01 +02002751 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002752 case MTD_OPS_PLACE_OOB:
2753 case MTD_OPS_AUTO_OOB:
2754 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002755 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002756
William Juul52c07962007-10-31 13:53:06 +01002757 default:
2758 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002759 }
2760
William Juul52c07962007-10-31 13:53:06 +01002761 if (!ops->datbuf)
2762 ret = nand_do_read_oob(mtd, from, ops);
2763 else
2764 ret = nand_do_read_ops(mtd, from, ops);
2765
Christian Hitz13fc0e22011-10-12 09:32:01 +02002766out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002767 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002768 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002769}
2770
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002771/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002772 * nand_write_page_raw - [INTERN] raw page write function
2773 * @mtd: mtd info structure
2774 * @chip: nand chip info structure
2775 * @buf: data buffer
2776 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002777 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002778 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002779 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002780 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002781static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002782 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002783{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002784 int ret;
2785
2786 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2787 if (ret)
2788 return ret;
2789
2790 if (oob_required) {
2791 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2792 false);
2793 if (ret)
2794 return ret;
2795 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002796
2797 return 0;
William Juul52c07962007-10-31 13:53:06 +01002798}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002799
William Juul52c07962007-10-31 13:53:06 +01002800/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002801 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2802 * @mtd: mtd info structure
2803 * @chip: nand chip info structure
2804 * @buf: data buffer
2805 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002806 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002807 *
2808 * We need a special oob layout and handling even when ECC isn't checked.
2809 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002810static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002811 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002812 const uint8_t *buf, int oob_required,
2813 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002814{
2815 int eccsize = chip->ecc.size;
2816 int eccbytes = chip->ecc.bytes;
2817 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002818 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002819
2820 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002821 ret = nand_write_data_op(chip, buf, eccsize, false);
2822 if (ret)
2823 return ret;
2824
David Brownellee86b8d2009-11-07 16:27:01 -05002825 buf += eccsize;
2826
2827 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002828 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2829 false);
2830 if (ret)
2831 return ret;
2832
David Brownellee86b8d2009-11-07 16:27:01 -05002833 oob += chip->ecc.prepad;
2834 }
2835
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002836 ret = nand_write_data_op(chip, oob, eccbytes, false);
2837 if (ret)
2838 return ret;
2839
David Brownellee86b8d2009-11-07 16:27:01 -05002840 oob += eccbytes;
2841
2842 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002843 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2844 false);
2845 if (ret)
2846 return ret;
2847
David Brownellee86b8d2009-11-07 16:27:01 -05002848 oob += chip->ecc.postpad;
2849 }
2850 }
2851
2852 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002853 if (size) {
2854 ret = nand_write_data_op(chip, oob, size, false);
2855 if (ret)
2856 return ret;
2857 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002858
2859 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002860}
2861/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002862 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2863 * @mtd: mtd info structure
2864 * @chip: nand chip info structure
2865 * @buf: data buffer
2866 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002867 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002868 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002869static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002870 const uint8_t *buf, int oob_required,
2871 int page)
William Juul52c07962007-10-31 13:53:06 +01002872{
2873 int i, eccsize = chip->ecc.size;
2874 int eccbytes = chip->ecc.bytes;
2875 int eccsteps = chip->ecc.steps;
2876 uint8_t *ecc_calc = chip->buffers->ecccalc;
2877 const uint8_t *p = buf;
2878 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002879
Sergey Lapin3a38a552013-01-14 03:46:50 +00002880 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002881 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2882 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002883
William Juul52c07962007-10-31 13:53:06 +01002884 for (i = 0; i < chip->ecc.total; i++)
2885 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002886
Scott Wood46e13102016-05-30 13:57:57 -05002887 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002888}
2889
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002890/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002891 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2892 * @mtd: mtd info structure
2893 * @chip: nand chip info structure
2894 * @buf: data buffer
2895 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002896 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002897 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002898static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002899 const uint8_t *buf, int oob_required,
2900 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002901{
William Juul52c07962007-10-31 13:53:06 +01002902 int i, eccsize = chip->ecc.size;
2903 int eccbytes = chip->ecc.bytes;
2904 int eccsteps = chip->ecc.steps;
2905 uint8_t *ecc_calc = chip->buffers->ecccalc;
2906 const uint8_t *p = buf;
2907 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002908 int ret;
William Juul52c07962007-10-31 13:53:06 +01002909
2910 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2911 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002912
2913 ret = nand_write_data_op(chip, p, eccsize, false);
2914 if (ret)
2915 return ret;
2916
William Juul52c07962007-10-31 13:53:06 +01002917 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2918 }
2919
2920 for (i = 0; i < chip->ecc.total; i++)
2921 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2922
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002923 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2924 if (ret)
2925 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002926
2927 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002928}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002929
Heiko Schocherf5895d12014-06-24 10:10:04 +02002930/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002931 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002932 * @mtd: mtd info structure
2933 * @chip: nand chip info structure
2934 * @offset: column address of subpage within the page
2935 * @data_len: data length
2936 * @buf: data buffer
2937 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002938 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002939 */
2940static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2941 struct nand_chip *chip, uint32_t offset,
2942 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002943 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002944{
2945 uint8_t *oob_buf = chip->oob_poi;
2946 uint8_t *ecc_calc = chip->buffers->ecccalc;
2947 int ecc_size = chip->ecc.size;
2948 int ecc_bytes = chip->ecc.bytes;
2949 int ecc_steps = chip->ecc.steps;
2950 uint32_t *eccpos = chip->ecc.layout->eccpos;
2951 uint32_t start_step = offset / ecc_size;
2952 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2953 int oob_bytes = mtd->oobsize / ecc_steps;
2954 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002955 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002956
2957 for (step = 0; step < ecc_steps; step++) {
2958 /* configure controller for WRITE access */
2959 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2960
2961 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002962 ret = nand_write_data_op(chip, buf, ecc_size, false);
2963 if (ret)
2964 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002965
2966 /* mask ECC of un-touched subpages by padding 0xFF */
2967 if ((step < start_step) || (step > end_step))
2968 memset(ecc_calc, 0xff, ecc_bytes);
2969 else
2970 chip->ecc.calculate(mtd, buf, ecc_calc);
2971
2972 /* mask OOB of un-touched subpages by padding 0xFF */
2973 /* if oob_required, preserve OOB metadata of written subpage */
2974 if (!oob_required || (step < start_step) || (step > end_step))
2975 memset(oob_buf, 0xff, oob_bytes);
2976
2977 buf += ecc_size;
2978 ecc_calc += ecc_bytes;
2979 oob_buf += oob_bytes;
2980 }
2981
2982 /* copy calculated ECC for whole page to chip->buffer->oob */
2983 /* this include masked-value(0xFF) for unwritten subpages */
2984 ecc_calc = chip->buffers->ecccalc;
2985 for (i = 0; i < chip->ecc.total; i++)
2986 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2987
2988 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002989 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2990 if (ret)
2991 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002992
2993 return 0;
2994}
2995
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002996/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002997 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2998 * @mtd: mtd info structure
2999 * @chip: nand chip info structure
3000 * @buf: data buffer
3001 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003002 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003003 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003004 * The hw generator calculates the error syndrome automatically. Therefore we
3005 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003006 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003007static int nand_write_page_syndrome(struct mtd_info *mtd,
3008 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003009 const uint8_t *buf, int oob_required,
3010 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003011{
William Juul52c07962007-10-31 13:53:06 +01003012 int i, eccsize = chip->ecc.size;
3013 int eccbytes = chip->ecc.bytes;
3014 int eccsteps = chip->ecc.steps;
3015 const uint8_t *p = buf;
3016 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003017 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003018
William Juul52c07962007-10-31 13:53:06 +01003019 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003020 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003021
3022 ret = nand_write_data_op(chip, p, eccsize, false);
3023 if (ret)
3024 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003025
William Juul52c07962007-10-31 13:53:06 +01003026 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003027 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3028 false);
3029 if (ret)
3030 return ret;
3031
William Juul52c07962007-10-31 13:53:06 +01003032 oob += chip->ecc.prepad;
3033 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003034
William Juul52c07962007-10-31 13:53:06 +01003035 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003036
3037 ret = nand_write_data_op(chip, oob, eccbytes, false);
3038 if (ret)
3039 return ret;
3040
William Juul52c07962007-10-31 13:53:06 +01003041 oob += eccbytes;
3042
3043 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003044 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3045 false);
3046 if (ret)
3047 return ret;
3048
William Juul52c07962007-10-31 13:53:06 +01003049 oob += chip->ecc.postpad;
3050 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003051 }
3052
William Juul52c07962007-10-31 13:53:06 +01003053 /* Calculate remaining oob bytes */
3054 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003055 if (i) {
3056 ret = nand_write_data_op(chip, oob, i, false);
3057 if (ret)
3058 return ret;
3059 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003060
3061 return 0;
William Juul52c07962007-10-31 13:53:06 +01003062}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003063
William Juul52c07962007-10-31 13:53:06 +01003064/**
3065 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003066 * @mtd: MTD device structure
3067 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003068 * @offset: address offset within the page
3069 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003070 * @buf: the data to write
3071 * @oob_required: must write chip->oob_poi to OOB
3072 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003073 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003074 */
3075static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003076 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003077 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003078{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003079 int status, subpage;
3080
3081 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3082 chip->ecc.write_subpage)
3083 subpage = offset || (data_len < mtd->writesize);
3084 else
3085 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003086
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003087 if (nand_standard_page_accessors(&chip->ecc)) {
3088 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3089 if (status)
3090 return status;
3091 }
William Juul52c07962007-10-31 13:53:06 +01003092
3093 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003094 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003095 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003096 else if (subpage)
3097 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003098 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003099 else
Scott Wood46e13102016-05-30 13:57:57 -05003100 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3101 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003102
3103 if (status < 0)
3104 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003105
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003106 if (nand_standard_page_accessors(&chip->ecc))
3107 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003108
William Juul52c07962007-10-31 13:53:06 +01003109 return 0;
3110}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003111
William Juul52c07962007-10-31 13:53:06 +01003112/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003113 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3114 * @mtd: MTD device structure
3115 * @oob: oob data buffer
3116 * @len: oob data write length
3117 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003118 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003119static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3120 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003121{
Scott Wood17fed142016-05-30 13:57:56 -05003122 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003123
3124 /*
3125 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3126 * data from a previous OOB read.
3127 */
3128 memset(chip->oob_poi, 0xff, mtd->oobsize);
3129
Christian Hitz13fc0e22011-10-12 09:32:01 +02003130 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003131
Sergey Lapin3a38a552013-01-14 03:46:50 +00003132 case MTD_OPS_PLACE_OOB:
3133 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003134 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3135 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003136
Sergey Lapin3a38a552013-01-14 03:46:50 +00003137 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003138 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3139 uint32_t boffs = 0, woffs = ops->ooboffs;
3140 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003141
Christian Hitz13fc0e22011-10-12 09:32:01 +02003142 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003143 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003144 if (unlikely(woffs)) {
3145 if (woffs >= free->length) {
3146 woffs -= free->length;
3147 continue;
3148 }
3149 boffs = free->offset + woffs;
3150 bytes = min_t(size_t, len,
3151 (free->length - woffs));
3152 woffs = 0;
3153 } else {
3154 bytes = min_t(size_t, len, free->length);
3155 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003156 }
William Juul52c07962007-10-31 13:53:06 +01003157 memcpy(chip->oob_poi + boffs, oob, bytes);
3158 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003159 }
William Juul52c07962007-10-31 13:53:06 +01003160 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003161 }
William Juul52c07962007-10-31 13:53:06 +01003162 default:
3163 BUG();
3164 }
3165 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003166}
3167
Christian Hitzb8a6b372011-10-12 09:32:02 +02003168#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003169
3170/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003171 * nand_do_write_ops - [INTERN] NAND write with ECC
3172 * @mtd: MTD device structure
3173 * @to: offset to write to
3174 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003175 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003176 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003177 */
William Juul52c07962007-10-31 13:53:06 +01003178static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3179 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003180{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003181 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003182 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003183 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003184
3185 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003186 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003187
William Juul52c07962007-10-31 13:53:06 +01003188 uint8_t *oob = ops->oobbuf;
3189 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003190 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003191 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003192
William Juul52c07962007-10-31 13:53:06 +01003193 ops->retlen = 0;
3194 if (!writelen)
3195 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003196
Heiko Schocherf5895d12014-06-24 10:10:04 +02003197 /* Reject writes, which are not page aligned */
3198 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003199 pr_notice("%s: attempt to write non page aligned data\n",
3200 __func__);
William Juul52c07962007-10-31 13:53:06 +01003201 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003202 }
3203
3204 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003205
William Juul52c07962007-10-31 13:53:06 +01003206 chipnr = (int)(to >> chip->chip_shift);
3207 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003208
3209 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003210 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003211 ret = -EIO;
3212 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003213 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003214
William Juul52c07962007-10-31 13:53:06 +01003215 realpage = (int)(to >> chip->page_shift);
3216 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003217
William Juul52c07962007-10-31 13:53:06 +01003218 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003219 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3220 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003221 chip->pagebuf = -1;
3222
Christian Hitzb8a6b372011-10-12 09:32:02 +02003223 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003224 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3225 ret = -EINVAL;
3226 goto err_out;
3227 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003228
Christian Hitz13fc0e22011-10-12 09:32:01 +02003229 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003230 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003231 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003232 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003233 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003234
3235 if (part_pagewr)
3236 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003237 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3238 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3239 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003240 else
3241 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003242
Stefan Roese80877fa2022-09-02 14:10:46 +02003243 schedule();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003244 /* Partial page write?, or need to use bounce buffer */
3245 if (use_bufpoi) {
3246 pr_debug("%s: using write bounce buffer for buf@%p\n",
3247 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003248 if (part_pagewr)
3249 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003250 chip->pagebuf = -1;
3251 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3252 memcpy(&chip->buffers->databuf[column], buf, bytes);
3253 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003254 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003255
Christian Hitzb8a6b372011-10-12 09:32:02 +02003256 if (unlikely(oob)) {
3257 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003258 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003259 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003260 } else {
3261 /* We still need to erase leftover OOB data */
3262 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003263 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003264 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003265 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003266 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003267 if (ret)
3268 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003269
William Juul52c07962007-10-31 13:53:06 +01003270 writelen -= bytes;
3271 if (!writelen)
3272 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003273
Heiko Schocherf5895d12014-06-24 10:10:04 +02003274 column = 0;
3275 buf += bytes;
3276 realpage++;
3277
3278 page = realpage & chip->pagemask;
3279 /* Check, if we cross a chip boundary */
3280 if (!page) {
3281 chipnr++;
3282 chip->select_chip(mtd, -1);
3283 chip->select_chip(mtd, chipnr);
3284 }
3285 }
3286
3287 ops->retlen = ops->len - writelen;
3288 if (unlikely(oob))
3289 ops->oobretlen = ops->ooblen;
3290
3291err_out:
3292 chip->select_chip(mtd, -1);
3293 return ret;
3294}
3295
3296/**
3297 * panic_nand_write - [MTD Interface] NAND write with ECC
3298 * @mtd: MTD device structure
3299 * @to: offset to write to
3300 * @len: number of bytes to write
3301 * @retlen: pointer to variable to store the number of written bytes
3302 * @buf: the data to write
3303 *
3304 * NAND write with ECC. Used when performing writes in interrupt context, this
3305 * may for example be called by mtdoops when writing an oops while in panic.
3306 */
3307static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3308 size_t *retlen, const uint8_t *buf)
3309{
Scott Wood17fed142016-05-30 13:57:56 -05003310 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003311 struct mtd_oob_ops ops;
3312 int ret;
3313
3314 /* Wait for the device to get ready */
3315 panic_nand_wait(mtd, chip, 400);
3316
3317 /* Grab the device */
3318 panic_nand_get_device(chip, mtd, FL_WRITING);
3319
Scott Wood3ea94ed2015-06-26 19:03:26 -05003320 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003321 ops.len = len;
3322 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003323 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003324
Heiko Schocherf5895d12014-06-24 10:10:04 +02003325 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003326
Sergey Lapin3a38a552013-01-14 03:46:50 +00003327 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003328 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003329}
3330
3331/**
William Juul52c07962007-10-31 13:53:06 +01003332 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003333 * @mtd: MTD device structure
3334 * @to: offset to write to
3335 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003336 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003337 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003338 */
William Juul52c07962007-10-31 13:53:06 +01003339static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3340 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003341{
William Juul52c07962007-10-31 13:53:06 +01003342 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003343 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003344
Heiko Schocherf5895d12014-06-24 10:10:04 +02003345 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003346 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003347
Scott Wood52ab7ce2016-05-30 13:57:58 -05003348 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003349
3350 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003351 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003352 pr_debug("%s: attempt to write past end of page\n",
3353 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003354 return -EINVAL;
3355 }
3356
William Juul52c07962007-10-31 13:53:06 +01003357 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003358 pr_debug("%s: attempt to start write outside oob\n",
3359 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003360 return -EINVAL;
3361 }
3362
Christian Hitz13fc0e22011-10-12 09:32:01 +02003363 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003364 if (unlikely(to >= mtd->size ||
3365 ops->ooboffs + ops->ooblen >
3366 ((mtd->size >> chip->page_shift) -
3367 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003368 pr_debug("%s: attempt to write beyond end of device\n",
3369 __func__);
William Juul52c07962007-10-31 13:53:06 +01003370 return -EINVAL;
3371 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003372
William Juul52c07962007-10-31 13:53:06 +01003373 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003374
3375 /*
3376 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3377 * of my DiskOnChip 2000 test units) will clear the whole data page too
3378 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3379 * it in the doc2000 driver in August 1999. dwmw2.
3380 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003381 nand_reset(chip, chipnr);
3382
3383 chip->select_chip(mtd, chipnr);
3384
3385 /* Shift to get page */
3386 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003387
3388 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003389 if (nand_check_wp(mtd)) {
3390 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003391 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003392 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003393
William Juul52c07962007-10-31 13:53:06 +01003394 /* Invalidate the page cache, if we write to the cached page */
3395 if (page == chip->pagebuf)
3396 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003397
Sergey Lapin3a38a552013-01-14 03:46:50 +00003398 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3399
3400 if (ops->mode == MTD_OPS_RAW)
3401 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3402 else
3403 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003404
Heiko Schocherf5895d12014-06-24 10:10:04 +02003405 chip->select_chip(mtd, -1);
3406
William Juul52c07962007-10-31 13:53:06 +01003407 if (status)
3408 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003409
William Juul52c07962007-10-31 13:53:06 +01003410 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003411
William Juul52c07962007-10-31 13:53:06 +01003412 return 0;
3413}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003414
William Juul52c07962007-10-31 13:53:06 +01003415/**
3416 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003417 * @mtd: MTD device structure
3418 * @to: offset to write to
3419 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003420 */
3421static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3422 struct mtd_oob_ops *ops)
3423{
William Juul52c07962007-10-31 13:53:06 +01003424 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003425
William Juul52c07962007-10-31 13:53:06 +01003426 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003427
William Juul52c07962007-10-31 13:53:06 +01003428 /* Do not allow writes past end of device */
3429 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003430 pr_debug("%s: attempt to write beyond end of device\n",
3431 __func__);
William Juul52c07962007-10-31 13:53:06 +01003432 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003433 }
William Juul52c07962007-10-31 13:53:06 +01003434
Heiko Schocherf5895d12014-06-24 10:10:04 +02003435 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003436
Christian Hitz13fc0e22011-10-12 09:32:01 +02003437 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003438 case MTD_OPS_PLACE_OOB:
3439 case MTD_OPS_AUTO_OOB:
3440 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003441 break;
3442
3443 default:
3444 goto out;
3445 }
3446
3447 if (!ops->datbuf)
3448 ret = nand_do_write_oob(mtd, to, ops);
3449 else
3450 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003451
Christian Hitz13fc0e22011-10-12 09:32:01 +02003452out:
William Juul52c07962007-10-31 13:53:06 +01003453 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003454 return ret;
3455}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003456
3457/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003458 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003459 * @mtd: MTD device structure
3460 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003461 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003462 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003463 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003464static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003465{
Scott Wood17fed142016-05-30 13:57:56 -05003466 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003467 unsigned int eraseblock;
3468
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003469 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003470 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003471
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003472 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003473}
3474
3475/**
3476 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003477 * @mtd: MTD device structure
3478 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003479 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003480 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003481 */
William Juul52c07962007-10-31 13:53:06 +01003482static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003483{
William Juul52c07962007-10-31 13:53:06 +01003484 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003485}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003486
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003487/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003488 * nand_erase_nand - [INTERN] erase block(s)
3489 * @mtd: MTD device structure
3490 * @instr: erase instruction
3491 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003492 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003493 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003494 */
William Juul52c07962007-10-31 13:53:06 +01003495int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3496 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003497{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003498 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003499 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003500 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003501
Heiko Schocherf5895d12014-06-24 10:10:04 +02003502 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3503 __func__, (unsigned long long)instr->addr,
3504 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003505
Christian Hitzb8a6b372011-10-12 09:32:02 +02003506 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003507 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003508
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003509 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003510 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003511
3512 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003513 page = (int)(instr->addr >> chip->page_shift);
3514 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003515
3516 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003517 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003518
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003519 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003520 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003521
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003522 /* Check, if it is write protected */
3523 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003524 pr_debug("%s: device is write protected!\n",
3525 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003526 instr->state = MTD_ERASE_FAILED;
3527 goto erase_exit;
3528 }
3529
3530 /* Loop through the pages */
3531 len = instr->len;
3532
3533 instr->state = MTD_ERASING;
3534
3535 while (len) {
Stefan Roese80877fa2022-09-02 14:10:46 +02003536 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003537
Sergey Lapin3a38a552013-01-14 03:46:50 +00003538 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003539 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003540 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003541 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003542 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003543 instr->state = MTD_ERASE_FAILED;
Farhan Ali7c053192021-02-24 15:25:53 -08003544 instr->fail_addr =
3545 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003546 goto erase_exit;
3547 }
William Juul52c07962007-10-31 13:53:06 +01003548
3549 /*
3550 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003551 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003552 */
3553 if (page <= chip->pagebuf && chip->pagebuf <
3554 (page + pages_per_block))
3555 chip->pagebuf = -1;
3556
Scott Wood3ea94ed2015-06-26 19:03:26 -05003557 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003558
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003559 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003560 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003561 pr_debug("%s: failed erase, page 0x%08x\n",
3562 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003563 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003564 instr->fail_addr =
3565 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003566 goto erase_exit;
3567 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003568
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003569 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003570 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003571 page += pages_per_block;
3572
3573 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003574 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003575 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003576 chip->select_chip(mtd, -1);
3577 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003578 }
3579 }
3580 instr->state = MTD_ERASE_DONE;
3581
Christian Hitz13fc0e22011-10-12 09:32:01 +02003582erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003583
3584 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003585
3586 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003587 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003588 nand_release_device(mtd);
3589
3590 /* Return more or less happy */
3591 return ret;
3592}
3593
3594/**
3595 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003596 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003597 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003598 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003599 */
William Juul52c07962007-10-31 13:53:06 +01003600static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003601{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003602 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003603
3604 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003605 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003606 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003607 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003608}
3609
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003610/**
William Juul52c07962007-10-31 13:53:06 +01003611 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003612 * @mtd: MTD device structure
3613 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003614 */
William Juul52c07962007-10-31 13:53:06 +01003615static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003616{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003617 struct nand_chip *chip = mtd_to_nand(mtd);
3618 int chipnr = (int)(offs >> chip->chip_shift);
3619 int ret;
3620
3621 /* Select the NAND device */
3622 nand_get_device(mtd, FL_READING);
3623 chip->select_chip(mtd, chipnr);
3624
3625 ret = nand_block_checkbad(mtd, offs, 0);
3626
3627 chip->select_chip(mtd, -1);
3628 nand_release_device(mtd);
3629
3630 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003631}
3632
3633/**
William Juul52c07962007-10-31 13:53:06 +01003634 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003635 * @mtd: MTD device structure
3636 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003637 */
William Juul52c07962007-10-31 13:53:06 +01003638static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003639{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003640 int ret;
3641
Christian Hitzb8a6b372011-10-12 09:32:02 +02003642 ret = nand_block_isbad(mtd, ofs);
3643 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003644 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003645 if (ret > 0)
3646 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003647 return ret;
3648 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003649
Heiko Schocherf5895d12014-06-24 10:10:04 +02003650 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003651}
3652
Heiko Schocherf5895d12014-06-24 10:10:04 +02003653/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003654 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3655 * @mtd: MTD device structure
3656 * @chip: nand chip info structure
3657 * @addr: feature address.
3658 * @subfeature_param: the subfeature parameters, a four bytes array.
3659 */
3660static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3661 int addr, uint8_t *subfeature_param)
3662{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003663#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3664 if (!chip->onfi_version ||
3665 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3666 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003667 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003668#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003669
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003670 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003671}
3672
3673/**
3674 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3675 * @mtd: MTD device structure
3676 * @chip: nand chip info structure
3677 * @addr: feature address.
3678 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003679 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003680static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3681 int addr, uint8_t *subfeature_param)
3682{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003683#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3684 if (!chip->onfi_version ||
3685 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3686 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003687 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003688#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003689
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003690 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003691}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003692
Sergey Lapin3a38a552013-01-14 03:46:50 +00003693/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003694static void nand_set_defaults(struct nand_chip *chip, int busw)
3695{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003696 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003697 if (!chip->chip_delay)
3698 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003699
3700 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003701 if (chip->cmdfunc == NULL)
3702 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003703
3704 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003705 if (chip->waitfunc == NULL)
3706 chip->waitfunc = nand_wait;
3707
3708 if (!chip->select_chip)
3709 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003710
3711 /* set for ONFI nand */
3712 if (!chip->onfi_set_features)
3713 chip->onfi_set_features = nand_onfi_set_features;
3714 if (!chip->onfi_get_features)
3715 chip->onfi_get_features = nand_onfi_get_features;
3716
3717 /* If called twice, pointers that depend on busw may need to be reset */
3718 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003719 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3720 if (!chip->read_word)
3721 chip->read_word = nand_read_word;
3722 if (!chip->block_bad)
3723 chip->block_bad = nand_block_bad;
3724 if (!chip->block_markbad)
3725 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003726 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003727 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003728 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3729 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3730 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003731 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Roger Quadrosb590f612022-12-20 12:21:57 +02003732
Simon Glass7ec24132024-09-29 19:49:48 -06003733#ifndef CONFIG_XPL_BUILD
William Juul52c07962007-10-31 13:53:06 +01003734 if (!chip->scan_bbt)
3735 chip->scan_bbt = nand_default_bbt;
Roger Quadrosb590f612022-12-20 12:21:57 +02003736#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02003737
3738 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003739 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003740 spin_lock_init(&chip->controller->lock);
3741 init_waitqueue_head(&chip->controller->wq);
3742 }
3743
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003744 if (!chip->buf_align)
3745 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003746}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003747
Sergey Lapin3a38a552013-01-14 03:46:50 +00003748/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003749static void sanitize_string(char *s, size_t len)
3750{
3751 ssize_t i;
3752
Sergey Lapin3a38a552013-01-14 03:46:50 +00003753 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003754 s[len - 1] = 0;
3755
Sergey Lapin3a38a552013-01-14 03:46:50 +00003756 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003757 for (i = 0; i < len - 1; i++) {
3758 if (s[i] < ' ' || s[i] > 127)
3759 s[i] = '?';
3760 }
3761
Sergey Lapin3a38a552013-01-14 03:46:50 +00003762 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003763 strim(s);
3764}
3765
Florian Fainellic98a9352011-02-25 00:01:34 +00003766static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3767{
3768 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003769 while (len--) {
3770 crc ^= *p++ << 8;
3771 for (i = 0; i < 8; i++)
3772 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3773 }
3774
3775 return crc;
3776}
3777
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003778#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003779/* Parse the Extended Parameter Page. */
3780static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3781 struct nand_chip *chip, struct nand_onfi_params *p)
3782{
3783 struct onfi_ext_param_page *ep;
3784 struct onfi_ext_section *s;
3785 struct onfi_ext_ecc_info *ecc;
3786 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003787 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003788 int len;
3789 int i;
3790
3791 len = le16_to_cpu(p->ext_param_page_length) * 16;
3792 ep = kmalloc(len, GFP_KERNEL);
3793 if (!ep)
3794 return -ENOMEM;
3795
3796 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003797 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3798 if (ret)
3799 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003800
3801 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003802 ret = nand_change_read_column_op(chip,
3803 sizeof(*p) * p->num_of_param_pages,
3804 ep, len, true);
3805 if (ret)
3806 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003807
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003808 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003809 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3810 != le16_to_cpu(ep->crc))) {
3811 pr_debug("fail in the CRC.\n");
3812 goto ext_out;
3813 }
3814
3815 /*
3816 * Check the signature.
3817 * Do not strictly follow the ONFI spec, maybe changed in future.
3818 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003819 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003820 pr_debug("The signature is invalid.\n");
3821 goto ext_out;
3822 }
3823
3824 /* find the ECC section. */
3825 cursor = (uint8_t *)(ep + 1);
3826 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3827 s = ep->sections + i;
3828 if (s->type == ONFI_SECTION_TYPE_2)
3829 break;
3830 cursor += s->length * 16;
3831 }
3832 if (i == ONFI_EXT_SECTION_MAX) {
3833 pr_debug("We can not find the ECC section.\n");
3834 goto ext_out;
3835 }
3836
3837 /* get the info we want. */
3838 ecc = (struct onfi_ext_ecc_info *)cursor;
3839
3840 if (!ecc->codeword_size) {
3841 pr_debug("Invalid codeword size\n");
3842 goto ext_out;
3843 }
3844
3845 chip->ecc_strength_ds = ecc->ecc_bits;
3846 chip->ecc_step_ds = 1 << ecc->codeword_size;
3847 ret = 0;
3848
3849ext_out:
3850 kfree(ep);
3851 return ret;
3852}
3853
Florian Fainellic98a9352011-02-25 00:01:34 +00003854/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003855 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003856 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003857static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003858{
3859 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003860 char id[4];
3861 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003862
Sergey Lapin3a38a552013-01-14 03:46:50 +00003863 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003864 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3865 if (ret || strncmp(id, "ONFI", 4))
3866 return 0;
3867
3868 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3869 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003870 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003871
Florian Fainellic98a9352011-02-25 00:01:34 +00003872 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003873 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3874 if (ret)
3875 return 0;
3876
Florian Fainellic98a9352011-02-25 00:01:34 +00003877 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003878 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003879 break;
3880 }
3881 }
3882
Heiko Schocherf5895d12014-06-24 10:10:04 +02003883 if (i == 3) {
3884 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003885 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003886 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003887
Sergey Lapin3a38a552013-01-14 03:46:50 +00003888 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003889 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003890 if (val & (1 << 5))
3891 chip->onfi_version = 23;
3892 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003893 chip->onfi_version = 22;
3894 else if (val & (1 << 3))
3895 chip->onfi_version = 21;
3896 else if (val & (1 << 2))
3897 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003898 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003899 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003900
3901 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003902 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003903 return 0;
3904 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003905
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003906 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3907 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003908 if (!mtd->name)
3909 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003910
Florian Fainellic98a9352011-02-25 00:01:34 +00003911 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003912
3913 /*
3914 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3915 * (don't ask me who thought of this...). MTD assumes that these
3916 * dimensions will be power-of-2, so just truncate the remaining area.
3917 */
3918 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3919 mtd->erasesize *= mtd->writesize;
3920
Florian Fainellic98a9352011-02-25 00:01:34 +00003921 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003922
3923 /* See erasesize comment */
3924 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003925 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003926 chip->bits_per_cell = p->bits_per_cell;
3927
3928 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003929 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003930
3931 if (p->ecc_bits != 0xff) {
3932 chip->ecc_strength_ds = p->ecc_bits;
3933 chip->ecc_step_ds = 512;
3934 } else if (chip->onfi_version >= 21 &&
3935 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3936
3937 /*
3938 * The nand_flash_detect_ext_param_page() uses the
3939 * Change Read Column command which maybe not supported
3940 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3941 * now. We do not replace user supplied command function.
3942 */
3943 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3944 chip->cmdfunc = nand_command_lp;
3945
3946 /* The Extended Parameter Page is supported since ONFI 2.1. */
3947 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3948 pr_warn("Failed to detect ONFI extended param page\n");
3949 } else {
3950 pr_warn("Could not retrieve ONFI ECC requirements\n");
3951 }
3952
Florian Fainellic98a9352011-02-25 00:01:34 +00003953 return 1;
3954}
3955#else
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003956static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003957{
3958 return 0;
3959}
3960#endif
3961
William Juul52c07962007-10-31 13:53:06 +01003962/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003963 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3964 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003965static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip)
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003966{
3967 struct nand_jedec_params *p = &chip->jedec_params;
3968 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003969 char id[5];
3970 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003971
3972 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003973 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
3974 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003975 return 0;
3976
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003977 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
3978 if (ret)
3979 return 0;
3980
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003981 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003982 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3983 if (ret)
3984 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003985
3986 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3987 le16_to_cpu(p->crc))
3988 break;
3989 }
3990
3991 if (i == 3) {
3992 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3993 return 0;
3994 }
3995
3996 /* Check version */
3997 val = le16_to_cpu(p->revision);
3998 if (val & (1 << 2))
3999 chip->jedec_version = 10;
4000 else if (val & (1 << 1))
4001 chip->jedec_version = 1; /* vendor specific version */
4002
4003 if (!chip->jedec_version) {
4004 pr_info("unsupported JEDEC version: %d\n", val);
4005 return 0;
4006 }
4007
4008 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4009 sanitize_string(p->model, sizeof(p->model));
4010 if (!mtd->name)
4011 mtd->name = p->model;
4012
4013 mtd->writesize = le32_to_cpu(p->byte_per_page);
4014
4015 /* Please reference to the comment for nand_flash_detect_onfi. */
4016 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4017 mtd->erasesize *= mtd->writesize;
4018
4019 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4020
4021 /* Please reference to the comment for nand_flash_detect_onfi. */
4022 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4023 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4024 chip->bits_per_cell = p->bits_per_cell;
4025
4026 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004027 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004028
4029 /* ECC info */
4030 ecc = &p->ecc_info[0];
4031
4032 if (ecc->codeword_size >= 9) {
4033 chip->ecc_strength_ds = ecc->ecc_bits;
4034 chip->ecc_step_ds = 1 << ecc->codeword_size;
4035 } else {
4036 pr_warn("Invalid codeword size\n");
4037 }
4038
4039 return 1;
4040}
4041
4042/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004043 * nand_id_has_period - Check if an ID string has a given wraparound period
4044 * @id_data: the ID string
4045 * @arrlen: the length of the @id_data array
4046 * @period: the period of repitition
4047 *
4048 * Check if an ID string is repeated within a given sequence of bytes at
4049 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02004050 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00004051 * if the repetition has a period of @period; otherwise, returns zero.
4052 */
4053static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4054{
4055 int i, j;
4056 for (i = 0; i < period; i++)
4057 for (j = i + period; j < arrlen; j += period)
4058 if (id_data[i] != id_data[j])
4059 return 0;
4060 return 1;
4061}
4062
4063/*
4064 * nand_id_len - Get the length of an ID string returned by CMD_READID
4065 * @id_data: the ID string
4066 * @arrlen: the length of the @id_data array
4067
4068 * Returns the length of the ID string, according to known wraparound/trailing
4069 * zero patterns. If no pattern exists, returns the length of the array.
4070 */
4071static int nand_id_len(u8 *id_data, int arrlen)
4072{
4073 int last_nonzero, period;
4074
4075 /* Find last non-zero byte */
4076 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4077 if (id_data[last_nonzero])
4078 break;
4079
4080 /* All zeros */
4081 if (last_nonzero < 0)
4082 return 0;
4083
4084 /* Calculate wraparound period */
4085 for (period = 1; period < arrlen; period++)
4086 if (nand_id_has_period(id_data, arrlen, period))
4087 break;
4088
4089 /* There's a repeated pattern */
4090 if (period < arrlen)
4091 return period;
4092
4093 /* There are trailing zeros */
4094 if (last_nonzero < arrlen - 1)
4095 return last_nonzero + 1;
4096
4097 /* No pattern detected */
4098 return arrlen;
4099}
4100
Heiko Schocherf5895d12014-06-24 10:10:04 +02004101/* Extract the bits of per cell from the 3rd byte of the extended ID */
4102static int nand_get_bits_per_cell(u8 cellinfo)
4103{
4104 int bits;
4105
4106 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4107 bits >>= NAND_CI_CELLTYPE_SHIFT;
4108 return bits + 1;
4109}
4110
Sergey Lapin3a38a552013-01-14 03:46:50 +00004111/*
4112 * Many new NAND share similar device ID codes, which represent the size of the
4113 * chip. The rest of the parameters must be decoded according to generic or
4114 * manufacturer-specific "extended ID" decoding patterns.
4115 */
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004116void nand_decode_ext_id(struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004117{
Alexander Dahlaa124532024-03-20 10:02:09 +01004118 struct mtd_info *mtd = nand_to_mtd(chip);
Michael Trimarchi3ba671b2022-07-20 18:22:11 +02004119 int extid;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004120 /* The 3rd id byte holds MLC / multichip data */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004121 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004122 /* The 4th id byte is the important one */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004123 extid = chip->id.data[3];
Sergey Lapin3a38a552013-01-14 03:46:50 +00004124
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004125 /* Calc pagesize */
4126 mtd->writesize = 1024 << (extid & 0x03);
4127 extid >>= 2;
4128 /* Calc oobsize */
4129 mtd->oobsize = (8 << (extid & 0x01)) *
4130 (mtd->writesize >> 9);
4131 extid >>= 2;
4132 /* Calc blocksize. Blocksize is multiples of 64KiB */
4133 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4134 extid >>= 2;
4135 /* Get buswidth information */
4136 /* Get buswidth information */
4137 if (extid & 0x1)
4138 chip->options |= NAND_BUSWIDTH_16;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004139}
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004140EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004141
Heiko Schocherf5895d12014-06-24 10:10:04 +02004142/*
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004143 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4144 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4145 * table.
4146 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004147static void nand_manufacturer_detect(struct nand_chip *chip)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004148{
4149 /*
4150 * Try manufacturer detection if available and use
4151 * nand_decode_ext_id() otherwise.
4152 */
4153 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004154 chip->manufacturer.desc->ops->detect) {
4155 /* The 3rd id byte holds MLC / multichip data */
4156 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004157 chip->manufacturer.desc->ops->detect(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004158 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004159 nand_decode_ext_id(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004160 }
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004161}
4162
4163/*
4164 * Manufacturer initialization. This function is called for all NANDs including
4165 * ONFI and JEDEC compliant ones.
4166 * Manufacturer drivers should put all their specific initialization code in
4167 * their ->init() hook.
4168 */
4169static int nand_manufacturer_init(struct nand_chip *chip)
4170{
4171 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4172 !chip->manufacturer.desc->ops->init)
4173 return 0;
4174
4175 return chip->manufacturer.desc->ops->init(chip);
4176}
4177
4178/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004179 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4180 * decodes a matching ID table entry and assigns the MTD size parameters for
4181 * the chip.
4182 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004183static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004184{
Alexander Dahlaa124532024-03-20 10:02:09 +01004185 struct mtd_info *mtd = nand_to_mtd(chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004186
4187 mtd->erasesize = type->erasesize;
4188 mtd->writesize = type->pagesize;
4189 mtd->oobsize = mtd->writesize / 32;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004190
Heiko Schocherf5895d12014-06-24 10:10:04 +02004191 /* All legacy ID NAND are small-page, SLC */
4192 chip->bits_per_cell = 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004193}
4194
Heiko Schocherf5895d12014-06-24 10:10:04 +02004195/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004196 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4197 * heuristic patterns using various detected parameters (e.g., manufacturer,
4198 * page size, cell-type information).
4199 */
4200static void nand_decode_bbm_options(struct mtd_info *mtd,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004201 struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004202{
Sergey Lapin3a38a552013-01-14 03:46:50 +00004203 /* Set the bad block position */
4204 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4205 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4206 else
4207 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004208}
4209
Heiko Schocherf5895d12014-06-24 10:10:04 +02004210static inline bool is_full_id_nand(struct nand_flash_dev *type)
4211{
4212 return type->id_len;
4213}
4214
4215static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004216 struct nand_flash_dev *type)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004217{
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004218 if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004219 mtd->writesize = type->pagesize;
4220 mtd->erasesize = type->erasesize;
4221 mtd->oobsize = type->oobsize;
4222
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004223 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004224 chip->chipsize = (uint64_t)type->chipsize << 20;
4225 chip->options |= type->options;
4226 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4227 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004228 chip->onfi_timing_mode_default =
4229 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004230
Heiko Schocherf5895d12014-06-24 10:10:04 +02004231 if (!mtd->name)
4232 mtd->name = type->name;
4233
4234 return true;
4235 }
4236 return false;
4237}
4238
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004239/**
4240 * nand_get_manufacturer_desc - Get manufacturer information from the
4241 * manufacturer ID
4242 * @id: manufacturer ID
4243 *
4244 * Returns a nand_manufacturer_desc object if the manufacturer is defined
4245 * in the NAND manufacturers database, NULL otherwise.
4246 */
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004247static const struct nand_manufacturer *nand_get_manufacturer_desc(u8 id)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004248{
4249 int i;
4250
4251 for (i = 0; nand_manuf_ids[i].id != 0x0; i++) {
4252 if (nand_manuf_ids[i].id == id)
4253 return &nand_manuf_ids[i];
4254 }
4255
4256 return NULL;
4257}
4258
Sergey Lapin3a38a552013-01-14 03:46:50 +00004259/*
4260 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004261 */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004262int nand_detect(struct nand_chip *chip, int *maf_id,
4263 int *dev_id, struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004264{
Alexander Dahlaa124532024-03-20 10:02:09 +01004265 struct mtd_info *mtd = nand_to_mtd(chip);
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004266 const struct nand_manufacturer *manufacturer_desc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004267 int busw, ret;
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004268 u8 *id_data = chip->id.data;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004269
Karl Beldanb6322fc2008-09-15 16:08:03 +02004270 /*
4271 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004272 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004273 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004274 ret = nand_reset(chip, 0);
4275 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004276 return ret;
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004277
4278 /* Select the device */
4279 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004280
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004281 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004282 ret = nand_readid_op(chip, 0, id_data, 2);
4283 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004284 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004285
4286 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004287 *maf_id = id_data[0];
4288 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004289
Sergey Lapin3a38a552013-01-14 03:46:50 +00004290 /*
4291 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004292 * interface concerns can cause random data which looks like a
4293 * possibly credible NAND flash to appear. If the two results do
4294 * not match, ignore the device completely.
4295 */
4296
Sergey Lapin3a38a552013-01-14 03:46:50 +00004297 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004298 ret = nand_readid_op(chip, 0, id_data, 8);
4299 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004300 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05004301
Christian Hitzb8a6b372011-10-12 09:32:02 +02004302 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004303 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004304 *maf_id, *dev_id, id_data[0], id_data[1]);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004305 return -ENODEV;
Scott Wood3628f002008-10-24 16:20:43 -05004306 }
4307
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004308 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
4309
4310 /* Try to identify manufacturer */
4311 manufacturer_desc = nand_get_manufacturer_desc(*maf_id);
4312 chip->manufacturer.desc = manufacturer_desc;
4313
Lei Wen75bde942011-01-06 09:48:18 +08004314 if (!type)
4315 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004316
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004317 /*
4318 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4319 * override it.
4320 * This is required to make sure initial NAND bus width set by the
4321 * NAND controller driver is coherent with the real NAND bus width
4322 * (extracted by auto-detection code).
4323 */
4324 busw = chip->options & NAND_BUSWIDTH_16;
4325
4326 /*
4327 * The flag is only set (never cleared), reset it to its default value
4328 * before starting auto-detection.
4329 */
4330 chip->options &= ~NAND_BUSWIDTH_16;
4331
Heiko Schocherf5895d12014-06-24 10:10:04 +02004332 for (; type->name != NULL; type++) {
4333 if (is_full_id_nand(type)) {
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004334 if (find_full_id_nand(mtd, chip, type))
Heiko Schocherf5895d12014-06-24 10:10:04 +02004335 goto ident_done;
4336 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004337 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004338 }
4339 }
Lei Wen75bde942011-01-06 09:48:18 +08004340
Christian Hitzb8a6b372011-10-12 09:32:02 +02004341 chip->onfi_version = 0;
4342 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004343 /* Check if the chip is ONFI compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004344 if (nand_flash_detect_onfi(mtd, chip))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004345 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004346
4347 /* Check if the chip is JEDEC compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004348 if (nand_flash_detect_jedec(mtd, chip))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004349 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004350 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004351
Christian Hitzb8a6b372011-10-12 09:32:02 +02004352 if (!type->name)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004353 return -ENODEV;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004354
William Juul52c07962007-10-31 13:53:06 +01004355 if (!mtd->name)
4356 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004357
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004358 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004359
Scott Wood52ab7ce2016-05-30 13:57:58 -05004360 if (!type->pagesize) {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004361 nand_manufacturer_detect(chip);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004362 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004363 nand_decode_id(chip, type);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004364 }
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004365
Heiko Schocherf5895d12014-06-24 10:10:04 +02004366 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004367 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004368
Christian Hitzb8a6b372011-10-12 09:32:02 +02004369ident_done:
4370
Heiko Schocherf5895d12014-06-24 10:10:04 +02004371 if (chip->options & NAND_BUSWIDTH_AUTO) {
4372 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4373 chip->options |= busw;
4374 nand_set_defaults(chip, busw);
4375 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4376 /*
4377 * Check, if buswidth is correct. Hardware drivers should set
4378 * chip correct!
4379 */
4380 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4381 *maf_id, *dev_id);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004382 pr_info("%s %s\n", manufacturer_desc->name, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004383 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004384 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4385 busw ? 16 : 8);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004386 return -EINVAL;
William Juul52c07962007-10-31 13:53:06 +01004387 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004388
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004389 nand_decode_bbm_options(mtd, chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004390
William Juul52c07962007-10-31 13:53:06 +01004391 /* Calculate the address shift from the page size */
4392 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004393 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004394 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004395
William Juul52c07962007-10-31 13:53:06 +01004396 chip->bbt_erase_shift = chip->phys_erase_shift =
4397 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004398 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004399 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004400 else {
4401 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4402 chip->chip_shift += 32 - 1;
4403 }
4404
Masahiro Yamada984926b2017-11-22 02:38:31 +09004405 if (chip->chip_shift - chip->page_shift > 16)
4406 chip->options |= NAND_ROW_ADDR_3;
4407
Christian Hitzb8a6b372011-10-12 09:32:02 +02004408 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004409 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004410
Sergey Lapin3a38a552013-01-14 03:46:50 +00004411 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004412 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4413 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004414
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004415 ret = nand_manufacturer_init(chip);
4416 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004417 return ret;
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004418
Heiko Schocherf5895d12014-06-24 10:10:04 +02004419 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4420 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004421
Christian Hitzb8a6b372011-10-12 09:32:02 +02004422#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004423 if (chip->onfi_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004424 pr_info("%s %s\n", manufacturer_desc->name,
4425 chip->onfi_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004426 else if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004427 pr_info("%s %s\n", manufacturer_desc->name,
4428 chip->jedec_params.model);
Sean Andersone476f112023-11-04 16:37:40 -04004429 else if (manufacturer_desc)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004430 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004431#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004432 if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004433 pr_info("%s %s\n", manufacturer_desc->name,
4434 chip->jedec_params.model);
Sean Andersone476f112023-11-04 16:37:40 -04004435 else if (manufacturer_desc)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004436 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004437#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004438
Scott Wood3ea94ed2015-06-26 19:03:26 -05004439 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004440 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004441 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004442 return 0;
William Juul52c07962007-10-31 13:53:06 +01004443}
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004444EXPORT_SYMBOL(nand_detect);
William Juul52c07962007-10-31 13:53:06 +01004445
Brian Norrisba6463d2016-06-15 21:09:22 +02004446#if CONFIG_IS_ENABLED(OF_CONTROL)
Brian Norrisba6463d2016-06-15 21:09:22 +02004447
Patrice Chotardbc77af52021-09-13 16:25:53 +02004448static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004449{
4450 int ret, ecc_mode = -1, ecc_strength, ecc_step;
Linus Walleij4e8611a2023-04-07 15:40:05 +02004451 int ecc_algo = NAND_ECC_UNKNOWN;
Brian Norrisba6463d2016-06-15 21:09:22 +02004452 const char *str;
4453
Patrice Chotardbc77af52021-09-13 16:25:53 +02004454 ret = ofnode_read_s32_default(node, "nand-bus-width", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004455 if (ret == 16)
4456 chip->options |= NAND_BUSWIDTH_16;
4457
Arseniy Krasnov8b4a27a2024-08-26 16:17:08 +03004458 if (ofnode_read_bool(node, "nand-is-boot-medium"))
4459 chip->options |= NAND_IS_BOOT_MEDIUM;
4460
Patrice Chotardbc77af52021-09-13 16:25:53 +02004461 if (ofnode_read_bool(node, "nand-on-flash-bbt"))
Brian Norrisba6463d2016-06-15 21:09:22 +02004462 chip->bbt_options |= NAND_BBT_USE_FLASH;
4463
Patrice Chotardbc77af52021-09-13 16:25:53 +02004464 str = ofnode_read_string(node, "nand-ecc-mode");
Brian Norrisba6463d2016-06-15 21:09:22 +02004465 if (str) {
4466 if (!strcmp(str, "none"))
4467 ecc_mode = NAND_ECC_NONE;
4468 else if (!strcmp(str, "soft"))
4469 ecc_mode = NAND_ECC_SOFT;
4470 else if (!strcmp(str, "hw"))
4471 ecc_mode = NAND_ECC_HW;
4472 else if (!strcmp(str, "hw_syndrome"))
4473 ecc_mode = NAND_ECC_HW_SYNDROME;
4474 else if (!strcmp(str, "hw_oob_first"))
4475 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4476 else if (!strcmp(str, "soft_bch"))
4477 ecc_mode = NAND_ECC_SOFT_BCH;
4478 }
4479
Linus Walleij4e8611a2023-04-07 15:40:05 +02004480 str = ofnode_read_string(node, "nand-ecc-algo");
4481 if (str) {
4482 /*
4483 * If we are in NAND_ECC_SOFT mode, just alter the
4484 * soft mode to BCH here. No change of algorithm.
4485 */
4486 if (ecc_mode == NAND_ECC_SOFT) {
4487 if (!strcmp(str, "bch"))
4488 ecc_mode = NAND_ECC_SOFT_BCH;
4489 } else {
4490 if (!strcmp(str, "bch")) {
4491 ecc_algo = NAND_ECC_BCH;
4492 } else if (!strcmp(str, "hamming")) {
4493 ecc_algo = NAND_ECC_HAMMING;
4494 }
4495 }
Pali Rohár44acf8a2022-04-04 18:17:21 +02004496 }
4497
Patrice Chotardbc77af52021-09-13 16:25:53 +02004498 ecc_strength = ofnode_read_s32_default(node,
4499 "nand-ecc-strength", -1);
4500 ecc_step = ofnode_read_s32_default(node,
4501 "nand-ecc-step-size", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004502
4503 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4504 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4505 pr_err("must set both strength and step size in DT\n");
4506 return -EINVAL;
4507 }
4508
Linus Walleij4e8611a2023-04-07 15:40:05 +02004509 /*
4510 * Chip drivers may have assigned default algorithms here,
4511 * onlt override it if we have found something explicitly
4512 * specified in the device tree.
4513 */
4514 if (ecc_algo != NAND_ECC_UNKNOWN)
4515 chip->ecc.algo = ecc_algo;
4516
Brian Norrisba6463d2016-06-15 21:09:22 +02004517 if (ecc_mode >= 0)
4518 chip->ecc.mode = ecc_mode;
4519
4520 if (ecc_strength >= 0)
4521 chip->ecc.strength = ecc_strength;
4522
4523 if (ecc_step > 0)
4524 chip->ecc.size = ecc_step;
4525
Patrice Chotardbc77af52021-09-13 16:25:53 +02004526 if (ofnode_read_bool(node, "nand-ecc-maximize"))
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004527 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4528
Brian Norrisba6463d2016-06-15 21:09:22 +02004529 return 0;
4530}
4531#else
Patrice Chotardbc77af52021-09-13 16:25:53 +02004532static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004533{
4534 return 0;
4535}
4536#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4537
William Juul52c07962007-10-31 13:53:06 +01004538/**
4539 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004540 * @mtd: MTD device structure
4541 * @maxchips: number of chips to scan for
4542 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004543 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004544 * This is the first phase of the normal nand_scan() function. It reads the
4545 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004546 *
William Juul52c07962007-10-31 13:53:06 +01004547 */
Lei Wen75bde942011-01-06 09:48:18 +08004548int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004549 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004550{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004551 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004552 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba6463d2016-06-15 21:09:22 +02004553 int ret;
4554
Patrice Chotardbc77af52021-09-13 16:25:53 +02004555 if (ofnode_valid(chip->flash_node)) {
Brian Norrisba6463d2016-06-15 21:09:22 +02004556 ret = nand_dt_init(mtd, chip, chip->flash_node);
4557 if (ret)
4558 return ret;
4559 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004560
William Juul52c07962007-10-31 13:53:06 +01004561 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004562 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004563
4564 /* Read the flash type */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004565 ret = nand_detect(chip, &nand_maf_id, &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004566
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004567 if (ret) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004568 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4569 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004570 chip->select_chip(mtd, -1);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004571 return ret;
William Juul52c07962007-10-31 13:53:06 +01004572 }
4573
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004574 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004575 ret = nand_init_data_interface(chip);
4576 if (ret)
4577 return ret;
4578
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004579 /*
4580 * Setup the data interface correctly on the chip and controller side.
4581 * This explicit call to nand_setup_data_interface() is only required
4582 * for the first die, because nand_reset() has been called before
4583 * ->data_interface and ->default_onfi_timing_mode were set.
4584 * For the other dies, nand_reset() will automatically switch to the
4585 * best mode for us.
4586 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004587 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004588 if (ret)
4589 return ret;
4590
Heiko Schocherf5895d12014-06-24 10:10:04 +02004591 chip->select_chip(mtd, -1);
4592
William Juul52c07962007-10-31 13:53:06 +01004593 /* Check for a chip array */
4594 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004595 u8 id[2];
4596
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004597 /* See comment in nand_detect for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004598 nand_reset(chip, i);
4599
4600 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004601 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004602 nand_readid_op(chip, 0, id, sizeof(id));
4603
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004604 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004605 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004606 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004607 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004608 }
4609 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004610 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004611
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004612#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004613 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004614 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004615#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004616
William Juul52c07962007-10-31 13:53:06 +01004617 /* Store the number of chips and calc total size for mtd */
4618 chip->numchips = i;
4619 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004620
William Juul52c07962007-10-31 13:53:06 +01004621 return 0;
4622}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004623EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004624
Masahiro Yamada820eb482017-11-22 02:38:29 +09004625/**
4626 * nand_check_ecc_caps - check the sanity of preset ECC settings
4627 * @chip: nand chip info structure
4628 * @caps: ECC caps info structure
4629 * @oobavail: OOB size that the ECC engine can use
4630 *
4631 * When ECC step size and strength are already set, check if they are supported
4632 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4633 * On success, the calculated ECC bytes is set.
4634 */
4635int nand_check_ecc_caps(struct nand_chip *chip,
4636 const struct nand_ecc_caps *caps, int oobavail)
4637{
4638 struct mtd_info *mtd = nand_to_mtd(chip);
4639 const struct nand_ecc_step_info *stepinfo;
4640 int preset_step = chip->ecc.size;
4641 int preset_strength = chip->ecc.strength;
4642 int nsteps, ecc_bytes;
4643 int i, j;
4644
4645 if (WARN_ON(oobavail < 0))
4646 return -EINVAL;
4647
4648 if (!preset_step || !preset_strength)
4649 return -ENODATA;
4650
4651 nsteps = mtd->writesize / preset_step;
4652
4653 for (i = 0; i < caps->nstepinfos; i++) {
4654 stepinfo = &caps->stepinfos[i];
4655
4656 if (stepinfo->stepsize != preset_step)
4657 continue;
4658
4659 for (j = 0; j < stepinfo->nstrengths; j++) {
4660 if (stepinfo->strengths[j] != preset_strength)
4661 continue;
4662
4663 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4664 preset_strength);
4665 if (WARN_ON_ONCE(ecc_bytes < 0))
4666 return ecc_bytes;
4667
4668 if (ecc_bytes * nsteps > oobavail) {
4669 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4670 preset_step, preset_strength);
4671 return -ENOSPC;
4672 }
4673
4674 chip->ecc.bytes = ecc_bytes;
4675
4676 return 0;
4677 }
4678 }
4679
4680 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4681 preset_step, preset_strength);
4682
4683 return -ENOTSUPP;
4684}
4685EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4686
4687/**
4688 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4689 * @chip: nand chip info structure
4690 * @caps: ECC engine caps info structure
4691 * @oobavail: OOB size that the ECC engine can use
4692 *
4693 * If a chip's ECC requirement is provided, try to meet it with the least
4694 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4695 * On success, the chosen ECC settings are set.
4696 */
4697int nand_match_ecc_req(struct nand_chip *chip,
4698 const struct nand_ecc_caps *caps, int oobavail)
4699{
4700 struct mtd_info *mtd = nand_to_mtd(chip);
4701 const struct nand_ecc_step_info *stepinfo;
4702 int req_step = chip->ecc_step_ds;
4703 int req_strength = chip->ecc_strength_ds;
4704 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4705 int best_step, best_strength, best_ecc_bytes;
4706 int best_ecc_bytes_total = INT_MAX;
4707 int i, j;
4708
4709 if (WARN_ON(oobavail < 0))
4710 return -EINVAL;
4711
4712 /* No information provided by the NAND chip */
4713 if (!req_step || !req_strength)
4714 return -ENOTSUPP;
4715
4716 /* number of correctable bits the chip requires in a page */
4717 req_corr = mtd->writesize / req_step * req_strength;
4718
4719 for (i = 0; i < caps->nstepinfos; i++) {
4720 stepinfo = &caps->stepinfos[i];
4721 step_size = stepinfo->stepsize;
4722
4723 for (j = 0; j < stepinfo->nstrengths; j++) {
4724 strength = stepinfo->strengths[j];
4725
4726 /*
4727 * If both step size and strength are smaller than the
4728 * chip's requirement, it is not easy to compare the
4729 * resulted reliability.
4730 */
4731 if (step_size < req_step && strength < req_strength)
4732 continue;
4733
4734 if (mtd->writesize % step_size)
4735 continue;
4736
4737 nsteps = mtd->writesize / step_size;
4738
4739 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4740 if (WARN_ON_ONCE(ecc_bytes < 0))
4741 continue;
4742 ecc_bytes_total = ecc_bytes * nsteps;
4743
4744 if (ecc_bytes_total > oobavail ||
4745 strength * nsteps < req_corr)
4746 continue;
4747
4748 /*
4749 * We assume the best is to meet the chip's requrement
4750 * with the least number of ECC bytes.
4751 */
4752 if (ecc_bytes_total < best_ecc_bytes_total) {
4753 best_ecc_bytes_total = ecc_bytes_total;
4754 best_step = step_size;
4755 best_strength = strength;
4756 best_ecc_bytes = ecc_bytes;
4757 }
4758 }
4759 }
4760
4761 if (best_ecc_bytes_total == INT_MAX)
4762 return -ENOTSUPP;
4763
4764 chip->ecc.size = best_step;
4765 chip->ecc.strength = best_strength;
4766 chip->ecc.bytes = best_ecc_bytes;
4767
4768 return 0;
4769}
4770EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4771
4772/**
4773 * nand_maximize_ecc - choose the max ECC strength available
4774 * @chip: nand chip info structure
4775 * @caps: ECC engine caps info structure
4776 * @oobavail: OOB size that the ECC engine can use
4777 *
4778 * Choose the max ECC strength that is supported on the controller, and can fit
4779 * within the chip's OOB. On success, the chosen ECC settings are set.
4780 */
4781int nand_maximize_ecc(struct nand_chip *chip,
4782 const struct nand_ecc_caps *caps, int oobavail)
4783{
4784 struct mtd_info *mtd = nand_to_mtd(chip);
4785 const struct nand_ecc_step_info *stepinfo;
4786 int step_size, strength, nsteps, ecc_bytes, corr;
4787 int best_corr = 0;
4788 int best_step = 0;
4789 int best_strength, best_ecc_bytes;
4790 int i, j;
4791
4792 if (WARN_ON(oobavail < 0))
4793 return -EINVAL;
4794
4795 for (i = 0; i < caps->nstepinfos; i++) {
4796 stepinfo = &caps->stepinfos[i];
4797 step_size = stepinfo->stepsize;
4798
4799 /* If chip->ecc.size is already set, respect it */
4800 if (chip->ecc.size && step_size != chip->ecc.size)
4801 continue;
4802
4803 for (j = 0; j < stepinfo->nstrengths; j++) {
4804 strength = stepinfo->strengths[j];
4805
4806 if (mtd->writesize % step_size)
4807 continue;
4808
4809 nsteps = mtd->writesize / step_size;
4810
4811 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4812 if (WARN_ON_ONCE(ecc_bytes < 0))
4813 continue;
4814
4815 if (ecc_bytes * nsteps > oobavail)
4816 continue;
4817
4818 corr = strength * nsteps;
4819
4820 /*
4821 * If the number of correctable bits is the same,
4822 * bigger step_size has more reliability.
4823 */
4824 if (corr > best_corr ||
4825 (corr == best_corr && step_size > best_step)) {
4826 best_corr = corr;
4827 best_step = step_size;
4828 best_strength = strength;
4829 best_ecc_bytes = ecc_bytes;
4830 }
4831 }
4832 }
4833
4834 if (!best_corr)
4835 return -ENOTSUPP;
4836
4837 chip->ecc.size = best_step;
4838 chip->ecc.strength = best_strength;
4839 chip->ecc.bytes = best_ecc_bytes;
4840
4841 return 0;
4842}
4843EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4844
Scott Wood3ea94ed2015-06-26 19:03:26 -05004845/*
4846 * Check if the chip configuration meet the datasheet requirements.
4847
4848 * If our configuration corrects A bits per B bytes and the minimum
4849 * required correction level is X bits per Y bytes, then we must ensure
4850 * both of the following are true:
4851 *
4852 * (1) A / B >= X / Y
4853 * (2) A >= X
4854 *
4855 * Requirement (1) ensures we can correct for the required bitflip density.
4856 * Requirement (2) ensures we can correct even when all bitflips are clumped
4857 * in the same sector.
4858 */
4859static bool nand_ecc_strength_good(struct mtd_info *mtd)
4860{
Scott Wood17fed142016-05-30 13:57:56 -05004861 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004862 struct nand_ecc_ctrl *ecc = &chip->ecc;
4863 int corr, ds_corr;
4864
4865 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4866 /* Not enough information */
4867 return true;
4868
4869 /*
4870 * We get the number of corrected bits per page to compare
4871 * the correction density.
4872 */
4873 corr = (mtd->writesize * ecc->strength) / ecc->size;
4874 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4875
4876 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4877}
William Juul52c07962007-10-31 13:53:06 +01004878
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004879static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4880{
4881 struct nand_ecc_ctrl *ecc = &chip->ecc;
4882
4883 if (nand_standard_page_accessors(ecc))
4884 return false;
4885
4886 /*
4887 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4888 * controller driver implements all the page accessors because
4889 * default helpers are not suitable when the core does not
4890 * send the READ0/PAGEPROG commands.
4891 */
4892 return (!ecc->read_page || !ecc->write_page ||
4893 !ecc->read_page_raw || !ecc->write_page_raw ||
4894 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4895 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4896 ecc->hwctl && ecc->calculate));
4897}
4898
William Juul52c07962007-10-31 13:53:06 +01004899/**
4900 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004901 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004902 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004903 * This is the second phase of the normal nand_scan() function. It fills out
4904 * all the uninitialized function pointers with the defaults and scans for a
4905 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004906 */
4907int nand_scan_tail(struct mtd_info *mtd)
4908{
4909 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004910 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004911 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004912 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004913
Sergey Lapin3a38a552013-01-14 03:46:50 +00004914 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4915 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4916 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4917
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004918 if (invalid_ecc_page_accessors(chip)) {
4919 pr_err("Invalid ECC page accessors setup\n");
4920 return -EINVAL;
4921 }
4922
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004923 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004924 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004925 chip->buffers = nbuf;
4926 } else {
4927 if (!chip->buffers)
4928 return -ENOMEM;
4929 }
William Juul52c07962007-10-31 13:53:06 +01004930
4931 /* Set the internal oob buffer location, just after the page data */
4932 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4933
4934 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004935 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004936 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004937 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004938 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02004939#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004940 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004941 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004942 break;
4943 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004944 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004945 break;
4946 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004947 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004948 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004949 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004950 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004951 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01004952#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004953 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004954 pr_warn("No oob scheme defined for oobsize %d\n",
4955 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004956 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004957 }
4958 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004959
William Juul52c07962007-10-31 13:53:06 +01004960 if (!chip->write_page)
4961 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004962
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004963 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004964 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01004965 * selected and we have 256 byte pagesize fallback to software ECC
4966 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004967
Heiko Schocherf5895d12014-06-24 10:10:04 +02004968 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004969 case NAND_ECC_HW_OOB_FIRST:
4970 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004971 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004972 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004973 BUG();
4974 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004975 if (!ecc->read_page)
4976 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004977
Andre Przywaraa50e5c62025-03-27 15:33:10 +00004978 fallthrough;
William Juul52c07962007-10-31 13:53:06 +01004979 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004980 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004981 if (!ecc->read_page)
4982 ecc->read_page = nand_read_page_hwecc;
4983 if (!ecc->write_page)
4984 ecc->write_page = nand_write_page_hwecc;
4985 if (!ecc->read_page_raw)
4986 ecc->read_page_raw = nand_read_page_raw;
4987 if (!ecc->write_page_raw)
4988 ecc->write_page_raw = nand_write_page_raw;
4989 if (!ecc->read_oob)
4990 ecc->read_oob = nand_read_oob_std;
4991 if (!ecc->write_oob)
4992 ecc->write_oob = nand_write_oob_std;
4993 if (!ecc->read_subpage)
4994 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05004995 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004996 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004997
Andre Przywaraa50e5c62025-03-27 15:33:10 +00004998 fallthrough;
William Juul52c07962007-10-31 13:53:06 +01004999 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005000 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5001 (!ecc->read_page ||
5002 ecc->read_page == nand_read_page_hwecc ||
5003 !ecc->write_page ||
5004 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005005 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005006 BUG();
5007 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005008 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005009 if (!ecc->read_page)
5010 ecc->read_page = nand_read_page_syndrome;
5011 if (!ecc->write_page)
5012 ecc->write_page = nand_write_page_syndrome;
5013 if (!ecc->read_page_raw)
5014 ecc->read_page_raw = nand_read_page_raw_syndrome;
5015 if (!ecc->write_page_raw)
5016 ecc->write_page_raw = nand_write_page_raw_syndrome;
5017 if (!ecc->read_oob)
5018 ecc->read_oob = nand_read_oob_syndrome;
5019 if (!ecc->write_oob)
5020 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005021
Heiko Schocherf5895d12014-06-24 10:10:04 +02005022 if (mtd->writesize >= ecc->size) {
5023 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005024 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5025 BUG();
5026 }
William Juul52c07962007-10-31 13:53:06 +01005027 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005028 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005029 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5030 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005031 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005032
Andre Przywaraa50e5c62025-03-27 15:33:10 +00005033 fallthrough;
William Juul52c07962007-10-31 13:53:06 +01005034 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005035 ecc->calculate = nand_calculate_ecc;
5036 ecc->correct = nand_correct_data;
5037 ecc->read_page = nand_read_page_swecc;
5038 ecc->read_subpage = nand_read_subpage;
5039 ecc->write_page = nand_write_page_swecc;
5040 ecc->read_page_raw = nand_read_page_raw;
5041 ecc->write_page_raw = nand_write_page_raw;
5042 ecc->read_oob = nand_read_oob_std;
5043 ecc->write_oob = nand_write_oob_std;
5044 if (!ecc->size)
5045 ecc->size = 256;
5046 ecc->bytes = 3;
5047 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005048 break;
5049
Christian Hitz55f7bca2011-10-12 09:31:59 +02005050 case NAND_ECC_SOFT_BCH:
5051 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005052 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005053 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005054 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005055 ecc->calculate = nand_bch_calculate_ecc;
5056 ecc->correct = nand_bch_correct_data;
5057 ecc->read_page = nand_read_page_swecc;
5058 ecc->read_subpage = nand_read_subpage;
5059 ecc->write_page = nand_write_page_swecc;
5060 ecc->read_page_raw = nand_read_page_raw;
5061 ecc->write_page_raw = nand_write_page_raw;
5062 ecc->read_oob = nand_read_oob_std;
5063 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005064 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005065 * Board driver should supply ecc.size and ecc.strength values
5066 * to select how many bits are correctable. Otherwise, default
5067 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005068 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005069 if (!ecc->size && (mtd->oobsize >= 64)) {
5070 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005071 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005072 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005073
5074 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005075 ecc->bytes = 0;
5076 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005077 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005078 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005079 BUG();
5080 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005081 break;
5082
William Juul52c07962007-10-31 13:53:06 +01005083 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005084 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005085 ecc->read_page = nand_read_page_raw;
5086 ecc->write_page = nand_write_page_raw;
5087 ecc->read_oob = nand_read_oob_std;
5088 ecc->read_page_raw = nand_read_page_raw;
5089 ecc->write_page_raw = nand_write_page_raw;
5090 ecc->write_oob = nand_write_oob_std;
5091 ecc->size = mtd->writesize;
5092 ecc->bytes = 0;
5093 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005094 break;
5095
5096 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005097 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005098 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005099 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005100
Sergey Lapin3a38a552013-01-14 03:46:50 +00005101 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005102 if (!ecc->read_oob_raw)
5103 ecc->read_oob_raw = ecc->read_oob;
5104 if (!ecc->write_oob_raw)
5105 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005106
William Juul52c07962007-10-31 13:53:06 +01005107 /*
5108 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005109 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005110 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005111 mtd->oobavail = 0;
5112 if (ecc->layout) {
5113 for (i = 0; ecc->layout->oobfree[i].length; i++)
5114 mtd->oobavail += ecc->layout->oobfree[i].length;
5115 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005116
Scott Wood3ea94ed2015-06-26 19:03:26 -05005117 /* ECC sanity check: warn if it's too weak */
5118 if (!nand_ecc_strength_good(mtd))
5119 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5120 mtd->name);
5121
William Juul52c07962007-10-31 13:53:06 +01005122 /*
5123 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005124 * mode.
William Juul52c07962007-10-31 13:53:06 +01005125 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005126 ecc->steps = mtd->writesize / ecc->size;
5127 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005128 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005129 BUG();
5130 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005131 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005132
Sergey Lapin3a38a552013-01-14 03:46:50 +00005133 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005134 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5135 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005136 case 2:
5137 mtd->subpage_sft = 1;
5138 break;
5139 case 4:
5140 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005141 case 16:
William Juul52c07962007-10-31 13:53:06 +01005142 mtd->subpage_sft = 2;
5143 break;
5144 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005145 }
William Juul52c07962007-10-31 13:53:06 +01005146 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005147
William Juul52c07962007-10-31 13:53:06 +01005148 /* Initialize state */
5149 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005150
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005151 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005152 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005153
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005154 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005155 switch (ecc->mode) {
5156 case NAND_ECC_SOFT:
5157 case NAND_ECC_SOFT_BCH:
5158 if (chip->page_shift > 9)
5159 chip->options |= NAND_SUBPAGE_READ;
5160 break;
5161
5162 default:
5163 break;
5164 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005165
Patrice Chotardbee32872022-03-21 09:13:36 +01005166 mtd->flash_node = chip->flash_node;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005167 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005168 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005169 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5170 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005171 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005172 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005173 mtd->_read_oob = nand_read_oob;
5174 mtd->_write_oob = nand_write_oob;
5175 mtd->_sync = nand_sync;
5176 mtd->_lock = NULL;
5177 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005178 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005179 mtd->_block_isbad = nand_block_isbad;
5180 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005181 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005182
Sergey Lapin3a38a552013-01-14 03:46:50 +00005183 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005184 mtd->ecclayout = ecc->layout;
5185 mtd->ecc_strength = ecc->strength;
5186 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005187 /*
5188 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5189 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5190 * properly set.
5191 */
5192 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005193 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005194
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005195 return 0;
William Juul52c07962007-10-31 13:53:06 +01005196}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005197EXPORT_SYMBOL(nand_scan_tail);
5198
William Juul52c07962007-10-31 13:53:06 +01005199/**
5200 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005201 * @mtd: MTD device structure
5202 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005203 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005204 * This fills out all the uninitialized function pointers with the defaults.
5205 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005206 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005207 */
5208int nand_scan(struct mtd_info *mtd, int maxchips)
5209{
5210 int ret;
5211
Lei Wen75bde942011-01-06 09:48:18 +08005212 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005213 if (!ret)
5214 ret = nand_scan_tail(mtd);
5215 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005216}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005217EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005218
Heiko Schocherf5895d12014-06-24 10:10:04 +02005219MODULE_LICENSE("GPL");
5220MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5221MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5222MODULE_DESCRIPTION("Generic NAND flash driver code");