blob: bb5460e0068bd5785ced70ece3584c74e4d215f8 [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
Dinesh Maniyamad2868f2025-02-27 00:18:28 +0800309/*
310 * nand_bbm_get_next_page - Get the next page for bad block markers
311 * @chip: The NAND chip
312 * @page: First page to start checking for bad block marker usage
313 *
314 * Returns an integer that corresponds to the page offset within a block, for
315 * a page that is used to store bad block markers. If no more pages are
316 * available, -EINVAL is returned.
317 */
318int nand_bbm_get_next_page(struct nand_chip *chip, int page)
319{
320 struct mtd_info *mtd = nand_to_mtd(chip);
321 int last_page = ((mtd->erasesize - mtd->writesize) >>
322 chip->page_shift) & chip->pagemask;
323 unsigned int bbm_flags = NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE
324 | NAND_BBM_LASTPAGE;
325
326 if (page == 0 && !(chip->options & bbm_flags))
327 return 0;
328 if (page == 0 && chip->options & NAND_BBM_FIRSTPAGE)
329 return 0;
330 if (page <= 1 && chip->options & NAND_BBM_SECONDPAGE)
331 return 1;
332 if (page <= last_page && chip->options & NAND_BBM_LASTPAGE)
333 return last_page;
334
335 return -EINVAL;
336}
337
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200338/**
339 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000340 * @mtd: MTD device structure
341 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200342 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200343 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200344 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500345static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200346{
Scott Wood17fed142016-05-30 13:57:56 -0500347 struct nand_chip *chip = mtd_to_nand(mtd);
Dinesh Maniyamad2868f2025-02-27 00:18:28 +0800348 int first_page, page_offset;
349 int res;
350 u8 bad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351
Dinesh Maniyamad2868f2025-02-27 00:18:28 +0800352 first_page = (int)(ofs >> chip->page_shift) & chip->pagemask;
353 page_offset = nand_bbm_get_next_page(chip, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200354
Dinesh Maniyamad2868f2025-02-27 00:18:28 +0800355 while (page_offset >= 0) {
356 res = chip->ecc.read_oob(mtd, chip, first_page + page_offset);
357 if (res < 0)
358 return res;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200359
Dinesh Maniyamad2868f2025-02-27 00:18:28 +0800360 bad = chip->oob_poi[chip->badblockpos];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200361
Sergey Lapin3a38a552013-01-14 03:46:50 +0000362 if (likely(chip->badblockbits == 8))
363 res = bad != 0xFF;
364 else
365 res = hweight8(bad) < chip->badblockbits;
Dinesh Maniyamad2868f2025-02-27 00:18:28 +0800366 if (res)
367 return res;
368
369 page_offset = nand_bbm_get_next_page(chip, page_offset + 1);
370 }
Christian Hitzb8a6b372011-10-12 09:32:02 +0200371
Dinesh Maniyamad2868f2025-02-27 00:18:28 +0800372 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200373}
374
375/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200376 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000377 * @mtd: MTD device structure
378 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200379 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000380 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200381 * specific driver. It provides the details for writing a bad block marker to a
382 * block.
383 */
384static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
385{
Scott Wood17fed142016-05-30 13:57:56 -0500386 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200387 struct mtd_oob_ops ops;
388 uint8_t buf[2] = { 0, 0 };
389 int ret = 0, res, i = 0;
390
Scott Wood3ea94ed2015-06-26 19:03:26 -0500391 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200392 ops.oobbuf = buf;
393 ops.ooboffs = chip->badblockpos;
394 if (chip->options & NAND_BUSWIDTH_16) {
395 ops.ooboffs &= ~0x01;
396 ops.len = ops.ooblen = 2;
397 } else {
398 ops.len = ops.ooblen = 1;
399 }
400 ops.mode = MTD_OPS_PLACE_OOB;
401
402 /* Write to first/last page(s) if necessary */
403 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
404 ofs += mtd->erasesize - mtd->writesize;
405 do {
406 res = nand_do_write_oob(mtd, ofs, &ops);
407 if (!ret)
408 ret = res;
409
410 i++;
411 ofs += mtd->writesize;
412 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
413
414 return ret;
415}
416
417/**
418 * nand_block_markbad_lowlevel - mark a block bad
419 * @mtd: MTD device structure
420 * @ofs: offset from device start
421 *
422 * This function performs the generic NAND bad block marking steps (i.e., bad
423 * block table(s) and/or marker(s)). We only allow the hardware driver to
424 * specify how to write bad block markers to OOB (chip->block_markbad).
425 *
426 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000427 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200428 * (2) write bad block marker to OOB area of affected block (unless flag
429 * NAND_BBT_NO_OOB_BBM is present)
430 * (3) update the BBT
431 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000432 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200433*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200434static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200435{
Scott Wood17fed142016-05-30 13:57:56 -0500436 struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadrosb590f612022-12-20 12:21:57 +0200437 int ret = 0;
Simon Glass7ec24132024-09-29 19:49:48 -0600438#ifndef CONFIG_XPL_BUILD
Roger Quadrosb590f612022-12-20 12:21:57 +0200439 int res;
440#endif
Christian Hitzb8a6b372011-10-12 09:32:02 +0200441
Heiko Schocherf5895d12014-06-24 10:10:04 +0200442 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000443 struct erase_info einfo;
444
445 /* Attempt erase before marking OOB */
446 memset(&einfo, 0, sizeof(einfo));
447 einfo.mtd = mtd;
448 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200449 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000450 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200451
Heiko Schocherf5895d12014-06-24 10:10:04 +0200452 /* Write bad block marker to OOB */
453 nand_get_device(mtd, FL_WRITING);
454 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500455 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100456 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000457
Simon Glass7ec24132024-09-29 19:49:48 -0600458#ifndef CONFIG_XPL_BUILD
Heiko Schocherf5895d12014-06-24 10:10:04 +0200459 /* Mark block bad in BBT */
460 if (chip->bbt) {
461 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000462 if (!ret)
463 ret = res;
464 }
Roger Quadrosb590f612022-12-20 12:21:57 +0200465#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +0000466
William Juul52c07962007-10-31 13:53:06 +0100467 if (!ret)
468 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500469
William Juul52c07962007-10-31 13:53:06 +0100470 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200471}
472
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200473/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200474 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000475 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200476 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000477 * Check, if the device is write protected. The function expects, that the
478 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200479 */
William Juul52c07962007-10-31 13:53:06 +0100480static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200481{
Scott Wood17fed142016-05-30 13:57:56 -0500482 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100483 u8 status;
484 int ret;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200485
Sergey Lapin3a38a552013-01-14 03:46:50 +0000486 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200487 if (chip->options & NAND_BROKEN_XD)
488 return 0;
489
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200490 /* Check the WP bit */
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100491 ret = nand_status_op(chip, &status);
492 if (ret)
493 return ret;
494
495 return status & NAND_STATUS_WP ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200496}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100497
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200498/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500499 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000500 * @mtd: MTD device structure
501 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300502 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500503 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300504 */
505static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
506{
Scott Wood17fed142016-05-30 13:57:56 -0500507 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300508
509 if (!chip->bbt)
510 return 0;
511 /* Return info from the table */
Simon Glass7ec24132024-09-29 19:49:48 -0600512#ifndef CONFIG_XPL_BUILD
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300513 return nand_isreserved_bbt(mtd, ofs);
Roger Quadrosb590f612022-12-20 12:21:57 +0200514#else
515 return 0;
516#endif
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300517}
518
519/**
520 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
521 * @mtd: MTD device structure
522 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000523 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200524 *
525 * Check, if the block is bad. Either by reading the bad block table or
526 * calling of the scan function.
527 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500528static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200529{
Scott Wood17fed142016-05-30 13:57:56 -0500530 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200531
Masahiro Yamada8d100542014-12-26 22:20:58 +0900532 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
533 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200534 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900535 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200536 }
537
William Juul52c07962007-10-31 13:53:06 +0100538 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500539 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200540
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200541 /* Return info from the table */
Simon Glass7ec24132024-09-29 19:49:48 -0600542#ifndef CONFIG_XPL_BUILD
William Juul52c07962007-10-31 13:53:06 +0100543 return nand_isbad_bbt(mtd, ofs, allowbbt);
Roger Quadrosb590f612022-12-20 12:21:57 +0200544#else
545 return 0;
546#endif
William Juul52c07962007-10-31 13:53:06 +0100547}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200548
Scott Wood52ab7ce2016-05-30 13:57:58 -0500549/**
550 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
551 * @mtd: MTD device structure
552 *
553 * Wait for the ready pin after a command, and warn if a timeout occurs.
554 */
William Juul52c07962007-10-31 13:53:06 +0100555void nand_wait_ready(struct mtd_info *mtd)
556{
Scott Wood17fed142016-05-30 13:57:56 -0500557 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500558 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000559 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100560
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000561 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000562 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000563 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100564 if (chip->dev_ready)
565 if (chip->dev_ready(mtd))
566 break;
567 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500568
569 if (!chip->dev_ready(mtd))
570 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200571}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200572EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200573
574/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500575 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
576 * @mtd: MTD device structure
577 * @timeo: Timeout in ms
578 *
579 * Wait for status ready (i.e. command done) or timeout.
580 */
581static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
582{
Scott Wood17fed142016-05-30 13:57:56 -0500583 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500584 u32 time_start;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100585 int ret;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500586
587 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
588 time_start = get_timer(0);
589 while (get_timer(time_start) < timeo) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100590 u8 status;
591
592 ret = nand_read_data_op(chip, &status, sizeof(status), true);
593 if (ret)
594 return;
595
596 if (status & NAND_STATUS_READY)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500597 break;
Stefan Roese80877fa2022-09-02 14:10:46 +0200598 schedule();
Scott Wood3ea94ed2015-06-26 19:03:26 -0500599 }
600};
601
602/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000604 * @mtd: MTD device structure
605 * @command: the command to be sent
606 * @column: the column address for this command, -1 if none
607 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200608 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000609 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200610 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 */
William Juul52c07962007-10-31 13:53:06 +0100612static void nand_command(struct mtd_info *mtd, unsigned int command,
613 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200614{
Scott Wood17fed142016-05-30 13:57:56 -0500615 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100616 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617
Sergey Lapin3a38a552013-01-14 03:46:50 +0000618 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200619 if (command == NAND_CMD_SEQIN) {
620 int readcmd;
621
William Juul52c07962007-10-31 13:53:06 +0100622 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100624 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200625 readcmd = NAND_CMD_READOOB;
626 } else if (column < 256) {
627 /* First 256 bytes --> READ0 */
628 readcmd = NAND_CMD_READ0;
629 } else {
630 column -= 256;
631 readcmd = NAND_CMD_READ1;
632 }
William Juul52c07962007-10-31 13:53:06 +0100633 chip->cmd_ctrl(mtd, readcmd, ctrl);
634 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200635 }
William Juul52c07962007-10-31 13:53:06 +0100636 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200637
Sergey Lapin3a38a552013-01-14 03:46:50 +0000638 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100639 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
640 /* Serially input address */
641 if (column != -1) {
642 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200643 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530644 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100645 column >>= 1;
646 chip->cmd_ctrl(mtd, column, ctrl);
647 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200648 }
William Juul52c07962007-10-31 13:53:06 +0100649 if (page_addr != -1) {
650 chip->cmd_ctrl(mtd, page_addr, ctrl);
651 ctrl &= ~NAND_CTRL_CHANGE;
652 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900653 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100654 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
655 }
656 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200657
658 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000659 * Program and erase have their own busy handlers status and sequential
660 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100661 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200662 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200663
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200664 case NAND_CMD_PAGEPROG:
665 case NAND_CMD_ERASE1:
666 case NAND_CMD_ERASE2:
667 case NAND_CMD_SEQIN:
668 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900669 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900670 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200671 return;
672
673 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100674 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200675 break;
William Juul52c07962007-10-31 13:53:06 +0100676 udelay(chip->chip_delay);
677 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
678 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
679 chip->cmd_ctrl(mtd,
680 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500681 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
682 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200683 return;
684
William Juul52c07962007-10-31 13:53:06 +0100685 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200687 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200688 * If we don't have access to the busy pin, we apply the given
689 * command delay
William Juul52c07962007-10-31 13:53:06 +0100690 */
691 if (!chip->dev_ready) {
692 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200693 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200694 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200695 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000696 /*
697 * Apply this short delay always to ensure that we do wait tWB in
698 * any case on any machine.
699 */
William Juul52c07962007-10-31 13:53:06 +0100700 ndelay(100);
701
702 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200703}
704
705/**
706 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000707 * @mtd: MTD device structure
708 * @command: the command to be sent
709 * @column: the column address for this command, -1 if none
710 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200711 *
William Juul52c07962007-10-31 13:53:06 +0100712 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000713 * devices. We don't have the separate regions as we have in the small page
714 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715 */
William Juul52c07962007-10-31 13:53:06 +0100716static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
717 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200718{
Scott Wood17fed142016-05-30 13:57:56 -0500719 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200720
721 /* Emulate NAND_CMD_READOOB */
722 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100723 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 command = NAND_CMD_READ0;
725 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200726
William Juul52c07962007-10-31 13:53:06 +0100727 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200728 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200729
730 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100731 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732
733 /* Serially input address */
734 if (column != -1) {
735 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200736 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530737 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200738 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100739 chip->cmd_ctrl(mtd, column, ctrl);
740 ctrl &= ~NAND_CTRL_CHANGE;
741 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200742 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200743 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100744 chip->cmd_ctrl(mtd, page_addr, ctrl);
745 chip->cmd_ctrl(mtd, page_addr >> 8,
746 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900747 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100748 chip->cmd_ctrl(mtd, page_addr >> 16,
749 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200751 }
William Juul52c07962007-10-31 13:53:06 +0100752 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200753
754 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000755 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500756 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100757 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200758 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200759
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200760 case NAND_CMD_CACHEDPROG:
761 case NAND_CMD_PAGEPROG:
762 case NAND_CMD_ERASE1:
763 case NAND_CMD_ERASE2:
764 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100765 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200766 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900767 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900768 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200769 return;
770
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200771 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100772 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200773 break;
William Juul52c07962007-10-31 13:53:06 +0100774 udelay(chip->chip_delay);
775 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
776 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
777 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
778 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500779 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
780 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100781 return;
782
783 case NAND_CMD_RNDOUT:
784 /* No ready / busy check necessary */
785 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
786 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
787 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
788 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200789 return;
790
791 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100792 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
793 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
794 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
795 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200796
William Juul52c07962007-10-31 13:53:06 +0100797 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200798 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200799 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200800 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000801 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100802 */
803 if (!chip->dev_ready) {
804 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200805 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200806 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200807 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200808
Sergey Lapin3a38a552013-01-14 03:46:50 +0000809 /*
810 * Apply this short delay always to ensure that we do wait tWB in
811 * any case on any machine.
812 */
William Juul52c07962007-10-31 13:53:06 +0100813 ndelay(100);
814
815 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200816}
817
818/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200819 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000820 * @chip: the nand chip descriptor
821 * @mtd: MTD device structure
822 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200823 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200824 * Used when in panic, no locks are taken.
825 */
826static void panic_nand_get_device(struct nand_chip *chip,
827 struct mtd_info *mtd, int new_state)
828{
829 /* Hardware controller shared among independent devices */
830 chip->controller->active = chip;
831 chip->state = new_state;
832}
833
834/**
835 * nand_get_device - [GENERIC] Get chip for selected access
836 * @mtd: MTD device structure
837 * @new_state: the state which is requested
838 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200839 * Get the device and lock it for exclusive access
840 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200841static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200842nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100843{
Scott Wood17fed142016-05-30 13:57:56 -0500844 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200845 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100846 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200847}
848
849/**
850 * panic_nand_wait - [GENERIC] wait until the command is done
851 * @mtd: MTD device structure
852 * @chip: NAND chip structure
853 * @timeo: timeout
854 *
855 * Wait for command done. This is a helper function for nand_wait used when
856 * we are in interrupt context. May happen when in panic and trying to write
857 * an oops through mtdoops.
858 */
859static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
860 unsigned long timeo)
861{
862 int i;
863 for (i = 0; i < timeo; i++) {
864 if (chip->dev_ready) {
865 if (chip->dev_ready(mtd))
866 break;
867 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100868 int ret;
869 u8 status;
870
871 ret = nand_read_data_op(chip, &status, sizeof(status),
872 true);
873 if (ret)
874 return;
875
876 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200877 break;
878 }
879 mdelay(1);
880 }
William Juul52c07962007-10-31 13:53:06 +0100881}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200882
883/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000884 * nand_wait - [DEFAULT] wait until the command is done
885 * @mtd: MTD device structure
886 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200887 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500888 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100889 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200890static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200891{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500892 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100893 u8 status;
894 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100895
Heiko Schocherf5895d12014-06-24 10:10:04 +0200896 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100897
Heiko Schocherf5895d12014-06-24 10:10:04 +0200898 /*
899 * Apply this short delay always to ensure that we do wait tWB in any
900 * case on any machine.
901 */
902 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100903
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100904 ret = nand_status_op(chip, NULL);
905 if (ret)
906 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100907
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200908 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
909 u32 time_start;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200910
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200911 time_start = get_timer(0);
912 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200913 if (chip->dev_ready) {
914 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100915 break;
916 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100917 ret = nand_read_data_op(chip, &status,
918 sizeof(status), true);
919 if (ret)
920 return ret;
921
922 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100923 break;
924 }
925 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200926 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100927
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100928 ret = nand_read_data_op(chip, &status, sizeof(status), true);
929 if (ret)
930 return ret;
931
Heiko Schocherf5895d12014-06-24 10:10:04 +0200932 /* This can happen if in case of timeout or buggy dev_ready */
933 WARN_ON(!(status & NAND_STATUS_READY));
934 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200935}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500936
Scott Wood52ab7ce2016-05-30 13:57:58 -0500937/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900938 * nand_reset_data_interface - Reset data interface and timings
939 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900940 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900941 *
942 * Reset the Data interface and timings to ONFI mode 0.
943 *
944 * Returns 0 for success or negative error code otherwise.
945 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900946static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900947{
948 struct mtd_info *mtd = nand_to_mtd(chip);
949 const struct nand_data_interface *conf;
950 int ret;
951
952 if (!chip->setup_data_interface)
953 return 0;
954
955 /*
956 * The ONFI specification says:
957 * "
958 * To transition from NV-DDR or NV-DDR2 to the SDR data
959 * interface, the host shall use the Reset (FFh) command
960 * using SDR timing mode 0. A device in any timing mode is
961 * required to recognize Reset (FFh) command issued in SDR
962 * timing mode 0.
963 * "
964 *
965 * Configure the data interface in SDR mode and set the
966 * timings to timing mode 0.
967 */
968
969 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900970 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900971 if (ret)
972 pr_err("Failed to configure data interface to SDR timing mode 0\n");
973
974 return ret;
975}
976
Kory Maincent0cda0cb2022-06-22 11:11:45 +0200977static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip)
978{
979 if (!chip->onfi_version ||
980 !(le16_to_cpu(chip->onfi_params.opt_cmd)
981 & ONFI_OPT_CMD_SET_GET_FEATURES))
982 return 0;
983
984 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
985 chip->onfi_timing_mode_default,
986 };
987
988 return chip->onfi_set_features(mtd, chip,
989 ONFI_FEATURE_ADDR_TIMING_MODE,
990 tmode_param);
991}
992
Boris Brezillone509cba2017-11-22 02:38:19 +0900993/**
994 * nand_setup_data_interface - Setup the best data interface and timings
995 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900996 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900997 *
998 * Find and configure the best data interface and NAND timings supported by
999 * the chip and the driver.
1000 * First tries to retrieve supported timing modes from ONFI information,
1001 * and if the NAND chip does not support ONFI, relies on the
1002 * ->onfi_timing_mode_default specified in the nand_ids table.
1003 *
1004 * Returns 0 for success or negative error code otherwise.
1005 */
Boris Brezillon32935f42017-11-22 02:38:28 +09001006static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +09001007{
1008 struct mtd_info *mtd = nand_to_mtd(chip);
1009 int ret;
1010
1011 if (!chip->setup_data_interface || !chip->data_interface)
1012 return 0;
1013
1014 /*
1015 * Ensure the timing mode has been changed on the chip side
1016 * before changing timings on the controller side.
1017 */
Kory Maincent0cda0cb2022-06-22 11:11:45 +02001018 ret = nand_onfi_set_timings(mtd, chip);
1019 if (ret)
1020 goto err;
Boris Brezillone509cba2017-11-22 02:38:19 +09001021
Boris Brezillon32935f42017-11-22 02:38:28 +09001022 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001023err:
1024 return ret;
1025}
1026
1027/**
1028 * nand_init_data_interface - find the best data interface and timings
1029 * @chip: The NAND chip
1030 *
1031 * Find the best data interface and NAND timings supported by the chip
1032 * and the driver.
1033 * First tries to retrieve supported timing modes from ONFI information,
1034 * and if the NAND chip does not support ONFI, relies on the
1035 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1036 * function nand_chip->data_interface is initialized with the best timing mode
1037 * available.
1038 *
1039 * Returns 0 for success or negative error code otherwise.
1040 */
1041static int nand_init_data_interface(struct nand_chip *chip)
1042{
1043 struct mtd_info *mtd = nand_to_mtd(chip);
1044 int modes, mode, ret;
1045
1046 if (!chip->setup_data_interface)
1047 return 0;
1048
1049 /*
1050 * First try to identify the best timings from ONFI parameters and
1051 * if the NAND does not support ONFI, fallback to the default ONFI
1052 * timing mode.
1053 */
1054 modes = onfi_get_async_timing_mode(chip);
1055 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1056 if (!chip->onfi_timing_mode_default)
1057 return 0;
1058
1059 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1060 }
1061
1062 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1063 GFP_KERNEL);
1064 if (!chip->data_interface)
1065 return -ENOMEM;
1066
1067 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1068 ret = onfi_init_data_interface(chip, chip->data_interface,
1069 NAND_SDR_IFACE, mode);
1070 if (ret)
1071 continue;
1072
Boris Brezillon32935f42017-11-22 02:38:28 +09001073 /* Pass -1 to only */
1074 ret = chip->setup_data_interface(mtd,
1075 NAND_DATA_IFACE_CHECK_ONLY,
1076 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001077 if (!ret) {
1078 chip->onfi_timing_mode_default = mode;
1079 break;
1080 }
1081 }
1082
1083 return 0;
1084}
1085
1086static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1087{
1088 kfree(chip->data_interface);
1089}
1090
1091/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001092 * nand_read_page_op - Do a READ PAGE operation
1093 * @chip: The NAND chip
1094 * @page: page to read
1095 * @offset_in_page: offset within the page
1096 * @buf: buffer used to store the data
1097 * @len: length of the buffer
1098 *
1099 * This function issues a READ PAGE operation.
1100 * This function does not select/unselect the CS line.
1101 *
1102 * Returns 0 on success, a negative error code otherwise.
1103 */
1104int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1105 unsigned int offset_in_page, void *buf, unsigned int len)
1106{
1107 struct mtd_info *mtd = nand_to_mtd(chip);
1108
1109 if (len && !buf)
1110 return -EINVAL;
1111
1112 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1113 return -EINVAL;
1114
1115 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1116 if (len)
1117 chip->read_buf(mtd, buf, len);
1118
1119 return 0;
1120}
1121EXPORT_SYMBOL_GPL(nand_read_page_op);
1122
1123/**
1124 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1125 * @chip: The NAND chip
1126 * @page: parameter page to read
1127 * @buf: buffer used to store the data
1128 * @len: length of the buffer
1129 *
1130 * This function issues a READ PARAMETER PAGE operation.
1131 * This function does not select/unselect the CS line.
1132 *
1133 * Returns 0 on success, a negative error code otherwise.
1134 */
1135static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1136 unsigned int len)
1137{
1138 struct mtd_info *mtd = nand_to_mtd(chip);
1139 unsigned int i;
1140 u8 *p = buf;
1141
1142 if (len && !buf)
1143 return -EINVAL;
1144
1145 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1146 for (i = 0; i < len; i++)
1147 p[i] = chip->read_byte(mtd);
1148
1149 return 0;
1150}
1151
1152/**
1153 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1154 * @chip: The NAND chip
1155 * @offset_in_page: offset within the page
1156 * @buf: buffer used to store the data
1157 * @len: length of the buffer
1158 * @force_8bit: force 8-bit bus access
1159 *
1160 * This function issues a CHANGE READ COLUMN operation.
1161 * This function does not select/unselect the CS line.
1162 *
1163 * Returns 0 on success, a negative error code otherwise.
1164 */
1165int nand_change_read_column_op(struct nand_chip *chip,
1166 unsigned int offset_in_page, void *buf,
1167 unsigned int len, bool force_8bit)
1168{
1169 struct mtd_info *mtd = nand_to_mtd(chip);
1170
1171 if (len && !buf)
1172 return -EINVAL;
1173
1174 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1175 return -EINVAL;
1176
1177 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1178 if (len)
1179 chip->read_buf(mtd, buf, len);
1180
1181 return 0;
1182}
1183EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1184
1185/**
1186 * nand_read_oob_op - Do a READ OOB operation
1187 * @chip: The NAND chip
1188 * @page: page to read
1189 * @offset_in_oob: offset within the OOB area
1190 * @buf: buffer used to store the data
1191 * @len: length of the buffer
1192 *
1193 * This function issues a READ OOB operation.
1194 * This function does not select/unselect the CS line.
1195 *
1196 * Returns 0 on success, a negative error code otherwise.
1197 */
1198int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1199 unsigned int offset_in_oob, void *buf, unsigned int len)
1200{
1201 struct mtd_info *mtd = nand_to_mtd(chip);
1202
1203 if (len && !buf)
1204 return -EINVAL;
1205
1206 if (offset_in_oob + len > mtd->oobsize)
1207 return -EINVAL;
1208
1209 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1210 if (len)
1211 chip->read_buf(mtd, buf, len);
1212
1213 return 0;
1214}
1215EXPORT_SYMBOL_GPL(nand_read_oob_op);
1216
1217/**
1218 * nand_prog_page_begin_op - starts a PROG PAGE operation
1219 * @chip: The NAND chip
1220 * @page: page to write
1221 * @offset_in_page: offset within the page
1222 * @buf: buffer containing the data to write to the page
1223 * @len: length of the buffer
1224 *
1225 * This function issues the first half of a PROG PAGE operation.
1226 * This function does not select/unselect the CS line.
1227 *
1228 * Returns 0 on success, a negative error code otherwise.
1229 */
1230int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1231 unsigned int offset_in_page, const void *buf,
1232 unsigned int len)
1233{
1234 struct mtd_info *mtd = nand_to_mtd(chip);
1235
1236 if (len && !buf)
1237 return -EINVAL;
1238
1239 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1240 return -EINVAL;
1241
1242 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1243
1244 if (buf)
1245 chip->write_buf(mtd, buf, len);
1246
1247 return 0;
1248}
1249EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1250
1251/**
1252 * nand_prog_page_end_op - ends a PROG PAGE operation
1253 * @chip: The NAND chip
1254 *
1255 * This function issues the second half of a PROG PAGE operation.
1256 * This function does not select/unselect the CS line.
1257 *
1258 * Returns 0 on success, a negative error code otherwise.
1259 */
1260int nand_prog_page_end_op(struct nand_chip *chip)
1261{
1262 struct mtd_info *mtd = nand_to_mtd(chip);
1263 int status;
1264
1265 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1266
1267 status = chip->waitfunc(mtd, chip);
1268 if (status & NAND_STATUS_FAIL)
1269 return -EIO;
1270
1271 return 0;
1272}
1273EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1274
1275/**
1276 * nand_prog_page_op - Do a full PROG PAGE operation
1277 * @chip: The NAND chip
1278 * @page: page to write
1279 * @offset_in_page: offset within the page
1280 * @buf: buffer containing the data to write to the page
1281 * @len: length of the buffer
1282 *
1283 * This function issues a full PROG PAGE operation.
1284 * This function does not select/unselect the CS line.
1285 *
1286 * Returns 0 on success, a negative error code otherwise.
1287 */
1288int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1289 unsigned int offset_in_page, const void *buf,
1290 unsigned int len)
1291{
1292 struct mtd_info *mtd = nand_to_mtd(chip);
1293 int status;
1294
1295 if (!len || !buf)
1296 return -EINVAL;
1297
1298 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1299 return -EINVAL;
1300
1301 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1302 chip->write_buf(mtd, buf, len);
1303 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1304
1305 status = chip->waitfunc(mtd, chip);
1306 if (status & NAND_STATUS_FAIL)
1307 return -EIO;
1308
1309 return 0;
1310}
1311EXPORT_SYMBOL_GPL(nand_prog_page_op);
1312
1313/**
1314 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1315 * @chip: The NAND chip
1316 * @offset_in_page: offset within the page
1317 * @buf: buffer containing the data to send to the NAND
1318 * @len: length of the buffer
1319 * @force_8bit: force 8-bit bus access
1320 *
1321 * This function issues a CHANGE WRITE COLUMN operation.
1322 * This function does not select/unselect the CS line.
1323 *
1324 * Returns 0 on success, a negative error code otherwise.
1325 */
1326int nand_change_write_column_op(struct nand_chip *chip,
1327 unsigned int offset_in_page,
1328 const void *buf, unsigned int len,
1329 bool force_8bit)
1330{
1331 struct mtd_info *mtd = nand_to_mtd(chip);
1332
1333 if (len && !buf)
1334 return -EINVAL;
1335
1336 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1337 return -EINVAL;
1338
1339 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1340 if (len)
1341 chip->write_buf(mtd, buf, len);
1342
1343 return 0;
1344}
1345EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1346
1347/**
1348 * nand_readid_op - Do a READID operation
1349 * @chip: The NAND chip
1350 * @addr: address cycle to pass after the READID command
1351 * @buf: buffer used to store the ID
1352 * @len: length of the buffer
1353 *
1354 * This function sends a READID command and reads back the ID returned by the
1355 * NAND.
1356 * This function does not select/unselect the CS line.
1357 *
1358 * Returns 0 on success, a negative error code otherwise.
1359 */
1360int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1361 unsigned int len)
1362{
1363 struct mtd_info *mtd = nand_to_mtd(chip);
1364 unsigned int i;
1365 u8 *id = buf;
1366
1367 if (len && !buf)
1368 return -EINVAL;
1369
1370 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1371
1372 for (i = 0; i < len; i++)
1373 id[i] = chip->read_byte(mtd);
1374
1375 return 0;
1376}
1377EXPORT_SYMBOL_GPL(nand_readid_op);
1378
1379/**
1380 * nand_status_op - Do a STATUS operation
1381 * @chip: The NAND chip
1382 * @status: out variable to store the NAND status
1383 *
1384 * This function sends a STATUS command and reads back the status returned by
1385 * the NAND.
1386 * This function does not select/unselect the CS line.
1387 *
1388 * Returns 0 on success, a negative error code otherwise.
1389 */
1390int nand_status_op(struct nand_chip *chip, u8 *status)
1391{
1392 struct mtd_info *mtd = nand_to_mtd(chip);
1393
1394 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1395 if (status)
1396 *status = chip->read_byte(mtd);
1397
1398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(nand_status_op);
1401
1402/**
1403 * nand_exit_status_op - Exit a STATUS operation
1404 * @chip: The NAND chip
1405 *
1406 * This function sends a READ0 command to cancel the effect of the STATUS
1407 * command to avoid reading only the status until a new read command is sent.
1408 *
1409 * This function does not select/unselect the CS line.
1410 *
1411 * Returns 0 on success, a negative error code otherwise.
1412 */
1413int nand_exit_status_op(struct nand_chip *chip)
1414{
1415 struct mtd_info *mtd = nand_to_mtd(chip);
1416
1417 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1418
1419 return 0;
1420}
1421EXPORT_SYMBOL_GPL(nand_exit_status_op);
1422
1423/**
1424 * nand_erase_op - Do an erase operation
1425 * @chip: The NAND chip
1426 * @eraseblock: block to erase
1427 *
1428 * This function sends an ERASE command and waits for the NAND to be ready
1429 * before returning.
1430 * This function does not select/unselect the CS line.
1431 *
1432 * Returns 0 on success, a negative error code otherwise.
1433 */
1434int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1435{
1436 struct mtd_info *mtd = nand_to_mtd(chip);
1437 unsigned int page = eraseblock <<
1438 (chip->phys_erase_shift - chip->page_shift);
1439 int status;
1440
1441 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1442 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1443
1444 status = chip->waitfunc(mtd, chip);
1445 if (status < 0)
1446 return status;
1447
1448 if (status & NAND_STATUS_FAIL)
1449 return -EIO;
1450
1451 return 0;
1452}
1453EXPORT_SYMBOL_GPL(nand_erase_op);
1454
1455/**
1456 * nand_set_features_op - Do a SET FEATURES operation
1457 * @chip: The NAND chip
1458 * @feature: feature id
1459 * @data: 4 bytes of data
1460 *
1461 * This function sends a SET FEATURES command and waits for the NAND to be
1462 * ready before returning.
1463 * This function does not select/unselect the CS line.
1464 *
1465 * Returns 0 on success, a negative error code otherwise.
1466 */
1467static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1468 const void *data)
1469{
1470 struct mtd_info *mtd = nand_to_mtd(chip);
1471 const u8 *params = data;
1472 int i, status;
1473
1474 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1475 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1476 chip->write_byte(mtd, params[i]);
1477
1478 status = chip->waitfunc(mtd, chip);
1479 if (status & NAND_STATUS_FAIL)
1480 return -EIO;
1481
1482 return 0;
1483}
1484
1485/**
1486 * nand_get_features_op - Do a GET FEATURES operation
1487 * @chip: The NAND chip
1488 * @feature: feature id
1489 * @data: 4 bytes of data
1490 *
1491 * This function sends a GET FEATURES command and waits for the NAND to be
1492 * ready before returning.
1493 * This function does not select/unselect the CS line.
1494 *
1495 * Returns 0 on success, a negative error code otherwise.
1496 */
1497static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1498 void *data)
1499{
1500 struct mtd_info *mtd = nand_to_mtd(chip);
1501 u8 *params = data;
1502 int i;
1503
1504 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1505 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1506 params[i] = chip->read_byte(mtd);
1507
1508 return 0;
1509}
1510
1511/**
1512 * nand_reset_op - Do a reset operation
1513 * @chip: The NAND chip
1514 *
1515 * This function sends a RESET command and waits for the NAND to be ready
1516 * before returning.
1517 * This function does not select/unselect the CS line.
1518 *
1519 * Returns 0 on success, a negative error code otherwise.
1520 */
1521int nand_reset_op(struct nand_chip *chip)
1522{
1523 struct mtd_info *mtd = nand_to_mtd(chip);
1524
1525 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1526
1527 return 0;
1528}
1529EXPORT_SYMBOL_GPL(nand_reset_op);
1530
1531/**
1532 * nand_read_data_op - Read data from the NAND
1533 * @chip: The NAND chip
1534 * @buf: buffer used to store the data
1535 * @len: length of the buffer
1536 * @force_8bit: force 8-bit bus access
1537 *
1538 * This function does a raw data read on the bus. Usually used after launching
1539 * another NAND operation like nand_read_page_op().
1540 * This function does not select/unselect the CS line.
1541 *
1542 * Returns 0 on success, a negative error code otherwise.
1543 */
1544int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1545 bool force_8bit)
1546{
1547 struct mtd_info *mtd = nand_to_mtd(chip);
1548
1549 if (!len || !buf)
1550 return -EINVAL;
1551
1552 if (force_8bit) {
1553 u8 *p = buf;
1554 unsigned int i;
1555
1556 for (i = 0; i < len; i++)
1557 p[i] = chip->read_byte(mtd);
1558 } else {
1559 chip->read_buf(mtd, buf, len);
1560 }
1561
1562 return 0;
1563}
1564EXPORT_SYMBOL_GPL(nand_read_data_op);
1565
1566/**
1567 * nand_write_data_op - Write data from the NAND
1568 * @chip: The NAND chip
1569 * @buf: buffer containing the data to send on the bus
1570 * @len: length of the buffer
1571 * @force_8bit: force 8-bit bus access
1572 *
1573 * This function does a raw data write on the bus. Usually used after launching
1574 * another NAND operation like nand_write_page_begin_op().
1575 * This function does not select/unselect the CS line.
1576 *
1577 * Returns 0 on success, a negative error code otherwise.
1578 */
1579int nand_write_data_op(struct nand_chip *chip, const void *buf,
1580 unsigned int len, bool force_8bit)
1581{
1582 struct mtd_info *mtd = nand_to_mtd(chip);
1583
1584 if (!len || !buf)
1585 return -EINVAL;
1586
1587 if (force_8bit) {
1588 const u8 *p = buf;
1589 unsigned int i;
1590
1591 for (i = 0; i < len; i++)
1592 chip->write_byte(mtd, p[i]);
1593 } else {
1594 chip->write_buf(mtd, buf, len);
1595 }
1596
1597 return 0;
1598}
1599EXPORT_SYMBOL_GPL(nand_write_data_op);
1600
1601/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001602 * nand_reset - Reset and initialize a NAND device
1603 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001604 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001605 *
1606 * Returns 0 for success or negative error code otherwise
1607 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001608int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001609{
1610 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001611 int ret;
1612
Boris Brezillon32935f42017-11-22 02:38:28 +09001613 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001614 if (ret)
1615 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001616
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001617 /*
1618 * The CS line has to be released before we can apply the new NAND
1619 * interface settings, hence this weird ->select_chip() dance.
1620 */
1621 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001622 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001623 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001624 if (ret)
1625 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001626
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001627 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001628 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001629 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001630 if (ret)
1631 return ret;
1632
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001633 return 0;
1634}
1635
1636/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001637 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1638 * @buf: buffer to test
1639 * @len: buffer length
1640 * @bitflips_threshold: maximum number of bitflips
1641 *
1642 * Check if a buffer contains only 0xff, which means the underlying region
1643 * has been erased and is ready to be programmed.
1644 * The bitflips_threshold specify the maximum number of bitflips before
1645 * considering the region is not erased.
1646 * Note: The logic of this function has been extracted from the memweight
1647 * implementation, except that nand_check_erased_buf function exit before
1648 * testing the whole buffer if the number of bitflips exceed the
1649 * bitflips_threshold value.
1650 *
1651 * Returns a positive number of bitflips less than or equal to
1652 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1653 * threshold.
1654 */
1655static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1656{
1657 const unsigned char *bitmap = buf;
1658 int bitflips = 0;
1659 int weight;
1660
1661 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1662 len--, bitmap++) {
1663 weight = hweight8(*bitmap);
1664 bitflips += BITS_PER_BYTE - weight;
1665 if (unlikely(bitflips > bitflips_threshold))
1666 return -EBADMSG;
1667 }
1668
1669 for (; len >= 4; len -= 4, bitmap += 4) {
1670 weight = hweight32(*((u32 *)bitmap));
1671 bitflips += 32 - weight;
1672 if (unlikely(bitflips > bitflips_threshold))
1673 return -EBADMSG;
1674 }
1675
1676 for (; len > 0; len--, bitmap++) {
1677 weight = hweight8(*bitmap);
1678 bitflips += BITS_PER_BYTE - weight;
1679 if (unlikely(bitflips > bitflips_threshold))
1680 return -EBADMSG;
1681 }
1682
1683 return bitflips;
1684}
1685
1686/**
1687 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1688 * 0xff data
1689 * @data: data buffer to test
1690 * @datalen: data length
1691 * @ecc: ECC buffer
1692 * @ecclen: ECC length
1693 * @extraoob: extra OOB buffer
1694 * @extraooblen: extra OOB length
1695 * @bitflips_threshold: maximum number of bitflips
1696 *
1697 * Check if a data buffer and its associated ECC and OOB data contains only
1698 * 0xff pattern, which means the underlying region has been erased and is
1699 * ready to be programmed.
1700 * The bitflips_threshold specify the maximum number of bitflips before
1701 * considering the region as not erased.
1702 *
1703 * Note:
1704 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1705 * different from the NAND page size. When fixing bitflips, ECC engines will
1706 * report the number of errors per chunk, and the NAND core infrastructure
1707 * expect you to return the maximum number of bitflips for the whole page.
1708 * This is why you should always use this function on a single chunk and
1709 * not on the whole page. After checking each chunk you should update your
1710 * max_bitflips value accordingly.
1711 * 2/ When checking for bitflips in erased pages you should not only check
1712 * the payload data but also their associated ECC data, because a user might
1713 * have programmed almost all bits to 1 but a few. In this case, we
1714 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1715 * this case.
1716 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1717 * data are protected by the ECC engine.
1718 * It could also be used if you support subpages and want to attach some
1719 * extra OOB data to an ECC chunk.
1720 *
1721 * Returns a positive number of bitflips less than or equal to
1722 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1723 * threshold. In case of success, the passed buffers are filled with 0xff.
1724 */
1725int nand_check_erased_ecc_chunk(void *data, int datalen,
1726 void *ecc, int ecclen,
1727 void *extraoob, int extraooblen,
1728 int bitflips_threshold)
1729{
1730 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1731
1732 data_bitflips = nand_check_erased_buf(data, datalen,
1733 bitflips_threshold);
1734 if (data_bitflips < 0)
1735 return data_bitflips;
1736
1737 bitflips_threshold -= data_bitflips;
1738
1739 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1740 if (ecc_bitflips < 0)
1741 return ecc_bitflips;
1742
1743 bitflips_threshold -= ecc_bitflips;
1744
1745 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1746 bitflips_threshold);
1747 if (extraoob_bitflips < 0)
1748 return extraoob_bitflips;
1749
1750 if (data_bitflips)
1751 memset(data, 0xff, datalen);
1752
1753 if (ecc_bitflips)
1754 memset(ecc, 0xff, ecclen);
1755
1756 if (extraoob_bitflips)
1757 memset(extraoob, 0xff, extraooblen);
1758
1759 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1760}
1761EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001762
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001763/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001764 * nand_read_page_raw - [INTERN] read raw page data without ecc
1765 * @mtd: mtd info structure
1766 * @chip: nand chip info structure
1767 * @buf: buffer to store read data
1768 * @oob_required: caller requires OOB data read to chip->oob_poi
1769 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001770 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001771 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001772 */
William Juul52c07962007-10-31 13:53:06 +01001773static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001774 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001775{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001776 int ret;
1777
1778 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1779 if (ret)
1780 return ret;
1781
1782 if (oob_required) {
1783 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1784 false);
1785 if (ret)
1786 return ret;
1787 }
1788
William Juul52c07962007-10-31 13:53:06 +01001789 return 0;
1790}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001791
William Juul52c07962007-10-31 13:53:06 +01001792/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001793 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1794 * @mtd: mtd info structure
1795 * @chip: nand chip info structure
1796 * @buf: buffer to store read data
1797 * @oob_required: caller requires OOB data read to chip->oob_poi
1798 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001799 *
1800 * We need a special oob layout and handling even when OOB isn't used.
1801 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001802static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001803 struct nand_chip *chip, uint8_t *buf,
1804 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001805{
1806 int eccsize = chip->ecc.size;
1807 int eccbytes = chip->ecc.bytes;
1808 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001809 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001810
1811 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001812 ret = nand_read_data_op(chip, buf, eccsize, false);
1813 if (ret)
1814 return ret;
1815
David Brownellee86b8d2009-11-07 16:27:01 -05001816 buf += eccsize;
1817
1818 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001819 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1820 false);
1821 if (ret)
1822 return ret;
1823
David Brownellee86b8d2009-11-07 16:27:01 -05001824 oob += chip->ecc.prepad;
1825 }
1826
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001827 ret = nand_read_data_op(chip, oob, eccbytes, false);
1828 if (ret)
1829 return ret;
1830
David Brownellee86b8d2009-11-07 16:27:01 -05001831 oob += eccbytes;
1832
1833 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001834 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1835 false);
1836 if (ret)
1837 return ret;
1838
David Brownellee86b8d2009-11-07 16:27:01 -05001839 oob += chip->ecc.postpad;
1840 }
1841 }
1842
1843 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001844 if (size) {
1845 ret = nand_read_data_op(chip, oob, size, false);
1846 if (ret)
1847 return ret;
1848 }
David Brownellee86b8d2009-11-07 16:27:01 -05001849
1850 return 0;
1851}
1852
1853/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001854 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1855 * @mtd: mtd info structure
1856 * @chip: nand chip info structure
1857 * @buf: buffer to store read data
1858 * @oob_required: caller requires OOB data read to chip->oob_poi
1859 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001860 */
1861static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001862 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001863{
1864 int i, eccsize = chip->ecc.size;
1865 int eccbytes = chip->ecc.bytes;
1866 int eccsteps = chip->ecc.steps;
1867 uint8_t *p = buf;
1868 uint8_t *ecc_calc = chip->buffers->ecccalc;
1869 uint8_t *ecc_code = chip->buffers->ecccode;
1870 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001871 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001872
Sergey Lapin3a38a552013-01-14 03:46:50 +00001873 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001874
William Juul52c07962007-10-31 13:53:06 +01001875 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1876 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001877
William Juul52c07962007-10-31 13:53:06 +01001878 for (i = 0; i < chip->ecc.total; i++)
1879 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001880
William Juul52c07962007-10-31 13:53:06 +01001881 eccsteps = chip->ecc.steps;
1882 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001883
William Juul52c07962007-10-31 13:53:06 +01001884 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1885 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001886
William Juul52c07962007-10-31 13:53:06 +01001887 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001888 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001889 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001890 } else {
William Juul52c07962007-10-31 13:53:06 +01001891 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001892 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1893 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001894 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001895 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001896}
1897
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001898/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001899 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001900 * @mtd: mtd info structure
1901 * @chip: nand chip info structure
1902 * @data_offs: offset of requested data within the page
1903 * @readlen: data length
1904 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001905 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001906 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001907static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001908 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1909 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001910{
1911 int start_step, end_step, num_steps;
1912 uint32_t *eccpos = chip->ecc.layout->eccpos;
1913 uint8_t *p;
1914 int data_col_addr, i, gaps = 0;
1915 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1916 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001917 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001918 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001919 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001920
Sergey Lapin3a38a552013-01-14 03:46:50 +00001921 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001922 start_step = data_offs / chip->ecc.size;
1923 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1924 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001925 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001926
Sergey Lapin3a38a552013-01-14 03:46:50 +00001927 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001928 datafrag_len = num_steps * chip->ecc.size;
1929 eccfrag_len = num_steps * chip->ecc.bytes;
1930
1931 data_col_addr = start_step * chip->ecc.size;
1932 /* If we read not a page aligned data */
1933 if (data_col_addr != 0)
1934 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1935
1936 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001937 ret = nand_read_data_op(chip, p, datafrag_len, false);
1938 if (ret)
1939 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001940
Sergey Lapin3a38a552013-01-14 03:46:50 +00001941 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001942 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1943 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1944
Sergey Lapin3a38a552013-01-14 03:46:50 +00001945 /*
1946 * The performance is faster if we position offsets according to
1947 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1948 */
Scott Wood3628f002008-10-24 16:20:43 -05001949 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001950 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001951 gaps = 1;
1952 break;
1953 }
1954 }
1955 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001956 ret = nand_change_read_column_op(chip, mtd->writesize,
1957 chip->oob_poi, mtd->oobsize,
1958 false);
1959 if (ret)
1960 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001961 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001962 /*
1963 * Send the command to read the particular ECC bytes take care
1964 * about buswidth alignment in read_buf.
1965 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001966 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001967 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001968 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001969 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001970 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001971 aligned_len++;
1972
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001973 ret = nand_change_read_column_op(chip,
1974 mtd->writesize + aligned_pos,
1975 &chip->oob_poi[aligned_pos],
1976 aligned_len, false);
1977 if (ret)
1978 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001979 }
1980
1981 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001982 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001983
1984 p = bufpoi + data_col_addr;
1985 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1986 int stat;
1987
Christian Hitzb8a6b372011-10-12 09:32:02 +02001988 stat = chip->ecc.correct(mtd, p,
1989 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001990 if (stat == -EBADMSG &&
1991 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1992 /* check for empty pages with bitflips */
1993 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1994 &chip->buffers->ecccode[i],
1995 chip->ecc.bytes,
1996 NULL, 0,
1997 chip->ecc.strength);
1998 }
1999
Heiko Schocherf5895d12014-06-24 10:10:04 +02002000 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05002001 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002002 } else {
Scott Wood3628f002008-10-24 16:20:43 -05002003 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002004 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2005 }
Scott Wood3628f002008-10-24 16:20:43 -05002006 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002007 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05002008}
2009
2010/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002011 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
2012 * @mtd: mtd info structure
2013 * @chip: nand chip info structure
2014 * @buf: buffer to store read data
2015 * @oob_required: caller requires OOB data read to chip->oob_poi
2016 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002017 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002018 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002019 */
William Juul52c07962007-10-31 13:53:06 +01002020static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002021 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002022{
William Juul52c07962007-10-31 13:53:06 +01002023 int i, eccsize = chip->ecc.size;
2024 int eccbytes = chip->ecc.bytes;
2025 int eccsteps = chip->ecc.steps;
2026 uint8_t *p = buf;
2027 uint8_t *ecc_calc = chip->buffers->ecccalc;
2028 uint8_t *ecc_code = chip->buffers->ecccode;
2029 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002030 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002031 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002032
William Juul52c07962007-10-31 13:53:06 +01002033 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2034 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002035
2036 ret = nand_read_data_op(chip, p, eccsize, false);
2037 if (ret)
2038 return ret;
2039
William Juul52c07962007-10-31 13:53:06 +01002040 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2041 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002042
2043 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2044 if (ret)
2045 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002046
William Juul52c07962007-10-31 13:53:06 +01002047 for (i = 0; i < chip->ecc.total; i++)
2048 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002049
William Juul52c07962007-10-31 13:53:06 +01002050 eccsteps = chip->ecc.steps;
2051 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002052
William Juul52c07962007-10-31 13:53:06 +01002053 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2054 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002055
William Juul52c07962007-10-31 13:53:06 +01002056 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002057 if (stat == -EBADMSG &&
2058 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2059 /* check for empty pages with bitflips */
2060 stat = nand_check_erased_ecc_chunk(p, eccsize,
2061 &ecc_code[i], eccbytes,
2062 NULL, 0,
2063 chip->ecc.strength);
2064 }
2065
Heiko Schocherf5895d12014-06-24 10:10:04 +02002066 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002067 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002068 } else {
William Juul52c07962007-10-31 13:53:06 +01002069 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002070 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2071 }
William Juul52c07962007-10-31 13:53:06 +01002072 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002073 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002074}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002075
William Juul52c07962007-10-31 13:53:06 +01002076/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002077 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2078 * @mtd: mtd info structure
2079 * @chip: nand chip info structure
2080 * @buf: buffer to store read data
2081 * @oob_required: caller requires OOB data read to chip->oob_poi
2082 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002083 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002084 * Hardware ECC for large page chips, require OOB to be read first. For this
2085 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2086 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2087 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2088 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002089 */
2090static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002091 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002092{
2093 int i, eccsize = chip->ecc.size;
2094 int eccbytes = chip->ecc.bytes;
2095 int eccsteps = chip->ecc.steps;
2096 uint8_t *p = buf;
2097 uint8_t *ecc_code = chip->buffers->ecccode;
2098 uint32_t *eccpos = chip->ecc.layout->eccpos;
2099 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002100 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002101 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002102
2103 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002104 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2105 if (ret)
2106 return ret;
2107
2108 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2109 if (ret)
2110 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002111
2112 for (i = 0; i < chip->ecc.total; i++)
2113 ecc_code[i] = chip->oob_poi[eccpos[i]];
2114
2115 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2116 int stat;
2117
2118 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002119
2120 ret = nand_read_data_op(chip, p, eccsize, false);
2121 if (ret)
2122 return ret;
2123
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002124 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2125
2126 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002127 if (stat == -EBADMSG &&
2128 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2129 /* check for empty pages with bitflips */
2130 stat = nand_check_erased_ecc_chunk(p, eccsize,
2131 &ecc_code[i], eccbytes,
2132 NULL, 0,
2133 chip->ecc.strength);
2134 }
2135
Heiko Schocherf5895d12014-06-24 10:10:04 +02002136 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002137 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002138 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002139 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002140 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2141 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002142 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002143 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002144}
2145
2146/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002147 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2148 * @mtd: mtd info structure
2149 * @chip: nand chip info structure
2150 * @buf: buffer to store read data
2151 * @oob_required: caller requires OOB data read to chip->oob_poi
2152 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002153 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002154 * The hw generator calculates the error syndrome automatically. Therefore we
2155 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002156 */
2157static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002158 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002159{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002160 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002161 int eccbytes = chip->ecc.bytes;
2162 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002163 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002164 uint8_t *p = buf;
2165 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002166 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002167
2168 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2169 int stat;
2170
2171 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002172
2173 ret = nand_read_data_op(chip, p, eccsize, false);
2174 if (ret)
2175 return ret;
William Juul52c07962007-10-31 13:53:06 +01002176
2177 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002178 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2179 false);
2180 if (ret)
2181 return ret;
2182
William Juul52c07962007-10-31 13:53:06 +01002183 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002184 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002185
William Juul52c07962007-10-31 13:53:06 +01002186 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002187
2188 ret = nand_read_data_op(chip, oob, eccbytes, false);
2189 if (ret)
2190 return ret;
2191
William Juul52c07962007-10-31 13:53:06 +01002192 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002193
William Juul52c07962007-10-31 13:53:06 +01002194 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002195
William Juul52c07962007-10-31 13:53:06 +01002196 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002197 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2198 false);
2199 if (ret)
2200 return ret;
2201
William Juul52c07962007-10-31 13:53:06 +01002202 oob += chip->ecc.postpad;
2203 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002204
2205 if (stat == -EBADMSG &&
2206 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2207 /* check for empty pages with bitflips */
2208 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2209 oob - eccpadbytes,
2210 eccpadbytes,
2211 NULL, 0,
2212 chip->ecc.strength);
2213 }
2214
2215 if (stat < 0) {
2216 mtd->ecc_stats.failed++;
2217 } else {
2218 mtd->ecc_stats.corrected += stat;
2219 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2220 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002221 }
William Juul52c07962007-10-31 13:53:06 +01002222
2223 /* Calculate remaining oob bytes */
2224 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002225 if (i) {
2226 ret = nand_read_data_op(chip, oob, i, false);
2227 if (ret)
2228 return ret;
2229 }
William Juul52c07962007-10-31 13:53:06 +01002230
Heiko Schocherf5895d12014-06-24 10:10:04 +02002231 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002232}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002233
2234/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002235 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2236 * @chip: nand chip structure
2237 * @oob: oob destination address
2238 * @ops: oob ops structure
2239 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002240 */
2241static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2242 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002243{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002244 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002245
Sergey Lapin3a38a552013-01-14 03:46:50 +00002246 case MTD_OPS_PLACE_OOB:
2247 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002248 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2249 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002250
Sergey Lapin3a38a552013-01-14 03:46:50 +00002251 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002252 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2253 uint32_t boffs = 0, roffs = ops->ooboffs;
2254 size_t bytes = 0;
2255
Christian Hitz13fc0e22011-10-12 09:32:01 +02002256 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002257 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002258 if (unlikely(roffs)) {
2259 if (roffs >= free->length) {
2260 roffs -= free->length;
2261 continue;
2262 }
2263 boffs = free->offset + roffs;
2264 bytes = min_t(size_t, len,
2265 (free->length - roffs));
2266 roffs = 0;
2267 } else {
2268 bytes = min_t(size_t, len, free->length);
2269 boffs = free->offset;
2270 }
2271 memcpy(oob, chip->oob_poi + boffs, bytes);
2272 oob += bytes;
2273 }
2274 return oob;
2275 }
2276 default:
2277 BUG();
2278 }
2279 return NULL;
2280}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002281
2282/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002283 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2284 * @mtd: MTD device structure
2285 * @retry_mode: the retry mode to use
2286 *
2287 * Some vendors supply a special command to shift the Vt threshold, to be used
2288 * when there are too many bitflips in a page (i.e., ECC error). After setting
2289 * a new threshold, the host should retry reading the page.
2290 */
2291static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2292{
Scott Wood17fed142016-05-30 13:57:56 -05002293 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002294
2295 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2296
2297 if (retry_mode >= chip->read_retries)
2298 return -EINVAL;
2299
2300 if (!chip->setup_read_retry)
2301 return -EOPNOTSUPP;
2302
2303 return chip->setup_read_retry(mtd, retry_mode);
2304}
2305
2306/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002307 * nand_do_read_ops - [INTERN] Read data with ECC
2308 * @mtd: MTD device structure
2309 * @from: offset to read from
2310 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002311 *
William Juul52c07962007-10-31 13:53:06 +01002312 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002313 */
William Juul52c07962007-10-31 13:53:06 +01002314static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2315 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002316{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002317 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002318 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002319 int ret = 0;
2320 uint32_t readlen = ops->len;
2321 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002322 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002323
William Juul52c07962007-10-31 13:53:06 +01002324 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002325 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002326 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002327 int retry_mode = 0;
2328 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002329
William Juul52c07962007-10-31 13:53:06 +01002330 chipnr = (int)(from >> chip->chip_shift);
2331 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002332
William Juul52c07962007-10-31 13:53:06 +01002333 realpage = (int)(from >> chip->page_shift);
2334 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002335
William Juul52c07962007-10-31 13:53:06 +01002336 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002337
William Juul52c07962007-10-31 13:53:06 +01002338 buf = ops->datbuf;
2339 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002340 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002341
Christian Hitz13fc0e22011-10-12 09:32:01 +02002342 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002343 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002344
Stefan Roese80877fa2022-09-02 14:10:46 +02002345 schedule();
William Juul52c07962007-10-31 13:53:06 +01002346 bytes = min(mtd->writesize - col, readlen);
2347 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002348
Scott Wood3ea94ed2015-06-26 19:03:26 -05002349 if (!aligned)
2350 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002351 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2352 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2353 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002354 else
2355 use_bufpoi = 0;
2356
Sergey Lapin3a38a552013-01-14 03:46:50 +00002357 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002358 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002359 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2360
2361 if (use_bufpoi && aligned)
2362 pr_debug("%s: using read bounce buffer for buf@%p\n",
2363 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002364
Heiko Schocherf5895d12014-06-24 10:10:04 +02002365read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002366 if (nand_standard_page_accessors(&chip->ecc)) {
2367 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2368 if (ret)
2369 break;
2370 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002371
Paul Burton700a76c2013-09-04 15:16:56 +01002372 /*
2373 * Now read the page into the buffer. Absent an error,
2374 * the read methods return max bitflips per ecc step.
2375 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002376 if (unlikely(ops->mode == MTD_OPS_RAW))
2377 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2378 oob_required,
2379 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002380 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002381 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002382 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002383 col, bytes, bufpoi,
2384 page);
William Juul52c07962007-10-31 13:53:06 +01002385 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002386 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002387 oob_required, page);
2388 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002389 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002390 /* Invalidate page cache */
2391 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002392 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002393 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002394
Paul Burton700a76c2013-09-04 15:16:56 +01002395 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2396
William Juul52c07962007-10-31 13:53:06 +01002397 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002398 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002399 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002400 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002401 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002402 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002403 chip->pagebuf_bitflips = ret;
2404 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002405 /* Invalidate page cache */
2406 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002407 }
William Juul52c07962007-10-31 13:53:06 +01002408 memcpy(buf, chip->buffers->databuf + col, bytes);
2409 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002410
William Juul52c07962007-10-31 13:53:06 +01002411 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002412 int toread = min(oobreadlen, max_oobsize);
2413
2414 if (toread) {
2415 oob = nand_transfer_oob(chip,
2416 oob, ops, toread);
2417 oobreadlen -= toread;
2418 }
William Juul52c07962007-10-31 13:53:06 +01002419 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002420
2421 if (chip->options & NAND_NEED_READRDY) {
2422 /* Apply delay or wait for ready/busy pin */
2423 if (!chip->dev_ready)
2424 udelay(chip->chip_delay);
2425 else
2426 nand_wait_ready(mtd);
2427 }
2428
2429 if (mtd->ecc_stats.failed - ecc_failures) {
2430 if (retry_mode + 1 < chip->read_retries) {
2431 retry_mode++;
2432 ret = nand_setup_read_retry(mtd,
2433 retry_mode);
2434 if (ret < 0)
2435 break;
2436
2437 /* Reset failures; retry */
2438 mtd->ecc_stats.failed = ecc_failures;
2439 goto read_retry;
2440 } else {
2441 /* No more retry modes; real failure */
2442 ecc_fail = true;
2443 }
2444 }
2445
2446 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002447 } else {
2448 memcpy(buf, chip->buffers->databuf + col, bytes);
2449 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002450 max_bitflips = max_t(unsigned int, max_bitflips,
2451 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002452 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002453
William Juul52c07962007-10-31 13:53:06 +01002454 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002455
Heiko Schocherf5895d12014-06-24 10:10:04 +02002456 /* Reset to retry mode 0 */
2457 if (retry_mode) {
2458 ret = nand_setup_read_retry(mtd, 0);
2459 if (ret < 0)
2460 break;
2461 retry_mode = 0;
2462 }
2463
William Juul52c07962007-10-31 13:53:06 +01002464 if (!readlen)
2465 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002466
Sergey Lapin3a38a552013-01-14 03:46:50 +00002467 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002468 col = 0;
2469 /* Increment page address */
2470 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002471
William Juul52c07962007-10-31 13:53:06 +01002472 page = realpage & chip->pagemask;
2473 /* Check, if we cross a chip boundary */
2474 if (!page) {
2475 chipnr++;
2476 chip->select_chip(mtd, -1);
2477 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002478 }
William Juul52c07962007-10-31 13:53:06 +01002479 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002480 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002481
William Juul52c07962007-10-31 13:53:06 +01002482 ops->retlen = ops->len - (size_t) readlen;
2483 if (oob)
2484 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002485
Heiko Schocherf5895d12014-06-24 10:10:04 +02002486 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002487 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002488
Heiko Schocherf5895d12014-06-24 10:10:04 +02002489 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002490 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002491
Paul Burton700a76c2013-09-04 15:16:56 +01002492 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002493}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002494
William Juul52c07962007-10-31 13:53:06 +01002495/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002496 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2497 * @mtd: mtd info structure
2498 * @chip: nand chip info structure
2499 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002500 */
2501static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002502 int page)
William Juul52c07962007-10-31 13:53:06 +01002503{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002504 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002505}
2506
2507/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002508 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002509 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002510 * @mtd: mtd info structure
2511 * @chip: nand chip info structure
2512 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002513 */
2514static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002515 int page)
William Juul52c07962007-10-31 13:53:06 +01002516{
William Juul52c07962007-10-31 13:53:06 +01002517 int length = mtd->oobsize;
2518 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2519 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002520 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002521 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002522
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002523 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2524 if (ret)
2525 return ret;
2526
William Juul52c07962007-10-31 13:53:06 +01002527 for (i = 0; i < chip->ecc.steps; i++) {
2528 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002529 int ret;
2530
William Juul52c07962007-10-31 13:53:06 +01002531 pos = eccsize + i * (eccsize + chunk);
2532 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002533 ret = nand_change_read_column_op(chip, pos,
2534 NULL, 0,
2535 false);
William Juul52c07962007-10-31 13:53:06 +01002536 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002537 ret = nand_read_page_op(chip, page, pos, NULL,
2538 0);
2539
2540 if (ret)
2541 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002542 } else
William Juul52c07962007-10-31 13:53:06 +01002543 sndrnd = 1;
2544 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002545
2546 ret = nand_read_data_op(chip, bufpoi, toread, false);
2547 if (ret)
2548 return ret;
2549
William Juul52c07962007-10-31 13:53:06 +01002550 bufpoi += toread;
2551 length -= toread;
2552 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002553 if (length > 0) {
2554 ret = nand_read_data_op(chip, bufpoi, length, false);
2555 if (ret)
2556 return ret;
2557 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002558
Sergey Lapin3a38a552013-01-14 03:46:50 +00002559 return 0;
William Juul52c07962007-10-31 13:53:06 +01002560}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002561
William Juul52c07962007-10-31 13:53:06 +01002562/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002563 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2564 * @mtd: mtd info structure
2565 * @chip: nand chip info structure
2566 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002567 */
2568static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2569 int page)
2570{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002571 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2572 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002573}
2574
2575/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002576 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2577 * with syndrome - only for large page flash
2578 * @mtd: mtd info structure
2579 * @chip: nand chip info structure
2580 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002581 */
2582static int nand_write_oob_syndrome(struct mtd_info *mtd,
2583 struct nand_chip *chip, int page)
2584{
2585 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2586 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002587 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002588 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002589
2590 /*
William Juul52c07962007-10-31 13:53:06 +01002591 * data-ecc-data-ecc ... ecc-oob
2592 * or
2593 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002594 */
William Juul52c07962007-10-31 13:53:06 +01002595 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2596 pos = steps * (eccsize + chunk);
2597 steps = 0;
2598 } else
2599 pos = eccsize;
2600
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002601 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2602 if (ret)
2603 return ret;
2604
William Juul52c07962007-10-31 13:53:06 +01002605 for (i = 0; i < steps; i++) {
2606 if (sndcmd) {
2607 if (mtd->writesize <= 512) {
2608 uint32_t fill = 0xFFFFFFFF;
2609
2610 len = eccsize;
2611 while (len > 0) {
2612 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002613
2614 ret = nand_write_data_op(chip, &fill,
2615 num, false);
2616 if (ret)
2617 return ret;
2618
William Juul52c07962007-10-31 13:53:06 +01002619 len -= num;
2620 }
2621 } else {
2622 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002623 ret = nand_change_write_column_op(chip, pos,
2624 NULL, 0,
2625 false);
2626 if (ret)
2627 return ret;
William Juul52c07962007-10-31 13:53:06 +01002628 }
2629 } else
2630 sndcmd = 1;
2631 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002632
2633 ret = nand_write_data_op(chip, bufpoi, len, false);
2634 if (ret)
2635 return ret;
2636
William Juul52c07962007-10-31 13:53:06 +01002637 bufpoi += len;
2638 length -= len;
2639 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002640 if (length > 0) {
2641 ret = nand_write_data_op(chip, bufpoi, length, false);
2642 if (ret)
2643 return ret;
2644 }
William Juul52c07962007-10-31 13:53:06 +01002645
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002646 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002647}
2648
2649/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002650 * nand_do_read_oob - [INTERN] NAND read out-of-band
2651 * @mtd: MTD device structure
2652 * @from: offset to read from
2653 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002654 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002655 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002656 */
William Juul52c07962007-10-31 13:53:06 +01002657static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2658 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002659{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002660 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002661 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002662 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002663 int readlen = ops->ooblen;
2664 int len;
2665 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002666 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002667
Heiko Schocherf5895d12014-06-24 10:10:04 +02002668 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002669 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002670
Sergey Lapin3a38a552013-01-14 03:46:50 +00002671 stats = mtd->ecc_stats;
2672
Scott Wood52ab7ce2016-05-30 13:57:58 -05002673 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002674
William Juul52c07962007-10-31 13:53:06 +01002675 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002676 pr_debug("%s: attempt to start read outside oob\n",
2677 __func__);
William Juul52c07962007-10-31 13:53:06 +01002678 return -EINVAL;
2679 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002680
2681 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002682 if (unlikely(from >= mtd->size ||
2683 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2684 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002685 pr_debug("%s: attempt to read beyond end of device\n",
2686 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002687 return -EINVAL;
2688 }
2689
William Juul52c07962007-10-31 13:53:06 +01002690 chipnr = (int)(from >> chip->chip_shift);
2691 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002692
William Juul52c07962007-10-31 13:53:06 +01002693 /* Shift to get page */
2694 realpage = (int)(from >> chip->page_shift);
2695 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002696
Christian Hitz13fc0e22011-10-12 09:32:01 +02002697 while (1) {
Stefan Roese80877fa2022-09-02 14:10:46 +02002698 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002699
Sergey Lapin3a38a552013-01-14 03:46:50 +00002700 if (ops->mode == MTD_OPS_RAW)
2701 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2702 else
2703 ret = chip->ecc.read_oob(mtd, chip, page);
2704
2705 if (ret < 0)
2706 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002707
William Juul52c07962007-10-31 13:53:06 +01002708 len = min(len, readlen);
2709 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002710
Heiko Schocherf5895d12014-06-24 10:10:04 +02002711 if (chip->options & NAND_NEED_READRDY) {
2712 /* Apply delay or wait for ready/busy pin */
2713 if (!chip->dev_ready)
2714 udelay(chip->chip_delay);
2715 else
2716 nand_wait_ready(mtd);
2717 }
2718
William Juul52c07962007-10-31 13:53:06 +01002719 readlen -= len;
2720 if (!readlen)
2721 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002722
William Juul52c07962007-10-31 13:53:06 +01002723 /* Increment page address */
2724 realpage++;
2725
2726 page = realpage & chip->pagemask;
2727 /* Check, if we cross a chip boundary */
2728 if (!page) {
2729 chipnr++;
2730 chip->select_chip(mtd, -1);
2731 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002732 }
William Juul52c07962007-10-31 13:53:06 +01002733 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002734 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002735
Sergey Lapin3a38a552013-01-14 03:46:50 +00002736 ops->oobretlen = ops->ooblen - readlen;
2737
2738 if (ret < 0)
2739 return ret;
2740
2741 if (mtd->ecc_stats.failed - stats.failed)
2742 return -EBADMSG;
2743
2744 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002745}
2746
2747/**
William Juul52c07962007-10-31 13:53:06 +01002748 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002749 * @mtd: MTD device structure
2750 * @from: offset to read from
2751 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002752 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002753 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002754 */
William Juul52c07962007-10-31 13:53:06 +01002755static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2756 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002757{
William Juul52c07962007-10-31 13:53:06 +01002758 int ret = -ENOTSUPP;
2759
2760 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002761
2762 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002763 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002764 pr_debug("%s: attempt to read beyond end of device\n",
2765 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002766 return -EINVAL;
2767 }
2768
Heiko Schocherf5895d12014-06-24 10:10:04 +02002769 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002770
Christian Hitz13fc0e22011-10-12 09:32:01 +02002771 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002772 case MTD_OPS_PLACE_OOB:
2773 case MTD_OPS_AUTO_OOB:
2774 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002775 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002776
William Juul52c07962007-10-31 13:53:06 +01002777 default:
2778 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779 }
2780
William Juul52c07962007-10-31 13:53:06 +01002781 if (!ops->datbuf)
2782 ret = nand_do_read_oob(mtd, from, ops);
2783 else
2784 ret = nand_do_read_ops(mtd, from, ops);
2785
Christian Hitz13fc0e22011-10-12 09:32:01 +02002786out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002787 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002788 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002789}
2790
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002791/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002792 * nand_write_page_raw - [INTERN] raw page write function
2793 * @mtd: mtd info structure
2794 * @chip: nand chip info structure
2795 * @buf: data buffer
2796 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002797 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002798 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002799 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002800 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002801static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002802 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002803{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002804 int ret;
2805
2806 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2807 if (ret)
2808 return ret;
2809
2810 if (oob_required) {
2811 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2812 false);
2813 if (ret)
2814 return ret;
2815 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002816
2817 return 0;
William Juul52c07962007-10-31 13:53:06 +01002818}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002819
William Juul52c07962007-10-31 13:53:06 +01002820/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002821 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2822 * @mtd: mtd info structure
2823 * @chip: nand chip info structure
2824 * @buf: data buffer
2825 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002826 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002827 *
2828 * We need a special oob layout and handling even when ECC isn't checked.
2829 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002830static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002831 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002832 const uint8_t *buf, int oob_required,
2833 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002834{
2835 int eccsize = chip->ecc.size;
2836 int eccbytes = chip->ecc.bytes;
2837 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002838 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002839
2840 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002841 ret = nand_write_data_op(chip, buf, eccsize, false);
2842 if (ret)
2843 return ret;
2844
David Brownellee86b8d2009-11-07 16:27:01 -05002845 buf += eccsize;
2846
2847 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002848 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2849 false);
2850 if (ret)
2851 return ret;
2852
David Brownellee86b8d2009-11-07 16:27:01 -05002853 oob += chip->ecc.prepad;
2854 }
2855
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002856 ret = nand_write_data_op(chip, oob, eccbytes, false);
2857 if (ret)
2858 return ret;
2859
David Brownellee86b8d2009-11-07 16:27:01 -05002860 oob += eccbytes;
2861
2862 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002863 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2864 false);
2865 if (ret)
2866 return ret;
2867
David Brownellee86b8d2009-11-07 16:27:01 -05002868 oob += chip->ecc.postpad;
2869 }
2870 }
2871
2872 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002873 if (size) {
2874 ret = nand_write_data_op(chip, oob, size, false);
2875 if (ret)
2876 return ret;
2877 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002878
2879 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002880}
2881/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002882 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2883 * @mtd: mtd info structure
2884 * @chip: nand chip info structure
2885 * @buf: data buffer
2886 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002887 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002888 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002889static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002890 const uint8_t *buf, int oob_required,
2891 int page)
William Juul52c07962007-10-31 13:53:06 +01002892{
2893 int i, eccsize = chip->ecc.size;
2894 int eccbytes = chip->ecc.bytes;
2895 int eccsteps = chip->ecc.steps;
2896 uint8_t *ecc_calc = chip->buffers->ecccalc;
2897 const uint8_t *p = buf;
2898 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002899
Sergey Lapin3a38a552013-01-14 03:46:50 +00002900 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002901 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2902 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002903
William Juul52c07962007-10-31 13:53:06 +01002904 for (i = 0; i < chip->ecc.total; i++)
2905 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002906
Scott Wood46e13102016-05-30 13:57:57 -05002907 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002908}
2909
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002910/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002911 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2912 * @mtd: mtd info structure
2913 * @chip: nand chip info structure
2914 * @buf: data buffer
2915 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002916 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002917 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002918static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002919 const uint8_t *buf, int oob_required,
2920 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002921{
William Juul52c07962007-10-31 13:53:06 +01002922 int i, eccsize = chip->ecc.size;
2923 int eccbytes = chip->ecc.bytes;
2924 int eccsteps = chip->ecc.steps;
2925 uint8_t *ecc_calc = chip->buffers->ecccalc;
2926 const uint8_t *p = buf;
2927 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002928 int ret;
William Juul52c07962007-10-31 13:53:06 +01002929
2930 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2931 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002932
2933 ret = nand_write_data_op(chip, p, eccsize, false);
2934 if (ret)
2935 return ret;
2936
William Juul52c07962007-10-31 13:53:06 +01002937 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2938 }
2939
2940 for (i = 0; i < chip->ecc.total; i++)
2941 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2942
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002943 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2944 if (ret)
2945 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002946
2947 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002948}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002949
Heiko Schocherf5895d12014-06-24 10:10:04 +02002950/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002951 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002952 * @mtd: mtd info structure
2953 * @chip: nand chip info structure
2954 * @offset: column address of subpage within the page
2955 * @data_len: data length
2956 * @buf: data buffer
2957 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002958 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002959 */
2960static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2961 struct nand_chip *chip, uint32_t offset,
2962 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002963 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002964{
2965 uint8_t *oob_buf = chip->oob_poi;
2966 uint8_t *ecc_calc = chip->buffers->ecccalc;
2967 int ecc_size = chip->ecc.size;
2968 int ecc_bytes = chip->ecc.bytes;
2969 int ecc_steps = chip->ecc.steps;
2970 uint32_t *eccpos = chip->ecc.layout->eccpos;
2971 uint32_t start_step = offset / ecc_size;
2972 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2973 int oob_bytes = mtd->oobsize / ecc_steps;
2974 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002975 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002976
2977 for (step = 0; step < ecc_steps; step++) {
2978 /* configure controller for WRITE access */
2979 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2980
2981 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002982 ret = nand_write_data_op(chip, buf, ecc_size, false);
2983 if (ret)
2984 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002985
2986 /* mask ECC of un-touched subpages by padding 0xFF */
2987 if ((step < start_step) || (step > end_step))
2988 memset(ecc_calc, 0xff, ecc_bytes);
2989 else
2990 chip->ecc.calculate(mtd, buf, ecc_calc);
2991
2992 /* mask OOB of un-touched subpages by padding 0xFF */
2993 /* if oob_required, preserve OOB metadata of written subpage */
2994 if (!oob_required || (step < start_step) || (step > end_step))
2995 memset(oob_buf, 0xff, oob_bytes);
2996
2997 buf += ecc_size;
2998 ecc_calc += ecc_bytes;
2999 oob_buf += oob_bytes;
3000 }
3001
3002 /* copy calculated ECC for whole page to chip->buffer->oob */
3003 /* this include masked-value(0xFF) for unwritten subpages */
3004 ecc_calc = chip->buffers->ecccalc;
3005 for (i = 0; i < chip->ecc.total; i++)
3006 chip->oob_poi[eccpos[i]] = ecc_calc[i];
3007
3008 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003009 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3010 if (ret)
3011 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003012
3013 return 0;
3014}
3015
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003016/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003017 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
3018 * @mtd: mtd info structure
3019 * @chip: nand chip info structure
3020 * @buf: data buffer
3021 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003022 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003023 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003024 * The hw generator calculates the error syndrome automatically. Therefore we
3025 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003026 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003027static int nand_write_page_syndrome(struct mtd_info *mtd,
3028 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003029 const uint8_t *buf, int oob_required,
3030 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003031{
William Juul52c07962007-10-31 13:53:06 +01003032 int i, eccsize = chip->ecc.size;
3033 int eccbytes = chip->ecc.bytes;
3034 int eccsteps = chip->ecc.steps;
3035 const uint8_t *p = buf;
3036 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003037 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003038
William Juul52c07962007-10-31 13:53:06 +01003039 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003040 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003041
3042 ret = nand_write_data_op(chip, p, eccsize, false);
3043 if (ret)
3044 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003045
William Juul52c07962007-10-31 13:53:06 +01003046 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003047 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3048 false);
3049 if (ret)
3050 return ret;
3051
William Juul52c07962007-10-31 13:53:06 +01003052 oob += chip->ecc.prepad;
3053 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003054
William Juul52c07962007-10-31 13:53:06 +01003055 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003056
3057 ret = nand_write_data_op(chip, oob, eccbytes, false);
3058 if (ret)
3059 return ret;
3060
William Juul52c07962007-10-31 13:53:06 +01003061 oob += eccbytes;
3062
3063 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003064 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3065 false);
3066 if (ret)
3067 return ret;
3068
William Juul52c07962007-10-31 13:53:06 +01003069 oob += chip->ecc.postpad;
3070 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003071 }
3072
William Juul52c07962007-10-31 13:53:06 +01003073 /* Calculate remaining oob bytes */
3074 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003075 if (i) {
3076 ret = nand_write_data_op(chip, oob, i, false);
3077 if (ret)
3078 return ret;
3079 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003080
3081 return 0;
William Juul52c07962007-10-31 13:53:06 +01003082}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003083
William Juul52c07962007-10-31 13:53:06 +01003084/**
3085 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003086 * @mtd: MTD device structure
3087 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003088 * @offset: address offset within the page
3089 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003090 * @buf: the data to write
3091 * @oob_required: must write chip->oob_poi to OOB
3092 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003093 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003094 */
3095static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003096 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003097 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003098{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003099 int status, subpage;
3100
3101 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3102 chip->ecc.write_subpage)
3103 subpage = offset || (data_len < mtd->writesize);
3104 else
3105 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003106
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003107 if (nand_standard_page_accessors(&chip->ecc)) {
3108 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3109 if (status)
3110 return status;
3111 }
William Juul52c07962007-10-31 13:53:06 +01003112
3113 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003114 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003115 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003116 else if (subpage)
3117 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003118 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003119 else
Scott Wood46e13102016-05-30 13:57:57 -05003120 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3121 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003122
3123 if (status < 0)
3124 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003125
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003126 if (nand_standard_page_accessors(&chip->ecc))
3127 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003128
William Juul52c07962007-10-31 13:53:06 +01003129 return 0;
3130}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003131
William Juul52c07962007-10-31 13:53:06 +01003132/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003133 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3134 * @mtd: MTD device structure
3135 * @oob: oob data buffer
3136 * @len: oob data write length
3137 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003138 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003139static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3140 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003141{
Scott Wood17fed142016-05-30 13:57:56 -05003142 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003143
3144 /*
3145 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3146 * data from a previous OOB read.
3147 */
3148 memset(chip->oob_poi, 0xff, mtd->oobsize);
3149
Christian Hitz13fc0e22011-10-12 09:32:01 +02003150 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003151
Sergey Lapin3a38a552013-01-14 03:46:50 +00003152 case MTD_OPS_PLACE_OOB:
3153 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003154 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3155 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003156
Sergey Lapin3a38a552013-01-14 03:46:50 +00003157 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003158 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3159 uint32_t boffs = 0, woffs = ops->ooboffs;
3160 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003161
Christian Hitz13fc0e22011-10-12 09:32:01 +02003162 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003163 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003164 if (unlikely(woffs)) {
3165 if (woffs >= free->length) {
3166 woffs -= free->length;
3167 continue;
3168 }
3169 boffs = free->offset + woffs;
3170 bytes = min_t(size_t, len,
3171 (free->length - woffs));
3172 woffs = 0;
3173 } else {
3174 bytes = min_t(size_t, len, free->length);
3175 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003176 }
William Juul52c07962007-10-31 13:53:06 +01003177 memcpy(chip->oob_poi + boffs, oob, bytes);
3178 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003179 }
William Juul52c07962007-10-31 13:53:06 +01003180 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003181 }
William Juul52c07962007-10-31 13:53:06 +01003182 default:
3183 BUG();
3184 }
3185 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003186}
3187
Christian Hitzb8a6b372011-10-12 09:32:02 +02003188#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003189
3190/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003191 * nand_do_write_ops - [INTERN] NAND write with ECC
3192 * @mtd: MTD device structure
3193 * @to: offset to write to
3194 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003195 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003196 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003197 */
William Juul52c07962007-10-31 13:53:06 +01003198static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3199 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003200{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003201 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003202 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003203 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003204
3205 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003206 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003207
William Juul52c07962007-10-31 13:53:06 +01003208 uint8_t *oob = ops->oobbuf;
3209 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003210 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003211 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003212
William Juul52c07962007-10-31 13:53:06 +01003213 ops->retlen = 0;
3214 if (!writelen)
3215 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003216
Heiko Schocherf5895d12014-06-24 10:10:04 +02003217 /* Reject writes, which are not page aligned */
3218 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003219 pr_notice("%s: attempt to write non page aligned data\n",
3220 __func__);
William Juul52c07962007-10-31 13:53:06 +01003221 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003222 }
3223
3224 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003225
William Juul52c07962007-10-31 13:53:06 +01003226 chipnr = (int)(to >> chip->chip_shift);
3227 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003228
3229 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003230 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003231 ret = -EIO;
3232 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003233 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003234
William Juul52c07962007-10-31 13:53:06 +01003235 realpage = (int)(to >> chip->page_shift);
3236 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003237
William Juul52c07962007-10-31 13:53:06 +01003238 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003239 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3240 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003241 chip->pagebuf = -1;
3242
Christian Hitzb8a6b372011-10-12 09:32:02 +02003243 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003244 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3245 ret = -EINVAL;
3246 goto err_out;
3247 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003248
Christian Hitz13fc0e22011-10-12 09:32:01 +02003249 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003250 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003251 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003252 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003253 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003254
3255 if (part_pagewr)
3256 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003257 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3258 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3259 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003260 else
3261 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003262
Stefan Roese80877fa2022-09-02 14:10:46 +02003263 schedule();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003264 /* Partial page write?, or need to use bounce buffer */
3265 if (use_bufpoi) {
3266 pr_debug("%s: using write bounce buffer for buf@%p\n",
3267 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003268 if (part_pagewr)
3269 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003270 chip->pagebuf = -1;
3271 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3272 memcpy(&chip->buffers->databuf[column], buf, bytes);
3273 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003274 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003275
Christian Hitzb8a6b372011-10-12 09:32:02 +02003276 if (unlikely(oob)) {
3277 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003278 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003279 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003280 } else {
3281 /* We still need to erase leftover OOB data */
3282 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003283 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003284 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003285 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003286 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003287 if (ret)
3288 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003289
William Juul52c07962007-10-31 13:53:06 +01003290 writelen -= bytes;
3291 if (!writelen)
3292 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003293
Heiko Schocherf5895d12014-06-24 10:10:04 +02003294 column = 0;
3295 buf += bytes;
3296 realpage++;
3297
3298 page = realpage & chip->pagemask;
3299 /* Check, if we cross a chip boundary */
3300 if (!page) {
3301 chipnr++;
3302 chip->select_chip(mtd, -1);
3303 chip->select_chip(mtd, chipnr);
3304 }
3305 }
3306
3307 ops->retlen = ops->len - writelen;
3308 if (unlikely(oob))
3309 ops->oobretlen = ops->ooblen;
3310
3311err_out:
3312 chip->select_chip(mtd, -1);
3313 return ret;
3314}
3315
3316/**
3317 * panic_nand_write - [MTD Interface] NAND write with ECC
3318 * @mtd: MTD device structure
3319 * @to: offset to write to
3320 * @len: number of bytes to write
3321 * @retlen: pointer to variable to store the number of written bytes
3322 * @buf: the data to write
3323 *
3324 * NAND write with ECC. Used when performing writes in interrupt context, this
3325 * may for example be called by mtdoops when writing an oops while in panic.
3326 */
3327static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3328 size_t *retlen, const uint8_t *buf)
3329{
Scott Wood17fed142016-05-30 13:57:56 -05003330 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003331 struct mtd_oob_ops ops;
3332 int ret;
3333
3334 /* Wait for the device to get ready */
3335 panic_nand_wait(mtd, chip, 400);
3336
3337 /* Grab the device */
3338 panic_nand_get_device(chip, mtd, FL_WRITING);
3339
Scott Wood3ea94ed2015-06-26 19:03:26 -05003340 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003341 ops.len = len;
3342 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003343 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003344
Heiko Schocherf5895d12014-06-24 10:10:04 +02003345 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003346
Sergey Lapin3a38a552013-01-14 03:46:50 +00003347 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003348 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003349}
3350
3351/**
William Juul52c07962007-10-31 13:53:06 +01003352 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003353 * @mtd: MTD device structure
3354 * @to: offset to write to
3355 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003356 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003357 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003358 */
William Juul52c07962007-10-31 13:53:06 +01003359static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3360 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003361{
William Juul52c07962007-10-31 13:53:06 +01003362 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003363 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003364
Heiko Schocherf5895d12014-06-24 10:10:04 +02003365 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003366 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003367
Scott Wood52ab7ce2016-05-30 13:57:58 -05003368 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003369
3370 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003371 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003372 pr_debug("%s: attempt to write past end of page\n",
3373 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003374 return -EINVAL;
3375 }
3376
William Juul52c07962007-10-31 13:53:06 +01003377 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003378 pr_debug("%s: attempt to start write outside oob\n",
3379 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003380 return -EINVAL;
3381 }
3382
Christian Hitz13fc0e22011-10-12 09:32:01 +02003383 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003384 if (unlikely(to >= mtd->size ||
3385 ops->ooboffs + ops->ooblen >
3386 ((mtd->size >> chip->page_shift) -
3387 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003388 pr_debug("%s: attempt to write beyond end of device\n",
3389 __func__);
William Juul52c07962007-10-31 13:53:06 +01003390 return -EINVAL;
3391 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003392
William Juul52c07962007-10-31 13:53:06 +01003393 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003394
3395 /*
3396 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3397 * of my DiskOnChip 2000 test units) will clear the whole data page too
3398 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3399 * it in the doc2000 driver in August 1999. dwmw2.
3400 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003401 nand_reset(chip, chipnr);
3402
3403 chip->select_chip(mtd, chipnr);
3404
3405 /* Shift to get page */
3406 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003407
3408 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003409 if (nand_check_wp(mtd)) {
3410 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003411 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003412 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003413
William Juul52c07962007-10-31 13:53:06 +01003414 /* Invalidate the page cache, if we write to the cached page */
3415 if (page == chip->pagebuf)
3416 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003417
Sergey Lapin3a38a552013-01-14 03:46:50 +00003418 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3419
3420 if (ops->mode == MTD_OPS_RAW)
3421 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3422 else
3423 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003424
Heiko Schocherf5895d12014-06-24 10:10:04 +02003425 chip->select_chip(mtd, -1);
3426
William Juul52c07962007-10-31 13:53:06 +01003427 if (status)
3428 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003429
William Juul52c07962007-10-31 13:53:06 +01003430 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003431
William Juul52c07962007-10-31 13:53:06 +01003432 return 0;
3433}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003434
William Juul52c07962007-10-31 13:53:06 +01003435/**
3436 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003437 * @mtd: MTD device structure
3438 * @to: offset to write to
3439 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003440 */
3441static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3442 struct mtd_oob_ops *ops)
3443{
William Juul52c07962007-10-31 13:53:06 +01003444 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003445
William Juul52c07962007-10-31 13:53:06 +01003446 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003447
William Juul52c07962007-10-31 13:53:06 +01003448 /* Do not allow writes past end of device */
3449 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003450 pr_debug("%s: attempt to write beyond end of device\n",
3451 __func__);
William Juul52c07962007-10-31 13:53:06 +01003452 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003453 }
William Juul52c07962007-10-31 13:53:06 +01003454
Heiko Schocherf5895d12014-06-24 10:10:04 +02003455 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003456
Christian Hitz13fc0e22011-10-12 09:32:01 +02003457 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003458 case MTD_OPS_PLACE_OOB:
3459 case MTD_OPS_AUTO_OOB:
3460 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003461 break;
3462
3463 default:
3464 goto out;
3465 }
3466
3467 if (!ops->datbuf)
3468 ret = nand_do_write_oob(mtd, to, ops);
3469 else
3470 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003471
Christian Hitz13fc0e22011-10-12 09:32:01 +02003472out:
William Juul52c07962007-10-31 13:53:06 +01003473 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003474 return ret;
3475}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003476
3477/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003478 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003479 * @mtd: MTD device structure
3480 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003481 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003482 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003483 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003484static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003485{
Scott Wood17fed142016-05-30 13:57:56 -05003486 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003487 unsigned int eraseblock;
3488
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003489 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003490 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003491
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003492 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003493}
3494
3495/**
3496 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003497 * @mtd: MTD device structure
3498 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003499 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003500 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003501 */
William Juul52c07962007-10-31 13:53:06 +01003502static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003503{
William Juul52c07962007-10-31 13:53:06 +01003504 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003505}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003506
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003507/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003508 * nand_erase_nand - [INTERN] erase block(s)
3509 * @mtd: MTD device structure
3510 * @instr: erase instruction
3511 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003512 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003513 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003514 */
William Juul52c07962007-10-31 13:53:06 +01003515int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3516 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003517{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003518 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003519 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003520 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003521
Heiko Schocherf5895d12014-06-24 10:10:04 +02003522 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3523 __func__, (unsigned long long)instr->addr,
3524 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003525
Christian Hitzb8a6b372011-10-12 09:32:02 +02003526 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003527 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003528
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003529 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003530 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003531
3532 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003533 page = (int)(instr->addr >> chip->page_shift);
3534 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003535
3536 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003537 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003538
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003539 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003540 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003541
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003542 /* Check, if it is write protected */
3543 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003544 pr_debug("%s: device is write protected!\n",
3545 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003546 instr->state = MTD_ERASE_FAILED;
3547 goto erase_exit;
3548 }
3549
3550 /* Loop through the pages */
3551 len = instr->len;
3552
3553 instr->state = MTD_ERASING;
3554
3555 while (len) {
Stefan Roese80877fa2022-09-02 14:10:46 +02003556 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003557
Sergey Lapin3a38a552013-01-14 03:46:50 +00003558 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003559 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003560 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003561 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003562 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003563 instr->state = MTD_ERASE_FAILED;
Farhan Ali7c053192021-02-24 15:25:53 -08003564 instr->fail_addr =
3565 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003566 goto erase_exit;
3567 }
William Juul52c07962007-10-31 13:53:06 +01003568
3569 /*
3570 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003571 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003572 */
3573 if (page <= chip->pagebuf && chip->pagebuf <
3574 (page + pages_per_block))
3575 chip->pagebuf = -1;
3576
Scott Wood3ea94ed2015-06-26 19:03:26 -05003577 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003578
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003579 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003580 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003581 pr_debug("%s: failed erase, page 0x%08x\n",
3582 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003583 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003584 instr->fail_addr =
3585 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003586 goto erase_exit;
3587 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003588
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003589 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003590 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003591 page += pages_per_block;
3592
3593 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003594 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003595 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003596 chip->select_chip(mtd, -1);
3597 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003598 }
3599 }
3600 instr->state = MTD_ERASE_DONE;
3601
Christian Hitz13fc0e22011-10-12 09:32:01 +02003602erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003603
3604 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003605
3606 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003607 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003608 nand_release_device(mtd);
3609
3610 /* Return more or less happy */
3611 return ret;
3612}
3613
3614/**
3615 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003616 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003617 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003618 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003619 */
William Juul52c07962007-10-31 13:53:06 +01003620static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003621{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003622 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003623
3624 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003625 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003626 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003627 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003628}
3629
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003630/**
William Juul52c07962007-10-31 13:53:06 +01003631 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003632 * @mtd: MTD device structure
3633 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003634 */
William Juul52c07962007-10-31 13:53:06 +01003635static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003636{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003637 struct nand_chip *chip = mtd_to_nand(mtd);
3638 int chipnr = (int)(offs >> chip->chip_shift);
3639 int ret;
3640
3641 /* Select the NAND device */
3642 nand_get_device(mtd, FL_READING);
3643 chip->select_chip(mtd, chipnr);
3644
3645 ret = nand_block_checkbad(mtd, offs, 0);
3646
3647 chip->select_chip(mtd, -1);
3648 nand_release_device(mtd);
3649
3650 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003651}
3652
3653/**
William Juul52c07962007-10-31 13:53:06 +01003654 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003655 * @mtd: MTD device structure
3656 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003657 */
William Juul52c07962007-10-31 13:53:06 +01003658static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003659{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003660 int ret;
3661
Christian Hitzb8a6b372011-10-12 09:32:02 +02003662 ret = nand_block_isbad(mtd, ofs);
3663 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003664 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003665 if (ret > 0)
3666 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003667 return ret;
3668 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003669
Heiko Schocherf5895d12014-06-24 10:10:04 +02003670 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003671}
3672
Heiko Schocherf5895d12014-06-24 10:10:04 +02003673/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003674 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3675 * @mtd: MTD device structure
3676 * @chip: nand chip info structure
3677 * @addr: feature address.
3678 * @subfeature_param: the subfeature parameters, a four bytes array.
3679 */
3680static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3681 int addr, uint8_t *subfeature_param)
3682{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003683#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3684 if (!chip->onfi_version ||
3685 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3686 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003687 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003688#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003689
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003690 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003691}
3692
3693/**
3694 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3695 * @mtd: MTD device structure
3696 * @chip: nand chip info structure
3697 * @addr: feature address.
3698 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003699 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003700static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3701 int addr, uint8_t *subfeature_param)
3702{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003703#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3704 if (!chip->onfi_version ||
3705 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3706 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003707 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003708#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003709
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003710 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003711}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003712
Sergey Lapin3a38a552013-01-14 03:46:50 +00003713/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003714static void nand_set_defaults(struct nand_chip *chip, int busw)
3715{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003716 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003717 if (!chip->chip_delay)
3718 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003719
3720 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003721 if (chip->cmdfunc == NULL)
3722 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003723
3724 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003725 if (chip->waitfunc == NULL)
3726 chip->waitfunc = nand_wait;
3727
3728 if (!chip->select_chip)
3729 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003730
3731 /* set for ONFI nand */
3732 if (!chip->onfi_set_features)
3733 chip->onfi_set_features = nand_onfi_set_features;
3734 if (!chip->onfi_get_features)
3735 chip->onfi_get_features = nand_onfi_get_features;
3736
3737 /* If called twice, pointers that depend on busw may need to be reset */
3738 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003739 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3740 if (!chip->read_word)
3741 chip->read_word = nand_read_word;
3742 if (!chip->block_bad)
3743 chip->block_bad = nand_block_bad;
3744 if (!chip->block_markbad)
3745 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003746 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003747 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003748 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3749 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3750 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003751 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Roger Quadrosb590f612022-12-20 12:21:57 +02003752
Simon Glass7ec24132024-09-29 19:49:48 -06003753#ifndef CONFIG_XPL_BUILD
William Juul52c07962007-10-31 13:53:06 +01003754 if (!chip->scan_bbt)
3755 chip->scan_bbt = nand_default_bbt;
Roger Quadrosb590f612022-12-20 12:21:57 +02003756#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02003757
3758 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003759 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003760 spin_lock_init(&chip->controller->lock);
3761 init_waitqueue_head(&chip->controller->wq);
3762 }
3763
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003764 if (!chip->buf_align)
3765 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003766}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003767
Sergey Lapin3a38a552013-01-14 03:46:50 +00003768/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003769static void sanitize_string(char *s, size_t len)
3770{
3771 ssize_t i;
3772
Sergey Lapin3a38a552013-01-14 03:46:50 +00003773 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003774 s[len - 1] = 0;
3775
Sergey Lapin3a38a552013-01-14 03:46:50 +00003776 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003777 for (i = 0; i < len - 1; i++) {
3778 if (s[i] < ' ' || s[i] > 127)
3779 s[i] = '?';
3780 }
3781
Sergey Lapin3a38a552013-01-14 03:46:50 +00003782 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003783 strim(s);
3784}
3785
Florian Fainellic98a9352011-02-25 00:01:34 +00003786static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3787{
3788 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003789 while (len--) {
3790 crc ^= *p++ << 8;
3791 for (i = 0; i < 8; i++)
3792 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3793 }
3794
3795 return crc;
3796}
3797
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003798#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003799/* Parse the Extended Parameter Page. */
3800static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3801 struct nand_chip *chip, struct nand_onfi_params *p)
3802{
3803 struct onfi_ext_param_page *ep;
3804 struct onfi_ext_section *s;
3805 struct onfi_ext_ecc_info *ecc;
3806 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003807 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003808 int len;
3809 int i;
3810
3811 len = le16_to_cpu(p->ext_param_page_length) * 16;
3812 ep = kmalloc(len, GFP_KERNEL);
3813 if (!ep)
3814 return -ENOMEM;
3815
3816 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003817 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3818 if (ret)
3819 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003820
3821 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003822 ret = nand_change_read_column_op(chip,
3823 sizeof(*p) * p->num_of_param_pages,
3824 ep, len, true);
3825 if (ret)
3826 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003827
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003828 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003829 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3830 != le16_to_cpu(ep->crc))) {
3831 pr_debug("fail in the CRC.\n");
3832 goto ext_out;
3833 }
3834
3835 /*
3836 * Check the signature.
3837 * Do not strictly follow the ONFI spec, maybe changed in future.
3838 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003839 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003840 pr_debug("The signature is invalid.\n");
3841 goto ext_out;
3842 }
3843
3844 /* find the ECC section. */
3845 cursor = (uint8_t *)(ep + 1);
3846 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3847 s = ep->sections + i;
3848 if (s->type == ONFI_SECTION_TYPE_2)
3849 break;
3850 cursor += s->length * 16;
3851 }
3852 if (i == ONFI_EXT_SECTION_MAX) {
3853 pr_debug("We can not find the ECC section.\n");
3854 goto ext_out;
3855 }
3856
3857 /* get the info we want. */
3858 ecc = (struct onfi_ext_ecc_info *)cursor;
3859
3860 if (!ecc->codeword_size) {
3861 pr_debug("Invalid codeword size\n");
3862 goto ext_out;
3863 }
3864
3865 chip->ecc_strength_ds = ecc->ecc_bits;
3866 chip->ecc_step_ds = 1 << ecc->codeword_size;
3867 ret = 0;
3868
3869ext_out:
3870 kfree(ep);
3871 return ret;
3872}
3873
Florian Fainellic98a9352011-02-25 00:01:34 +00003874/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003875 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003876 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003877static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003878{
3879 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003880 char id[4];
3881 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003882
Sergey Lapin3a38a552013-01-14 03:46:50 +00003883 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003884 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3885 if (ret || strncmp(id, "ONFI", 4))
3886 return 0;
3887
3888 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3889 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003890 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003891
Florian Fainellic98a9352011-02-25 00:01:34 +00003892 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003893 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3894 if (ret)
3895 return 0;
3896
Florian Fainellic98a9352011-02-25 00:01:34 +00003897 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003898 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003899 break;
3900 }
3901 }
3902
Heiko Schocherf5895d12014-06-24 10:10:04 +02003903 if (i == 3) {
3904 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003905 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003906 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003907
Sergey Lapin3a38a552013-01-14 03:46:50 +00003908 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003909 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003910 if (val & (1 << 5))
3911 chip->onfi_version = 23;
3912 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003913 chip->onfi_version = 22;
3914 else if (val & (1 << 3))
3915 chip->onfi_version = 21;
3916 else if (val & (1 << 2))
3917 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003918 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003919 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003920
3921 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003922 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003923 return 0;
3924 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003925
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003926 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3927 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003928 if (!mtd->name)
3929 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003930
Florian Fainellic98a9352011-02-25 00:01:34 +00003931 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003932
3933 /*
3934 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3935 * (don't ask me who thought of this...). MTD assumes that these
3936 * dimensions will be power-of-2, so just truncate the remaining area.
3937 */
3938 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3939 mtd->erasesize *= mtd->writesize;
3940
Florian Fainellic98a9352011-02-25 00:01:34 +00003941 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003942
3943 /* See erasesize comment */
3944 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003945 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003946 chip->bits_per_cell = p->bits_per_cell;
3947
3948 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003949 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003950
3951 if (p->ecc_bits != 0xff) {
3952 chip->ecc_strength_ds = p->ecc_bits;
3953 chip->ecc_step_ds = 512;
3954 } else if (chip->onfi_version >= 21 &&
3955 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3956
3957 /*
3958 * The nand_flash_detect_ext_param_page() uses the
3959 * Change Read Column command which maybe not supported
3960 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3961 * now. We do not replace user supplied command function.
3962 */
3963 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3964 chip->cmdfunc = nand_command_lp;
3965
3966 /* The Extended Parameter Page is supported since ONFI 2.1. */
3967 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3968 pr_warn("Failed to detect ONFI extended param page\n");
3969 } else {
3970 pr_warn("Could not retrieve ONFI ECC requirements\n");
3971 }
3972
Florian Fainellic98a9352011-02-25 00:01:34 +00003973 return 1;
3974}
3975#else
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003976static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003977{
3978 return 0;
3979}
3980#endif
3981
William Juul52c07962007-10-31 13:53:06 +01003982/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003983 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3984 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003985static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip)
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003986{
3987 struct nand_jedec_params *p = &chip->jedec_params;
3988 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003989 char id[5];
3990 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003991
3992 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003993 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
3994 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003995 return 0;
3996
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003997 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
3998 if (ret)
3999 return 0;
4000
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004001 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004002 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4003 if (ret)
4004 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004005
4006 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4007 le16_to_cpu(p->crc))
4008 break;
4009 }
4010
4011 if (i == 3) {
4012 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4013 return 0;
4014 }
4015
4016 /* Check version */
4017 val = le16_to_cpu(p->revision);
4018 if (val & (1 << 2))
4019 chip->jedec_version = 10;
4020 else if (val & (1 << 1))
4021 chip->jedec_version = 1; /* vendor specific version */
4022
4023 if (!chip->jedec_version) {
4024 pr_info("unsupported JEDEC version: %d\n", val);
4025 return 0;
4026 }
4027
4028 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4029 sanitize_string(p->model, sizeof(p->model));
4030 if (!mtd->name)
4031 mtd->name = p->model;
4032
4033 mtd->writesize = le32_to_cpu(p->byte_per_page);
4034
4035 /* Please reference to the comment for nand_flash_detect_onfi. */
4036 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4037 mtd->erasesize *= mtd->writesize;
4038
4039 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4040
4041 /* Please reference to the comment for nand_flash_detect_onfi. */
4042 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4043 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4044 chip->bits_per_cell = p->bits_per_cell;
4045
4046 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004047 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004048
4049 /* ECC info */
4050 ecc = &p->ecc_info[0];
4051
4052 if (ecc->codeword_size >= 9) {
4053 chip->ecc_strength_ds = ecc->ecc_bits;
4054 chip->ecc_step_ds = 1 << ecc->codeword_size;
4055 } else {
4056 pr_warn("Invalid codeword size\n");
4057 }
4058
4059 return 1;
4060}
4061
4062/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004063 * nand_id_has_period - Check if an ID string has a given wraparound period
4064 * @id_data: the ID string
4065 * @arrlen: the length of the @id_data array
4066 * @period: the period of repitition
4067 *
4068 * Check if an ID string is repeated within a given sequence of bytes at
4069 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02004070 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00004071 * if the repetition has a period of @period; otherwise, returns zero.
4072 */
4073static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4074{
4075 int i, j;
4076 for (i = 0; i < period; i++)
4077 for (j = i + period; j < arrlen; j += period)
4078 if (id_data[i] != id_data[j])
4079 return 0;
4080 return 1;
4081}
4082
4083/*
4084 * nand_id_len - Get the length of an ID string returned by CMD_READID
4085 * @id_data: the ID string
4086 * @arrlen: the length of the @id_data array
4087
4088 * Returns the length of the ID string, according to known wraparound/trailing
4089 * zero patterns. If no pattern exists, returns the length of the array.
4090 */
4091static int nand_id_len(u8 *id_data, int arrlen)
4092{
4093 int last_nonzero, period;
4094
4095 /* Find last non-zero byte */
4096 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4097 if (id_data[last_nonzero])
4098 break;
4099
4100 /* All zeros */
4101 if (last_nonzero < 0)
4102 return 0;
4103
4104 /* Calculate wraparound period */
4105 for (period = 1; period < arrlen; period++)
4106 if (nand_id_has_period(id_data, arrlen, period))
4107 break;
4108
4109 /* There's a repeated pattern */
4110 if (period < arrlen)
4111 return period;
4112
4113 /* There are trailing zeros */
4114 if (last_nonzero < arrlen - 1)
4115 return last_nonzero + 1;
4116
4117 /* No pattern detected */
4118 return arrlen;
4119}
4120
Heiko Schocherf5895d12014-06-24 10:10:04 +02004121/* Extract the bits of per cell from the 3rd byte of the extended ID */
4122static int nand_get_bits_per_cell(u8 cellinfo)
4123{
4124 int bits;
4125
4126 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4127 bits >>= NAND_CI_CELLTYPE_SHIFT;
4128 return bits + 1;
4129}
4130
Sergey Lapin3a38a552013-01-14 03:46:50 +00004131/*
4132 * Many new NAND share similar device ID codes, which represent the size of the
4133 * chip. The rest of the parameters must be decoded according to generic or
4134 * manufacturer-specific "extended ID" decoding patterns.
4135 */
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004136void nand_decode_ext_id(struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004137{
Alexander Dahlaa124532024-03-20 10:02:09 +01004138 struct mtd_info *mtd = nand_to_mtd(chip);
Michael Trimarchi3ba671b2022-07-20 18:22:11 +02004139 int extid;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004140 /* The 3rd id byte holds MLC / multichip data */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004141 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004142 /* The 4th id byte is the important one */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004143 extid = chip->id.data[3];
Sergey Lapin3a38a552013-01-14 03:46:50 +00004144
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004145 /* Calc pagesize */
4146 mtd->writesize = 1024 << (extid & 0x03);
4147 extid >>= 2;
4148 /* Calc oobsize */
4149 mtd->oobsize = (8 << (extid & 0x01)) *
4150 (mtd->writesize >> 9);
4151 extid >>= 2;
4152 /* Calc blocksize. Blocksize is multiples of 64KiB */
4153 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4154 extid >>= 2;
4155 /* Get buswidth information */
4156 /* Get buswidth information */
4157 if (extid & 0x1)
4158 chip->options |= NAND_BUSWIDTH_16;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004159}
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004160EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004161
Heiko Schocherf5895d12014-06-24 10:10:04 +02004162/*
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004163 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4164 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4165 * table.
4166 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004167static void nand_manufacturer_detect(struct nand_chip *chip)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004168{
4169 /*
4170 * Try manufacturer detection if available and use
4171 * nand_decode_ext_id() otherwise.
4172 */
4173 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004174 chip->manufacturer.desc->ops->detect) {
4175 /* The 3rd id byte holds MLC / multichip data */
4176 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004177 chip->manufacturer.desc->ops->detect(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004178 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004179 nand_decode_ext_id(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004180 }
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004181}
4182
4183/*
4184 * Manufacturer initialization. This function is called for all NANDs including
4185 * ONFI and JEDEC compliant ones.
4186 * Manufacturer drivers should put all their specific initialization code in
4187 * their ->init() hook.
4188 */
4189static int nand_manufacturer_init(struct nand_chip *chip)
4190{
4191 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4192 !chip->manufacturer.desc->ops->init)
4193 return 0;
4194
4195 return chip->manufacturer.desc->ops->init(chip);
4196}
4197
4198/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004199 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4200 * decodes a matching ID table entry and assigns the MTD size parameters for
4201 * the chip.
4202 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004203static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004204{
Alexander Dahlaa124532024-03-20 10:02:09 +01004205 struct mtd_info *mtd = nand_to_mtd(chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004206
4207 mtd->erasesize = type->erasesize;
4208 mtd->writesize = type->pagesize;
4209 mtd->oobsize = mtd->writesize / 32;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004210
Heiko Schocherf5895d12014-06-24 10:10:04 +02004211 /* All legacy ID NAND are small-page, SLC */
4212 chip->bits_per_cell = 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004213}
4214
Heiko Schocherf5895d12014-06-24 10:10:04 +02004215/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004216 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4217 * heuristic patterns using various detected parameters (e.g., manufacturer,
4218 * page size, cell-type information).
4219 */
4220static void nand_decode_bbm_options(struct mtd_info *mtd,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004221 struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004222{
Sergey Lapin3a38a552013-01-14 03:46:50 +00004223 /* Set the bad block position */
4224 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4225 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4226 else
4227 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004228}
4229
Heiko Schocherf5895d12014-06-24 10:10:04 +02004230static inline bool is_full_id_nand(struct nand_flash_dev *type)
4231{
4232 return type->id_len;
4233}
4234
4235static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004236 struct nand_flash_dev *type)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004237{
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004238 if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004239 mtd->writesize = type->pagesize;
4240 mtd->erasesize = type->erasesize;
4241 mtd->oobsize = type->oobsize;
4242
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004243 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004244 chip->chipsize = (uint64_t)type->chipsize << 20;
4245 chip->options |= type->options;
4246 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4247 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004248 chip->onfi_timing_mode_default =
4249 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004250
Heiko Schocherf5895d12014-06-24 10:10:04 +02004251 if (!mtd->name)
4252 mtd->name = type->name;
4253
4254 return true;
4255 }
4256 return false;
4257}
4258
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004259/**
4260 * nand_get_manufacturer_desc - Get manufacturer information from the
4261 * manufacturer ID
4262 * @id: manufacturer ID
4263 *
4264 * Returns a nand_manufacturer_desc object if the manufacturer is defined
4265 * in the NAND manufacturers database, NULL otherwise.
4266 */
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004267static const struct nand_manufacturer *nand_get_manufacturer_desc(u8 id)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004268{
4269 int i;
4270
4271 for (i = 0; nand_manuf_ids[i].id != 0x0; i++) {
4272 if (nand_manuf_ids[i].id == id)
4273 return &nand_manuf_ids[i];
4274 }
4275
4276 return NULL;
4277}
4278
Sergey Lapin3a38a552013-01-14 03:46:50 +00004279/*
4280 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004281 */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004282int nand_detect(struct nand_chip *chip, int *maf_id,
4283 int *dev_id, struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004284{
Alexander Dahlaa124532024-03-20 10:02:09 +01004285 struct mtd_info *mtd = nand_to_mtd(chip);
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004286 const struct nand_manufacturer *manufacturer_desc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004287 int busw, ret;
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004288 u8 *id_data = chip->id.data;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004289
Karl Beldanb6322fc2008-09-15 16:08:03 +02004290 /*
4291 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004292 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004293 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004294 ret = nand_reset(chip, 0);
4295 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004296 return ret;
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004297
4298 /* Select the device */
4299 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004300
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004301 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004302 ret = nand_readid_op(chip, 0, id_data, 2);
4303 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004304 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004305
4306 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004307 *maf_id = id_data[0];
4308 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004309
Sergey Lapin3a38a552013-01-14 03:46:50 +00004310 /*
4311 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004312 * interface concerns can cause random data which looks like a
4313 * possibly credible NAND flash to appear. If the two results do
4314 * not match, ignore the device completely.
4315 */
4316
Sergey Lapin3a38a552013-01-14 03:46:50 +00004317 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004318 ret = nand_readid_op(chip, 0, id_data, 8);
4319 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004320 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05004321
Christian Hitzb8a6b372011-10-12 09:32:02 +02004322 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004323 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004324 *maf_id, *dev_id, id_data[0], id_data[1]);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004325 return -ENODEV;
Scott Wood3628f002008-10-24 16:20:43 -05004326 }
4327
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004328 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
4329
4330 /* Try to identify manufacturer */
4331 manufacturer_desc = nand_get_manufacturer_desc(*maf_id);
4332 chip->manufacturer.desc = manufacturer_desc;
4333
Lei Wen75bde942011-01-06 09:48:18 +08004334 if (!type)
4335 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004336
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004337 /*
4338 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4339 * override it.
4340 * This is required to make sure initial NAND bus width set by the
4341 * NAND controller driver is coherent with the real NAND bus width
4342 * (extracted by auto-detection code).
4343 */
4344 busw = chip->options & NAND_BUSWIDTH_16;
4345
4346 /*
4347 * The flag is only set (never cleared), reset it to its default value
4348 * before starting auto-detection.
4349 */
4350 chip->options &= ~NAND_BUSWIDTH_16;
4351
Heiko Schocherf5895d12014-06-24 10:10:04 +02004352 for (; type->name != NULL; type++) {
4353 if (is_full_id_nand(type)) {
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004354 if (find_full_id_nand(mtd, chip, type))
Heiko Schocherf5895d12014-06-24 10:10:04 +02004355 goto ident_done;
4356 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004357 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004358 }
4359 }
Lei Wen75bde942011-01-06 09:48:18 +08004360
Christian Hitzb8a6b372011-10-12 09:32:02 +02004361 chip->onfi_version = 0;
4362 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004363 /* Check if the chip is ONFI compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004364 if (nand_flash_detect_onfi(mtd, chip))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004365 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004366
4367 /* Check if the chip is JEDEC compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004368 if (nand_flash_detect_jedec(mtd, chip))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004369 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004370 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004371
Christian Hitzb8a6b372011-10-12 09:32:02 +02004372 if (!type->name)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004373 return -ENODEV;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004374
William Juul52c07962007-10-31 13:53:06 +01004375 if (!mtd->name)
4376 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004377
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004378 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004379
Scott Wood52ab7ce2016-05-30 13:57:58 -05004380 if (!type->pagesize) {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004381 nand_manufacturer_detect(chip);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004382 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004383 nand_decode_id(chip, type);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004384 }
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004385
Heiko Schocherf5895d12014-06-24 10:10:04 +02004386 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004387 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004388
Christian Hitzb8a6b372011-10-12 09:32:02 +02004389ident_done:
4390
Heiko Schocherf5895d12014-06-24 10:10:04 +02004391 if (chip->options & NAND_BUSWIDTH_AUTO) {
4392 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4393 chip->options |= busw;
4394 nand_set_defaults(chip, busw);
4395 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4396 /*
4397 * Check, if buswidth is correct. Hardware drivers should set
4398 * chip correct!
4399 */
4400 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4401 *maf_id, *dev_id);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004402 pr_info("%s %s\n", manufacturer_desc->name, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004403 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004404 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4405 busw ? 16 : 8);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004406 return -EINVAL;
William Juul52c07962007-10-31 13:53:06 +01004407 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004408
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004409 nand_decode_bbm_options(mtd, chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004410
William Juul52c07962007-10-31 13:53:06 +01004411 /* Calculate the address shift from the page size */
4412 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004413 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004414 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004415
William Juul52c07962007-10-31 13:53:06 +01004416 chip->bbt_erase_shift = chip->phys_erase_shift =
4417 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004418 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004419 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004420 else {
4421 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4422 chip->chip_shift += 32 - 1;
4423 }
4424
Masahiro Yamada984926b2017-11-22 02:38:31 +09004425 if (chip->chip_shift - chip->page_shift > 16)
4426 chip->options |= NAND_ROW_ADDR_3;
4427
Christian Hitzb8a6b372011-10-12 09:32:02 +02004428 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004429 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004430
Sergey Lapin3a38a552013-01-14 03:46:50 +00004431 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004432 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4433 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004434
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004435 ret = nand_manufacturer_init(chip);
4436 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004437 return ret;
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004438
Heiko Schocherf5895d12014-06-24 10:10:04 +02004439 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4440 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004441
Christian Hitzb8a6b372011-10-12 09:32:02 +02004442#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004443 if (chip->onfi_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004444 pr_info("%s %s\n", manufacturer_desc->name,
4445 chip->onfi_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004446 else if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004447 pr_info("%s %s\n", manufacturer_desc->name,
4448 chip->jedec_params.model);
Sean Andersone476f112023-11-04 16:37:40 -04004449 else if (manufacturer_desc)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004450 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004451#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004452 if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004453 pr_info("%s %s\n", manufacturer_desc->name,
4454 chip->jedec_params.model);
Sean Andersone476f112023-11-04 16:37:40 -04004455 else if (manufacturer_desc)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004456 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004457#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004458
Scott Wood3ea94ed2015-06-26 19:03:26 -05004459 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004460 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004461 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004462 return 0;
William Juul52c07962007-10-31 13:53:06 +01004463}
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004464EXPORT_SYMBOL(nand_detect);
William Juul52c07962007-10-31 13:53:06 +01004465
Brian Norrisba6463d2016-06-15 21:09:22 +02004466#if CONFIG_IS_ENABLED(OF_CONTROL)
Brian Norrisba6463d2016-06-15 21:09:22 +02004467
Patrice Chotardbc77af52021-09-13 16:25:53 +02004468static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004469{
4470 int ret, ecc_mode = -1, ecc_strength, ecc_step;
Linus Walleij4e8611a2023-04-07 15:40:05 +02004471 int ecc_algo = NAND_ECC_UNKNOWN;
Brian Norrisba6463d2016-06-15 21:09:22 +02004472 const char *str;
4473
Patrice Chotardbc77af52021-09-13 16:25:53 +02004474 ret = ofnode_read_s32_default(node, "nand-bus-width", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004475 if (ret == 16)
4476 chip->options |= NAND_BUSWIDTH_16;
4477
Arseniy Krasnov8b4a27a2024-08-26 16:17:08 +03004478 if (ofnode_read_bool(node, "nand-is-boot-medium"))
4479 chip->options |= NAND_IS_BOOT_MEDIUM;
4480
Patrice Chotardbc77af52021-09-13 16:25:53 +02004481 if (ofnode_read_bool(node, "nand-on-flash-bbt"))
Brian Norrisba6463d2016-06-15 21:09:22 +02004482 chip->bbt_options |= NAND_BBT_USE_FLASH;
4483
Patrice Chotardbc77af52021-09-13 16:25:53 +02004484 str = ofnode_read_string(node, "nand-ecc-mode");
Brian Norrisba6463d2016-06-15 21:09:22 +02004485 if (str) {
4486 if (!strcmp(str, "none"))
4487 ecc_mode = NAND_ECC_NONE;
4488 else if (!strcmp(str, "soft"))
4489 ecc_mode = NAND_ECC_SOFT;
4490 else if (!strcmp(str, "hw"))
4491 ecc_mode = NAND_ECC_HW;
4492 else if (!strcmp(str, "hw_syndrome"))
4493 ecc_mode = NAND_ECC_HW_SYNDROME;
4494 else if (!strcmp(str, "hw_oob_first"))
4495 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4496 else if (!strcmp(str, "soft_bch"))
4497 ecc_mode = NAND_ECC_SOFT_BCH;
4498 }
4499
Linus Walleij4e8611a2023-04-07 15:40:05 +02004500 str = ofnode_read_string(node, "nand-ecc-algo");
4501 if (str) {
4502 /*
4503 * If we are in NAND_ECC_SOFT mode, just alter the
4504 * soft mode to BCH here. No change of algorithm.
4505 */
4506 if (ecc_mode == NAND_ECC_SOFT) {
4507 if (!strcmp(str, "bch"))
4508 ecc_mode = NAND_ECC_SOFT_BCH;
4509 } else {
4510 if (!strcmp(str, "bch")) {
4511 ecc_algo = NAND_ECC_BCH;
4512 } else if (!strcmp(str, "hamming")) {
4513 ecc_algo = NAND_ECC_HAMMING;
4514 }
4515 }
Pali Rohár44acf8a2022-04-04 18:17:21 +02004516 }
4517
Patrice Chotardbc77af52021-09-13 16:25:53 +02004518 ecc_strength = ofnode_read_s32_default(node,
4519 "nand-ecc-strength", -1);
4520 ecc_step = ofnode_read_s32_default(node,
4521 "nand-ecc-step-size", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004522
4523 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4524 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4525 pr_err("must set both strength and step size in DT\n");
4526 return -EINVAL;
4527 }
4528
Linus Walleij4e8611a2023-04-07 15:40:05 +02004529 /*
4530 * Chip drivers may have assigned default algorithms here,
4531 * onlt override it if we have found something explicitly
4532 * specified in the device tree.
4533 */
4534 if (ecc_algo != NAND_ECC_UNKNOWN)
4535 chip->ecc.algo = ecc_algo;
4536
Brian Norrisba6463d2016-06-15 21:09:22 +02004537 if (ecc_mode >= 0)
4538 chip->ecc.mode = ecc_mode;
4539
4540 if (ecc_strength >= 0)
4541 chip->ecc.strength = ecc_strength;
4542
4543 if (ecc_step > 0)
4544 chip->ecc.size = ecc_step;
4545
Patrice Chotardbc77af52021-09-13 16:25:53 +02004546 if (ofnode_read_bool(node, "nand-ecc-maximize"))
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004547 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4548
Brian Norrisba6463d2016-06-15 21:09:22 +02004549 return 0;
4550}
4551#else
Patrice Chotardbc77af52021-09-13 16:25:53 +02004552static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004553{
4554 return 0;
4555}
4556#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4557
William Juul52c07962007-10-31 13:53:06 +01004558/**
4559 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004560 * @mtd: MTD device structure
4561 * @maxchips: number of chips to scan for
4562 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004563 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004564 * This is the first phase of the normal nand_scan() function. It reads the
4565 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004566 *
William Juul52c07962007-10-31 13:53:06 +01004567 */
Lei Wen75bde942011-01-06 09:48:18 +08004568int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004569 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004570{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004571 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004572 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba6463d2016-06-15 21:09:22 +02004573 int ret;
4574
Patrice Chotardbc77af52021-09-13 16:25:53 +02004575 if (ofnode_valid(chip->flash_node)) {
Brian Norrisba6463d2016-06-15 21:09:22 +02004576 ret = nand_dt_init(mtd, chip, chip->flash_node);
4577 if (ret)
4578 return ret;
4579 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004580
William Juul52c07962007-10-31 13:53:06 +01004581 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004582 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004583
4584 /* Read the flash type */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004585 ret = nand_detect(chip, &nand_maf_id, &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004586
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004587 if (ret) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004588 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4589 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004590 chip->select_chip(mtd, -1);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004591 return ret;
William Juul52c07962007-10-31 13:53:06 +01004592 }
4593
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004594 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004595 ret = nand_init_data_interface(chip);
4596 if (ret)
4597 return ret;
4598
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004599 /*
4600 * Setup the data interface correctly on the chip and controller side.
4601 * This explicit call to nand_setup_data_interface() is only required
4602 * for the first die, because nand_reset() has been called before
4603 * ->data_interface and ->default_onfi_timing_mode were set.
4604 * For the other dies, nand_reset() will automatically switch to the
4605 * best mode for us.
4606 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004607 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004608 if (ret)
4609 return ret;
4610
Heiko Schocherf5895d12014-06-24 10:10:04 +02004611 chip->select_chip(mtd, -1);
4612
William Juul52c07962007-10-31 13:53:06 +01004613 /* Check for a chip array */
4614 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004615 u8 id[2];
4616
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004617 /* See comment in nand_detect for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004618 nand_reset(chip, i);
4619
4620 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004621 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004622 nand_readid_op(chip, 0, id, sizeof(id));
4623
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004624 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004625 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004626 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004627 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004628 }
4629 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004630 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004631
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004632#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004633 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004634 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004635#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004636
William Juul52c07962007-10-31 13:53:06 +01004637 /* Store the number of chips and calc total size for mtd */
4638 chip->numchips = i;
4639 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004640
William Juul52c07962007-10-31 13:53:06 +01004641 return 0;
4642}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004643EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004644
Masahiro Yamada820eb482017-11-22 02:38:29 +09004645/**
4646 * nand_check_ecc_caps - check the sanity of preset ECC settings
4647 * @chip: nand chip info structure
4648 * @caps: ECC caps info structure
4649 * @oobavail: OOB size that the ECC engine can use
4650 *
4651 * When ECC step size and strength are already set, check if they are supported
4652 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4653 * On success, the calculated ECC bytes is set.
4654 */
4655int nand_check_ecc_caps(struct nand_chip *chip,
4656 const struct nand_ecc_caps *caps, int oobavail)
4657{
4658 struct mtd_info *mtd = nand_to_mtd(chip);
4659 const struct nand_ecc_step_info *stepinfo;
4660 int preset_step = chip->ecc.size;
4661 int preset_strength = chip->ecc.strength;
4662 int nsteps, ecc_bytes;
4663 int i, j;
4664
4665 if (WARN_ON(oobavail < 0))
4666 return -EINVAL;
4667
4668 if (!preset_step || !preset_strength)
4669 return -ENODATA;
4670
4671 nsteps = mtd->writesize / preset_step;
4672
4673 for (i = 0; i < caps->nstepinfos; i++) {
4674 stepinfo = &caps->stepinfos[i];
4675
4676 if (stepinfo->stepsize != preset_step)
4677 continue;
4678
4679 for (j = 0; j < stepinfo->nstrengths; j++) {
4680 if (stepinfo->strengths[j] != preset_strength)
4681 continue;
4682
4683 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4684 preset_strength);
4685 if (WARN_ON_ONCE(ecc_bytes < 0))
4686 return ecc_bytes;
4687
4688 if (ecc_bytes * nsteps > oobavail) {
4689 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4690 preset_step, preset_strength);
4691 return -ENOSPC;
4692 }
4693
4694 chip->ecc.bytes = ecc_bytes;
4695
4696 return 0;
4697 }
4698 }
4699
4700 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4701 preset_step, preset_strength);
4702
4703 return -ENOTSUPP;
4704}
4705EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4706
4707/**
4708 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4709 * @chip: nand chip info structure
4710 * @caps: ECC engine caps info structure
4711 * @oobavail: OOB size that the ECC engine can use
4712 *
4713 * If a chip's ECC requirement is provided, try to meet it with the least
4714 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4715 * On success, the chosen ECC settings are set.
4716 */
4717int nand_match_ecc_req(struct nand_chip *chip,
4718 const struct nand_ecc_caps *caps, int oobavail)
4719{
4720 struct mtd_info *mtd = nand_to_mtd(chip);
4721 const struct nand_ecc_step_info *stepinfo;
4722 int req_step = chip->ecc_step_ds;
4723 int req_strength = chip->ecc_strength_ds;
4724 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4725 int best_step, best_strength, best_ecc_bytes;
4726 int best_ecc_bytes_total = INT_MAX;
4727 int i, j;
4728
4729 if (WARN_ON(oobavail < 0))
4730 return -EINVAL;
4731
4732 /* No information provided by the NAND chip */
4733 if (!req_step || !req_strength)
4734 return -ENOTSUPP;
4735
4736 /* number of correctable bits the chip requires in a page */
4737 req_corr = mtd->writesize / req_step * req_strength;
4738
4739 for (i = 0; i < caps->nstepinfos; i++) {
4740 stepinfo = &caps->stepinfos[i];
4741 step_size = stepinfo->stepsize;
4742
4743 for (j = 0; j < stepinfo->nstrengths; j++) {
4744 strength = stepinfo->strengths[j];
4745
4746 /*
4747 * If both step size and strength are smaller than the
4748 * chip's requirement, it is not easy to compare the
4749 * resulted reliability.
4750 */
4751 if (step_size < req_step && strength < req_strength)
4752 continue;
4753
4754 if (mtd->writesize % step_size)
4755 continue;
4756
4757 nsteps = mtd->writesize / step_size;
4758
4759 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4760 if (WARN_ON_ONCE(ecc_bytes < 0))
4761 continue;
4762 ecc_bytes_total = ecc_bytes * nsteps;
4763
4764 if (ecc_bytes_total > oobavail ||
4765 strength * nsteps < req_corr)
4766 continue;
4767
4768 /*
4769 * We assume the best is to meet the chip's requrement
4770 * with the least number of ECC bytes.
4771 */
4772 if (ecc_bytes_total < best_ecc_bytes_total) {
4773 best_ecc_bytes_total = ecc_bytes_total;
4774 best_step = step_size;
4775 best_strength = strength;
4776 best_ecc_bytes = ecc_bytes;
4777 }
4778 }
4779 }
4780
4781 if (best_ecc_bytes_total == INT_MAX)
4782 return -ENOTSUPP;
4783
4784 chip->ecc.size = best_step;
4785 chip->ecc.strength = best_strength;
4786 chip->ecc.bytes = best_ecc_bytes;
4787
4788 return 0;
4789}
4790EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4791
4792/**
4793 * nand_maximize_ecc - choose the max ECC strength available
4794 * @chip: nand chip info structure
4795 * @caps: ECC engine caps info structure
4796 * @oobavail: OOB size that the ECC engine can use
4797 *
4798 * Choose the max ECC strength that is supported on the controller, and can fit
4799 * within the chip's OOB. On success, the chosen ECC settings are set.
4800 */
4801int nand_maximize_ecc(struct nand_chip *chip,
4802 const struct nand_ecc_caps *caps, int oobavail)
4803{
4804 struct mtd_info *mtd = nand_to_mtd(chip);
4805 const struct nand_ecc_step_info *stepinfo;
4806 int step_size, strength, nsteps, ecc_bytes, corr;
4807 int best_corr = 0;
4808 int best_step = 0;
4809 int best_strength, best_ecc_bytes;
4810 int i, j;
4811
4812 if (WARN_ON(oobavail < 0))
4813 return -EINVAL;
4814
4815 for (i = 0; i < caps->nstepinfos; i++) {
4816 stepinfo = &caps->stepinfos[i];
4817 step_size = stepinfo->stepsize;
4818
4819 /* If chip->ecc.size is already set, respect it */
4820 if (chip->ecc.size && step_size != chip->ecc.size)
4821 continue;
4822
4823 for (j = 0; j < stepinfo->nstrengths; j++) {
4824 strength = stepinfo->strengths[j];
4825
4826 if (mtd->writesize % step_size)
4827 continue;
4828
4829 nsteps = mtd->writesize / step_size;
4830
4831 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4832 if (WARN_ON_ONCE(ecc_bytes < 0))
4833 continue;
4834
4835 if (ecc_bytes * nsteps > oobavail)
4836 continue;
4837
4838 corr = strength * nsteps;
4839
4840 /*
4841 * If the number of correctable bits is the same,
4842 * bigger step_size has more reliability.
4843 */
4844 if (corr > best_corr ||
4845 (corr == best_corr && step_size > best_step)) {
4846 best_corr = corr;
4847 best_step = step_size;
4848 best_strength = strength;
4849 best_ecc_bytes = ecc_bytes;
4850 }
4851 }
4852 }
4853
4854 if (!best_corr)
4855 return -ENOTSUPP;
4856
4857 chip->ecc.size = best_step;
4858 chip->ecc.strength = best_strength;
4859 chip->ecc.bytes = best_ecc_bytes;
4860
4861 return 0;
4862}
4863EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4864
Scott Wood3ea94ed2015-06-26 19:03:26 -05004865/*
4866 * Check if the chip configuration meet the datasheet requirements.
4867
4868 * If our configuration corrects A bits per B bytes and the minimum
4869 * required correction level is X bits per Y bytes, then we must ensure
4870 * both of the following are true:
4871 *
4872 * (1) A / B >= X / Y
4873 * (2) A >= X
4874 *
4875 * Requirement (1) ensures we can correct for the required bitflip density.
4876 * Requirement (2) ensures we can correct even when all bitflips are clumped
4877 * in the same sector.
4878 */
4879static bool nand_ecc_strength_good(struct mtd_info *mtd)
4880{
Scott Wood17fed142016-05-30 13:57:56 -05004881 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004882 struct nand_ecc_ctrl *ecc = &chip->ecc;
4883 int corr, ds_corr;
4884
4885 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4886 /* Not enough information */
4887 return true;
4888
4889 /*
4890 * We get the number of corrected bits per page to compare
4891 * the correction density.
4892 */
4893 corr = (mtd->writesize * ecc->strength) / ecc->size;
4894 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4895
4896 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4897}
William Juul52c07962007-10-31 13:53:06 +01004898
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004899static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4900{
4901 struct nand_ecc_ctrl *ecc = &chip->ecc;
4902
4903 if (nand_standard_page_accessors(ecc))
4904 return false;
4905
4906 /*
4907 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4908 * controller driver implements all the page accessors because
4909 * default helpers are not suitable when the core does not
4910 * send the READ0/PAGEPROG commands.
4911 */
4912 return (!ecc->read_page || !ecc->write_page ||
4913 !ecc->read_page_raw || !ecc->write_page_raw ||
4914 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4915 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4916 ecc->hwctl && ecc->calculate));
4917}
4918
William Juul52c07962007-10-31 13:53:06 +01004919/**
4920 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004921 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004922 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004923 * This is the second phase of the normal nand_scan() function. It fills out
4924 * all the uninitialized function pointers with the defaults and scans for a
4925 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004926 */
4927int nand_scan_tail(struct mtd_info *mtd)
4928{
4929 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004930 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004931 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004932 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004933
Sergey Lapin3a38a552013-01-14 03:46:50 +00004934 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4935 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4936 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4937
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004938 if (invalid_ecc_page_accessors(chip)) {
4939 pr_err("Invalid ECC page accessors setup\n");
4940 return -EINVAL;
4941 }
4942
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004943 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004944 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004945 chip->buffers = nbuf;
4946 } else {
4947 if (!chip->buffers)
4948 return -ENOMEM;
4949 }
William Juul52c07962007-10-31 13:53:06 +01004950
4951 /* Set the internal oob buffer location, just after the page data */
4952 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4953
4954 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004955 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004956 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004957 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004958 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02004959#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004960 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004961 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004962 break;
4963 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004964 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004965 break;
4966 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004967 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004968 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004969 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004970 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004971 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01004972#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004973 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004974 pr_warn("No oob scheme defined for oobsize %d\n",
4975 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004976 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004977 }
4978 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004979
William Juul52c07962007-10-31 13:53:06 +01004980 if (!chip->write_page)
4981 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004982
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004983 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004984 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01004985 * selected and we have 256 byte pagesize fallback to software ECC
4986 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004987
Heiko Schocherf5895d12014-06-24 10:10:04 +02004988 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004989 case NAND_ECC_HW_OOB_FIRST:
4990 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004991 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004992 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004993 BUG();
4994 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004995 if (!ecc->read_page)
4996 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004997
William Juul52c07962007-10-31 13:53:06 +01004998 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004999 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005000 if (!ecc->read_page)
5001 ecc->read_page = nand_read_page_hwecc;
5002 if (!ecc->write_page)
5003 ecc->write_page = nand_write_page_hwecc;
5004 if (!ecc->read_page_raw)
5005 ecc->read_page_raw = nand_read_page_raw;
5006 if (!ecc->write_page_raw)
5007 ecc->write_page_raw = nand_write_page_raw;
5008 if (!ecc->read_oob)
5009 ecc->read_oob = nand_read_oob_std;
5010 if (!ecc->write_oob)
5011 ecc->write_oob = nand_write_oob_std;
5012 if (!ecc->read_subpage)
5013 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05005014 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02005015 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005016
William Juul52c07962007-10-31 13:53:06 +01005017 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005018 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5019 (!ecc->read_page ||
5020 ecc->read_page == nand_read_page_hwecc ||
5021 !ecc->write_page ||
5022 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005023 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005024 BUG();
5025 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005026 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005027 if (!ecc->read_page)
5028 ecc->read_page = nand_read_page_syndrome;
5029 if (!ecc->write_page)
5030 ecc->write_page = nand_write_page_syndrome;
5031 if (!ecc->read_page_raw)
5032 ecc->read_page_raw = nand_read_page_raw_syndrome;
5033 if (!ecc->write_page_raw)
5034 ecc->write_page_raw = nand_write_page_raw_syndrome;
5035 if (!ecc->read_oob)
5036 ecc->read_oob = nand_read_oob_syndrome;
5037 if (!ecc->write_oob)
5038 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005039
Heiko Schocherf5895d12014-06-24 10:10:04 +02005040 if (mtd->writesize >= ecc->size) {
5041 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005042 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5043 BUG();
5044 }
William Juul52c07962007-10-31 13:53:06 +01005045 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005046 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005047 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5048 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005049 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005050
William Juul52c07962007-10-31 13:53:06 +01005051 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005052 ecc->calculate = nand_calculate_ecc;
5053 ecc->correct = nand_correct_data;
5054 ecc->read_page = nand_read_page_swecc;
5055 ecc->read_subpage = nand_read_subpage;
5056 ecc->write_page = nand_write_page_swecc;
5057 ecc->read_page_raw = nand_read_page_raw;
5058 ecc->write_page_raw = nand_write_page_raw;
5059 ecc->read_oob = nand_read_oob_std;
5060 ecc->write_oob = nand_write_oob_std;
5061 if (!ecc->size)
5062 ecc->size = 256;
5063 ecc->bytes = 3;
5064 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005065 break;
5066
Christian Hitz55f7bca2011-10-12 09:31:59 +02005067 case NAND_ECC_SOFT_BCH:
5068 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005069 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005070 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005071 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005072 ecc->calculate = nand_bch_calculate_ecc;
5073 ecc->correct = nand_bch_correct_data;
5074 ecc->read_page = nand_read_page_swecc;
5075 ecc->read_subpage = nand_read_subpage;
5076 ecc->write_page = nand_write_page_swecc;
5077 ecc->read_page_raw = nand_read_page_raw;
5078 ecc->write_page_raw = nand_write_page_raw;
5079 ecc->read_oob = nand_read_oob_std;
5080 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005081 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005082 * Board driver should supply ecc.size and ecc.strength values
5083 * to select how many bits are correctable. Otherwise, default
5084 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005085 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005086 if (!ecc->size && (mtd->oobsize >= 64)) {
5087 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005088 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005089 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005090
5091 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005092 ecc->bytes = 0;
5093 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005094 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005095 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005096 BUG();
5097 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005098 break;
5099
William Juul52c07962007-10-31 13:53:06 +01005100 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005101 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005102 ecc->read_page = nand_read_page_raw;
5103 ecc->write_page = nand_write_page_raw;
5104 ecc->read_oob = nand_read_oob_std;
5105 ecc->read_page_raw = nand_read_page_raw;
5106 ecc->write_page_raw = nand_write_page_raw;
5107 ecc->write_oob = nand_write_oob_std;
5108 ecc->size = mtd->writesize;
5109 ecc->bytes = 0;
5110 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005111 break;
5112
5113 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005114 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005115 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005116 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005117
Sergey Lapin3a38a552013-01-14 03:46:50 +00005118 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005119 if (!ecc->read_oob_raw)
5120 ecc->read_oob_raw = ecc->read_oob;
5121 if (!ecc->write_oob_raw)
5122 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005123
William Juul52c07962007-10-31 13:53:06 +01005124 /*
5125 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005126 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005127 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005128 mtd->oobavail = 0;
5129 if (ecc->layout) {
5130 for (i = 0; ecc->layout->oobfree[i].length; i++)
5131 mtd->oobavail += ecc->layout->oobfree[i].length;
5132 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005133
Scott Wood3ea94ed2015-06-26 19:03:26 -05005134 /* ECC sanity check: warn if it's too weak */
5135 if (!nand_ecc_strength_good(mtd))
5136 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5137 mtd->name);
5138
William Juul52c07962007-10-31 13:53:06 +01005139 /*
5140 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005141 * mode.
William Juul52c07962007-10-31 13:53:06 +01005142 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005143 ecc->steps = mtd->writesize / ecc->size;
5144 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005145 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005146 BUG();
5147 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005148 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005149
Sergey Lapin3a38a552013-01-14 03:46:50 +00005150 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005151 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5152 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005153 case 2:
5154 mtd->subpage_sft = 1;
5155 break;
5156 case 4:
5157 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005158 case 16:
William Juul52c07962007-10-31 13:53:06 +01005159 mtd->subpage_sft = 2;
5160 break;
5161 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005162 }
William Juul52c07962007-10-31 13:53:06 +01005163 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005164
William Juul52c07962007-10-31 13:53:06 +01005165 /* Initialize state */
5166 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005167
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005168 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005169 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005170
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005171 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005172 switch (ecc->mode) {
5173 case NAND_ECC_SOFT:
5174 case NAND_ECC_SOFT_BCH:
5175 if (chip->page_shift > 9)
5176 chip->options |= NAND_SUBPAGE_READ;
5177 break;
5178
5179 default:
5180 break;
5181 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005182
Patrice Chotardbee32872022-03-21 09:13:36 +01005183 mtd->flash_node = chip->flash_node;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005184 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005185 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005186 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5187 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005188 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005189 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005190 mtd->_read_oob = nand_read_oob;
5191 mtd->_write_oob = nand_write_oob;
5192 mtd->_sync = nand_sync;
5193 mtd->_lock = NULL;
5194 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005195 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005196 mtd->_block_isbad = nand_block_isbad;
5197 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005198 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005199
Sergey Lapin3a38a552013-01-14 03:46:50 +00005200 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005201 mtd->ecclayout = ecc->layout;
5202 mtd->ecc_strength = ecc->strength;
5203 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005204 /*
5205 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5206 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5207 * properly set.
5208 */
5209 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005210 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005211
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005212 return 0;
William Juul52c07962007-10-31 13:53:06 +01005213}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005214EXPORT_SYMBOL(nand_scan_tail);
5215
William Juul52c07962007-10-31 13:53:06 +01005216/**
5217 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005218 * @mtd: MTD device structure
5219 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005220 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005221 * This fills out all the uninitialized function pointers with the defaults.
5222 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005223 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005224 */
5225int nand_scan(struct mtd_info *mtd, int maxchips)
5226{
5227 int ret;
5228
Lei Wen75bde942011-01-06 09:48:18 +08005229 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005230 if (!ret)
5231 ret = nand_scan_tail(mtd);
5232 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005233}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005234EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005235
Heiko Schocherf5895d12014-06-24 10:10:04 +02005236MODULE_LICENSE("GPL");
5237MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5238MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5239MODULE_DESCRIPTION("Generic NAND flash driver code");