blob: 4401bdcdb90c3c4337eaa7916b3b5c084fa65dff [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;
417#ifndef CONFIG_SPL_BUILD
418 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
Roger Quadrosb590f612022-12-20 12:21:57 +0200437#ifndef CONFIG_SPL_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 */
Roger Quadrosb590f612022-12-20 12:21:57 +0200491#ifndef CONFIG_SPL_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 */
Roger Quadrosb590f612022-12-20 12:21:57 +0200521#ifndef CONFIG_SPL_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 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200777 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200778 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200779 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000780 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100781 */
782 if (!chip->dev_ready) {
783 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200784 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200785 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200786 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200787
Sergey Lapin3a38a552013-01-14 03:46:50 +0000788 /*
789 * Apply this short delay always to ensure that we do wait tWB in
790 * any case on any machine.
791 */
William Juul52c07962007-10-31 13:53:06 +0100792 ndelay(100);
793
794 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200795}
796
797/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200798 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000799 * @chip: the nand chip descriptor
800 * @mtd: MTD device structure
801 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200802 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200803 * Used when in panic, no locks are taken.
804 */
805static void panic_nand_get_device(struct nand_chip *chip,
806 struct mtd_info *mtd, int new_state)
807{
808 /* Hardware controller shared among independent devices */
809 chip->controller->active = chip;
810 chip->state = new_state;
811}
812
813/**
814 * nand_get_device - [GENERIC] Get chip for selected access
815 * @mtd: MTD device structure
816 * @new_state: the state which is requested
817 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200818 * Get the device and lock it for exclusive access
819 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200820static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200821nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100822{
Scott Wood17fed142016-05-30 13:57:56 -0500823 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200824 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100825 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200826}
827
828/**
829 * panic_nand_wait - [GENERIC] wait until the command is done
830 * @mtd: MTD device structure
831 * @chip: NAND chip structure
832 * @timeo: timeout
833 *
834 * Wait for command done. This is a helper function for nand_wait used when
835 * we are in interrupt context. May happen when in panic and trying to write
836 * an oops through mtdoops.
837 */
838static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
839 unsigned long timeo)
840{
841 int i;
842 for (i = 0; i < timeo; i++) {
843 if (chip->dev_ready) {
844 if (chip->dev_ready(mtd))
845 break;
846 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100847 int ret;
848 u8 status;
849
850 ret = nand_read_data_op(chip, &status, sizeof(status),
851 true);
852 if (ret)
853 return;
854
855 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200856 break;
857 }
858 mdelay(1);
859 }
William Juul52c07962007-10-31 13:53:06 +0100860}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200861
862/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000863 * nand_wait - [DEFAULT] wait until the command is done
864 * @mtd: MTD device structure
865 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200866 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500867 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100868 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200869static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200870{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500871 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100872 u8 status;
873 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100874
Heiko Schocherf5895d12014-06-24 10:10:04 +0200875 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100876
Heiko Schocherf5895d12014-06-24 10:10:04 +0200877 /*
878 * Apply this short delay always to ensure that we do wait tWB in any
879 * case on any machine.
880 */
881 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100882
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100883 ret = nand_status_op(chip, NULL);
884 if (ret)
885 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100886
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200887 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
888 u32 time_start;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200889
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200890 time_start = get_timer(0);
891 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200892 if (chip->dev_ready) {
893 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100894 break;
895 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100896 ret = nand_read_data_op(chip, &status,
897 sizeof(status), true);
898 if (ret)
899 return ret;
900
901 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100902 break;
903 }
904 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200905 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100906
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100907 ret = nand_read_data_op(chip, &status, sizeof(status), true);
908 if (ret)
909 return ret;
910
Heiko Schocherf5895d12014-06-24 10:10:04 +0200911 /* This can happen if in case of timeout or buggy dev_ready */
912 WARN_ON(!(status & NAND_STATUS_READY));
913 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200914}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500915
Scott Wood52ab7ce2016-05-30 13:57:58 -0500916/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900917 * nand_reset_data_interface - Reset data interface and timings
918 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900919 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900920 *
921 * Reset the Data interface and timings to ONFI mode 0.
922 *
923 * Returns 0 for success or negative error code otherwise.
924 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900925static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900926{
927 struct mtd_info *mtd = nand_to_mtd(chip);
928 const struct nand_data_interface *conf;
929 int ret;
930
931 if (!chip->setup_data_interface)
932 return 0;
933
934 /*
935 * The ONFI specification says:
936 * "
937 * To transition from NV-DDR or NV-DDR2 to the SDR data
938 * interface, the host shall use the Reset (FFh) command
939 * using SDR timing mode 0. A device in any timing mode is
940 * required to recognize Reset (FFh) command issued in SDR
941 * timing mode 0.
942 * "
943 *
944 * Configure the data interface in SDR mode and set the
945 * timings to timing mode 0.
946 */
947
948 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900949 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900950 if (ret)
951 pr_err("Failed to configure data interface to SDR timing mode 0\n");
952
953 return ret;
954}
955
Kory Maincent0cda0cb2022-06-22 11:11:45 +0200956static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip)
957{
958 if (!chip->onfi_version ||
959 !(le16_to_cpu(chip->onfi_params.opt_cmd)
960 & ONFI_OPT_CMD_SET_GET_FEATURES))
961 return 0;
962
963 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
964 chip->onfi_timing_mode_default,
965 };
966
967 return chip->onfi_set_features(mtd, chip,
968 ONFI_FEATURE_ADDR_TIMING_MODE,
969 tmode_param);
970}
971
Boris Brezillone509cba2017-11-22 02:38:19 +0900972/**
973 * nand_setup_data_interface - Setup the best data interface and timings
974 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900975 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900976 *
977 * Find and configure the best data interface and NAND timings supported by
978 * the chip and the driver.
979 * First tries to retrieve supported timing modes from ONFI information,
980 * and if the NAND chip does not support ONFI, relies on the
981 * ->onfi_timing_mode_default specified in the nand_ids table.
982 *
983 * Returns 0 for success or negative error code otherwise.
984 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900985static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900986{
987 struct mtd_info *mtd = nand_to_mtd(chip);
988 int ret;
989
990 if (!chip->setup_data_interface || !chip->data_interface)
991 return 0;
992
993 /*
994 * Ensure the timing mode has been changed on the chip side
995 * before changing timings on the controller side.
996 */
Kory Maincent0cda0cb2022-06-22 11:11:45 +0200997 ret = nand_onfi_set_timings(mtd, chip);
998 if (ret)
999 goto err;
Boris Brezillone509cba2017-11-22 02:38:19 +09001000
Boris Brezillon32935f42017-11-22 02:38:28 +09001001 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001002err:
1003 return ret;
1004}
1005
1006/**
1007 * nand_init_data_interface - find the best data interface and timings
1008 * @chip: The NAND chip
1009 *
1010 * Find the best data interface and NAND timings supported by the chip
1011 * and the driver.
1012 * First tries to retrieve supported timing modes from ONFI information,
1013 * and if the NAND chip does not support ONFI, relies on the
1014 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1015 * function nand_chip->data_interface is initialized with the best timing mode
1016 * available.
1017 *
1018 * Returns 0 for success or negative error code otherwise.
1019 */
1020static int nand_init_data_interface(struct nand_chip *chip)
1021{
1022 struct mtd_info *mtd = nand_to_mtd(chip);
1023 int modes, mode, ret;
1024
1025 if (!chip->setup_data_interface)
1026 return 0;
1027
1028 /*
1029 * First try to identify the best timings from ONFI parameters and
1030 * if the NAND does not support ONFI, fallback to the default ONFI
1031 * timing mode.
1032 */
1033 modes = onfi_get_async_timing_mode(chip);
1034 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1035 if (!chip->onfi_timing_mode_default)
1036 return 0;
1037
1038 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1039 }
1040
1041 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1042 GFP_KERNEL);
1043 if (!chip->data_interface)
1044 return -ENOMEM;
1045
1046 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1047 ret = onfi_init_data_interface(chip, chip->data_interface,
1048 NAND_SDR_IFACE, mode);
1049 if (ret)
1050 continue;
1051
Boris Brezillon32935f42017-11-22 02:38:28 +09001052 /* Pass -1 to only */
1053 ret = chip->setup_data_interface(mtd,
1054 NAND_DATA_IFACE_CHECK_ONLY,
1055 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001056 if (!ret) {
1057 chip->onfi_timing_mode_default = mode;
1058 break;
1059 }
1060 }
1061
1062 return 0;
1063}
1064
1065static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1066{
1067 kfree(chip->data_interface);
1068}
1069
1070/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001071 * nand_read_page_op - Do a READ PAGE operation
1072 * @chip: The NAND chip
1073 * @page: page to read
1074 * @offset_in_page: offset within the page
1075 * @buf: buffer used to store the data
1076 * @len: length of the buffer
1077 *
1078 * This function issues a READ PAGE operation.
1079 * This function does not select/unselect the CS line.
1080 *
1081 * Returns 0 on success, a negative error code otherwise.
1082 */
1083int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1084 unsigned int offset_in_page, void *buf, unsigned int len)
1085{
1086 struct mtd_info *mtd = nand_to_mtd(chip);
1087
1088 if (len && !buf)
1089 return -EINVAL;
1090
1091 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1092 return -EINVAL;
1093
1094 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1095 if (len)
1096 chip->read_buf(mtd, buf, len);
1097
1098 return 0;
1099}
1100EXPORT_SYMBOL_GPL(nand_read_page_op);
1101
1102/**
1103 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1104 * @chip: The NAND chip
1105 * @page: parameter page to read
1106 * @buf: buffer used to store the data
1107 * @len: length of the buffer
1108 *
1109 * This function issues a READ PARAMETER PAGE operation.
1110 * This function does not select/unselect the CS line.
1111 *
1112 * Returns 0 on success, a negative error code otherwise.
1113 */
1114static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1115 unsigned int len)
1116{
1117 struct mtd_info *mtd = nand_to_mtd(chip);
1118 unsigned int i;
1119 u8 *p = buf;
1120
1121 if (len && !buf)
1122 return -EINVAL;
1123
1124 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1125 for (i = 0; i < len; i++)
1126 p[i] = chip->read_byte(mtd);
1127
1128 return 0;
1129}
1130
1131/**
1132 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1133 * @chip: The NAND chip
1134 * @offset_in_page: offset within the page
1135 * @buf: buffer used to store the data
1136 * @len: length of the buffer
1137 * @force_8bit: force 8-bit bus access
1138 *
1139 * This function issues a CHANGE READ COLUMN operation.
1140 * This function does not select/unselect the CS line.
1141 *
1142 * Returns 0 on success, a negative error code otherwise.
1143 */
1144int nand_change_read_column_op(struct nand_chip *chip,
1145 unsigned int offset_in_page, void *buf,
1146 unsigned int len, bool force_8bit)
1147{
1148 struct mtd_info *mtd = nand_to_mtd(chip);
1149
1150 if (len && !buf)
1151 return -EINVAL;
1152
1153 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1154 return -EINVAL;
1155
1156 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1157 if (len)
1158 chip->read_buf(mtd, buf, len);
1159
1160 return 0;
1161}
1162EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1163
1164/**
1165 * nand_read_oob_op - Do a READ OOB operation
1166 * @chip: The NAND chip
1167 * @page: page to read
1168 * @offset_in_oob: offset within the OOB area
1169 * @buf: buffer used to store the data
1170 * @len: length of the buffer
1171 *
1172 * This function issues a READ OOB operation.
1173 * This function does not select/unselect the CS line.
1174 *
1175 * Returns 0 on success, a negative error code otherwise.
1176 */
1177int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1178 unsigned int offset_in_oob, void *buf, unsigned int len)
1179{
1180 struct mtd_info *mtd = nand_to_mtd(chip);
1181
1182 if (len && !buf)
1183 return -EINVAL;
1184
1185 if (offset_in_oob + len > mtd->oobsize)
1186 return -EINVAL;
1187
1188 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1189 if (len)
1190 chip->read_buf(mtd, buf, len);
1191
1192 return 0;
1193}
1194EXPORT_SYMBOL_GPL(nand_read_oob_op);
1195
1196/**
1197 * nand_prog_page_begin_op - starts a PROG PAGE operation
1198 * @chip: The NAND chip
1199 * @page: page to write
1200 * @offset_in_page: offset within the page
1201 * @buf: buffer containing the data to write to the page
1202 * @len: length of the buffer
1203 *
1204 * This function issues the first half of a PROG PAGE operation.
1205 * This function does not select/unselect the CS line.
1206 *
1207 * Returns 0 on success, a negative error code otherwise.
1208 */
1209int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1210 unsigned int offset_in_page, const void *buf,
1211 unsigned int len)
1212{
1213 struct mtd_info *mtd = nand_to_mtd(chip);
1214
1215 if (len && !buf)
1216 return -EINVAL;
1217
1218 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1219 return -EINVAL;
1220
1221 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1222
1223 if (buf)
1224 chip->write_buf(mtd, buf, len);
1225
1226 return 0;
1227}
1228EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1229
1230/**
1231 * nand_prog_page_end_op - ends a PROG PAGE operation
1232 * @chip: The NAND chip
1233 *
1234 * This function issues the second half of a PROG PAGE operation.
1235 * This function does not select/unselect the CS line.
1236 *
1237 * Returns 0 on success, a negative error code otherwise.
1238 */
1239int nand_prog_page_end_op(struct nand_chip *chip)
1240{
1241 struct mtd_info *mtd = nand_to_mtd(chip);
1242 int status;
1243
1244 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1245
1246 status = chip->waitfunc(mtd, chip);
1247 if (status & NAND_STATUS_FAIL)
1248 return -EIO;
1249
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1253
1254/**
1255 * nand_prog_page_op - Do a full PROG PAGE operation
1256 * @chip: The NAND chip
1257 * @page: page to write
1258 * @offset_in_page: offset within the page
1259 * @buf: buffer containing the data to write to the page
1260 * @len: length of the buffer
1261 *
1262 * This function issues a full PROG PAGE operation.
1263 * This function does not select/unselect the CS line.
1264 *
1265 * Returns 0 on success, a negative error code otherwise.
1266 */
1267int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1268 unsigned int offset_in_page, const void *buf,
1269 unsigned int len)
1270{
1271 struct mtd_info *mtd = nand_to_mtd(chip);
1272 int status;
1273
1274 if (!len || !buf)
1275 return -EINVAL;
1276
1277 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1278 return -EINVAL;
1279
1280 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1281 chip->write_buf(mtd, buf, len);
1282 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1283
1284 status = chip->waitfunc(mtd, chip);
1285 if (status & NAND_STATUS_FAIL)
1286 return -EIO;
1287
1288 return 0;
1289}
1290EXPORT_SYMBOL_GPL(nand_prog_page_op);
1291
1292/**
1293 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1294 * @chip: The NAND chip
1295 * @offset_in_page: offset within the page
1296 * @buf: buffer containing the data to send to the NAND
1297 * @len: length of the buffer
1298 * @force_8bit: force 8-bit bus access
1299 *
1300 * This function issues a CHANGE WRITE COLUMN operation.
1301 * This function does not select/unselect the CS line.
1302 *
1303 * Returns 0 on success, a negative error code otherwise.
1304 */
1305int nand_change_write_column_op(struct nand_chip *chip,
1306 unsigned int offset_in_page,
1307 const void *buf, unsigned int len,
1308 bool force_8bit)
1309{
1310 struct mtd_info *mtd = nand_to_mtd(chip);
1311
1312 if (len && !buf)
1313 return -EINVAL;
1314
1315 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1316 return -EINVAL;
1317
1318 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1319 if (len)
1320 chip->write_buf(mtd, buf, len);
1321
1322 return 0;
1323}
1324EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1325
1326/**
1327 * nand_readid_op - Do a READID operation
1328 * @chip: The NAND chip
1329 * @addr: address cycle to pass after the READID command
1330 * @buf: buffer used to store the ID
1331 * @len: length of the buffer
1332 *
1333 * This function sends a READID command and reads back the ID returned by the
1334 * NAND.
1335 * This function does not select/unselect the CS line.
1336 *
1337 * Returns 0 on success, a negative error code otherwise.
1338 */
1339int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1340 unsigned int len)
1341{
1342 struct mtd_info *mtd = nand_to_mtd(chip);
1343 unsigned int i;
1344 u8 *id = buf;
1345
1346 if (len && !buf)
1347 return -EINVAL;
1348
1349 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1350
1351 for (i = 0; i < len; i++)
1352 id[i] = chip->read_byte(mtd);
1353
1354 return 0;
1355}
1356EXPORT_SYMBOL_GPL(nand_readid_op);
1357
1358/**
1359 * nand_status_op - Do a STATUS operation
1360 * @chip: The NAND chip
1361 * @status: out variable to store the NAND status
1362 *
1363 * This function sends a STATUS command and reads back the status returned by
1364 * the NAND.
1365 * This function does not select/unselect the CS line.
1366 *
1367 * Returns 0 on success, a negative error code otherwise.
1368 */
1369int nand_status_op(struct nand_chip *chip, u8 *status)
1370{
1371 struct mtd_info *mtd = nand_to_mtd(chip);
1372
1373 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1374 if (status)
1375 *status = chip->read_byte(mtd);
1376
1377 return 0;
1378}
1379EXPORT_SYMBOL_GPL(nand_status_op);
1380
1381/**
1382 * nand_exit_status_op - Exit a STATUS operation
1383 * @chip: The NAND chip
1384 *
1385 * This function sends a READ0 command to cancel the effect of the STATUS
1386 * command to avoid reading only the status until a new read command is sent.
1387 *
1388 * This function does not select/unselect the CS line.
1389 *
1390 * Returns 0 on success, a negative error code otherwise.
1391 */
1392int nand_exit_status_op(struct nand_chip *chip)
1393{
1394 struct mtd_info *mtd = nand_to_mtd(chip);
1395
1396 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1397
1398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(nand_exit_status_op);
1401
1402/**
1403 * nand_erase_op - Do an erase operation
1404 * @chip: The NAND chip
1405 * @eraseblock: block to erase
1406 *
1407 * This function sends an ERASE command and waits for the NAND to be ready
1408 * before returning.
1409 * This function does not select/unselect the CS line.
1410 *
1411 * Returns 0 on success, a negative error code otherwise.
1412 */
1413int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1414{
1415 struct mtd_info *mtd = nand_to_mtd(chip);
1416 unsigned int page = eraseblock <<
1417 (chip->phys_erase_shift - chip->page_shift);
1418 int status;
1419
1420 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1421 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1422
1423 status = chip->waitfunc(mtd, chip);
1424 if (status < 0)
1425 return status;
1426
1427 if (status & NAND_STATUS_FAIL)
1428 return -EIO;
1429
1430 return 0;
1431}
1432EXPORT_SYMBOL_GPL(nand_erase_op);
1433
1434/**
1435 * nand_set_features_op - Do a SET FEATURES operation
1436 * @chip: The NAND chip
1437 * @feature: feature id
1438 * @data: 4 bytes of data
1439 *
1440 * This function sends a SET FEATURES command and waits for the NAND to be
1441 * ready before returning.
1442 * This function does not select/unselect the CS line.
1443 *
1444 * Returns 0 on success, a negative error code otherwise.
1445 */
1446static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1447 const void *data)
1448{
1449 struct mtd_info *mtd = nand_to_mtd(chip);
1450 const u8 *params = data;
1451 int i, status;
1452
1453 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1454 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1455 chip->write_byte(mtd, params[i]);
1456
1457 status = chip->waitfunc(mtd, chip);
1458 if (status & NAND_STATUS_FAIL)
1459 return -EIO;
1460
1461 return 0;
1462}
1463
1464/**
1465 * nand_get_features_op - Do a GET FEATURES operation
1466 * @chip: The NAND chip
1467 * @feature: feature id
1468 * @data: 4 bytes of data
1469 *
1470 * This function sends a GET FEATURES command and waits for the NAND to be
1471 * ready before returning.
1472 * This function does not select/unselect the CS line.
1473 *
1474 * Returns 0 on success, a negative error code otherwise.
1475 */
1476static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1477 void *data)
1478{
1479 struct mtd_info *mtd = nand_to_mtd(chip);
1480 u8 *params = data;
1481 int i;
1482
1483 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1484 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1485 params[i] = chip->read_byte(mtd);
1486
1487 return 0;
1488}
1489
1490/**
1491 * nand_reset_op - Do a reset operation
1492 * @chip: The NAND chip
1493 *
1494 * This function sends a RESET command and waits for the NAND to be ready
1495 * before returning.
1496 * This function does not select/unselect the CS line.
1497 *
1498 * Returns 0 on success, a negative error code otherwise.
1499 */
1500int nand_reset_op(struct nand_chip *chip)
1501{
1502 struct mtd_info *mtd = nand_to_mtd(chip);
1503
1504 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1505
1506 return 0;
1507}
1508EXPORT_SYMBOL_GPL(nand_reset_op);
1509
1510/**
1511 * nand_read_data_op - Read data from the NAND
1512 * @chip: The NAND chip
1513 * @buf: buffer used to store the data
1514 * @len: length of the buffer
1515 * @force_8bit: force 8-bit bus access
1516 *
1517 * This function does a raw data read on the bus. Usually used after launching
1518 * another NAND operation like nand_read_page_op().
1519 * This function does not select/unselect the CS line.
1520 *
1521 * Returns 0 on success, a negative error code otherwise.
1522 */
1523int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1524 bool force_8bit)
1525{
1526 struct mtd_info *mtd = nand_to_mtd(chip);
1527
1528 if (!len || !buf)
1529 return -EINVAL;
1530
1531 if (force_8bit) {
1532 u8 *p = buf;
1533 unsigned int i;
1534
1535 for (i = 0; i < len; i++)
1536 p[i] = chip->read_byte(mtd);
1537 } else {
1538 chip->read_buf(mtd, buf, len);
1539 }
1540
1541 return 0;
1542}
1543EXPORT_SYMBOL_GPL(nand_read_data_op);
1544
1545/**
1546 * nand_write_data_op - Write data from the NAND
1547 * @chip: The NAND chip
1548 * @buf: buffer containing the data to send on the bus
1549 * @len: length of the buffer
1550 * @force_8bit: force 8-bit bus access
1551 *
1552 * This function does a raw data write on the bus. Usually used after launching
1553 * another NAND operation like nand_write_page_begin_op().
1554 * This function does not select/unselect the CS line.
1555 *
1556 * Returns 0 on success, a negative error code otherwise.
1557 */
1558int nand_write_data_op(struct nand_chip *chip, const void *buf,
1559 unsigned int len, bool force_8bit)
1560{
1561 struct mtd_info *mtd = nand_to_mtd(chip);
1562
1563 if (!len || !buf)
1564 return -EINVAL;
1565
1566 if (force_8bit) {
1567 const u8 *p = buf;
1568 unsigned int i;
1569
1570 for (i = 0; i < len; i++)
1571 chip->write_byte(mtd, p[i]);
1572 } else {
1573 chip->write_buf(mtd, buf, len);
1574 }
1575
1576 return 0;
1577}
1578EXPORT_SYMBOL_GPL(nand_write_data_op);
1579
1580/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001581 * nand_reset - Reset and initialize a NAND device
1582 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001583 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001584 *
1585 * Returns 0 for success or negative error code otherwise
1586 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001587int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001588{
1589 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001590 int ret;
1591
Boris Brezillon32935f42017-11-22 02:38:28 +09001592 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001593 if (ret)
1594 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001595
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001596 /*
1597 * The CS line has to be released before we can apply the new NAND
1598 * interface settings, hence this weird ->select_chip() dance.
1599 */
1600 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001601 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001602 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001603 if (ret)
1604 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001605
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001606 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001607 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001608 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001609 if (ret)
1610 return ret;
1611
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001612 return 0;
1613}
1614
1615/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001616 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1617 * @buf: buffer to test
1618 * @len: buffer length
1619 * @bitflips_threshold: maximum number of bitflips
1620 *
1621 * Check if a buffer contains only 0xff, which means the underlying region
1622 * has been erased and is ready to be programmed.
1623 * The bitflips_threshold specify the maximum number of bitflips before
1624 * considering the region is not erased.
1625 * Note: The logic of this function has been extracted from the memweight
1626 * implementation, except that nand_check_erased_buf function exit before
1627 * testing the whole buffer if the number of bitflips exceed the
1628 * bitflips_threshold value.
1629 *
1630 * Returns a positive number of bitflips less than or equal to
1631 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1632 * threshold.
1633 */
1634static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1635{
1636 const unsigned char *bitmap = buf;
1637 int bitflips = 0;
1638 int weight;
1639
1640 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1641 len--, bitmap++) {
1642 weight = hweight8(*bitmap);
1643 bitflips += BITS_PER_BYTE - weight;
1644 if (unlikely(bitflips > bitflips_threshold))
1645 return -EBADMSG;
1646 }
1647
1648 for (; len >= 4; len -= 4, bitmap += 4) {
1649 weight = hweight32(*((u32 *)bitmap));
1650 bitflips += 32 - weight;
1651 if (unlikely(bitflips > bitflips_threshold))
1652 return -EBADMSG;
1653 }
1654
1655 for (; len > 0; len--, bitmap++) {
1656 weight = hweight8(*bitmap);
1657 bitflips += BITS_PER_BYTE - weight;
1658 if (unlikely(bitflips > bitflips_threshold))
1659 return -EBADMSG;
1660 }
1661
1662 return bitflips;
1663}
1664
1665/**
1666 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1667 * 0xff data
1668 * @data: data buffer to test
1669 * @datalen: data length
1670 * @ecc: ECC buffer
1671 * @ecclen: ECC length
1672 * @extraoob: extra OOB buffer
1673 * @extraooblen: extra OOB length
1674 * @bitflips_threshold: maximum number of bitflips
1675 *
1676 * Check if a data buffer and its associated ECC and OOB data contains only
1677 * 0xff pattern, which means the underlying region has been erased and is
1678 * ready to be programmed.
1679 * The bitflips_threshold specify the maximum number of bitflips before
1680 * considering the region as not erased.
1681 *
1682 * Note:
1683 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1684 * different from the NAND page size. When fixing bitflips, ECC engines will
1685 * report the number of errors per chunk, and the NAND core infrastructure
1686 * expect you to return the maximum number of bitflips for the whole page.
1687 * This is why you should always use this function on a single chunk and
1688 * not on the whole page. After checking each chunk you should update your
1689 * max_bitflips value accordingly.
1690 * 2/ When checking for bitflips in erased pages you should not only check
1691 * the payload data but also their associated ECC data, because a user might
1692 * have programmed almost all bits to 1 but a few. In this case, we
1693 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1694 * this case.
1695 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1696 * data are protected by the ECC engine.
1697 * It could also be used if you support subpages and want to attach some
1698 * extra OOB data to an ECC chunk.
1699 *
1700 * Returns a positive number of bitflips less than or equal to
1701 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1702 * threshold. In case of success, the passed buffers are filled with 0xff.
1703 */
1704int nand_check_erased_ecc_chunk(void *data, int datalen,
1705 void *ecc, int ecclen,
1706 void *extraoob, int extraooblen,
1707 int bitflips_threshold)
1708{
1709 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1710
1711 data_bitflips = nand_check_erased_buf(data, datalen,
1712 bitflips_threshold);
1713 if (data_bitflips < 0)
1714 return data_bitflips;
1715
1716 bitflips_threshold -= data_bitflips;
1717
1718 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1719 if (ecc_bitflips < 0)
1720 return ecc_bitflips;
1721
1722 bitflips_threshold -= ecc_bitflips;
1723
1724 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1725 bitflips_threshold);
1726 if (extraoob_bitflips < 0)
1727 return extraoob_bitflips;
1728
1729 if (data_bitflips)
1730 memset(data, 0xff, datalen);
1731
1732 if (ecc_bitflips)
1733 memset(ecc, 0xff, ecclen);
1734
1735 if (extraoob_bitflips)
1736 memset(extraoob, 0xff, extraooblen);
1737
1738 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1739}
1740EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001741
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001742/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001743 * nand_read_page_raw - [INTERN] read raw page data without ecc
1744 * @mtd: mtd info structure
1745 * @chip: nand chip info structure
1746 * @buf: buffer to store read data
1747 * @oob_required: caller requires OOB data read to chip->oob_poi
1748 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001749 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001750 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001751 */
William Juul52c07962007-10-31 13:53:06 +01001752static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001753 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001754{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001755 int ret;
1756
1757 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1758 if (ret)
1759 return ret;
1760
1761 if (oob_required) {
1762 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1763 false);
1764 if (ret)
1765 return ret;
1766 }
1767
William Juul52c07962007-10-31 13:53:06 +01001768 return 0;
1769}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001770
William Juul52c07962007-10-31 13:53:06 +01001771/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001772 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1773 * @mtd: mtd info structure
1774 * @chip: nand chip info structure
1775 * @buf: buffer to store read data
1776 * @oob_required: caller requires OOB data read to chip->oob_poi
1777 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001778 *
1779 * We need a special oob layout and handling even when OOB isn't used.
1780 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001781static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001782 struct nand_chip *chip, uint8_t *buf,
1783 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001784{
1785 int eccsize = chip->ecc.size;
1786 int eccbytes = chip->ecc.bytes;
1787 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001788 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001789
1790 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001791 ret = nand_read_data_op(chip, buf, eccsize, false);
1792 if (ret)
1793 return ret;
1794
David Brownellee86b8d2009-11-07 16:27:01 -05001795 buf += eccsize;
1796
1797 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001798 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1799 false);
1800 if (ret)
1801 return ret;
1802
David Brownellee86b8d2009-11-07 16:27:01 -05001803 oob += chip->ecc.prepad;
1804 }
1805
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001806 ret = nand_read_data_op(chip, oob, eccbytes, false);
1807 if (ret)
1808 return ret;
1809
David Brownellee86b8d2009-11-07 16:27:01 -05001810 oob += eccbytes;
1811
1812 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001813 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1814 false);
1815 if (ret)
1816 return ret;
1817
David Brownellee86b8d2009-11-07 16:27:01 -05001818 oob += chip->ecc.postpad;
1819 }
1820 }
1821
1822 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001823 if (size) {
1824 ret = nand_read_data_op(chip, oob, size, false);
1825 if (ret)
1826 return ret;
1827 }
David Brownellee86b8d2009-11-07 16:27:01 -05001828
1829 return 0;
1830}
1831
1832/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001833 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1834 * @mtd: mtd info structure
1835 * @chip: nand chip info structure
1836 * @buf: buffer to store read data
1837 * @oob_required: caller requires OOB data read to chip->oob_poi
1838 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001839 */
1840static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001841 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001842{
1843 int i, eccsize = chip->ecc.size;
1844 int eccbytes = chip->ecc.bytes;
1845 int eccsteps = chip->ecc.steps;
1846 uint8_t *p = buf;
1847 uint8_t *ecc_calc = chip->buffers->ecccalc;
1848 uint8_t *ecc_code = chip->buffers->ecccode;
1849 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001850 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001851
Sergey Lapin3a38a552013-01-14 03:46:50 +00001852 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001853
William Juul52c07962007-10-31 13:53:06 +01001854 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1855 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001856
William Juul52c07962007-10-31 13:53:06 +01001857 for (i = 0; i < chip->ecc.total; i++)
1858 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001859
William Juul52c07962007-10-31 13:53:06 +01001860 eccsteps = chip->ecc.steps;
1861 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001862
William Juul52c07962007-10-31 13:53:06 +01001863 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1864 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001865
William Juul52c07962007-10-31 13:53:06 +01001866 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001867 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001868 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001869 } else {
William Juul52c07962007-10-31 13:53:06 +01001870 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001871 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1872 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001873 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001874 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001875}
1876
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001877/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001878 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001879 * @mtd: mtd info structure
1880 * @chip: nand chip info structure
1881 * @data_offs: offset of requested data within the page
1882 * @readlen: data length
1883 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001884 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001885 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001886static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001887 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1888 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001889{
1890 int start_step, end_step, num_steps;
1891 uint32_t *eccpos = chip->ecc.layout->eccpos;
1892 uint8_t *p;
1893 int data_col_addr, i, gaps = 0;
1894 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1895 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001896 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001897 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001898 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001899
Sergey Lapin3a38a552013-01-14 03:46:50 +00001900 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001901 start_step = data_offs / chip->ecc.size;
1902 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1903 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001904 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001905
Sergey Lapin3a38a552013-01-14 03:46:50 +00001906 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001907 datafrag_len = num_steps * chip->ecc.size;
1908 eccfrag_len = num_steps * chip->ecc.bytes;
1909
1910 data_col_addr = start_step * chip->ecc.size;
1911 /* If we read not a page aligned data */
1912 if (data_col_addr != 0)
1913 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1914
1915 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001916 ret = nand_read_data_op(chip, p, datafrag_len, false);
1917 if (ret)
1918 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001919
Sergey Lapin3a38a552013-01-14 03:46:50 +00001920 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001921 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1922 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1923
Sergey Lapin3a38a552013-01-14 03:46:50 +00001924 /*
1925 * The performance is faster if we position offsets according to
1926 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1927 */
Scott Wood3628f002008-10-24 16:20:43 -05001928 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001929 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001930 gaps = 1;
1931 break;
1932 }
1933 }
1934 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001935 ret = nand_change_read_column_op(chip, mtd->writesize,
1936 chip->oob_poi, mtd->oobsize,
1937 false);
1938 if (ret)
1939 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001940 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001941 /*
1942 * Send the command to read the particular ECC bytes take care
1943 * about buswidth alignment in read_buf.
1944 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001945 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001946 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001947 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001948 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001949 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001950 aligned_len++;
1951
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001952 ret = nand_change_read_column_op(chip,
1953 mtd->writesize + aligned_pos,
1954 &chip->oob_poi[aligned_pos],
1955 aligned_len, false);
1956 if (ret)
1957 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001958 }
1959
1960 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001961 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001962
1963 p = bufpoi + data_col_addr;
1964 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1965 int stat;
1966
Christian Hitzb8a6b372011-10-12 09:32:02 +02001967 stat = chip->ecc.correct(mtd, p,
1968 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001969 if (stat == -EBADMSG &&
1970 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1971 /* check for empty pages with bitflips */
1972 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1973 &chip->buffers->ecccode[i],
1974 chip->ecc.bytes,
1975 NULL, 0,
1976 chip->ecc.strength);
1977 }
1978
Heiko Schocherf5895d12014-06-24 10:10:04 +02001979 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001980 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001981 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001982 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001983 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1984 }
Scott Wood3628f002008-10-24 16:20:43 -05001985 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001986 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05001987}
1988
1989/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001990 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1991 * @mtd: mtd info structure
1992 * @chip: nand chip info structure
1993 * @buf: buffer to store read data
1994 * @oob_required: caller requires OOB data read to chip->oob_poi
1995 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001996 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001997 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001998 */
William Juul52c07962007-10-31 13:53:06 +01001999static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002000 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002001{
William Juul52c07962007-10-31 13:53:06 +01002002 int i, eccsize = chip->ecc.size;
2003 int eccbytes = chip->ecc.bytes;
2004 int eccsteps = chip->ecc.steps;
2005 uint8_t *p = buf;
2006 uint8_t *ecc_calc = chip->buffers->ecccalc;
2007 uint8_t *ecc_code = chip->buffers->ecccode;
2008 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002009 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002010 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002011
William Juul52c07962007-10-31 13:53:06 +01002012 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2013 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002014
2015 ret = nand_read_data_op(chip, p, eccsize, false);
2016 if (ret)
2017 return ret;
2018
William Juul52c07962007-10-31 13:53:06 +01002019 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2020 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002021
2022 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2023 if (ret)
2024 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002025
William Juul52c07962007-10-31 13:53:06 +01002026 for (i = 0; i < chip->ecc.total; i++)
2027 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002028
William Juul52c07962007-10-31 13:53:06 +01002029 eccsteps = chip->ecc.steps;
2030 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002031
William Juul52c07962007-10-31 13:53:06 +01002032 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2033 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002034
William Juul52c07962007-10-31 13:53:06 +01002035 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002036 if (stat == -EBADMSG &&
2037 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2038 /* check for empty pages with bitflips */
2039 stat = nand_check_erased_ecc_chunk(p, eccsize,
2040 &ecc_code[i], eccbytes,
2041 NULL, 0,
2042 chip->ecc.strength);
2043 }
2044
Heiko Schocherf5895d12014-06-24 10:10:04 +02002045 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002046 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002047 } else {
William Juul52c07962007-10-31 13:53:06 +01002048 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002049 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2050 }
William Juul52c07962007-10-31 13:53:06 +01002051 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002052 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002053}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002054
William Juul52c07962007-10-31 13:53:06 +01002055/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002056 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2057 * @mtd: mtd info structure
2058 * @chip: nand chip info structure
2059 * @buf: buffer to store read data
2060 * @oob_required: caller requires OOB data read to chip->oob_poi
2061 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002062 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002063 * Hardware ECC for large page chips, require OOB to be read first. For this
2064 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2065 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2066 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2067 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002068 */
2069static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002070 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002071{
2072 int i, eccsize = chip->ecc.size;
2073 int eccbytes = chip->ecc.bytes;
2074 int eccsteps = chip->ecc.steps;
2075 uint8_t *p = buf;
2076 uint8_t *ecc_code = chip->buffers->ecccode;
2077 uint32_t *eccpos = chip->ecc.layout->eccpos;
2078 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002079 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002080 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002081
2082 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002083 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2084 if (ret)
2085 return ret;
2086
2087 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2088 if (ret)
2089 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002090
2091 for (i = 0; i < chip->ecc.total; i++)
2092 ecc_code[i] = chip->oob_poi[eccpos[i]];
2093
2094 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2095 int stat;
2096
2097 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002098
2099 ret = nand_read_data_op(chip, p, eccsize, false);
2100 if (ret)
2101 return ret;
2102
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002103 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2104
2105 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002106 if (stat == -EBADMSG &&
2107 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2108 /* check for empty pages with bitflips */
2109 stat = nand_check_erased_ecc_chunk(p, eccsize,
2110 &ecc_code[i], eccbytes,
2111 NULL, 0,
2112 chip->ecc.strength);
2113 }
2114
Heiko Schocherf5895d12014-06-24 10:10:04 +02002115 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002116 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002117 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002118 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002119 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2120 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002121 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002122 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002123}
2124
2125/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002126 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2127 * @mtd: mtd info structure
2128 * @chip: nand chip info structure
2129 * @buf: buffer to store read data
2130 * @oob_required: caller requires OOB data read to chip->oob_poi
2131 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002132 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002133 * The hw generator calculates the error syndrome automatically. Therefore we
2134 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002135 */
2136static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002137 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002138{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002139 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002140 int eccbytes = chip->ecc.bytes;
2141 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002142 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002143 uint8_t *p = buf;
2144 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002145 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002146
2147 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2148 int stat;
2149
2150 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002151
2152 ret = nand_read_data_op(chip, p, eccsize, false);
2153 if (ret)
2154 return ret;
William Juul52c07962007-10-31 13:53:06 +01002155
2156 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002157 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2158 false);
2159 if (ret)
2160 return ret;
2161
William Juul52c07962007-10-31 13:53:06 +01002162 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002163 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002164
William Juul52c07962007-10-31 13:53:06 +01002165 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002166
2167 ret = nand_read_data_op(chip, oob, eccbytes, false);
2168 if (ret)
2169 return ret;
2170
William Juul52c07962007-10-31 13:53:06 +01002171 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002172
William Juul52c07962007-10-31 13:53:06 +01002173 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002174
William Juul52c07962007-10-31 13:53:06 +01002175 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002176 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2177 false);
2178 if (ret)
2179 return ret;
2180
William Juul52c07962007-10-31 13:53:06 +01002181 oob += chip->ecc.postpad;
2182 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002183
2184 if (stat == -EBADMSG &&
2185 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2186 /* check for empty pages with bitflips */
2187 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2188 oob - eccpadbytes,
2189 eccpadbytes,
2190 NULL, 0,
2191 chip->ecc.strength);
2192 }
2193
2194 if (stat < 0) {
2195 mtd->ecc_stats.failed++;
2196 } else {
2197 mtd->ecc_stats.corrected += stat;
2198 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2199 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002200 }
William Juul52c07962007-10-31 13:53:06 +01002201
2202 /* Calculate remaining oob bytes */
2203 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002204 if (i) {
2205 ret = nand_read_data_op(chip, oob, i, false);
2206 if (ret)
2207 return ret;
2208 }
William Juul52c07962007-10-31 13:53:06 +01002209
Heiko Schocherf5895d12014-06-24 10:10:04 +02002210 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002211}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002212
2213/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002214 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2215 * @chip: nand chip structure
2216 * @oob: oob destination address
2217 * @ops: oob ops structure
2218 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002219 */
2220static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2221 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002222{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002223 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002224
Sergey Lapin3a38a552013-01-14 03:46:50 +00002225 case MTD_OPS_PLACE_OOB:
2226 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002227 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2228 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002229
Sergey Lapin3a38a552013-01-14 03:46:50 +00002230 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002231 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2232 uint32_t boffs = 0, roffs = ops->ooboffs;
2233 size_t bytes = 0;
2234
Christian Hitz13fc0e22011-10-12 09:32:01 +02002235 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002236 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002237 if (unlikely(roffs)) {
2238 if (roffs >= free->length) {
2239 roffs -= free->length;
2240 continue;
2241 }
2242 boffs = free->offset + roffs;
2243 bytes = min_t(size_t, len,
2244 (free->length - roffs));
2245 roffs = 0;
2246 } else {
2247 bytes = min_t(size_t, len, free->length);
2248 boffs = free->offset;
2249 }
2250 memcpy(oob, chip->oob_poi + boffs, bytes);
2251 oob += bytes;
2252 }
2253 return oob;
2254 }
2255 default:
2256 BUG();
2257 }
2258 return NULL;
2259}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002260
2261/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002262 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2263 * @mtd: MTD device structure
2264 * @retry_mode: the retry mode to use
2265 *
2266 * Some vendors supply a special command to shift the Vt threshold, to be used
2267 * when there are too many bitflips in a page (i.e., ECC error). After setting
2268 * a new threshold, the host should retry reading the page.
2269 */
2270static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2271{
Scott Wood17fed142016-05-30 13:57:56 -05002272 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002273
2274 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2275
2276 if (retry_mode >= chip->read_retries)
2277 return -EINVAL;
2278
2279 if (!chip->setup_read_retry)
2280 return -EOPNOTSUPP;
2281
2282 return chip->setup_read_retry(mtd, retry_mode);
2283}
2284
2285/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002286 * nand_do_read_ops - [INTERN] Read data with ECC
2287 * @mtd: MTD device structure
2288 * @from: offset to read from
2289 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002290 *
William Juul52c07962007-10-31 13:53:06 +01002291 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002292 */
William Juul52c07962007-10-31 13:53:06 +01002293static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2294 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002295{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002296 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002297 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002298 int ret = 0;
2299 uint32_t readlen = ops->len;
2300 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002301 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002302
William Juul52c07962007-10-31 13:53:06 +01002303 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002304 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002305 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002306 int retry_mode = 0;
2307 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002308
William Juul52c07962007-10-31 13:53:06 +01002309 chipnr = (int)(from >> chip->chip_shift);
2310 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002311
William Juul52c07962007-10-31 13:53:06 +01002312 realpage = (int)(from >> chip->page_shift);
2313 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002314
William Juul52c07962007-10-31 13:53:06 +01002315 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002316
William Juul52c07962007-10-31 13:53:06 +01002317 buf = ops->datbuf;
2318 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002319 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002320
Christian Hitz13fc0e22011-10-12 09:32:01 +02002321 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002322 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002323
Stefan Roese80877fa2022-09-02 14:10:46 +02002324 schedule();
William Juul52c07962007-10-31 13:53:06 +01002325 bytes = min(mtd->writesize - col, readlen);
2326 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002327
Scott Wood3ea94ed2015-06-26 19:03:26 -05002328 if (!aligned)
2329 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002330 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2331 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2332 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002333 else
2334 use_bufpoi = 0;
2335
Sergey Lapin3a38a552013-01-14 03:46:50 +00002336 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002337 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002338 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2339
2340 if (use_bufpoi && aligned)
2341 pr_debug("%s: using read bounce buffer for buf@%p\n",
2342 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002343
Heiko Schocherf5895d12014-06-24 10:10:04 +02002344read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002345 if (nand_standard_page_accessors(&chip->ecc)) {
2346 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2347 if (ret)
2348 break;
2349 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002350
Paul Burton700a76c2013-09-04 15:16:56 +01002351 /*
2352 * Now read the page into the buffer. Absent an error,
2353 * the read methods return max bitflips per ecc step.
2354 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002355 if (unlikely(ops->mode == MTD_OPS_RAW))
2356 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2357 oob_required,
2358 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002359 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002360 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002361 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002362 col, bytes, bufpoi,
2363 page);
William Juul52c07962007-10-31 13:53:06 +01002364 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002365 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002366 oob_required, page);
2367 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002368 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002369 /* Invalidate page cache */
2370 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002371 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002372 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002373
Paul Burton700a76c2013-09-04 15:16:56 +01002374 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2375
William Juul52c07962007-10-31 13:53:06 +01002376 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002377 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002378 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002379 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002380 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002381 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002382 chip->pagebuf_bitflips = ret;
2383 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002384 /* Invalidate page cache */
2385 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002386 }
William Juul52c07962007-10-31 13:53:06 +01002387 memcpy(buf, chip->buffers->databuf + col, bytes);
2388 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002389
William Juul52c07962007-10-31 13:53:06 +01002390 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002391 int toread = min(oobreadlen, max_oobsize);
2392
2393 if (toread) {
2394 oob = nand_transfer_oob(chip,
2395 oob, ops, toread);
2396 oobreadlen -= toread;
2397 }
William Juul52c07962007-10-31 13:53:06 +01002398 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002399
2400 if (chip->options & NAND_NEED_READRDY) {
2401 /* Apply delay or wait for ready/busy pin */
2402 if (!chip->dev_ready)
2403 udelay(chip->chip_delay);
2404 else
2405 nand_wait_ready(mtd);
2406 }
2407
2408 if (mtd->ecc_stats.failed - ecc_failures) {
2409 if (retry_mode + 1 < chip->read_retries) {
2410 retry_mode++;
2411 ret = nand_setup_read_retry(mtd,
2412 retry_mode);
2413 if (ret < 0)
2414 break;
2415
2416 /* Reset failures; retry */
2417 mtd->ecc_stats.failed = ecc_failures;
2418 goto read_retry;
2419 } else {
2420 /* No more retry modes; real failure */
2421 ecc_fail = true;
2422 }
2423 }
2424
2425 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002426 } else {
2427 memcpy(buf, chip->buffers->databuf + col, bytes);
2428 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002429 max_bitflips = max_t(unsigned int, max_bitflips,
2430 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002431 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002432
William Juul52c07962007-10-31 13:53:06 +01002433 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002434
Heiko Schocherf5895d12014-06-24 10:10:04 +02002435 /* Reset to retry mode 0 */
2436 if (retry_mode) {
2437 ret = nand_setup_read_retry(mtd, 0);
2438 if (ret < 0)
2439 break;
2440 retry_mode = 0;
2441 }
2442
William Juul52c07962007-10-31 13:53:06 +01002443 if (!readlen)
2444 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002445
Sergey Lapin3a38a552013-01-14 03:46:50 +00002446 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002447 col = 0;
2448 /* Increment page address */
2449 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002450
William Juul52c07962007-10-31 13:53:06 +01002451 page = realpage & chip->pagemask;
2452 /* Check, if we cross a chip boundary */
2453 if (!page) {
2454 chipnr++;
2455 chip->select_chip(mtd, -1);
2456 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002457 }
William Juul52c07962007-10-31 13:53:06 +01002458 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002459 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002460
William Juul52c07962007-10-31 13:53:06 +01002461 ops->retlen = ops->len - (size_t) readlen;
2462 if (oob)
2463 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002464
Heiko Schocherf5895d12014-06-24 10:10:04 +02002465 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002466 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002467
Heiko Schocherf5895d12014-06-24 10:10:04 +02002468 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002469 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002470
Paul Burton700a76c2013-09-04 15:16:56 +01002471 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002472}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002473
William Juul52c07962007-10-31 13:53:06 +01002474/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002475 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2476 * @mtd: mtd info structure
2477 * @chip: nand chip info structure
2478 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002479 */
2480static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002481 int page)
William Juul52c07962007-10-31 13:53:06 +01002482{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002483 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002484}
2485
2486/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002487 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002488 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002489 * @mtd: mtd info structure
2490 * @chip: nand chip info structure
2491 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002492 */
2493static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002494 int page)
William Juul52c07962007-10-31 13:53:06 +01002495{
William Juul52c07962007-10-31 13:53:06 +01002496 int length = mtd->oobsize;
2497 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2498 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002499 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002500 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002501
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002502 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2503 if (ret)
2504 return ret;
2505
William Juul52c07962007-10-31 13:53:06 +01002506 for (i = 0; i < chip->ecc.steps; i++) {
2507 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002508 int ret;
2509
William Juul52c07962007-10-31 13:53:06 +01002510 pos = eccsize + i * (eccsize + chunk);
2511 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002512 ret = nand_change_read_column_op(chip, pos,
2513 NULL, 0,
2514 false);
William Juul52c07962007-10-31 13:53:06 +01002515 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002516 ret = nand_read_page_op(chip, page, pos, NULL,
2517 0);
2518
2519 if (ret)
2520 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002521 } else
William Juul52c07962007-10-31 13:53:06 +01002522 sndrnd = 1;
2523 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002524
2525 ret = nand_read_data_op(chip, bufpoi, toread, false);
2526 if (ret)
2527 return ret;
2528
William Juul52c07962007-10-31 13:53:06 +01002529 bufpoi += toread;
2530 length -= toread;
2531 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002532 if (length > 0) {
2533 ret = nand_read_data_op(chip, bufpoi, length, false);
2534 if (ret)
2535 return ret;
2536 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002537
Sergey Lapin3a38a552013-01-14 03:46:50 +00002538 return 0;
William Juul52c07962007-10-31 13:53:06 +01002539}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002540
William Juul52c07962007-10-31 13:53:06 +01002541/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002542 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2543 * @mtd: mtd info structure
2544 * @chip: nand chip info structure
2545 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002546 */
2547static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2548 int page)
2549{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002550 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2551 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002552}
2553
2554/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002555 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2556 * with syndrome - only for large page flash
2557 * @mtd: mtd info structure
2558 * @chip: nand chip info structure
2559 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002560 */
2561static int nand_write_oob_syndrome(struct mtd_info *mtd,
2562 struct nand_chip *chip, int page)
2563{
2564 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2565 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002566 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002567 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002568
2569 /*
William Juul52c07962007-10-31 13:53:06 +01002570 * data-ecc-data-ecc ... ecc-oob
2571 * or
2572 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002573 */
William Juul52c07962007-10-31 13:53:06 +01002574 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2575 pos = steps * (eccsize + chunk);
2576 steps = 0;
2577 } else
2578 pos = eccsize;
2579
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002580 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2581 if (ret)
2582 return ret;
2583
William Juul52c07962007-10-31 13:53:06 +01002584 for (i = 0; i < steps; i++) {
2585 if (sndcmd) {
2586 if (mtd->writesize <= 512) {
2587 uint32_t fill = 0xFFFFFFFF;
2588
2589 len = eccsize;
2590 while (len > 0) {
2591 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002592
2593 ret = nand_write_data_op(chip, &fill,
2594 num, false);
2595 if (ret)
2596 return ret;
2597
William Juul52c07962007-10-31 13:53:06 +01002598 len -= num;
2599 }
2600 } else {
2601 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002602 ret = nand_change_write_column_op(chip, pos,
2603 NULL, 0,
2604 false);
2605 if (ret)
2606 return ret;
William Juul52c07962007-10-31 13:53:06 +01002607 }
2608 } else
2609 sndcmd = 1;
2610 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002611
2612 ret = nand_write_data_op(chip, bufpoi, len, false);
2613 if (ret)
2614 return ret;
2615
William Juul52c07962007-10-31 13:53:06 +01002616 bufpoi += len;
2617 length -= len;
2618 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002619 if (length > 0) {
2620 ret = nand_write_data_op(chip, bufpoi, length, false);
2621 if (ret)
2622 return ret;
2623 }
William Juul52c07962007-10-31 13:53:06 +01002624
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002625 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002626}
2627
2628/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002629 * nand_do_read_oob - [INTERN] NAND read out-of-band
2630 * @mtd: MTD device structure
2631 * @from: offset to read from
2632 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002633 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002634 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002635 */
William Juul52c07962007-10-31 13:53:06 +01002636static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2637 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002638{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002639 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002640 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002641 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002642 int readlen = ops->ooblen;
2643 int len;
2644 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002645 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002646
Heiko Schocherf5895d12014-06-24 10:10:04 +02002647 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002648 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002649
Sergey Lapin3a38a552013-01-14 03:46:50 +00002650 stats = mtd->ecc_stats;
2651
Scott Wood52ab7ce2016-05-30 13:57:58 -05002652 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002653
William Juul52c07962007-10-31 13:53:06 +01002654 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002655 pr_debug("%s: attempt to start read outside oob\n",
2656 __func__);
William Juul52c07962007-10-31 13:53:06 +01002657 return -EINVAL;
2658 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002659
2660 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002661 if (unlikely(from >= mtd->size ||
2662 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2663 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002664 pr_debug("%s: attempt to read beyond end of device\n",
2665 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002666 return -EINVAL;
2667 }
2668
William Juul52c07962007-10-31 13:53:06 +01002669 chipnr = (int)(from >> chip->chip_shift);
2670 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002671
William Juul52c07962007-10-31 13:53:06 +01002672 /* Shift to get page */
2673 realpage = (int)(from >> chip->page_shift);
2674 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002675
Christian Hitz13fc0e22011-10-12 09:32:01 +02002676 while (1) {
Stefan Roese80877fa2022-09-02 14:10:46 +02002677 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002678
Sergey Lapin3a38a552013-01-14 03:46:50 +00002679 if (ops->mode == MTD_OPS_RAW)
2680 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2681 else
2682 ret = chip->ecc.read_oob(mtd, chip, page);
2683
2684 if (ret < 0)
2685 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002686
William Juul52c07962007-10-31 13:53:06 +01002687 len = min(len, readlen);
2688 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002689
Heiko Schocherf5895d12014-06-24 10:10:04 +02002690 if (chip->options & NAND_NEED_READRDY) {
2691 /* Apply delay or wait for ready/busy pin */
2692 if (!chip->dev_ready)
2693 udelay(chip->chip_delay);
2694 else
2695 nand_wait_ready(mtd);
2696 }
2697
William Juul52c07962007-10-31 13:53:06 +01002698 readlen -= len;
2699 if (!readlen)
2700 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002701
William Juul52c07962007-10-31 13:53:06 +01002702 /* Increment page address */
2703 realpage++;
2704
2705 page = realpage & chip->pagemask;
2706 /* Check, if we cross a chip boundary */
2707 if (!page) {
2708 chipnr++;
2709 chip->select_chip(mtd, -1);
2710 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002711 }
William Juul52c07962007-10-31 13:53:06 +01002712 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002713 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002714
Sergey Lapin3a38a552013-01-14 03:46:50 +00002715 ops->oobretlen = ops->ooblen - readlen;
2716
2717 if (ret < 0)
2718 return ret;
2719
2720 if (mtd->ecc_stats.failed - stats.failed)
2721 return -EBADMSG;
2722
2723 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002724}
2725
2726/**
William Juul52c07962007-10-31 13:53:06 +01002727 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002728 * @mtd: MTD device structure
2729 * @from: offset to read from
2730 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002731 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002732 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002733 */
William Juul52c07962007-10-31 13:53:06 +01002734static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2735 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002736{
William Juul52c07962007-10-31 13:53:06 +01002737 int ret = -ENOTSUPP;
2738
2739 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002740
2741 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002742 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002743 pr_debug("%s: attempt to read beyond end of device\n",
2744 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002745 return -EINVAL;
2746 }
2747
Heiko Schocherf5895d12014-06-24 10:10:04 +02002748 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002749
Christian Hitz13fc0e22011-10-12 09:32:01 +02002750 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002751 case MTD_OPS_PLACE_OOB:
2752 case MTD_OPS_AUTO_OOB:
2753 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002754 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002755
William Juul52c07962007-10-31 13:53:06 +01002756 default:
2757 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002758 }
2759
William Juul52c07962007-10-31 13:53:06 +01002760 if (!ops->datbuf)
2761 ret = nand_do_read_oob(mtd, from, ops);
2762 else
2763 ret = nand_do_read_ops(mtd, from, ops);
2764
Christian Hitz13fc0e22011-10-12 09:32:01 +02002765out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002766 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002767 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002768}
2769
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002770/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002771 * nand_write_page_raw - [INTERN] raw page write function
2772 * @mtd: mtd info structure
2773 * @chip: nand chip info structure
2774 * @buf: data buffer
2775 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002776 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002777 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002778 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002779 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002780static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002781 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002782{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002783 int ret;
2784
2785 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2786 if (ret)
2787 return ret;
2788
2789 if (oob_required) {
2790 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2791 false);
2792 if (ret)
2793 return ret;
2794 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002795
2796 return 0;
William Juul52c07962007-10-31 13:53:06 +01002797}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002798
William Juul52c07962007-10-31 13:53:06 +01002799/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002800 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2801 * @mtd: mtd info structure
2802 * @chip: nand chip info structure
2803 * @buf: data buffer
2804 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002805 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002806 *
2807 * We need a special oob layout and handling even when ECC isn't checked.
2808 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002809static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002810 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002811 const uint8_t *buf, int oob_required,
2812 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002813{
2814 int eccsize = chip->ecc.size;
2815 int eccbytes = chip->ecc.bytes;
2816 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002817 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002818
2819 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002820 ret = nand_write_data_op(chip, buf, eccsize, false);
2821 if (ret)
2822 return ret;
2823
David Brownellee86b8d2009-11-07 16:27:01 -05002824 buf += eccsize;
2825
2826 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002827 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2828 false);
2829 if (ret)
2830 return ret;
2831
David Brownellee86b8d2009-11-07 16:27:01 -05002832 oob += chip->ecc.prepad;
2833 }
2834
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002835 ret = nand_write_data_op(chip, oob, eccbytes, false);
2836 if (ret)
2837 return ret;
2838
David Brownellee86b8d2009-11-07 16:27:01 -05002839 oob += eccbytes;
2840
2841 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002842 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2843 false);
2844 if (ret)
2845 return ret;
2846
David Brownellee86b8d2009-11-07 16:27:01 -05002847 oob += chip->ecc.postpad;
2848 }
2849 }
2850
2851 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002852 if (size) {
2853 ret = nand_write_data_op(chip, oob, size, false);
2854 if (ret)
2855 return ret;
2856 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002857
2858 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002859}
2860/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002861 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2862 * @mtd: mtd info structure
2863 * @chip: nand chip info structure
2864 * @buf: data buffer
2865 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002866 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002867 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002868static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002869 const uint8_t *buf, int oob_required,
2870 int page)
William Juul52c07962007-10-31 13:53:06 +01002871{
2872 int i, eccsize = chip->ecc.size;
2873 int eccbytes = chip->ecc.bytes;
2874 int eccsteps = chip->ecc.steps;
2875 uint8_t *ecc_calc = chip->buffers->ecccalc;
2876 const uint8_t *p = buf;
2877 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002878
Sergey Lapin3a38a552013-01-14 03:46:50 +00002879 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002880 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2881 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002882
William Juul52c07962007-10-31 13:53:06 +01002883 for (i = 0; i < chip->ecc.total; i++)
2884 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002885
Scott Wood46e13102016-05-30 13:57:57 -05002886 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002887}
2888
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002889/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002890 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2891 * @mtd: mtd info structure
2892 * @chip: nand chip info structure
2893 * @buf: data buffer
2894 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002895 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002896 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002897static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002898 const uint8_t *buf, int oob_required,
2899 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002900{
William Juul52c07962007-10-31 13:53:06 +01002901 int i, eccsize = chip->ecc.size;
2902 int eccbytes = chip->ecc.bytes;
2903 int eccsteps = chip->ecc.steps;
2904 uint8_t *ecc_calc = chip->buffers->ecccalc;
2905 const uint8_t *p = buf;
2906 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002907 int ret;
William Juul52c07962007-10-31 13:53:06 +01002908
2909 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2910 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002911
2912 ret = nand_write_data_op(chip, p, eccsize, false);
2913 if (ret)
2914 return ret;
2915
William Juul52c07962007-10-31 13:53:06 +01002916 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2917 }
2918
2919 for (i = 0; i < chip->ecc.total; i++)
2920 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2921
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002922 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2923 if (ret)
2924 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002925
2926 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002927}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002928
Heiko Schocherf5895d12014-06-24 10:10:04 +02002929/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002930 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002931 * @mtd: mtd info structure
2932 * @chip: nand chip info structure
2933 * @offset: column address of subpage within the page
2934 * @data_len: data length
2935 * @buf: data buffer
2936 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002937 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002938 */
2939static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2940 struct nand_chip *chip, uint32_t offset,
2941 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002942 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002943{
2944 uint8_t *oob_buf = chip->oob_poi;
2945 uint8_t *ecc_calc = chip->buffers->ecccalc;
2946 int ecc_size = chip->ecc.size;
2947 int ecc_bytes = chip->ecc.bytes;
2948 int ecc_steps = chip->ecc.steps;
2949 uint32_t *eccpos = chip->ecc.layout->eccpos;
2950 uint32_t start_step = offset / ecc_size;
2951 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2952 int oob_bytes = mtd->oobsize / ecc_steps;
2953 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002954 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002955
2956 for (step = 0; step < ecc_steps; step++) {
2957 /* configure controller for WRITE access */
2958 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2959
2960 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002961 ret = nand_write_data_op(chip, buf, ecc_size, false);
2962 if (ret)
2963 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002964
2965 /* mask ECC of un-touched subpages by padding 0xFF */
2966 if ((step < start_step) || (step > end_step))
2967 memset(ecc_calc, 0xff, ecc_bytes);
2968 else
2969 chip->ecc.calculate(mtd, buf, ecc_calc);
2970
2971 /* mask OOB of un-touched subpages by padding 0xFF */
2972 /* if oob_required, preserve OOB metadata of written subpage */
2973 if (!oob_required || (step < start_step) || (step > end_step))
2974 memset(oob_buf, 0xff, oob_bytes);
2975
2976 buf += ecc_size;
2977 ecc_calc += ecc_bytes;
2978 oob_buf += oob_bytes;
2979 }
2980
2981 /* copy calculated ECC for whole page to chip->buffer->oob */
2982 /* this include masked-value(0xFF) for unwritten subpages */
2983 ecc_calc = chip->buffers->ecccalc;
2984 for (i = 0; i < chip->ecc.total; i++)
2985 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2986
2987 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002988 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2989 if (ret)
2990 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002991
2992 return 0;
2993}
2994
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002995/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002996 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2997 * @mtd: mtd info structure
2998 * @chip: nand chip info structure
2999 * @buf: data buffer
3000 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003001 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003002 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003003 * The hw generator calculates the error syndrome automatically. Therefore we
3004 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003005 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003006static int nand_write_page_syndrome(struct mtd_info *mtd,
3007 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003008 const uint8_t *buf, int oob_required,
3009 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003010{
William Juul52c07962007-10-31 13:53:06 +01003011 int i, eccsize = chip->ecc.size;
3012 int eccbytes = chip->ecc.bytes;
3013 int eccsteps = chip->ecc.steps;
3014 const uint8_t *p = buf;
3015 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003016 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003017
William Juul52c07962007-10-31 13:53:06 +01003018 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003019 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003020
3021 ret = nand_write_data_op(chip, p, eccsize, false);
3022 if (ret)
3023 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003024
William Juul52c07962007-10-31 13:53:06 +01003025 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003026 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3027 false);
3028 if (ret)
3029 return ret;
3030
William Juul52c07962007-10-31 13:53:06 +01003031 oob += chip->ecc.prepad;
3032 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003033
William Juul52c07962007-10-31 13:53:06 +01003034 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003035
3036 ret = nand_write_data_op(chip, oob, eccbytes, false);
3037 if (ret)
3038 return ret;
3039
William Juul52c07962007-10-31 13:53:06 +01003040 oob += eccbytes;
3041
3042 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003043 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3044 false);
3045 if (ret)
3046 return ret;
3047
William Juul52c07962007-10-31 13:53:06 +01003048 oob += chip->ecc.postpad;
3049 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003050 }
3051
William Juul52c07962007-10-31 13:53:06 +01003052 /* Calculate remaining oob bytes */
3053 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003054 if (i) {
3055 ret = nand_write_data_op(chip, oob, i, false);
3056 if (ret)
3057 return ret;
3058 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003059
3060 return 0;
William Juul52c07962007-10-31 13:53:06 +01003061}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003062
William Juul52c07962007-10-31 13:53:06 +01003063/**
3064 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003065 * @mtd: MTD device structure
3066 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003067 * @offset: address offset within the page
3068 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003069 * @buf: the data to write
3070 * @oob_required: must write chip->oob_poi to OOB
3071 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003072 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003073 */
3074static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003075 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003076 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003077{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003078 int status, subpage;
3079
3080 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3081 chip->ecc.write_subpage)
3082 subpage = offset || (data_len < mtd->writesize);
3083 else
3084 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003085
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003086 if (nand_standard_page_accessors(&chip->ecc)) {
3087 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3088 if (status)
3089 return status;
3090 }
William Juul52c07962007-10-31 13:53:06 +01003091
3092 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003093 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003094 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003095 else if (subpage)
3096 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003097 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003098 else
Scott Wood46e13102016-05-30 13:57:57 -05003099 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3100 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003101
3102 if (status < 0)
3103 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003104
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003105 if (nand_standard_page_accessors(&chip->ecc))
3106 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003107
William Juul52c07962007-10-31 13:53:06 +01003108 return 0;
3109}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003110
William Juul52c07962007-10-31 13:53:06 +01003111/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003112 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3113 * @mtd: MTD device structure
3114 * @oob: oob data buffer
3115 * @len: oob data write length
3116 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003117 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003118static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3119 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003120{
Scott Wood17fed142016-05-30 13:57:56 -05003121 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003122
3123 /*
3124 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3125 * data from a previous OOB read.
3126 */
3127 memset(chip->oob_poi, 0xff, mtd->oobsize);
3128
Christian Hitz13fc0e22011-10-12 09:32:01 +02003129 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003130
Sergey Lapin3a38a552013-01-14 03:46:50 +00003131 case MTD_OPS_PLACE_OOB:
3132 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003133 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3134 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003135
Sergey Lapin3a38a552013-01-14 03:46:50 +00003136 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003137 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3138 uint32_t boffs = 0, woffs = ops->ooboffs;
3139 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003140
Christian Hitz13fc0e22011-10-12 09:32:01 +02003141 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003142 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003143 if (unlikely(woffs)) {
3144 if (woffs >= free->length) {
3145 woffs -= free->length;
3146 continue;
3147 }
3148 boffs = free->offset + woffs;
3149 bytes = min_t(size_t, len,
3150 (free->length - woffs));
3151 woffs = 0;
3152 } else {
3153 bytes = min_t(size_t, len, free->length);
3154 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003155 }
William Juul52c07962007-10-31 13:53:06 +01003156 memcpy(chip->oob_poi + boffs, oob, bytes);
3157 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003158 }
William Juul52c07962007-10-31 13:53:06 +01003159 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003160 }
William Juul52c07962007-10-31 13:53:06 +01003161 default:
3162 BUG();
3163 }
3164 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003165}
3166
Christian Hitzb8a6b372011-10-12 09:32:02 +02003167#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003168
3169/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003170 * nand_do_write_ops - [INTERN] NAND write with ECC
3171 * @mtd: MTD device structure
3172 * @to: offset to write to
3173 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003174 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003175 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003176 */
William Juul52c07962007-10-31 13:53:06 +01003177static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3178 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003179{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003180 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003181 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003182 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003183
3184 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003185 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003186
William Juul52c07962007-10-31 13:53:06 +01003187 uint8_t *oob = ops->oobbuf;
3188 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003189 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003190 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003191
William Juul52c07962007-10-31 13:53:06 +01003192 ops->retlen = 0;
3193 if (!writelen)
3194 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003195
Heiko Schocherf5895d12014-06-24 10:10:04 +02003196 /* Reject writes, which are not page aligned */
3197 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003198 pr_notice("%s: attempt to write non page aligned data\n",
3199 __func__);
William Juul52c07962007-10-31 13:53:06 +01003200 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003201 }
3202
3203 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003204
William Juul52c07962007-10-31 13:53:06 +01003205 chipnr = (int)(to >> chip->chip_shift);
3206 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003207
3208 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003209 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003210 ret = -EIO;
3211 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003212 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003213
William Juul52c07962007-10-31 13:53:06 +01003214 realpage = (int)(to >> chip->page_shift);
3215 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003216
William Juul52c07962007-10-31 13:53:06 +01003217 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003218 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3219 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003220 chip->pagebuf = -1;
3221
Christian Hitzb8a6b372011-10-12 09:32:02 +02003222 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003223 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3224 ret = -EINVAL;
3225 goto err_out;
3226 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003227
Christian Hitz13fc0e22011-10-12 09:32:01 +02003228 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003229 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003230 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003231 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003232 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003233
3234 if (part_pagewr)
3235 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003236 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3237 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3238 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003239 else
3240 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003241
Stefan Roese80877fa2022-09-02 14:10:46 +02003242 schedule();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003243 /* Partial page write?, or need to use bounce buffer */
3244 if (use_bufpoi) {
3245 pr_debug("%s: using write bounce buffer for buf@%p\n",
3246 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003247 if (part_pagewr)
3248 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003249 chip->pagebuf = -1;
3250 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3251 memcpy(&chip->buffers->databuf[column], buf, bytes);
3252 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003253 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003254
Christian Hitzb8a6b372011-10-12 09:32:02 +02003255 if (unlikely(oob)) {
3256 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003257 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003258 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003259 } else {
3260 /* We still need to erase leftover OOB data */
3261 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003262 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003263 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003264 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003265 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003266 if (ret)
3267 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003268
William Juul52c07962007-10-31 13:53:06 +01003269 writelen -= bytes;
3270 if (!writelen)
3271 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003272
Heiko Schocherf5895d12014-06-24 10:10:04 +02003273 column = 0;
3274 buf += bytes;
3275 realpage++;
3276
3277 page = realpage & chip->pagemask;
3278 /* Check, if we cross a chip boundary */
3279 if (!page) {
3280 chipnr++;
3281 chip->select_chip(mtd, -1);
3282 chip->select_chip(mtd, chipnr);
3283 }
3284 }
3285
3286 ops->retlen = ops->len - writelen;
3287 if (unlikely(oob))
3288 ops->oobretlen = ops->ooblen;
3289
3290err_out:
3291 chip->select_chip(mtd, -1);
3292 return ret;
3293}
3294
3295/**
3296 * panic_nand_write - [MTD Interface] NAND write with ECC
3297 * @mtd: MTD device structure
3298 * @to: offset to write to
3299 * @len: number of bytes to write
3300 * @retlen: pointer to variable to store the number of written bytes
3301 * @buf: the data to write
3302 *
3303 * NAND write with ECC. Used when performing writes in interrupt context, this
3304 * may for example be called by mtdoops when writing an oops while in panic.
3305 */
3306static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3307 size_t *retlen, const uint8_t *buf)
3308{
Scott Wood17fed142016-05-30 13:57:56 -05003309 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003310 struct mtd_oob_ops ops;
3311 int ret;
3312
3313 /* Wait for the device to get ready */
3314 panic_nand_wait(mtd, chip, 400);
3315
3316 /* Grab the device */
3317 panic_nand_get_device(chip, mtd, FL_WRITING);
3318
Scott Wood3ea94ed2015-06-26 19:03:26 -05003319 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003320 ops.len = len;
3321 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003322 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003323
Heiko Schocherf5895d12014-06-24 10:10:04 +02003324 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003325
Sergey Lapin3a38a552013-01-14 03:46:50 +00003326 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003327 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003328}
3329
3330/**
William Juul52c07962007-10-31 13:53:06 +01003331 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003332 * @mtd: MTD device structure
3333 * @to: offset to write to
3334 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003335 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003336 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003337 */
William Juul52c07962007-10-31 13:53:06 +01003338static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3339 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003340{
William Juul52c07962007-10-31 13:53:06 +01003341 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003342 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003343
Heiko Schocherf5895d12014-06-24 10:10:04 +02003344 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003345 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003346
Scott Wood52ab7ce2016-05-30 13:57:58 -05003347 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003348
3349 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003350 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003351 pr_debug("%s: attempt to write past end of page\n",
3352 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003353 return -EINVAL;
3354 }
3355
William Juul52c07962007-10-31 13:53:06 +01003356 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003357 pr_debug("%s: attempt to start write outside oob\n",
3358 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003359 return -EINVAL;
3360 }
3361
Christian Hitz13fc0e22011-10-12 09:32:01 +02003362 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003363 if (unlikely(to >= mtd->size ||
3364 ops->ooboffs + ops->ooblen >
3365 ((mtd->size >> chip->page_shift) -
3366 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003367 pr_debug("%s: attempt to write beyond end of device\n",
3368 __func__);
William Juul52c07962007-10-31 13:53:06 +01003369 return -EINVAL;
3370 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003371
William Juul52c07962007-10-31 13:53:06 +01003372 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003373
3374 /*
3375 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3376 * of my DiskOnChip 2000 test units) will clear the whole data page too
3377 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3378 * it in the doc2000 driver in August 1999. dwmw2.
3379 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003380 nand_reset(chip, chipnr);
3381
3382 chip->select_chip(mtd, chipnr);
3383
3384 /* Shift to get page */
3385 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003386
3387 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003388 if (nand_check_wp(mtd)) {
3389 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003390 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003391 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003392
William Juul52c07962007-10-31 13:53:06 +01003393 /* Invalidate the page cache, if we write to the cached page */
3394 if (page == chip->pagebuf)
3395 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003396
Sergey Lapin3a38a552013-01-14 03:46:50 +00003397 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3398
3399 if (ops->mode == MTD_OPS_RAW)
3400 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3401 else
3402 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003403
Heiko Schocherf5895d12014-06-24 10:10:04 +02003404 chip->select_chip(mtd, -1);
3405
William Juul52c07962007-10-31 13:53:06 +01003406 if (status)
3407 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003408
William Juul52c07962007-10-31 13:53:06 +01003409 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003410
William Juul52c07962007-10-31 13:53:06 +01003411 return 0;
3412}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003413
William Juul52c07962007-10-31 13:53:06 +01003414/**
3415 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003416 * @mtd: MTD device structure
3417 * @to: offset to write to
3418 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003419 */
3420static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3421 struct mtd_oob_ops *ops)
3422{
William Juul52c07962007-10-31 13:53:06 +01003423 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003424
William Juul52c07962007-10-31 13:53:06 +01003425 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003426
William Juul52c07962007-10-31 13:53:06 +01003427 /* Do not allow writes past end of device */
3428 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003429 pr_debug("%s: attempt to write beyond end of device\n",
3430 __func__);
William Juul52c07962007-10-31 13:53:06 +01003431 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003432 }
William Juul52c07962007-10-31 13:53:06 +01003433
Heiko Schocherf5895d12014-06-24 10:10:04 +02003434 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003435
Christian Hitz13fc0e22011-10-12 09:32:01 +02003436 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003437 case MTD_OPS_PLACE_OOB:
3438 case MTD_OPS_AUTO_OOB:
3439 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003440 break;
3441
3442 default:
3443 goto out;
3444 }
3445
3446 if (!ops->datbuf)
3447 ret = nand_do_write_oob(mtd, to, ops);
3448 else
3449 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003450
Christian Hitz13fc0e22011-10-12 09:32:01 +02003451out:
William Juul52c07962007-10-31 13:53:06 +01003452 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003453 return ret;
3454}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003455
3456/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003457 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003458 * @mtd: MTD device structure
3459 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003460 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003461 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003462 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003463static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003464{
Scott Wood17fed142016-05-30 13:57:56 -05003465 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003466 unsigned int eraseblock;
3467
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003468 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003469 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003470
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003471 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003472}
3473
3474/**
3475 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003476 * @mtd: MTD device structure
3477 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003478 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003479 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003480 */
William Juul52c07962007-10-31 13:53:06 +01003481static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003482{
William Juul52c07962007-10-31 13:53:06 +01003483 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003484}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003485
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003486/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003487 * nand_erase_nand - [INTERN] erase block(s)
3488 * @mtd: MTD device structure
3489 * @instr: erase instruction
3490 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003491 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003492 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003493 */
William Juul52c07962007-10-31 13:53:06 +01003494int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3495 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003496{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003497 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003498 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003499 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003500
Heiko Schocherf5895d12014-06-24 10:10:04 +02003501 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3502 __func__, (unsigned long long)instr->addr,
3503 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003504
Christian Hitzb8a6b372011-10-12 09:32:02 +02003505 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003506 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003507
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003508 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003509 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003510
3511 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003512 page = (int)(instr->addr >> chip->page_shift);
3513 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003514
3515 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003516 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003517
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003518 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003519 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003520
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003521 /* Check, if it is write protected */
3522 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003523 pr_debug("%s: device is write protected!\n",
3524 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003525 instr->state = MTD_ERASE_FAILED;
3526 goto erase_exit;
3527 }
3528
3529 /* Loop through the pages */
3530 len = instr->len;
3531
3532 instr->state = MTD_ERASING;
3533
3534 while (len) {
Stefan Roese80877fa2022-09-02 14:10:46 +02003535 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003536
Sergey Lapin3a38a552013-01-14 03:46:50 +00003537 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003538 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003539 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003540 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003541 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003542 instr->state = MTD_ERASE_FAILED;
Farhan Ali7c053192021-02-24 15:25:53 -08003543 instr->fail_addr =
3544 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003545 goto erase_exit;
3546 }
William Juul52c07962007-10-31 13:53:06 +01003547
3548 /*
3549 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003550 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003551 */
3552 if (page <= chip->pagebuf && chip->pagebuf <
3553 (page + pages_per_block))
3554 chip->pagebuf = -1;
3555
Scott Wood3ea94ed2015-06-26 19:03:26 -05003556 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003557
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003558 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003559 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003560 pr_debug("%s: failed erase, page 0x%08x\n",
3561 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003562 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003563 instr->fail_addr =
3564 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003565 goto erase_exit;
3566 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003567
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003568 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003569 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003570 page += pages_per_block;
3571
3572 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003573 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003574 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003575 chip->select_chip(mtd, -1);
3576 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003577 }
3578 }
3579 instr->state = MTD_ERASE_DONE;
3580
Christian Hitz13fc0e22011-10-12 09:32:01 +02003581erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003582
3583 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003584
3585 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003586 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003587 nand_release_device(mtd);
3588
3589 /* Return more or less happy */
3590 return ret;
3591}
3592
3593/**
3594 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003595 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003596 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003597 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003598 */
William Juul52c07962007-10-31 13:53:06 +01003599static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003600{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003601 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003602
3603 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003604 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003605 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003606 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003607}
3608
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003609/**
William Juul52c07962007-10-31 13:53:06 +01003610 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003611 * @mtd: MTD device structure
3612 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003613 */
William Juul52c07962007-10-31 13:53:06 +01003614static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003615{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003616 struct nand_chip *chip = mtd_to_nand(mtd);
3617 int chipnr = (int)(offs >> chip->chip_shift);
3618 int ret;
3619
3620 /* Select the NAND device */
3621 nand_get_device(mtd, FL_READING);
3622 chip->select_chip(mtd, chipnr);
3623
3624 ret = nand_block_checkbad(mtd, offs, 0);
3625
3626 chip->select_chip(mtd, -1);
3627 nand_release_device(mtd);
3628
3629 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003630}
3631
3632/**
William Juul52c07962007-10-31 13:53:06 +01003633 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003634 * @mtd: MTD device structure
3635 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003636 */
William Juul52c07962007-10-31 13:53:06 +01003637static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003638{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003639 int ret;
3640
Christian Hitzb8a6b372011-10-12 09:32:02 +02003641 ret = nand_block_isbad(mtd, ofs);
3642 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003643 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003644 if (ret > 0)
3645 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003646 return ret;
3647 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003648
Heiko Schocherf5895d12014-06-24 10:10:04 +02003649 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003650}
3651
Heiko Schocherf5895d12014-06-24 10:10:04 +02003652/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003653 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3654 * @mtd: MTD device structure
3655 * @chip: nand chip info structure
3656 * @addr: feature address.
3657 * @subfeature_param: the subfeature parameters, a four bytes array.
3658 */
3659static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3660 int addr, uint8_t *subfeature_param)
3661{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003662#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3663 if (!chip->onfi_version ||
3664 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3665 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003666 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003667#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003668
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003669 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003670}
3671
3672/**
3673 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3674 * @mtd: MTD device structure
3675 * @chip: nand chip info structure
3676 * @addr: feature address.
3677 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003678 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003679static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3680 int addr, uint8_t *subfeature_param)
3681{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003682#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3683 if (!chip->onfi_version ||
3684 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3685 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003686 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003687#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003688
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003689 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003690}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003691
Sergey Lapin3a38a552013-01-14 03:46:50 +00003692/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003693static void nand_set_defaults(struct nand_chip *chip, int busw)
3694{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003695 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003696 if (!chip->chip_delay)
3697 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003698
3699 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003700 if (chip->cmdfunc == NULL)
3701 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003702
3703 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003704 if (chip->waitfunc == NULL)
3705 chip->waitfunc = nand_wait;
3706
3707 if (!chip->select_chip)
3708 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003709
3710 /* set for ONFI nand */
3711 if (!chip->onfi_set_features)
3712 chip->onfi_set_features = nand_onfi_set_features;
3713 if (!chip->onfi_get_features)
3714 chip->onfi_get_features = nand_onfi_get_features;
3715
3716 /* If called twice, pointers that depend on busw may need to be reset */
3717 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003718 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3719 if (!chip->read_word)
3720 chip->read_word = nand_read_word;
3721 if (!chip->block_bad)
3722 chip->block_bad = nand_block_bad;
3723 if (!chip->block_markbad)
3724 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003725 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003726 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003727 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3728 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3729 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003730 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Roger Quadrosb590f612022-12-20 12:21:57 +02003731
3732#ifndef CONFIG_SPL_BUILD
William Juul52c07962007-10-31 13:53:06 +01003733 if (!chip->scan_bbt)
3734 chip->scan_bbt = nand_default_bbt;
Roger Quadrosb590f612022-12-20 12:21:57 +02003735#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02003736
3737 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003738 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003739 spin_lock_init(&chip->controller->lock);
3740 init_waitqueue_head(&chip->controller->wq);
3741 }
3742
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003743 if (!chip->buf_align)
3744 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003745}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003746
Sergey Lapin3a38a552013-01-14 03:46:50 +00003747/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003748static void sanitize_string(char *s, size_t len)
3749{
3750 ssize_t i;
3751
Sergey Lapin3a38a552013-01-14 03:46:50 +00003752 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003753 s[len - 1] = 0;
3754
Sergey Lapin3a38a552013-01-14 03:46:50 +00003755 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003756 for (i = 0; i < len - 1; i++) {
3757 if (s[i] < ' ' || s[i] > 127)
3758 s[i] = '?';
3759 }
3760
Sergey Lapin3a38a552013-01-14 03:46:50 +00003761 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003762 strim(s);
3763}
3764
Florian Fainellic98a9352011-02-25 00:01:34 +00003765static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3766{
3767 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003768 while (len--) {
3769 crc ^= *p++ << 8;
3770 for (i = 0; i < 8; i++)
3771 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3772 }
3773
3774 return crc;
3775}
3776
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003777#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003778/* Parse the Extended Parameter Page. */
3779static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3780 struct nand_chip *chip, struct nand_onfi_params *p)
3781{
3782 struct onfi_ext_param_page *ep;
3783 struct onfi_ext_section *s;
3784 struct onfi_ext_ecc_info *ecc;
3785 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003786 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003787 int len;
3788 int i;
3789
3790 len = le16_to_cpu(p->ext_param_page_length) * 16;
3791 ep = kmalloc(len, GFP_KERNEL);
3792 if (!ep)
3793 return -ENOMEM;
3794
3795 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003796 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3797 if (ret)
3798 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003799
3800 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003801 ret = nand_change_read_column_op(chip,
3802 sizeof(*p) * p->num_of_param_pages,
3803 ep, len, true);
3804 if (ret)
3805 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003806
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003807 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003808 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3809 != le16_to_cpu(ep->crc))) {
3810 pr_debug("fail in the CRC.\n");
3811 goto ext_out;
3812 }
3813
3814 /*
3815 * Check the signature.
3816 * Do not strictly follow the ONFI spec, maybe changed in future.
3817 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003818 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003819 pr_debug("The signature is invalid.\n");
3820 goto ext_out;
3821 }
3822
3823 /* find the ECC section. */
3824 cursor = (uint8_t *)(ep + 1);
3825 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3826 s = ep->sections + i;
3827 if (s->type == ONFI_SECTION_TYPE_2)
3828 break;
3829 cursor += s->length * 16;
3830 }
3831 if (i == ONFI_EXT_SECTION_MAX) {
3832 pr_debug("We can not find the ECC section.\n");
3833 goto ext_out;
3834 }
3835
3836 /* get the info we want. */
3837 ecc = (struct onfi_ext_ecc_info *)cursor;
3838
3839 if (!ecc->codeword_size) {
3840 pr_debug("Invalid codeword size\n");
3841 goto ext_out;
3842 }
3843
3844 chip->ecc_strength_ds = ecc->ecc_bits;
3845 chip->ecc_step_ds = 1 << ecc->codeword_size;
3846 ret = 0;
3847
3848ext_out:
3849 kfree(ep);
3850 return ret;
3851}
3852
Florian Fainellic98a9352011-02-25 00:01:34 +00003853/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003854 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003855 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003856static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003857{
3858 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003859 char id[4];
3860 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003861
Sergey Lapin3a38a552013-01-14 03:46:50 +00003862 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003863 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3864 if (ret || strncmp(id, "ONFI", 4))
3865 return 0;
3866
3867 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3868 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003869 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003870
Florian Fainellic98a9352011-02-25 00:01:34 +00003871 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003872 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3873 if (ret)
3874 return 0;
3875
Florian Fainellic98a9352011-02-25 00:01:34 +00003876 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003877 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003878 break;
3879 }
3880 }
3881
Heiko Schocherf5895d12014-06-24 10:10:04 +02003882 if (i == 3) {
3883 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003884 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003885 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003886
Sergey Lapin3a38a552013-01-14 03:46:50 +00003887 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003888 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003889 if (val & (1 << 5))
3890 chip->onfi_version = 23;
3891 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003892 chip->onfi_version = 22;
3893 else if (val & (1 << 3))
3894 chip->onfi_version = 21;
3895 else if (val & (1 << 2))
3896 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003897 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003898 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003899
3900 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003901 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003902 return 0;
3903 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003904
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003905 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3906 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003907 if (!mtd->name)
3908 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003909
Florian Fainellic98a9352011-02-25 00:01:34 +00003910 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003911
3912 /*
3913 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3914 * (don't ask me who thought of this...). MTD assumes that these
3915 * dimensions will be power-of-2, so just truncate the remaining area.
3916 */
3917 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3918 mtd->erasesize *= mtd->writesize;
3919
Florian Fainellic98a9352011-02-25 00:01:34 +00003920 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003921
3922 /* See erasesize comment */
3923 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003924 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003925 chip->bits_per_cell = p->bits_per_cell;
3926
3927 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003928 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003929
3930 if (p->ecc_bits != 0xff) {
3931 chip->ecc_strength_ds = p->ecc_bits;
3932 chip->ecc_step_ds = 512;
3933 } else if (chip->onfi_version >= 21 &&
3934 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3935
3936 /*
3937 * The nand_flash_detect_ext_param_page() uses the
3938 * Change Read Column command which maybe not supported
3939 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3940 * now. We do not replace user supplied command function.
3941 */
3942 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3943 chip->cmdfunc = nand_command_lp;
3944
3945 /* The Extended Parameter Page is supported since ONFI 2.1. */
3946 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3947 pr_warn("Failed to detect ONFI extended param page\n");
3948 } else {
3949 pr_warn("Could not retrieve ONFI ECC requirements\n");
3950 }
3951
Florian Fainellic98a9352011-02-25 00:01:34 +00003952 return 1;
3953}
3954#else
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003955static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003956{
3957 return 0;
3958}
3959#endif
3960
William Juul52c07962007-10-31 13:53:06 +01003961/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003962 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3963 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003964static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip)
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003965{
3966 struct nand_jedec_params *p = &chip->jedec_params;
3967 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003968 char id[5];
3969 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003970
3971 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003972 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
3973 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003974 return 0;
3975
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003976 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
3977 if (ret)
3978 return 0;
3979
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003980 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003981 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3982 if (ret)
3983 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003984
3985 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3986 le16_to_cpu(p->crc))
3987 break;
3988 }
3989
3990 if (i == 3) {
3991 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3992 return 0;
3993 }
3994
3995 /* Check version */
3996 val = le16_to_cpu(p->revision);
3997 if (val & (1 << 2))
3998 chip->jedec_version = 10;
3999 else if (val & (1 << 1))
4000 chip->jedec_version = 1; /* vendor specific version */
4001
4002 if (!chip->jedec_version) {
4003 pr_info("unsupported JEDEC version: %d\n", val);
4004 return 0;
4005 }
4006
4007 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4008 sanitize_string(p->model, sizeof(p->model));
4009 if (!mtd->name)
4010 mtd->name = p->model;
4011
4012 mtd->writesize = le32_to_cpu(p->byte_per_page);
4013
4014 /* Please reference to the comment for nand_flash_detect_onfi. */
4015 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4016 mtd->erasesize *= mtd->writesize;
4017
4018 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4019
4020 /* Please reference to the comment for nand_flash_detect_onfi. */
4021 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4022 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4023 chip->bits_per_cell = p->bits_per_cell;
4024
4025 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004026 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004027
4028 /* ECC info */
4029 ecc = &p->ecc_info[0];
4030
4031 if (ecc->codeword_size >= 9) {
4032 chip->ecc_strength_ds = ecc->ecc_bits;
4033 chip->ecc_step_ds = 1 << ecc->codeword_size;
4034 } else {
4035 pr_warn("Invalid codeword size\n");
4036 }
4037
4038 return 1;
4039}
4040
4041/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004042 * nand_id_has_period - Check if an ID string has a given wraparound period
4043 * @id_data: the ID string
4044 * @arrlen: the length of the @id_data array
4045 * @period: the period of repitition
4046 *
4047 * Check if an ID string is repeated within a given sequence of bytes at
4048 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02004049 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00004050 * if the repetition has a period of @period; otherwise, returns zero.
4051 */
4052static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4053{
4054 int i, j;
4055 for (i = 0; i < period; i++)
4056 for (j = i + period; j < arrlen; j += period)
4057 if (id_data[i] != id_data[j])
4058 return 0;
4059 return 1;
4060}
4061
4062/*
4063 * nand_id_len - Get the length of an ID string returned by CMD_READID
4064 * @id_data: the ID string
4065 * @arrlen: the length of the @id_data array
4066
4067 * Returns the length of the ID string, according to known wraparound/trailing
4068 * zero patterns. If no pattern exists, returns the length of the array.
4069 */
4070static int nand_id_len(u8 *id_data, int arrlen)
4071{
4072 int last_nonzero, period;
4073
4074 /* Find last non-zero byte */
4075 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4076 if (id_data[last_nonzero])
4077 break;
4078
4079 /* All zeros */
4080 if (last_nonzero < 0)
4081 return 0;
4082
4083 /* Calculate wraparound period */
4084 for (period = 1; period < arrlen; period++)
4085 if (nand_id_has_period(id_data, arrlen, period))
4086 break;
4087
4088 /* There's a repeated pattern */
4089 if (period < arrlen)
4090 return period;
4091
4092 /* There are trailing zeros */
4093 if (last_nonzero < arrlen - 1)
4094 return last_nonzero + 1;
4095
4096 /* No pattern detected */
4097 return arrlen;
4098}
4099
Heiko Schocherf5895d12014-06-24 10:10:04 +02004100/* Extract the bits of per cell from the 3rd byte of the extended ID */
4101static int nand_get_bits_per_cell(u8 cellinfo)
4102{
4103 int bits;
4104
4105 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4106 bits >>= NAND_CI_CELLTYPE_SHIFT;
4107 return bits + 1;
4108}
4109
Sergey Lapin3a38a552013-01-14 03:46:50 +00004110/*
4111 * Many new NAND share similar device ID codes, which represent the size of the
4112 * chip. The rest of the parameters must be decoded according to generic or
4113 * manufacturer-specific "extended ID" decoding patterns.
4114 */
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004115void nand_decode_ext_id(struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004116{
Alexander Dahlaa124532024-03-20 10:02:09 +01004117 struct mtd_info *mtd = nand_to_mtd(chip);
Michael Trimarchi3ba671b2022-07-20 18:22:11 +02004118 int extid;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004119 /* The 3rd id byte holds MLC / multichip data */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004120 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004121 /* The 4th id byte is the important one */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004122 extid = chip->id.data[3];
Sergey Lapin3a38a552013-01-14 03:46:50 +00004123
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004124 /* Calc pagesize */
4125 mtd->writesize = 1024 << (extid & 0x03);
4126 extid >>= 2;
4127 /* Calc oobsize */
4128 mtd->oobsize = (8 << (extid & 0x01)) *
4129 (mtd->writesize >> 9);
4130 extid >>= 2;
4131 /* Calc blocksize. Blocksize is multiples of 64KiB */
4132 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4133 extid >>= 2;
4134 /* Get buswidth information */
4135 /* Get buswidth information */
4136 if (extid & 0x1)
4137 chip->options |= NAND_BUSWIDTH_16;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004138}
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004139EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004140
Heiko Schocherf5895d12014-06-24 10:10:04 +02004141/*
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004142 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4143 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4144 * table.
4145 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004146static void nand_manufacturer_detect(struct nand_chip *chip)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004147{
4148 /*
4149 * Try manufacturer detection if available and use
4150 * nand_decode_ext_id() otherwise.
4151 */
4152 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004153 chip->manufacturer.desc->ops->detect) {
4154 /* The 3rd id byte holds MLC / multichip data */
4155 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004156 chip->manufacturer.desc->ops->detect(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004157 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004158 nand_decode_ext_id(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004159 }
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004160}
4161
4162/*
4163 * Manufacturer initialization. This function is called for all NANDs including
4164 * ONFI and JEDEC compliant ones.
4165 * Manufacturer drivers should put all their specific initialization code in
4166 * their ->init() hook.
4167 */
4168static int nand_manufacturer_init(struct nand_chip *chip)
4169{
4170 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4171 !chip->manufacturer.desc->ops->init)
4172 return 0;
4173
4174 return chip->manufacturer.desc->ops->init(chip);
4175}
4176
4177/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004178 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4179 * decodes a matching ID table entry and assigns the MTD size parameters for
4180 * the chip.
4181 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004182static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004183{
Alexander Dahlaa124532024-03-20 10:02:09 +01004184 struct mtd_info *mtd = nand_to_mtd(chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004185
4186 mtd->erasesize = type->erasesize;
4187 mtd->writesize = type->pagesize;
4188 mtd->oobsize = mtd->writesize / 32;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004189
Heiko Schocherf5895d12014-06-24 10:10:04 +02004190 /* All legacy ID NAND are small-page, SLC */
4191 chip->bits_per_cell = 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004192}
4193
Heiko Schocherf5895d12014-06-24 10:10:04 +02004194/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004195 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4196 * heuristic patterns using various detected parameters (e.g., manufacturer,
4197 * page size, cell-type information).
4198 */
4199static void nand_decode_bbm_options(struct mtd_info *mtd,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004200 struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004201{
Sergey Lapin3a38a552013-01-14 03:46:50 +00004202 /* Set the bad block position */
4203 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4204 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4205 else
4206 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004207}
4208
Heiko Schocherf5895d12014-06-24 10:10:04 +02004209static inline bool is_full_id_nand(struct nand_flash_dev *type)
4210{
4211 return type->id_len;
4212}
4213
4214static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004215 struct nand_flash_dev *type)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004216{
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004217 if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004218 mtd->writesize = type->pagesize;
4219 mtd->erasesize = type->erasesize;
4220 mtd->oobsize = type->oobsize;
4221
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004222 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004223 chip->chipsize = (uint64_t)type->chipsize << 20;
4224 chip->options |= type->options;
4225 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4226 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004227 chip->onfi_timing_mode_default =
4228 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004229
Heiko Schocherf5895d12014-06-24 10:10:04 +02004230 if (!mtd->name)
4231 mtd->name = type->name;
4232
4233 return true;
4234 }
4235 return false;
4236}
4237
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004238/**
4239 * nand_get_manufacturer_desc - Get manufacturer information from the
4240 * manufacturer ID
4241 * @id: manufacturer ID
4242 *
4243 * Returns a nand_manufacturer_desc object if the manufacturer is defined
4244 * in the NAND manufacturers database, NULL otherwise.
4245 */
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004246static const struct nand_manufacturer *nand_get_manufacturer_desc(u8 id)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004247{
4248 int i;
4249
4250 for (i = 0; nand_manuf_ids[i].id != 0x0; i++) {
4251 if (nand_manuf_ids[i].id == id)
4252 return &nand_manuf_ids[i];
4253 }
4254
4255 return NULL;
4256}
4257
Sergey Lapin3a38a552013-01-14 03:46:50 +00004258/*
4259 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004260 */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004261int nand_detect(struct nand_chip *chip, int *maf_id,
4262 int *dev_id, struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004263{
Alexander Dahlaa124532024-03-20 10:02:09 +01004264 struct mtd_info *mtd = nand_to_mtd(chip);
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004265 const struct nand_manufacturer *manufacturer_desc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004266 int busw, ret;
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004267 u8 *id_data = chip->id.data;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004268
Karl Beldanb6322fc2008-09-15 16:08:03 +02004269 /*
4270 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004271 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004272 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004273 ret = nand_reset(chip, 0);
4274 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004275 return ret;
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004276
4277 /* Select the device */
4278 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004279
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004280 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004281 ret = nand_readid_op(chip, 0, id_data, 2);
4282 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004283 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004284
4285 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004286 *maf_id = id_data[0];
4287 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004288
Sergey Lapin3a38a552013-01-14 03:46:50 +00004289 /*
4290 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004291 * interface concerns can cause random data which looks like a
4292 * possibly credible NAND flash to appear. If the two results do
4293 * not match, ignore the device completely.
4294 */
4295
Sergey Lapin3a38a552013-01-14 03:46:50 +00004296 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004297 ret = nand_readid_op(chip, 0, id_data, 8);
4298 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004299 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05004300
Christian Hitzb8a6b372011-10-12 09:32:02 +02004301 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004302 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004303 *maf_id, *dev_id, id_data[0], id_data[1]);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004304 return -ENODEV;
Scott Wood3628f002008-10-24 16:20:43 -05004305 }
4306
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004307 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
4308
4309 /* Try to identify manufacturer */
4310 manufacturer_desc = nand_get_manufacturer_desc(*maf_id);
4311 chip->manufacturer.desc = manufacturer_desc;
4312
Lei Wen75bde942011-01-06 09:48:18 +08004313 if (!type)
4314 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004315
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004316 /*
4317 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4318 * override it.
4319 * This is required to make sure initial NAND bus width set by the
4320 * NAND controller driver is coherent with the real NAND bus width
4321 * (extracted by auto-detection code).
4322 */
4323 busw = chip->options & NAND_BUSWIDTH_16;
4324
4325 /*
4326 * The flag is only set (never cleared), reset it to its default value
4327 * before starting auto-detection.
4328 */
4329 chip->options &= ~NAND_BUSWIDTH_16;
4330
Heiko Schocherf5895d12014-06-24 10:10:04 +02004331 for (; type->name != NULL; type++) {
4332 if (is_full_id_nand(type)) {
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004333 if (find_full_id_nand(mtd, chip, type))
Heiko Schocherf5895d12014-06-24 10:10:04 +02004334 goto ident_done;
4335 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004336 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004337 }
4338 }
Lei Wen75bde942011-01-06 09:48:18 +08004339
Christian Hitzb8a6b372011-10-12 09:32:02 +02004340 chip->onfi_version = 0;
4341 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004342 /* Check if the chip is ONFI compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004343 if (nand_flash_detect_onfi(mtd, chip))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004344 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004345
4346 /* Check if the chip is JEDEC compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004347 if (nand_flash_detect_jedec(mtd, chip))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004348 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004349 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004350
Christian Hitzb8a6b372011-10-12 09:32:02 +02004351 if (!type->name)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004352 return -ENODEV;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004353
William Juul52c07962007-10-31 13:53:06 +01004354 if (!mtd->name)
4355 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004356
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004357 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004358
Scott Wood52ab7ce2016-05-30 13:57:58 -05004359 if (!type->pagesize) {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004360 nand_manufacturer_detect(chip);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004361 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004362 nand_decode_id(chip, type);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004363 }
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004364
Heiko Schocherf5895d12014-06-24 10:10:04 +02004365 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004366 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004367
Christian Hitzb8a6b372011-10-12 09:32:02 +02004368ident_done:
4369
Heiko Schocherf5895d12014-06-24 10:10:04 +02004370 if (chip->options & NAND_BUSWIDTH_AUTO) {
4371 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4372 chip->options |= busw;
4373 nand_set_defaults(chip, busw);
4374 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4375 /*
4376 * Check, if buswidth is correct. Hardware drivers should set
4377 * chip correct!
4378 */
4379 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4380 *maf_id, *dev_id);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004381 pr_info("%s %s\n", manufacturer_desc->name, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004382 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004383 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4384 busw ? 16 : 8);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004385 return -EINVAL;
William Juul52c07962007-10-31 13:53:06 +01004386 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004387
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004388 nand_decode_bbm_options(mtd, chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004389
William Juul52c07962007-10-31 13:53:06 +01004390 /* Calculate the address shift from the page size */
4391 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004392 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004393 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004394
William Juul52c07962007-10-31 13:53:06 +01004395 chip->bbt_erase_shift = chip->phys_erase_shift =
4396 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004397 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004398 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004399 else {
4400 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4401 chip->chip_shift += 32 - 1;
4402 }
4403
Masahiro Yamada984926b2017-11-22 02:38:31 +09004404 if (chip->chip_shift - chip->page_shift > 16)
4405 chip->options |= NAND_ROW_ADDR_3;
4406
Christian Hitzb8a6b372011-10-12 09:32:02 +02004407 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004408 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004409
Sergey Lapin3a38a552013-01-14 03:46:50 +00004410 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004411 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4412 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004413
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004414 ret = nand_manufacturer_init(chip);
4415 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004416 return ret;
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004417
Heiko Schocherf5895d12014-06-24 10:10:04 +02004418 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4419 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004420
Christian Hitzb8a6b372011-10-12 09:32:02 +02004421#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004422 if (chip->onfi_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004423 pr_info("%s %s\n", manufacturer_desc->name,
4424 chip->onfi_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004425 else if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004426 pr_info("%s %s\n", manufacturer_desc->name,
4427 chip->jedec_params.model);
Sean Andersone476f112023-11-04 16:37:40 -04004428 else if (manufacturer_desc)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004429 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004430#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004431 if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004432 pr_info("%s %s\n", manufacturer_desc->name,
4433 chip->jedec_params.model);
Sean Andersone476f112023-11-04 16:37:40 -04004434 else if (manufacturer_desc)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004435 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004436#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004437
Scott Wood3ea94ed2015-06-26 19:03:26 -05004438 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004439 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004440 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004441 return 0;
William Juul52c07962007-10-31 13:53:06 +01004442}
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004443EXPORT_SYMBOL(nand_detect);
William Juul52c07962007-10-31 13:53:06 +01004444
Brian Norrisba6463d2016-06-15 21:09:22 +02004445#if CONFIG_IS_ENABLED(OF_CONTROL)
Brian Norrisba6463d2016-06-15 21:09:22 +02004446
Patrice Chotardbc77af52021-09-13 16:25:53 +02004447static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004448{
4449 int ret, ecc_mode = -1, ecc_strength, ecc_step;
Linus Walleij4e8611a2023-04-07 15:40:05 +02004450 int ecc_algo = NAND_ECC_UNKNOWN;
Brian Norrisba6463d2016-06-15 21:09:22 +02004451 const char *str;
4452
Patrice Chotardbc77af52021-09-13 16:25:53 +02004453 ret = ofnode_read_s32_default(node, "nand-bus-width", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004454 if (ret == 16)
4455 chip->options |= NAND_BUSWIDTH_16;
4456
Patrice Chotardbc77af52021-09-13 16:25:53 +02004457 if (ofnode_read_bool(node, "nand-on-flash-bbt"))
Brian Norrisba6463d2016-06-15 21:09:22 +02004458 chip->bbt_options |= NAND_BBT_USE_FLASH;
4459
Patrice Chotardbc77af52021-09-13 16:25:53 +02004460 str = ofnode_read_string(node, "nand-ecc-mode");
Brian Norrisba6463d2016-06-15 21:09:22 +02004461 if (str) {
4462 if (!strcmp(str, "none"))
4463 ecc_mode = NAND_ECC_NONE;
4464 else if (!strcmp(str, "soft"))
4465 ecc_mode = NAND_ECC_SOFT;
4466 else if (!strcmp(str, "hw"))
4467 ecc_mode = NAND_ECC_HW;
4468 else if (!strcmp(str, "hw_syndrome"))
4469 ecc_mode = NAND_ECC_HW_SYNDROME;
4470 else if (!strcmp(str, "hw_oob_first"))
4471 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4472 else if (!strcmp(str, "soft_bch"))
4473 ecc_mode = NAND_ECC_SOFT_BCH;
4474 }
4475
Linus Walleij4e8611a2023-04-07 15:40:05 +02004476 str = ofnode_read_string(node, "nand-ecc-algo");
4477 if (str) {
4478 /*
4479 * If we are in NAND_ECC_SOFT mode, just alter the
4480 * soft mode to BCH here. No change of algorithm.
4481 */
4482 if (ecc_mode == NAND_ECC_SOFT) {
4483 if (!strcmp(str, "bch"))
4484 ecc_mode = NAND_ECC_SOFT_BCH;
4485 } else {
4486 if (!strcmp(str, "bch")) {
4487 ecc_algo = NAND_ECC_BCH;
4488 } else if (!strcmp(str, "hamming")) {
4489 ecc_algo = NAND_ECC_HAMMING;
4490 }
4491 }
Pali Rohár44acf8a2022-04-04 18:17:21 +02004492 }
4493
Patrice Chotardbc77af52021-09-13 16:25:53 +02004494 ecc_strength = ofnode_read_s32_default(node,
4495 "nand-ecc-strength", -1);
4496 ecc_step = ofnode_read_s32_default(node,
4497 "nand-ecc-step-size", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004498
4499 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4500 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4501 pr_err("must set both strength and step size in DT\n");
4502 return -EINVAL;
4503 }
4504
Linus Walleij4e8611a2023-04-07 15:40:05 +02004505 /*
4506 * Chip drivers may have assigned default algorithms here,
4507 * onlt override it if we have found something explicitly
4508 * specified in the device tree.
4509 */
4510 if (ecc_algo != NAND_ECC_UNKNOWN)
4511 chip->ecc.algo = ecc_algo;
4512
Brian Norrisba6463d2016-06-15 21:09:22 +02004513 if (ecc_mode >= 0)
4514 chip->ecc.mode = ecc_mode;
4515
4516 if (ecc_strength >= 0)
4517 chip->ecc.strength = ecc_strength;
4518
4519 if (ecc_step > 0)
4520 chip->ecc.size = ecc_step;
4521
Patrice Chotardbc77af52021-09-13 16:25:53 +02004522 if (ofnode_read_bool(node, "nand-ecc-maximize"))
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004523 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4524
Brian Norrisba6463d2016-06-15 21:09:22 +02004525 return 0;
4526}
4527#else
Patrice Chotardbc77af52021-09-13 16:25:53 +02004528static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004529{
4530 return 0;
4531}
4532#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4533
William Juul52c07962007-10-31 13:53:06 +01004534/**
4535 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004536 * @mtd: MTD device structure
4537 * @maxchips: number of chips to scan for
4538 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004539 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004540 * This is the first phase of the normal nand_scan() function. It reads the
4541 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004542 *
William Juul52c07962007-10-31 13:53:06 +01004543 */
Lei Wen75bde942011-01-06 09:48:18 +08004544int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004545 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004546{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004547 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004548 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba6463d2016-06-15 21:09:22 +02004549 int ret;
4550
Patrice Chotardbc77af52021-09-13 16:25:53 +02004551 if (ofnode_valid(chip->flash_node)) {
Brian Norrisba6463d2016-06-15 21:09:22 +02004552 ret = nand_dt_init(mtd, chip, chip->flash_node);
4553 if (ret)
4554 return ret;
4555 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004556
William Juul52c07962007-10-31 13:53:06 +01004557 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004558 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004559
4560 /* Read the flash type */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004561 ret = nand_detect(chip, &nand_maf_id, &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004562
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004563 if (ret) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004564 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4565 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004566 chip->select_chip(mtd, -1);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004567 return ret;
William Juul52c07962007-10-31 13:53:06 +01004568 }
4569
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004570 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004571 ret = nand_init_data_interface(chip);
4572 if (ret)
4573 return ret;
4574
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004575 /*
4576 * Setup the data interface correctly on the chip and controller side.
4577 * This explicit call to nand_setup_data_interface() is only required
4578 * for the first die, because nand_reset() has been called before
4579 * ->data_interface and ->default_onfi_timing_mode were set.
4580 * For the other dies, nand_reset() will automatically switch to the
4581 * best mode for us.
4582 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004583 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004584 if (ret)
4585 return ret;
4586
Heiko Schocherf5895d12014-06-24 10:10:04 +02004587 chip->select_chip(mtd, -1);
4588
William Juul52c07962007-10-31 13:53:06 +01004589 /* Check for a chip array */
4590 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004591 u8 id[2];
4592
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004593 /* See comment in nand_detect for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004594 nand_reset(chip, i);
4595
4596 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004597 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004598 nand_readid_op(chip, 0, id, sizeof(id));
4599
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004600 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004601 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004602 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004603 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004604 }
4605 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004606 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004607
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004608#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004609 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004610 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004611#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004612
William Juul52c07962007-10-31 13:53:06 +01004613 /* Store the number of chips and calc total size for mtd */
4614 chip->numchips = i;
4615 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004616
William Juul52c07962007-10-31 13:53:06 +01004617 return 0;
4618}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004619EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004620
Masahiro Yamada820eb482017-11-22 02:38:29 +09004621/**
4622 * nand_check_ecc_caps - check the sanity of preset ECC settings
4623 * @chip: nand chip info structure
4624 * @caps: ECC caps info structure
4625 * @oobavail: OOB size that the ECC engine can use
4626 *
4627 * When ECC step size and strength are already set, check if they are supported
4628 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4629 * On success, the calculated ECC bytes is set.
4630 */
4631int nand_check_ecc_caps(struct nand_chip *chip,
4632 const struct nand_ecc_caps *caps, int oobavail)
4633{
4634 struct mtd_info *mtd = nand_to_mtd(chip);
4635 const struct nand_ecc_step_info *stepinfo;
4636 int preset_step = chip->ecc.size;
4637 int preset_strength = chip->ecc.strength;
4638 int nsteps, ecc_bytes;
4639 int i, j;
4640
4641 if (WARN_ON(oobavail < 0))
4642 return -EINVAL;
4643
4644 if (!preset_step || !preset_strength)
4645 return -ENODATA;
4646
4647 nsteps = mtd->writesize / preset_step;
4648
4649 for (i = 0; i < caps->nstepinfos; i++) {
4650 stepinfo = &caps->stepinfos[i];
4651
4652 if (stepinfo->stepsize != preset_step)
4653 continue;
4654
4655 for (j = 0; j < stepinfo->nstrengths; j++) {
4656 if (stepinfo->strengths[j] != preset_strength)
4657 continue;
4658
4659 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4660 preset_strength);
4661 if (WARN_ON_ONCE(ecc_bytes < 0))
4662 return ecc_bytes;
4663
4664 if (ecc_bytes * nsteps > oobavail) {
4665 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4666 preset_step, preset_strength);
4667 return -ENOSPC;
4668 }
4669
4670 chip->ecc.bytes = ecc_bytes;
4671
4672 return 0;
4673 }
4674 }
4675
4676 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4677 preset_step, preset_strength);
4678
4679 return -ENOTSUPP;
4680}
4681EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4682
4683/**
4684 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4685 * @chip: nand chip info structure
4686 * @caps: ECC engine caps info structure
4687 * @oobavail: OOB size that the ECC engine can use
4688 *
4689 * If a chip's ECC requirement is provided, try to meet it with the least
4690 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4691 * On success, the chosen ECC settings are set.
4692 */
4693int nand_match_ecc_req(struct nand_chip *chip,
4694 const struct nand_ecc_caps *caps, int oobavail)
4695{
4696 struct mtd_info *mtd = nand_to_mtd(chip);
4697 const struct nand_ecc_step_info *stepinfo;
4698 int req_step = chip->ecc_step_ds;
4699 int req_strength = chip->ecc_strength_ds;
4700 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4701 int best_step, best_strength, best_ecc_bytes;
4702 int best_ecc_bytes_total = INT_MAX;
4703 int i, j;
4704
4705 if (WARN_ON(oobavail < 0))
4706 return -EINVAL;
4707
4708 /* No information provided by the NAND chip */
4709 if (!req_step || !req_strength)
4710 return -ENOTSUPP;
4711
4712 /* number of correctable bits the chip requires in a page */
4713 req_corr = mtd->writesize / req_step * req_strength;
4714
4715 for (i = 0; i < caps->nstepinfos; i++) {
4716 stepinfo = &caps->stepinfos[i];
4717 step_size = stepinfo->stepsize;
4718
4719 for (j = 0; j < stepinfo->nstrengths; j++) {
4720 strength = stepinfo->strengths[j];
4721
4722 /*
4723 * If both step size and strength are smaller than the
4724 * chip's requirement, it is not easy to compare the
4725 * resulted reliability.
4726 */
4727 if (step_size < req_step && strength < req_strength)
4728 continue;
4729
4730 if (mtd->writesize % step_size)
4731 continue;
4732
4733 nsteps = mtd->writesize / step_size;
4734
4735 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4736 if (WARN_ON_ONCE(ecc_bytes < 0))
4737 continue;
4738 ecc_bytes_total = ecc_bytes * nsteps;
4739
4740 if (ecc_bytes_total > oobavail ||
4741 strength * nsteps < req_corr)
4742 continue;
4743
4744 /*
4745 * We assume the best is to meet the chip's requrement
4746 * with the least number of ECC bytes.
4747 */
4748 if (ecc_bytes_total < best_ecc_bytes_total) {
4749 best_ecc_bytes_total = ecc_bytes_total;
4750 best_step = step_size;
4751 best_strength = strength;
4752 best_ecc_bytes = ecc_bytes;
4753 }
4754 }
4755 }
4756
4757 if (best_ecc_bytes_total == INT_MAX)
4758 return -ENOTSUPP;
4759
4760 chip->ecc.size = best_step;
4761 chip->ecc.strength = best_strength;
4762 chip->ecc.bytes = best_ecc_bytes;
4763
4764 return 0;
4765}
4766EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4767
4768/**
4769 * nand_maximize_ecc - choose the max ECC strength available
4770 * @chip: nand chip info structure
4771 * @caps: ECC engine caps info structure
4772 * @oobavail: OOB size that the ECC engine can use
4773 *
4774 * Choose the max ECC strength that is supported on the controller, and can fit
4775 * within the chip's OOB. On success, the chosen ECC settings are set.
4776 */
4777int nand_maximize_ecc(struct nand_chip *chip,
4778 const struct nand_ecc_caps *caps, int oobavail)
4779{
4780 struct mtd_info *mtd = nand_to_mtd(chip);
4781 const struct nand_ecc_step_info *stepinfo;
4782 int step_size, strength, nsteps, ecc_bytes, corr;
4783 int best_corr = 0;
4784 int best_step = 0;
4785 int best_strength, best_ecc_bytes;
4786 int i, j;
4787
4788 if (WARN_ON(oobavail < 0))
4789 return -EINVAL;
4790
4791 for (i = 0; i < caps->nstepinfos; i++) {
4792 stepinfo = &caps->stepinfos[i];
4793 step_size = stepinfo->stepsize;
4794
4795 /* If chip->ecc.size is already set, respect it */
4796 if (chip->ecc.size && step_size != chip->ecc.size)
4797 continue;
4798
4799 for (j = 0; j < stepinfo->nstrengths; j++) {
4800 strength = stepinfo->strengths[j];
4801
4802 if (mtd->writesize % step_size)
4803 continue;
4804
4805 nsteps = mtd->writesize / step_size;
4806
4807 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4808 if (WARN_ON_ONCE(ecc_bytes < 0))
4809 continue;
4810
4811 if (ecc_bytes * nsteps > oobavail)
4812 continue;
4813
4814 corr = strength * nsteps;
4815
4816 /*
4817 * If the number of correctable bits is the same,
4818 * bigger step_size has more reliability.
4819 */
4820 if (corr > best_corr ||
4821 (corr == best_corr && step_size > best_step)) {
4822 best_corr = corr;
4823 best_step = step_size;
4824 best_strength = strength;
4825 best_ecc_bytes = ecc_bytes;
4826 }
4827 }
4828 }
4829
4830 if (!best_corr)
4831 return -ENOTSUPP;
4832
4833 chip->ecc.size = best_step;
4834 chip->ecc.strength = best_strength;
4835 chip->ecc.bytes = best_ecc_bytes;
4836
4837 return 0;
4838}
4839EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4840
Scott Wood3ea94ed2015-06-26 19:03:26 -05004841/*
4842 * Check if the chip configuration meet the datasheet requirements.
4843
4844 * If our configuration corrects A bits per B bytes and the minimum
4845 * required correction level is X bits per Y bytes, then we must ensure
4846 * both of the following are true:
4847 *
4848 * (1) A / B >= X / Y
4849 * (2) A >= X
4850 *
4851 * Requirement (1) ensures we can correct for the required bitflip density.
4852 * Requirement (2) ensures we can correct even when all bitflips are clumped
4853 * in the same sector.
4854 */
4855static bool nand_ecc_strength_good(struct mtd_info *mtd)
4856{
Scott Wood17fed142016-05-30 13:57:56 -05004857 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004858 struct nand_ecc_ctrl *ecc = &chip->ecc;
4859 int corr, ds_corr;
4860
4861 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4862 /* Not enough information */
4863 return true;
4864
4865 /*
4866 * We get the number of corrected bits per page to compare
4867 * the correction density.
4868 */
4869 corr = (mtd->writesize * ecc->strength) / ecc->size;
4870 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4871
4872 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4873}
William Juul52c07962007-10-31 13:53:06 +01004874
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004875static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4876{
4877 struct nand_ecc_ctrl *ecc = &chip->ecc;
4878
4879 if (nand_standard_page_accessors(ecc))
4880 return false;
4881
4882 /*
4883 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4884 * controller driver implements all the page accessors because
4885 * default helpers are not suitable when the core does not
4886 * send the READ0/PAGEPROG commands.
4887 */
4888 return (!ecc->read_page || !ecc->write_page ||
4889 !ecc->read_page_raw || !ecc->write_page_raw ||
4890 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4891 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4892 ecc->hwctl && ecc->calculate));
4893}
4894
William Juul52c07962007-10-31 13:53:06 +01004895/**
4896 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004897 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004898 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004899 * This is the second phase of the normal nand_scan() function. It fills out
4900 * all the uninitialized function pointers with the defaults and scans for a
4901 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004902 */
4903int nand_scan_tail(struct mtd_info *mtd)
4904{
4905 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004906 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004907 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004908 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004909
Sergey Lapin3a38a552013-01-14 03:46:50 +00004910 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4911 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4912 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4913
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004914 if (invalid_ecc_page_accessors(chip)) {
4915 pr_err("Invalid ECC page accessors setup\n");
4916 return -EINVAL;
4917 }
4918
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004919 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004920 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004921 chip->buffers = nbuf;
4922 } else {
4923 if (!chip->buffers)
4924 return -ENOMEM;
4925 }
William Juul52c07962007-10-31 13:53:06 +01004926
4927 /* Set the internal oob buffer location, just after the page data */
4928 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4929
4930 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004931 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004932 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004933 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004934 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02004935#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004936 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004937 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004938 break;
4939 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004940 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004941 break;
4942 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004943 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004944 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004945 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004946 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004947 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01004948#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004949 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004950 pr_warn("No oob scheme defined for oobsize %d\n",
4951 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004952 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004953 }
4954 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004955
William Juul52c07962007-10-31 13:53:06 +01004956 if (!chip->write_page)
4957 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004958
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004959 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004960 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01004961 * selected and we have 256 byte pagesize fallback to software ECC
4962 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004963
Heiko Schocherf5895d12014-06-24 10:10:04 +02004964 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004965 case NAND_ECC_HW_OOB_FIRST:
4966 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004967 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004968 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004969 BUG();
4970 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004971 if (!ecc->read_page)
4972 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004973
William Juul52c07962007-10-31 13:53:06 +01004974 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004975 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004976 if (!ecc->read_page)
4977 ecc->read_page = nand_read_page_hwecc;
4978 if (!ecc->write_page)
4979 ecc->write_page = nand_write_page_hwecc;
4980 if (!ecc->read_page_raw)
4981 ecc->read_page_raw = nand_read_page_raw;
4982 if (!ecc->write_page_raw)
4983 ecc->write_page_raw = nand_write_page_raw;
4984 if (!ecc->read_oob)
4985 ecc->read_oob = nand_read_oob_std;
4986 if (!ecc->write_oob)
4987 ecc->write_oob = nand_write_oob_std;
4988 if (!ecc->read_subpage)
4989 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05004990 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004991 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004992
William Juul52c07962007-10-31 13:53:06 +01004993 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004994 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4995 (!ecc->read_page ||
4996 ecc->read_page == nand_read_page_hwecc ||
4997 !ecc->write_page ||
4998 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004999 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005000 BUG();
5001 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005002 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005003 if (!ecc->read_page)
5004 ecc->read_page = nand_read_page_syndrome;
5005 if (!ecc->write_page)
5006 ecc->write_page = nand_write_page_syndrome;
5007 if (!ecc->read_page_raw)
5008 ecc->read_page_raw = nand_read_page_raw_syndrome;
5009 if (!ecc->write_page_raw)
5010 ecc->write_page_raw = nand_write_page_raw_syndrome;
5011 if (!ecc->read_oob)
5012 ecc->read_oob = nand_read_oob_syndrome;
5013 if (!ecc->write_oob)
5014 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005015
Heiko Schocherf5895d12014-06-24 10:10:04 +02005016 if (mtd->writesize >= ecc->size) {
5017 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005018 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5019 BUG();
5020 }
William Juul52c07962007-10-31 13:53:06 +01005021 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005022 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005023 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5024 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005025 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005026
William Juul52c07962007-10-31 13:53:06 +01005027 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005028 ecc->calculate = nand_calculate_ecc;
5029 ecc->correct = nand_correct_data;
5030 ecc->read_page = nand_read_page_swecc;
5031 ecc->read_subpage = nand_read_subpage;
5032 ecc->write_page = nand_write_page_swecc;
5033 ecc->read_page_raw = nand_read_page_raw;
5034 ecc->write_page_raw = nand_write_page_raw;
5035 ecc->read_oob = nand_read_oob_std;
5036 ecc->write_oob = nand_write_oob_std;
5037 if (!ecc->size)
5038 ecc->size = 256;
5039 ecc->bytes = 3;
5040 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005041 break;
5042
Christian Hitz55f7bca2011-10-12 09:31:59 +02005043 case NAND_ECC_SOFT_BCH:
5044 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005045 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005046 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005047 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005048 ecc->calculate = nand_bch_calculate_ecc;
5049 ecc->correct = nand_bch_correct_data;
5050 ecc->read_page = nand_read_page_swecc;
5051 ecc->read_subpage = nand_read_subpage;
5052 ecc->write_page = nand_write_page_swecc;
5053 ecc->read_page_raw = nand_read_page_raw;
5054 ecc->write_page_raw = nand_write_page_raw;
5055 ecc->read_oob = nand_read_oob_std;
5056 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005057 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005058 * Board driver should supply ecc.size and ecc.strength values
5059 * to select how many bits are correctable. Otherwise, default
5060 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005061 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005062 if (!ecc->size && (mtd->oobsize >= 64)) {
5063 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005064 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005065 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005066
5067 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005068 ecc->bytes = 0;
5069 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005070 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005071 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005072 BUG();
5073 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005074 break;
5075
William Juul52c07962007-10-31 13:53:06 +01005076 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005077 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005078 ecc->read_page = nand_read_page_raw;
5079 ecc->write_page = nand_write_page_raw;
5080 ecc->read_oob = nand_read_oob_std;
5081 ecc->read_page_raw = nand_read_page_raw;
5082 ecc->write_page_raw = nand_write_page_raw;
5083 ecc->write_oob = nand_write_oob_std;
5084 ecc->size = mtd->writesize;
5085 ecc->bytes = 0;
5086 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005087 break;
5088
5089 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005090 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005091 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005092 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005093
Sergey Lapin3a38a552013-01-14 03:46:50 +00005094 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005095 if (!ecc->read_oob_raw)
5096 ecc->read_oob_raw = ecc->read_oob;
5097 if (!ecc->write_oob_raw)
5098 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005099
William Juul52c07962007-10-31 13:53:06 +01005100 /*
5101 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005102 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005103 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005104 mtd->oobavail = 0;
5105 if (ecc->layout) {
5106 for (i = 0; ecc->layout->oobfree[i].length; i++)
5107 mtd->oobavail += ecc->layout->oobfree[i].length;
5108 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005109
Scott Wood3ea94ed2015-06-26 19:03:26 -05005110 /* ECC sanity check: warn if it's too weak */
5111 if (!nand_ecc_strength_good(mtd))
5112 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5113 mtd->name);
5114
William Juul52c07962007-10-31 13:53:06 +01005115 /*
5116 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005117 * mode.
William Juul52c07962007-10-31 13:53:06 +01005118 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005119 ecc->steps = mtd->writesize / ecc->size;
5120 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005121 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005122 BUG();
5123 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005124 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005125
Sergey Lapin3a38a552013-01-14 03:46:50 +00005126 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005127 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5128 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005129 case 2:
5130 mtd->subpage_sft = 1;
5131 break;
5132 case 4:
5133 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005134 case 16:
William Juul52c07962007-10-31 13:53:06 +01005135 mtd->subpage_sft = 2;
5136 break;
5137 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005138 }
William Juul52c07962007-10-31 13:53:06 +01005139 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005140
William Juul52c07962007-10-31 13:53:06 +01005141 /* Initialize state */
5142 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005143
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005144 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005145 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005146
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005147 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005148 switch (ecc->mode) {
5149 case NAND_ECC_SOFT:
5150 case NAND_ECC_SOFT_BCH:
5151 if (chip->page_shift > 9)
5152 chip->options |= NAND_SUBPAGE_READ;
5153 break;
5154
5155 default:
5156 break;
5157 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005158
Patrice Chotardbee32872022-03-21 09:13:36 +01005159 mtd->flash_node = chip->flash_node;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005160 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005161 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005162 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5163 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005164 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005165 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005166 mtd->_read_oob = nand_read_oob;
5167 mtd->_write_oob = nand_write_oob;
5168 mtd->_sync = nand_sync;
5169 mtd->_lock = NULL;
5170 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005171 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005172 mtd->_block_isbad = nand_block_isbad;
5173 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005174 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005175
Sergey Lapin3a38a552013-01-14 03:46:50 +00005176 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005177 mtd->ecclayout = ecc->layout;
5178 mtd->ecc_strength = ecc->strength;
5179 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005180 /*
5181 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5182 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5183 * properly set.
5184 */
5185 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005186 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005187
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005188 return 0;
William Juul52c07962007-10-31 13:53:06 +01005189}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005190EXPORT_SYMBOL(nand_scan_tail);
5191
William Juul52c07962007-10-31 13:53:06 +01005192/**
5193 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005194 * @mtd: MTD device structure
5195 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005196 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005197 * This fills out all the uninitialized function pointers with the defaults.
5198 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005199 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005200 */
5201int nand_scan(struct mtd_info *mtd, int maxchips)
5202{
5203 int ret;
5204
Lei Wen75bde942011-01-06 09:48:18 +08005205 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005206 if (!ret)
5207 ret = nand_scan_tail(mtd);
5208 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005209}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005210EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005211
Heiko Schocherf5895d12014-06-24 10:10:04 +02005212MODULE_LICENSE("GPL");
5213MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5214MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5215MODULE_DESCRIPTION("Generic NAND flash driver code");