blob: 6b4adcf6bdc92016236a14e39ea47d474eb606df [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02006 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -05007 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020011 *
William Juul52c07962007-10-31 13:53:06 +010012 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020013 * David Woodhouse for adding multichip support
14 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
William Juul52c07962007-10-31 13:53:06 +010018 * TODO:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapin3a38a552013-01-14 03:46:50 +000021 * if we have HW ECC support.
Scott Wood3628f002008-10-24 16:20:43 -050022 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020023 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Heiko Schocherf5895d12014-06-24 10:10:04 +020030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060032#include <log.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020033#include <malloc.h>
34#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070035#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060036#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060037#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060038#include <linux/delay.h>
William Juul52c07962007-10-31 13:53:06 +010039#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000040#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020041#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090042#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020043#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020044#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020045#ifdef CONFIG_MTD_PARTITIONS
46#include <linux/mtd/partitions.h>
47#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020048#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090049#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020050
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051/* Define default oob placement schemes for large and small page devices */
Gregory CLEMENTe5b96312019-04-17 11:22:05 +020052#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
William Juul52c07962007-10-31 13:53:06 +010053static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020054 .eccbytes = 3,
55 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010056 .oobfree = {
57 {.offset = 3,
58 .length = 2},
59 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020060 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020061};
62
William Juul52c07962007-10-31 13:53:06 +010063static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020064 .eccbytes = 6,
65 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010066 .oobfree = {
67 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020068 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020069};
70
William Juul52c07962007-10-31 13:53:06 +010071static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020072 .eccbytes = 24,
73 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010074 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
77 .oobfree = {
78 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020079 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020080};
81
William Juul52c07962007-10-31 13:53:06 +010082static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020083 .eccbytes = 48,
84 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020085 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010088 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
91 .oobfree = {
92 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020093 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094};
Stefan Agnerbd186142018-12-06 14:57:09 +010095#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020096
Heiko Schocherf5895d12014-06-24 10:10:04 +020097static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010098
99static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
100 struct mtd_oob_ops *ops);
101
Heiko Schocherf5895d12014-06-24 10:10:04 +0200102/*
103 * For devices which display every fart in the system on a separate LED. Is
104 * compiled away when LED support is disabled.
105 */
106DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200107
Christian Hitzb8a6b372011-10-12 09:32:02 +0200108static int check_offs_len(struct mtd_info *mtd,
109 loff_t ofs, uint64_t len)
110{
Scott Wood17fed142016-05-30 13:57:56 -0500111 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200112 int ret = 0;
113
114 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200115 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
116 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200117 ret = -EINVAL;
118 }
119
120 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200121 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
122 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200123 ret = -EINVAL;
124 }
125
Christian Hitzb8a6b372011-10-12 09:32:02 +0200126 return ret;
127}
128
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200129/**
130 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000131 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200132 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200133 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200134 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200135static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100136{
Scott Wood17fed142016-05-30 13:57:56 -0500137 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200138
139 /* De-select the NAND device */
140 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100141}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200142
143/**
144 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000145 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200147 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200148 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000149uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200150{
Scott Wood17fed142016-05-30 13:57:56 -0500151 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100152 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200153}
154
155/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200156 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000157 * @mtd: MTD device structure
158 *
159 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200161 */
William Juul52c07962007-10-31 13:53:06 +0100162static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200163{
Scott Wood17fed142016-05-30 13:57:56 -0500164 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100165 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200166}
167
168/**
169 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000170 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200171 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000172 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200173 */
174static u16 nand_read_word(struct mtd_info *mtd)
175{
Scott Wood17fed142016-05-30 13:57:56 -0500176 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100177 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200178}
179
180/**
181 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000182 * @mtd: MTD device structure
183 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200184 *
185 * Default select function for 1 chip devices.
186 */
William Juul52c07962007-10-31 13:53:06 +0100187static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200188{
Scott Wood17fed142016-05-30 13:57:56 -0500189 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100190
191 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192 case -1:
William Juul52c07962007-10-31 13:53:06 +0100193 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200194 break;
195 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200196 break;
197
198 default:
199 BUG();
200 }
201}
202
203/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200204 * nand_write_byte - [DEFAULT] write single byte to chip
205 * @mtd: MTD device structure
206 * @byte: value to write
207 *
208 * Default function to write a byte to I/O[7:0]
209 */
210static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
211{
Scott Wood17fed142016-05-30 13:57:56 -0500212 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200213
214 chip->write_buf(mtd, &byte, 1);
215}
216
217/**
218 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
219 * @mtd: MTD device structure
220 * @byte: value to write
221 *
222 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
223 */
224static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
225{
Scott Wood17fed142016-05-30 13:57:56 -0500226 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200227 uint16_t word = byte;
228
229 /*
230 * It's not entirely clear what should happen to I/O[15:8] when writing
231 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
232 *
233 * When the host supports a 16-bit bus width, only data is
234 * transferred at the 16-bit width. All address and command line
235 * transfers shall use only the lower 8-bits of the data bus. During
236 * command transfers, the host may place any value on the upper
237 * 8-bits of the data bus. During address transfers, the host shall
238 * set the upper 8-bits of the data bus to 00h.
239 *
240 * One user of the write_byte callback is nand_onfi_set_features. The
241 * four parameters are specified to be written to I/O[7:0], but this is
242 * neither an address nor a command transfer. Let's assume a 0 on the
243 * upper I/O lines is OK.
244 */
245 chip->write_buf(mtd, (uint8_t *)&word, 2);
246}
247
Heiko Schocherf5895d12014-06-24 10:10:04 +0200248static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
249{
250 int i;
251
252 for (i = 0; i < len; i++)
253 writeb(buf[i], addr);
254}
255static void ioread8_rep(void *addr, uint8_t *buf, int len)
256{
257 int i;
258
259 for (i = 0; i < len; i++)
260 buf[i] = readb(addr);
261}
262
263static void ioread16_rep(void *addr, void *buf, int len)
264{
265 int i;
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200266 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200267
Heiko Schocherf5895d12014-06-24 10:10:04 +0200268 for (i = 0; i < len; i++)
269 p[i] = readw(addr);
270}
271
272static void iowrite16_rep(void *addr, void *buf, int len)
273{
274 int i;
275 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200276
277 for (i = 0; i < len; i++)
278 writew(p[i], addr);
279}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200280
281/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200282 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000283 * @mtd: MTD device structure
284 * @buf: data buffer
285 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000287 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000289void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200290{
Scott Wood17fed142016-05-30 13:57:56 -0500291 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200292
Heiko Schocherf5895d12014-06-24 10:10:04 +0200293 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200294}
295
296/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200297 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000298 * @mtd: MTD device structure
299 * @buf: buffer to store date
300 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200301 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000302 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200303 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400304void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305{
Scott Wood17fed142016-05-30 13:57:56 -0500306 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307
Heiko Schocherf5895d12014-06-24 10:10:04 +0200308 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200309}
310
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200311/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200312 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000313 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200314 * @buf: data buffer
315 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200317 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200318 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200319void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200320{
Scott Wood17fed142016-05-30 13:57:56 -0500321 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200322 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200323
Heiko Schocherf5895d12014-06-24 10:10:04 +0200324 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200325}
326
327/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200328 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000329 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200330 * @buf: buffer to store date
331 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200333 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200335void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200336{
Scott Wood17fed142016-05-30 13:57:56 -0500337 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200338 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200339
Heiko Schocherf5895d12014-06-24 10:10:04 +0200340 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200341}
342
343/**
344 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000345 * @mtd: MTD device structure
346 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200347 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200348 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200349 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500350static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500352 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500353 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200354 u16 bad;
355
Sergey Lapin3a38a552013-01-14 03:46:50 +0000356 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200357 ofs += mtd->erasesize - mtd->writesize;
358
William Juul52c07962007-10-31 13:53:06 +0100359 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200360
Sergey Lapin3a38a552013-01-14 03:46:50 +0000361 do {
362 if (chip->options & NAND_BUSWIDTH_16) {
363 chip->cmdfunc(mtd, NAND_CMD_READOOB,
364 chip->badblockpos & 0xFE, page);
365 bad = cpu_to_le16(chip->read_word(mtd));
366 if (chip->badblockpos & 0x1)
367 bad >>= 8;
368 else
369 bad &= 0xFF;
370 } else {
371 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
372 page);
373 bad = chip->read_byte(mtd);
374 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200375
Sergey Lapin3a38a552013-01-14 03:46:50 +0000376 if (likely(chip->badblockbits == 8))
377 res = bad != 0xFF;
378 else
379 res = hweight8(bad) < chip->badblockbits;
380 ofs += mtd->writesize;
381 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
382 i++;
383 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200384
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200385 return res;
386}
387
388/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200389 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000390 * @mtd: MTD device structure
391 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200392 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000393 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200394 * specific driver. It provides the details for writing a bad block marker to a
395 * block.
396 */
397static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398{
Scott Wood17fed142016-05-30 13:57:56 -0500399 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200400 struct mtd_oob_ops ops;
401 uint8_t buf[2] = { 0, 0 };
402 int ret = 0, res, i = 0;
403
Scott Wood3ea94ed2015-06-26 19:03:26 -0500404 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200405 ops.oobbuf = buf;
406 ops.ooboffs = chip->badblockpos;
407 if (chip->options & NAND_BUSWIDTH_16) {
408 ops.ooboffs &= ~0x01;
409 ops.len = ops.ooblen = 2;
410 } else {
411 ops.len = ops.ooblen = 1;
412 }
413 ops.mode = MTD_OPS_PLACE_OOB;
414
415 /* Write to first/last page(s) if necessary */
416 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
417 ofs += mtd->erasesize - mtd->writesize;
418 do {
419 res = nand_do_write_oob(mtd, ofs, &ops);
420 if (!ret)
421 ret = res;
422
423 i++;
424 ofs += mtd->writesize;
425 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
426
427 return ret;
428}
429
430/**
431 * nand_block_markbad_lowlevel - mark a block bad
432 * @mtd: MTD device structure
433 * @ofs: offset from device start
434 *
435 * This function performs the generic NAND bad block marking steps (i.e., bad
436 * block table(s) and/or marker(s)). We only allow the hardware driver to
437 * specify how to write bad block markers to OOB (chip->block_markbad).
438 *
439 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000440 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200441 * (2) write bad block marker to OOB area of affected block (unless flag
442 * NAND_BBT_NO_OOB_BBM is present)
443 * (3) update the BBT
444 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000445 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200446*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200447static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200448{
Scott Wood17fed142016-05-30 13:57:56 -0500449 struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadrosb590f612022-12-20 12:21:57 +0200450 int ret = 0;
451#ifndef CONFIG_SPL_BUILD
452 int res;
453#endif
Christian Hitzb8a6b372011-10-12 09:32:02 +0200454
Heiko Schocherf5895d12014-06-24 10:10:04 +0200455 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000456 struct erase_info einfo;
457
458 /* Attempt erase before marking OOB */
459 memset(&einfo, 0, sizeof(einfo));
460 einfo.mtd = mtd;
461 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200462 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000463 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200464
Heiko Schocherf5895d12014-06-24 10:10:04 +0200465 /* Write bad block marker to OOB */
466 nand_get_device(mtd, FL_WRITING);
467 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500468 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100469 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000470
Roger Quadrosb590f612022-12-20 12:21:57 +0200471#ifndef CONFIG_SPL_BUILD
Heiko Schocherf5895d12014-06-24 10:10:04 +0200472 /* Mark block bad in BBT */
473 if (chip->bbt) {
474 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000475 if (!ret)
476 ret = res;
477 }
Roger Quadrosb590f612022-12-20 12:21:57 +0200478#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +0000479
William Juul52c07962007-10-31 13:53:06 +0100480 if (!ret)
481 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500482
William Juul52c07962007-10-31 13:53:06 +0100483 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200484}
485
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200486/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200487 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000488 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200489 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000490 * Check, if the device is write protected. The function expects, that the
491 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200492 */
William Juul52c07962007-10-31 13:53:06 +0100493static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200494{
Scott Wood17fed142016-05-30 13:57:56 -0500495 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100496 u8 status;
497 int ret;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200498
Sergey Lapin3a38a552013-01-14 03:46:50 +0000499 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200500 if (chip->options & NAND_BROKEN_XD)
501 return 0;
502
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200503 /* Check the WP bit */
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100504 ret = nand_status_op(chip, &status);
505 if (ret)
506 return ret;
507
508 return status & NAND_STATUS_WP ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200509}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100510
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200511/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500512 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000513 * @mtd: MTD device structure
514 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300515 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500516 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300517 */
518static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
519{
Scott Wood17fed142016-05-30 13:57:56 -0500520 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300521
522 if (!chip->bbt)
523 return 0;
524 /* Return info from the table */
Roger Quadrosb590f612022-12-20 12:21:57 +0200525#ifndef CONFIG_SPL_BUILD
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300526 return nand_isreserved_bbt(mtd, ofs);
Roger Quadrosb590f612022-12-20 12:21:57 +0200527#else
528 return 0;
529#endif
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300530}
531
532/**
533 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
534 * @mtd: MTD device structure
535 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000536 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200537 *
538 * Check, if the block is bad. Either by reading the bad block table or
539 * calling of the scan function.
540 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500541static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200542{
Scott Wood17fed142016-05-30 13:57:56 -0500543 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200544
Masahiro Yamada8d100542014-12-26 22:20:58 +0900545 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
546 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200547 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900548 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200549 }
550
William Juul52c07962007-10-31 13:53:06 +0100551 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500552 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200553
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200554 /* Return info from the table */
Roger Quadrosb590f612022-12-20 12:21:57 +0200555#ifndef CONFIG_SPL_BUILD
William Juul52c07962007-10-31 13:53:06 +0100556 return nand_isbad_bbt(mtd, ofs, allowbbt);
Roger Quadrosb590f612022-12-20 12:21:57 +0200557#else
558 return 0;
559#endif
William Juul52c07962007-10-31 13:53:06 +0100560}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200561
Scott Wood52ab7ce2016-05-30 13:57:58 -0500562/**
563 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
564 * @mtd: MTD device structure
565 *
566 * Wait for the ready pin after a command, and warn if a timeout occurs.
567 */
William Juul52c07962007-10-31 13:53:06 +0100568void nand_wait_ready(struct mtd_info *mtd)
569{
Scott Wood17fed142016-05-30 13:57:56 -0500570 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500571 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000572 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100573
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000574 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000575 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000576 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100577 if (chip->dev_ready)
578 if (chip->dev_ready(mtd))
579 break;
580 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500581
582 if (!chip->dev_ready(mtd))
583 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200584}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200585EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200586
587/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500588 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
589 * @mtd: MTD device structure
590 * @timeo: Timeout in ms
591 *
592 * Wait for status ready (i.e. command done) or timeout.
593 */
594static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
595{
Scott Wood17fed142016-05-30 13:57:56 -0500596 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500597 u32 time_start;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100598 int ret;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500599
600 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
601 time_start = get_timer(0);
602 while (get_timer(time_start) < timeo) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100603 u8 status;
604
605 ret = nand_read_data_op(chip, &status, sizeof(status), true);
606 if (ret)
607 return;
608
609 if (status & NAND_STATUS_READY)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500610 break;
Stefan Roese80877fa2022-09-02 14:10:46 +0200611 schedule();
Scott Wood3ea94ed2015-06-26 19:03:26 -0500612 }
613};
614
615/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200616 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000617 * @mtd: MTD device structure
618 * @command: the command to be sent
619 * @column: the column address for this command, -1 if none
620 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200621 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000622 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200623 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200624 */
William Juul52c07962007-10-31 13:53:06 +0100625static void nand_command(struct mtd_info *mtd, unsigned int command,
626 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200627{
Scott Wood17fed142016-05-30 13:57:56 -0500628 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100629 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200630
Sergey Lapin3a38a552013-01-14 03:46:50 +0000631 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200632 if (command == NAND_CMD_SEQIN) {
633 int readcmd;
634
William Juul52c07962007-10-31 13:53:06 +0100635 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200636 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100637 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200638 readcmd = NAND_CMD_READOOB;
639 } else if (column < 256) {
640 /* First 256 bytes --> READ0 */
641 readcmd = NAND_CMD_READ0;
642 } else {
643 column -= 256;
644 readcmd = NAND_CMD_READ1;
645 }
William Juul52c07962007-10-31 13:53:06 +0100646 chip->cmd_ctrl(mtd, readcmd, ctrl);
647 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200648 }
William Juul52c07962007-10-31 13:53:06 +0100649 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200650
Sergey Lapin3a38a552013-01-14 03:46:50 +0000651 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100652 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
653 /* Serially input address */
654 if (column != -1) {
655 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200656 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530657 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100658 column >>= 1;
659 chip->cmd_ctrl(mtd, column, ctrl);
660 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200661 }
William Juul52c07962007-10-31 13:53:06 +0100662 if (page_addr != -1) {
663 chip->cmd_ctrl(mtd, page_addr, ctrl);
664 ctrl &= ~NAND_CTRL_CHANGE;
665 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900666 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100667 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
668 }
669 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200670
671 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000672 * Program and erase have their own busy handlers status and sequential
673 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100674 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200675 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200676
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200677 case NAND_CMD_PAGEPROG:
678 case NAND_CMD_ERASE1:
679 case NAND_CMD_ERASE2:
680 case NAND_CMD_SEQIN:
681 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900682 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900683 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200684 return;
685
686 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100687 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200688 break;
William Juul52c07962007-10-31 13:53:06 +0100689 udelay(chip->chip_delay);
690 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
691 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
692 chip->cmd_ctrl(mtd,
693 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500694 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
695 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200696 return;
697
William Juul52c07962007-10-31 13:53:06 +0100698 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200699 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200700 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200701 * If we don't have access to the busy pin, we apply the given
702 * command delay
William Juul52c07962007-10-31 13:53:06 +0100703 */
704 if (!chip->dev_ready) {
705 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200706 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200707 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200708 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000709 /*
710 * Apply this short delay always to ensure that we do wait tWB in
711 * any case on any machine.
712 */
William Juul52c07962007-10-31 13:53:06 +0100713 ndelay(100);
714
715 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200716}
717
718/**
719 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000720 * @mtd: MTD device structure
721 * @command: the command to be sent
722 * @column: the column address for this command, -1 if none
723 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 *
William Juul52c07962007-10-31 13:53:06 +0100725 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000726 * devices. We don't have the separate regions as we have in the small page
727 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200728 */
William Juul52c07962007-10-31 13:53:06 +0100729static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
730 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200731{
Scott Wood17fed142016-05-30 13:57:56 -0500732 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200733
734 /* Emulate NAND_CMD_READOOB */
735 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100736 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200737 command = NAND_CMD_READ0;
738 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200739
William Juul52c07962007-10-31 13:53:06 +0100740 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200741 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200742
743 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100744 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200745
746 /* Serially input address */
747 if (column != -1) {
748 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200749 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530750 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200751 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100752 chip->cmd_ctrl(mtd, column, ctrl);
753 ctrl &= ~NAND_CTRL_CHANGE;
754 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200755 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200756 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100757 chip->cmd_ctrl(mtd, page_addr, ctrl);
758 chip->cmd_ctrl(mtd, page_addr >> 8,
759 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900760 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100761 chip->cmd_ctrl(mtd, page_addr >> 16,
762 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200763 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200764 }
William Juul52c07962007-10-31 13:53:06 +0100765 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200766
767 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000768 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500769 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100770 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200771 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200772
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200773 case NAND_CMD_CACHEDPROG:
774 case NAND_CMD_PAGEPROG:
775 case NAND_CMD_ERASE1:
776 case NAND_CMD_ERASE2:
777 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100778 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200779 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900780 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900781 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200782 return;
783
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200784 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100785 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200786 break;
William Juul52c07962007-10-31 13:53:06 +0100787 udelay(chip->chip_delay);
788 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
789 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
790 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
791 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500792 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
793 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100794 return;
795
796 case NAND_CMD_RNDOUT:
797 /* No ready / busy check necessary */
798 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
799 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
800 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
801 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200802 return;
803
804 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100805 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
806 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
807 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
808 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200809
William Juul52c07962007-10-31 13:53:06 +0100810 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200811 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200812 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200813 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000814 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100815 */
816 if (!chip->dev_ready) {
817 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200818 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200819 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200820 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200821
Sergey Lapin3a38a552013-01-14 03:46:50 +0000822 /*
823 * Apply this short delay always to ensure that we do wait tWB in
824 * any case on any machine.
825 */
William Juul52c07962007-10-31 13:53:06 +0100826 ndelay(100);
827
828 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200829}
830
831/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200832 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000833 * @chip: the nand chip descriptor
834 * @mtd: MTD device structure
835 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200836 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200837 * Used when in panic, no locks are taken.
838 */
839static void panic_nand_get_device(struct nand_chip *chip,
840 struct mtd_info *mtd, int new_state)
841{
842 /* Hardware controller shared among independent devices */
843 chip->controller->active = chip;
844 chip->state = new_state;
845}
846
847/**
848 * nand_get_device - [GENERIC] Get chip for selected access
849 * @mtd: MTD device structure
850 * @new_state: the state which is requested
851 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200852 * Get the device and lock it for exclusive access
853 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200854static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200855nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100856{
Scott Wood17fed142016-05-30 13:57:56 -0500857 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200858 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100859 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200860}
861
862/**
863 * panic_nand_wait - [GENERIC] wait until the command is done
864 * @mtd: MTD device structure
865 * @chip: NAND chip structure
866 * @timeo: timeout
867 *
868 * Wait for command done. This is a helper function for nand_wait used when
869 * we are in interrupt context. May happen when in panic and trying to write
870 * an oops through mtdoops.
871 */
872static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
873 unsigned long timeo)
874{
875 int i;
876 for (i = 0; i < timeo; i++) {
877 if (chip->dev_ready) {
878 if (chip->dev_ready(mtd))
879 break;
880 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100881 int ret;
882 u8 status;
883
884 ret = nand_read_data_op(chip, &status, sizeof(status),
885 true);
886 if (ret)
887 return;
888
889 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200890 break;
891 }
892 mdelay(1);
893 }
William Juul52c07962007-10-31 13:53:06 +0100894}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200895
896/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000897 * nand_wait - [DEFAULT] wait until the command is done
898 * @mtd: MTD device structure
899 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200900 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500901 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100902 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200903static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200904{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500905 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100906 u8 status;
907 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100908
Heiko Schocherf5895d12014-06-24 10:10:04 +0200909 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100910
Heiko Schocherf5895d12014-06-24 10:10:04 +0200911 /*
912 * Apply this short delay always to ensure that we do wait tWB in any
913 * case on any machine.
914 */
915 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100916
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100917 ret = nand_status_op(chip, NULL);
918 if (ret)
919 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100920
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200921 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
922 u32 time_start;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200923
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200924 time_start = get_timer(0);
925 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200926 if (chip->dev_ready) {
927 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100928 break;
929 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100930 ret = nand_read_data_op(chip, &status,
931 sizeof(status), true);
932 if (ret)
933 return ret;
934
935 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100936 break;
937 }
938 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200939 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100940
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100941 ret = nand_read_data_op(chip, &status, sizeof(status), true);
942 if (ret)
943 return ret;
944
Heiko Schocherf5895d12014-06-24 10:10:04 +0200945 /* This can happen if in case of timeout or buggy dev_ready */
946 WARN_ON(!(status & NAND_STATUS_READY));
947 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200948}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500949
Scott Wood52ab7ce2016-05-30 13:57:58 -0500950/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900951 * nand_reset_data_interface - Reset data interface and timings
952 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900953 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900954 *
955 * Reset the Data interface and timings to ONFI mode 0.
956 *
957 * Returns 0 for success or negative error code otherwise.
958 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900959static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900960{
961 struct mtd_info *mtd = nand_to_mtd(chip);
962 const struct nand_data_interface *conf;
963 int ret;
964
965 if (!chip->setup_data_interface)
966 return 0;
967
968 /*
969 * The ONFI specification says:
970 * "
971 * To transition from NV-DDR or NV-DDR2 to the SDR data
972 * interface, the host shall use the Reset (FFh) command
973 * using SDR timing mode 0. A device in any timing mode is
974 * required to recognize Reset (FFh) command issued in SDR
975 * timing mode 0.
976 * "
977 *
978 * Configure the data interface in SDR mode and set the
979 * timings to timing mode 0.
980 */
981
982 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900983 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900984 if (ret)
985 pr_err("Failed to configure data interface to SDR timing mode 0\n");
986
987 return ret;
988}
989
Kory Maincent0cda0cb2022-06-22 11:11:45 +0200990static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip)
991{
992 if (!chip->onfi_version ||
993 !(le16_to_cpu(chip->onfi_params.opt_cmd)
994 & ONFI_OPT_CMD_SET_GET_FEATURES))
995 return 0;
996
997 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
998 chip->onfi_timing_mode_default,
999 };
1000
1001 return chip->onfi_set_features(mtd, chip,
1002 ONFI_FEATURE_ADDR_TIMING_MODE,
1003 tmode_param);
1004}
1005
Boris Brezillone509cba2017-11-22 02:38:19 +09001006/**
1007 * nand_setup_data_interface - Setup the best data interface and timings
1008 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +09001009 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +09001010 *
1011 * Find and configure the best data interface and NAND timings supported by
1012 * the chip and the driver.
1013 * First tries to retrieve supported timing modes from ONFI information,
1014 * and if the NAND chip does not support ONFI, relies on the
1015 * ->onfi_timing_mode_default specified in the nand_ids table.
1016 *
1017 * Returns 0 for success or negative error code otherwise.
1018 */
Boris Brezillon32935f42017-11-22 02:38:28 +09001019static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +09001020{
1021 struct mtd_info *mtd = nand_to_mtd(chip);
1022 int ret;
1023
1024 if (!chip->setup_data_interface || !chip->data_interface)
1025 return 0;
1026
1027 /*
1028 * Ensure the timing mode has been changed on the chip side
1029 * before changing timings on the controller side.
1030 */
Kory Maincent0cda0cb2022-06-22 11:11:45 +02001031 ret = nand_onfi_set_timings(mtd, chip);
1032 if (ret)
1033 goto err;
Boris Brezillone509cba2017-11-22 02:38:19 +09001034
Boris Brezillon32935f42017-11-22 02:38:28 +09001035 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001036err:
1037 return ret;
1038}
1039
1040/**
1041 * nand_init_data_interface - find the best data interface and timings
1042 * @chip: The NAND chip
1043 *
1044 * Find the best data interface and NAND timings supported by the chip
1045 * and the driver.
1046 * First tries to retrieve supported timing modes from ONFI information,
1047 * and if the NAND chip does not support ONFI, relies on the
1048 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1049 * function nand_chip->data_interface is initialized with the best timing mode
1050 * available.
1051 *
1052 * Returns 0 for success or negative error code otherwise.
1053 */
1054static int nand_init_data_interface(struct nand_chip *chip)
1055{
1056 struct mtd_info *mtd = nand_to_mtd(chip);
1057 int modes, mode, ret;
1058
1059 if (!chip->setup_data_interface)
1060 return 0;
1061
1062 /*
1063 * First try to identify the best timings from ONFI parameters and
1064 * if the NAND does not support ONFI, fallback to the default ONFI
1065 * timing mode.
1066 */
1067 modes = onfi_get_async_timing_mode(chip);
1068 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1069 if (!chip->onfi_timing_mode_default)
1070 return 0;
1071
1072 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1073 }
1074
1075 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1076 GFP_KERNEL);
1077 if (!chip->data_interface)
1078 return -ENOMEM;
1079
1080 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1081 ret = onfi_init_data_interface(chip, chip->data_interface,
1082 NAND_SDR_IFACE, mode);
1083 if (ret)
1084 continue;
1085
Boris Brezillon32935f42017-11-22 02:38:28 +09001086 /* Pass -1 to only */
1087 ret = chip->setup_data_interface(mtd,
1088 NAND_DATA_IFACE_CHECK_ONLY,
1089 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001090 if (!ret) {
1091 chip->onfi_timing_mode_default = mode;
1092 break;
1093 }
1094 }
1095
1096 return 0;
1097}
1098
1099static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1100{
1101 kfree(chip->data_interface);
1102}
1103
1104/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001105 * nand_read_page_op - Do a READ PAGE operation
1106 * @chip: The NAND chip
1107 * @page: page to read
1108 * @offset_in_page: offset within the page
1109 * @buf: buffer used to store the data
1110 * @len: length of the buffer
1111 *
1112 * This function issues a READ PAGE operation.
1113 * This function does not select/unselect the CS line.
1114 *
1115 * Returns 0 on success, a negative error code otherwise.
1116 */
1117int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1118 unsigned int offset_in_page, void *buf, unsigned int len)
1119{
1120 struct mtd_info *mtd = nand_to_mtd(chip);
1121
1122 if (len && !buf)
1123 return -EINVAL;
1124
1125 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1126 return -EINVAL;
1127
1128 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1129 if (len)
1130 chip->read_buf(mtd, buf, len);
1131
1132 return 0;
1133}
1134EXPORT_SYMBOL_GPL(nand_read_page_op);
1135
1136/**
1137 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1138 * @chip: The NAND chip
1139 * @page: parameter page to read
1140 * @buf: buffer used to store the data
1141 * @len: length of the buffer
1142 *
1143 * This function issues a READ PARAMETER PAGE operation.
1144 * This function does not select/unselect the CS line.
1145 *
1146 * Returns 0 on success, a negative error code otherwise.
1147 */
1148static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1149 unsigned int len)
1150{
1151 struct mtd_info *mtd = nand_to_mtd(chip);
1152 unsigned int i;
1153 u8 *p = buf;
1154
1155 if (len && !buf)
1156 return -EINVAL;
1157
1158 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1159 for (i = 0; i < len; i++)
1160 p[i] = chip->read_byte(mtd);
1161
1162 return 0;
1163}
1164
1165/**
1166 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1167 * @chip: The NAND chip
1168 * @offset_in_page: offset within the page
1169 * @buf: buffer used to store the data
1170 * @len: length of the buffer
1171 * @force_8bit: force 8-bit bus access
1172 *
1173 * This function issues a CHANGE READ COLUMN operation.
1174 * This function does not select/unselect the CS line.
1175 *
1176 * Returns 0 on success, a negative error code otherwise.
1177 */
1178int nand_change_read_column_op(struct nand_chip *chip,
1179 unsigned int offset_in_page, void *buf,
1180 unsigned int len, bool force_8bit)
1181{
1182 struct mtd_info *mtd = nand_to_mtd(chip);
1183
1184 if (len && !buf)
1185 return -EINVAL;
1186
1187 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1188 return -EINVAL;
1189
1190 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1191 if (len)
1192 chip->read_buf(mtd, buf, len);
1193
1194 return 0;
1195}
1196EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1197
1198/**
1199 * nand_read_oob_op - Do a READ OOB operation
1200 * @chip: The NAND chip
1201 * @page: page to read
1202 * @offset_in_oob: offset within the OOB area
1203 * @buf: buffer used to store the data
1204 * @len: length of the buffer
1205 *
1206 * This function issues a READ OOB operation.
1207 * This function does not select/unselect the CS line.
1208 *
1209 * Returns 0 on success, a negative error code otherwise.
1210 */
1211int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1212 unsigned int offset_in_oob, void *buf, unsigned int len)
1213{
1214 struct mtd_info *mtd = nand_to_mtd(chip);
1215
1216 if (len && !buf)
1217 return -EINVAL;
1218
1219 if (offset_in_oob + len > mtd->oobsize)
1220 return -EINVAL;
1221
1222 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1223 if (len)
1224 chip->read_buf(mtd, buf, len);
1225
1226 return 0;
1227}
1228EXPORT_SYMBOL_GPL(nand_read_oob_op);
1229
1230/**
1231 * nand_prog_page_begin_op - starts a PROG PAGE operation
1232 * @chip: The NAND chip
1233 * @page: page to write
1234 * @offset_in_page: offset within the page
1235 * @buf: buffer containing the data to write to the page
1236 * @len: length of the buffer
1237 *
1238 * This function issues the first half of a PROG PAGE operation.
1239 * This function does not select/unselect the CS line.
1240 *
1241 * Returns 0 on success, a negative error code otherwise.
1242 */
1243int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1244 unsigned int offset_in_page, const void *buf,
1245 unsigned int len)
1246{
1247 struct mtd_info *mtd = nand_to_mtd(chip);
1248
1249 if (len && !buf)
1250 return -EINVAL;
1251
1252 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1253 return -EINVAL;
1254
1255 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1256
1257 if (buf)
1258 chip->write_buf(mtd, buf, len);
1259
1260 return 0;
1261}
1262EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1263
1264/**
1265 * nand_prog_page_end_op - ends a PROG PAGE operation
1266 * @chip: The NAND chip
1267 *
1268 * This function issues the second half of a PROG PAGE operation.
1269 * This function does not select/unselect the CS line.
1270 *
1271 * Returns 0 on success, a negative error code otherwise.
1272 */
1273int nand_prog_page_end_op(struct nand_chip *chip)
1274{
1275 struct mtd_info *mtd = nand_to_mtd(chip);
1276 int status;
1277
1278 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1279
1280 status = chip->waitfunc(mtd, chip);
1281 if (status & NAND_STATUS_FAIL)
1282 return -EIO;
1283
1284 return 0;
1285}
1286EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1287
1288/**
1289 * nand_prog_page_op - Do a full PROG PAGE operation
1290 * @chip: The NAND chip
1291 * @page: page to write
1292 * @offset_in_page: offset within the page
1293 * @buf: buffer containing the data to write to the page
1294 * @len: length of the buffer
1295 *
1296 * This function issues a full PROG PAGE operation.
1297 * This function does not select/unselect the CS line.
1298 *
1299 * Returns 0 on success, a negative error code otherwise.
1300 */
1301int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1302 unsigned int offset_in_page, const void *buf,
1303 unsigned int len)
1304{
1305 struct mtd_info *mtd = nand_to_mtd(chip);
1306 int status;
1307
1308 if (!len || !buf)
1309 return -EINVAL;
1310
1311 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1312 return -EINVAL;
1313
1314 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1315 chip->write_buf(mtd, buf, len);
1316 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1317
1318 status = chip->waitfunc(mtd, chip);
1319 if (status & NAND_STATUS_FAIL)
1320 return -EIO;
1321
1322 return 0;
1323}
1324EXPORT_SYMBOL_GPL(nand_prog_page_op);
1325
1326/**
1327 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1328 * @chip: The NAND chip
1329 * @offset_in_page: offset within the page
1330 * @buf: buffer containing the data to send to the NAND
1331 * @len: length of the buffer
1332 * @force_8bit: force 8-bit bus access
1333 *
1334 * This function issues a CHANGE WRITE COLUMN operation.
1335 * This function does not select/unselect the CS line.
1336 *
1337 * Returns 0 on success, a negative error code otherwise.
1338 */
1339int nand_change_write_column_op(struct nand_chip *chip,
1340 unsigned int offset_in_page,
1341 const void *buf, unsigned int len,
1342 bool force_8bit)
1343{
1344 struct mtd_info *mtd = nand_to_mtd(chip);
1345
1346 if (len && !buf)
1347 return -EINVAL;
1348
1349 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1350 return -EINVAL;
1351
1352 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1353 if (len)
1354 chip->write_buf(mtd, buf, len);
1355
1356 return 0;
1357}
1358EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1359
1360/**
1361 * nand_readid_op - Do a READID operation
1362 * @chip: The NAND chip
1363 * @addr: address cycle to pass after the READID command
1364 * @buf: buffer used to store the ID
1365 * @len: length of the buffer
1366 *
1367 * This function sends a READID command and reads back the ID returned by the
1368 * NAND.
1369 * This function does not select/unselect the CS line.
1370 *
1371 * Returns 0 on success, a negative error code otherwise.
1372 */
1373int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1374 unsigned int len)
1375{
1376 struct mtd_info *mtd = nand_to_mtd(chip);
1377 unsigned int i;
1378 u8 *id = buf;
1379
1380 if (len && !buf)
1381 return -EINVAL;
1382
1383 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1384
1385 for (i = 0; i < len; i++)
1386 id[i] = chip->read_byte(mtd);
1387
1388 return 0;
1389}
1390EXPORT_SYMBOL_GPL(nand_readid_op);
1391
1392/**
1393 * nand_status_op - Do a STATUS operation
1394 * @chip: The NAND chip
1395 * @status: out variable to store the NAND status
1396 *
1397 * This function sends a STATUS command and reads back the status returned by
1398 * the NAND.
1399 * This function does not select/unselect the CS line.
1400 *
1401 * Returns 0 on success, a negative error code otherwise.
1402 */
1403int nand_status_op(struct nand_chip *chip, u8 *status)
1404{
1405 struct mtd_info *mtd = nand_to_mtd(chip);
1406
1407 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1408 if (status)
1409 *status = chip->read_byte(mtd);
1410
1411 return 0;
1412}
1413EXPORT_SYMBOL_GPL(nand_status_op);
1414
1415/**
1416 * nand_exit_status_op - Exit a STATUS operation
1417 * @chip: The NAND chip
1418 *
1419 * This function sends a READ0 command to cancel the effect of the STATUS
1420 * command to avoid reading only the status until a new read command is sent.
1421 *
1422 * This function does not select/unselect the CS line.
1423 *
1424 * Returns 0 on success, a negative error code otherwise.
1425 */
1426int nand_exit_status_op(struct nand_chip *chip)
1427{
1428 struct mtd_info *mtd = nand_to_mtd(chip);
1429
1430 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1431
1432 return 0;
1433}
1434EXPORT_SYMBOL_GPL(nand_exit_status_op);
1435
1436/**
1437 * nand_erase_op - Do an erase operation
1438 * @chip: The NAND chip
1439 * @eraseblock: block to erase
1440 *
1441 * This function sends an ERASE command and waits for the NAND to be ready
1442 * before returning.
1443 * This function does not select/unselect the CS line.
1444 *
1445 * Returns 0 on success, a negative error code otherwise.
1446 */
1447int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1448{
1449 struct mtd_info *mtd = nand_to_mtd(chip);
1450 unsigned int page = eraseblock <<
1451 (chip->phys_erase_shift - chip->page_shift);
1452 int status;
1453
1454 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1455 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1456
1457 status = chip->waitfunc(mtd, chip);
1458 if (status < 0)
1459 return status;
1460
1461 if (status & NAND_STATUS_FAIL)
1462 return -EIO;
1463
1464 return 0;
1465}
1466EXPORT_SYMBOL_GPL(nand_erase_op);
1467
1468/**
1469 * nand_set_features_op - Do a SET FEATURES operation
1470 * @chip: The NAND chip
1471 * @feature: feature id
1472 * @data: 4 bytes of data
1473 *
1474 * This function sends a SET FEATURES command and waits for the NAND to be
1475 * ready before returning.
1476 * This function does not select/unselect the CS line.
1477 *
1478 * Returns 0 on success, a negative error code otherwise.
1479 */
1480static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1481 const void *data)
1482{
1483 struct mtd_info *mtd = nand_to_mtd(chip);
1484 const u8 *params = data;
1485 int i, status;
1486
1487 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1488 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1489 chip->write_byte(mtd, params[i]);
1490
1491 status = chip->waitfunc(mtd, chip);
1492 if (status & NAND_STATUS_FAIL)
1493 return -EIO;
1494
1495 return 0;
1496}
1497
1498/**
1499 * nand_get_features_op - Do a GET FEATURES operation
1500 * @chip: The NAND chip
1501 * @feature: feature id
1502 * @data: 4 bytes of data
1503 *
1504 * This function sends a GET FEATURES command and waits for the NAND to be
1505 * ready before returning.
1506 * This function does not select/unselect the CS line.
1507 *
1508 * Returns 0 on success, a negative error code otherwise.
1509 */
1510static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1511 void *data)
1512{
1513 struct mtd_info *mtd = nand_to_mtd(chip);
1514 u8 *params = data;
1515 int i;
1516
1517 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1518 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1519 params[i] = chip->read_byte(mtd);
1520
1521 return 0;
1522}
1523
1524/**
1525 * nand_reset_op - Do a reset operation
1526 * @chip: The NAND chip
1527 *
1528 * This function sends a RESET command and waits for the NAND to be ready
1529 * before returning.
1530 * This function does not select/unselect the CS line.
1531 *
1532 * Returns 0 on success, a negative error code otherwise.
1533 */
1534int nand_reset_op(struct nand_chip *chip)
1535{
1536 struct mtd_info *mtd = nand_to_mtd(chip);
1537
1538 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1539
1540 return 0;
1541}
1542EXPORT_SYMBOL_GPL(nand_reset_op);
1543
1544/**
1545 * nand_read_data_op - Read data from the NAND
1546 * @chip: The NAND chip
1547 * @buf: buffer used to store the data
1548 * @len: length of the buffer
1549 * @force_8bit: force 8-bit bus access
1550 *
1551 * This function does a raw data read on the bus. Usually used after launching
1552 * another NAND operation like nand_read_page_op().
1553 * This function does not select/unselect the CS line.
1554 *
1555 * Returns 0 on success, a negative error code otherwise.
1556 */
1557int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1558 bool force_8bit)
1559{
1560 struct mtd_info *mtd = nand_to_mtd(chip);
1561
1562 if (!len || !buf)
1563 return -EINVAL;
1564
1565 if (force_8bit) {
1566 u8 *p = buf;
1567 unsigned int i;
1568
1569 for (i = 0; i < len; i++)
1570 p[i] = chip->read_byte(mtd);
1571 } else {
1572 chip->read_buf(mtd, buf, len);
1573 }
1574
1575 return 0;
1576}
1577EXPORT_SYMBOL_GPL(nand_read_data_op);
1578
1579/**
1580 * nand_write_data_op - Write data from the NAND
1581 * @chip: The NAND chip
1582 * @buf: buffer containing the data to send on the bus
1583 * @len: length of the buffer
1584 * @force_8bit: force 8-bit bus access
1585 *
1586 * This function does a raw data write on the bus. Usually used after launching
1587 * another NAND operation like nand_write_page_begin_op().
1588 * This function does not select/unselect the CS line.
1589 *
1590 * Returns 0 on success, a negative error code otherwise.
1591 */
1592int nand_write_data_op(struct nand_chip *chip, const void *buf,
1593 unsigned int len, bool force_8bit)
1594{
1595 struct mtd_info *mtd = nand_to_mtd(chip);
1596
1597 if (!len || !buf)
1598 return -EINVAL;
1599
1600 if (force_8bit) {
1601 const u8 *p = buf;
1602 unsigned int i;
1603
1604 for (i = 0; i < len; i++)
1605 chip->write_byte(mtd, p[i]);
1606 } else {
1607 chip->write_buf(mtd, buf, len);
1608 }
1609
1610 return 0;
1611}
1612EXPORT_SYMBOL_GPL(nand_write_data_op);
1613
1614/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001615 * nand_reset - Reset and initialize a NAND device
1616 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001617 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001618 *
1619 * Returns 0 for success or negative error code otherwise
1620 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001621int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001622{
1623 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001624 int ret;
1625
Boris Brezillon32935f42017-11-22 02:38:28 +09001626 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001627 if (ret)
1628 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001629
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001630 /*
1631 * The CS line has to be released before we can apply the new NAND
1632 * interface settings, hence this weird ->select_chip() dance.
1633 */
1634 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001635 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001636 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001637 if (ret)
1638 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001639
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001640 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001641 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001642 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001643 if (ret)
1644 return ret;
1645
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001646 return 0;
1647}
1648
1649/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001650 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1651 * @buf: buffer to test
1652 * @len: buffer length
1653 * @bitflips_threshold: maximum number of bitflips
1654 *
1655 * Check if a buffer contains only 0xff, which means the underlying region
1656 * has been erased and is ready to be programmed.
1657 * The bitflips_threshold specify the maximum number of bitflips before
1658 * considering the region is not erased.
1659 * Note: The logic of this function has been extracted from the memweight
1660 * implementation, except that nand_check_erased_buf function exit before
1661 * testing the whole buffer if the number of bitflips exceed the
1662 * bitflips_threshold value.
1663 *
1664 * Returns a positive number of bitflips less than or equal to
1665 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1666 * threshold.
1667 */
1668static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1669{
1670 const unsigned char *bitmap = buf;
1671 int bitflips = 0;
1672 int weight;
1673
1674 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1675 len--, bitmap++) {
1676 weight = hweight8(*bitmap);
1677 bitflips += BITS_PER_BYTE - weight;
1678 if (unlikely(bitflips > bitflips_threshold))
1679 return -EBADMSG;
1680 }
1681
1682 for (; len >= 4; len -= 4, bitmap += 4) {
1683 weight = hweight32(*((u32 *)bitmap));
1684 bitflips += 32 - weight;
1685 if (unlikely(bitflips > bitflips_threshold))
1686 return -EBADMSG;
1687 }
1688
1689 for (; len > 0; len--, bitmap++) {
1690 weight = hweight8(*bitmap);
1691 bitflips += BITS_PER_BYTE - weight;
1692 if (unlikely(bitflips > bitflips_threshold))
1693 return -EBADMSG;
1694 }
1695
1696 return bitflips;
1697}
1698
1699/**
1700 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1701 * 0xff data
1702 * @data: data buffer to test
1703 * @datalen: data length
1704 * @ecc: ECC buffer
1705 * @ecclen: ECC length
1706 * @extraoob: extra OOB buffer
1707 * @extraooblen: extra OOB length
1708 * @bitflips_threshold: maximum number of bitflips
1709 *
1710 * Check if a data buffer and its associated ECC and OOB data contains only
1711 * 0xff pattern, which means the underlying region has been erased and is
1712 * ready to be programmed.
1713 * The bitflips_threshold specify the maximum number of bitflips before
1714 * considering the region as not erased.
1715 *
1716 * Note:
1717 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1718 * different from the NAND page size. When fixing bitflips, ECC engines will
1719 * report the number of errors per chunk, and the NAND core infrastructure
1720 * expect you to return the maximum number of bitflips for the whole page.
1721 * This is why you should always use this function on a single chunk and
1722 * not on the whole page. After checking each chunk you should update your
1723 * max_bitflips value accordingly.
1724 * 2/ When checking for bitflips in erased pages you should not only check
1725 * the payload data but also their associated ECC data, because a user might
1726 * have programmed almost all bits to 1 but a few. In this case, we
1727 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1728 * this case.
1729 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1730 * data are protected by the ECC engine.
1731 * It could also be used if you support subpages and want to attach some
1732 * extra OOB data to an ECC chunk.
1733 *
1734 * Returns a positive number of bitflips less than or equal to
1735 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1736 * threshold. In case of success, the passed buffers are filled with 0xff.
1737 */
1738int nand_check_erased_ecc_chunk(void *data, int datalen,
1739 void *ecc, int ecclen,
1740 void *extraoob, int extraooblen,
1741 int bitflips_threshold)
1742{
1743 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1744
1745 data_bitflips = nand_check_erased_buf(data, datalen,
1746 bitflips_threshold);
1747 if (data_bitflips < 0)
1748 return data_bitflips;
1749
1750 bitflips_threshold -= data_bitflips;
1751
1752 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1753 if (ecc_bitflips < 0)
1754 return ecc_bitflips;
1755
1756 bitflips_threshold -= ecc_bitflips;
1757
1758 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1759 bitflips_threshold);
1760 if (extraoob_bitflips < 0)
1761 return extraoob_bitflips;
1762
1763 if (data_bitflips)
1764 memset(data, 0xff, datalen);
1765
1766 if (ecc_bitflips)
1767 memset(ecc, 0xff, ecclen);
1768
1769 if (extraoob_bitflips)
1770 memset(extraoob, 0xff, extraooblen);
1771
1772 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1773}
1774EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001775
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001776/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001777 * nand_read_page_raw - [INTERN] read raw page data without ecc
1778 * @mtd: mtd info structure
1779 * @chip: nand chip info structure
1780 * @buf: buffer to store read data
1781 * @oob_required: caller requires OOB data read to chip->oob_poi
1782 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001783 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001784 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001785 */
William Juul52c07962007-10-31 13:53:06 +01001786static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001787 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001788{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001789 int ret;
1790
1791 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1792 if (ret)
1793 return ret;
1794
1795 if (oob_required) {
1796 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1797 false);
1798 if (ret)
1799 return ret;
1800 }
1801
William Juul52c07962007-10-31 13:53:06 +01001802 return 0;
1803}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001804
William Juul52c07962007-10-31 13:53:06 +01001805/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001806 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1807 * @mtd: mtd info structure
1808 * @chip: nand chip info structure
1809 * @buf: buffer to store read data
1810 * @oob_required: caller requires OOB data read to chip->oob_poi
1811 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001812 *
1813 * We need a special oob layout and handling even when OOB isn't used.
1814 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001815static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001816 struct nand_chip *chip, uint8_t *buf,
1817 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001818{
1819 int eccsize = chip->ecc.size;
1820 int eccbytes = chip->ecc.bytes;
1821 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001822 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001823
1824 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001825 ret = nand_read_data_op(chip, buf, eccsize, false);
1826 if (ret)
1827 return ret;
1828
David Brownellee86b8d2009-11-07 16:27:01 -05001829 buf += eccsize;
1830
1831 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001832 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1833 false);
1834 if (ret)
1835 return ret;
1836
David Brownellee86b8d2009-11-07 16:27:01 -05001837 oob += chip->ecc.prepad;
1838 }
1839
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001840 ret = nand_read_data_op(chip, oob, eccbytes, false);
1841 if (ret)
1842 return ret;
1843
David Brownellee86b8d2009-11-07 16:27:01 -05001844 oob += eccbytes;
1845
1846 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001847 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1848 false);
1849 if (ret)
1850 return ret;
1851
David Brownellee86b8d2009-11-07 16:27:01 -05001852 oob += chip->ecc.postpad;
1853 }
1854 }
1855
1856 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001857 if (size) {
1858 ret = nand_read_data_op(chip, oob, size, false);
1859 if (ret)
1860 return ret;
1861 }
David Brownellee86b8d2009-11-07 16:27:01 -05001862
1863 return 0;
1864}
1865
1866/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001867 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1868 * @mtd: mtd info structure
1869 * @chip: nand chip info structure
1870 * @buf: buffer to store read data
1871 * @oob_required: caller requires OOB data read to chip->oob_poi
1872 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001873 */
1874static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001875 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001876{
1877 int i, eccsize = chip->ecc.size;
1878 int eccbytes = chip->ecc.bytes;
1879 int eccsteps = chip->ecc.steps;
1880 uint8_t *p = buf;
1881 uint8_t *ecc_calc = chip->buffers->ecccalc;
1882 uint8_t *ecc_code = chip->buffers->ecccode;
1883 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001884 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001885
Sergey Lapin3a38a552013-01-14 03:46:50 +00001886 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001887
William Juul52c07962007-10-31 13:53:06 +01001888 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1889 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001890
William Juul52c07962007-10-31 13:53:06 +01001891 for (i = 0; i < chip->ecc.total; i++)
1892 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001893
William Juul52c07962007-10-31 13:53:06 +01001894 eccsteps = chip->ecc.steps;
1895 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001896
William Juul52c07962007-10-31 13:53:06 +01001897 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1898 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001899
William Juul52c07962007-10-31 13:53:06 +01001900 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001901 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001902 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001903 } else {
William Juul52c07962007-10-31 13:53:06 +01001904 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001905 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1906 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001907 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001908 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001909}
1910
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001911/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001912 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001913 * @mtd: mtd info structure
1914 * @chip: nand chip info structure
1915 * @data_offs: offset of requested data within the page
1916 * @readlen: data length
1917 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001918 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001919 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001920static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001921 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1922 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001923{
1924 int start_step, end_step, num_steps;
1925 uint32_t *eccpos = chip->ecc.layout->eccpos;
1926 uint8_t *p;
1927 int data_col_addr, i, gaps = 0;
1928 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1929 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001930 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001931 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001932 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001933
Sergey Lapin3a38a552013-01-14 03:46:50 +00001934 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001935 start_step = data_offs / chip->ecc.size;
1936 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1937 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001938 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001939
Sergey Lapin3a38a552013-01-14 03:46:50 +00001940 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001941 datafrag_len = num_steps * chip->ecc.size;
1942 eccfrag_len = num_steps * chip->ecc.bytes;
1943
1944 data_col_addr = start_step * chip->ecc.size;
1945 /* If we read not a page aligned data */
1946 if (data_col_addr != 0)
1947 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1948
1949 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001950 ret = nand_read_data_op(chip, p, datafrag_len, false);
1951 if (ret)
1952 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001953
Sergey Lapin3a38a552013-01-14 03:46:50 +00001954 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001955 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1956 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1957
Sergey Lapin3a38a552013-01-14 03:46:50 +00001958 /*
1959 * The performance is faster if we position offsets according to
1960 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1961 */
Scott Wood3628f002008-10-24 16:20:43 -05001962 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001963 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001964 gaps = 1;
1965 break;
1966 }
1967 }
1968 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001969 ret = nand_change_read_column_op(chip, mtd->writesize,
1970 chip->oob_poi, mtd->oobsize,
1971 false);
1972 if (ret)
1973 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001974 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001975 /*
1976 * Send the command to read the particular ECC bytes take care
1977 * about buswidth alignment in read_buf.
1978 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001979 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001980 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001981 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001982 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001983 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001984 aligned_len++;
1985
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001986 ret = nand_change_read_column_op(chip,
1987 mtd->writesize + aligned_pos,
1988 &chip->oob_poi[aligned_pos],
1989 aligned_len, false);
1990 if (ret)
1991 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001992 }
1993
1994 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001995 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001996
1997 p = bufpoi + data_col_addr;
1998 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1999 int stat;
2000
Christian Hitzb8a6b372011-10-12 09:32:02 +02002001 stat = chip->ecc.correct(mtd, p,
2002 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002003 if (stat == -EBADMSG &&
2004 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2005 /* check for empty pages with bitflips */
2006 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2007 &chip->buffers->ecccode[i],
2008 chip->ecc.bytes,
2009 NULL, 0,
2010 chip->ecc.strength);
2011 }
2012
Heiko Schocherf5895d12014-06-24 10:10:04 +02002013 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05002014 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002015 } else {
Scott Wood3628f002008-10-24 16:20:43 -05002016 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002017 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2018 }
Scott Wood3628f002008-10-24 16:20:43 -05002019 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002020 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05002021}
2022
2023/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002024 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
2025 * @mtd: mtd info structure
2026 * @chip: nand chip info structure
2027 * @buf: buffer to store read data
2028 * @oob_required: caller requires OOB data read to chip->oob_poi
2029 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002030 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002031 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002032 */
William Juul52c07962007-10-31 13:53:06 +01002033static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002034 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002035{
William Juul52c07962007-10-31 13:53:06 +01002036 int i, eccsize = chip->ecc.size;
2037 int eccbytes = chip->ecc.bytes;
2038 int eccsteps = chip->ecc.steps;
2039 uint8_t *p = buf;
2040 uint8_t *ecc_calc = chip->buffers->ecccalc;
2041 uint8_t *ecc_code = chip->buffers->ecccode;
2042 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002043 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002044 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002045
William Juul52c07962007-10-31 13:53:06 +01002046 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2047 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002048
2049 ret = nand_read_data_op(chip, p, eccsize, false);
2050 if (ret)
2051 return ret;
2052
William Juul52c07962007-10-31 13:53:06 +01002053 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2054 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002055
2056 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2057 if (ret)
2058 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002059
William Juul52c07962007-10-31 13:53:06 +01002060 for (i = 0; i < chip->ecc.total; i++)
2061 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002062
William Juul52c07962007-10-31 13:53:06 +01002063 eccsteps = chip->ecc.steps;
2064 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002065
William Juul52c07962007-10-31 13:53:06 +01002066 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2067 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002068
William Juul52c07962007-10-31 13:53:06 +01002069 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002070 if (stat == -EBADMSG &&
2071 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2072 /* check for empty pages with bitflips */
2073 stat = nand_check_erased_ecc_chunk(p, eccsize,
2074 &ecc_code[i], eccbytes,
2075 NULL, 0,
2076 chip->ecc.strength);
2077 }
2078
Heiko Schocherf5895d12014-06-24 10:10:04 +02002079 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002080 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002081 } else {
William Juul52c07962007-10-31 13:53:06 +01002082 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002083 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2084 }
William Juul52c07962007-10-31 13:53:06 +01002085 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002086 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002087}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002088
William Juul52c07962007-10-31 13:53:06 +01002089/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002090 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2091 * @mtd: mtd info structure
2092 * @chip: nand chip info structure
2093 * @buf: buffer to store read data
2094 * @oob_required: caller requires OOB data read to chip->oob_poi
2095 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002096 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002097 * Hardware ECC for large page chips, require OOB to be read first. For this
2098 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2099 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2100 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2101 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002102 */
2103static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002104 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002105{
2106 int i, eccsize = chip->ecc.size;
2107 int eccbytes = chip->ecc.bytes;
2108 int eccsteps = chip->ecc.steps;
2109 uint8_t *p = buf;
2110 uint8_t *ecc_code = chip->buffers->ecccode;
2111 uint32_t *eccpos = chip->ecc.layout->eccpos;
2112 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002113 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002114 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002115
2116 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002117 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2118 if (ret)
2119 return ret;
2120
2121 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2122 if (ret)
2123 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002124
2125 for (i = 0; i < chip->ecc.total; i++)
2126 ecc_code[i] = chip->oob_poi[eccpos[i]];
2127
2128 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2129 int stat;
2130
2131 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002132
2133 ret = nand_read_data_op(chip, p, eccsize, false);
2134 if (ret)
2135 return ret;
2136
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002137 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2138
2139 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002140 if (stat == -EBADMSG &&
2141 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2142 /* check for empty pages with bitflips */
2143 stat = nand_check_erased_ecc_chunk(p, eccsize,
2144 &ecc_code[i], eccbytes,
2145 NULL, 0,
2146 chip->ecc.strength);
2147 }
2148
Heiko Schocherf5895d12014-06-24 10:10:04 +02002149 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002150 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002151 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002152 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002153 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2154 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002155 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002156 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002157}
2158
2159/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002160 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2161 * @mtd: mtd info structure
2162 * @chip: nand chip info structure
2163 * @buf: buffer to store read data
2164 * @oob_required: caller requires OOB data read to chip->oob_poi
2165 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002166 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002167 * The hw generator calculates the error syndrome automatically. Therefore we
2168 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002169 */
2170static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002171 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002172{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002173 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002174 int eccbytes = chip->ecc.bytes;
2175 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002176 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002177 uint8_t *p = buf;
2178 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002179 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002180
2181 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2182 int stat;
2183
2184 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002185
2186 ret = nand_read_data_op(chip, p, eccsize, false);
2187 if (ret)
2188 return ret;
William Juul52c07962007-10-31 13:53:06 +01002189
2190 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002191 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2192 false);
2193 if (ret)
2194 return ret;
2195
William Juul52c07962007-10-31 13:53:06 +01002196 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002197 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002198
William Juul52c07962007-10-31 13:53:06 +01002199 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002200
2201 ret = nand_read_data_op(chip, oob, eccbytes, false);
2202 if (ret)
2203 return ret;
2204
William Juul52c07962007-10-31 13:53:06 +01002205 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002206
William Juul52c07962007-10-31 13:53:06 +01002207 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002208
William Juul52c07962007-10-31 13:53:06 +01002209 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002210 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2211 false);
2212 if (ret)
2213 return ret;
2214
William Juul52c07962007-10-31 13:53:06 +01002215 oob += chip->ecc.postpad;
2216 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002217
2218 if (stat == -EBADMSG &&
2219 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2220 /* check for empty pages with bitflips */
2221 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2222 oob - eccpadbytes,
2223 eccpadbytes,
2224 NULL, 0,
2225 chip->ecc.strength);
2226 }
2227
2228 if (stat < 0) {
2229 mtd->ecc_stats.failed++;
2230 } else {
2231 mtd->ecc_stats.corrected += stat;
2232 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2233 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002234 }
William Juul52c07962007-10-31 13:53:06 +01002235
2236 /* Calculate remaining oob bytes */
2237 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002238 if (i) {
2239 ret = nand_read_data_op(chip, oob, i, false);
2240 if (ret)
2241 return ret;
2242 }
William Juul52c07962007-10-31 13:53:06 +01002243
Heiko Schocherf5895d12014-06-24 10:10:04 +02002244 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002245}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002246
2247/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002248 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2249 * @chip: nand chip structure
2250 * @oob: oob destination address
2251 * @ops: oob ops structure
2252 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002253 */
2254static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2255 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002256{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002257 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002258
Sergey Lapin3a38a552013-01-14 03:46:50 +00002259 case MTD_OPS_PLACE_OOB:
2260 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002261 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2262 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002263
Sergey Lapin3a38a552013-01-14 03:46:50 +00002264 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002265 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2266 uint32_t boffs = 0, roffs = ops->ooboffs;
2267 size_t bytes = 0;
2268
Christian Hitz13fc0e22011-10-12 09:32:01 +02002269 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002270 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002271 if (unlikely(roffs)) {
2272 if (roffs >= free->length) {
2273 roffs -= free->length;
2274 continue;
2275 }
2276 boffs = free->offset + roffs;
2277 bytes = min_t(size_t, len,
2278 (free->length - roffs));
2279 roffs = 0;
2280 } else {
2281 bytes = min_t(size_t, len, free->length);
2282 boffs = free->offset;
2283 }
2284 memcpy(oob, chip->oob_poi + boffs, bytes);
2285 oob += bytes;
2286 }
2287 return oob;
2288 }
2289 default:
2290 BUG();
2291 }
2292 return NULL;
2293}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002294
2295/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002296 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2297 * @mtd: MTD device structure
2298 * @retry_mode: the retry mode to use
2299 *
2300 * Some vendors supply a special command to shift the Vt threshold, to be used
2301 * when there are too many bitflips in a page (i.e., ECC error). After setting
2302 * a new threshold, the host should retry reading the page.
2303 */
2304static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2305{
Scott Wood17fed142016-05-30 13:57:56 -05002306 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002307
2308 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2309
2310 if (retry_mode >= chip->read_retries)
2311 return -EINVAL;
2312
2313 if (!chip->setup_read_retry)
2314 return -EOPNOTSUPP;
2315
2316 return chip->setup_read_retry(mtd, retry_mode);
2317}
2318
2319/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002320 * nand_do_read_ops - [INTERN] Read data with ECC
2321 * @mtd: MTD device structure
2322 * @from: offset to read from
2323 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002324 *
William Juul52c07962007-10-31 13:53:06 +01002325 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002326 */
William Juul52c07962007-10-31 13:53:06 +01002327static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2328 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002329{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002330 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002331 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002332 int ret = 0;
2333 uint32_t readlen = ops->len;
2334 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002335 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002336
William Juul52c07962007-10-31 13:53:06 +01002337 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002338 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002339 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002340 int retry_mode = 0;
2341 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002342
William Juul52c07962007-10-31 13:53:06 +01002343 chipnr = (int)(from >> chip->chip_shift);
2344 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002345
William Juul52c07962007-10-31 13:53:06 +01002346 realpage = (int)(from >> chip->page_shift);
2347 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002348
William Juul52c07962007-10-31 13:53:06 +01002349 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002350
William Juul52c07962007-10-31 13:53:06 +01002351 buf = ops->datbuf;
2352 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002353 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002354
Christian Hitz13fc0e22011-10-12 09:32:01 +02002355 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002356 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002357
Stefan Roese80877fa2022-09-02 14:10:46 +02002358 schedule();
William Juul52c07962007-10-31 13:53:06 +01002359 bytes = min(mtd->writesize - col, readlen);
2360 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002361
Scott Wood3ea94ed2015-06-26 19:03:26 -05002362 if (!aligned)
2363 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002364 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2365 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2366 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002367 else
2368 use_bufpoi = 0;
2369
Sergey Lapin3a38a552013-01-14 03:46:50 +00002370 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002371 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002372 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2373
2374 if (use_bufpoi && aligned)
2375 pr_debug("%s: using read bounce buffer for buf@%p\n",
2376 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002377
Heiko Schocherf5895d12014-06-24 10:10:04 +02002378read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002379 if (nand_standard_page_accessors(&chip->ecc)) {
2380 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2381 if (ret)
2382 break;
2383 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002384
Paul Burton700a76c2013-09-04 15:16:56 +01002385 /*
2386 * Now read the page into the buffer. Absent an error,
2387 * the read methods return max bitflips per ecc step.
2388 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002389 if (unlikely(ops->mode == MTD_OPS_RAW))
2390 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2391 oob_required,
2392 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002393 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002394 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002395 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002396 col, bytes, bufpoi,
2397 page);
William Juul52c07962007-10-31 13:53:06 +01002398 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002399 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002400 oob_required, page);
2401 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002402 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002403 /* Invalidate page cache */
2404 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002405 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002406 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002407
Paul Burton700a76c2013-09-04 15:16:56 +01002408 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2409
William Juul52c07962007-10-31 13:53:06 +01002410 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002411 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002412 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002413 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002414 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002415 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002416 chip->pagebuf_bitflips = ret;
2417 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002418 /* Invalidate page cache */
2419 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002420 }
William Juul52c07962007-10-31 13:53:06 +01002421 memcpy(buf, chip->buffers->databuf + col, bytes);
2422 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002423
William Juul52c07962007-10-31 13:53:06 +01002424 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002425 int toread = min(oobreadlen, max_oobsize);
2426
2427 if (toread) {
2428 oob = nand_transfer_oob(chip,
2429 oob, ops, toread);
2430 oobreadlen -= toread;
2431 }
William Juul52c07962007-10-31 13:53:06 +01002432 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002433
2434 if (chip->options & NAND_NEED_READRDY) {
2435 /* Apply delay or wait for ready/busy pin */
2436 if (!chip->dev_ready)
2437 udelay(chip->chip_delay);
2438 else
2439 nand_wait_ready(mtd);
2440 }
2441
2442 if (mtd->ecc_stats.failed - ecc_failures) {
2443 if (retry_mode + 1 < chip->read_retries) {
2444 retry_mode++;
2445 ret = nand_setup_read_retry(mtd,
2446 retry_mode);
2447 if (ret < 0)
2448 break;
2449
2450 /* Reset failures; retry */
2451 mtd->ecc_stats.failed = ecc_failures;
2452 goto read_retry;
2453 } else {
2454 /* No more retry modes; real failure */
2455 ecc_fail = true;
2456 }
2457 }
2458
2459 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002460 } else {
2461 memcpy(buf, chip->buffers->databuf + col, bytes);
2462 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002463 max_bitflips = max_t(unsigned int, max_bitflips,
2464 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002465 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002466
William Juul52c07962007-10-31 13:53:06 +01002467 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002468
Heiko Schocherf5895d12014-06-24 10:10:04 +02002469 /* Reset to retry mode 0 */
2470 if (retry_mode) {
2471 ret = nand_setup_read_retry(mtd, 0);
2472 if (ret < 0)
2473 break;
2474 retry_mode = 0;
2475 }
2476
William Juul52c07962007-10-31 13:53:06 +01002477 if (!readlen)
2478 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002479
Sergey Lapin3a38a552013-01-14 03:46:50 +00002480 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002481 col = 0;
2482 /* Increment page address */
2483 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002484
William Juul52c07962007-10-31 13:53:06 +01002485 page = realpage & chip->pagemask;
2486 /* Check, if we cross a chip boundary */
2487 if (!page) {
2488 chipnr++;
2489 chip->select_chip(mtd, -1);
2490 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002491 }
William Juul52c07962007-10-31 13:53:06 +01002492 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002493 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002494
William Juul52c07962007-10-31 13:53:06 +01002495 ops->retlen = ops->len - (size_t) readlen;
2496 if (oob)
2497 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002498
Heiko Schocherf5895d12014-06-24 10:10:04 +02002499 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002500 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002501
Heiko Schocherf5895d12014-06-24 10:10:04 +02002502 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002503 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002504
Paul Burton700a76c2013-09-04 15:16:56 +01002505 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002506}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002507
William Juul52c07962007-10-31 13:53:06 +01002508/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002509 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2510 * @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_std(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{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002517 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002518}
2519
2520/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002521 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002522 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002523 * @mtd: mtd info structure
2524 * @chip: nand chip info structure
2525 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002526 */
2527static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002528 int page)
William Juul52c07962007-10-31 13:53:06 +01002529{
William Juul52c07962007-10-31 13:53:06 +01002530 int length = mtd->oobsize;
2531 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2532 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002533 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002534 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002535
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002536 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2537 if (ret)
2538 return ret;
2539
William Juul52c07962007-10-31 13:53:06 +01002540 for (i = 0; i < chip->ecc.steps; i++) {
2541 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002542 int ret;
2543
William Juul52c07962007-10-31 13:53:06 +01002544 pos = eccsize + i * (eccsize + chunk);
2545 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002546 ret = nand_change_read_column_op(chip, pos,
2547 NULL, 0,
2548 false);
William Juul52c07962007-10-31 13:53:06 +01002549 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002550 ret = nand_read_page_op(chip, page, pos, NULL,
2551 0);
2552
2553 if (ret)
2554 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002555 } else
William Juul52c07962007-10-31 13:53:06 +01002556 sndrnd = 1;
2557 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002558
2559 ret = nand_read_data_op(chip, bufpoi, toread, false);
2560 if (ret)
2561 return ret;
2562
William Juul52c07962007-10-31 13:53:06 +01002563 bufpoi += toread;
2564 length -= toread;
2565 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002566 if (length > 0) {
2567 ret = nand_read_data_op(chip, bufpoi, length, false);
2568 if (ret)
2569 return ret;
2570 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002571
Sergey Lapin3a38a552013-01-14 03:46:50 +00002572 return 0;
William Juul52c07962007-10-31 13:53:06 +01002573}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002574
William Juul52c07962007-10-31 13:53:06 +01002575/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002576 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2577 * @mtd: mtd info structure
2578 * @chip: nand chip info structure
2579 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002580 */
2581static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2582 int page)
2583{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002584 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2585 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002586}
2587
2588/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002589 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2590 * with syndrome - only for large page flash
2591 * @mtd: mtd info structure
2592 * @chip: nand chip info structure
2593 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002594 */
2595static int nand_write_oob_syndrome(struct mtd_info *mtd,
2596 struct nand_chip *chip, int page)
2597{
2598 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2599 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002600 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002601 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002602
2603 /*
William Juul52c07962007-10-31 13:53:06 +01002604 * data-ecc-data-ecc ... ecc-oob
2605 * or
2606 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002607 */
William Juul52c07962007-10-31 13:53:06 +01002608 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2609 pos = steps * (eccsize + chunk);
2610 steps = 0;
2611 } else
2612 pos = eccsize;
2613
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002614 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2615 if (ret)
2616 return ret;
2617
William Juul52c07962007-10-31 13:53:06 +01002618 for (i = 0; i < steps; i++) {
2619 if (sndcmd) {
2620 if (mtd->writesize <= 512) {
2621 uint32_t fill = 0xFFFFFFFF;
2622
2623 len = eccsize;
2624 while (len > 0) {
2625 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002626
2627 ret = nand_write_data_op(chip, &fill,
2628 num, false);
2629 if (ret)
2630 return ret;
2631
William Juul52c07962007-10-31 13:53:06 +01002632 len -= num;
2633 }
2634 } else {
2635 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002636 ret = nand_change_write_column_op(chip, pos,
2637 NULL, 0,
2638 false);
2639 if (ret)
2640 return ret;
William Juul52c07962007-10-31 13:53:06 +01002641 }
2642 } else
2643 sndcmd = 1;
2644 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002645
2646 ret = nand_write_data_op(chip, bufpoi, len, false);
2647 if (ret)
2648 return ret;
2649
William Juul52c07962007-10-31 13:53:06 +01002650 bufpoi += len;
2651 length -= len;
2652 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002653 if (length > 0) {
2654 ret = nand_write_data_op(chip, bufpoi, length, false);
2655 if (ret)
2656 return ret;
2657 }
William Juul52c07962007-10-31 13:53:06 +01002658
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002659 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002660}
2661
2662/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002663 * nand_do_read_oob - [INTERN] NAND read out-of-band
2664 * @mtd: MTD device structure
2665 * @from: offset to read from
2666 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002667 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002668 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002669 */
William Juul52c07962007-10-31 13:53:06 +01002670static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2671 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002672{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002673 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002674 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002675 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002676 int readlen = ops->ooblen;
2677 int len;
2678 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002679 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002680
Heiko Schocherf5895d12014-06-24 10:10:04 +02002681 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002682 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002683
Sergey Lapin3a38a552013-01-14 03:46:50 +00002684 stats = mtd->ecc_stats;
2685
Scott Wood52ab7ce2016-05-30 13:57:58 -05002686 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002687
William Juul52c07962007-10-31 13:53:06 +01002688 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002689 pr_debug("%s: attempt to start read outside oob\n",
2690 __func__);
William Juul52c07962007-10-31 13:53:06 +01002691 return -EINVAL;
2692 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002693
2694 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002695 if (unlikely(from >= mtd->size ||
2696 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2697 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002698 pr_debug("%s: attempt to read beyond end of device\n",
2699 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002700 return -EINVAL;
2701 }
2702
William Juul52c07962007-10-31 13:53:06 +01002703 chipnr = (int)(from >> chip->chip_shift);
2704 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002705
William Juul52c07962007-10-31 13:53:06 +01002706 /* Shift to get page */
2707 realpage = (int)(from >> chip->page_shift);
2708 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002709
Christian Hitz13fc0e22011-10-12 09:32:01 +02002710 while (1) {
Stefan Roese80877fa2022-09-02 14:10:46 +02002711 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002712
Sergey Lapin3a38a552013-01-14 03:46:50 +00002713 if (ops->mode == MTD_OPS_RAW)
2714 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2715 else
2716 ret = chip->ecc.read_oob(mtd, chip, page);
2717
2718 if (ret < 0)
2719 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002720
William Juul52c07962007-10-31 13:53:06 +01002721 len = min(len, readlen);
2722 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002723
Heiko Schocherf5895d12014-06-24 10:10:04 +02002724 if (chip->options & NAND_NEED_READRDY) {
2725 /* Apply delay or wait for ready/busy pin */
2726 if (!chip->dev_ready)
2727 udelay(chip->chip_delay);
2728 else
2729 nand_wait_ready(mtd);
2730 }
2731
William Juul52c07962007-10-31 13:53:06 +01002732 readlen -= len;
2733 if (!readlen)
2734 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002735
William Juul52c07962007-10-31 13:53:06 +01002736 /* Increment page address */
2737 realpage++;
2738
2739 page = realpage & chip->pagemask;
2740 /* Check, if we cross a chip boundary */
2741 if (!page) {
2742 chipnr++;
2743 chip->select_chip(mtd, -1);
2744 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002745 }
William Juul52c07962007-10-31 13:53:06 +01002746 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002747 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002748
Sergey Lapin3a38a552013-01-14 03:46:50 +00002749 ops->oobretlen = ops->ooblen - readlen;
2750
2751 if (ret < 0)
2752 return ret;
2753
2754 if (mtd->ecc_stats.failed - stats.failed)
2755 return -EBADMSG;
2756
2757 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002758}
2759
2760/**
William Juul52c07962007-10-31 13:53:06 +01002761 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002762 * @mtd: MTD device structure
2763 * @from: offset to read from
2764 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002765 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002766 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002767 */
William Juul52c07962007-10-31 13:53:06 +01002768static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2769 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002770{
William Juul52c07962007-10-31 13:53:06 +01002771 int ret = -ENOTSUPP;
2772
2773 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002774
2775 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002776 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002777 pr_debug("%s: attempt to read beyond end of device\n",
2778 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779 return -EINVAL;
2780 }
2781
Heiko Schocherf5895d12014-06-24 10:10:04 +02002782 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002783
Christian Hitz13fc0e22011-10-12 09:32:01 +02002784 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002785 case MTD_OPS_PLACE_OOB:
2786 case MTD_OPS_AUTO_OOB:
2787 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002788 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002789
William Juul52c07962007-10-31 13:53:06 +01002790 default:
2791 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002792 }
2793
William Juul52c07962007-10-31 13:53:06 +01002794 if (!ops->datbuf)
2795 ret = nand_do_read_oob(mtd, from, ops);
2796 else
2797 ret = nand_do_read_ops(mtd, from, ops);
2798
Christian Hitz13fc0e22011-10-12 09:32:01 +02002799out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002800 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002801 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002802}
2803
2804
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002805/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002806 * nand_write_page_raw - [INTERN] raw page write function
2807 * @mtd: mtd info structure
2808 * @chip: nand chip info structure
2809 * @buf: data buffer
2810 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002811 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002812 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002813 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002814 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002815static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002816 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002817{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002818 int ret;
2819
2820 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2821 if (ret)
2822 return ret;
2823
2824 if (oob_required) {
2825 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2826 false);
2827 if (ret)
2828 return ret;
2829 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002830
2831 return 0;
William Juul52c07962007-10-31 13:53:06 +01002832}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002833
William Juul52c07962007-10-31 13:53:06 +01002834/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002835 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2836 * @mtd: mtd info structure
2837 * @chip: nand chip info structure
2838 * @buf: data buffer
2839 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002840 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002841 *
2842 * We need a special oob layout and handling even when ECC isn't checked.
2843 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002844static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002845 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002846 const uint8_t *buf, int oob_required,
2847 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002848{
2849 int eccsize = chip->ecc.size;
2850 int eccbytes = chip->ecc.bytes;
2851 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002852 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002853
2854 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002855 ret = nand_write_data_op(chip, buf, eccsize, false);
2856 if (ret)
2857 return ret;
2858
David Brownellee86b8d2009-11-07 16:27:01 -05002859 buf += eccsize;
2860
2861 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002862 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2863 false);
2864 if (ret)
2865 return ret;
2866
David Brownellee86b8d2009-11-07 16:27:01 -05002867 oob += chip->ecc.prepad;
2868 }
2869
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002870 ret = nand_write_data_op(chip, oob, eccbytes, false);
2871 if (ret)
2872 return ret;
2873
David Brownellee86b8d2009-11-07 16:27:01 -05002874 oob += eccbytes;
2875
2876 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002877 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2878 false);
2879 if (ret)
2880 return ret;
2881
David Brownellee86b8d2009-11-07 16:27:01 -05002882 oob += chip->ecc.postpad;
2883 }
2884 }
2885
2886 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002887 if (size) {
2888 ret = nand_write_data_op(chip, oob, size, false);
2889 if (ret)
2890 return ret;
2891 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002892
2893 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002894}
2895/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002896 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2897 * @mtd: mtd info structure
2898 * @chip: nand chip info structure
2899 * @buf: data buffer
2900 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002901 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002902 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002903static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002904 const uint8_t *buf, int oob_required,
2905 int page)
William Juul52c07962007-10-31 13:53:06 +01002906{
2907 int i, eccsize = chip->ecc.size;
2908 int eccbytes = chip->ecc.bytes;
2909 int eccsteps = chip->ecc.steps;
2910 uint8_t *ecc_calc = chip->buffers->ecccalc;
2911 const uint8_t *p = buf;
2912 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002913
Sergey Lapin3a38a552013-01-14 03:46:50 +00002914 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002915 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2916 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002917
William Juul52c07962007-10-31 13:53:06 +01002918 for (i = 0; i < chip->ecc.total; i++)
2919 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002920
Scott Wood46e13102016-05-30 13:57:57 -05002921 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002922}
2923
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002924/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002925 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2926 * @mtd: mtd info structure
2927 * @chip: nand chip info structure
2928 * @buf: data buffer
2929 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002930 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002931 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002932static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002933 const uint8_t *buf, int oob_required,
2934 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002935{
William Juul52c07962007-10-31 13:53:06 +01002936 int i, eccsize = chip->ecc.size;
2937 int eccbytes = chip->ecc.bytes;
2938 int eccsteps = chip->ecc.steps;
2939 uint8_t *ecc_calc = chip->buffers->ecccalc;
2940 const uint8_t *p = buf;
2941 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002942 int ret;
William Juul52c07962007-10-31 13:53:06 +01002943
2944 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2945 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002946
2947 ret = nand_write_data_op(chip, p, eccsize, false);
2948 if (ret)
2949 return ret;
2950
William Juul52c07962007-10-31 13:53:06 +01002951 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2952 }
2953
2954 for (i = 0; i < chip->ecc.total; i++)
2955 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2956
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002957 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2958 if (ret)
2959 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002960
2961 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002962}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002963
Heiko Schocherf5895d12014-06-24 10:10:04 +02002964
2965/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002966 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002967 * @mtd: mtd info structure
2968 * @chip: nand chip info structure
2969 * @offset: column address of subpage within the page
2970 * @data_len: data length
2971 * @buf: data buffer
2972 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002973 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002974 */
2975static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2976 struct nand_chip *chip, uint32_t offset,
2977 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002978 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002979{
2980 uint8_t *oob_buf = chip->oob_poi;
2981 uint8_t *ecc_calc = chip->buffers->ecccalc;
2982 int ecc_size = chip->ecc.size;
2983 int ecc_bytes = chip->ecc.bytes;
2984 int ecc_steps = chip->ecc.steps;
2985 uint32_t *eccpos = chip->ecc.layout->eccpos;
2986 uint32_t start_step = offset / ecc_size;
2987 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2988 int oob_bytes = mtd->oobsize / ecc_steps;
2989 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002990 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002991
2992 for (step = 0; step < ecc_steps; step++) {
2993 /* configure controller for WRITE access */
2994 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2995
2996 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002997 ret = nand_write_data_op(chip, buf, ecc_size, false);
2998 if (ret)
2999 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003000
3001 /* mask ECC of un-touched subpages by padding 0xFF */
3002 if ((step < start_step) || (step > end_step))
3003 memset(ecc_calc, 0xff, ecc_bytes);
3004 else
3005 chip->ecc.calculate(mtd, buf, ecc_calc);
3006
3007 /* mask OOB of un-touched subpages by padding 0xFF */
3008 /* if oob_required, preserve OOB metadata of written subpage */
3009 if (!oob_required || (step < start_step) || (step > end_step))
3010 memset(oob_buf, 0xff, oob_bytes);
3011
3012 buf += ecc_size;
3013 ecc_calc += ecc_bytes;
3014 oob_buf += oob_bytes;
3015 }
3016
3017 /* copy calculated ECC for whole page to chip->buffer->oob */
3018 /* this include masked-value(0xFF) for unwritten subpages */
3019 ecc_calc = chip->buffers->ecccalc;
3020 for (i = 0; i < chip->ecc.total; i++)
3021 chip->oob_poi[eccpos[i]] = ecc_calc[i];
3022
3023 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003024 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3025 if (ret)
3026 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003027
3028 return 0;
3029}
3030
3031
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003032/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003033 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
3034 * @mtd: mtd info structure
3035 * @chip: nand chip info structure
3036 * @buf: data buffer
3037 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003038 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003039 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003040 * The hw generator calculates the error syndrome automatically. Therefore we
3041 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003042 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003043static int nand_write_page_syndrome(struct mtd_info *mtd,
3044 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003045 const uint8_t *buf, int oob_required,
3046 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003047{
William Juul52c07962007-10-31 13:53:06 +01003048 int i, eccsize = chip->ecc.size;
3049 int eccbytes = chip->ecc.bytes;
3050 int eccsteps = chip->ecc.steps;
3051 const uint8_t *p = buf;
3052 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003053 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003054
William Juul52c07962007-10-31 13:53:06 +01003055 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003056 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003057
3058 ret = nand_write_data_op(chip, p, eccsize, false);
3059 if (ret)
3060 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003061
William Juul52c07962007-10-31 13:53:06 +01003062 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003063 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3064 false);
3065 if (ret)
3066 return ret;
3067
William Juul52c07962007-10-31 13:53:06 +01003068 oob += chip->ecc.prepad;
3069 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003070
William Juul52c07962007-10-31 13:53:06 +01003071 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003072
3073 ret = nand_write_data_op(chip, oob, eccbytes, false);
3074 if (ret)
3075 return ret;
3076
William Juul52c07962007-10-31 13:53:06 +01003077 oob += eccbytes;
3078
3079 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003080 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3081 false);
3082 if (ret)
3083 return ret;
3084
William Juul52c07962007-10-31 13:53:06 +01003085 oob += chip->ecc.postpad;
3086 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003087 }
3088
William Juul52c07962007-10-31 13:53:06 +01003089 /* Calculate remaining oob bytes */
3090 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003091 if (i) {
3092 ret = nand_write_data_op(chip, oob, i, false);
3093 if (ret)
3094 return ret;
3095 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003096
3097 return 0;
William Juul52c07962007-10-31 13:53:06 +01003098}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003099
William Juul52c07962007-10-31 13:53:06 +01003100/**
3101 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003102 * @mtd: MTD device structure
3103 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003104 * @offset: address offset within the page
3105 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003106 * @buf: the data to write
3107 * @oob_required: must write chip->oob_poi to OOB
3108 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003109 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003110 */
3111static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003112 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003113 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003114{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003115 int status, subpage;
3116
3117 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3118 chip->ecc.write_subpage)
3119 subpage = offset || (data_len < mtd->writesize);
3120 else
3121 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003122
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003123 if (nand_standard_page_accessors(&chip->ecc)) {
3124 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3125 if (status)
3126 return status;
3127 }
William Juul52c07962007-10-31 13:53:06 +01003128
3129 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003130 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003131 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003132 else if (subpage)
3133 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003134 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003135 else
Scott Wood46e13102016-05-30 13:57:57 -05003136 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3137 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003138
3139 if (status < 0)
3140 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003141
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003142 if (nand_standard_page_accessors(&chip->ecc))
3143 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003144
William Juul52c07962007-10-31 13:53:06 +01003145 return 0;
3146}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003147
William Juul52c07962007-10-31 13:53:06 +01003148/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003149 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3150 * @mtd: MTD device structure
3151 * @oob: oob data buffer
3152 * @len: oob data write length
3153 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003154 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003155static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3156 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003157{
Scott Wood17fed142016-05-30 13:57:56 -05003158 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003159
3160 /*
3161 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3162 * data from a previous OOB read.
3163 */
3164 memset(chip->oob_poi, 0xff, mtd->oobsize);
3165
Christian Hitz13fc0e22011-10-12 09:32:01 +02003166 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003167
Sergey Lapin3a38a552013-01-14 03:46:50 +00003168 case MTD_OPS_PLACE_OOB:
3169 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003170 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3171 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003172
Sergey Lapin3a38a552013-01-14 03:46:50 +00003173 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003174 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3175 uint32_t boffs = 0, woffs = ops->ooboffs;
3176 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003177
Christian Hitz13fc0e22011-10-12 09:32:01 +02003178 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003179 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003180 if (unlikely(woffs)) {
3181 if (woffs >= free->length) {
3182 woffs -= free->length;
3183 continue;
3184 }
3185 boffs = free->offset + woffs;
3186 bytes = min_t(size_t, len,
3187 (free->length - woffs));
3188 woffs = 0;
3189 } else {
3190 bytes = min_t(size_t, len, free->length);
3191 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003192 }
William Juul52c07962007-10-31 13:53:06 +01003193 memcpy(chip->oob_poi + boffs, oob, bytes);
3194 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003195 }
William Juul52c07962007-10-31 13:53:06 +01003196 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003197 }
William Juul52c07962007-10-31 13:53:06 +01003198 default:
3199 BUG();
3200 }
3201 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003202}
3203
Christian Hitzb8a6b372011-10-12 09:32:02 +02003204#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003205
3206/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003207 * nand_do_write_ops - [INTERN] NAND write with ECC
3208 * @mtd: MTD device structure
3209 * @to: offset to write to
3210 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003211 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003212 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003213 */
William Juul52c07962007-10-31 13:53:06 +01003214static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3215 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003216{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003217 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003218 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003219 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003220
3221 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003222 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003223
William Juul52c07962007-10-31 13:53:06 +01003224 uint8_t *oob = ops->oobbuf;
3225 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003226 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003227 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003228
William Juul52c07962007-10-31 13:53:06 +01003229 ops->retlen = 0;
3230 if (!writelen)
3231 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003232
Heiko Schocherf5895d12014-06-24 10:10:04 +02003233 /* Reject writes, which are not page aligned */
3234 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003235 pr_notice("%s: attempt to write non page aligned data\n",
3236 __func__);
William Juul52c07962007-10-31 13:53:06 +01003237 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003238 }
3239
3240 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003241
William Juul52c07962007-10-31 13:53:06 +01003242 chipnr = (int)(to >> chip->chip_shift);
3243 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003244
3245 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003246 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003247 ret = -EIO;
3248 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003249 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003250
William Juul52c07962007-10-31 13:53:06 +01003251 realpage = (int)(to >> chip->page_shift);
3252 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003253
William Juul52c07962007-10-31 13:53:06 +01003254 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003255 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3256 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003257 chip->pagebuf = -1;
3258
Christian Hitzb8a6b372011-10-12 09:32:02 +02003259 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003260 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3261 ret = -EINVAL;
3262 goto err_out;
3263 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003264
Christian Hitz13fc0e22011-10-12 09:32:01 +02003265 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003266 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003267 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003268 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003269 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003270
3271 if (part_pagewr)
3272 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003273 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3274 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3275 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003276 else
3277 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003278
Stefan Roese80877fa2022-09-02 14:10:46 +02003279 schedule();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003280 /* Partial page write?, or need to use bounce buffer */
3281 if (use_bufpoi) {
3282 pr_debug("%s: using write bounce buffer for buf@%p\n",
3283 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003284 if (part_pagewr)
3285 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003286 chip->pagebuf = -1;
3287 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3288 memcpy(&chip->buffers->databuf[column], buf, bytes);
3289 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003290 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003291
Christian Hitzb8a6b372011-10-12 09:32:02 +02003292 if (unlikely(oob)) {
3293 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003294 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003295 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003296 } else {
3297 /* We still need to erase leftover OOB data */
3298 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003299 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003300 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003301 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003302 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003303 if (ret)
3304 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003305
William Juul52c07962007-10-31 13:53:06 +01003306 writelen -= bytes;
3307 if (!writelen)
3308 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003309
Heiko Schocherf5895d12014-06-24 10:10:04 +02003310 column = 0;
3311 buf += bytes;
3312 realpage++;
3313
3314 page = realpage & chip->pagemask;
3315 /* Check, if we cross a chip boundary */
3316 if (!page) {
3317 chipnr++;
3318 chip->select_chip(mtd, -1);
3319 chip->select_chip(mtd, chipnr);
3320 }
3321 }
3322
3323 ops->retlen = ops->len - writelen;
3324 if (unlikely(oob))
3325 ops->oobretlen = ops->ooblen;
3326
3327err_out:
3328 chip->select_chip(mtd, -1);
3329 return ret;
3330}
3331
3332/**
3333 * panic_nand_write - [MTD Interface] NAND write with ECC
3334 * @mtd: MTD device structure
3335 * @to: offset to write to
3336 * @len: number of bytes to write
3337 * @retlen: pointer to variable to store the number of written bytes
3338 * @buf: the data to write
3339 *
3340 * NAND write with ECC. Used when performing writes in interrupt context, this
3341 * may for example be called by mtdoops when writing an oops while in panic.
3342 */
3343static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3344 size_t *retlen, const uint8_t *buf)
3345{
Scott Wood17fed142016-05-30 13:57:56 -05003346 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003347 struct mtd_oob_ops ops;
3348 int ret;
3349
3350 /* Wait for the device to get ready */
3351 panic_nand_wait(mtd, chip, 400);
3352
3353 /* Grab the device */
3354 panic_nand_get_device(chip, mtd, FL_WRITING);
3355
Scott Wood3ea94ed2015-06-26 19:03:26 -05003356 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003357 ops.len = len;
3358 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003359 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003360
Heiko Schocherf5895d12014-06-24 10:10:04 +02003361 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003362
Sergey Lapin3a38a552013-01-14 03:46:50 +00003363 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003364 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003365}
3366
3367/**
William Juul52c07962007-10-31 13:53:06 +01003368 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003369 * @mtd: MTD device structure
3370 * @to: offset to write to
3371 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003372 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003373 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003374 */
William Juul52c07962007-10-31 13:53:06 +01003375static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3376 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003377{
William Juul52c07962007-10-31 13:53:06 +01003378 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003379 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003380
Heiko Schocherf5895d12014-06-24 10:10:04 +02003381 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003382 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003383
Scott Wood52ab7ce2016-05-30 13:57:58 -05003384 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003385
3386 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003387 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003388 pr_debug("%s: attempt to write past end of page\n",
3389 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003390 return -EINVAL;
3391 }
3392
William Juul52c07962007-10-31 13:53:06 +01003393 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003394 pr_debug("%s: attempt to start write outside oob\n",
3395 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003396 return -EINVAL;
3397 }
3398
Christian Hitz13fc0e22011-10-12 09:32:01 +02003399 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003400 if (unlikely(to >= mtd->size ||
3401 ops->ooboffs + ops->ooblen >
3402 ((mtd->size >> chip->page_shift) -
3403 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003404 pr_debug("%s: attempt to write beyond end of device\n",
3405 __func__);
William Juul52c07962007-10-31 13:53:06 +01003406 return -EINVAL;
3407 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003408
William Juul52c07962007-10-31 13:53:06 +01003409 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003410
3411 /*
3412 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3413 * of my DiskOnChip 2000 test units) will clear the whole data page too
3414 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3415 * it in the doc2000 driver in August 1999. dwmw2.
3416 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003417 nand_reset(chip, chipnr);
3418
3419 chip->select_chip(mtd, chipnr);
3420
3421 /* Shift to get page */
3422 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003423
3424 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003425 if (nand_check_wp(mtd)) {
3426 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003427 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003428 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003429
William Juul52c07962007-10-31 13:53:06 +01003430 /* Invalidate the page cache, if we write to the cached page */
3431 if (page == chip->pagebuf)
3432 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003433
Sergey Lapin3a38a552013-01-14 03:46:50 +00003434 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3435
3436 if (ops->mode == MTD_OPS_RAW)
3437 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3438 else
3439 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003440
Heiko Schocherf5895d12014-06-24 10:10:04 +02003441 chip->select_chip(mtd, -1);
3442
William Juul52c07962007-10-31 13:53:06 +01003443 if (status)
3444 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003445
William Juul52c07962007-10-31 13:53:06 +01003446 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003447
William Juul52c07962007-10-31 13:53:06 +01003448 return 0;
3449}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003450
William Juul52c07962007-10-31 13:53:06 +01003451/**
3452 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003453 * @mtd: MTD device structure
3454 * @to: offset to write to
3455 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003456 */
3457static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3458 struct mtd_oob_ops *ops)
3459{
William Juul52c07962007-10-31 13:53:06 +01003460 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003461
William Juul52c07962007-10-31 13:53:06 +01003462 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003463
William Juul52c07962007-10-31 13:53:06 +01003464 /* Do not allow writes past end of device */
3465 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003466 pr_debug("%s: attempt to write beyond end of device\n",
3467 __func__);
William Juul52c07962007-10-31 13:53:06 +01003468 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003469 }
William Juul52c07962007-10-31 13:53:06 +01003470
Heiko Schocherf5895d12014-06-24 10:10:04 +02003471 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003472
Christian Hitz13fc0e22011-10-12 09:32:01 +02003473 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003474 case MTD_OPS_PLACE_OOB:
3475 case MTD_OPS_AUTO_OOB:
3476 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003477 break;
3478
3479 default:
3480 goto out;
3481 }
3482
3483 if (!ops->datbuf)
3484 ret = nand_do_write_oob(mtd, to, ops);
3485 else
3486 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003487
Christian Hitz13fc0e22011-10-12 09:32:01 +02003488out:
William Juul52c07962007-10-31 13:53:06 +01003489 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003490 return ret;
3491}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003492
3493/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003494 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003495 * @mtd: MTD device structure
3496 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003497 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003498 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003499 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003500static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003501{
Scott Wood17fed142016-05-30 13:57:56 -05003502 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003503 unsigned int eraseblock;
3504
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003505 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003506 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003507
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003508 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003509}
3510
3511/**
3512 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003513 * @mtd: MTD device structure
3514 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003515 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003516 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003517 */
William Juul52c07962007-10-31 13:53:06 +01003518static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003519{
William Juul52c07962007-10-31 13:53:06 +01003520 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003521}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003522
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003523/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003524 * nand_erase_nand - [INTERN] erase block(s)
3525 * @mtd: MTD device structure
3526 * @instr: erase instruction
3527 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003528 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003529 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003530 */
William Juul52c07962007-10-31 13:53:06 +01003531int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3532 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003533{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003534 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003535 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003536 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003537
Heiko Schocherf5895d12014-06-24 10:10:04 +02003538 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3539 __func__, (unsigned long long)instr->addr,
3540 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003541
Christian Hitzb8a6b372011-10-12 09:32:02 +02003542 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003543 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003544
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003545 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003546 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003547
3548 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003549 page = (int)(instr->addr >> chip->page_shift);
3550 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003551
3552 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003553 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003554
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003555 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003556 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003557
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003558 /* Check, if it is write protected */
3559 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003560 pr_debug("%s: device is write protected!\n",
3561 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003562 instr->state = MTD_ERASE_FAILED;
3563 goto erase_exit;
3564 }
3565
3566 /* Loop through the pages */
3567 len = instr->len;
3568
3569 instr->state = MTD_ERASING;
3570
3571 while (len) {
Stefan Roese80877fa2022-09-02 14:10:46 +02003572 schedule();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003573
Sergey Lapin3a38a552013-01-14 03:46:50 +00003574 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003575 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003576 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003577 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003578 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003579 instr->state = MTD_ERASE_FAILED;
Farhan Ali7c053192021-02-24 15:25:53 -08003580 instr->fail_addr =
3581 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003582 goto erase_exit;
3583 }
William Juul52c07962007-10-31 13:53:06 +01003584
3585 /*
3586 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003587 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003588 */
3589 if (page <= chip->pagebuf && chip->pagebuf <
3590 (page + pages_per_block))
3591 chip->pagebuf = -1;
3592
Scott Wood3ea94ed2015-06-26 19:03:26 -05003593 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003594
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003595 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003596 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003597 pr_debug("%s: failed erase, page 0x%08x\n",
3598 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003599 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003600 instr->fail_addr =
3601 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003602 goto erase_exit;
3603 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003604
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003605 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003606 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003607 page += pages_per_block;
3608
3609 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003610 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003611 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003612 chip->select_chip(mtd, -1);
3613 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003614 }
3615 }
3616 instr->state = MTD_ERASE_DONE;
3617
Christian Hitz13fc0e22011-10-12 09:32:01 +02003618erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003619
3620 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003621
3622 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003623 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003624 nand_release_device(mtd);
3625
3626 /* Return more or less happy */
3627 return ret;
3628}
3629
3630/**
3631 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003632 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003633 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003634 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003635 */
William Juul52c07962007-10-31 13:53:06 +01003636static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003637{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003638 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003639
3640 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003641 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003642 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003643 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003644}
3645
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003646/**
William Juul52c07962007-10-31 13:53:06 +01003647 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003648 * @mtd: MTD device structure
3649 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003650 */
William Juul52c07962007-10-31 13:53:06 +01003651static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003652{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003653 struct nand_chip *chip = mtd_to_nand(mtd);
3654 int chipnr = (int)(offs >> chip->chip_shift);
3655 int ret;
3656
3657 /* Select the NAND device */
3658 nand_get_device(mtd, FL_READING);
3659 chip->select_chip(mtd, chipnr);
3660
3661 ret = nand_block_checkbad(mtd, offs, 0);
3662
3663 chip->select_chip(mtd, -1);
3664 nand_release_device(mtd);
3665
3666 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003667}
3668
3669/**
William Juul52c07962007-10-31 13:53:06 +01003670 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003671 * @mtd: MTD device structure
3672 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003673 */
William Juul52c07962007-10-31 13:53:06 +01003674static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003675{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003676 int ret;
3677
Christian Hitzb8a6b372011-10-12 09:32:02 +02003678 ret = nand_block_isbad(mtd, ofs);
3679 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003680 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003681 if (ret > 0)
3682 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003683 return ret;
3684 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003685
Heiko Schocherf5895d12014-06-24 10:10:04 +02003686 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003687}
3688
Heiko Schocherf5895d12014-06-24 10:10:04 +02003689/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003690 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3691 * @mtd: MTD device structure
3692 * @chip: nand chip info structure
3693 * @addr: feature address.
3694 * @subfeature_param: the subfeature parameters, a four bytes array.
3695 */
3696static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3697 int addr, uint8_t *subfeature_param)
3698{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003699#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3700 if (!chip->onfi_version ||
3701 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3702 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003703 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003704#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003705
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003706 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003707}
3708
3709/**
3710 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3711 * @mtd: MTD device structure
3712 * @chip: nand chip info structure
3713 * @addr: feature address.
3714 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003715 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003716static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3717 int addr, uint8_t *subfeature_param)
3718{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003719#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3720 if (!chip->onfi_version ||
3721 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3722 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003723 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003724#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003725
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003726 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003727}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003728
Sergey Lapin3a38a552013-01-14 03:46:50 +00003729/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003730static void nand_set_defaults(struct nand_chip *chip, int busw)
3731{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003732 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003733 if (!chip->chip_delay)
3734 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003735
3736 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003737 if (chip->cmdfunc == NULL)
3738 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003739
3740 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003741 if (chip->waitfunc == NULL)
3742 chip->waitfunc = nand_wait;
3743
3744 if (!chip->select_chip)
3745 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003746
3747 /* set for ONFI nand */
3748 if (!chip->onfi_set_features)
3749 chip->onfi_set_features = nand_onfi_set_features;
3750 if (!chip->onfi_get_features)
3751 chip->onfi_get_features = nand_onfi_get_features;
3752
3753 /* If called twice, pointers that depend on busw may need to be reset */
3754 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003755 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3756 if (!chip->read_word)
3757 chip->read_word = nand_read_word;
3758 if (!chip->block_bad)
3759 chip->block_bad = nand_block_bad;
3760 if (!chip->block_markbad)
3761 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003762 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003763 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003764 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3765 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3766 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003767 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Roger Quadrosb590f612022-12-20 12:21:57 +02003768
3769#ifndef CONFIG_SPL_BUILD
William Juul52c07962007-10-31 13:53:06 +01003770 if (!chip->scan_bbt)
3771 chip->scan_bbt = nand_default_bbt;
Roger Quadrosb590f612022-12-20 12:21:57 +02003772#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02003773
3774 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003775 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003776 spin_lock_init(&chip->controller->lock);
3777 init_waitqueue_head(&chip->controller->wq);
3778 }
3779
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003780 if (!chip->buf_align)
3781 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003782}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003783
Sergey Lapin3a38a552013-01-14 03:46:50 +00003784/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003785static void sanitize_string(char *s, size_t len)
3786{
3787 ssize_t i;
3788
Sergey Lapin3a38a552013-01-14 03:46:50 +00003789 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003790 s[len - 1] = 0;
3791
Sergey Lapin3a38a552013-01-14 03:46:50 +00003792 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003793 for (i = 0; i < len - 1; i++) {
3794 if (s[i] < ' ' || s[i] > 127)
3795 s[i] = '?';
3796 }
3797
Sergey Lapin3a38a552013-01-14 03:46:50 +00003798 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003799 strim(s);
3800}
3801
Florian Fainellic98a9352011-02-25 00:01:34 +00003802static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3803{
3804 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003805 while (len--) {
3806 crc ^= *p++ << 8;
3807 for (i = 0; i < 8; i++)
3808 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3809 }
3810
3811 return crc;
3812}
3813
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003814#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003815/* Parse the Extended Parameter Page. */
3816static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3817 struct nand_chip *chip, struct nand_onfi_params *p)
3818{
3819 struct onfi_ext_param_page *ep;
3820 struct onfi_ext_section *s;
3821 struct onfi_ext_ecc_info *ecc;
3822 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003823 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003824 int len;
3825 int i;
3826
3827 len = le16_to_cpu(p->ext_param_page_length) * 16;
3828 ep = kmalloc(len, GFP_KERNEL);
3829 if (!ep)
3830 return -ENOMEM;
3831
3832 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003833 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3834 if (ret)
3835 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003836
3837 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003838 ret = nand_change_read_column_op(chip,
3839 sizeof(*p) * p->num_of_param_pages,
3840 ep, len, true);
3841 if (ret)
3842 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003843
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003844 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003845 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3846 != le16_to_cpu(ep->crc))) {
3847 pr_debug("fail in the CRC.\n");
3848 goto ext_out;
3849 }
3850
3851 /*
3852 * Check the signature.
3853 * Do not strictly follow the ONFI spec, maybe changed in future.
3854 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003855 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003856 pr_debug("The signature is invalid.\n");
3857 goto ext_out;
3858 }
3859
3860 /* find the ECC section. */
3861 cursor = (uint8_t *)(ep + 1);
3862 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3863 s = ep->sections + i;
3864 if (s->type == ONFI_SECTION_TYPE_2)
3865 break;
3866 cursor += s->length * 16;
3867 }
3868 if (i == ONFI_EXT_SECTION_MAX) {
3869 pr_debug("We can not find the ECC section.\n");
3870 goto ext_out;
3871 }
3872
3873 /* get the info we want. */
3874 ecc = (struct onfi_ext_ecc_info *)cursor;
3875
3876 if (!ecc->codeword_size) {
3877 pr_debug("Invalid codeword size\n");
3878 goto ext_out;
3879 }
3880
3881 chip->ecc_strength_ds = ecc->ecc_bits;
3882 chip->ecc_step_ds = 1 << ecc->codeword_size;
3883 ret = 0;
3884
3885ext_out:
3886 kfree(ep);
3887 return ret;
3888}
3889
Florian Fainellic98a9352011-02-25 00:01:34 +00003890/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003891 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003892 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003893static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003894{
3895 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003896 char id[4];
3897 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003898
Sergey Lapin3a38a552013-01-14 03:46:50 +00003899 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003900 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3901 if (ret || strncmp(id, "ONFI", 4))
3902 return 0;
3903
3904 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3905 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003906 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003907
Florian Fainellic98a9352011-02-25 00:01:34 +00003908 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003909 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3910 if (ret)
3911 return 0;
3912
Florian Fainellic98a9352011-02-25 00:01:34 +00003913 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003914 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003915 break;
3916 }
3917 }
3918
Heiko Schocherf5895d12014-06-24 10:10:04 +02003919 if (i == 3) {
3920 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003921 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003922 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003923
Sergey Lapin3a38a552013-01-14 03:46:50 +00003924 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003925 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003926 if (val & (1 << 5))
3927 chip->onfi_version = 23;
3928 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003929 chip->onfi_version = 22;
3930 else if (val & (1 << 3))
3931 chip->onfi_version = 21;
3932 else if (val & (1 << 2))
3933 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003934 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003935 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003936
3937 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003938 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003939 return 0;
3940 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003941
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003942 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3943 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003944 if (!mtd->name)
3945 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003946
Florian Fainellic98a9352011-02-25 00:01:34 +00003947 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003948
3949 /*
3950 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3951 * (don't ask me who thought of this...). MTD assumes that these
3952 * dimensions will be power-of-2, so just truncate the remaining area.
3953 */
3954 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3955 mtd->erasesize *= mtd->writesize;
3956
Florian Fainellic98a9352011-02-25 00:01:34 +00003957 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003958
3959 /* See erasesize comment */
3960 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003961 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003962 chip->bits_per_cell = p->bits_per_cell;
3963
3964 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003965 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003966
3967 if (p->ecc_bits != 0xff) {
3968 chip->ecc_strength_ds = p->ecc_bits;
3969 chip->ecc_step_ds = 512;
3970 } else if (chip->onfi_version >= 21 &&
3971 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3972
3973 /*
3974 * The nand_flash_detect_ext_param_page() uses the
3975 * Change Read Column command which maybe not supported
3976 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3977 * now. We do not replace user supplied command function.
3978 */
3979 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3980 chip->cmdfunc = nand_command_lp;
3981
3982 /* The Extended Parameter Page is supported since ONFI 2.1. */
3983 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3984 pr_warn("Failed to detect ONFI extended param page\n");
3985 } else {
3986 pr_warn("Could not retrieve ONFI ECC requirements\n");
3987 }
3988
Florian Fainellic98a9352011-02-25 00:01:34 +00003989 return 1;
3990}
3991#else
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003992static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003993{
3994 return 0;
3995}
3996#endif
3997
William Juul52c07962007-10-31 13:53:06 +01003998/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003999 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4000 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004001static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip)
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004002{
4003 struct nand_jedec_params *p = &chip->jedec_params;
4004 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004005 char id[5];
4006 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004007
4008 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004009 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4010 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004011 return 0;
4012
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004013 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4014 if (ret)
4015 return 0;
4016
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004017 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004018 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4019 if (ret)
4020 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004021
4022 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4023 le16_to_cpu(p->crc))
4024 break;
4025 }
4026
4027 if (i == 3) {
4028 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4029 return 0;
4030 }
4031
4032 /* Check version */
4033 val = le16_to_cpu(p->revision);
4034 if (val & (1 << 2))
4035 chip->jedec_version = 10;
4036 else if (val & (1 << 1))
4037 chip->jedec_version = 1; /* vendor specific version */
4038
4039 if (!chip->jedec_version) {
4040 pr_info("unsupported JEDEC version: %d\n", val);
4041 return 0;
4042 }
4043
4044 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4045 sanitize_string(p->model, sizeof(p->model));
4046 if (!mtd->name)
4047 mtd->name = p->model;
4048
4049 mtd->writesize = le32_to_cpu(p->byte_per_page);
4050
4051 /* Please reference to the comment for nand_flash_detect_onfi. */
4052 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4053 mtd->erasesize *= mtd->writesize;
4054
4055 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4056
4057 /* Please reference to the comment for nand_flash_detect_onfi. */
4058 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4059 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4060 chip->bits_per_cell = p->bits_per_cell;
4061
4062 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004063 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004064
4065 /* ECC info */
4066 ecc = &p->ecc_info[0];
4067
4068 if (ecc->codeword_size >= 9) {
4069 chip->ecc_strength_ds = ecc->ecc_bits;
4070 chip->ecc_step_ds = 1 << ecc->codeword_size;
4071 } else {
4072 pr_warn("Invalid codeword size\n");
4073 }
4074
4075 return 1;
4076}
4077
4078/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004079 * nand_id_has_period - Check if an ID string has a given wraparound period
4080 * @id_data: the ID string
4081 * @arrlen: the length of the @id_data array
4082 * @period: the period of repitition
4083 *
4084 * Check if an ID string is repeated within a given sequence of bytes at
4085 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02004086 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00004087 * if the repetition has a period of @period; otherwise, returns zero.
4088 */
4089static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4090{
4091 int i, j;
4092 for (i = 0; i < period; i++)
4093 for (j = i + period; j < arrlen; j += period)
4094 if (id_data[i] != id_data[j])
4095 return 0;
4096 return 1;
4097}
4098
4099/*
4100 * nand_id_len - Get the length of an ID string returned by CMD_READID
4101 * @id_data: the ID string
4102 * @arrlen: the length of the @id_data array
4103
4104 * Returns the length of the ID string, according to known wraparound/trailing
4105 * zero patterns. If no pattern exists, returns the length of the array.
4106 */
4107static int nand_id_len(u8 *id_data, int arrlen)
4108{
4109 int last_nonzero, period;
4110
4111 /* Find last non-zero byte */
4112 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4113 if (id_data[last_nonzero])
4114 break;
4115
4116 /* All zeros */
4117 if (last_nonzero < 0)
4118 return 0;
4119
4120 /* Calculate wraparound period */
4121 for (period = 1; period < arrlen; period++)
4122 if (nand_id_has_period(id_data, arrlen, period))
4123 break;
4124
4125 /* There's a repeated pattern */
4126 if (period < arrlen)
4127 return period;
4128
4129 /* There are trailing zeros */
4130 if (last_nonzero < arrlen - 1)
4131 return last_nonzero + 1;
4132
4133 /* No pattern detected */
4134 return arrlen;
4135}
4136
Heiko Schocherf5895d12014-06-24 10:10:04 +02004137/* Extract the bits of per cell from the 3rd byte of the extended ID */
4138static int nand_get_bits_per_cell(u8 cellinfo)
4139{
4140 int bits;
4141
4142 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4143 bits >>= NAND_CI_CELLTYPE_SHIFT;
4144 return bits + 1;
4145}
4146
Sergey Lapin3a38a552013-01-14 03:46:50 +00004147/*
4148 * Many new NAND share similar device ID codes, which represent the size of the
4149 * chip. The rest of the parameters must be decoded according to generic or
4150 * manufacturer-specific "extended ID" decoding patterns.
4151 */
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004152void nand_decode_ext_id(struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004153{
Michael Trimarchi270c1532022-07-20 18:22:07 +02004154 struct mtd_info *mtd = &chip->mtd;
Michael Trimarchi3ba671b2022-07-20 18:22:11 +02004155 int extid;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004156 /* The 3rd id byte holds MLC / multichip data */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004157 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004158 /* The 4th id byte is the important one */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004159 extid = chip->id.data[3];
Sergey Lapin3a38a552013-01-14 03:46:50 +00004160
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004161 /* Calc pagesize */
4162 mtd->writesize = 1024 << (extid & 0x03);
4163 extid >>= 2;
4164 /* Calc oobsize */
4165 mtd->oobsize = (8 << (extid & 0x01)) *
4166 (mtd->writesize >> 9);
4167 extid >>= 2;
4168 /* Calc blocksize. Blocksize is multiples of 64KiB */
4169 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4170 extid >>= 2;
4171 /* Get buswidth information */
4172 /* Get buswidth information */
4173 if (extid & 0x1)
4174 chip->options |= NAND_BUSWIDTH_16;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004175}
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004176EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004177
Heiko Schocherf5895d12014-06-24 10:10:04 +02004178/*
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004179 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4180 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4181 * table.
4182 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004183static void nand_manufacturer_detect(struct nand_chip *chip)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004184{
4185 /*
4186 * Try manufacturer detection if available and use
4187 * nand_decode_ext_id() otherwise.
4188 */
4189 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004190 chip->manufacturer.desc->ops->detect) {
4191 /* The 3rd id byte holds MLC / multichip data */
4192 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004193 chip->manufacturer.desc->ops->detect(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004194 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004195 nand_decode_ext_id(chip);
Michael Trimarchi39c5bef2022-10-21 08:05:36 +02004196 }
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004197}
4198
4199/*
4200 * Manufacturer initialization. This function is called for all NANDs including
4201 * ONFI and JEDEC compliant ones.
4202 * Manufacturer drivers should put all their specific initialization code in
4203 * their ->init() hook.
4204 */
4205static int nand_manufacturer_init(struct nand_chip *chip)
4206{
4207 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4208 !chip->manufacturer.desc->ops->init)
4209 return 0;
4210
4211 return chip->manufacturer.desc->ops->init(chip);
4212}
4213
4214/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004215 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4216 * decodes a matching ID table entry and assigns the MTD size parameters for
4217 * the chip.
4218 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004219static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004220{
Michael Trimarchi270c1532022-07-20 18:22:07 +02004221 struct mtd_info *mtd = &chip->mtd;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004222
4223 mtd->erasesize = type->erasesize;
4224 mtd->writesize = type->pagesize;
4225 mtd->oobsize = mtd->writesize / 32;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004226
Heiko Schocherf5895d12014-06-24 10:10:04 +02004227 /* All legacy ID NAND are small-page, SLC */
4228 chip->bits_per_cell = 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004229}
4230
Heiko Schocherf5895d12014-06-24 10:10:04 +02004231/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004232 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4233 * heuristic patterns using various detected parameters (e.g., manufacturer,
4234 * page size, cell-type information).
4235 */
4236static void nand_decode_bbm_options(struct mtd_info *mtd,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004237 struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004238{
Sergey Lapin3a38a552013-01-14 03:46:50 +00004239 /* Set the bad block position */
4240 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4241 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4242 else
4243 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004244}
4245
Heiko Schocherf5895d12014-06-24 10:10:04 +02004246static inline bool is_full_id_nand(struct nand_flash_dev *type)
4247{
4248 return type->id_len;
4249}
4250
4251static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004252 struct nand_flash_dev *type)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004253{
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004254 if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004255 mtd->writesize = type->pagesize;
4256 mtd->erasesize = type->erasesize;
4257 mtd->oobsize = type->oobsize;
4258
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004259 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004260 chip->chipsize = (uint64_t)type->chipsize << 20;
4261 chip->options |= type->options;
4262 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4263 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004264 chip->onfi_timing_mode_default =
4265 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004266
Heiko Schocherf5895d12014-06-24 10:10:04 +02004267 if (!mtd->name)
4268 mtd->name = type->name;
4269
4270 return true;
4271 }
4272 return false;
4273}
4274
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004275/**
4276 * nand_get_manufacturer_desc - Get manufacturer information from the
4277 * manufacturer ID
4278 * @id: manufacturer ID
4279 *
4280 * Returns a nand_manufacturer_desc object if the manufacturer is defined
4281 * in the NAND manufacturers database, NULL otherwise.
4282 */
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004283static const struct nand_manufacturer *nand_get_manufacturer_desc(u8 id)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004284{
4285 int i;
4286
4287 for (i = 0; nand_manuf_ids[i].id != 0x0; i++) {
4288 if (nand_manuf_ids[i].id == id)
4289 return &nand_manuf_ids[i];
4290 }
4291
4292 return NULL;
4293}
4294
Sergey Lapin3a38a552013-01-14 03:46:50 +00004295/*
4296 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004297 */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004298int nand_detect(struct nand_chip *chip, int *maf_id,
4299 int *dev_id, struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004300{
Michael Trimarchi270c1532022-07-20 18:22:07 +02004301 struct mtd_info *mtd = &chip->mtd;
Michael Trimarchi25bb1792022-07-26 18:33:11 +02004302 const struct nand_manufacturer *manufacturer_desc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004303 int busw, ret;
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004304 u8 *id_data = chip->id.data;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004305
Karl Beldanb6322fc2008-09-15 16:08:03 +02004306 /*
4307 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004308 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004309 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004310 ret = nand_reset(chip, 0);
4311 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004312 return ret;
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004313
4314 /* Select the device */
4315 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004316
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004317 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004318 ret = nand_readid_op(chip, 0, id_data, 2);
4319 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004320 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004321
4322 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004323 *maf_id = id_data[0];
4324 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004325
Sergey Lapin3a38a552013-01-14 03:46:50 +00004326 /*
4327 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004328 * interface concerns can cause random data which looks like a
4329 * possibly credible NAND flash to appear. If the two results do
4330 * not match, ignore the device completely.
4331 */
4332
Sergey Lapin3a38a552013-01-14 03:46:50 +00004333 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004334 ret = nand_readid_op(chip, 0, id_data, 8);
4335 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004336 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05004337
Christian Hitzb8a6b372011-10-12 09:32:02 +02004338 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004339 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004340 *maf_id, *dev_id, id_data[0], id_data[1]);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004341 return -ENODEV;
Scott Wood3628f002008-10-24 16:20:43 -05004342 }
4343
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004344 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
4345
4346 /* Try to identify manufacturer */
4347 manufacturer_desc = nand_get_manufacturer_desc(*maf_id);
4348 chip->manufacturer.desc = manufacturer_desc;
4349
Lei Wen75bde942011-01-06 09:48:18 +08004350 if (!type)
4351 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004352
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004353 /*
4354 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4355 * override it.
4356 * This is required to make sure initial NAND bus width set by the
4357 * NAND controller driver is coherent with the real NAND bus width
4358 * (extracted by auto-detection code).
4359 */
4360 busw = chip->options & NAND_BUSWIDTH_16;
4361
4362 /*
4363 * The flag is only set (never cleared), reset it to its default value
4364 * before starting auto-detection.
4365 */
4366 chip->options &= ~NAND_BUSWIDTH_16;
4367
Heiko Schocherf5895d12014-06-24 10:10:04 +02004368 for (; type->name != NULL; type++) {
4369 if (is_full_id_nand(type)) {
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004370 if (find_full_id_nand(mtd, chip, type))
Heiko Schocherf5895d12014-06-24 10:10:04 +02004371 goto ident_done;
4372 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004373 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004374 }
4375 }
Lei Wen75bde942011-01-06 09:48:18 +08004376
Christian Hitzb8a6b372011-10-12 09:32:02 +02004377 chip->onfi_version = 0;
4378 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004379 /* Check if the chip is ONFI compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004380 if (nand_flash_detect_onfi(mtd, chip))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004381 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004382
4383 /* Check if the chip is JEDEC compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004384 if (nand_flash_detect_jedec(mtd, chip))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004385 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004386 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004387
Christian Hitzb8a6b372011-10-12 09:32:02 +02004388 if (!type->name)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004389 return -ENODEV;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004390
William Juul52c07962007-10-31 13:53:06 +01004391 if (!mtd->name)
4392 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004393
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004394 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004395
Scott Wood52ab7ce2016-05-30 13:57:58 -05004396 if (!type->pagesize) {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004397 nand_manufacturer_detect(chip);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004398 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004399 nand_decode_id(chip, type);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004400 }
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004401
Heiko Schocherf5895d12014-06-24 10:10:04 +02004402 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004403 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004404
Christian Hitzb8a6b372011-10-12 09:32:02 +02004405ident_done:
4406
Heiko Schocherf5895d12014-06-24 10:10:04 +02004407 if (chip->options & NAND_BUSWIDTH_AUTO) {
4408 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4409 chip->options |= busw;
4410 nand_set_defaults(chip, busw);
4411 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4412 /*
4413 * Check, if buswidth is correct. Hardware drivers should set
4414 * chip correct!
4415 */
4416 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4417 *maf_id, *dev_id);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004418 pr_info("%s %s\n", manufacturer_desc->name, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004419 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004420 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4421 busw ? 16 : 8);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004422 return -EINVAL;
William Juul52c07962007-10-31 13:53:06 +01004423 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004424
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004425 nand_decode_bbm_options(mtd, chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004426
William Juul52c07962007-10-31 13:53:06 +01004427 /* Calculate the address shift from the page size */
4428 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004429 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004430 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004431
William Juul52c07962007-10-31 13:53:06 +01004432 chip->bbt_erase_shift = chip->phys_erase_shift =
4433 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004434 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004435 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004436 else {
4437 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4438 chip->chip_shift += 32 - 1;
4439 }
4440
Masahiro Yamada984926b2017-11-22 02:38:31 +09004441 if (chip->chip_shift - chip->page_shift > 16)
4442 chip->options |= NAND_ROW_ADDR_3;
4443
Christian Hitzb8a6b372011-10-12 09:32:02 +02004444 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004445 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004446
Sergey Lapin3a38a552013-01-14 03:46:50 +00004447 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004448 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4449 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004450
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004451 ret = nand_manufacturer_init(chip);
4452 if (ret)
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004453 return ret;
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004454
Heiko Schocherf5895d12014-06-24 10:10:04 +02004455 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4456 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004457
Christian Hitzb8a6b372011-10-12 09:32:02 +02004458#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004459 if (chip->onfi_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004460 pr_info("%s %s\n", manufacturer_desc->name,
4461 chip->onfi_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004462 else if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004463 pr_info("%s %s\n", manufacturer_desc->name,
4464 chip->jedec_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004465 else
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004466 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004467#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004468 if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004469 pr_info("%s %s\n", manufacturer_desc->name,
4470 chip->jedec_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004471 else
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004472 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004473
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004474 pr_info("%s %s\n", manufacturer_desc->name,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004475 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004476#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004477
Scott Wood3ea94ed2015-06-26 19:03:26 -05004478 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004479 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004480 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004481 return 0;
William Juul52c07962007-10-31 13:53:06 +01004482}
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004483EXPORT_SYMBOL(nand_detect);
William Juul52c07962007-10-31 13:53:06 +01004484
Brian Norrisba6463d2016-06-15 21:09:22 +02004485#if CONFIG_IS_ENABLED(OF_CONTROL)
Brian Norrisba6463d2016-06-15 21:09:22 +02004486
Patrice Chotardbc77af52021-09-13 16:25:53 +02004487static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004488{
4489 int ret, ecc_mode = -1, ecc_strength, ecc_step;
Linus Walleij4e8611a2023-04-07 15:40:05 +02004490 int ecc_algo = NAND_ECC_UNKNOWN;
Brian Norrisba6463d2016-06-15 21:09:22 +02004491 const char *str;
4492
Patrice Chotardbc77af52021-09-13 16:25:53 +02004493 ret = ofnode_read_s32_default(node, "nand-bus-width", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004494 if (ret == 16)
4495 chip->options |= NAND_BUSWIDTH_16;
4496
Patrice Chotardbc77af52021-09-13 16:25:53 +02004497 if (ofnode_read_bool(node, "nand-on-flash-bbt"))
Brian Norrisba6463d2016-06-15 21:09:22 +02004498 chip->bbt_options |= NAND_BBT_USE_FLASH;
4499
Patrice Chotardbc77af52021-09-13 16:25:53 +02004500 str = ofnode_read_string(node, "nand-ecc-mode");
Brian Norrisba6463d2016-06-15 21:09:22 +02004501 if (str) {
4502 if (!strcmp(str, "none"))
4503 ecc_mode = NAND_ECC_NONE;
4504 else if (!strcmp(str, "soft"))
4505 ecc_mode = NAND_ECC_SOFT;
4506 else if (!strcmp(str, "hw"))
4507 ecc_mode = NAND_ECC_HW;
4508 else if (!strcmp(str, "hw_syndrome"))
4509 ecc_mode = NAND_ECC_HW_SYNDROME;
4510 else if (!strcmp(str, "hw_oob_first"))
4511 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4512 else if (!strcmp(str, "soft_bch"))
4513 ecc_mode = NAND_ECC_SOFT_BCH;
4514 }
4515
Linus Walleij4e8611a2023-04-07 15:40:05 +02004516 str = ofnode_read_string(node, "nand-ecc-algo");
4517 if (str) {
4518 /*
4519 * If we are in NAND_ECC_SOFT mode, just alter the
4520 * soft mode to BCH here. No change of algorithm.
4521 */
4522 if (ecc_mode == NAND_ECC_SOFT) {
4523 if (!strcmp(str, "bch"))
4524 ecc_mode = NAND_ECC_SOFT_BCH;
4525 } else {
4526 if (!strcmp(str, "bch")) {
4527 ecc_algo = NAND_ECC_BCH;
4528 } else if (!strcmp(str, "hamming")) {
4529 ecc_algo = NAND_ECC_HAMMING;
4530 }
4531 }
Pali Rohár44acf8a2022-04-04 18:17:21 +02004532 }
4533
Patrice Chotardbc77af52021-09-13 16:25:53 +02004534 ecc_strength = ofnode_read_s32_default(node,
4535 "nand-ecc-strength", -1);
4536 ecc_step = ofnode_read_s32_default(node,
4537 "nand-ecc-step-size", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004538
4539 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4540 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4541 pr_err("must set both strength and step size in DT\n");
4542 return -EINVAL;
4543 }
4544
Linus Walleij4e8611a2023-04-07 15:40:05 +02004545 /*
4546 * Chip drivers may have assigned default algorithms here,
4547 * onlt override it if we have found something explicitly
4548 * specified in the device tree.
4549 */
4550 if (ecc_algo != NAND_ECC_UNKNOWN)
4551 chip->ecc.algo = ecc_algo;
4552
Brian Norrisba6463d2016-06-15 21:09:22 +02004553 if (ecc_mode >= 0)
4554 chip->ecc.mode = ecc_mode;
4555
4556 if (ecc_strength >= 0)
4557 chip->ecc.strength = ecc_strength;
4558
4559 if (ecc_step > 0)
4560 chip->ecc.size = ecc_step;
4561
Patrice Chotardbc77af52021-09-13 16:25:53 +02004562 if (ofnode_read_bool(node, "nand-ecc-maximize"))
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004563 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4564
Brian Norrisba6463d2016-06-15 21:09:22 +02004565 return 0;
4566}
4567#else
Patrice Chotardbc77af52021-09-13 16:25:53 +02004568static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004569{
4570 return 0;
4571}
4572#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4573
William Juul52c07962007-10-31 13:53:06 +01004574/**
4575 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004576 * @mtd: MTD device structure
4577 * @maxchips: number of chips to scan for
4578 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004579 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004580 * This is the first phase of the normal nand_scan() function. It reads the
4581 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004582 *
William Juul52c07962007-10-31 13:53:06 +01004583 */
Lei Wen75bde942011-01-06 09:48:18 +08004584int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004585 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004586{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004587 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004588 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba6463d2016-06-15 21:09:22 +02004589 int ret;
4590
Patrice Chotardbc77af52021-09-13 16:25:53 +02004591 if (ofnode_valid(chip->flash_node)) {
Brian Norrisba6463d2016-06-15 21:09:22 +02004592 ret = nand_dt_init(mtd, chip, chip->flash_node);
4593 if (ret)
4594 return ret;
4595 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004596
William Juul52c07962007-10-31 13:53:06 +01004597 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004598 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004599
4600 /* Read the flash type */
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004601 ret = nand_detect(chip, &nand_maf_id, &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004602
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004603 if (ret) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004604 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4605 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004606 chip->select_chip(mtd, -1);
Michael Trimarchi4d4862d92022-07-25 10:06:06 +02004607 return ret;
William Juul52c07962007-10-31 13:53:06 +01004608 }
4609
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004610 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004611 ret = nand_init_data_interface(chip);
4612 if (ret)
4613 return ret;
4614
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004615 /*
4616 * Setup the data interface correctly on the chip and controller side.
4617 * This explicit call to nand_setup_data_interface() is only required
4618 * for the first die, because nand_reset() has been called before
4619 * ->data_interface and ->default_onfi_timing_mode were set.
4620 * For the other dies, nand_reset() will automatically switch to the
4621 * best mode for us.
4622 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004623 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004624 if (ret)
4625 return ret;
4626
Heiko Schocherf5895d12014-06-24 10:10:04 +02004627 chip->select_chip(mtd, -1);
4628
William Juul52c07962007-10-31 13:53:06 +01004629 /* Check for a chip array */
4630 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004631 u8 id[2];
4632
Michael Trimarchif20a6f02022-07-25 10:18:51 +02004633 /* See comment in nand_detect for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004634 nand_reset(chip, i);
4635
4636 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004637 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004638 nand_readid_op(chip, 0, id, sizeof(id));
4639
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004640 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004641 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004642 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004643 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004644 }
4645 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004646 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004647
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004648#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004649 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004650 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004651#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004652
William Juul52c07962007-10-31 13:53:06 +01004653 /* Store the number of chips and calc total size for mtd */
4654 chip->numchips = i;
4655 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004656
William Juul52c07962007-10-31 13:53:06 +01004657 return 0;
4658}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004659EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004660
Masahiro Yamada820eb482017-11-22 02:38:29 +09004661/**
4662 * nand_check_ecc_caps - check the sanity of preset ECC settings
4663 * @chip: nand chip info structure
4664 * @caps: ECC caps info structure
4665 * @oobavail: OOB size that the ECC engine can use
4666 *
4667 * When ECC step size and strength are already set, check if they are supported
4668 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4669 * On success, the calculated ECC bytes is set.
4670 */
4671int nand_check_ecc_caps(struct nand_chip *chip,
4672 const struct nand_ecc_caps *caps, int oobavail)
4673{
4674 struct mtd_info *mtd = nand_to_mtd(chip);
4675 const struct nand_ecc_step_info *stepinfo;
4676 int preset_step = chip->ecc.size;
4677 int preset_strength = chip->ecc.strength;
4678 int nsteps, ecc_bytes;
4679 int i, j;
4680
4681 if (WARN_ON(oobavail < 0))
4682 return -EINVAL;
4683
4684 if (!preset_step || !preset_strength)
4685 return -ENODATA;
4686
4687 nsteps = mtd->writesize / preset_step;
4688
4689 for (i = 0; i < caps->nstepinfos; i++) {
4690 stepinfo = &caps->stepinfos[i];
4691
4692 if (stepinfo->stepsize != preset_step)
4693 continue;
4694
4695 for (j = 0; j < stepinfo->nstrengths; j++) {
4696 if (stepinfo->strengths[j] != preset_strength)
4697 continue;
4698
4699 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4700 preset_strength);
4701 if (WARN_ON_ONCE(ecc_bytes < 0))
4702 return ecc_bytes;
4703
4704 if (ecc_bytes * nsteps > oobavail) {
4705 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4706 preset_step, preset_strength);
4707 return -ENOSPC;
4708 }
4709
4710 chip->ecc.bytes = ecc_bytes;
4711
4712 return 0;
4713 }
4714 }
4715
4716 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4717 preset_step, preset_strength);
4718
4719 return -ENOTSUPP;
4720}
4721EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4722
4723/**
4724 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4725 * @chip: nand chip info structure
4726 * @caps: ECC engine caps info structure
4727 * @oobavail: OOB size that the ECC engine can use
4728 *
4729 * If a chip's ECC requirement is provided, try to meet it with the least
4730 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4731 * On success, the chosen ECC settings are set.
4732 */
4733int nand_match_ecc_req(struct nand_chip *chip,
4734 const struct nand_ecc_caps *caps, int oobavail)
4735{
4736 struct mtd_info *mtd = nand_to_mtd(chip);
4737 const struct nand_ecc_step_info *stepinfo;
4738 int req_step = chip->ecc_step_ds;
4739 int req_strength = chip->ecc_strength_ds;
4740 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4741 int best_step, best_strength, best_ecc_bytes;
4742 int best_ecc_bytes_total = INT_MAX;
4743 int i, j;
4744
4745 if (WARN_ON(oobavail < 0))
4746 return -EINVAL;
4747
4748 /* No information provided by the NAND chip */
4749 if (!req_step || !req_strength)
4750 return -ENOTSUPP;
4751
4752 /* number of correctable bits the chip requires in a page */
4753 req_corr = mtd->writesize / req_step * req_strength;
4754
4755 for (i = 0; i < caps->nstepinfos; i++) {
4756 stepinfo = &caps->stepinfos[i];
4757 step_size = stepinfo->stepsize;
4758
4759 for (j = 0; j < stepinfo->nstrengths; j++) {
4760 strength = stepinfo->strengths[j];
4761
4762 /*
4763 * If both step size and strength are smaller than the
4764 * chip's requirement, it is not easy to compare the
4765 * resulted reliability.
4766 */
4767 if (step_size < req_step && strength < req_strength)
4768 continue;
4769
4770 if (mtd->writesize % step_size)
4771 continue;
4772
4773 nsteps = mtd->writesize / step_size;
4774
4775 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4776 if (WARN_ON_ONCE(ecc_bytes < 0))
4777 continue;
4778 ecc_bytes_total = ecc_bytes * nsteps;
4779
4780 if (ecc_bytes_total > oobavail ||
4781 strength * nsteps < req_corr)
4782 continue;
4783
4784 /*
4785 * We assume the best is to meet the chip's requrement
4786 * with the least number of ECC bytes.
4787 */
4788 if (ecc_bytes_total < best_ecc_bytes_total) {
4789 best_ecc_bytes_total = ecc_bytes_total;
4790 best_step = step_size;
4791 best_strength = strength;
4792 best_ecc_bytes = ecc_bytes;
4793 }
4794 }
4795 }
4796
4797 if (best_ecc_bytes_total == INT_MAX)
4798 return -ENOTSUPP;
4799
4800 chip->ecc.size = best_step;
4801 chip->ecc.strength = best_strength;
4802 chip->ecc.bytes = best_ecc_bytes;
4803
4804 return 0;
4805}
4806EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4807
4808/**
4809 * nand_maximize_ecc - choose the max ECC strength available
4810 * @chip: nand chip info structure
4811 * @caps: ECC engine caps info structure
4812 * @oobavail: OOB size that the ECC engine can use
4813 *
4814 * Choose the max ECC strength that is supported on the controller, and can fit
4815 * within the chip's OOB. On success, the chosen ECC settings are set.
4816 */
4817int nand_maximize_ecc(struct nand_chip *chip,
4818 const struct nand_ecc_caps *caps, int oobavail)
4819{
4820 struct mtd_info *mtd = nand_to_mtd(chip);
4821 const struct nand_ecc_step_info *stepinfo;
4822 int step_size, strength, nsteps, ecc_bytes, corr;
4823 int best_corr = 0;
4824 int best_step = 0;
4825 int best_strength, best_ecc_bytes;
4826 int i, j;
4827
4828 if (WARN_ON(oobavail < 0))
4829 return -EINVAL;
4830
4831 for (i = 0; i < caps->nstepinfos; i++) {
4832 stepinfo = &caps->stepinfos[i];
4833 step_size = stepinfo->stepsize;
4834
4835 /* If chip->ecc.size is already set, respect it */
4836 if (chip->ecc.size && step_size != chip->ecc.size)
4837 continue;
4838
4839 for (j = 0; j < stepinfo->nstrengths; j++) {
4840 strength = stepinfo->strengths[j];
4841
4842 if (mtd->writesize % step_size)
4843 continue;
4844
4845 nsteps = mtd->writesize / step_size;
4846
4847 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4848 if (WARN_ON_ONCE(ecc_bytes < 0))
4849 continue;
4850
4851 if (ecc_bytes * nsteps > oobavail)
4852 continue;
4853
4854 corr = strength * nsteps;
4855
4856 /*
4857 * If the number of correctable bits is the same,
4858 * bigger step_size has more reliability.
4859 */
4860 if (corr > best_corr ||
4861 (corr == best_corr && step_size > best_step)) {
4862 best_corr = corr;
4863 best_step = step_size;
4864 best_strength = strength;
4865 best_ecc_bytes = ecc_bytes;
4866 }
4867 }
4868 }
4869
4870 if (!best_corr)
4871 return -ENOTSUPP;
4872
4873 chip->ecc.size = best_step;
4874 chip->ecc.strength = best_strength;
4875 chip->ecc.bytes = best_ecc_bytes;
4876
4877 return 0;
4878}
4879EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4880
Scott Wood3ea94ed2015-06-26 19:03:26 -05004881/*
4882 * Check if the chip configuration meet the datasheet requirements.
4883
4884 * If our configuration corrects A bits per B bytes and the minimum
4885 * required correction level is X bits per Y bytes, then we must ensure
4886 * both of the following are true:
4887 *
4888 * (1) A / B >= X / Y
4889 * (2) A >= X
4890 *
4891 * Requirement (1) ensures we can correct for the required bitflip density.
4892 * Requirement (2) ensures we can correct even when all bitflips are clumped
4893 * in the same sector.
4894 */
4895static bool nand_ecc_strength_good(struct mtd_info *mtd)
4896{
Scott Wood17fed142016-05-30 13:57:56 -05004897 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004898 struct nand_ecc_ctrl *ecc = &chip->ecc;
4899 int corr, ds_corr;
4900
4901 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4902 /* Not enough information */
4903 return true;
4904
4905 /*
4906 * We get the number of corrected bits per page to compare
4907 * the correction density.
4908 */
4909 corr = (mtd->writesize * ecc->strength) / ecc->size;
4910 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4911
4912 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4913}
William Juul52c07962007-10-31 13:53:06 +01004914
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004915static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4916{
4917 struct nand_ecc_ctrl *ecc = &chip->ecc;
4918
4919 if (nand_standard_page_accessors(ecc))
4920 return false;
4921
4922 /*
4923 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4924 * controller driver implements all the page accessors because
4925 * default helpers are not suitable when the core does not
4926 * send the READ0/PAGEPROG commands.
4927 */
4928 return (!ecc->read_page || !ecc->write_page ||
4929 !ecc->read_page_raw || !ecc->write_page_raw ||
4930 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4931 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4932 ecc->hwctl && ecc->calculate));
4933}
4934
William Juul52c07962007-10-31 13:53:06 +01004935/**
4936 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004937 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004938 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004939 * This is the second phase of the normal nand_scan() function. It fills out
4940 * all the uninitialized function pointers with the defaults and scans for a
4941 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004942 */
4943int nand_scan_tail(struct mtd_info *mtd)
4944{
4945 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004946 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004947 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004948 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004949
Sergey Lapin3a38a552013-01-14 03:46:50 +00004950 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4951 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4952 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4953
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004954 if (invalid_ecc_page_accessors(chip)) {
4955 pr_err("Invalid ECC page accessors setup\n");
4956 return -EINVAL;
4957 }
4958
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004959 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004960 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004961 chip->buffers = nbuf;
4962 } else {
4963 if (!chip->buffers)
4964 return -ENOMEM;
4965 }
William Juul52c07962007-10-31 13:53:06 +01004966
4967 /* Set the internal oob buffer location, just after the page data */
4968 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4969
4970 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004971 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004972 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004973 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004974 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02004975#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004976 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004977 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004978 break;
4979 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004980 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004981 break;
4982 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004983 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004984 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004985 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004986 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004987 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01004988#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004989 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004990 pr_warn("No oob scheme defined for oobsize %d\n",
4991 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004992 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004993 }
4994 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004995
William Juul52c07962007-10-31 13:53:06 +01004996 if (!chip->write_page)
4997 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004998
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004999 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005000 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01005001 * selected and we have 256 byte pagesize fallback to software ECC
5002 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005003
Heiko Schocherf5895d12014-06-24 10:10:04 +02005004 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005005 case NAND_ECC_HW_OOB_FIRST:
5006 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005007 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005008 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005009 BUG();
5010 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005011 if (!ecc->read_page)
5012 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005013
William Juul52c07962007-10-31 13:53:06 +01005014 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005015 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005016 if (!ecc->read_page)
5017 ecc->read_page = nand_read_page_hwecc;
5018 if (!ecc->write_page)
5019 ecc->write_page = nand_write_page_hwecc;
5020 if (!ecc->read_page_raw)
5021 ecc->read_page_raw = nand_read_page_raw;
5022 if (!ecc->write_page_raw)
5023 ecc->write_page_raw = nand_write_page_raw;
5024 if (!ecc->read_oob)
5025 ecc->read_oob = nand_read_oob_std;
5026 if (!ecc->write_oob)
5027 ecc->write_oob = nand_write_oob_std;
5028 if (!ecc->read_subpage)
5029 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05005030 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02005031 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005032
William Juul52c07962007-10-31 13:53:06 +01005033 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005034 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5035 (!ecc->read_page ||
5036 ecc->read_page == nand_read_page_hwecc ||
5037 !ecc->write_page ||
5038 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005039 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005040 BUG();
5041 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005042 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005043 if (!ecc->read_page)
5044 ecc->read_page = nand_read_page_syndrome;
5045 if (!ecc->write_page)
5046 ecc->write_page = nand_write_page_syndrome;
5047 if (!ecc->read_page_raw)
5048 ecc->read_page_raw = nand_read_page_raw_syndrome;
5049 if (!ecc->write_page_raw)
5050 ecc->write_page_raw = nand_write_page_raw_syndrome;
5051 if (!ecc->read_oob)
5052 ecc->read_oob = nand_read_oob_syndrome;
5053 if (!ecc->write_oob)
5054 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005055
Heiko Schocherf5895d12014-06-24 10:10:04 +02005056 if (mtd->writesize >= ecc->size) {
5057 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005058 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5059 BUG();
5060 }
William Juul52c07962007-10-31 13:53:06 +01005061 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005062 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005063 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5064 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005065 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005066
William Juul52c07962007-10-31 13:53:06 +01005067 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005068 ecc->calculate = nand_calculate_ecc;
5069 ecc->correct = nand_correct_data;
5070 ecc->read_page = nand_read_page_swecc;
5071 ecc->read_subpage = nand_read_subpage;
5072 ecc->write_page = nand_write_page_swecc;
5073 ecc->read_page_raw = nand_read_page_raw;
5074 ecc->write_page_raw = nand_write_page_raw;
5075 ecc->read_oob = nand_read_oob_std;
5076 ecc->write_oob = nand_write_oob_std;
5077 if (!ecc->size)
5078 ecc->size = 256;
5079 ecc->bytes = 3;
5080 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005081 break;
5082
Christian Hitz55f7bca2011-10-12 09:31:59 +02005083 case NAND_ECC_SOFT_BCH:
5084 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005085 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005086 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005087 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005088 ecc->calculate = nand_bch_calculate_ecc;
5089 ecc->correct = nand_bch_correct_data;
5090 ecc->read_page = nand_read_page_swecc;
5091 ecc->read_subpage = nand_read_subpage;
5092 ecc->write_page = nand_write_page_swecc;
5093 ecc->read_page_raw = nand_read_page_raw;
5094 ecc->write_page_raw = nand_write_page_raw;
5095 ecc->read_oob = nand_read_oob_std;
5096 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005097 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005098 * Board driver should supply ecc.size and ecc.strength values
5099 * to select how many bits are correctable. Otherwise, default
5100 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005101 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005102 if (!ecc->size && (mtd->oobsize >= 64)) {
5103 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005104 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005105 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005106
5107 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005108 ecc->bytes = 0;
5109 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005110 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005111 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005112 BUG();
5113 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005114 break;
5115
William Juul52c07962007-10-31 13:53:06 +01005116 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005117 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005118 ecc->read_page = nand_read_page_raw;
5119 ecc->write_page = nand_write_page_raw;
5120 ecc->read_oob = nand_read_oob_std;
5121 ecc->read_page_raw = nand_read_page_raw;
5122 ecc->write_page_raw = nand_write_page_raw;
5123 ecc->write_oob = nand_write_oob_std;
5124 ecc->size = mtd->writesize;
5125 ecc->bytes = 0;
5126 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005127 break;
5128
5129 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005130 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005131 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005132 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005133
Sergey Lapin3a38a552013-01-14 03:46:50 +00005134 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005135 if (!ecc->read_oob_raw)
5136 ecc->read_oob_raw = ecc->read_oob;
5137 if (!ecc->write_oob_raw)
5138 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005139
William Juul52c07962007-10-31 13:53:06 +01005140 /*
5141 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005142 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005143 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005144 mtd->oobavail = 0;
5145 if (ecc->layout) {
5146 for (i = 0; ecc->layout->oobfree[i].length; i++)
5147 mtd->oobavail += ecc->layout->oobfree[i].length;
5148 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005149
Scott Wood3ea94ed2015-06-26 19:03:26 -05005150 /* ECC sanity check: warn if it's too weak */
5151 if (!nand_ecc_strength_good(mtd))
5152 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5153 mtd->name);
5154
William Juul52c07962007-10-31 13:53:06 +01005155 /*
5156 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005157 * mode.
William Juul52c07962007-10-31 13:53:06 +01005158 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005159 ecc->steps = mtd->writesize / ecc->size;
5160 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005161 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005162 BUG();
5163 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005164 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005165
Sergey Lapin3a38a552013-01-14 03:46:50 +00005166 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005167 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5168 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005169 case 2:
5170 mtd->subpage_sft = 1;
5171 break;
5172 case 4:
5173 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005174 case 16:
William Juul52c07962007-10-31 13:53:06 +01005175 mtd->subpage_sft = 2;
5176 break;
5177 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005178 }
William Juul52c07962007-10-31 13:53:06 +01005179 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005180
William Juul52c07962007-10-31 13:53:06 +01005181 /* Initialize state */
5182 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005183
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005184 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005185 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005186
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005187 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005188 switch (ecc->mode) {
5189 case NAND_ECC_SOFT:
5190 case NAND_ECC_SOFT_BCH:
5191 if (chip->page_shift > 9)
5192 chip->options |= NAND_SUBPAGE_READ;
5193 break;
5194
5195 default:
5196 break;
5197 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005198
Patrice Chotardbee32872022-03-21 09:13:36 +01005199 mtd->flash_node = chip->flash_node;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005200 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005201 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005202 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5203 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005204 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005205 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005206 mtd->_read_oob = nand_read_oob;
5207 mtd->_write_oob = nand_write_oob;
5208 mtd->_sync = nand_sync;
5209 mtd->_lock = NULL;
5210 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005211 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005212 mtd->_block_isbad = nand_block_isbad;
5213 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005214 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005215
Sergey Lapin3a38a552013-01-14 03:46:50 +00005216 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005217 mtd->ecclayout = ecc->layout;
5218 mtd->ecc_strength = ecc->strength;
5219 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005220 /*
5221 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5222 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5223 * properly set.
5224 */
5225 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005226 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005227
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005228 return 0;
William Juul52c07962007-10-31 13:53:06 +01005229}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005230EXPORT_SYMBOL(nand_scan_tail);
5231
William Juul52c07962007-10-31 13:53:06 +01005232/**
5233 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005234 * @mtd: MTD device structure
5235 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005236 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005237 * This fills out all the uninitialized function pointers with the defaults.
5238 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005239 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005240 */
5241int nand_scan(struct mtd_info *mtd, int maxchips)
5242{
5243 int ret;
5244
Lei Wen75bde942011-01-06 09:48:18 +08005245 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005246 if (!ret)
5247 ret = nand_scan_tail(mtd);
5248 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005249}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005250EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005251
Heiko Schocherf5895d12014-06-24 10:10:04 +02005252MODULE_LICENSE("GPL");
5253MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5254MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5255MODULE_DESCRIPTION("Generic NAND flash driver code");