blob: 5698c1e6a229715a86bd95f899c8d7249b1b7511 [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02006 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -05007 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020011 *
William Juul52c07962007-10-31 13:53:06 +010012 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020013 * David Woodhouse for adding multichip support
14 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
William Juul52c07962007-10-31 13:53:06 +010018 * TODO:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapin3a38a552013-01-14 03:46:50 +000021 * if we have HW ECC support.
Scott Wood3628f002008-10-24 16:20:43 -050022 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020023 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Heiko Schocherf5895d12014-06-24 10:10:04 +020030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060032#include <log.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020033#include <malloc.h>
34#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070035#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060036#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060037#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060038#include <linux/delay.h>
William Juul52c07962007-10-31 13:53:06 +010039#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000040#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020041#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090042#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020043#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020044#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020045#ifdef CONFIG_MTD_PARTITIONS
46#include <linux/mtd/partitions.h>
47#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020048#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090049#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020050
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051/* Define default oob placement schemes for large and small page devices */
Gregory CLEMENTe5b96312019-04-17 11:22:05 +020052#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
William Juul52c07962007-10-31 13:53:06 +010053static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020054 .eccbytes = 3,
55 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010056 .oobfree = {
57 {.offset = 3,
58 .length = 2},
59 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020060 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020061};
62
William Juul52c07962007-10-31 13:53:06 +010063static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020064 .eccbytes = 6,
65 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010066 .oobfree = {
67 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020068 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020069};
70
William Juul52c07962007-10-31 13:53:06 +010071static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020072 .eccbytes = 24,
73 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010074 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
77 .oobfree = {
78 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020079 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020080};
81
William Juul52c07962007-10-31 13:53:06 +010082static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020083 .eccbytes = 48,
84 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020085 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010088 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
91 .oobfree = {
92 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020093 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094};
Stefan Agnerbd186142018-12-06 14:57:09 +010095#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020096
Heiko Schocherf5895d12014-06-24 10:10:04 +020097static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010098
99static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
100 struct mtd_oob_ops *ops);
101
Heiko Schocherf5895d12014-06-24 10:10:04 +0200102/*
103 * For devices which display every fart in the system on a separate LED. Is
104 * compiled away when LED support is disabled.
105 */
106DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200107
Christian Hitzb8a6b372011-10-12 09:32:02 +0200108static int check_offs_len(struct mtd_info *mtd,
109 loff_t ofs, uint64_t len)
110{
Scott Wood17fed142016-05-30 13:57:56 -0500111 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200112 int ret = 0;
113
114 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200115 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
116 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200117 ret = -EINVAL;
118 }
119
120 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200121 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
122 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200123 ret = -EINVAL;
124 }
125
Christian Hitzb8a6b372011-10-12 09:32:02 +0200126 return ret;
127}
128
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200129/**
130 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000131 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200132 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200133 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200134 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200135static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100136{
Scott Wood17fed142016-05-30 13:57:56 -0500137 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200138
139 /* De-select the NAND device */
140 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100141}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200142
143/**
144 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000145 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200147 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200148 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000149uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200150{
Scott Wood17fed142016-05-30 13:57:56 -0500151 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100152 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200153}
154
155/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200156 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000157 * @mtd: MTD device structure
158 *
159 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200161 */
William Juul52c07962007-10-31 13:53:06 +0100162static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200163{
Scott Wood17fed142016-05-30 13:57:56 -0500164 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100165 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200166}
167
168/**
169 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000170 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200171 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000172 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200173 */
174static u16 nand_read_word(struct mtd_info *mtd)
175{
Scott Wood17fed142016-05-30 13:57:56 -0500176 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100177 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200178}
179
180/**
181 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000182 * @mtd: MTD device structure
183 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200184 *
185 * Default select function for 1 chip devices.
186 */
William Juul52c07962007-10-31 13:53:06 +0100187static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200188{
Scott Wood17fed142016-05-30 13:57:56 -0500189 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100190
191 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192 case -1:
William Juul52c07962007-10-31 13:53:06 +0100193 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200194 break;
195 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200196 break;
197
198 default:
199 BUG();
200 }
201}
202
203/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200204 * nand_write_byte - [DEFAULT] write single byte to chip
205 * @mtd: MTD device structure
206 * @byte: value to write
207 *
208 * Default function to write a byte to I/O[7:0]
209 */
210static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
211{
Scott Wood17fed142016-05-30 13:57:56 -0500212 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200213
214 chip->write_buf(mtd, &byte, 1);
215}
216
217/**
218 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
219 * @mtd: MTD device structure
220 * @byte: value to write
221 *
222 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
223 */
224static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
225{
Scott Wood17fed142016-05-30 13:57:56 -0500226 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200227 uint16_t word = byte;
228
229 /*
230 * It's not entirely clear what should happen to I/O[15:8] when writing
231 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
232 *
233 * When the host supports a 16-bit bus width, only data is
234 * transferred at the 16-bit width. All address and command line
235 * transfers shall use only the lower 8-bits of the data bus. During
236 * command transfers, the host may place any value on the upper
237 * 8-bits of the data bus. During address transfers, the host shall
238 * set the upper 8-bits of the data bus to 00h.
239 *
240 * One user of the write_byte callback is nand_onfi_set_features. The
241 * four parameters are specified to be written to I/O[7:0], but this is
242 * neither an address nor a command transfer. Let's assume a 0 on the
243 * upper I/O lines is OK.
244 */
245 chip->write_buf(mtd, (uint8_t *)&word, 2);
246}
247
Heiko Schocherf5895d12014-06-24 10:10:04 +0200248static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
249{
250 int i;
251
252 for (i = 0; i < len; i++)
253 writeb(buf[i], addr);
254}
255static void ioread8_rep(void *addr, uint8_t *buf, int len)
256{
257 int i;
258
259 for (i = 0; i < len; i++)
260 buf[i] = readb(addr);
261}
262
263static void ioread16_rep(void *addr, void *buf, int len)
264{
265 int i;
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200266 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200267
Heiko Schocherf5895d12014-06-24 10:10:04 +0200268 for (i = 0; i < len; i++)
269 p[i] = readw(addr);
270}
271
272static void iowrite16_rep(void *addr, void *buf, int len)
273{
274 int i;
275 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200276
277 for (i = 0; i < len; i++)
278 writew(p[i], addr);
279}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200280
281/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200282 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000283 * @mtd: MTD device structure
284 * @buf: data buffer
285 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000287 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000289void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200290{
Scott Wood17fed142016-05-30 13:57:56 -0500291 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200292
Heiko Schocherf5895d12014-06-24 10:10:04 +0200293 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200294}
295
296/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200297 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000298 * @mtd: MTD device structure
299 * @buf: buffer to store date
300 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200301 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000302 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200303 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400304void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305{
Scott Wood17fed142016-05-30 13:57:56 -0500306 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307
Heiko Schocherf5895d12014-06-24 10:10:04 +0200308 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200309}
310
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200311/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200312 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000313 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200314 * @buf: data buffer
315 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200317 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200318 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200319void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200320{
Scott Wood17fed142016-05-30 13:57:56 -0500321 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200322 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200323
Heiko Schocherf5895d12014-06-24 10:10:04 +0200324 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200325}
326
327/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200328 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000329 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200330 * @buf: buffer to store date
331 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200333 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200335void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200336{
Scott Wood17fed142016-05-30 13:57:56 -0500337 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200338 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200339
Heiko Schocherf5895d12014-06-24 10:10:04 +0200340 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200341}
342
343/**
344 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000345 * @mtd: MTD device structure
346 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200347 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200348 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200349 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500350static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500352 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500353 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200354 u16 bad;
355
Sergey Lapin3a38a552013-01-14 03:46:50 +0000356 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200357 ofs += mtd->erasesize - mtd->writesize;
358
William Juul52c07962007-10-31 13:53:06 +0100359 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200360
Sergey Lapin3a38a552013-01-14 03:46:50 +0000361 do {
362 if (chip->options & NAND_BUSWIDTH_16) {
363 chip->cmdfunc(mtd, NAND_CMD_READOOB,
364 chip->badblockpos & 0xFE, page);
365 bad = cpu_to_le16(chip->read_word(mtd));
366 if (chip->badblockpos & 0x1)
367 bad >>= 8;
368 else
369 bad &= 0xFF;
370 } else {
371 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
372 page);
373 bad = chip->read_byte(mtd);
374 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200375
Sergey Lapin3a38a552013-01-14 03:46:50 +0000376 if (likely(chip->badblockbits == 8))
377 res = bad != 0xFF;
378 else
379 res = hweight8(bad) < chip->badblockbits;
380 ofs += mtd->writesize;
381 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
382 i++;
383 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200384
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200385 return res;
386}
387
388/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200389 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000390 * @mtd: MTD device structure
391 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200392 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000393 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200394 * specific driver. It provides the details for writing a bad block marker to a
395 * block.
396 */
397static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398{
Scott Wood17fed142016-05-30 13:57:56 -0500399 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200400 struct mtd_oob_ops ops;
401 uint8_t buf[2] = { 0, 0 };
402 int ret = 0, res, i = 0;
403
Scott Wood3ea94ed2015-06-26 19:03:26 -0500404 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200405 ops.oobbuf = buf;
406 ops.ooboffs = chip->badblockpos;
407 if (chip->options & NAND_BUSWIDTH_16) {
408 ops.ooboffs &= ~0x01;
409 ops.len = ops.ooblen = 2;
410 } else {
411 ops.len = ops.ooblen = 1;
412 }
413 ops.mode = MTD_OPS_PLACE_OOB;
414
415 /* Write to first/last page(s) if necessary */
416 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
417 ofs += mtd->erasesize - mtd->writesize;
418 do {
419 res = nand_do_write_oob(mtd, ofs, &ops);
420 if (!ret)
421 ret = res;
422
423 i++;
424 ofs += mtd->writesize;
425 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
426
427 return ret;
428}
429
430/**
431 * nand_block_markbad_lowlevel - mark a block bad
432 * @mtd: MTD device structure
433 * @ofs: offset from device start
434 *
435 * This function performs the generic NAND bad block marking steps (i.e., bad
436 * block table(s) and/or marker(s)). We only allow the hardware driver to
437 * specify how to write bad block markers to OOB (chip->block_markbad).
438 *
439 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000440 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200441 * (2) write bad block marker to OOB area of affected block (unless flag
442 * NAND_BBT_NO_OOB_BBM is present)
443 * (3) update the BBT
444 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000445 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200446*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200447static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200448{
Scott Wood17fed142016-05-30 13:57:56 -0500449 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200450 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200451
Heiko Schocherf5895d12014-06-24 10:10:04 +0200452 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000453 struct erase_info einfo;
454
455 /* Attempt erase before marking OOB */
456 memset(&einfo, 0, sizeof(einfo));
457 einfo.mtd = mtd;
458 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200459 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000460 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200461
Heiko Schocherf5895d12014-06-24 10:10:04 +0200462 /* Write bad block marker to OOB */
463 nand_get_device(mtd, FL_WRITING);
464 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500465 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100466 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000467
Heiko Schocherf5895d12014-06-24 10:10:04 +0200468 /* Mark block bad in BBT */
469 if (chip->bbt) {
470 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000471 if (!ret)
472 ret = res;
473 }
474
William Juul52c07962007-10-31 13:53:06 +0100475 if (!ret)
476 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500477
William Juul52c07962007-10-31 13:53:06 +0100478 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200479}
480
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200481/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200482 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000483 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200484 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000485 * Check, if the device is write protected. The function expects, that the
486 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200487 */
William Juul52c07962007-10-31 13:53:06 +0100488static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200489{
Scott Wood17fed142016-05-30 13:57:56 -0500490 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100491 u8 status;
492 int ret;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200493
Sergey Lapin3a38a552013-01-14 03:46:50 +0000494 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200495 if (chip->options & NAND_BROKEN_XD)
496 return 0;
497
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200498 /* Check the WP bit */
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100499 ret = nand_status_op(chip, &status);
500 if (ret)
501 return ret;
502
503 return status & NAND_STATUS_WP ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200504}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100505
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200506/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500507 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000508 * @mtd: MTD device structure
509 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300510 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500511 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300512 */
513static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
514{
Scott Wood17fed142016-05-30 13:57:56 -0500515 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300516
517 if (!chip->bbt)
518 return 0;
519 /* Return info from the table */
520 return nand_isreserved_bbt(mtd, ofs);
521}
522
523/**
524 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
525 * @mtd: MTD device structure
526 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000527 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200528 *
529 * Check, if the block is bad. Either by reading the bad block table or
530 * calling of the scan function.
531 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500532static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200533{
Scott Wood17fed142016-05-30 13:57:56 -0500534 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200535
Masahiro Yamada8d100542014-12-26 22:20:58 +0900536 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
537 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200538 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900539 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200540 }
541
William Juul52c07962007-10-31 13:53:06 +0100542 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500543 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200544
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200545 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100546 return nand_isbad_bbt(mtd, ofs, allowbbt);
547}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200548
Scott Wood52ab7ce2016-05-30 13:57:58 -0500549/**
550 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
551 * @mtd: MTD device structure
552 *
553 * Wait for the ready pin after a command, and warn if a timeout occurs.
554 */
William Juul52c07962007-10-31 13:53:06 +0100555void nand_wait_ready(struct mtd_info *mtd)
556{
Scott Wood17fed142016-05-30 13:57:56 -0500557 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500558 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000559 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100560
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000561 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000562 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000563 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100564 if (chip->dev_ready)
565 if (chip->dev_ready(mtd))
566 break;
567 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500568
569 if (!chip->dev_ready(mtd))
570 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200571}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200572EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200573
574/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500575 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
576 * @mtd: MTD device structure
577 * @timeo: Timeout in ms
578 *
579 * Wait for status ready (i.e. command done) or timeout.
580 */
581static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
582{
Scott Wood17fed142016-05-30 13:57:56 -0500583 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500584 u32 time_start;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100585 int ret;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500586
587 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
588 time_start = get_timer(0);
589 while (get_timer(time_start) < timeo) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100590 u8 status;
591
592 ret = nand_read_data_op(chip, &status, sizeof(status), true);
593 if (ret)
594 return;
595
596 if (status & NAND_STATUS_READY)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500597 break;
598 WATCHDOG_RESET();
599 }
600};
601
602/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000604 * @mtd: MTD device structure
605 * @command: the command to be sent
606 * @column: the column address for this command, -1 if none
607 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200608 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000609 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200610 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 */
William Juul52c07962007-10-31 13:53:06 +0100612static void nand_command(struct mtd_info *mtd, unsigned int command,
613 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200614{
Scott Wood17fed142016-05-30 13:57:56 -0500615 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100616 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617
Sergey Lapin3a38a552013-01-14 03:46:50 +0000618 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200619 if (command == NAND_CMD_SEQIN) {
620 int readcmd;
621
William Juul52c07962007-10-31 13:53:06 +0100622 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100624 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200625 readcmd = NAND_CMD_READOOB;
626 } else if (column < 256) {
627 /* First 256 bytes --> READ0 */
628 readcmd = NAND_CMD_READ0;
629 } else {
630 column -= 256;
631 readcmd = NAND_CMD_READ1;
632 }
William Juul52c07962007-10-31 13:53:06 +0100633 chip->cmd_ctrl(mtd, readcmd, ctrl);
634 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200635 }
William Juul52c07962007-10-31 13:53:06 +0100636 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200637
Sergey Lapin3a38a552013-01-14 03:46:50 +0000638 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100639 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
640 /* Serially input address */
641 if (column != -1) {
642 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200643 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530644 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100645 column >>= 1;
646 chip->cmd_ctrl(mtd, column, ctrl);
647 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200648 }
William Juul52c07962007-10-31 13:53:06 +0100649 if (page_addr != -1) {
650 chip->cmd_ctrl(mtd, page_addr, ctrl);
651 ctrl &= ~NAND_CTRL_CHANGE;
652 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900653 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100654 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
655 }
656 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200657
658 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000659 * Program and erase have their own busy handlers status and sequential
660 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100661 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200662 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200663
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200664 case NAND_CMD_PAGEPROG:
665 case NAND_CMD_ERASE1:
666 case NAND_CMD_ERASE2:
667 case NAND_CMD_SEQIN:
668 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900669 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900670 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200671 return;
672
673 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100674 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200675 break;
William Juul52c07962007-10-31 13:53:06 +0100676 udelay(chip->chip_delay);
677 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
678 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
679 chip->cmd_ctrl(mtd,
680 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500681 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
682 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200683 return;
684
William Juul52c07962007-10-31 13:53:06 +0100685 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200687 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200688 * If we don't have access to the busy pin, we apply the given
689 * command delay
William Juul52c07962007-10-31 13:53:06 +0100690 */
691 if (!chip->dev_ready) {
692 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200693 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200694 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200695 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000696 /*
697 * Apply this short delay always to ensure that we do wait tWB in
698 * any case on any machine.
699 */
William Juul52c07962007-10-31 13:53:06 +0100700 ndelay(100);
701
702 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200703}
704
705/**
706 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000707 * @mtd: MTD device structure
708 * @command: the command to be sent
709 * @column: the column address for this command, -1 if none
710 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200711 *
William Juul52c07962007-10-31 13:53:06 +0100712 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000713 * devices. We don't have the separate regions as we have in the small page
714 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715 */
William Juul52c07962007-10-31 13:53:06 +0100716static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
717 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200718{
Scott Wood17fed142016-05-30 13:57:56 -0500719 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200720
721 /* Emulate NAND_CMD_READOOB */
722 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100723 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 command = NAND_CMD_READ0;
725 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200726
William Juul52c07962007-10-31 13:53:06 +0100727 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200728 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200729
730 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100731 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732
733 /* Serially input address */
734 if (column != -1) {
735 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200736 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530737 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200738 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100739 chip->cmd_ctrl(mtd, column, ctrl);
740 ctrl &= ~NAND_CTRL_CHANGE;
741 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200742 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200743 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100744 chip->cmd_ctrl(mtd, page_addr, ctrl);
745 chip->cmd_ctrl(mtd, page_addr >> 8,
746 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900747 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100748 chip->cmd_ctrl(mtd, page_addr >> 16,
749 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200751 }
William Juul52c07962007-10-31 13:53:06 +0100752 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200753
754 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000755 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500756 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100757 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200758 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200759
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200760 case NAND_CMD_CACHEDPROG:
761 case NAND_CMD_PAGEPROG:
762 case NAND_CMD_ERASE1:
763 case NAND_CMD_ERASE2:
764 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100765 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200766 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900767 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900768 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200769 return;
770
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200771 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100772 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200773 break;
William Juul52c07962007-10-31 13:53:06 +0100774 udelay(chip->chip_delay);
775 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
776 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
777 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
778 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500779 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
780 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100781 return;
782
783 case NAND_CMD_RNDOUT:
784 /* No ready / busy check necessary */
785 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
786 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
787 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
788 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200789 return;
790
791 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100792 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
793 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
794 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
795 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200796
William Juul52c07962007-10-31 13:53:06 +0100797 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200798 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200799 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200800 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000801 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100802 */
803 if (!chip->dev_ready) {
804 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200805 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200806 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200807 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200808
Sergey Lapin3a38a552013-01-14 03:46:50 +0000809 /*
810 * Apply this short delay always to ensure that we do wait tWB in
811 * any case on any machine.
812 */
William Juul52c07962007-10-31 13:53:06 +0100813 ndelay(100);
814
815 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200816}
817
818/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200819 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000820 * @chip: the nand chip descriptor
821 * @mtd: MTD device structure
822 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200823 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200824 * Used when in panic, no locks are taken.
825 */
826static void panic_nand_get_device(struct nand_chip *chip,
827 struct mtd_info *mtd, int new_state)
828{
829 /* Hardware controller shared among independent devices */
830 chip->controller->active = chip;
831 chip->state = new_state;
832}
833
834/**
835 * nand_get_device - [GENERIC] Get chip for selected access
836 * @mtd: MTD device structure
837 * @new_state: the state which is requested
838 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200839 * Get the device and lock it for exclusive access
840 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200841static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200842nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100843{
Scott Wood17fed142016-05-30 13:57:56 -0500844 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200845 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100846 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200847}
848
849/**
850 * panic_nand_wait - [GENERIC] wait until the command is done
851 * @mtd: MTD device structure
852 * @chip: NAND chip structure
853 * @timeo: timeout
854 *
855 * Wait for command done. This is a helper function for nand_wait used when
856 * we are in interrupt context. May happen when in panic and trying to write
857 * an oops through mtdoops.
858 */
859static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
860 unsigned long timeo)
861{
862 int i;
863 for (i = 0; i < timeo; i++) {
864 if (chip->dev_ready) {
865 if (chip->dev_ready(mtd))
866 break;
867 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100868 int ret;
869 u8 status;
870
871 ret = nand_read_data_op(chip, &status, sizeof(status),
872 true);
873 if (ret)
874 return;
875
876 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200877 break;
878 }
879 mdelay(1);
880 }
William Juul52c07962007-10-31 13:53:06 +0100881}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200882
883/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000884 * nand_wait - [DEFAULT] wait until the command is done
885 * @mtd: MTD device structure
886 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200887 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500888 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100889 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200890static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200891{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500892 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100893 u8 status;
894 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100895
Heiko Schocherf5895d12014-06-24 10:10:04 +0200896 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100897
Heiko Schocherf5895d12014-06-24 10:10:04 +0200898 /*
899 * Apply this short delay always to ensure that we do wait tWB in any
900 * case on any machine.
901 */
902 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100903
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100904 ret = nand_status_op(chip, NULL);
905 if (ret)
906 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100907
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200908 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
909 u32 time_start;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200910
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200911 time_start = get_timer(0);
912 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200913 if (chip->dev_ready) {
914 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100915 break;
916 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100917 ret = nand_read_data_op(chip, &status,
918 sizeof(status), true);
919 if (ret)
920 return ret;
921
922 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100923 break;
924 }
925 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200926 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100927
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100928 ret = nand_read_data_op(chip, &status, sizeof(status), true);
929 if (ret)
930 return ret;
931
Heiko Schocherf5895d12014-06-24 10:10:04 +0200932 /* This can happen if in case of timeout or buggy dev_ready */
933 WARN_ON(!(status & NAND_STATUS_READY));
934 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200935}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500936
Scott Wood52ab7ce2016-05-30 13:57:58 -0500937/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900938 * nand_reset_data_interface - Reset data interface and timings
939 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900940 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900941 *
942 * Reset the Data interface and timings to ONFI mode 0.
943 *
944 * Returns 0 for success or negative error code otherwise.
945 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900946static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900947{
948 struct mtd_info *mtd = nand_to_mtd(chip);
949 const struct nand_data_interface *conf;
950 int ret;
951
952 if (!chip->setup_data_interface)
953 return 0;
954
955 /*
956 * The ONFI specification says:
957 * "
958 * To transition from NV-DDR or NV-DDR2 to the SDR data
959 * interface, the host shall use the Reset (FFh) command
960 * using SDR timing mode 0. A device in any timing mode is
961 * required to recognize Reset (FFh) command issued in SDR
962 * timing mode 0.
963 * "
964 *
965 * Configure the data interface in SDR mode and set the
966 * timings to timing mode 0.
967 */
968
969 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900970 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900971 if (ret)
972 pr_err("Failed to configure data interface to SDR timing mode 0\n");
973
974 return ret;
975}
976
Kory Maincent0cda0cb2022-06-22 11:11:45 +0200977static int nand_onfi_set_timings(struct mtd_info *mtd, struct nand_chip *chip)
978{
979 if (!chip->onfi_version ||
980 !(le16_to_cpu(chip->onfi_params.opt_cmd)
981 & ONFI_OPT_CMD_SET_GET_FEATURES))
982 return 0;
983
984 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
985 chip->onfi_timing_mode_default,
986 };
987
988 return chip->onfi_set_features(mtd, chip,
989 ONFI_FEATURE_ADDR_TIMING_MODE,
990 tmode_param);
991}
992
Boris Brezillone509cba2017-11-22 02:38:19 +0900993/**
994 * nand_setup_data_interface - Setup the best data interface and timings
995 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900996 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900997 *
998 * Find and configure the best data interface and NAND timings supported by
999 * the chip and the driver.
1000 * First tries to retrieve supported timing modes from ONFI information,
1001 * and if the NAND chip does not support ONFI, relies on the
1002 * ->onfi_timing_mode_default specified in the nand_ids table.
1003 *
1004 * Returns 0 for success or negative error code otherwise.
1005 */
Boris Brezillon32935f42017-11-22 02:38:28 +09001006static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +09001007{
1008 struct mtd_info *mtd = nand_to_mtd(chip);
1009 int ret;
1010
1011 if (!chip->setup_data_interface || !chip->data_interface)
1012 return 0;
1013
1014 /*
1015 * Ensure the timing mode has been changed on the chip side
1016 * before changing timings on the controller side.
1017 */
Kory Maincent0cda0cb2022-06-22 11:11:45 +02001018 ret = nand_onfi_set_timings(mtd, chip);
1019 if (ret)
1020 goto err;
Boris Brezillone509cba2017-11-22 02:38:19 +09001021
Boris Brezillon32935f42017-11-22 02:38:28 +09001022 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001023err:
1024 return ret;
1025}
1026
1027/**
1028 * nand_init_data_interface - find the best data interface and timings
1029 * @chip: The NAND chip
1030 *
1031 * Find the best data interface and NAND timings supported by the chip
1032 * and the driver.
1033 * First tries to retrieve supported timing modes from ONFI information,
1034 * and if the NAND chip does not support ONFI, relies on the
1035 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1036 * function nand_chip->data_interface is initialized with the best timing mode
1037 * available.
1038 *
1039 * Returns 0 for success or negative error code otherwise.
1040 */
1041static int nand_init_data_interface(struct nand_chip *chip)
1042{
1043 struct mtd_info *mtd = nand_to_mtd(chip);
1044 int modes, mode, ret;
1045
1046 if (!chip->setup_data_interface)
1047 return 0;
1048
1049 /*
1050 * First try to identify the best timings from ONFI parameters and
1051 * if the NAND does not support ONFI, fallback to the default ONFI
1052 * timing mode.
1053 */
1054 modes = onfi_get_async_timing_mode(chip);
1055 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1056 if (!chip->onfi_timing_mode_default)
1057 return 0;
1058
1059 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1060 }
1061
1062 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1063 GFP_KERNEL);
1064 if (!chip->data_interface)
1065 return -ENOMEM;
1066
1067 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1068 ret = onfi_init_data_interface(chip, chip->data_interface,
1069 NAND_SDR_IFACE, mode);
1070 if (ret)
1071 continue;
1072
Boris Brezillon32935f42017-11-22 02:38:28 +09001073 /* Pass -1 to only */
1074 ret = chip->setup_data_interface(mtd,
1075 NAND_DATA_IFACE_CHECK_ONLY,
1076 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001077 if (!ret) {
1078 chip->onfi_timing_mode_default = mode;
1079 break;
1080 }
1081 }
1082
1083 return 0;
1084}
1085
1086static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1087{
1088 kfree(chip->data_interface);
1089}
1090
1091/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001092 * nand_read_page_op - Do a READ PAGE operation
1093 * @chip: The NAND chip
1094 * @page: page to read
1095 * @offset_in_page: offset within the page
1096 * @buf: buffer used to store the data
1097 * @len: length of the buffer
1098 *
1099 * This function issues a READ PAGE operation.
1100 * This function does not select/unselect the CS line.
1101 *
1102 * Returns 0 on success, a negative error code otherwise.
1103 */
1104int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1105 unsigned int offset_in_page, void *buf, unsigned int len)
1106{
1107 struct mtd_info *mtd = nand_to_mtd(chip);
1108
1109 if (len && !buf)
1110 return -EINVAL;
1111
1112 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1113 return -EINVAL;
1114
1115 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1116 if (len)
1117 chip->read_buf(mtd, buf, len);
1118
1119 return 0;
1120}
1121EXPORT_SYMBOL_GPL(nand_read_page_op);
1122
1123/**
1124 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1125 * @chip: The NAND chip
1126 * @page: parameter page to read
1127 * @buf: buffer used to store the data
1128 * @len: length of the buffer
1129 *
1130 * This function issues a READ PARAMETER PAGE operation.
1131 * This function does not select/unselect the CS line.
1132 *
1133 * Returns 0 on success, a negative error code otherwise.
1134 */
1135static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1136 unsigned int len)
1137{
1138 struct mtd_info *mtd = nand_to_mtd(chip);
1139 unsigned int i;
1140 u8 *p = buf;
1141
1142 if (len && !buf)
1143 return -EINVAL;
1144
1145 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1146 for (i = 0; i < len; i++)
1147 p[i] = chip->read_byte(mtd);
1148
1149 return 0;
1150}
1151
1152/**
1153 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1154 * @chip: The NAND chip
1155 * @offset_in_page: offset within the page
1156 * @buf: buffer used to store the data
1157 * @len: length of the buffer
1158 * @force_8bit: force 8-bit bus access
1159 *
1160 * This function issues a CHANGE READ COLUMN operation.
1161 * This function does not select/unselect the CS line.
1162 *
1163 * Returns 0 on success, a negative error code otherwise.
1164 */
1165int nand_change_read_column_op(struct nand_chip *chip,
1166 unsigned int offset_in_page, void *buf,
1167 unsigned int len, bool force_8bit)
1168{
1169 struct mtd_info *mtd = nand_to_mtd(chip);
1170
1171 if (len && !buf)
1172 return -EINVAL;
1173
1174 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1175 return -EINVAL;
1176
1177 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1178 if (len)
1179 chip->read_buf(mtd, buf, len);
1180
1181 return 0;
1182}
1183EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1184
1185/**
1186 * nand_read_oob_op - Do a READ OOB operation
1187 * @chip: The NAND chip
1188 * @page: page to read
1189 * @offset_in_oob: offset within the OOB area
1190 * @buf: buffer used to store the data
1191 * @len: length of the buffer
1192 *
1193 * This function issues a READ OOB operation.
1194 * This function does not select/unselect the CS line.
1195 *
1196 * Returns 0 on success, a negative error code otherwise.
1197 */
1198int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1199 unsigned int offset_in_oob, void *buf, unsigned int len)
1200{
1201 struct mtd_info *mtd = nand_to_mtd(chip);
1202
1203 if (len && !buf)
1204 return -EINVAL;
1205
1206 if (offset_in_oob + len > mtd->oobsize)
1207 return -EINVAL;
1208
1209 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1210 if (len)
1211 chip->read_buf(mtd, buf, len);
1212
1213 return 0;
1214}
1215EXPORT_SYMBOL_GPL(nand_read_oob_op);
1216
1217/**
1218 * nand_prog_page_begin_op - starts a PROG PAGE operation
1219 * @chip: The NAND chip
1220 * @page: page to write
1221 * @offset_in_page: offset within the page
1222 * @buf: buffer containing the data to write to the page
1223 * @len: length of the buffer
1224 *
1225 * This function issues the first half of a PROG PAGE operation.
1226 * This function does not select/unselect the CS line.
1227 *
1228 * Returns 0 on success, a negative error code otherwise.
1229 */
1230int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1231 unsigned int offset_in_page, const void *buf,
1232 unsigned int len)
1233{
1234 struct mtd_info *mtd = nand_to_mtd(chip);
1235
1236 if (len && !buf)
1237 return -EINVAL;
1238
1239 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1240 return -EINVAL;
1241
1242 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1243
1244 if (buf)
1245 chip->write_buf(mtd, buf, len);
1246
1247 return 0;
1248}
1249EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1250
1251/**
1252 * nand_prog_page_end_op - ends a PROG PAGE operation
1253 * @chip: The NAND chip
1254 *
1255 * This function issues the second half of a PROG PAGE operation.
1256 * This function does not select/unselect the CS line.
1257 *
1258 * Returns 0 on success, a negative error code otherwise.
1259 */
1260int nand_prog_page_end_op(struct nand_chip *chip)
1261{
1262 struct mtd_info *mtd = nand_to_mtd(chip);
1263 int status;
1264
1265 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1266
1267 status = chip->waitfunc(mtd, chip);
1268 if (status & NAND_STATUS_FAIL)
1269 return -EIO;
1270
1271 return 0;
1272}
1273EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1274
1275/**
1276 * nand_prog_page_op - Do a full PROG PAGE operation
1277 * @chip: The NAND chip
1278 * @page: page to write
1279 * @offset_in_page: offset within the page
1280 * @buf: buffer containing the data to write to the page
1281 * @len: length of the buffer
1282 *
1283 * This function issues a full PROG PAGE operation.
1284 * This function does not select/unselect the CS line.
1285 *
1286 * Returns 0 on success, a negative error code otherwise.
1287 */
1288int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1289 unsigned int offset_in_page, const void *buf,
1290 unsigned int len)
1291{
1292 struct mtd_info *mtd = nand_to_mtd(chip);
1293 int status;
1294
1295 if (!len || !buf)
1296 return -EINVAL;
1297
1298 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1299 return -EINVAL;
1300
1301 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1302 chip->write_buf(mtd, buf, len);
1303 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1304
1305 status = chip->waitfunc(mtd, chip);
1306 if (status & NAND_STATUS_FAIL)
1307 return -EIO;
1308
1309 return 0;
1310}
1311EXPORT_SYMBOL_GPL(nand_prog_page_op);
1312
1313/**
1314 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1315 * @chip: The NAND chip
1316 * @offset_in_page: offset within the page
1317 * @buf: buffer containing the data to send to the NAND
1318 * @len: length of the buffer
1319 * @force_8bit: force 8-bit bus access
1320 *
1321 * This function issues a CHANGE WRITE COLUMN operation.
1322 * This function does not select/unselect the CS line.
1323 *
1324 * Returns 0 on success, a negative error code otherwise.
1325 */
1326int nand_change_write_column_op(struct nand_chip *chip,
1327 unsigned int offset_in_page,
1328 const void *buf, unsigned int len,
1329 bool force_8bit)
1330{
1331 struct mtd_info *mtd = nand_to_mtd(chip);
1332
1333 if (len && !buf)
1334 return -EINVAL;
1335
1336 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1337 return -EINVAL;
1338
1339 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1340 if (len)
1341 chip->write_buf(mtd, buf, len);
1342
1343 return 0;
1344}
1345EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1346
1347/**
1348 * nand_readid_op - Do a READID operation
1349 * @chip: The NAND chip
1350 * @addr: address cycle to pass after the READID command
1351 * @buf: buffer used to store the ID
1352 * @len: length of the buffer
1353 *
1354 * This function sends a READID command and reads back the ID returned by the
1355 * NAND.
1356 * This function does not select/unselect the CS line.
1357 *
1358 * Returns 0 on success, a negative error code otherwise.
1359 */
1360int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1361 unsigned int len)
1362{
1363 struct mtd_info *mtd = nand_to_mtd(chip);
1364 unsigned int i;
1365 u8 *id = buf;
1366
1367 if (len && !buf)
1368 return -EINVAL;
1369
1370 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1371
1372 for (i = 0; i < len; i++)
1373 id[i] = chip->read_byte(mtd);
1374
1375 return 0;
1376}
1377EXPORT_SYMBOL_GPL(nand_readid_op);
1378
1379/**
1380 * nand_status_op - Do a STATUS operation
1381 * @chip: The NAND chip
1382 * @status: out variable to store the NAND status
1383 *
1384 * This function sends a STATUS command and reads back the status returned by
1385 * the NAND.
1386 * This function does not select/unselect the CS line.
1387 *
1388 * Returns 0 on success, a negative error code otherwise.
1389 */
1390int nand_status_op(struct nand_chip *chip, u8 *status)
1391{
1392 struct mtd_info *mtd = nand_to_mtd(chip);
1393
1394 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1395 if (status)
1396 *status = chip->read_byte(mtd);
1397
1398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(nand_status_op);
1401
1402/**
1403 * nand_exit_status_op - Exit a STATUS operation
1404 * @chip: The NAND chip
1405 *
1406 * This function sends a READ0 command to cancel the effect of the STATUS
1407 * command to avoid reading only the status until a new read command is sent.
1408 *
1409 * This function does not select/unselect the CS line.
1410 *
1411 * Returns 0 on success, a negative error code otherwise.
1412 */
1413int nand_exit_status_op(struct nand_chip *chip)
1414{
1415 struct mtd_info *mtd = nand_to_mtd(chip);
1416
1417 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1418
1419 return 0;
1420}
1421EXPORT_SYMBOL_GPL(nand_exit_status_op);
1422
1423/**
1424 * nand_erase_op - Do an erase operation
1425 * @chip: The NAND chip
1426 * @eraseblock: block to erase
1427 *
1428 * This function sends an ERASE command and waits for the NAND to be ready
1429 * before returning.
1430 * This function does not select/unselect the CS line.
1431 *
1432 * Returns 0 on success, a negative error code otherwise.
1433 */
1434int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1435{
1436 struct mtd_info *mtd = nand_to_mtd(chip);
1437 unsigned int page = eraseblock <<
1438 (chip->phys_erase_shift - chip->page_shift);
1439 int status;
1440
1441 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1442 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1443
1444 status = chip->waitfunc(mtd, chip);
1445 if (status < 0)
1446 return status;
1447
1448 if (status & NAND_STATUS_FAIL)
1449 return -EIO;
1450
1451 return 0;
1452}
1453EXPORT_SYMBOL_GPL(nand_erase_op);
1454
1455/**
1456 * nand_set_features_op - Do a SET FEATURES operation
1457 * @chip: The NAND chip
1458 * @feature: feature id
1459 * @data: 4 bytes of data
1460 *
1461 * This function sends a SET FEATURES command and waits for the NAND to be
1462 * ready before returning.
1463 * This function does not select/unselect the CS line.
1464 *
1465 * Returns 0 on success, a negative error code otherwise.
1466 */
1467static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1468 const void *data)
1469{
1470 struct mtd_info *mtd = nand_to_mtd(chip);
1471 const u8 *params = data;
1472 int i, status;
1473
1474 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1475 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1476 chip->write_byte(mtd, params[i]);
1477
1478 status = chip->waitfunc(mtd, chip);
1479 if (status & NAND_STATUS_FAIL)
1480 return -EIO;
1481
1482 return 0;
1483}
1484
1485/**
1486 * nand_get_features_op - Do a GET FEATURES operation
1487 * @chip: The NAND chip
1488 * @feature: feature id
1489 * @data: 4 bytes of data
1490 *
1491 * This function sends a GET FEATURES command and waits for the NAND to be
1492 * ready before returning.
1493 * This function does not select/unselect the CS line.
1494 *
1495 * Returns 0 on success, a negative error code otherwise.
1496 */
1497static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1498 void *data)
1499{
1500 struct mtd_info *mtd = nand_to_mtd(chip);
1501 u8 *params = data;
1502 int i;
1503
1504 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1505 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1506 params[i] = chip->read_byte(mtd);
1507
1508 return 0;
1509}
1510
1511/**
1512 * nand_reset_op - Do a reset operation
1513 * @chip: The NAND chip
1514 *
1515 * This function sends a RESET command and waits for the NAND to be ready
1516 * before returning.
1517 * This function does not select/unselect the CS line.
1518 *
1519 * Returns 0 on success, a negative error code otherwise.
1520 */
1521int nand_reset_op(struct nand_chip *chip)
1522{
1523 struct mtd_info *mtd = nand_to_mtd(chip);
1524
1525 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1526
1527 return 0;
1528}
1529EXPORT_SYMBOL_GPL(nand_reset_op);
1530
1531/**
1532 * nand_read_data_op - Read data from the NAND
1533 * @chip: The NAND chip
1534 * @buf: buffer used to store the data
1535 * @len: length of the buffer
1536 * @force_8bit: force 8-bit bus access
1537 *
1538 * This function does a raw data read on the bus. Usually used after launching
1539 * another NAND operation like nand_read_page_op().
1540 * This function does not select/unselect the CS line.
1541 *
1542 * Returns 0 on success, a negative error code otherwise.
1543 */
1544int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1545 bool force_8bit)
1546{
1547 struct mtd_info *mtd = nand_to_mtd(chip);
1548
1549 if (!len || !buf)
1550 return -EINVAL;
1551
1552 if (force_8bit) {
1553 u8 *p = buf;
1554 unsigned int i;
1555
1556 for (i = 0; i < len; i++)
1557 p[i] = chip->read_byte(mtd);
1558 } else {
1559 chip->read_buf(mtd, buf, len);
1560 }
1561
1562 return 0;
1563}
1564EXPORT_SYMBOL_GPL(nand_read_data_op);
1565
1566/**
1567 * nand_write_data_op - Write data from the NAND
1568 * @chip: The NAND chip
1569 * @buf: buffer containing the data to send on the bus
1570 * @len: length of the buffer
1571 * @force_8bit: force 8-bit bus access
1572 *
1573 * This function does a raw data write on the bus. Usually used after launching
1574 * another NAND operation like nand_write_page_begin_op().
1575 * This function does not select/unselect the CS line.
1576 *
1577 * Returns 0 on success, a negative error code otherwise.
1578 */
1579int nand_write_data_op(struct nand_chip *chip, const void *buf,
1580 unsigned int len, bool force_8bit)
1581{
1582 struct mtd_info *mtd = nand_to_mtd(chip);
1583
1584 if (!len || !buf)
1585 return -EINVAL;
1586
1587 if (force_8bit) {
1588 const u8 *p = buf;
1589 unsigned int i;
1590
1591 for (i = 0; i < len; i++)
1592 chip->write_byte(mtd, p[i]);
1593 } else {
1594 chip->write_buf(mtd, buf, len);
1595 }
1596
1597 return 0;
1598}
1599EXPORT_SYMBOL_GPL(nand_write_data_op);
1600
1601/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001602 * nand_reset - Reset and initialize a NAND device
1603 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001604 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001605 *
1606 * Returns 0 for success or negative error code otherwise
1607 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001608int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001609{
1610 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001611 int ret;
1612
Boris Brezillon32935f42017-11-22 02:38:28 +09001613 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001614 if (ret)
1615 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001616
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001617 /*
1618 * The CS line has to be released before we can apply the new NAND
1619 * interface settings, hence this weird ->select_chip() dance.
1620 */
1621 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001622 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001623 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001624 if (ret)
1625 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001626
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001627 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001628 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001629 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001630 if (ret)
1631 return ret;
1632
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001633 return 0;
1634}
1635
1636/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001637 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1638 * @buf: buffer to test
1639 * @len: buffer length
1640 * @bitflips_threshold: maximum number of bitflips
1641 *
1642 * Check if a buffer contains only 0xff, which means the underlying region
1643 * has been erased and is ready to be programmed.
1644 * The bitflips_threshold specify the maximum number of bitflips before
1645 * considering the region is not erased.
1646 * Note: The logic of this function has been extracted from the memweight
1647 * implementation, except that nand_check_erased_buf function exit before
1648 * testing the whole buffer if the number of bitflips exceed the
1649 * bitflips_threshold value.
1650 *
1651 * Returns a positive number of bitflips less than or equal to
1652 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1653 * threshold.
1654 */
1655static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1656{
1657 const unsigned char *bitmap = buf;
1658 int bitflips = 0;
1659 int weight;
1660
1661 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1662 len--, bitmap++) {
1663 weight = hweight8(*bitmap);
1664 bitflips += BITS_PER_BYTE - weight;
1665 if (unlikely(bitflips > bitflips_threshold))
1666 return -EBADMSG;
1667 }
1668
1669 for (; len >= 4; len -= 4, bitmap += 4) {
1670 weight = hweight32(*((u32 *)bitmap));
1671 bitflips += 32 - weight;
1672 if (unlikely(bitflips > bitflips_threshold))
1673 return -EBADMSG;
1674 }
1675
1676 for (; len > 0; len--, bitmap++) {
1677 weight = hweight8(*bitmap);
1678 bitflips += BITS_PER_BYTE - weight;
1679 if (unlikely(bitflips > bitflips_threshold))
1680 return -EBADMSG;
1681 }
1682
1683 return bitflips;
1684}
1685
1686/**
1687 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1688 * 0xff data
1689 * @data: data buffer to test
1690 * @datalen: data length
1691 * @ecc: ECC buffer
1692 * @ecclen: ECC length
1693 * @extraoob: extra OOB buffer
1694 * @extraooblen: extra OOB length
1695 * @bitflips_threshold: maximum number of bitflips
1696 *
1697 * Check if a data buffer and its associated ECC and OOB data contains only
1698 * 0xff pattern, which means the underlying region has been erased and is
1699 * ready to be programmed.
1700 * The bitflips_threshold specify the maximum number of bitflips before
1701 * considering the region as not erased.
1702 *
1703 * Note:
1704 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1705 * different from the NAND page size. When fixing bitflips, ECC engines will
1706 * report the number of errors per chunk, and the NAND core infrastructure
1707 * expect you to return the maximum number of bitflips for the whole page.
1708 * This is why you should always use this function on a single chunk and
1709 * not on the whole page. After checking each chunk you should update your
1710 * max_bitflips value accordingly.
1711 * 2/ When checking for bitflips in erased pages you should not only check
1712 * the payload data but also their associated ECC data, because a user might
1713 * have programmed almost all bits to 1 but a few. In this case, we
1714 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1715 * this case.
1716 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1717 * data are protected by the ECC engine.
1718 * It could also be used if you support subpages and want to attach some
1719 * extra OOB data to an ECC chunk.
1720 *
1721 * Returns a positive number of bitflips less than or equal to
1722 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1723 * threshold. In case of success, the passed buffers are filled with 0xff.
1724 */
1725int nand_check_erased_ecc_chunk(void *data, int datalen,
1726 void *ecc, int ecclen,
1727 void *extraoob, int extraooblen,
1728 int bitflips_threshold)
1729{
1730 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1731
1732 data_bitflips = nand_check_erased_buf(data, datalen,
1733 bitflips_threshold);
1734 if (data_bitflips < 0)
1735 return data_bitflips;
1736
1737 bitflips_threshold -= data_bitflips;
1738
1739 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1740 if (ecc_bitflips < 0)
1741 return ecc_bitflips;
1742
1743 bitflips_threshold -= ecc_bitflips;
1744
1745 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1746 bitflips_threshold);
1747 if (extraoob_bitflips < 0)
1748 return extraoob_bitflips;
1749
1750 if (data_bitflips)
1751 memset(data, 0xff, datalen);
1752
1753 if (ecc_bitflips)
1754 memset(ecc, 0xff, ecclen);
1755
1756 if (extraoob_bitflips)
1757 memset(extraoob, 0xff, extraooblen);
1758
1759 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1760}
1761EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001762
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001763/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001764 * nand_read_page_raw - [INTERN] read raw page data without ecc
1765 * @mtd: mtd info structure
1766 * @chip: nand chip info structure
1767 * @buf: buffer to store read data
1768 * @oob_required: caller requires OOB data read to chip->oob_poi
1769 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001770 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001771 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001772 */
William Juul52c07962007-10-31 13:53:06 +01001773static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001774 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001775{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001776 int ret;
1777
1778 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1779 if (ret)
1780 return ret;
1781
1782 if (oob_required) {
1783 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1784 false);
1785 if (ret)
1786 return ret;
1787 }
1788
William Juul52c07962007-10-31 13:53:06 +01001789 return 0;
1790}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001791
William Juul52c07962007-10-31 13:53:06 +01001792/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001793 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1794 * @mtd: mtd info structure
1795 * @chip: nand chip info structure
1796 * @buf: buffer to store read data
1797 * @oob_required: caller requires OOB data read to chip->oob_poi
1798 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001799 *
1800 * We need a special oob layout and handling even when OOB isn't used.
1801 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001802static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001803 struct nand_chip *chip, uint8_t *buf,
1804 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001805{
1806 int eccsize = chip->ecc.size;
1807 int eccbytes = chip->ecc.bytes;
1808 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001809 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001810
1811 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001812 ret = nand_read_data_op(chip, buf, eccsize, false);
1813 if (ret)
1814 return ret;
1815
David Brownellee86b8d2009-11-07 16:27:01 -05001816 buf += eccsize;
1817
1818 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001819 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1820 false);
1821 if (ret)
1822 return ret;
1823
David Brownellee86b8d2009-11-07 16:27:01 -05001824 oob += chip->ecc.prepad;
1825 }
1826
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001827 ret = nand_read_data_op(chip, oob, eccbytes, false);
1828 if (ret)
1829 return ret;
1830
David Brownellee86b8d2009-11-07 16:27:01 -05001831 oob += eccbytes;
1832
1833 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001834 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1835 false);
1836 if (ret)
1837 return ret;
1838
David Brownellee86b8d2009-11-07 16:27:01 -05001839 oob += chip->ecc.postpad;
1840 }
1841 }
1842
1843 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001844 if (size) {
1845 ret = nand_read_data_op(chip, oob, size, false);
1846 if (ret)
1847 return ret;
1848 }
David Brownellee86b8d2009-11-07 16:27:01 -05001849
1850 return 0;
1851}
1852
1853/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001854 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1855 * @mtd: mtd info structure
1856 * @chip: nand chip info structure
1857 * @buf: buffer to store read data
1858 * @oob_required: caller requires OOB data read to chip->oob_poi
1859 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001860 */
1861static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001862 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001863{
1864 int i, eccsize = chip->ecc.size;
1865 int eccbytes = chip->ecc.bytes;
1866 int eccsteps = chip->ecc.steps;
1867 uint8_t *p = buf;
1868 uint8_t *ecc_calc = chip->buffers->ecccalc;
1869 uint8_t *ecc_code = chip->buffers->ecccode;
1870 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001871 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001872
Sergey Lapin3a38a552013-01-14 03:46:50 +00001873 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001874
William Juul52c07962007-10-31 13:53:06 +01001875 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1876 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001877
William Juul52c07962007-10-31 13:53:06 +01001878 for (i = 0; i < chip->ecc.total; i++)
1879 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001880
William Juul52c07962007-10-31 13:53:06 +01001881 eccsteps = chip->ecc.steps;
1882 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001883
William Juul52c07962007-10-31 13:53:06 +01001884 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1885 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001886
William Juul52c07962007-10-31 13:53:06 +01001887 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001888 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001889 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001890 } else {
William Juul52c07962007-10-31 13:53:06 +01001891 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001892 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1893 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001894 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001895 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001896}
1897
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001898/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001899 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001900 * @mtd: mtd info structure
1901 * @chip: nand chip info structure
1902 * @data_offs: offset of requested data within the page
1903 * @readlen: data length
1904 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001905 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001906 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001907static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001908 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1909 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001910{
1911 int start_step, end_step, num_steps;
1912 uint32_t *eccpos = chip->ecc.layout->eccpos;
1913 uint8_t *p;
1914 int data_col_addr, i, gaps = 0;
1915 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1916 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001917 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001918 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001919 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001920
Sergey Lapin3a38a552013-01-14 03:46:50 +00001921 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001922 start_step = data_offs / chip->ecc.size;
1923 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1924 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001925 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001926
Sergey Lapin3a38a552013-01-14 03:46:50 +00001927 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001928 datafrag_len = num_steps * chip->ecc.size;
1929 eccfrag_len = num_steps * chip->ecc.bytes;
1930
1931 data_col_addr = start_step * chip->ecc.size;
1932 /* If we read not a page aligned data */
1933 if (data_col_addr != 0)
1934 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1935
1936 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001937 ret = nand_read_data_op(chip, p, datafrag_len, false);
1938 if (ret)
1939 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001940
Sergey Lapin3a38a552013-01-14 03:46:50 +00001941 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001942 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1943 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1944
Sergey Lapin3a38a552013-01-14 03:46:50 +00001945 /*
1946 * The performance is faster if we position offsets according to
1947 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1948 */
Scott Wood3628f002008-10-24 16:20:43 -05001949 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001950 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001951 gaps = 1;
1952 break;
1953 }
1954 }
1955 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001956 ret = nand_change_read_column_op(chip, mtd->writesize,
1957 chip->oob_poi, mtd->oobsize,
1958 false);
1959 if (ret)
1960 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001961 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001962 /*
1963 * Send the command to read the particular ECC bytes take care
1964 * about buswidth alignment in read_buf.
1965 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001966 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001967 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001968 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001969 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001970 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001971 aligned_len++;
1972
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001973 ret = nand_change_read_column_op(chip,
1974 mtd->writesize + aligned_pos,
1975 &chip->oob_poi[aligned_pos],
1976 aligned_len, false);
1977 if (ret)
1978 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001979 }
1980
1981 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001982 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001983
1984 p = bufpoi + data_col_addr;
1985 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1986 int stat;
1987
Christian Hitzb8a6b372011-10-12 09:32:02 +02001988 stat = chip->ecc.correct(mtd, p,
1989 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001990 if (stat == -EBADMSG &&
1991 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1992 /* check for empty pages with bitflips */
1993 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1994 &chip->buffers->ecccode[i],
1995 chip->ecc.bytes,
1996 NULL, 0,
1997 chip->ecc.strength);
1998 }
1999
Heiko Schocherf5895d12014-06-24 10:10:04 +02002000 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05002001 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002002 } else {
Scott Wood3628f002008-10-24 16:20:43 -05002003 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002004 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2005 }
Scott Wood3628f002008-10-24 16:20:43 -05002006 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002007 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05002008}
2009
2010/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002011 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
2012 * @mtd: mtd info structure
2013 * @chip: nand chip info structure
2014 * @buf: buffer to store read data
2015 * @oob_required: caller requires OOB data read to chip->oob_poi
2016 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002017 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002018 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002019 */
William Juul52c07962007-10-31 13:53:06 +01002020static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002021 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002022{
William Juul52c07962007-10-31 13:53:06 +01002023 int i, eccsize = chip->ecc.size;
2024 int eccbytes = chip->ecc.bytes;
2025 int eccsteps = chip->ecc.steps;
2026 uint8_t *p = buf;
2027 uint8_t *ecc_calc = chip->buffers->ecccalc;
2028 uint8_t *ecc_code = chip->buffers->ecccode;
2029 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002030 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002031 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002032
William Juul52c07962007-10-31 13:53:06 +01002033 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2034 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002035
2036 ret = nand_read_data_op(chip, p, eccsize, false);
2037 if (ret)
2038 return ret;
2039
William Juul52c07962007-10-31 13:53:06 +01002040 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2041 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002042
2043 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2044 if (ret)
2045 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002046
William Juul52c07962007-10-31 13:53:06 +01002047 for (i = 0; i < chip->ecc.total; i++)
2048 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002049
William Juul52c07962007-10-31 13:53:06 +01002050 eccsteps = chip->ecc.steps;
2051 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002052
William Juul52c07962007-10-31 13:53:06 +01002053 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2054 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002055
William Juul52c07962007-10-31 13:53:06 +01002056 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002057 if (stat == -EBADMSG &&
2058 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2059 /* check for empty pages with bitflips */
2060 stat = nand_check_erased_ecc_chunk(p, eccsize,
2061 &ecc_code[i], eccbytes,
2062 NULL, 0,
2063 chip->ecc.strength);
2064 }
2065
Heiko Schocherf5895d12014-06-24 10:10:04 +02002066 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002067 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002068 } else {
William Juul52c07962007-10-31 13:53:06 +01002069 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002070 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2071 }
William Juul52c07962007-10-31 13:53:06 +01002072 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002073 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002074}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002075
William Juul52c07962007-10-31 13:53:06 +01002076/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002077 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2078 * @mtd: mtd info structure
2079 * @chip: nand chip info structure
2080 * @buf: buffer to store read data
2081 * @oob_required: caller requires OOB data read to chip->oob_poi
2082 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002083 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002084 * Hardware ECC for large page chips, require OOB to be read first. For this
2085 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2086 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2087 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2088 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002089 */
2090static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002091 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002092{
2093 int i, eccsize = chip->ecc.size;
2094 int eccbytes = chip->ecc.bytes;
2095 int eccsteps = chip->ecc.steps;
2096 uint8_t *p = buf;
2097 uint8_t *ecc_code = chip->buffers->ecccode;
2098 uint32_t *eccpos = chip->ecc.layout->eccpos;
2099 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002100 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002101 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002102
2103 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002104 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2105 if (ret)
2106 return ret;
2107
2108 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2109 if (ret)
2110 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002111
2112 for (i = 0; i < chip->ecc.total; i++)
2113 ecc_code[i] = chip->oob_poi[eccpos[i]];
2114
2115 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2116 int stat;
2117
2118 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002119
2120 ret = nand_read_data_op(chip, p, eccsize, false);
2121 if (ret)
2122 return ret;
2123
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002124 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2125
2126 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002127 if (stat == -EBADMSG &&
2128 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2129 /* check for empty pages with bitflips */
2130 stat = nand_check_erased_ecc_chunk(p, eccsize,
2131 &ecc_code[i], eccbytes,
2132 NULL, 0,
2133 chip->ecc.strength);
2134 }
2135
Heiko Schocherf5895d12014-06-24 10:10:04 +02002136 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002137 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002138 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002139 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002140 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2141 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002142 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002143 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002144}
2145
2146/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002147 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2148 * @mtd: mtd info structure
2149 * @chip: nand chip info structure
2150 * @buf: buffer to store read data
2151 * @oob_required: caller requires OOB data read to chip->oob_poi
2152 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002153 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002154 * The hw generator calculates the error syndrome automatically. Therefore we
2155 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002156 */
2157static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002158 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002159{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002160 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002161 int eccbytes = chip->ecc.bytes;
2162 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002163 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002164 uint8_t *p = buf;
2165 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002166 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002167
2168 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2169 int stat;
2170
2171 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002172
2173 ret = nand_read_data_op(chip, p, eccsize, false);
2174 if (ret)
2175 return ret;
William Juul52c07962007-10-31 13:53:06 +01002176
2177 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002178 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2179 false);
2180 if (ret)
2181 return ret;
2182
William Juul52c07962007-10-31 13:53:06 +01002183 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002184 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002185
William Juul52c07962007-10-31 13:53:06 +01002186 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002187
2188 ret = nand_read_data_op(chip, oob, eccbytes, false);
2189 if (ret)
2190 return ret;
2191
William Juul52c07962007-10-31 13:53:06 +01002192 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002193
William Juul52c07962007-10-31 13:53:06 +01002194 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002195
William Juul52c07962007-10-31 13:53:06 +01002196 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002197 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2198 false);
2199 if (ret)
2200 return ret;
2201
William Juul52c07962007-10-31 13:53:06 +01002202 oob += chip->ecc.postpad;
2203 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002204
2205 if (stat == -EBADMSG &&
2206 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2207 /* check for empty pages with bitflips */
2208 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2209 oob - eccpadbytes,
2210 eccpadbytes,
2211 NULL, 0,
2212 chip->ecc.strength);
2213 }
2214
2215 if (stat < 0) {
2216 mtd->ecc_stats.failed++;
2217 } else {
2218 mtd->ecc_stats.corrected += stat;
2219 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2220 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002221 }
William Juul52c07962007-10-31 13:53:06 +01002222
2223 /* Calculate remaining oob bytes */
2224 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002225 if (i) {
2226 ret = nand_read_data_op(chip, oob, i, false);
2227 if (ret)
2228 return ret;
2229 }
William Juul52c07962007-10-31 13:53:06 +01002230
Heiko Schocherf5895d12014-06-24 10:10:04 +02002231 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002232}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002233
2234/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002235 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2236 * @chip: nand chip structure
2237 * @oob: oob destination address
2238 * @ops: oob ops structure
2239 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002240 */
2241static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2242 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002243{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002244 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002245
Sergey Lapin3a38a552013-01-14 03:46:50 +00002246 case MTD_OPS_PLACE_OOB:
2247 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002248 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2249 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002250
Sergey Lapin3a38a552013-01-14 03:46:50 +00002251 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002252 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2253 uint32_t boffs = 0, roffs = ops->ooboffs;
2254 size_t bytes = 0;
2255
Christian Hitz13fc0e22011-10-12 09:32:01 +02002256 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002257 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002258 if (unlikely(roffs)) {
2259 if (roffs >= free->length) {
2260 roffs -= free->length;
2261 continue;
2262 }
2263 boffs = free->offset + roffs;
2264 bytes = min_t(size_t, len,
2265 (free->length - roffs));
2266 roffs = 0;
2267 } else {
2268 bytes = min_t(size_t, len, free->length);
2269 boffs = free->offset;
2270 }
2271 memcpy(oob, chip->oob_poi + boffs, bytes);
2272 oob += bytes;
2273 }
2274 return oob;
2275 }
2276 default:
2277 BUG();
2278 }
2279 return NULL;
2280}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002281
2282/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002283 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2284 * @mtd: MTD device structure
2285 * @retry_mode: the retry mode to use
2286 *
2287 * Some vendors supply a special command to shift the Vt threshold, to be used
2288 * when there are too many bitflips in a page (i.e., ECC error). After setting
2289 * a new threshold, the host should retry reading the page.
2290 */
2291static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2292{
Scott Wood17fed142016-05-30 13:57:56 -05002293 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002294
2295 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2296
2297 if (retry_mode >= chip->read_retries)
2298 return -EINVAL;
2299
2300 if (!chip->setup_read_retry)
2301 return -EOPNOTSUPP;
2302
2303 return chip->setup_read_retry(mtd, retry_mode);
2304}
2305
2306/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002307 * nand_do_read_ops - [INTERN] Read data with ECC
2308 * @mtd: MTD device structure
2309 * @from: offset to read from
2310 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002311 *
William Juul52c07962007-10-31 13:53:06 +01002312 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002313 */
William Juul52c07962007-10-31 13:53:06 +01002314static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2315 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002316{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002317 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002318 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002319 int ret = 0;
2320 uint32_t readlen = ops->len;
2321 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002322 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002323
William Juul52c07962007-10-31 13:53:06 +01002324 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002325 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002326 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002327 int retry_mode = 0;
2328 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002329
William Juul52c07962007-10-31 13:53:06 +01002330 chipnr = (int)(from >> chip->chip_shift);
2331 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002332
William Juul52c07962007-10-31 13:53:06 +01002333 realpage = (int)(from >> chip->page_shift);
2334 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002335
William Juul52c07962007-10-31 13:53:06 +01002336 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002337
William Juul52c07962007-10-31 13:53:06 +01002338 buf = ops->datbuf;
2339 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002340 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002341
Christian Hitz13fc0e22011-10-12 09:32:01 +02002342 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002343 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002344
Heiko Schocherf5895d12014-06-24 10:10:04 +02002345 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01002346 bytes = min(mtd->writesize - col, readlen);
2347 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002348
Scott Wood3ea94ed2015-06-26 19:03:26 -05002349 if (!aligned)
2350 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002351 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2352 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2353 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002354 else
2355 use_bufpoi = 0;
2356
Sergey Lapin3a38a552013-01-14 03:46:50 +00002357 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002358 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002359 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2360
2361 if (use_bufpoi && aligned)
2362 pr_debug("%s: using read bounce buffer for buf@%p\n",
2363 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002364
Heiko Schocherf5895d12014-06-24 10:10:04 +02002365read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002366 if (nand_standard_page_accessors(&chip->ecc)) {
2367 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2368 if (ret)
2369 break;
2370 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002371
Paul Burton700a76c2013-09-04 15:16:56 +01002372 /*
2373 * Now read the page into the buffer. Absent an error,
2374 * the read methods return max bitflips per ecc step.
2375 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002376 if (unlikely(ops->mode == MTD_OPS_RAW))
2377 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2378 oob_required,
2379 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002380 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002381 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002382 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002383 col, bytes, bufpoi,
2384 page);
William Juul52c07962007-10-31 13:53:06 +01002385 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002386 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002387 oob_required, page);
2388 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002389 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002390 /* Invalidate page cache */
2391 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002392 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002393 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002394
Paul Burton700a76c2013-09-04 15:16:56 +01002395 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2396
William Juul52c07962007-10-31 13:53:06 +01002397 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002398 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002399 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002400 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002401 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002402 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002403 chip->pagebuf_bitflips = ret;
2404 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002405 /* Invalidate page cache */
2406 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002407 }
William Juul52c07962007-10-31 13:53:06 +01002408 memcpy(buf, chip->buffers->databuf + col, bytes);
2409 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002410
William Juul52c07962007-10-31 13:53:06 +01002411 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002412 int toread = min(oobreadlen, max_oobsize);
2413
2414 if (toread) {
2415 oob = nand_transfer_oob(chip,
2416 oob, ops, toread);
2417 oobreadlen -= toread;
2418 }
William Juul52c07962007-10-31 13:53:06 +01002419 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002420
2421 if (chip->options & NAND_NEED_READRDY) {
2422 /* Apply delay or wait for ready/busy pin */
2423 if (!chip->dev_ready)
2424 udelay(chip->chip_delay);
2425 else
2426 nand_wait_ready(mtd);
2427 }
2428
2429 if (mtd->ecc_stats.failed - ecc_failures) {
2430 if (retry_mode + 1 < chip->read_retries) {
2431 retry_mode++;
2432 ret = nand_setup_read_retry(mtd,
2433 retry_mode);
2434 if (ret < 0)
2435 break;
2436
2437 /* Reset failures; retry */
2438 mtd->ecc_stats.failed = ecc_failures;
2439 goto read_retry;
2440 } else {
2441 /* No more retry modes; real failure */
2442 ecc_fail = true;
2443 }
2444 }
2445
2446 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002447 } else {
2448 memcpy(buf, chip->buffers->databuf + col, bytes);
2449 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002450 max_bitflips = max_t(unsigned int, max_bitflips,
2451 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002452 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002453
William Juul52c07962007-10-31 13:53:06 +01002454 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002455
Heiko Schocherf5895d12014-06-24 10:10:04 +02002456 /* Reset to retry mode 0 */
2457 if (retry_mode) {
2458 ret = nand_setup_read_retry(mtd, 0);
2459 if (ret < 0)
2460 break;
2461 retry_mode = 0;
2462 }
2463
William Juul52c07962007-10-31 13:53:06 +01002464 if (!readlen)
2465 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002466
Sergey Lapin3a38a552013-01-14 03:46:50 +00002467 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002468 col = 0;
2469 /* Increment page address */
2470 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002471
William Juul52c07962007-10-31 13:53:06 +01002472 page = realpage & chip->pagemask;
2473 /* Check, if we cross a chip boundary */
2474 if (!page) {
2475 chipnr++;
2476 chip->select_chip(mtd, -1);
2477 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002478 }
William Juul52c07962007-10-31 13:53:06 +01002479 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002480 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002481
William Juul52c07962007-10-31 13:53:06 +01002482 ops->retlen = ops->len - (size_t) readlen;
2483 if (oob)
2484 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002485
Heiko Schocherf5895d12014-06-24 10:10:04 +02002486 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002487 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002488
Heiko Schocherf5895d12014-06-24 10:10:04 +02002489 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002490 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002491
Paul Burton700a76c2013-09-04 15:16:56 +01002492 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002493}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002494
William Juul52c07962007-10-31 13:53:06 +01002495/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002496 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2497 * @mtd: mtd info structure
2498 * @chip: nand chip info structure
2499 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002500 */
2501static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002502 int page)
William Juul52c07962007-10-31 13:53:06 +01002503{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002504 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002505}
2506
2507/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002508 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002509 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002510 * @mtd: mtd info structure
2511 * @chip: nand chip info structure
2512 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002513 */
2514static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002515 int page)
William Juul52c07962007-10-31 13:53:06 +01002516{
William Juul52c07962007-10-31 13:53:06 +01002517 int length = mtd->oobsize;
2518 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2519 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002520 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002521 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002522
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002523 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2524 if (ret)
2525 return ret;
2526
William Juul52c07962007-10-31 13:53:06 +01002527 for (i = 0; i < chip->ecc.steps; i++) {
2528 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002529 int ret;
2530
William Juul52c07962007-10-31 13:53:06 +01002531 pos = eccsize + i * (eccsize + chunk);
2532 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002533 ret = nand_change_read_column_op(chip, pos,
2534 NULL, 0,
2535 false);
William Juul52c07962007-10-31 13:53:06 +01002536 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002537 ret = nand_read_page_op(chip, page, pos, NULL,
2538 0);
2539
2540 if (ret)
2541 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002542 } else
William Juul52c07962007-10-31 13:53:06 +01002543 sndrnd = 1;
2544 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002545
2546 ret = nand_read_data_op(chip, bufpoi, toread, false);
2547 if (ret)
2548 return ret;
2549
William Juul52c07962007-10-31 13:53:06 +01002550 bufpoi += toread;
2551 length -= toread;
2552 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002553 if (length > 0) {
2554 ret = nand_read_data_op(chip, bufpoi, length, false);
2555 if (ret)
2556 return ret;
2557 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002558
Sergey Lapin3a38a552013-01-14 03:46:50 +00002559 return 0;
William Juul52c07962007-10-31 13:53:06 +01002560}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002561
William Juul52c07962007-10-31 13:53:06 +01002562/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002563 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2564 * @mtd: mtd info structure
2565 * @chip: nand chip info structure
2566 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002567 */
2568static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2569 int page)
2570{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002571 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2572 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002573}
2574
2575/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002576 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2577 * with syndrome - only for large page flash
2578 * @mtd: mtd info structure
2579 * @chip: nand chip info structure
2580 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002581 */
2582static int nand_write_oob_syndrome(struct mtd_info *mtd,
2583 struct nand_chip *chip, int page)
2584{
2585 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2586 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002587 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002588 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002589
2590 /*
William Juul52c07962007-10-31 13:53:06 +01002591 * data-ecc-data-ecc ... ecc-oob
2592 * or
2593 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002594 */
William Juul52c07962007-10-31 13:53:06 +01002595 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2596 pos = steps * (eccsize + chunk);
2597 steps = 0;
2598 } else
2599 pos = eccsize;
2600
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002601 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2602 if (ret)
2603 return ret;
2604
William Juul52c07962007-10-31 13:53:06 +01002605 for (i = 0; i < steps; i++) {
2606 if (sndcmd) {
2607 if (mtd->writesize <= 512) {
2608 uint32_t fill = 0xFFFFFFFF;
2609
2610 len = eccsize;
2611 while (len > 0) {
2612 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002613
2614 ret = nand_write_data_op(chip, &fill,
2615 num, false);
2616 if (ret)
2617 return ret;
2618
William Juul52c07962007-10-31 13:53:06 +01002619 len -= num;
2620 }
2621 } else {
2622 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002623 ret = nand_change_write_column_op(chip, pos,
2624 NULL, 0,
2625 false);
2626 if (ret)
2627 return ret;
William Juul52c07962007-10-31 13:53:06 +01002628 }
2629 } else
2630 sndcmd = 1;
2631 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002632
2633 ret = nand_write_data_op(chip, bufpoi, len, false);
2634 if (ret)
2635 return ret;
2636
William Juul52c07962007-10-31 13:53:06 +01002637 bufpoi += len;
2638 length -= len;
2639 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002640 if (length > 0) {
2641 ret = nand_write_data_op(chip, bufpoi, length, false);
2642 if (ret)
2643 return ret;
2644 }
William Juul52c07962007-10-31 13:53:06 +01002645
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002646 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002647}
2648
2649/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002650 * nand_do_read_oob - [INTERN] NAND read out-of-band
2651 * @mtd: MTD device structure
2652 * @from: offset to read from
2653 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002654 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002655 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002656 */
William Juul52c07962007-10-31 13:53:06 +01002657static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2658 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002659{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002660 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002661 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002662 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002663 int readlen = ops->ooblen;
2664 int len;
2665 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002666 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002667
Heiko Schocherf5895d12014-06-24 10:10:04 +02002668 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002669 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002670
Sergey Lapin3a38a552013-01-14 03:46:50 +00002671 stats = mtd->ecc_stats;
2672
Scott Wood52ab7ce2016-05-30 13:57:58 -05002673 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002674
William Juul52c07962007-10-31 13:53:06 +01002675 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002676 pr_debug("%s: attempt to start read outside oob\n",
2677 __func__);
William Juul52c07962007-10-31 13:53:06 +01002678 return -EINVAL;
2679 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002680
2681 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002682 if (unlikely(from >= mtd->size ||
2683 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2684 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002685 pr_debug("%s: attempt to read beyond end of device\n",
2686 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002687 return -EINVAL;
2688 }
2689
William Juul52c07962007-10-31 13:53:06 +01002690 chipnr = (int)(from >> chip->chip_shift);
2691 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002692
William Juul52c07962007-10-31 13:53:06 +01002693 /* Shift to get page */
2694 realpage = (int)(from >> chip->page_shift);
2695 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002696
Christian Hitz13fc0e22011-10-12 09:32:01 +02002697 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002698 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002699
Sergey Lapin3a38a552013-01-14 03:46:50 +00002700 if (ops->mode == MTD_OPS_RAW)
2701 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2702 else
2703 ret = chip->ecc.read_oob(mtd, chip, page);
2704
2705 if (ret < 0)
2706 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002707
William Juul52c07962007-10-31 13:53:06 +01002708 len = min(len, readlen);
2709 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002710
Heiko Schocherf5895d12014-06-24 10:10:04 +02002711 if (chip->options & NAND_NEED_READRDY) {
2712 /* Apply delay or wait for ready/busy pin */
2713 if (!chip->dev_ready)
2714 udelay(chip->chip_delay);
2715 else
2716 nand_wait_ready(mtd);
2717 }
2718
William Juul52c07962007-10-31 13:53:06 +01002719 readlen -= len;
2720 if (!readlen)
2721 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002722
William Juul52c07962007-10-31 13:53:06 +01002723 /* Increment page address */
2724 realpage++;
2725
2726 page = realpage & chip->pagemask;
2727 /* Check, if we cross a chip boundary */
2728 if (!page) {
2729 chipnr++;
2730 chip->select_chip(mtd, -1);
2731 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002732 }
William Juul52c07962007-10-31 13:53:06 +01002733 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002734 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002735
Sergey Lapin3a38a552013-01-14 03:46:50 +00002736 ops->oobretlen = ops->ooblen - readlen;
2737
2738 if (ret < 0)
2739 return ret;
2740
2741 if (mtd->ecc_stats.failed - stats.failed)
2742 return -EBADMSG;
2743
2744 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002745}
2746
2747/**
William Juul52c07962007-10-31 13:53:06 +01002748 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002749 * @mtd: MTD device structure
2750 * @from: offset to read from
2751 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002752 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002753 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002754 */
William Juul52c07962007-10-31 13:53:06 +01002755static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2756 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002757{
William Juul52c07962007-10-31 13:53:06 +01002758 int ret = -ENOTSUPP;
2759
2760 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002761
2762 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002763 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002764 pr_debug("%s: attempt to read beyond end of device\n",
2765 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002766 return -EINVAL;
2767 }
2768
Heiko Schocherf5895d12014-06-24 10:10:04 +02002769 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002770
Christian Hitz13fc0e22011-10-12 09:32:01 +02002771 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002772 case MTD_OPS_PLACE_OOB:
2773 case MTD_OPS_AUTO_OOB:
2774 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002775 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002776
William Juul52c07962007-10-31 13:53:06 +01002777 default:
2778 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779 }
2780
William Juul52c07962007-10-31 13:53:06 +01002781 if (!ops->datbuf)
2782 ret = nand_do_read_oob(mtd, from, ops);
2783 else
2784 ret = nand_do_read_ops(mtd, from, ops);
2785
Christian Hitz13fc0e22011-10-12 09:32:01 +02002786out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002787 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002788 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002789}
2790
2791
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002792/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002793 * nand_write_page_raw - [INTERN] raw page write function
2794 * @mtd: mtd info structure
2795 * @chip: nand chip info structure
2796 * @buf: data buffer
2797 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002798 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002799 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002800 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002801 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002802static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002803 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002804{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002805 int ret;
2806
2807 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2808 if (ret)
2809 return ret;
2810
2811 if (oob_required) {
2812 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2813 false);
2814 if (ret)
2815 return ret;
2816 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002817
2818 return 0;
William Juul52c07962007-10-31 13:53:06 +01002819}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002820
William Juul52c07962007-10-31 13:53:06 +01002821/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002822 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2823 * @mtd: mtd info structure
2824 * @chip: nand chip info structure
2825 * @buf: data buffer
2826 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002827 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002828 *
2829 * We need a special oob layout and handling even when ECC isn't checked.
2830 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002831static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002832 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002833 const uint8_t *buf, int oob_required,
2834 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002835{
2836 int eccsize = chip->ecc.size;
2837 int eccbytes = chip->ecc.bytes;
2838 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002839 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002840
2841 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002842 ret = nand_write_data_op(chip, buf, eccsize, false);
2843 if (ret)
2844 return ret;
2845
David Brownellee86b8d2009-11-07 16:27:01 -05002846 buf += eccsize;
2847
2848 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002849 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2850 false);
2851 if (ret)
2852 return ret;
2853
David Brownellee86b8d2009-11-07 16:27:01 -05002854 oob += chip->ecc.prepad;
2855 }
2856
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002857 ret = nand_write_data_op(chip, oob, eccbytes, false);
2858 if (ret)
2859 return ret;
2860
David Brownellee86b8d2009-11-07 16:27:01 -05002861 oob += eccbytes;
2862
2863 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002864 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2865 false);
2866 if (ret)
2867 return ret;
2868
David Brownellee86b8d2009-11-07 16:27:01 -05002869 oob += chip->ecc.postpad;
2870 }
2871 }
2872
2873 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002874 if (size) {
2875 ret = nand_write_data_op(chip, oob, size, false);
2876 if (ret)
2877 return ret;
2878 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002879
2880 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002881}
2882/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002883 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2884 * @mtd: mtd info structure
2885 * @chip: nand chip info structure
2886 * @buf: data buffer
2887 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002888 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002889 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002890static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002891 const uint8_t *buf, int oob_required,
2892 int page)
William Juul52c07962007-10-31 13:53:06 +01002893{
2894 int i, eccsize = chip->ecc.size;
2895 int eccbytes = chip->ecc.bytes;
2896 int eccsteps = chip->ecc.steps;
2897 uint8_t *ecc_calc = chip->buffers->ecccalc;
2898 const uint8_t *p = buf;
2899 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002900
Sergey Lapin3a38a552013-01-14 03:46:50 +00002901 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002902 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2903 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002904
William Juul52c07962007-10-31 13:53:06 +01002905 for (i = 0; i < chip->ecc.total; i++)
2906 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002907
Scott Wood46e13102016-05-30 13:57:57 -05002908 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002909}
2910
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002911/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002912 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2913 * @mtd: mtd info structure
2914 * @chip: nand chip info structure
2915 * @buf: data buffer
2916 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002917 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002918 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002919static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002920 const uint8_t *buf, int oob_required,
2921 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002922{
William Juul52c07962007-10-31 13:53:06 +01002923 int i, eccsize = chip->ecc.size;
2924 int eccbytes = chip->ecc.bytes;
2925 int eccsteps = chip->ecc.steps;
2926 uint8_t *ecc_calc = chip->buffers->ecccalc;
2927 const uint8_t *p = buf;
2928 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002929 int ret;
William Juul52c07962007-10-31 13:53:06 +01002930
2931 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2932 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002933
2934 ret = nand_write_data_op(chip, p, eccsize, false);
2935 if (ret)
2936 return ret;
2937
William Juul52c07962007-10-31 13:53:06 +01002938 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2939 }
2940
2941 for (i = 0; i < chip->ecc.total; i++)
2942 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2943
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002944 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2945 if (ret)
2946 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002947
2948 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002949}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002950
Heiko Schocherf5895d12014-06-24 10:10:04 +02002951
2952/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002953 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002954 * @mtd: mtd info structure
2955 * @chip: nand chip info structure
2956 * @offset: column address of subpage within the page
2957 * @data_len: data length
2958 * @buf: data buffer
2959 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002960 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002961 */
2962static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2963 struct nand_chip *chip, uint32_t offset,
2964 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002965 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002966{
2967 uint8_t *oob_buf = chip->oob_poi;
2968 uint8_t *ecc_calc = chip->buffers->ecccalc;
2969 int ecc_size = chip->ecc.size;
2970 int ecc_bytes = chip->ecc.bytes;
2971 int ecc_steps = chip->ecc.steps;
2972 uint32_t *eccpos = chip->ecc.layout->eccpos;
2973 uint32_t start_step = offset / ecc_size;
2974 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2975 int oob_bytes = mtd->oobsize / ecc_steps;
2976 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002977 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002978
2979 for (step = 0; step < ecc_steps; step++) {
2980 /* configure controller for WRITE access */
2981 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2982
2983 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002984 ret = nand_write_data_op(chip, buf, ecc_size, false);
2985 if (ret)
2986 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002987
2988 /* mask ECC of un-touched subpages by padding 0xFF */
2989 if ((step < start_step) || (step > end_step))
2990 memset(ecc_calc, 0xff, ecc_bytes);
2991 else
2992 chip->ecc.calculate(mtd, buf, ecc_calc);
2993
2994 /* mask OOB of un-touched subpages by padding 0xFF */
2995 /* if oob_required, preserve OOB metadata of written subpage */
2996 if (!oob_required || (step < start_step) || (step > end_step))
2997 memset(oob_buf, 0xff, oob_bytes);
2998
2999 buf += ecc_size;
3000 ecc_calc += ecc_bytes;
3001 oob_buf += oob_bytes;
3002 }
3003
3004 /* copy calculated ECC for whole page to chip->buffer->oob */
3005 /* this include masked-value(0xFF) for unwritten subpages */
3006 ecc_calc = chip->buffers->ecccalc;
3007 for (i = 0; i < chip->ecc.total; i++)
3008 chip->oob_poi[eccpos[i]] = ecc_calc[i];
3009
3010 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003011 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3012 if (ret)
3013 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003014
3015 return 0;
3016}
3017
3018
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003019/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003020 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
3021 * @mtd: mtd info structure
3022 * @chip: nand chip info structure
3023 * @buf: data buffer
3024 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003025 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003026 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003027 * The hw generator calculates the error syndrome automatically. Therefore we
3028 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003029 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003030static int nand_write_page_syndrome(struct mtd_info *mtd,
3031 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003032 const uint8_t *buf, int oob_required,
3033 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003034{
William Juul52c07962007-10-31 13:53:06 +01003035 int i, eccsize = chip->ecc.size;
3036 int eccbytes = chip->ecc.bytes;
3037 int eccsteps = chip->ecc.steps;
3038 const uint8_t *p = buf;
3039 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003040 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003041
William Juul52c07962007-10-31 13:53:06 +01003042 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003043 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003044
3045 ret = nand_write_data_op(chip, p, eccsize, false);
3046 if (ret)
3047 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003048
William Juul52c07962007-10-31 13:53:06 +01003049 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003050 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3051 false);
3052 if (ret)
3053 return ret;
3054
William Juul52c07962007-10-31 13:53:06 +01003055 oob += chip->ecc.prepad;
3056 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003057
William Juul52c07962007-10-31 13:53:06 +01003058 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003059
3060 ret = nand_write_data_op(chip, oob, eccbytes, false);
3061 if (ret)
3062 return ret;
3063
William Juul52c07962007-10-31 13:53:06 +01003064 oob += eccbytes;
3065
3066 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003067 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3068 false);
3069 if (ret)
3070 return ret;
3071
William Juul52c07962007-10-31 13:53:06 +01003072 oob += chip->ecc.postpad;
3073 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003074 }
3075
William Juul52c07962007-10-31 13:53:06 +01003076 /* Calculate remaining oob bytes */
3077 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003078 if (i) {
3079 ret = nand_write_data_op(chip, oob, i, false);
3080 if (ret)
3081 return ret;
3082 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003083
3084 return 0;
William Juul52c07962007-10-31 13:53:06 +01003085}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003086
William Juul52c07962007-10-31 13:53:06 +01003087/**
3088 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003089 * @mtd: MTD device structure
3090 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003091 * @offset: address offset within the page
3092 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003093 * @buf: the data to write
3094 * @oob_required: must write chip->oob_poi to OOB
3095 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003096 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003097 */
3098static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003099 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003100 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003101{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003102 int status, subpage;
3103
3104 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3105 chip->ecc.write_subpage)
3106 subpage = offset || (data_len < mtd->writesize);
3107 else
3108 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003109
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003110 if (nand_standard_page_accessors(&chip->ecc)) {
3111 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3112 if (status)
3113 return status;
3114 }
William Juul52c07962007-10-31 13:53:06 +01003115
3116 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003117 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003118 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003119 else if (subpage)
3120 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003121 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003122 else
Scott Wood46e13102016-05-30 13:57:57 -05003123 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3124 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003125
3126 if (status < 0)
3127 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003128
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003129 if (nand_standard_page_accessors(&chip->ecc))
3130 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003131
William Juul52c07962007-10-31 13:53:06 +01003132 return 0;
3133}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003134
William Juul52c07962007-10-31 13:53:06 +01003135/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003136 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3137 * @mtd: MTD device structure
3138 * @oob: oob data buffer
3139 * @len: oob data write length
3140 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003141 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003142static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3143 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003144{
Scott Wood17fed142016-05-30 13:57:56 -05003145 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003146
3147 /*
3148 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3149 * data from a previous OOB read.
3150 */
3151 memset(chip->oob_poi, 0xff, mtd->oobsize);
3152
Christian Hitz13fc0e22011-10-12 09:32:01 +02003153 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003154
Sergey Lapin3a38a552013-01-14 03:46:50 +00003155 case MTD_OPS_PLACE_OOB:
3156 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003157 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3158 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003159
Sergey Lapin3a38a552013-01-14 03:46:50 +00003160 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003161 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3162 uint32_t boffs = 0, woffs = ops->ooboffs;
3163 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003164
Christian Hitz13fc0e22011-10-12 09:32:01 +02003165 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003166 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003167 if (unlikely(woffs)) {
3168 if (woffs >= free->length) {
3169 woffs -= free->length;
3170 continue;
3171 }
3172 boffs = free->offset + woffs;
3173 bytes = min_t(size_t, len,
3174 (free->length - woffs));
3175 woffs = 0;
3176 } else {
3177 bytes = min_t(size_t, len, free->length);
3178 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003179 }
William Juul52c07962007-10-31 13:53:06 +01003180 memcpy(chip->oob_poi + boffs, oob, bytes);
3181 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003182 }
William Juul52c07962007-10-31 13:53:06 +01003183 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003184 }
William Juul52c07962007-10-31 13:53:06 +01003185 default:
3186 BUG();
3187 }
3188 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003189}
3190
Christian Hitzb8a6b372011-10-12 09:32:02 +02003191#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003192
3193/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003194 * nand_do_write_ops - [INTERN] NAND write with ECC
3195 * @mtd: MTD device structure
3196 * @to: offset to write to
3197 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003198 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003199 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003200 */
William Juul52c07962007-10-31 13:53:06 +01003201static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3202 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003203{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003204 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003205 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003206 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003207
3208 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003209 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003210
William Juul52c07962007-10-31 13:53:06 +01003211 uint8_t *oob = ops->oobbuf;
3212 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003213 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003214 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003215
William Juul52c07962007-10-31 13:53:06 +01003216 ops->retlen = 0;
3217 if (!writelen)
3218 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003219
Heiko Schocherf5895d12014-06-24 10:10:04 +02003220 /* Reject writes, which are not page aligned */
3221 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003222 pr_notice("%s: attempt to write non page aligned data\n",
3223 __func__);
William Juul52c07962007-10-31 13:53:06 +01003224 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003225 }
3226
3227 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003228
William Juul52c07962007-10-31 13:53:06 +01003229 chipnr = (int)(to >> chip->chip_shift);
3230 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003231
3232 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003233 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003234 ret = -EIO;
3235 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003236 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003237
William Juul52c07962007-10-31 13:53:06 +01003238 realpage = (int)(to >> chip->page_shift);
3239 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003240
William Juul52c07962007-10-31 13:53:06 +01003241 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003242 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3243 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003244 chip->pagebuf = -1;
3245
Christian Hitzb8a6b372011-10-12 09:32:02 +02003246 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003247 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3248 ret = -EINVAL;
3249 goto err_out;
3250 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003251
Christian Hitz13fc0e22011-10-12 09:32:01 +02003252 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003253 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003254 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003255 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003256 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003257
3258 if (part_pagewr)
3259 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003260 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3261 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3262 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003263 else
3264 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003265
Heiko Schocherf5895d12014-06-24 10:10:04 +02003266 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003267 /* Partial page write?, or need to use bounce buffer */
3268 if (use_bufpoi) {
3269 pr_debug("%s: using write bounce buffer for buf@%p\n",
3270 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003271 if (part_pagewr)
3272 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003273 chip->pagebuf = -1;
3274 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3275 memcpy(&chip->buffers->databuf[column], buf, bytes);
3276 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003277 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003278
Christian Hitzb8a6b372011-10-12 09:32:02 +02003279 if (unlikely(oob)) {
3280 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003281 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003282 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003283 } else {
3284 /* We still need to erase leftover OOB data */
3285 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003286 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003287 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003288 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003289 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003290 if (ret)
3291 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003292
William Juul52c07962007-10-31 13:53:06 +01003293 writelen -= bytes;
3294 if (!writelen)
3295 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003296
Heiko Schocherf5895d12014-06-24 10:10:04 +02003297 column = 0;
3298 buf += bytes;
3299 realpage++;
3300
3301 page = realpage & chip->pagemask;
3302 /* Check, if we cross a chip boundary */
3303 if (!page) {
3304 chipnr++;
3305 chip->select_chip(mtd, -1);
3306 chip->select_chip(mtd, chipnr);
3307 }
3308 }
3309
3310 ops->retlen = ops->len - writelen;
3311 if (unlikely(oob))
3312 ops->oobretlen = ops->ooblen;
3313
3314err_out:
3315 chip->select_chip(mtd, -1);
3316 return ret;
3317}
3318
3319/**
3320 * panic_nand_write - [MTD Interface] NAND write with ECC
3321 * @mtd: MTD device structure
3322 * @to: offset to write to
3323 * @len: number of bytes to write
3324 * @retlen: pointer to variable to store the number of written bytes
3325 * @buf: the data to write
3326 *
3327 * NAND write with ECC. Used when performing writes in interrupt context, this
3328 * may for example be called by mtdoops when writing an oops while in panic.
3329 */
3330static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3331 size_t *retlen, const uint8_t *buf)
3332{
Scott Wood17fed142016-05-30 13:57:56 -05003333 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003334 struct mtd_oob_ops ops;
3335 int ret;
3336
3337 /* Wait for the device to get ready */
3338 panic_nand_wait(mtd, chip, 400);
3339
3340 /* Grab the device */
3341 panic_nand_get_device(chip, mtd, FL_WRITING);
3342
Scott Wood3ea94ed2015-06-26 19:03:26 -05003343 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003344 ops.len = len;
3345 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003346 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003347
Heiko Schocherf5895d12014-06-24 10:10:04 +02003348 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003349
Sergey Lapin3a38a552013-01-14 03:46:50 +00003350 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003351 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003352}
3353
3354/**
William Juul52c07962007-10-31 13:53:06 +01003355 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003356 * @mtd: MTD device structure
3357 * @to: offset to write to
3358 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003359 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003360 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003361 */
William Juul52c07962007-10-31 13:53:06 +01003362static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3363 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003364{
William Juul52c07962007-10-31 13:53:06 +01003365 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003366 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003367
Heiko Schocherf5895d12014-06-24 10:10:04 +02003368 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003369 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003370
Scott Wood52ab7ce2016-05-30 13:57:58 -05003371 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003372
3373 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003374 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003375 pr_debug("%s: attempt to write past end of page\n",
3376 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003377 return -EINVAL;
3378 }
3379
William Juul52c07962007-10-31 13:53:06 +01003380 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003381 pr_debug("%s: attempt to start write outside oob\n",
3382 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003383 return -EINVAL;
3384 }
3385
Christian Hitz13fc0e22011-10-12 09:32:01 +02003386 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003387 if (unlikely(to >= mtd->size ||
3388 ops->ooboffs + ops->ooblen >
3389 ((mtd->size >> chip->page_shift) -
3390 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003391 pr_debug("%s: attempt to write beyond end of device\n",
3392 __func__);
William Juul52c07962007-10-31 13:53:06 +01003393 return -EINVAL;
3394 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003395
William Juul52c07962007-10-31 13:53:06 +01003396 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003397
3398 /*
3399 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3400 * of my DiskOnChip 2000 test units) will clear the whole data page too
3401 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3402 * it in the doc2000 driver in August 1999. dwmw2.
3403 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003404 nand_reset(chip, chipnr);
3405
3406 chip->select_chip(mtd, chipnr);
3407
3408 /* Shift to get page */
3409 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003410
3411 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003412 if (nand_check_wp(mtd)) {
3413 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003414 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003415 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003416
William Juul52c07962007-10-31 13:53:06 +01003417 /* Invalidate the page cache, if we write to the cached page */
3418 if (page == chip->pagebuf)
3419 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003420
Sergey Lapin3a38a552013-01-14 03:46:50 +00003421 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3422
3423 if (ops->mode == MTD_OPS_RAW)
3424 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3425 else
3426 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003427
Heiko Schocherf5895d12014-06-24 10:10:04 +02003428 chip->select_chip(mtd, -1);
3429
William Juul52c07962007-10-31 13:53:06 +01003430 if (status)
3431 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003432
William Juul52c07962007-10-31 13:53:06 +01003433 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003434
William Juul52c07962007-10-31 13:53:06 +01003435 return 0;
3436}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003437
William Juul52c07962007-10-31 13:53:06 +01003438/**
3439 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003440 * @mtd: MTD device structure
3441 * @to: offset to write to
3442 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003443 */
3444static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3445 struct mtd_oob_ops *ops)
3446{
William Juul52c07962007-10-31 13:53:06 +01003447 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003448
William Juul52c07962007-10-31 13:53:06 +01003449 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003450
William Juul52c07962007-10-31 13:53:06 +01003451 /* Do not allow writes past end of device */
3452 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003453 pr_debug("%s: attempt to write beyond end of device\n",
3454 __func__);
William Juul52c07962007-10-31 13:53:06 +01003455 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003456 }
William Juul52c07962007-10-31 13:53:06 +01003457
Heiko Schocherf5895d12014-06-24 10:10:04 +02003458 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003459
Christian Hitz13fc0e22011-10-12 09:32:01 +02003460 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003461 case MTD_OPS_PLACE_OOB:
3462 case MTD_OPS_AUTO_OOB:
3463 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003464 break;
3465
3466 default:
3467 goto out;
3468 }
3469
3470 if (!ops->datbuf)
3471 ret = nand_do_write_oob(mtd, to, ops);
3472 else
3473 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003474
Christian Hitz13fc0e22011-10-12 09:32:01 +02003475out:
William Juul52c07962007-10-31 13:53:06 +01003476 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003477 return ret;
3478}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003479
3480/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003481 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003482 * @mtd: MTD device structure
3483 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003484 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003485 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003486 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003487static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003488{
Scott Wood17fed142016-05-30 13:57:56 -05003489 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003490 unsigned int eraseblock;
3491
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003492 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003493 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003494
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003495 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003496}
3497
3498/**
3499 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003500 * @mtd: MTD device structure
3501 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003502 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003503 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003504 */
William Juul52c07962007-10-31 13:53:06 +01003505static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003506{
William Juul52c07962007-10-31 13:53:06 +01003507 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003508}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003509
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003510/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003511 * nand_erase_nand - [INTERN] erase block(s)
3512 * @mtd: MTD device structure
3513 * @instr: erase instruction
3514 * @allowbbt: allow erasing the bbt area
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 +01003518int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3519 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003520{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003521 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003522 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003523 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003524
Heiko Schocherf5895d12014-06-24 10:10:04 +02003525 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3526 __func__, (unsigned long long)instr->addr,
3527 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003528
Christian Hitzb8a6b372011-10-12 09:32:02 +02003529 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003530 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003531
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003532 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003533 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003534
3535 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003536 page = (int)(instr->addr >> chip->page_shift);
3537 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003538
3539 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003540 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003541
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003542 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003543 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003544
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003545 /* Check, if it is write protected */
3546 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003547 pr_debug("%s: device is write protected!\n",
3548 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003549 instr->state = MTD_ERASE_FAILED;
3550 goto erase_exit;
3551 }
3552
3553 /* Loop through the pages */
3554 len = instr->len;
3555
3556 instr->state = MTD_ERASING;
3557
3558 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06003559 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003560
Sergey Lapin3a38a552013-01-14 03:46:50 +00003561 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003562 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003563 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003564 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003565 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003566 instr->state = MTD_ERASE_FAILED;
Farhan Ali7c053192021-02-24 15:25:53 -08003567 instr->fail_addr =
3568 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003569 goto erase_exit;
3570 }
William Juul52c07962007-10-31 13:53:06 +01003571
3572 /*
3573 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003574 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003575 */
3576 if (page <= chip->pagebuf && chip->pagebuf <
3577 (page + pages_per_block))
3578 chip->pagebuf = -1;
3579
Scott Wood3ea94ed2015-06-26 19:03:26 -05003580 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003581
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003582 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003583 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003584 pr_debug("%s: failed erase, page 0x%08x\n",
3585 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003586 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003587 instr->fail_addr =
3588 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003589 goto erase_exit;
3590 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003591
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003592 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003593 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003594 page += pages_per_block;
3595
3596 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003597 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003598 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003599 chip->select_chip(mtd, -1);
3600 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003601 }
3602 }
3603 instr->state = MTD_ERASE_DONE;
3604
Christian Hitz13fc0e22011-10-12 09:32:01 +02003605erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003606
3607 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003608
3609 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003610 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003611 nand_release_device(mtd);
3612
3613 /* Return more or less happy */
3614 return ret;
3615}
3616
3617/**
3618 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003619 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003620 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003621 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003622 */
William Juul52c07962007-10-31 13:53:06 +01003623static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003624{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003625 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003626
3627 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003628 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003629 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003630 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003631}
3632
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003633/**
William Juul52c07962007-10-31 13:53:06 +01003634 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003635 * @mtd: MTD device structure
3636 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003637 */
William Juul52c07962007-10-31 13:53:06 +01003638static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003639{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003640 struct nand_chip *chip = mtd_to_nand(mtd);
3641 int chipnr = (int)(offs >> chip->chip_shift);
3642 int ret;
3643
3644 /* Select the NAND device */
3645 nand_get_device(mtd, FL_READING);
3646 chip->select_chip(mtd, chipnr);
3647
3648 ret = nand_block_checkbad(mtd, offs, 0);
3649
3650 chip->select_chip(mtd, -1);
3651 nand_release_device(mtd);
3652
3653 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003654}
3655
3656/**
William Juul52c07962007-10-31 13:53:06 +01003657 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003658 * @mtd: MTD device structure
3659 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003660 */
William Juul52c07962007-10-31 13:53:06 +01003661static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003662{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003663 int ret;
3664
Christian Hitzb8a6b372011-10-12 09:32:02 +02003665 ret = nand_block_isbad(mtd, ofs);
3666 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003667 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003668 if (ret > 0)
3669 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003670 return ret;
3671 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003672
Heiko Schocherf5895d12014-06-24 10:10:04 +02003673 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003674}
3675
Heiko Schocherf5895d12014-06-24 10:10:04 +02003676/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003677 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3678 * @mtd: MTD device structure
3679 * @chip: nand chip info structure
3680 * @addr: feature address.
3681 * @subfeature_param: the subfeature parameters, a four bytes array.
3682 */
3683static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3684 int addr, uint8_t *subfeature_param)
3685{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003686#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3687 if (!chip->onfi_version ||
3688 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3689 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003690 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003691#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003692
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003693 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003694}
3695
3696/**
3697 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3698 * @mtd: MTD device structure
3699 * @chip: nand chip info structure
3700 * @addr: feature address.
3701 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003702 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003703static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3704 int addr, uint8_t *subfeature_param)
3705{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003706#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3707 if (!chip->onfi_version ||
3708 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3709 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003710 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003711#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003712
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003713 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003714}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003715
Sergey Lapin3a38a552013-01-14 03:46:50 +00003716/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003717static void nand_set_defaults(struct nand_chip *chip, int busw)
3718{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003719 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003720 if (!chip->chip_delay)
3721 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003722
3723 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003724 if (chip->cmdfunc == NULL)
3725 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003726
3727 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003728 if (chip->waitfunc == NULL)
3729 chip->waitfunc = nand_wait;
3730
3731 if (!chip->select_chip)
3732 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003733
3734 /* set for ONFI nand */
3735 if (!chip->onfi_set_features)
3736 chip->onfi_set_features = nand_onfi_set_features;
3737 if (!chip->onfi_get_features)
3738 chip->onfi_get_features = nand_onfi_get_features;
3739
3740 /* If called twice, pointers that depend on busw may need to be reset */
3741 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003742 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3743 if (!chip->read_word)
3744 chip->read_word = nand_read_word;
3745 if (!chip->block_bad)
3746 chip->block_bad = nand_block_bad;
3747 if (!chip->block_markbad)
3748 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003749 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003750 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003751 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3752 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3753 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003754 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003755 if (!chip->scan_bbt)
3756 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003757
3758 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003759 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003760 spin_lock_init(&chip->controller->lock);
3761 init_waitqueue_head(&chip->controller->wq);
3762 }
3763
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003764 if (!chip->buf_align)
3765 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003766}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003767
Sergey Lapin3a38a552013-01-14 03:46:50 +00003768/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003769static void sanitize_string(char *s, size_t len)
3770{
3771 ssize_t i;
3772
Sergey Lapin3a38a552013-01-14 03:46:50 +00003773 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003774 s[len - 1] = 0;
3775
Sergey Lapin3a38a552013-01-14 03:46:50 +00003776 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003777 for (i = 0; i < len - 1; i++) {
3778 if (s[i] < ' ' || s[i] > 127)
3779 s[i] = '?';
3780 }
3781
Sergey Lapin3a38a552013-01-14 03:46:50 +00003782 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003783 strim(s);
3784}
3785
Florian Fainellic98a9352011-02-25 00:01:34 +00003786static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3787{
3788 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003789 while (len--) {
3790 crc ^= *p++ << 8;
3791 for (i = 0; i < 8; i++)
3792 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3793 }
3794
3795 return crc;
3796}
3797
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003798#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003799/* Parse the Extended Parameter Page. */
3800static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3801 struct nand_chip *chip, struct nand_onfi_params *p)
3802{
3803 struct onfi_ext_param_page *ep;
3804 struct onfi_ext_section *s;
3805 struct onfi_ext_ecc_info *ecc;
3806 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003807 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003808 int len;
3809 int i;
3810
3811 len = le16_to_cpu(p->ext_param_page_length) * 16;
3812 ep = kmalloc(len, GFP_KERNEL);
3813 if (!ep)
3814 return -ENOMEM;
3815
3816 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003817 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3818 if (ret)
3819 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003820
3821 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003822 ret = nand_change_read_column_op(chip,
3823 sizeof(*p) * p->num_of_param_pages,
3824 ep, len, true);
3825 if (ret)
3826 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003827
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003828 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003829 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3830 != le16_to_cpu(ep->crc))) {
3831 pr_debug("fail in the CRC.\n");
3832 goto ext_out;
3833 }
3834
3835 /*
3836 * Check the signature.
3837 * Do not strictly follow the ONFI spec, maybe changed in future.
3838 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003839 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003840 pr_debug("The signature is invalid.\n");
3841 goto ext_out;
3842 }
3843
3844 /* find the ECC section. */
3845 cursor = (uint8_t *)(ep + 1);
3846 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3847 s = ep->sections + i;
3848 if (s->type == ONFI_SECTION_TYPE_2)
3849 break;
3850 cursor += s->length * 16;
3851 }
3852 if (i == ONFI_EXT_SECTION_MAX) {
3853 pr_debug("We can not find the ECC section.\n");
3854 goto ext_out;
3855 }
3856
3857 /* get the info we want. */
3858 ecc = (struct onfi_ext_ecc_info *)cursor;
3859
3860 if (!ecc->codeword_size) {
3861 pr_debug("Invalid codeword size\n");
3862 goto ext_out;
3863 }
3864
3865 chip->ecc_strength_ds = ecc->ecc_bits;
3866 chip->ecc_step_ds = 1 << ecc->codeword_size;
3867 ret = 0;
3868
3869ext_out:
3870 kfree(ep);
3871 return ret;
3872}
3873
3874static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3875{
Scott Wood17fed142016-05-30 13:57:56 -05003876 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003877 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3878
3879 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3880 feature);
3881}
3882
3883/*
3884 * Configure chip properties from Micron vendor-specific ONFI table
3885 */
3886static void nand_onfi_detect_micron(struct nand_chip *chip,
3887 struct nand_onfi_params *p)
3888{
3889 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3890
3891 if (le16_to_cpu(p->vendor_revision) < 1)
3892 return;
3893
3894 chip->read_retries = micron->read_retry_options;
3895 chip->setup_read_retry = nand_setup_read_retry_micron;
3896}
3897
Florian Fainellic98a9352011-02-25 00:01:34 +00003898/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003899 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003900 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003901static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00003902{
3903 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003904 char id[4];
3905 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003906
Sergey Lapin3a38a552013-01-14 03:46:50 +00003907 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003908 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3909 if (ret || strncmp(id, "ONFI", 4))
3910 return 0;
3911
3912 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3913 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003914 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003915
Florian Fainellic98a9352011-02-25 00:01:34 +00003916 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003917 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3918 if (ret)
3919 return 0;
3920
Florian Fainellic98a9352011-02-25 00:01:34 +00003921 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003922 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003923 break;
3924 }
3925 }
3926
Heiko Schocherf5895d12014-06-24 10:10:04 +02003927 if (i == 3) {
3928 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003929 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003930 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003931
Sergey Lapin3a38a552013-01-14 03:46:50 +00003932 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003933 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003934 if (val & (1 << 5))
3935 chip->onfi_version = 23;
3936 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003937 chip->onfi_version = 22;
3938 else if (val & (1 << 3))
3939 chip->onfi_version = 21;
3940 else if (val & (1 << 2))
3941 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003942 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003943 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003944
3945 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003946 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003947 return 0;
3948 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003949
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003950 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3951 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003952 if (!mtd->name)
3953 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003954
Florian Fainellic98a9352011-02-25 00:01:34 +00003955 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003956
3957 /*
3958 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3959 * (don't ask me who thought of this...). MTD assumes that these
3960 * dimensions will be power-of-2, so just truncate the remaining area.
3961 */
3962 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3963 mtd->erasesize *= mtd->writesize;
3964
Florian Fainellic98a9352011-02-25 00:01:34 +00003965 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003966
3967 /* See erasesize comment */
3968 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003969 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003970 chip->bits_per_cell = p->bits_per_cell;
3971
3972 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02003973 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003974
3975 if (p->ecc_bits != 0xff) {
3976 chip->ecc_strength_ds = p->ecc_bits;
3977 chip->ecc_step_ds = 512;
3978 } else if (chip->onfi_version >= 21 &&
3979 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3980
3981 /*
3982 * The nand_flash_detect_ext_param_page() uses the
3983 * Change Read Column command which maybe not supported
3984 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3985 * now. We do not replace user supplied command function.
3986 */
3987 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3988 chip->cmdfunc = nand_command_lp;
3989
3990 /* The Extended Parameter Page is supported since ONFI 2.1. */
3991 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3992 pr_warn("Failed to detect ONFI extended param page\n");
3993 } else {
3994 pr_warn("Could not retrieve ONFI ECC requirements\n");
3995 }
3996
3997 if (p->jedec_id == NAND_MFR_MICRON)
3998 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003999
4000 return 1;
4001}
4002#else
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004003static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip)
Florian Fainellic98a9352011-02-25 00:01:34 +00004004{
4005 return 0;
4006}
4007#endif
4008
William Juul52c07962007-10-31 13:53:06 +01004009/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004010 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4011 */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004012static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip)
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004013{
4014 struct nand_jedec_params *p = &chip->jedec_params;
4015 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004016 char id[5];
4017 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004018
4019 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004020 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4021 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004022 return 0;
4023
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004024 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4025 if (ret)
4026 return 0;
4027
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004028 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004029 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4030 if (ret)
4031 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004032
4033 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4034 le16_to_cpu(p->crc))
4035 break;
4036 }
4037
4038 if (i == 3) {
4039 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4040 return 0;
4041 }
4042
4043 /* Check version */
4044 val = le16_to_cpu(p->revision);
4045 if (val & (1 << 2))
4046 chip->jedec_version = 10;
4047 else if (val & (1 << 1))
4048 chip->jedec_version = 1; /* vendor specific version */
4049
4050 if (!chip->jedec_version) {
4051 pr_info("unsupported JEDEC version: %d\n", val);
4052 return 0;
4053 }
4054
4055 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4056 sanitize_string(p->model, sizeof(p->model));
4057 if (!mtd->name)
4058 mtd->name = p->model;
4059
4060 mtd->writesize = le32_to_cpu(p->byte_per_page);
4061
4062 /* Please reference to the comment for nand_flash_detect_onfi. */
4063 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4064 mtd->erasesize *= mtd->writesize;
4065
4066 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4067
4068 /* Please reference to the comment for nand_flash_detect_onfi. */
4069 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4070 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4071 chip->bits_per_cell = p->bits_per_cell;
4072
4073 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004074 chip->options |= NAND_BUSWIDTH_16;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004075
4076 /* ECC info */
4077 ecc = &p->ecc_info[0];
4078
4079 if (ecc->codeword_size >= 9) {
4080 chip->ecc_strength_ds = ecc->ecc_bits;
4081 chip->ecc_step_ds = 1 << ecc->codeword_size;
4082 } else {
4083 pr_warn("Invalid codeword size\n");
4084 }
4085
4086 return 1;
4087}
4088
4089/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004090 * nand_id_has_period - Check if an ID string has a given wraparound period
4091 * @id_data: the ID string
4092 * @arrlen: the length of the @id_data array
4093 * @period: the period of repitition
4094 *
4095 * Check if an ID string is repeated within a given sequence of bytes at
4096 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02004097 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00004098 * if the repetition has a period of @period; otherwise, returns zero.
4099 */
4100static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4101{
4102 int i, j;
4103 for (i = 0; i < period; i++)
4104 for (j = i + period; j < arrlen; j += period)
4105 if (id_data[i] != id_data[j])
4106 return 0;
4107 return 1;
4108}
4109
4110/*
4111 * nand_id_len - Get the length of an ID string returned by CMD_READID
4112 * @id_data: the ID string
4113 * @arrlen: the length of the @id_data array
4114
4115 * Returns the length of the ID string, according to known wraparound/trailing
4116 * zero patterns. If no pattern exists, returns the length of the array.
4117 */
4118static int nand_id_len(u8 *id_data, int arrlen)
4119{
4120 int last_nonzero, period;
4121
4122 /* Find last non-zero byte */
4123 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4124 if (id_data[last_nonzero])
4125 break;
4126
4127 /* All zeros */
4128 if (last_nonzero < 0)
4129 return 0;
4130
4131 /* Calculate wraparound period */
4132 for (period = 1; period < arrlen; period++)
4133 if (nand_id_has_period(id_data, arrlen, period))
4134 break;
4135
4136 /* There's a repeated pattern */
4137 if (period < arrlen)
4138 return period;
4139
4140 /* There are trailing zeros */
4141 if (last_nonzero < arrlen - 1)
4142 return last_nonzero + 1;
4143
4144 /* No pattern detected */
4145 return arrlen;
4146}
4147
Heiko Schocherf5895d12014-06-24 10:10:04 +02004148/* Extract the bits of per cell from the 3rd byte of the extended ID */
4149static int nand_get_bits_per_cell(u8 cellinfo)
4150{
4151 int bits;
4152
4153 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4154 bits >>= NAND_CI_CELLTYPE_SHIFT;
4155 return bits + 1;
4156}
4157
Sergey Lapin3a38a552013-01-14 03:46:50 +00004158/*
4159 * Many new NAND share similar device ID codes, which represent the size of the
4160 * chip. The rest of the parameters must be decoded according to generic or
4161 * manufacturer-specific "extended ID" decoding patterns.
4162 */
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004163void nand_decode_ext_id(struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004164{
Michael Trimarchi270c1532022-07-20 18:22:07 +02004165 struct mtd_info *mtd = &chip->mtd;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004166 int extid, id_len;
4167 /* The 3rd id byte holds MLC / multichip data */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004168 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004169 /* The 4th id byte is the important one */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004170 extid = chip->id.data[3];
4171 id_len = chip->id.len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004172
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004173 /* Calc pagesize */
4174 mtd->writesize = 1024 << (extid & 0x03);
4175 extid >>= 2;
4176 /* Calc oobsize */
4177 mtd->oobsize = (8 << (extid & 0x01)) *
4178 (mtd->writesize >> 9);
4179 extid >>= 2;
4180 /* Calc blocksize. Blocksize is multiples of 64KiB */
4181 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4182 extid >>= 2;
4183 /* Get buswidth information */
4184 /* Get buswidth information */
4185 if (extid & 0x1)
4186 chip->options |= NAND_BUSWIDTH_16;
4187
Sergey Lapin3a38a552013-01-14 03:46:50 +00004188 /*
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004189 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
4190 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
4191 * follows:
4192 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
4193 * 110b -> 24nm
4194 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
Sergey Lapin3a38a552013-01-14 03:46:50 +00004195 */
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004196 if (id_len >= 6 && chip->id.data[0] == NAND_MFR_TOSHIBA &&
4197 nand_is_slc(chip) &&
4198 (chip->id.data[5] & 0x7) == 0x6 /* 24nm */ &&
4199 !(chip->id.data[4] & 0x80) /* !BENAND */) {
4200 mtd->oobsize = 32 * mtd->writesize >> 9;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004201 }
4202}
Michael Trimarchi60f26dc2022-07-20 18:22:08 +02004203EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004204
Heiko Schocherf5895d12014-06-24 10:10:04 +02004205/*
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004206 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4207 * compliant and does not have a full-id or legacy-id entry in the nand_ids
4208 * table.
4209 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004210static void nand_manufacturer_detect(struct nand_chip *chip)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004211{
4212 /*
4213 * Try manufacturer detection if available and use
4214 * nand_decode_ext_id() otherwise.
4215 */
4216 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4217 chip->manufacturer.desc->ops->detect)
4218 chip->manufacturer.desc->ops->detect(chip);
4219 else
Michael Trimarchi270c1532022-07-20 18:22:07 +02004220 nand_decode_ext_id(chip);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004221}
4222
4223/*
4224 * Manufacturer initialization. This function is called for all NANDs including
4225 * ONFI and JEDEC compliant ones.
4226 * Manufacturer drivers should put all their specific initialization code in
4227 * their ->init() hook.
4228 */
4229static int nand_manufacturer_init(struct nand_chip *chip)
4230{
4231 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4232 !chip->manufacturer.desc->ops->init)
4233 return 0;
4234
4235 return chip->manufacturer.desc->ops->init(chip);
4236}
4237
4238/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004239 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4240 * decodes a matching ID table entry and assigns the MTD size parameters for
4241 * the chip.
4242 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004243static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004244{
Michael Trimarchi270c1532022-07-20 18:22:07 +02004245 struct mtd_info *mtd = &chip->mtd;
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004246 int maf_id = chip->id.data[0];
Sergey Lapin3a38a552013-01-14 03:46:50 +00004247
4248 mtd->erasesize = type->erasesize;
4249 mtd->writesize = type->pagesize;
4250 mtd->oobsize = mtd->writesize / 32;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004251
Heiko Schocherf5895d12014-06-24 10:10:04 +02004252 /* All legacy ID NAND are small-page, SLC */
4253 chip->bits_per_cell = 1;
4254
Sergey Lapin3a38a552013-01-14 03:46:50 +00004255 /*
4256 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
4257 * some Spansion chips have erasesize that conflicts with size
4258 * listed in nand_ids table.
4259 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
4260 */
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004261 if (maf_id == NAND_MFR_AMD && chip->id.data[4] != 0x00 &&
4262 chip->id.data[5] == 0x00 && chip->id.data[6] == 0x00 &&
4263 chip->id.data[7] == 0x00 && mtd->writesize == 512) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004264 mtd->erasesize = 128 * 1024;
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004265 mtd->erasesize <<= ((chip->id.data[3] & 0x03) << 1);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004266 }
4267}
4268
Heiko Schocherf5895d12014-06-24 10:10:04 +02004269/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004270 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4271 * heuristic patterns using various detected parameters (e.g., manufacturer,
4272 * page size, cell-type information).
4273 */
4274static void nand_decode_bbm_options(struct mtd_info *mtd,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004275 struct nand_chip *chip)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004276{
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004277 int maf_id = chip->id.data[0];
Sergey Lapin3a38a552013-01-14 03:46:50 +00004278
4279 /* Set the bad block position */
4280 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4281 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4282 else
4283 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4284
4285 /*
4286 * Bad block marker is stored in the last page of each block on Samsung
4287 * and Hynix MLC devices; stored in first two pages of each block on
4288 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4289 * AMD/Spansion, and Macronix. All others scan only the first page.
4290 */
Michael Trimarchi3dc90602022-07-20 18:22:10 +02004291 if ((nand_is_slc(chip) &&
4292 (maf_id == NAND_MFR_TOSHIBA ||
4293 maf_id == NAND_MFR_AMD ||
4294 maf_id == NAND_MFR_MACRONIX)) ||
4295 (mtd->writesize == 2048 && maf_id == NAND_MFR_MICRON))
Sergey Lapin3a38a552013-01-14 03:46:50 +00004296 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4297}
4298
Heiko Schocherf5895d12014-06-24 10:10:04 +02004299static inline bool is_full_id_nand(struct nand_flash_dev *type)
4300{
4301 return type->id_len;
4302}
4303
4304static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004305 struct nand_flash_dev *type)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004306{
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004307 if (!strncmp((char *)type->id, (char *)chip->id.data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004308 mtd->writesize = type->pagesize;
4309 mtd->erasesize = type->erasesize;
4310 mtd->oobsize = type->oobsize;
4311
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004312 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004313 chip->chipsize = (uint64_t)type->chipsize << 20;
4314 chip->options |= type->options;
4315 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4316 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004317 chip->onfi_timing_mode_default =
4318 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004319
Heiko Schocherf5895d12014-06-24 10:10:04 +02004320 if (!mtd->name)
4321 mtd->name = type->name;
4322
4323 return true;
4324 }
4325 return false;
4326}
4327
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004328/**
4329 * nand_get_manufacturer_desc - Get manufacturer information from the
4330 * manufacturer ID
4331 * @id: manufacturer ID
4332 *
4333 * Returns a nand_manufacturer_desc object if the manufacturer is defined
4334 * in the NAND manufacturers database, NULL otherwise.
4335 */
4336static const struct nand_manufacturers *nand_get_manufacturer_desc(u8 id)
4337{
4338 int i;
4339
4340 for (i = 0; nand_manuf_ids[i].id != 0x0; i++) {
4341 if (nand_manuf_ids[i].id == id)
4342 return &nand_manuf_ids[i];
4343 }
4344
4345 return NULL;
4346}
4347
Sergey Lapin3a38a552013-01-14 03:46:50 +00004348/*
4349 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004350 */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004351struct nand_flash_dev *nand_get_flash_type(struct nand_chip *chip, int *maf_id,
4352 int *dev_id,
4353 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004354{
Michael Trimarchi270c1532022-07-20 18:22:07 +02004355 struct mtd_info *mtd = &chip->mtd;
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004356 const struct nand_manufacturers *manufacturer_desc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004357 int busw, ret;
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004358 u8 *id_data = chip->id.data;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004359
Karl Beldanb6322fc2008-09-15 16:08:03 +02004360 /*
4361 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004362 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004363 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004364 ret = nand_reset(chip, 0);
4365 if (ret)
4366 return ERR_PTR(ret);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004367
4368 /* Select the device */
4369 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004370
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004371 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004372 ret = nand_readid_op(chip, 0, id_data, 2);
4373 if (ret)
4374 return ERR_PTR(ret);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004375
4376 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004377 *maf_id = id_data[0];
4378 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004379
Sergey Lapin3a38a552013-01-14 03:46:50 +00004380 /*
4381 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004382 * interface concerns can cause random data which looks like a
4383 * possibly credible NAND flash to appear. If the two results do
4384 * not match, ignore the device completely.
4385 */
4386
Sergey Lapin3a38a552013-01-14 03:46:50 +00004387 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004388 ret = nand_readid_op(chip, 0, id_data, 8);
4389 if (ret)
4390 return ERR_PTR(ret);
Scott Wood3628f002008-10-24 16:20:43 -05004391
Christian Hitzb8a6b372011-10-12 09:32:02 +02004392 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004393 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004394 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05004395 return ERR_PTR(-ENODEV);
4396 }
4397
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004398 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
4399
4400 /* Try to identify manufacturer */
4401 manufacturer_desc = nand_get_manufacturer_desc(*maf_id);
4402 chip->manufacturer.desc = manufacturer_desc;
4403
Lei Wen75bde942011-01-06 09:48:18 +08004404 if (!type)
4405 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004406
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004407 /*
4408 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4409 * override it.
4410 * This is required to make sure initial NAND bus width set by the
4411 * NAND controller driver is coherent with the real NAND bus width
4412 * (extracted by auto-detection code).
4413 */
4414 busw = chip->options & NAND_BUSWIDTH_16;
4415
4416 /*
4417 * The flag is only set (never cleared), reset it to its default value
4418 * before starting auto-detection.
4419 */
4420 chip->options &= ~NAND_BUSWIDTH_16;
4421
Heiko Schocherf5895d12014-06-24 10:10:04 +02004422 for (; type->name != NULL; type++) {
4423 if (is_full_id_nand(type)) {
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004424 if (find_full_id_nand(mtd, chip, type))
Heiko Schocherf5895d12014-06-24 10:10:04 +02004425 goto ident_done;
4426 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004427 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004428 }
4429 }
Lei Wen75bde942011-01-06 09:48:18 +08004430
Christian Hitzb8a6b372011-10-12 09:32:02 +02004431 chip->onfi_version = 0;
4432 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004433 /* Check if the chip is ONFI compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004434 if (nand_flash_detect_onfi(mtd, chip))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004435 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004436
4437 /* Check if the chip is JEDEC compliant */
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004438 if (nand_flash_detect_jedec(mtd, chip))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004439 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004440 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004441
Christian Hitzb8a6b372011-10-12 09:32:02 +02004442 if (!type->name)
4443 return ERR_PTR(-ENODEV);
4444
William Juul52c07962007-10-31 13:53:06 +01004445 if (!mtd->name)
4446 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004447
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004448 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004449
Scott Wood52ab7ce2016-05-30 13:57:58 -05004450 if (!type->pagesize) {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004451 nand_manufacturer_detect(chip);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004452 } else {
Michael Trimarchi270c1532022-07-20 18:22:07 +02004453 nand_decode_id(chip, type);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004454 }
Michael Trimarchidc7fdd62022-07-20 18:22:04 +02004455
Heiko Schocherf5895d12014-06-24 10:10:04 +02004456 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004457 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004458
Christian Hitzb8a6b372011-10-12 09:32:02 +02004459ident_done:
4460
Heiko Schocherf5895d12014-06-24 10:10:04 +02004461 if (chip->options & NAND_BUSWIDTH_AUTO) {
4462 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4463 chip->options |= busw;
4464 nand_set_defaults(chip, busw);
4465 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4466 /*
4467 * Check, if buswidth is correct. Hardware drivers should set
4468 * chip correct!
4469 */
4470 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4471 *maf_id, *dev_id);
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004472 pr_info("%s %s\n", manufacturer_desc->name, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004473 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004474 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4475 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01004476 return ERR_PTR(-EINVAL);
4477 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004478
Michael Trimarchicd4d9042022-07-20 18:22:05 +02004479 nand_decode_bbm_options(mtd, chip);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004480
William Juul52c07962007-10-31 13:53:06 +01004481 /* Calculate the address shift from the page size */
4482 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004483 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004484 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004485
William Juul52c07962007-10-31 13:53:06 +01004486 chip->bbt_erase_shift = chip->phys_erase_shift =
4487 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004488 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004489 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004490 else {
4491 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4492 chip->chip_shift += 32 - 1;
4493 }
4494
Masahiro Yamada984926b2017-11-22 02:38:31 +09004495 if (chip->chip_shift - chip->page_shift > 16)
4496 chip->options |= NAND_ROW_ADDR_3;
4497
Christian Hitzb8a6b372011-10-12 09:32:02 +02004498 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004499 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004500
Sergey Lapin3a38a552013-01-14 03:46:50 +00004501 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004502 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4503 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004504
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004505 ret = nand_manufacturer_init(chip);
4506 if (ret)
4507 return ERR_PTR(ret);
4508
Heiko Schocherf5895d12014-06-24 10:10:04 +02004509 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4510 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004511
Christian Hitzb8a6b372011-10-12 09:32:02 +02004512#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004513 if (chip->onfi_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004514 pr_info("%s %s\n", manufacturer_desc->name,
4515 chip->onfi_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004516 else if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004517 pr_info("%s %s\n", manufacturer_desc->name,
4518 chip->jedec_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004519 else
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004520 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004521#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004522 if (chip->jedec_version)
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004523 pr_info("%s %s\n", manufacturer_desc->name,
4524 chip->jedec_params.model);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004525 else
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004526 pr_info("%s %s\n", manufacturer_desc->name, type->name);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004527
Michael Trimarchi4a26e1d2022-07-20 18:22:06 +02004528 pr_info("%s %s\n", manufacturer_desc->name,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004529 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004530#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004531
Scott Wood3ea94ed2015-06-26 19:03:26 -05004532 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004533 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004534 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01004535 return type;
4536}
Jörg Krause929fb442018-01-14 19:26:37 +01004537EXPORT_SYMBOL(nand_get_flash_type);
William Juul52c07962007-10-31 13:53:06 +01004538
Brian Norrisba6463d2016-06-15 21:09:22 +02004539#if CONFIG_IS_ENABLED(OF_CONTROL)
Brian Norrisba6463d2016-06-15 21:09:22 +02004540
Patrice Chotardbc77af52021-09-13 16:25:53 +02004541static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004542{
4543 int ret, ecc_mode = -1, ecc_strength, ecc_step;
Brian Norrisba6463d2016-06-15 21:09:22 +02004544 const char *str;
4545
Patrice Chotardbc77af52021-09-13 16:25:53 +02004546 ret = ofnode_read_s32_default(node, "nand-bus-width", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004547 if (ret == 16)
4548 chip->options |= NAND_BUSWIDTH_16;
4549
Patrice Chotardbc77af52021-09-13 16:25:53 +02004550 if (ofnode_read_bool(node, "nand-on-flash-bbt"))
Brian Norrisba6463d2016-06-15 21:09:22 +02004551 chip->bbt_options |= NAND_BBT_USE_FLASH;
4552
Patrice Chotardbc77af52021-09-13 16:25:53 +02004553 str = ofnode_read_string(node, "nand-ecc-mode");
Brian Norrisba6463d2016-06-15 21:09:22 +02004554 if (str) {
4555 if (!strcmp(str, "none"))
4556 ecc_mode = NAND_ECC_NONE;
4557 else if (!strcmp(str, "soft"))
4558 ecc_mode = NAND_ECC_SOFT;
4559 else if (!strcmp(str, "hw"))
4560 ecc_mode = NAND_ECC_HW;
4561 else if (!strcmp(str, "hw_syndrome"))
4562 ecc_mode = NAND_ECC_HW_SYNDROME;
4563 else if (!strcmp(str, "hw_oob_first"))
4564 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4565 else if (!strcmp(str, "soft_bch"))
4566 ecc_mode = NAND_ECC_SOFT_BCH;
4567 }
4568
Pali Rohár44acf8a2022-04-04 18:17:21 +02004569 if (ecc_mode == NAND_ECC_SOFT) {
4570 str = ofnode_read_string(node, "nand-ecc-algo");
4571 if (str && !strcmp(str, "bch"))
4572 ecc_mode = NAND_ECC_SOFT_BCH;
4573 }
4574
Patrice Chotardbc77af52021-09-13 16:25:53 +02004575 ecc_strength = ofnode_read_s32_default(node,
4576 "nand-ecc-strength", -1);
4577 ecc_step = ofnode_read_s32_default(node,
4578 "nand-ecc-step-size", -1);
Brian Norrisba6463d2016-06-15 21:09:22 +02004579
4580 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4581 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4582 pr_err("must set both strength and step size in DT\n");
4583 return -EINVAL;
4584 }
4585
4586 if (ecc_mode >= 0)
4587 chip->ecc.mode = ecc_mode;
4588
4589 if (ecc_strength >= 0)
4590 chip->ecc.strength = ecc_strength;
4591
4592 if (ecc_step > 0)
4593 chip->ecc.size = ecc_step;
4594
Patrice Chotardbc77af52021-09-13 16:25:53 +02004595 if (ofnode_read_bool(node, "nand-ecc-maximize"))
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004596 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4597
Brian Norrisba6463d2016-06-15 21:09:22 +02004598 return 0;
4599}
4600#else
Patrice Chotardbc77af52021-09-13 16:25:53 +02004601static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, ofnode node)
Brian Norrisba6463d2016-06-15 21:09:22 +02004602{
4603 return 0;
4604}
4605#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4606
William Juul52c07962007-10-31 13:53:06 +01004607/**
4608 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004609 * @mtd: MTD device structure
4610 * @maxchips: number of chips to scan for
4611 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004612 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004613 * This is the first phase of the normal nand_scan() function. It reads the
4614 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004615 *
William Juul52c07962007-10-31 13:53:06 +01004616 */
Lei Wen75bde942011-01-06 09:48:18 +08004617int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004618 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004619{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004620 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004621 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004622 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02004623 int ret;
4624
Patrice Chotardbc77af52021-09-13 16:25:53 +02004625 if (ofnode_valid(chip->flash_node)) {
Brian Norrisba6463d2016-06-15 21:09:22 +02004626 ret = nand_dt_init(mtd, chip, chip->flash_node);
4627 if (ret)
4628 return ret;
4629 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004630
William Juul52c07962007-10-31 13:53:06 +01004631 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004632 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004633
4634 /* Read the flash type */
Michael Trimarchi270c1532022-07-20 18:22:07 +02004635 type = nand_get_flash_type(chip, &nand_maf_id,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004636 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004637
4638 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004639 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4640 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004641 chip->select_chip(mtd, -1);
4642 return PTR_ERR(type);
4643 }
4644
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004645 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004646 ret = nand_init_data_interface(chip);
4647 if (ret)
4648 return ret;
4649
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004650 /*
4651 * Setup the data interface correctly on the chip and controller side.
4652 * This explicit call to nand_setup_data_interface() is only required
4653 * for the first die, because nand_reset() has been called before
4654 * ->data_interface and ->default_onfi_timing_mode were set.
4655 * For the other dies, nand_reset() will automatically switch to the
4656 * best mode for us.
4657 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004658 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004659 if (ret)
4660 return ret;
4661
Heiko Schocherf5895d12014-06-24 10:10:04 +02004662 chip->select_chip(mtd, -1);
4663
William Juul52c07962007-10-31 13:53:06 +01004664 /* Check for a chip array */
4665 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004666 u8 id[2];
4667
Karl Beldanb6322fc2008-09-15 16:08:03 +02004668 /* See comment in nand_get_flash_type for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004669 nand_reset(chip, i);
4670
4671 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004672 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004673 nand_readid_op(chip, 0, id, sizeof(id));
4674
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004675 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004676 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004677 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004678 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004679 }
4680 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004681 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004682
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004683#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004684 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004685 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004686#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004687
William Juul52c07962007-10-31 13:53:06 +01004688 /* Store the number of chips and calc total size for mtd */
4689 chip->numchips = i;
4690 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004691
William Juul52c07962007-10-31 13:53:06 +01004692 return 0;
4693}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004694EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004695
Masahiro Yamada820eb482017-11-22 02:38:29 +09004696/**
4697 * nand_check_ecc_caps - check the sanity of preset ECC settings
4698 * @chip: nand chip info structure
4699 * @caps: ECC caps info structure
4700 * @oobavail: OOB size that the ECC engine can use
4701 *
4702 * When ECC step size and strength are already set, check if they are supported
4703 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4704 * On success, the calculated ECC bytes is set.
4705 */
4706int nand_check_ecc_caps(struct nand_chip *chip,
4707 const struct nand_ecc_caps *caps, int oobavail)
4708{
4709 struct mtd_info *mtd = nand_to_mtd(chip);
4710 const struct nand_ecc_step_info *stepinfo;
4711 int preset_step = chip->ecc.size;
4712 int preset_strength = chip->ecc.strength;
4713 int nsteps, ecc_bytes;
4714 int i, j;
4715
4716 if (WARN_ON(oobavail < 0))
4717 return -EINVAL;
4718
4719 if (!preset_step || !preset_strength)
4720 return -ENODATA;
4721
4722 nsteps = mtd->writesize / preset_step;
4723
4724 for (i = 0; i < caps->nstepinfos; i++) {
4725 stepinfo = &caps->stepinfos[i];
4726
4727 if (stepinfo->stepsize != preset_step)
4728 continue;
4729
4730 for (j = 0; j < stepinfo->nstrengths; j++) {
4731 if (stepinfo->strengths[j] != preset_strength)
4732 continue;
4733
4734 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4735 preset_strength);
4736 if (WARN_ON_ONCE(ecc_bytes < 0))
4737 return ecc_bytes;
4738
4739 if (ecc_bytes * nsteps > oobavail) {
4740 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4741 preset_step, preset_strength);
4742 return -ENOSPC;
4743 }
4744
4745 chip->ecc.bytes = ecc_bytes;
4746
4747 return 0;
4748 }
4749 }
4750
4751 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4752 preset_step, preset_strength);
4753
4754 return -ENOTSUPP;
4755}
4756EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4757
4758/**
4759 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4760 * @chip: nand chip info structure
4761 * @caps: ECC engine caps info structure
4762 * @oobavail: OOB size that the ECC engine can use
4763 *
4764 * If a chip's ECC requirement is provided, try to meet it with the least
4765 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4766 * On success, the chosen ECC settings are set.
4767 */
4768int nand_match_ecc_req(struct nand_chip *chip,
4769 const struct nand_ecc_caps *caps, int oobavail)
4770{
4771 struct mtd_info *mtd = nand_to_mtd(chip);
4772 const struct nand_ecc_step_info *stepinfo;
4773 int req_step = chip->ecc_step_ds;
4774 int req_strength = chip->ecc_strength_ds;
4775 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4776 int best_step, best_strength, best_ecc_bytes;
4777 int best_ecc_bytes_total = INT_MAX;
4778 int i, j;
4779
4780 if (WARN_ON(oobavail < 0))
4781 return -EINVAL;
4782
4783 /* No information provided by the NAND chip */
4784 if (!req_step || !req_strength)
4785 return -ENOTSUPP;
4786
4787 /* number of correctable bits the chip requires in a page */
4788 req_corr = mtd->writesize / req_step * req_strength;
4789
4790 for (i = 0; i < caps->nstepinfos; i++) {
4791 stepinfo = &caps->stepinfos[i];
4792 step_size = stepinfo->stepsize;
4793
4794 for (j = 0; j < stepinfo->nstrengths; j++) {
4795 strength = stepinfo->strengths[j];
4796
4797 /*
4798 * If both step size and strength are smaller than the
4799 * chip's requirement, it is not easy to compare the
4800 * resulted reliability.
4801 */
4802 if (step_size < req_step && strength < req_strength)
4803 continue;
4804
4805 if (mtd->writesize % step_size)
4806 continue;
4807
4808 nsteps = mtd->writesize / step_size;
4809
4810 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4811 if (WARN_ON_ONCE(ecc_bytes < 0))
4812 continue;
4813 ecc_bytes_total = ecc_bytes * nsteps;
4814
4815 if (ecc_bytes_total > oobavail ||
4816 strength * nsteps < req_corr)
4817 continue;
4818
4819 /*
4820 * We assume the best is to meet the chip's requrement
4821 * with the least number of ECC bytes.
4822 */
4823 if (ecc_bytes_total < best_ecc_bytes_total) {
4824 best_ecc_bytes_total = ecc_bytes_total;
4825 best_step = step_size;
4826 best_strength = strength;
4827 best_ecc_bytes = ecc_bytes;
4828 }
4829 }
4830 }
4831
4832 if (best_ecc_bytes_total == INT_MAX)
4833 return -ENOTSUPP;
4834
4835 chip->ecc.size = best_step;
4836 chip->ecc.strength = best_strength;
4837 chip->ecc.bytes = best_ecc_bytes;
4838
4839 return 0;
4840}
4841EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4842
4843/**
4844 * nand_maximize_ecc - choose the max ECC strength available
4845 * @chip: nand chip info structure
4846 * @caps: ECC engine caps info structure
4847 * @oobavail: OOB size that the ECC engine can use
4848 *
4849 * Choose the max ECC strength that is supported on the controller, and can fit
4850 * within the chip's OOB. On success, the chosen ECC settings are set.
4851 */
4852int nand_maximize_ecc(struct nand_chip *chip,
4853 const struct nand_ecc_caps *caps, int oobavail)
4854{
4855 struct mtd_info *mtd = nand_to_mtd(chip);
4856 const struct nand_ecc_step_info *stepinfo;
4857 int step_size, strength, nsteps, ecc_bytes, corr;
4858 int best_corr = 0;
4859 int best_step = 0;
4860 int best_strength, best_ecc_bytes;
4861 int i, j;
4862
4863 if (WARN_ON(oobavail < 0))
4864 return -EINVAL;
4865
4866 for (i = 0; i < caps->nstepinfos; i++) {
4867 stepinfo = &caps->stepinfos[i];
4868 step_size = stepinfo->stepsize;
4869
4870 /* If chip->ecc.size is already set, respect it */
4871 if (chip->ecc.size && step_size != chip->ecc.size)
4872 continue;
4873
4874 for (j = 0; j < stepinfo->nstrengths; j++) {
4875 strength = stepinfo->strengths[j];
4876
4877 if (mtd->writesize % step_size)
4878 continue;
4879
4880 nsteps = mtd->writesize / step_size;
4881
4882 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4883 if (WARN_ON_ONCE(ecc_bytes < 0))
4884 continue;
4885
4886 if (ecc_bytes * nsteps > oobavail)
4887 continue;
4888
4889 corr = strength * nsteps;
4890
4891 /*
4892 * If the number of correctable bits is the same,
4893 * bigger step_size has more reliability.
4894 */
4895 if (corr > best_corr ||
4896 (corr == best_corr && step_size > best_step)) {
4897 best_corr = corr;
4898 best_step = step_size;
4899 best_strength = strength;
4900 best_ecc_bytes = ecc_bytes;
4901 }
4902 }
4903 }
4904
4905 if (!best_corr)
4906 return -ENOTSUPP;
4907
4908 chip->ecc.size = best_step;
4909 chip->ecc.strength = best_strength;
4910 chip->ecc.bytes = best_ecc_bytes;
4911
4912 return 0;
4913}
4914EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4915
Scott Wood3ea94ed2015-06-26 19:03:26 -05004916/*
4917 * Check if the chip configuration meet the datasheet requirements.
4918
4919 * If our configuration corrects A bits per B bytes and the minimum
4920 * required correction level is X bits per Y bytes, then we must ensure
4921 * both of the following are true:
4922 *
4923 * (1) A / B >= X / Y
4924 * (2) A >= X
4925 *
4926 * Requirement (1) ensures we can correct for the required bitflip density.
4927 * Requirement (2) ensures we can correct even when all bitflips are clumped
4928 * in the same sector.
4929 */
4930static bool nand_ecc_strength_good(struct mtd_info *mtd)
4931{
Scott Wood17fed142016-05-30 13:57:56 -05004932 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004933 struct nand_ecc_ctrl *ecc = &chip->ecc;
4934 int corr, ds_corr;
4935
4936 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4937 /* Not enough information */
4938 return true;
4939
4940 /*
4941 * We get the number of corrected bits per page to compare
4942 * the correction density.
4943 */
4944 corr = (mtd->writesize * ecc->strength) / ecc->size;
4945 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4946
4947 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4948}
William Juul52c07962007-10-31 13:53:06 +01004949
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004950static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4951{
4952 struct nand_ecc_ctrl *ecc = &chip->ecc;
4953
4954 if (nand_standard_page_accessors(ecc))
4955 return false;
4956
4957 /*
4958 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4959 * controller driver implements all the page accessors because
4960 * default helpers are not suitable when the core does not
4961 * send the READ0/PAGEPROG commands.
4962 */
4963 return (!ecc->read_page || !ecc->write_page ||
4964 !ecc->read_page_raw || !ecc->write_page_raw ||
4965 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4966 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4967 ecc->hwctl && ecc->calculate));
4968}
4969
William Juul52c07962007-10-31 13:53:06 +01004970/**
4971 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004972 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004973 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004974 * This is the second phase of the normal nand_scan() function. It fills out
4975 * all the uninitialized function pointers with the defaults and scans for a
4976 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004977 */
4978int nand_scan_tail(struct mtd_info *mtd)
4979{
4980 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004981 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004982 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004983 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004984
Sergey Lapin3a38a552013-01-14 03:46:50 +00004985 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4986 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4987 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4988
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004989 if (invalid_ecc_page_accessors(chip)) {
4990 pr_err("Invalid ECC page accessors setup\n");
4991 return -EINVAL;
4992 }
4993
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004994 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004995 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004996 chip->buffers = nbuf;
4997 } else {
4998 if (!chip->buffers)
4999 return -ENOMEM;
5000 }
William Juul52c07962007-10-31 13:53:06 +01005001
5002 /* Set the internal oob buffer location, just after the page data */
5003 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
5004
5005 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005006 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01005007 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005008 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005009 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02005010#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005011 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005012 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005013 break;
5014 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005015 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005016 break;
5017 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005018 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005019 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005020 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005021 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005022 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01005023#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005024 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005025 pr_warn("No oob scheme defined for oobsize %d\n",
5026 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005027 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005028 }
5029 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005030
William Juul52c07962007-10-31 13:53:06 +01005031 if (!chip->write_page)
5032 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005033
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005034 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005035 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01005036 * selected and we have 256 byte pagesize fallback to software ECC
5037 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005038
Heiko Schocherf5895d12014-06-24 10:10:04 +02005039 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005040 case NAND_ECC_HW_OOB_FIRST:
5041 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005042 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005043 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005044 BUG();
5045 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005046 if (!ecc->read_page)
5047 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005048
William Juul52c07962007-10-31 13:53:06 +01005049 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005050 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005051 if (!ecc->read_page)
5052 ecc->read_page = nand_read_page_hwecc;
5053 if (!ecc->write_page)
5054 ecc->write_page = nand_write_page_hwecc;
5055 if (!ecc->read_page_raw)
5056 ecc->read_page_raw = nand_read_page_raw;
5057 if (!ecc->write_page_raw)
5058 ecc->write_page_raw = nand_write_page_raw;
5059 if (!ecc->read_oob)
5060 ecc->read_oob = nand_read_oob_std;
5061 if (!ecc->write_oob)
5062 ecc->write_oob = nand_write_oob_std;
5063 if (!ecc->read_subpage)
5064 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05005065 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02005066 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005067
William Juul52c07962007-10-31 13:53:06 +01005068 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005069 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5070 (!ecc->read_page ||
5071 ecc->read_page == nand_read_page_hwecc ||
5072 !ecc->write_page ||
5073 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005074 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005075 BUG();
5076 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005077 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005078 if (!ecc->read_page)
5079 ecc->read_page = nand_read_page_syndrome;
5080 if (!ecc->write_page)
5081 ecc->write_page = nand_write_page_syndrome;
5082 if (!ecc->read_page_raw)
5083 ecc->read_page_raw = nand_read_page_raw_syndrome;
5084 if (!ecc->write_page_raw)
5085 ecc->write_page_raw = nand_write_page_raw_syndrome;
5086 if (!ecc->read_oob)
5087 ecc->read_oob = nand_read_oob_syndrome;
5088 if (!ecc->write_oob)
5089 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005090
Heiko Schocherf5895d12014-06-24 10:10:04 +02005091 if (mtd->writesize >= ecc->size) {
5092 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005093 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5094 BUG();
5095 }
William Juul52c07962007-10-31 13:53:06 +01005096 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005097 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005098 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5099 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005100 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005101
William Juul52c07962007-10-31 13:53:06 +01005102 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005103 ecc->calculate = nand_calculate_ecc;
5104 ecc->correct = nand_correct_data;
5105 ecc->read_page = nand_read_page_swecc;
5106 ecc->read_subpage = nand_read_subpage;
5107 ecc->write_page = nand_write_page_swecc;
5108 ecc->read_page_raw = nand_read_page_raw;
5109 ecc->write_page_raw = nand_write_page_raw;
5110 ecc->read_oob = nand_read_oob_std;
5111 ecc->write_oob = nand_write_oob_std;
5112 if (!ecc->size)
5113 ecc->size = 256;
5114 ecc->bytes = 3;
5115 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005116 break;
5117
Christian Hitz55f7bca2011-10-12 09:31:59 +02005118 case NAND_ECC_SOFT_BCH:
5119 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005120 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005121 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005122 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005123 ecc->calculate = nand_bch_calculate_ecc;
5124 ecc->correct = nand_bch_correct_data;
5125 ecc->read_page = nand_read_page_swecc;
5126 ecc->read_subpage = nand_read_subpage;
5127 ecc->write_page = nand_write_page_swecc;
5128 ecc->read_page_raw = nand_read_page_raw;
5129 ecc->write_page_raw = nand_write_page_raw;
5130 ecc->read_oob = nand_read_oob_std;
5131 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005132 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005133 * Board driver should supply ecc.size and ecc.strength values
5134 * to select how many bits are correctable. Otherwise, default
5135 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005136 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005137 if (!ecc->size && (mtd->oobsize >= 64)) {
5138 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005139 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005140 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005141
5142 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005143 ecc->bytes = 0;
5144 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005145 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005146 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005147 BUG();
5148 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005149 break;
5150
William Juul52c07962007-10-31 13:53:06 +01005151 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005152 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005153 ecc->read_page = nand_read_page_raw;
5154 ecc->write_page = nand_write_page_raw;
5155 ecc->read_oob = nand_read_oob_std;
5156 ecc->read_page_raw = nand_read_page_raw;
5157 ecc->write_page_raw = nand_write_page_raw;
5158 ecc->write_oob = nand_write_oob_std;
5159 ecc->size = mtd->writesize;
5160 ecc->bytes = 0;
5161 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005162 break;
5163
5164 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005165 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005166 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005167 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005168
Sergey Lapin3a38a552013-01-14 03:46:50 +00005169 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005170 if (!ecc->read_oob_raw)
5171 ecc->read_oob_raw = ecc->read_oob;
5172 if (!ecc->write_oob_raw)
5173 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005174
William Juul52c07962007-10-31 13:53:06 +01005175 /*
5176 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005177 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005178 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005179 mtd->oobavail = 0;
5180 if (ecc->layout) {
5181 for (i = 0; ecc->layout->oobfree[i].length; i++)
5182 mtd->oobavail += ecc->layout->oobfree[i].length;
5183 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005184
Scott Wood3ea94ed2015-06-26 19:03:26 -05005185 /* ECC sanity check: warn if it's too weak */
5186 if (!nand_ecc_strength_good(mtd))
5187 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5188 mtd->name);
5189
William Juul52c07962007-10-31 13:53:06 +01005190 /*
5191 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005192 * mode.
William Juul52c07962007-10-31 13:53:06 +01005193 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005194 ecc->steps = mtd->writesize / ecc->size;
5195 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005196 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005197 BUG();
5198 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005199 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005200
Sergey Lapin3a38a552013-01-14 03:46:50 +00005201 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005202 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5203 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005204 case 2:
5205 mtd->subpage_sft = 1;
5206 break;
5207 case 4:
5208 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005209 case 16:
William Juul52c07962007-10-31 13:53:06 +01005210 mtd->subpage_sft = 2;
5211 break;
5212 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005213 }
William Juul52c07962007-10-31 13:53:06 +01005214 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005215
William Juul52c07962007-10-31 13:53:06 +01005216 /* Initialize state */
5217 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005218
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005219 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005220 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005221
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005222 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005223 switch (ecc->mode) {
5224 case NAND_ECC_SOFT:
5225 case NAND_ECC_SOFT_BCH:
5226 if (chip->page_shift > 9)
5227 chip->options |= NAND_SUBPAGE_READ;
5228 break;
5229
5230 default:
5231 break;
5232 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005233
Patrice Chotardbee32872022-03-21 09:13:36 +01005234 mtd->flash_node = chip->flash_node;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005235 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005236 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005237 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5238 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005239 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005240 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005241 mtd->_read_oob = nand_read_oob;
5242 mtd->_write_oob = nand_write_oob;
5243 mtd->_sync = nand_sync;
5244 mtd->_lock = NULL;
5245 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005246 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005247 mtd->_block_isbad = nand_block_isbad;
5248 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005249 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005250
Sergey Lapin3a38a552013-01-14 03:46:50 +00005251 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005252 mtd->ecclayout = ecc->layout;
5253 mtd->ecc_strength = ecc->strength;
5254 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005255 /*
5256 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5257 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5258 * properly set.
5259 */
5260 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005261 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005262
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005263 return 0;
William Juul52c07962007-10-31 13:53:06 +01005264}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005265EXPORT_SYMBOL(nand_scan_tail);
5266
William Juul52c07962007-10-31 13:53:06 +01005267/**
5268 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005269 * @mtd: MTD device structure
5270 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005271 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005272 * This fills out all the uninitialized function pointers with the defaults.
5273 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005274 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005275 */
5276int nand_scan(struct mtd_info *mtd, int maxchips)
5277{
5278 int ret;
5279
Lei Wen75bde942011-01-06 09:48:18 +08005280 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005281 if (!ret)
5282 ret = nand_scan_tail(mtd);
5283 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005284}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005285EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005286
Heiko Schocherf5895d12014-06-24 10:10:04 +02005287MODULE_LICENSE("GPL");
5288MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5289MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5290MODULE_DESCRIPTION("Generic NAND flash driver code");