blob: 49d5e261b561caca371c6194966bdbb291647dc3 [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>
Brian Norrisba6463d2016-06-15 21:09:22 +020032#if CONFIG_IS_ENABLED(OF_CONTROL)
33#include <fdtdec.h>
34#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020035#include <malloc.h>
36#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070037#include <dm/devres.h>
William Juul52c07962007-10-31 13:53:06 +010038#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000039#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020040#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090041#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020042#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020043#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020044#ifdef CONFIG_MTD_PARTITIONS
45#include <linux/mtd/partitions.h>
46#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020047#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090048#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020049
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020050/* Define default oob placement schemes for large and small page devices */
Gregory CLEMENTe5b96312019-04-17 11:22:05 +020051#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
William Juul52c07962007-10-31 13:53:06 +010052static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020053 .eccbytes = 3,
54 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010055 .oobfree = {
56 {.offset = 3,
57 .length = 2},
58 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020059 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020060};
61
William Juul52c07962007-10-31 13:53:06 +010062static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020063 .eccbytes = 6,
64 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010065 .oobfree = {
66 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020067 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020068};
69
William Juul52c07962007-10-31 13:53:06 +010070static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020071 .eccbytes = 24,
72 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010073 40, 41, 42, 43, 44, 45, 46, 47,
74 48, 49, 50, 51, 52, 53, 54, 55,
75 56, 57, 58, 59, 60, 61, 62, 63},
76 .oobfree = {
77 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020078 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020079};
80
William Juul52c07962007-10-31 13:53:06 +010081static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020082 .eccbytes = 48,
83 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020084 80, 81, 82, 83, 84, 85, 86, 87,
85 88, 89, 90, 91, 92, 93, 94, 95,
86 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010087 104, 105, 106, 107, 108, 109, 110, 111,
88 112, 113, 114, 115, 116, 117, 118, 119,
89 120, 121, 122, 123, 124, 125, 126, 127},
90 .oobfree = {
91 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020092 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020093};
Stefan Agnerbd186142018-12-06 14:57:09 +010094#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020095
Heiko Schocherf5895d12014-06-24 10:10:04 +020096static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010097
98static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
99 struct mtd_oob_ops *ops);
100
Heiko Schocherf5895d12014-06-24 10:10:04 +0200101/*
102 * For devices which display every fart in the system on a separate LED. Is
103 * compiled away when LED support is disabled.
104 */
105DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200106
Christian Hitzb8a6b372011-10-12 09:32:02 +0200107static int check_offs_len(struct mtd_info *mtd,
108 loff_t ofs, uint64_t len)
109{
Scott Wood17fed142016-05-30 13:57:56 -0500110 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200111 int ret = 0;
112
113 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200114 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
115 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200116 ret = -EINVAL;
117 }
118
119 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200120 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
121 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200122 ret = -EINVAL;
123 }
124
Christian Hitzb8a6b372011-10-12 09:32:02 +0200125 return ret;
126}
127
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200128/**
129 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000130 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200131 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200132 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200133 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200134static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100135{
Scott Wood17fed142016-05-30 13:57:56 -0500136 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200137
138 /* De-select the NAND device */
139 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100140}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200141
142/**
143 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000144 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200145 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200146 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200147 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000148uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149{
Scott Wood17fed142016-05-30 13:57:56 -0500150 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100151 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200152}
153
154/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200155 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000156 * @mtd: MTD device structure
157 *
158 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200159 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160 */
William Juul52c07962007-10-31 13:53:06 +0100161static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200162{
Scott Wood17fed142016-05-30 13:57:56 -0500163 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100164 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200165}
166
167/**
168 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000169 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200170 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000171 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200172 */
173static u16 nand_read_word(struct mtd_info *mtd)
174{
Scott Wood17fed142016-05-30 13:57:56 -0500175 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100176 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200177}
178
179/**
180 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000181 * @mtd: MTD device structure
182 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200183 *
184 * Default select function for 1 chip devices.
185 */
William Juul52c07962007-10-31 13:53:06 +0100186static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200187{
Scott Wood17fed142016-05-30 13:57:56 -0500188 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100189
190 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200191 case -1:
William Juul52c07962007-10-31 13:53:06 +0100192 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200193 break;
194 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200195 break;
196
197 default:
198 BUG();
199 }
200}
201
202/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200203 * nand_write_byte - [DEFAULT] write single byte to chip
204 * @mtd: MTD device structure
205 * @byte: value to write
206 *
207 * Default function to write a byte to I/O[7:0]
208 */
209static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
210{
Scott Wood17fed142016-05-30 13:57:56 -0500211 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200212
213 chip->write_buf(mtd, &byte, 1);
214}
215
216/**
217 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
218 * @mtd: MTD device structure
219 * @byte: value to write
220 *
221 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
222 */
223static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
224{
Scott Wood17fed142016-05-30 13:57:56 -0500225 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200226 uint16_t word = byte;
227
228 /*
229 * It's not entirely clear what should happen to I/O[15:8] when writing
230 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
231 *
232 * When the host supports a 16-bit bus width, only data is
233 * transferred at the 16-bit width. All address and command line
234 * transfers shall use only the lower 8-bits of the data bus. During
235 * command transfers, the host may place any value on the upper
236 * 8-bits of the data bus. During address transfers, the host shall
237 * set the upper 8-bits of the data bus to 00h.
238 *
239 * One user of the write_byte callback is nand_onfi_set_features. The
240 * four parameters are specified to be written to I/O[7:0], but this is
241 * neither an address nor a command transfer. Let's assume a 0 on the
242 * upper I/O lines is OK.
243 */
244 chip->write_buf(mtd, (uint8_t *)&word, 2);
245}
246
Heiko Schocherf5895d12014-06-24 10:10:04 +0200247static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
248{
249 int i;
250
251 for (i = 0; i < len; i++)
252 writeb(buf[i], addr);
253}
254static void ioread8_rep(void *addr, uint8_t *buf, int len)
255{
256 int i;
257
258 for (i = 0; i < len; i++)
259 buf[i] = readb(addr);
260}
261
262static void ioread16_rep(void *addr, void *buf, int len)
263{
264 int i;
265 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200266
Heiko Schocherf5895d12014-06-24 10:10:04 +0200267 for (i = 0; i < len; i++)
268 p[i] = readw(addr);
269}
270
271static void iowrite16_rep(void *addr, void *buf, int len)
272{
273 int i;
274 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200275
276 for (i = 0; i < len; i++)
277 writew(p[i], addr);
278}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200279
280/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200281 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000282 * @mtd: MTD device structure
283 * @buf: data buffer
284 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200285 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000286 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200287 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000288void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200289{
Scott Wood17fed142016-05-30 13:57:56 -0500290 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200291
Heiko Schocherf5895d12014-06-24 10:10:04 +0200292 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200293}
294
295/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200296 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000297 * @mtd: MTD device structure
298 * @buf: buffer to store date
299 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200300 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000301 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200302 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400303void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200304{
Scott Wood17fed142016-05-30 13:57:56 -0500305 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200306
Heiko Schocherf5895d12014-06-24 10:10:04 +0200307 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200308}
309
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200310/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200311 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000312 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200313 * @buf: data buffer
314 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200315 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200316 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200317 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200318void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200319{
Scott Wood17fed142016-05-30 13:57:56 -0500320 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200321 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200322
Heiko Schocherf5895d12014-06-24 10:10:04 +0200323 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200324}
325
326/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200327 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000328 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200329 * @buf: buffer to store date
330 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200331 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200332 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200333 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200334void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200335{
Scott Wood17fed142016-05-30 13:57:56 -0500336 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200337 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200338
Heiko Schocherf5895d12014-06-24 10:10:04 +0200339 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200340}
341
342/**
343 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000344 * @mtd: MTD device structure
345 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200346 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200347 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200348 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500349static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200350{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500351 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500352 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200353 u16 bad;
354
Sergey Lapin3a38a552013-01-14 03:46:50 +0000355 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200356 ofs += mtd->erasesize - mtd->writesize;
357
William Juul52c07962007-10-31 13:53:06 +0100358 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200359
Sergey Lapin3a38a552013-01-14 03:46:50 +0000360 do {
361 if (chip->options & NAND_BUSWIDTH_16) {
362 chip->cmdfunc(mtd, NAND_CMD_READOOB,
363 chip->badblockpos & 0xFE, page);
364 bad = cpu_to_le16(chip->read_word(mtd));
365 if (chip->badblockpos & 0x1)
366 bad >>= 8;
367 else
368 bad &= 0xFF;
369 } else {
370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
371 page);
372 bad = chip->read_byte(mtd);
373 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200374
Sergey Lapin3a38a552013-01-14 03:46:50 +0000375 if (likely(chip->badblockbits == 8))
376 res = bad != 0xFF;
377 else
378 res = hweight8(bad) < chip->badblockbits;
379 ofs += mtd->writesize;
380 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
381 i++;
382 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200383
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200384 return res;
385}
386
387/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200388 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000389 * @mtd: MTD device structure
390 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200391 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000392 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200393 * specific driver. It provides the details for writing a bad block marker to a
394 * block.
395 */
396static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
397{
Scott Wood17fed142016-05-30 13:57:56 -0500398 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200399 struct mtd_oob_ops ops;
400 uint8_t buf[2] = { 0, 0 };
401 int ret = 0, res, i = 0;
402
Scott Wood3ea94ed2015-06-26 19:03:26 -0500403 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200404 ops.oobbuf = buf;
405 ops.ooboffs = chip->badblockpos;
406 if (chip->options & NAND_BUSWIDTH_16) {
407 ops.ooboffs &= ~0x01;
408 ops.len = ops.ooblen = 2;
409 } else {
410 ops.len = ops.ooblen = 1;
411 }
412 ops.mode = MTD_OPS_PLACE_OOB;
413
414 /* Write to first/last page(s) if necessary */
415 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
416 ofs += mtd->erasesize - mtd->writesize;
417 do {
418 res = nand_do_write_oob(mtd, ofs, &ops);
419 if (!ret)
420 ret = res;
421
422 i++;
423 ofs += mtd->writesize;
424 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
425
426 return ret;
427}
428
429/**
430 * nand_block_markbad_lowlevel - mark a block bad
431 * @mtd: MTD device structure
432 * @ofs: offset from device start
433 *
434 * This function performs the generic NAND bad block marking steps (i.e., bad
435 * block table(s) and/or marker(s)). We only allow the hardware driver to
436 * specify how to write bad block markers to OOB (chip->block_markbad).
437 *
438 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000439 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200440 * (2) write bad block marker to OOB area of affected block (unless flag
441 * NAND_BBT_NO_OOB_BBM is present)
442 * (3) update the BBT
443 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000444 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200445*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200446static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200447{
Scott Wood17fed142016-05-30 13:57:56 -0500448 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200449 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200450
Heiko Schocherf5895d12014-06-24 10:10:04 +0200451 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000452 struct erase_info einfo;
453
454 /* Attempt erase before marking OOB */
455 memset(&einfo, 0, sizeof(einfo));
456 einfo.mtd = mtd;
457 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200458 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000459 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200460
Heiko Schocherf5895d12014-06-24 10:10:04 +0200461 /* Write bad block marker to OOB */
462 nand_get_device(mtd, FL_WRITING);
463 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500464 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100465 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000466
Heiko Schocherf5895d12014-06-24 10:10:04 +0200467 /* Mark block bad in BBT */
468 if (chip->bbt) {
469 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000470 if (!ret)
471 ret = res;
472 }
473
William Juul52c07962007-10-31 13:53:06 +0100474 if (!ret)
475 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500476
William Juul52c07962007-10-31 13:53:06 +0100477 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200478}
479
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200480/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200481 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000482 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200483 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000484 * Check, if the device is write protected. The function expects, that the
485 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200486 */
William Juul52c07962007-10-31 13:53:06 +0100487static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200488{
Scott Wood17fed142016-05-30 13:57:56 -0500489 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100490 u8 status;
491 int ret;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200492
Sergey Lapin3a38a552013-01-14 03:46:50 +0000493 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200494 if (chip->options & NAND_BROKEN_XD)
495 return 0;
496
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200497 /* Check the WP bit */
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100498 ret = nand_status_op(chip, &status);
499 if (ret)
500 return ret;
501
502 return status & NAND_STATUS_WP ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200503}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100504
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200505/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500506 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000507 * @mtd: MTD device structure
508 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300509 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500510 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300511 */
512static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
513{
Scott Wood17fed142016-05-30 13:57:56 -0500514 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300515
516 if (!chip->bbt)
517 return 0;
518 /* Return info from the table */
519 return nand_isreserved_bbt(mtd, ofs);
520}
521
522/**
523 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
524 * @mtd: MTD device structure
525 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000526 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200527 *
528 * Check, if the block is bad. Either by reading the bad block table or
529 * calling of the scan function.
530 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500531static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200532{
Scott Wood17fed142016-05-30 13:57:56 -0500533 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200534
Masahiro Yamada8d100542014-12-26 22:20:58 +0900535 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
536 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200537 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900538 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200539 }
540
William Juul52c07962007-10-31 13:53:06 +0100541 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500542 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200543
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200544 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100545 return nand_isbad_bbt(mtd, ofs, allowbbt);
546}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200547
Scott Wood52ab7ce2016-05-30 13:57:58 -0500548/**
549 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
550 * @mtd: MTD device structure
551 *
552 * Wait for the ready pin after a command, and warn if a timeout occurs.
553 */
William Juul52c07962007-10-31 13:53:06 +0100554void nand_wait_ready(struct mtd_info *mtd)
555{
Scott Wood17fed142016-05-30 13:57:56 -0500556 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500557 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000558 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100559
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000560 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000561 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000562 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100563 if (chip->dev_ready)
564 if (chip->dev_ready(mtd))
565 break;
566 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500567
568 if (!chip->dev_ready(mtd))
569 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200570}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200571EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200572
573/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500574 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
575 * @mtd: MTD device structure
576 * @timeo: Timeout in ms
577 *
578 * Wait for status ready (i.e. command done) or timeout.
579 */
580static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
581{
Scott Wood17fed142016-05-30 13:57:56 -0500582 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500583 u32 time_start;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100584 int ret;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500585
586 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
587 time_start = get_timer(0);
588 while (get_timer(time_start) < timeo) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100589 u8 status;
590
591 ret = nand_read_data_op(chip, &status, sizeof(status), true);
592 if (ret)
593 return;
594
595 if (status & NAND_STATUS_READY)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500596 break;
597 WATCHDOG_RESET();
598 }
599};
600
601/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200602 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000603 * @mtd: MTD device structure
604 * @command: the command to be sent
605 * @column: the column address for this command, -1 if none
606 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200607 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000608 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200609 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200610 */
William Juul52c07962007-10-31 13:53:06 +0100611static void nand_command(struct mtd_info *mtd, unsigned int command,
612 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200613{
Scott Wood17fed142016-05-30 13:57:56 -0500614 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100615 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200616
Sergey Lapin3a38a552013-01-14 03:46:50 +0000617 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200618 if (command == NAND_CMD_SEQIN) {
619 int readcmd;
620
William Juul52c07962007-10-31 13:53:06 +0100621 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200622 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100623 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200624 readcmd = NAND_CMD_READOOB;
625 } else if (column < 256) {
626 /* First 256 bytes --> READ0 */
627 readcmd = NAND_CMD_READ0;
628 } else {
629 column -= 256;
630 readcmd = NAND_CMD_READ1;
631 }
William Juul52c07962007-10-31 13:53:06 +0100632 chip->cmd_ctrl(mtd, readcmd, ctrl);
633 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200634 }
William Juul52c07962007-10-31 13:53:06 +0100635 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200636
Sergey Lapin3a38a552013-01-14 03:46:50 +0000637 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100638 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
639 /* Serially input address */
640 if (column != -1) {
641 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200642 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530643 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100644 column >>= 1;
645 chip->cmd_ctrl(mtd, column, ctrl);
646 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200647 }
William Juul52c07962007-10-31 13:53:06 +0100648 if (page_addr != -1) {
649 chip->cmd_ctrl(mtd, page_addr, ctrl);
650 ctrl &= ~NAND_CTRL_CHANGE;
651 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900652 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100653 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
654 }
655 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200656
657 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000658 * Program and erase have their own busy handlers status and sequential
659 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100660 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200661 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200662
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200663 case NAND_CMD_PAGEPROG:
664 case NAND_CMD_ERASE1:
665 case NAND_CMD_ERASE2:
666 case NAND_CMD_SEQIN:
667 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900668 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900669 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200670 return;
671
672 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100673 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200674 break;
William Juul52c07962007-10-31 13:53:06 +0100675 udelay(chip->chip_delay);
676 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
677 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
678 chip->cmd_ctrl(mtd,
679 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500680 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
681 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200682 return;
683
William Juul52c07962007-10-31 13:53:06 +0100684 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200685 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200686 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200687 * If we don't have access to the busy pin, we apply the given
688 * command delay
William Juul52c07962007-10-31 13:53:06 +0100689 */
690 if (!chip->dev_ready) {
691 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200692 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200693 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200694 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000695 /*
696 * Apply this short delay always to ensure that we do wait tWB in
697 * any case on any machine.
698 */
William Juul52c07962007-10-31 13:53:06 +0100699 ndelay(100);
700
701 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200702}
703
704/**
705 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000706 * @mtd: MTD device structure
707 * @command: the command to be sent
708 * @column: the column address for this command, -1 if none
709 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200710 *
William Juul52c07962007-10-31 13:53:06 +0100711 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000712 * devices. We don't have the separate regions as we have in the small page
713 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200714 */
William Juul52c07962007-10-31 13:53:06 +0100715static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
716 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200717{
Scott Wood17fed142016-05-30 13:57:56 -0500718 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200719
720 /* Emulate NAND_CMD_READOOB */
721 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100722 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200723 command = NAND_CMD_READ0;
724 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200725
William Juul52c07962007-10-31 13:53:06 +0100726 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200727 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200728
729 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100730 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200731
732 /* Serially input address */
733 if (column != -1) {
734 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200735 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530736 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200737 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100738 chip->cmd_ctrl(mtd, column, ctrl);
739 ctrl &= ~NAND_CTRL_CHANGE;
740 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200741 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200742 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100743 chip->cmd_ctrl(mtd, page_addr, ctrl);
744 chip->cmd_ctrl(mtd, page_addr >> 8,
745 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900746 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100747 chip->cmd_ctrl(mtd, page_addr >> 16,
748 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200749 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 }
William Juul52c07962007-10-31 13:53:06 +0100751 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200752
753 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000754 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500755 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100756 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200757 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200758
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200759 case NAND_CMD_CACHEDPROG:
760 case NAND_CMD_PAGEPROG:
761 case NAND_CMD_ERASE1:
762 case NAND_CMD_ERASE2:
763 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100764 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200765 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900766 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900767 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200768 return;
769
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200770 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100771 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200772 break;
William Juul52c07962007-10-31 13:53:06 +0100773 udelay(chip->chip_delay);
774 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
775 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
776 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
777 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500778 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
779 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100780 return;
781
782 case NAND_CMD_RNDOUT:
783 /* No ready / busy check necessary */
784 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
785 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
786 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
787 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200788 return;
789
790 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100791 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
792 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
793 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
794 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200795
William Juul52c07962007-10-31 13:53:06 +0100796 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200797 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200798 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200799 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000800 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100801 */
802 if (!chip->dev_ready) {
803 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200804 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200805 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200806 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200807
Sergey Lapin3a38a552013-01-14 03:46:50 +0000808 /*
809 * Apply this short delay always to ensure that we do wait tWB in
810 * any case on any machine.
811 */
William Juul52c07962007-10-31 13:53:06 +0100812 ndelay(100);
813
814 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200815}
816
817/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200818 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000819 * @chip: the nand chip descriptor
820 * @mtd: MTD device structure
821 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200822 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200823 * Used when in panic, no locks are taken.
824 */
825static void panic_nand_get_device(struct nand_chip *chip,
826 struct mtd_info *mtd, int new_state)
827{
828 /* Hardware controller shared among independent devices */
829 chip->controller->active = chip;
830 chip->state = new_state;
831}
832
833/**
834 * nand_get_device - [GENERIC] Get chip for selected access
835 * @mtd: MTD device structure
836 * @new_state: the state which is requested
837 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200838 * Get the device and lock it for exclusive access
839 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200840static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200841nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100842{
Scott Wood17fed142016-05-30 13:57:56 -0500843 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200844 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100845 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200846}
847
848/**
849 * panic_nand_wait - [GENERIC] wait until the command is done
850 * @mtd: MTD device structure
851 * @chip: NAND chip structure
852 * @timeo: timeout
853 *
854 * Wait for command done. This is a helper function for nand_wait used when
855 * we are in interrupt context. May happen when in panic and trying to write
856 * an oops through mtdoops.
857 */
858static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
859 unsigned long timeo)
860{
861 int i;
862 for (i = 0; i < timeo; i++) {
863 if (chip->dev_ready) {
864 if (chip->dev_ready(mtd))
865 break;
866 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100867 int ret;
868 u8 status;
869
870 ret = nand_read_data_op(chip, &status, sizeof(status),
871 true);
872 if (ret)
873 return;
874
875 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200876 break;
877 }
878 mdelay(1);
879 }
William Juul52c07962007-10-31 13:53:06 +0100880}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200881
882/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000883 * nand_wait - [DEFAULT] wait until the command is done
884 * @mtd: MTD device structure
885 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200886 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500887 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100888 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200889static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200890{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500891 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100892 u8 status;
893 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100894
Heiko Schocherf5895d12014-06-24 10:10:04 +0200895 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100896
Heiko Schocherf5895d12014-06-24 10:10:04 +0200897 /*
898 * Apply this short delay always to ensure that we do wait tWB in any
899 * case on any machine.
900 */
901 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100902
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100903 ret = nand_status_op(chip, NULL);
904 if (ret)
905 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100906
Heiko Schocherf5895d12014-06-24 10:10:04 +0200907 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
908 u32 time_start;
909
910 time_start = get_timer(0);
911 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200912 if (chip->dev_ready) {
913 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100914 break;
915 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100916 ret = nand_read_data_op(chip, &status,
917 sizeof(status), true);
918 if (ret)
919 return ret;
920
921 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100922 break;
923 }
924 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200925 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100926
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100927 ret = nand_read_data_op(chip, &status, sizeof(status), true);
928 if (ret)
929 return ret;
930
Heiko Schocherf5895d12014-06-24 10:10:04 +0200931 /* This can happen if in case of timeout or buggy dev_ready */
932 WARN_ON(!(status & NAND_STATUS_READY));
933 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200934}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500935
Scott Wood52ab7ce2016-05-30 13:57:58 -0500936/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900937 * nand_reset_data_interface - Reset data interface and timings
938 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900939 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900940 *
941 * Reset the Data interface and timings to ONFI mode 0.
942 *
943 * Returns 0 for success or negative error code otherwise.
944 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900945static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900946{
947 struct mtd_info *mtd = nand_to_mtd(chip);
948 const struct nand_data_interface *conf;
949 int ret;
950
951 if (!chip->setup_data_interface)
952 return 0;
953
954 /*
955 * The ONFI specification says:
956 * "
957 * To transition from NV-DDR or NV-DDR2 to the SDR data
958 * interface, the host shall use the Reset (FFh) command
959 * using SDR timing mode 0. A device in any timing mode is
960 * required to recognize Reset (FFh) command issued in SDR
961 * timing mode 0.
962 * "
963 *
964 * Configure the data interface in SDR mode and set the
965 * timings to timing mode 0.
966 */
967
968 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900969 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900970 if (ret)
971 pr_err("Failed to configure data interface to SDR timing mode 0\n");
972
973 return ret;
974}
975
976/**
977 * nand_setup_data_interface - Setup the best data interface and timings
978 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900979 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900980 *
981 * Find and configure the best data interface and NAND timings supported by
982 * the chip and the driver.
983 * First tries to retrieve supported timing modes from ONFI information,
984 * and if the NAND chip does not support ONFI, relies on the
985 * ->onfi_timing_mode_default specified in the nand_ids table.
986 *
987 * Returns 0 for success or negative error code otherwise.
988 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900989static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900990{
991 struct mtd_info *mtd = nand_to_mtd(chip);
992 int ret;
993
994 if (!chip->setup_data_interface || !chip->data_interface)
995 return 0;
996
997 /*
998 * Ensure the timing mode has been changed on the chip side
999 * before changing timings on the controller side.
1000 */
1001 if (chip->onfi_version) {
1002 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1003 chip->onfi_timing_mode_default,
1004 };
1005
1006 ret = chip->onfi_set_features(mtd, chip,
1007 ONFI_FEATURE_ADDR_TIMING_MODE,
1008 tmode_param);
1009 if (ret)
1010 goto err;
1011 }
1012
Boris Brezillon32935f42017-11-22 02:38:28 +09001013 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001014err:
1015 return ret;
1016}
1017
1018/**
1019 * nand_init_data_interface - find the best data interface and timings
1020 * @chip: The NAND chip
1021 *
1022 * Find the best data interface and NAND timings supported by the chip
1023 * and the driver.
1024 * First tries to retrieve supported timing modes from ONFI information,
1025 * and if the NAND chip does not support ONFI, relies on the
1026 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1027 * function nand_chip->data_interface is initialized with the best timing mode
1028 * available.
1029 *
1030 * Returns 0 for success or negative error code otherwise.
1031 */
1032static int nand_init_data_interface(struct nand_chip *chip)
1033{
1034 struct mtd_info *mtd = nand_to_mtd(chip);
1035 int modes, mode, ret;
1036
1037 if (!chip->setup_data_interface)
1038 return 0;
1039
1040 /*
1041 * First try to identify the best timings from ONFI parameters and
1042 * if the NAND does not support ONFI, fallback to the default ONFI
1043 * timing mode.
1044 */
1045 modes = onfi_get_async_timing_mode(chip);
1046 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1047 if (!chip->onfi_timing_mode_default)
1048 return 0;
1049
1050 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1051 }
1052
1053 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1054 GFP_KERNEL);
1055 if (!chip->data_interface)
1056 return -ENOMEM;
1057
1058 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1059 ret = onfi_init_data_interface(chip, chip->data_interface,
1060 NAND_SDR_IFACE, mode);
1061 if (ret)
1062 continue;
1063
Boris Brezillon32935f42017-11-22 02:38:28 +09001064 /* Pass -1 to only */
1065 ret = chip->setup_data_interface(mtd,
1066 NAND_DATA_IFACE_CHECK_ONLY,
1067 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001068 if (!ret) {
1069 chip->onfi_timing_mode_default = mode;
1070 break;
1071 }
1072 }
1073
1074 return 0;
1075}
1076
1077static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1078{
1079 kfree(chip->data_interface);
1080}
1081
1082/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001083 * nand_read_page_op - Do a READ PAGE operation
1084 * @chip: The NAND chip
1085 * @page: page to read
1086 * @offset_in_page: offset within the page
1087 * @buf: buffer used to store the data
1088 * @len: length of the buffer
1089 *
1090 * This function issues a READ PAGE operation.
1091 * This function does not select/unselect the CS line.
1092 *
1093 * Returns 0 on success, a negative error code otherwise.
1094 */
1095int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1096 unsigned int offset_in_page, void *buf, unsigned int len)
1097{
1098 struct mtd_info *mtd = nand_to_mtd(chip);
1099
1100 if (len && !buf)
1101 return -EINVAL;
1102
1103 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1104 return -EINVAL;
1105
1106 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1107 if (len)
1108 chip->read_buf(mtd, buf, len);
1109
1110 return 0;
1111}
1112EXPORT_SYMBOL_GPL(nand_read_page_op);
1113
1114/**
1115 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1116 * @chip: The NAND chip
1117 * @page: parameter page to read
1118 * @buf: buffer used to store the data
1119 * @len: length of the buffer
1120 *
1121 * This function issues a READ PARAMETER PAGE operation.
1122 * This function does not select/unselect the CS line.
1123 *
1124 * Returns 0 on success, a negative error code otherwise.
1125 */
1126static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1127 unsigned int len)
1128{
1129 struct mtd_info *mtd = nand_to_mtd(chip);
1130 unsigned int i;
1131 u8 *p = buf;
1132
1133 if (len && !buf)
1134 return -EINVAL;
1135
1136 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1137 for (i = 0; i < len; i++)
1138 p[i] = chip->read_byte(mtd);
1139
1140 return 0;
1141}
1142
1143/**
1144 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1145 * @chip: The NAND chip
1146 * @offset_in_page: offset within the page
1147 * @buf: buffer used to store the data
1148 * @len: length of the buffer
1149 * @force_8bit: force 8-bit bus access
1150 *
1151 * This function issues a CHANGE READ COLUMN operation.
1152 * This function does not select/unselect the CS line.
1153 *
1154 * Returns 0 on success, a negative error code otherwise.
1155 */
1156int nand_change_read_column_op(struct nand_chip *chip,
1157 unsigned int offset_in_page, void *buf,
1158 unsigned int len, bool force_8bit)
1159{
1160 struct mtd_info *mtd = nand_to_mtd(chip);
1161
1162 if (len && !buf)
1163 return -EINVAL;
1164
1165 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1166 return -EINVAL;
1167
1168 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1169 if (len)
1170 chip->read_buf(mtd, buf, len);
1171
1172 return 0;
1173}
1174EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1175
1176/**
1177 * nand_read_oob_op - Do a READ OOB operation
1178 * @chip: The NAND chip
1179 * @page: page to read
1180 * @offset_in_oob: offset within the OOB area
1181 * @buf: buffer used to store the data
1182 * @len: length of the buffer
1183 *
1184 * This function issues a READ OOB operation.
1185 * This function does not select/unselect the CS line.
1186 *
1187 * Returns 0 on success, a negative error code otherwise.
1188 */
1189int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1190 unsigned int offset_in_oob, void *buf, unsigned int len)
1191{
1192 struct mtd_info *mtd = nand_to_mtd(chip);
1193
1194 if (len && !buf)
1195 return -EINVAL;
1196
1197 if (offset_in_oob + len > mtd->oobsize)
1198 return -EINVAL;
1199
1200 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1201 if (len)
1202 chip->read_buf(mtd, buf, len);
1203
1204 return 0;
1205}
1206EXPORT_SYMBOL_GPL(nand_read_oob_op);
1207
1208/**
1209 * nand_prog_page_begin_op - starts a PROG PAGE operation
1210 * @chip: The NAND chip
1211 * @page: page to write
1212 * @offset_in_page: offset within the page
1213 * @buf: buffer containing the data to write to the page
1214 * @len: length of the buffer
1215 *
1216 * This function issues the first half of a PROG PAGE operation.
1217 * This function does not select/unselect the CS line.
1218 *
1219 * Returns 0 on success, a negative error code otherwise.
1220 */
1221int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1222 unsigned int offset_in_page, const void *buf,
1223 unsigned int len)
1224{
1225 struct mtd_info *mtd = nand_to_mtd(chip);
1226
1227 if (len && !buf)
1228 return -EINVAL;
1229
1230 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1231 return -EINVAL;
1232
1233 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1234
1235 if (buf)
1236 chip->write_buf(mtd, buf, len);
1237
1238 return 0;
1239}
1240EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1241
1242/**
1243 * nand_prog_page_end_op - ends a PROG PAGE operation
1244 * @chip: The NAND chip
1245 *
1246 * This function issues the second half of a PROG PAGE operation.
1247 * This function does not select/unselect the CS line.
1248 *
1249 * Returns 0 on success, a negative error code otherwise.
1250 */
1251int nand_prog_page_end_op(struct nand_chip *chip)
1252{
1253 struct mtd_info *mtd = nand_to_mtd(chip);
1254 int status;
1255
1256 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1257
1258 status = chip->waitfunc(mtd, chip);
1259 if (status & NAND_STATUS_FAIL)
1260 return -EIO;
1261
1262 return 0;
1263}
1264EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1265
1266/**
1267 * nand_prog_page_op - Do a full PROG PAGE operation
1268 * @chip: The NAND chip
1269 * @page: page to write
1270 * @offset_in_page: offset within the page
1271 * @buf: buffer containing the data to write to the page
1272 * @len: length of the buffer
1273 *
1274 * This function issues a full PROG PAGE operation.
1275 * This function does not select/unselect the CS line.
1276 *
1277 * Returns 0 on success, a negative error code otherwise.
1278 */
1279int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1280 unsigned int offset_in_page, const void *buf,
1281 unsigned int len)
1282{
1283 struct mtd_info *mtd = nand_to_mtd(chip);
1284 int status;
1285
1286 if (!len || !buf)
1287 return -EINVAL;
1288
1289 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1290 return -EINVAL;
1291
1292 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1293 chip->write_buf(mtd, buf, len);
1294 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1295
1296 status = chip->waitfunc(mtd, chip);
1297 if (status & NAND_STATUS_FAIL)
1298 return -EIO;
1299
1300 return 0;
1301}
1302EXPORT_SYMBOL_GPL(nand_prog_page_op);
1303
1304/**
1305 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1306 * @chip: The NAND chip
1307 * @offset_in_page: offset within the page
1308 * @buf: buffer containing the data to send to the NAND
1309 * @len: length of the buffer
1310 * @force_8bit: force 8-bit bus access
1311 *
1312 * This function issues a CHANGE WRITE COLUMN operation.
1313 * This function does not select/unselect the CS line.
1314 *
1315 * Returns 0 on success, a negative error code otherwise.
1316 */
1317int nand_change_write_column_op(struct nand_chip *chip,
1318 unsigned int offset_in_page,
1319 const void *buf, unsigned int len,
1320 bool force_8bit)
1321{
1322 struct mtd_info *mtd = nand_to_mtd(chip);
1323
1324 if (len && !buf)
1325 return -EINVAL;
1326
1327 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1328 return -EINVAL;
1329
1330 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1331 if (len)
1332 chip->write_buf(mtd, buf, len);
1333
1334 return 0;
1335}
1336EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1337
1338/**
1339 * nand_readid_op - Do a READID operation
1340 * @chip: The NAND chip
1341 * @addr: address cycle to pass after the READID command
1342 * @buf: buffer used to store the ID
1343 * @len: length of the buffer
1344 *
1345 * This function sends a READID command and reads back the ID returned by the
1346 * NAND.
1347 * This function does not select/unselect the CS line.
1348 *
1349 * Returns 0 on success, a negative error code otherwise.
1350 */
1351int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1352 unsigned int len)
1353{
1354 struct mtd_info *mtd = nand_to_mtd(chip);
1355 unsigned int i;
1356 u8 *id = buf;
1357
1358 if (len && !buf)
1359 return -EINVAL;
1360
1361 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1362
1363 for (i = 0; i < len; i++)
1364 id[i] = chip->read_byte(mtd);
1365
1366 return 0;
1367}
1368EXPORT_SYMBOL_GPL(nand_readid_op);
1369
1370/**
1371 * nand_status_op - Do a STATUS operation
1372 * @chip: The NAND chip
1373 * @status: out variable to store the NAND status
1374 *
1375 * This function sends a STATUS command and reads back the status returned by
1376 * the NAND.
1377 * This function does not select/unselect the CS line.
1378 *
1379 * Returns 0 on success, a negative error code otherwise.
1380 */
1381int nand_status_op(struct nand_chip *chip, u8 *status)
1382{
1383 struct mtd_info *mtd = nand_to_mtd(chip);
1384
1385 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1386 if (status)
1387 *status = chip->read_byte(mtd);
1388
1389 return 0;
1390}
1391EXPORT_SYMBOL_GPL(nand_status_op);
1392
1393/**
1394 * nand_exit_status_op - Exit a STATUS operation
1395 * @chip: The NAND chip
1396 *
1397 * This function sends a READ0 command to cancel the effect of the STATUS
1398 * command to avoid reading only the status until a new read command is sent.
1399 *
1400 * This function does not select/unselect the CS line.
1401 *
1402 * Returns 0 on success, a negative error code otherwise.
1403 */
1404int nand_exit_status_op(struct nand_chip *chip)
1405{
1406 struct mtd_info *mtd = nand_to_mtd(chip);
1407
1408 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1409
1410 return 0;
1411}
1412EXPORT_SYMBOL_GPL(nand_exit_status_op);
1413
1414/**
1415 * nand_erase_op - Do an erase operation
1416 * @chip: The NAND chip
1417 * @eraseblock: block to erase
1418 *
1419 * This function sends an ERASE command and waits for the NAND to be ready
1420 * before returning.
1421 * This function does not select/unselect the CS line.
1422 *
1423 * Returns 0 on success, a negative error code otherwise.
1424 */
1425int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1426{
1427 struct mtd_info *mtd = nand_to_mtd(chip);
1428 unsigned int page = eraseblock <<
1429 (chip->phys_erase_shift - chip->page_shift);
1430 int status;
1431
1432 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1433 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1434
1435 status = chip->waitfunc(mtd, chip);
1436 if (status < 0)
1437 return status;
1438
1439 if (status & NAND_STATUS_FAIL)
1440 return -EIO;
1441
1442 return 0;
1443}
1444EXPORT_SYMBOL_GPL(nand_erase_op);
1445
1446/**
1447 * nand_set_features_op - Do a SET FEATURES operation
1448 * @chip: The NAND chip
1449 * @feature: feature id
1450 * @data: 4 bytes of data
1451 *
1452 * This function sends a SET FEATURES command and waits for the NAND to be
1453 * ready before returning.
1454 * This function does not select/unselect the CS line.
1455 *
1456 * Returns 0 on success, a negative error code otherwise.
1457 */
1458static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1459 const void *data)
1460{
1461 struct mtd_info *mtd = nand_to_mtd(chip);
1462 const u8 *params = data;
1463 int i, status;
1464
1465 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1466 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1467 chip->write_byte(mtd, params[i]);
1468
1469 status = chip->waitfunc(mtd, chip);
1470 if (status & NAND_STATUS_FAIL)
1471 return -EIO;
1472
1473 return 0;
1474}
1475
1476/**
1477 * nand_get_features_op - Do a GET FEATURES operation
1478 * @chip: The NAND chip
1479 * @feature: feature id
1480 * @data: 4 bytes of data
1481 *
1482 * This function sends a GET FEATURES command and waits for the NAND to be
1483 * ready before returning.
1484 * This function does not select/unselect the CS line.
1485 *
1486 * Returns 0 on success, a negative error code otherwise.
1487 */
1488static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1489 void *data)
1490{
1491 struct mtd_info *mtd = nand_to_mtd(chip);
1492 u8 *params = data;
1493 int i;
1494
1495 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1496 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1497 params[i] = chip->read_byte(mtd);
1498
1499 return 0;
1500}
1501
1502/**
1503 * nand_reset_op - Do a reset operation
1504 * @chip: The NAND chip
1505 *
1506 * This function sends a RESET command and waits for the NAND to be ready
1507 * before returning.
1508 * This function does not select/unselect the CS line.
1509 *
1510 * Returns 0 on success, a negative error code otherwise.
1511 */
1512int nand_reset_op(struct nand_chip *chip)
1513{
1514 struct mtd_info *mtd = nand_to_mtd(chip);
1515
1516 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1517
1518 return 0;
1519}
1520EXPORT_SYMBOL_GPL(nand_reset_op);
1521
1522/**
1523 * nand_read_data_op - Read data from the NAND
1524 * @chip: The NAND chip
1525 * @buf: buffer used to store the data
1526 * @len: length of the buffer
1527 * @force_8bit: force 8-bit bus access
1528 *
1529 * This function does a raw data read on the bus. Usually used after launching
1530 * another NAND operation like nand_read_page_op().
1531 * This function does not select/unselect the CS line.
1532 *
1533 * Returns 0 on success, a negative error code otherwise.
1534 */
1535int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1536 bool force_8bit)
1537{
1538 struct mtd_info *mtd = nand_to_mtd(chip);
1539
1540 if (!len || !buf)
1541 return -EINVAL;
1542
1543 if (force_8bit) {
1544 u8 *p = buf;
1545 unsigned int i;
1546
1547 for (i = 0; i < len; i++)
1548 p[i] = chip->read_byte(mtd);
1549 } else {
1550 chip->read_buf(mtd, buf, len);
1551 }
1552
1553 return 0;
1554}
1555EXPORT_SYMBOL_GPL(nand_read_data_op);
1556
1557/**
1558 * nand_write_data_op - Write data from the NAND
1559 * @chip: The NAND chip
1560 * @buf: buffer containing the data to send on the bus
1561 * @len: length of the buffer
1562 * @force_8bit: force 8-bit bus access
1563 *
1564 * This function does a raw data write on the bus. Usually used after launching
1565 * another NAND operation like nand_write_page_begin_op().
1566 * This function does not select/unselect the CS line.
1567 *
1568 * Returns 0 on success, a negative error code otherwise.
1569 */
1570int nand_write_data_op(struct nand_chip *chip, const void *buf,
1571 unsigned int len, bool force_8bit)
1572{
1573 struct mtd_info *mtd = nand_to_mtd(chip);
1574
1575 if (!len || !buf)
1576 return -EINVAL;
1577
1578 if (force_8bit) {
1579 const u8 *p = buf;
1580 unsigned int i;
1581
1582 for (i = 0; i < len; i++)
1583 chip->write_byte(mtd, p[i]);
1584 } else {
1585 chip->write_buf(mtd, buf, len);
1586 }
1587
1588 return 0;
1589}
1590EXPORT_SYMBOL_GPL(nand_write_data_op);
1591
1592/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001593 * nand_reset - Reset and initialize a NAND device
1594 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001595 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001596 *
1597 * Returns 0 for success or negative error code otherwise
1598 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001599int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001600{
1601 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001602 int ret;
1603
Boris Brezillon32935f42017-11-22 02:38:28 +09001604 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001605 if (ret)
1606 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001607
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001608 /*
1609 * The CS line has to be released before we can apply the new NAND
1610 * interface settings, hence this weird ->select_chip() dance.
1611 */
1612 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001613 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001614 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001615 if (ret)
1616 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001617
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001618 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001619 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001620 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001621 if (ret)
1622 return ret;
1623
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001624 return 0;
1625}
1626
1627/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001628 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1629 * @buf: buffer to test
1630 * @len: buffer length
1631 * @bitflips_threshold: maximum number of bitflips
1632 *
1633 * Check if a buffer contains only 0xff, which means the underlying region
1634 * has been erased and is ready to be programmed.
1635 * The bitflips_threshold specify the maximum number of bitflips before
1636 * considering the region is not erased.
1637 * Note: The logic of this function has been extracted from the memweight
1638 * implementation, except that nand_check_erased_buf function exit before
1639 * testing the whole buffer if the number of bitflips exceed the
1640 * bitflips_threshold value.
1641 *
1642 * Returns a positive number of bitflips less than or equal to
1643 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1644 * threshold.
1645 */
1646static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1647{
1648 const unsigned char *bitmap = buf;
1649 int bitflips = 0;
1650 int weight;
1651
1652 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1653 len--, bitmap++) {
1654 weight = hweight8(*bitmap);
1655 bitflips += BITS_PER_BYTE - weight;
1656 if (unlikely(bitflips > bitflips_threshold))
1657 return -EBADMSG;
1658 }
1659
1660 for (; len >= 4; len -= 4, bitmap += 4) {
1661 weight = hweight32(*((u32 *)bitmap));
1662 bitflips += 32 - weight;
1663 if (unlikely(bitflips > bitflips_threshold))
1664 return -EBADMSG;
1665 }
1666
1667 for (; len > 0; len--, bitmap++) {
1668 weight = hweight8(*bitmap);
1669 bitflips += BITS_PER_BYTE - weight;
1670 if (unlikely(bitflips > bitflips_threshold))
1671 return -EBADMSG;
1672 }
1673
1674 return bitflips;
1675}
1676
1677/**
1678 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1679 * 0xff data
1680 * @data: data buffer to test
1681 * @datalen: data length
1682 * @ecc: ECC buffer
1683 * @ecclen: ECC length
1684 * @extraoob: extra OOB buffer
1685 * @extraooblen: extra OOB length
1686 * @bitflips_threshold: maximum number of bitflips
1687 *
1688 * Check if a data buffer and its associated ECC and OOB data contains only
1689 * 0xff pattern, which means the underlying region has been erased and is
1690 * ready to be programmed.
1691 * The bitflips_threshold specify the maximum number of bitflips before
1692 * considering the region as not erased.
1693 *
1694 * Note:
1695 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1696 * different from the NAND page size. When fixing bitflips, ECC engines will
1697 * report the number of errors per chunk, and the NAND core infrastructure
1698 * expect you to return the maximum number of bitflips for the whole page.
1699 * This is why you should always use this function on a single chunk and
1700 * not on the whole page. After checking each chunk you should update your
1701 * max_bitflips value accordingly.
1702 * 2/ When checking for bitflips in erased pages you should not only check
1703 * the payload data but also their associated ECC data, because a user might
1704 * have programmed almost all bits to 1 but a few. In this case, we
1705 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1706 * this case.
1707 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1708 * data are protected by the ECC engine.
1709 * It could also be used if you support subpages and want to attach some
1710 * extra OOB data to an ECC chunk.
1711 *
1712 * Returns a positive number of bitflips less than or equal to
1713 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1714 * threshold. In case of success, the passed buffers are filled with 0xff.
1715 */
1716int nand_check_erased_ecc_chunk(void *data, int datalen,
1717 void *ecc, int ecclen,
1718 void *extraoob, int extraooblen,
1719 int bitflips_threshold)
1720{
1721 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1722
1723 data_bitflips = nand_check_erased_buf(data, datalen,
1724 bitflips_threshold);
1725 if (data_bitflips < 0)
1726 return data_bitflips;
1727
1728 bitflips_threshold -= data_bitflips;
1729
1730 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1731 if (ecc_bitflips < 0)
1732 return ecc_bitflips;
1733
1734 bitflips_threshold -= ecc_bitflips;
1735
1736 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1737 bitflips_threshold);
1738 if (extraoob_bitflips < 0)
1739 return extraoob_bitflips;
1740
1741 if (data_bitflips)
1742 memset(data, 0xff, datalen);
1743
1744 if (ecc_bitflips)
1745 memset(ecc, 0xff, ecclen);
1746
1747 if (extraoob_bitflips)
1748 memset(extraoob, 0xff, extraooblen);
1749
1750 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1751}
1752EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001753
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001754/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001755 * nand_read_page_raw - [INTERN] read raw page data without ecc
1756 * @mtd: mtd info structure
1757 * @chip: nand chip info structure
1758 * @buf: buffer to store read data
1759 * @oob_required: caller requires OOB data read to chip->oob_poi
1760 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001761 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001762 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001763 */
William Juul52c07962007-10-31 13:53:06 +01001764static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001765 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001766{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001767 int ret;
1768
1769 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1770 if (ret)
1771 return ret;
1772
1773 if (oob_required) {
1774 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1775 false);
1776 if (ret)
1777 return ret;
1778 }
1779
William Juul52c07962007-10-31 13:53:06 +01001780 return 0;
1781}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001782
William Juul52c07962007-10-31 13:53:06 +01001783/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001784 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1785 * @mtd: mtd info structure
1786 * @chip: nand chip info structure
1787 * @buf: buffer to store read data
1788 * @oob_required: caller requires OOB data read to chip->oob_poi
1789 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001790 *
1791 * We need a special oob layout and handling even when OOB isn't used.
1792 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001793static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001794 struct nand_chip *chip, uint8_t *buf,
1795 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001796{
1797 int eccsize = chip->ecc.size;
1798 int eccbytes = chip->ecc.bytes;
1799 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001800 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001801
1802 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001803 ret = nand_read_data_op(chip, buf, eccsize, false);
1804 if (ret)
1805 return ret;
1806
David Brownellee86b8d2009-11-07 16:27:01 -05001807 buf += eccsize;
1808
1809 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001810 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1811 false);
1812 if (ret)
1813 return ret;
1814
David Brownellee86b8d2009-11-07 16:27:01 -05001815 oob += chip->ecc.prepad;
1816 }
1817
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001818 ret = nand_read_data_op(chip, oob, eccbytes, false);
1819 if (ret)
1820 return ret;
1821
David Brownellee86b8d2009-11-07 16:27:01 -05001822 oob += eccbytes;
1823
1824 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001825 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1826 false);
1827 if (ret)
1828 return ret;
1829
David Brownellee86b8d2009-11-07 16:27:01 -05001830 oob += chip->ecc.postpad;
1831 }
1832 }
1833
1834 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001835 if (size) {
1836 ret = nand_read_data_op(chip, oob, size, false);
1837 if (ret)
1838 return ret;
1839 }
David Brownellee86b8d2009-11-07 16:27:01 -05001840
1841 return 0;
1842}
1843
1844/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001845 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1846 * @mtd: mtd info structure
1847 * @chip: nand chip info structure
1848 * @buf: buffer to store read data
1849 * @oob_required: caller requires OOB data read to chip->oob_poi
1850 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001851 */
1852static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001853 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001854{
1855 int i, eccsize = chip->ecc.size;
1856 int eccbytes = chip->ecc.bytes;
1857 int eccsteps = chip->ecc.steps;
1858 uint8_t *p = buf;
1859 uint8_t *ecc_calc = chip->buffers->ecccalc;
1860 uint8_t *ecc_code = chip->buffers->ecccode;
1861 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001862 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001863
Sergey Lapin3a38a552013-01-14 03:46:50 +00001864 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001865
William Juul52c07962007-10-31 13:53:06 +01001866 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1867 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001868
William Juul52c07962007-10-31 13:53:06 +01001869 for (i = 0; i < chip->ecc.total; i++)
1870 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001871
William Juul52c07962007-10-31 13:53:06 +01001872 eccsteps = chip->ecc.steps;
1873 p = buf;
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 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001877
William Juul52c07962007-10-31 13:53:06 +01001878 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001879 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001880 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001881 } else {
William Juul52c07962007-10-31 13:53:06 +01001882 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001883 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1884 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001885 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001886 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001887}
1888
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001889/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001890 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001891 * @mtd: mtd info structure
1892 * @chip: nand chip info structure
1893 * @data_offs: offset of requested data within the page
1894 * @readlen: data length
1895 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001896 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001897 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001898static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001899 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1900 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001901{
1902 int start_step, end_step, num_steps;
1903 uint32_t *eccpos = chip->ecc.layout->eccpos;
1904 uint8_t *p;
1905 int data_col_addr, i, gaps = 0;
1906 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1907 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001908 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001909 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001910 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001911
Sergey Lapin3a38a552013-01-14 03:46:50 +00001912 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001913 start_step = data_offs / chip->ecc.size;
1914 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1915 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001916 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001917
Sergey Lapin3a38a552013-01-14 03:46:50 +00001918 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001919 datafrag_len = num_steps * chip->ecc.size;
1920 eccfrag_len = num_steps * chip->ecc.bytes;
1921
1922 data_col_addr = start_step * chip->ecc.size;
1923 /* If we read not a page aligned data */
1924 if (data_col_addr != 0)
1925 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1926
1927 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001928 ret = nand_read_data_op(chip, p, datafrag_len, false);
1929 if (ret)
1930 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001931
Sergey Lapin3a38a552013-01-14 03:46:50 +00001932 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001933 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1934 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1935
Sergey Lapin3a38a552013-01-14 03:46:50 +00001936 /*
1937 * The performance is faster if we position offsets according to
1938 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1939 */
Scott Wood3628f002008-10-24 16:20:43 -05001940 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001941 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001942 gaps = 1;
1943 break;
1944 }
1945 }
1946 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001947 ret = nand_change_read_column_op(chip, mtd->writesize,
1948 chip->oob_poi, mtd->oobsize,
1949 false);
1950 if (ret)
1951 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001952 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001953 /*
1954 * Send the command to read the particular ECC bytes take care
1955 * about buswidth alignment in read_buf.
1956 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001957 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001958 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001959 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001960 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001961 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001962 aligned_len++;
1963
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001964 ret = nand_change_read_column_op(chip,
1965 mtd->writesize + aligned_pos,
1966 &chip->oob_poi[aligned_pos],
1967 aligned_len, false);
1968 if (ret)
1969 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001970 }
1971
1972 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001973 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001974
1975 p = bufpoi + data_col_addr;
1976 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1977 int stat;
1978
Christian Hitzb8a6b372011-10-12 09:32:02 +02001979 stat = chip->ecc.correct(mtd, p,
1980 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001981 if (stat == -EBADMSG &&
1982 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1983 /* check for empty pages with bitflips */
1984 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1985 &chip->buffers->ecccode[i],
1986 chip->ecc.bytes,
1987 NULL, 0,
1988 chip->ecc.strength);
1989 }
1990
Heiko Schocherf5895d12014-06-24 10:10:04 +02001991 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001992 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001993 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001994 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001995 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1996 }
Scott Wood3628f002008-10-24 16:20:43 -05001997 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001998 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05001999}
2000
2001/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002002 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
2003 * @mtd: mtd info structure
2004 * @chip: nand chip info structure
2005 * @buf: buffer to store read data
2006 * @oob_required: caller requires OOB data read to chip->oob_poi
2007 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002008 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002009 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002010 */
William Juul52c07962007-10-31 13:53:06 +01002011static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002012 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002013{
William Juul52c07962007-10-31 13:53:06 +01002014 int i, eccsize = chip->ecc.size;
2015 int eccbytes = chip->ecc.bytes;
2016 int eccsteps = chip->ecc.steps;
2017 uint8_t *p = buf;
2018 uint8_t *ecc_calc = chip->buffers->ecccalc;
2019 uint8_t *ecc_code = chip->buffers->ecccode;
2020 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002021 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002022 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002023
William Juul52c07962007-10-31 13:53:06 +01002024 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2025 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002026
2027 ret = nand_read_data_op(chip, p, eccsize, false);
2028 if (ret)
2029 return ret;
2030
William Juul52c07962007-10-31 13:53:06 +01002031 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2032 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002033
2034 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2035 if (ret)
2036 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002037
William Juul52c07962007-10-31 13:53:06 +01002038 for (i = 0; i < chip->ecc.total; i++)
2039 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002040
William Juul52c07962007-10-31 13:53:06 +01002041 eccsteps = chip->ecc.steps;
2042 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002043
William Juul52c07962007-10-31 13:53:06 +01002044 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2045 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002046
William Juul52c07962007-10-31 13:53:06 +01002047 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002048 if (stat == -EBADMSG &&
2049 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2050 /* check for empty pages with bitflips */
2051 stat = nand_check_erased_ecc_chunk(p, eccsize,
2052 &ecc_code[i], eccbytes,
2053 NULL, 0,
2054 chip->ecc.strength);
2055 }
2056
Heiko Schocherf5895d12014-06-24 10:10:04 +02002057 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002058 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002059 } else {
William Juul52c07962007-10-31 13:53:06 +01002060 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002061 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2062 }
William Juul52c07962007-10-31 13:53:06 +01002063 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002064 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002065}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002066
William Juul52c07962007-10-31 13:53:06 +01002067/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002068 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2069 * @mtd: mtd info structure
2070 * @chip: nand chip info structure
2071 * @buf: buffer to store read data
2072 * @oob_required: caller requires OOB data read to chip->oob_poi
2073 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002074 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002075 * Hardware ECC for large page chips, require OOB to be read first. For this
2076 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2077 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2078 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2079 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002080 */
2081static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002082 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002083{
2084 int i, eccsize = chip->ecc.size;
2085 int eccbytes = chip->ecc.bytes;
2086 int eccsteps = chip->ecc.steps;
2087 uint8_t *p = buf;
2088 uint8_t *ecc_code = chip->buffers->ecccode;
2089 uint32_t *eccpos = chip->ecc.layout->eccpos;
2090 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002091 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002092 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002093
2094 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002095 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2096 if (ret)
2097 return ret;
2098
2099 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2100 if (ret)
2101 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002102
2103 for (i = 0; i < chip->ecc.total; i++)
2104 ecc_code[i] = chip->oob_poi[eccpos[i]];
2105
2106 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2107 int stat;
2108
2109 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002110
2111 ret = nand_read_data_op(chip, p, eccsize, false);
2112 if (ret)
2113 return ret;
2114
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002115 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2116
2117 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002118 if (stat == -EBADMSG &&
2119 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2120 /* check for empty pages with bitflips */
2121 stat = nand_check_erased_ecc_chunk(p, eccsize,
2122 &ecc_code[i], eccbytes,
2123 NULL, 0,
2124 chip->ecc.strength);
2125 }
2126
Heiko Schocherf5895d12014-06-24 10:10:04 +02002127 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002128 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002129 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002130 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002131 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2132 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002133 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002134 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002135}
2136
2137/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002138 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2139 * @mtd: mtd info structure
2140 * @chip: nand chip info structure
2141 * @buf: buffer to store read data
2142 * @oob_required: caller requires OOB data read to chip->oob_poi
2143 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002144 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002145 * The hw generator calculates the error syndrome automatically. Therefore we
2146 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002147 */
2148static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002149 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002150{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002151 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002152 int eccbytes = chip->ecc.bytes;
2153 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002154 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002155 uint8_t *p = buf;
2156 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002157 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002158
2159 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2160 int stat;
2161
2162 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002163
2164 ret = nand_read_data_op(chip, p, eccsize, false);
2165 if (ret)
2166 return ret;
William Juul52c07962007-10-31 13:53:06 +01002167
2168 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002169 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2170 false);
2171 if (ret)
2172 return ret;
2173
William Juul52c07962007-10-31 13:53:06 +01002174 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002175 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002176
William Juul52c07962007-10-31 13:53:06 +01002177 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002178
2179 ret = nand_read_data_op(chip, oob, eccbytes, false);
2180 if (ret)
2181 return ret;
2182
William Juul52c07962007-10-31 13:53:06 +01002183 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002184
William Juul52c07962007-10-31 13:53:06 +01002185 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002186
William Juul52c07962007-10-31 13:53:06 +01002187 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002188 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2189 false);
2190 if (ret)
2191 return ret;
2192
William Juul52c07962007-10-31 13:53:06 +01002193 oob += chip->ecc.postpad;
2194 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002195
2196 if (stat == -EBADMSG &&
2197 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2198 /* check for empty pages with bitflips */
2199 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2200 oob - eccpadbytes,
2201 eccpadbytes,
2202 NULL, 0,
2203 chip->ecc.strength);
2204 }
2205
2206 if (stat < 0) {
2207 mtd->ecc_stats.failed++;
2208 } else {
2209 mtd->ecc_stats.corrected += stat;
2210 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2211 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002212 }
William Juul52c07962007-10-31 13:53:06 +01002213
2214 /* Calculate remaining oob bytes */
2215 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002216 if (i) {
2217 ret = nand_read_data_op(chip, oob, i, false);
2218 if (ret)
2219 return ret;
2220 }
William Juul52c07962007-10-31 13:53:06 +01002221
Heiko Schocherf5895d12014-06-24 10:10:04 +02002222 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002223}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002224
2225/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002226 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2227 * @chip: nand chip structure
2228 * @oob: oob destination address
2229 * @ops: oob ops structure
2230 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002231 */
2232static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2233 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002234{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002235 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002236
Sergey Lapin3a38a552013-01-14 03:46:50 +00002237 case MTD_OPS_PLACE_OOB:
2238 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002239 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2240 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002241
Sergey Lapin3a38a552013-01-14 03:46:50 +00002242 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002243 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2244 uint32_t boffs = 0, roffs = ops->ooboffs;
2245 size_t bytes = 0;
2246
Christian Hitz13fc0e22011-10-12 09:32:01 +02002247 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002248 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002249 if (unlikely(roffs)) {
2250 if (roffs >= free->length) {
2251 roffs -= free->length;
2252 continue;
2253 }
2254 boffs = free->offset + roffs;
2255 bytes = min_t(size_t, len,
2256 (free->length - roffs));
2257 roffs = 0;
2258 } else {
2259 bytes = min_t(size_t, len, free->length);
2260 boffs = free->offset;
2261 }
2262 memcpy(oob, chip->oob_poi + boffs, bytes);
2263 oob += bytes;
2264 }
2265 return oob;
2266 }
2267 default:
2268 BUG();
2269 }
2270 return NULL;
2271}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002272
2273/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002274 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2275 * @mtd: MTD device structure
2276 * @retry_mode: the retry mode to use
2277 *
2278 * Some vendors supply a special command to shift the Vt threshold, to be used
2279 * when there are too many bitflips in a page (i.e., ECC error). After setting
2280 * a new threshold, the host should retry reading the page.
2281 */
2282static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2283{
Scott Wood17fed142016-05-30 13:57:56 -05002284 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002285
2286 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2287
2288 if (retry_mode >= chip->read_retries)
2289 return -EINVAL;
2290
2291 if (!chip->setup_read_retry)
2292 return -EOPNOTSUPP;
2293
2294 return chip->setup_read_retry(mtd, retry_mode);
2295}
2296
2297/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002298 * nand_do_read_ops - [INTERN] Read data with ECC
2299 * @mtd: MTD device structure
2300 * @from: offset to read from
2301 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002302 *
William Juul52c07962007-10-31 13:53:06 +01002303 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002304 */
William Juul52c07962007-10-31 13:53:06 +01002305static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2306 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002307{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002308 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002309 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002310 int ret = 0;
2311 uint32_t readlen = ops->len;
2312 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002313 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002314
William Juul52c07962007-10-31 13:53:06 +01002315 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002316 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002317 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002318 int retry_mode = 0;
2319 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002320
William Juul52c07962007-10-31 13:53:06 +01002321 chipnr = (int)(from >> chip->chip_shift);
2322 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002323
William Juul52c07962007-10-31 13:53:06 +01002324 realpage = (int)(from >> chip->page_shift);
2325 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002326
William Juul52c07962007-10-31 13:53:06 +01002327 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002328
William Juul52c07962007-10-31 13:53:06 +01002329 buf = ops->datbuf;
2330 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002331 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002332
Christian Hitz13fc0e22011-10-12 09:32:01 +02002333 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002334 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002335
Heiko Schocherf5895d12014-06-24 10:10:04 +02002336 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01002337 bytes = min(mtd->writesize - col, readlen);
2338 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002339
Scott Wood3ea94ed2015-06-26 19:03:26 -05002340 if (!aligned)
2341 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002342 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2343 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2344 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002345 else
2346 use_bufpoi = 0;
2347
Sergey Lapin3a38a552013-01-14 03:46:50 +00002348 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002349 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002350 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2351
2352 if (use_bufpoi && aligned)
2353 pr_debug("%s: using read bounce buffer for buf@%p\n",
2354 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002355
Heiko Schocherf5895d12014-06-24 10:10:04 +02002356read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002357 if (nand_standard_page_accessors(&chip->ecc)) {
2358 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2359 if (ret)
2360 break;
2361 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002362
Paul Burton700a76c2013-09-04 15:16:56 +01002363 /*
2364 * Now read the page into the buffer. Absent an error,
2365 * the read methods return max bitflips per ecc step.
2366 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002367 if (unlikely(ops->mode == MTD_OPS_RAW))
2368 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2369 oob_required,
2370 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002371 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002372 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002373 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002374 col, bytes, bufpoi,
2375 page);
William Juul52c07962007-10-31 13:53:06 +01002376 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002377 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002378 oob_required, page);
2379 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002380 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002381 /* Invalidate page cache */
2382 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002383 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002384 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002385
Paul Burton700a76c2013-09-04 15:16:56 +01002386 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2387
William Juul52c07962007-10-31 13:53:06 +01002388 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002389 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002390 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002391 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002392 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002393 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002394 chip->pagebuf_bitflips = ret;
2395 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002396 /* Invalidate page cache */
2397 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002398 }
William Juul52c07962007-10-31 13:53:06 +01002399 memcpy(buf, chip->buffers->databuf + col, bytes);
2400 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002401
William Juul52c07962007-10-31 13:53:06 +01002402 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002403 int toread = min(oobreadlen, max_oobsize);
2404
2405 if (toread) {
2406 oob = nand_transfer_oob(chip,
2407 oob, ops, toread);
2408 oobreadlen -= toread;
2409 }
William Juul52c07962007-10-31 13:53:06 +01002410 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002411
2412 if (chip->options & NAND_NEED_READRDY) {
2413 /* Apply delay or wait for ready/busy pin */
2414 if (!chip->dev_ready)
2415 udelay(chip->chip_delay);
2416 else
2417 nand_wait_ready(mtd);
2418 }
2419
2420 if (mtd->ecc_stats.failed - ecc_failures) {
2421 if (retry_mode + 1 < chip->read_retries) {
2422 retry_mode++;
2423 ret = nand_setup_read_retry(mtd,
2424 retry_mode);
2425 if (ret < 0)
2426 break;
2427
2428 /* Reset failures; retry */
2429 mtd->ecc_stats.failed = ecc_failures;
2430 goto read_retry;
2431 } else {
2432 /* No more retry modes; real failure */
2433 ecc_fail = true;
2434 }
2435 }
2436
2437 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002438 } else {
2439 memcpy(buf, chip->buffers->databuf + col, bytes);
2440 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002441 max_bitflips = max_t(unsigned int, max_bitflips,
2442 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002443 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002444
William Juul52c07962007-10-31 13:53:06 +01002445 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002446
Heiko Schocherf5895d12014-06-24 10:10:04 +02002447 /* Reset to retry mode 0 */
2448 if (retry_mode) {
2449 ret = nand_setup_read_retry(mtd, 0);
2450 if (ret < 0)
2451 break;
2452 retry_mode = 0;
2453 }
2454
William Juul52c07962007-10-31 13:53:06 +01002455 if (!readlen)
2456 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002457
Sergey Lapin3a38a552013-01-14 03:46:50 +00002458 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002459 col = 0;
2460 /* Increment page address */
2461 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002462
William Juul52c07962007-10-31 13:53:06 +01002463 page = realpage & chip->pagemask;
2464 /* Check, if we cross a chip boundary */
2465 if (!page) {
2466 chipnr++;
2467 chip->select_chip(mtd, -1);
2468 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002469 }
William Juul52c07962007-10-31 13:53:06 +01002470 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002471 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002472
William Juul52c07962007-10-31 13:53:06 +01002473 ops->retlen = ops->len - (size_t) readlen;
2474 if (oob)
2475 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002476
Heiko Schocherf5895d12014-06-24 10:10:04 +02002477 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002478 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002479
Heiko Schocherf5895d12014-06-24 10:10:04 +02002480 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002481 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002482
Paul Burton700a76c2013-09-04 15:16:56 +01002483 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002484}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002485
William Juul52c07962007-10-31 13:53:06 +01002486/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002487 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2488 * @mtd: mtd info structure
2489 * @chip: nand chip info structure
2490 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002491 */
2492static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002493 int page)
William Juul52c07962007-10-31 13:53:06 +01002494{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002495 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002496}
2497
2498/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002499 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002500 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002501 * @mtd: mtd info structure
2502 * @chip: nand chip info structure
2503 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002504 */
2505static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002506 int page)
William Juul52c07962007-10-31 13:53:06 +01002507{
William Juul52c07962007-10-31 13:53:06 +01002508 int length = mtd->oobsize;
2509 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2510 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002511 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002512 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002513
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002514 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2515 if (ret)
2516 return ret;
2517
William Juul52c07962007-10-31 13:53:06 +01002518 for (i = 0; i < chip->ecc.steps; i++) {
2519 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002520 int ret;
2521
William Juul52c07962007-10-31 13:53:06 +01002522 pos = eccsize + i * (eccsize + chunk);
2523 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002524 ret = nand_change_read_column_op(chip, pos,
2525 NULL, 0,
2526 false);
William Juul52c07962007-10-31 13:53:06 +01002527 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002528 ret = nand_read_page_op(chip, page, pos, NULL,
2529 0);
2530
2531 if (ret)
2532 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002533 } else
William Juul52c07962007-10-31 13:53:06 +01002534 sndrnd = 1;
2535 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002536
2537 ret = nand_read_data_op(chip, bufpoi, toread, false);
2538 if (ret)
2539 return ret;
2540
William Juul52c07962007-10-31 13:53:06 +01002541 bufpoi += toread;
2542 length -= toread;
2543 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002544 if (length > 0) {
2545 ret = nand_read_data_op(chip, bufpoi, length, false);
2546 if (ret)
2547 return ret;
2548 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002549
Sergey Lapin3a38a552013-01-14 03:46:50 +00002550 return 0;
William Juul52c07962007-10-31 13:53:06 +01002551}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002552
William Juul52c07962007-10-31 13:53:06 +01002553/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002554 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2555 * @mtd: mtd info structure
2556 * @chip: nand chip info structure
2557 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002558 */
2559static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2560 int page)
2561{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002562 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2563 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002564}
2565
2566/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002567 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2568 * with syndrome - only for large page flash
2569 * @mtd: mtd info structure
2570 * @chip: nand chip info structure
2571 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002572 */
2573static int nand_write_oob_syndrome(struct mtd_info *mtd,
2574 struct nand_chip *chip, int page)
2575{
2576 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2577 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002578 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002579 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002580
2581 /*
William Juul52c07962007-10-31 13:53:06 +01002582 * data-ecc-data-ecc ... ecc-oob
2583 * or
2584 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002585 */
William Juul52c07962007-10-31 13:53:06 +01002586 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2587 pos = steps * (eccsize + chunk);
2588 steps = 0;
2589 } else
2590 pos = eccsize;
2591
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002592 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2593 if (ret)
2594 return ret;
2595
William Juul52c07962007-10-31 13:53:06 +01002596 for (i = 0; i < steps; i++) {
2597 if (sndcmd) {
2598 if (mtd->writesize <= 512) {
2599 uint32_t fill = 0xFFFFFFFF;
2600
2601 len = eccsize;
2602 while (len > 0) {
2603 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002604
2605 ret = nand_write_data_op(chip, &fill,
2606 num, false);
2607 if (ret)
2608 return ret;
2609
William Juul52c07962007-10-31 13:53:06 +01002610 len -= num;
2611 }
2612 } else {
2613 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002614 ret = nand_change_write_column_op(chip, pos,
2615 NULL, 0,
2616 false);
2617 if (ret)
2618 return ret;
William Juul52c07962007-10-31 13:53:06 +01002619 }
2620 } else
2621 sndcmd = 1;
2622 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002623
2624 ret = nand_write_data_op(chip, bufpoi, len, false);
2625 if (ret)
2626 return ret;
2627
William Juul52c07962007-10-31 13:53:06 +01002628 bufpoi += len;
2629 length -= len;
2630 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002631 if (length > 0) {
2632 ret = nand_write_data_op(chip, bufpoi, length, false);
2633 if (ret)
2634 return ret;
2635 }
William Juul52c07962007-10-31 13:53:06 +01002636
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002637 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002638}
2639
2640/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002641 * nand_do_read_oob - [INTERN] NAND read out-of-band
2642 * @mtd: MTD device structure
2643 * @from: offset to read from
2644 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002645 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002646 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002647 */
William Juul52c07962007-10-31 13:53:06 +01002648static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2649 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002650{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002651 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002652 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002653 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002654 int readlen = ops->ooblen;
2655 int len;
2656 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002657 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002658
Heiko Schocherf5895d12014-06-24 10:10:04 +02002659 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002660 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002661
Sergey Lapin3a38a552013-01-14 03:46:50 +00002662 stats = mtd->ecc_stats;
2663
Scott Wood52ab7ce2016-05-30 13:57:58 -05002664 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002665
William Juul52c07962007-10-31 13:53:06 +01002666 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002667 pr_debug("%s: attempt to start read outside oob\n",
2668 __func__);
William Juul52c07962007-10-31 13:53:06 +01002669 return -EINVAL;
2670 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002671
2672 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002673 if (unlikely(from >= mtd->size ||
2674 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2675 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002676 pr_debug("%s: attempt to read beyond end of device\n",
2677 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002678 return -EINVAL;
2679 }
2680
William Juul52c07962007-10-31 13:53:06 +01002681 chipnr = (int)(from >> chip->chip_shift);
2682 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002683
William Juul52c07962007-10-31 13:53:06 +01002684 /* Shift to get page */
2685 realpage = (int)(from >> chip->page_shift);
2686 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002687
Christian Hitz13fc0e22011-10-12 09:32:01 +02002688 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002689 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002690
Sergey Lapin3a38a552013-01-14 03:46:50 +00002691 if (ops->mode == MTD_OPS_RAW)
2692 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2693 else
2694 ret = chip->ecc.read_oob(mtd, chip, page);
2695
2696 if (ret < 0)
2697 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002698
William Juul52c07962007-10-31 13:53:06 +01002699 len = min(len, readlen);
2700 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002701
Heiko Schocherf5895d12014-06-24 10:10:04 +02002702 if (chip->options & NAND_NEED_READRDY) {
2703 /* Apply delay or wait for ready/busy pin */
2704 if (!chip->dev_ready)
2705 udelay(chip->chip_delay);
2706 else
2707 nand_wait_ready(mtd);
2708 }
2709
William Juul52c07962007-10-31 13:53:06 +01002710 readlen -= len;
2711 if (!readlen)
2712 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002713
William Juul52c07962007-10-31 13:53:06 +01002714 /* Increment page address */
2715 realpage++;
2716
2717 page = realpage & chip->pagemask;
2718 /* Check, if we cross a chip boundary */
2719 if (!page) {
2720 chipnr++;
2721 chip->select_chip(mtd, -1);
2722 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002723 }
William Juul52c07962007-10-31 13:53:06 +01002724 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002725 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002726
Sergey Lapin3a38a552013-01-14 03:46:50 +00002727 ops->oobretlen = ops->ooblen - readlen;
2728
2729 if (ret < 0)
2730 return ret;
2731
2732 if (mtd->ecc_stats.failed - stats.failed)
2733 return -EBADMSG;
2734
2735 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002736}
2737
2738/**
William Juul52c07962007-10-31 13:53:06 +01002739 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002740 * @mtd: MTD device structure
2741 * @from: offset to read from
2742 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002743 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002744 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002745 */
William Juul52c07962007-10-31 13:53:06 +01002746static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2747 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002748{
William Juul52c07962007-10-31 13:53:06 +01002749 int ret = -ENOTSUPP;
2750
2751 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002752
2753 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002754 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002755 pr_debug("%s: attempt to read beyond end of device\n",
2756 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002757 return -EINVAL;
2758 }
2759
Heiko Schocherf5895d12014-06-24 10:10:04 +02002760 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002761
Christian Hitz13fc0e22011-10-12 09:32:01 +02002762 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002763 case MTD_OPS_PLACE_OOB:
2764 case MTD_OPS_AUTO_OOB:
2765 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002766 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002767
William Juul52c07962007-10-31 13:53:06 +01002768 default:
2769 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002770 }
2771
William Juul52c07962007-10-31 13:53:06 +01002772 if (!ops->datbuf)
2773 ret = nand_do_read_oob(mtd, from, ops);
2774 else
2775 ret = nand_do_read_ops(mtd, from, ops);
2776
Christian Hitz13fc0e22011-10-12 09:32:01 +02002777out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002778 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002779 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002780}
2781
2782
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002783/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002784 * nand_write_page_raw - [INTERN] raw page write function
2785 * @mtd: mtd info structure
2786 * @chip: nand chip info structure
2787 * @buf: data buffer
2788 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002789 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002790 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002791 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002792 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002793static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002794 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002795{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002796 int ret;
2797
2798 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2799 if (ret)
2800 return ret;
2801
2802 if (oob_required) {
2803 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2804 false);
2805 if (ret)
2806 return ret;
2807 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002808
2809 return 0;
William Juul52c07962007-10-31 13:53:06 +01002810}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002811
William Juul52c07962007-10-31 13:53:06 +01002812/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002813 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2814 * @mtd: mtd info structure
2815 * @chip: nand chip info structure
2816 * @buf: data buffer
2817 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002818 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002819 *
2820 * We need a special oob layout and handling even when ECC isn't checked.
2821 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002822static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002823 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002824 const uint8_t *buf, int oob_required,
2825 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002826{
2827 int eccsize = chip->ecc.size;
2828 int eccbytes = chip->ecc.bytes;
2829 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002830 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002831
2832 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002833 ret = nand_write_data_op(chip, buf, eccsize, false);
2834 if (ret)
2835 return ret;
2836
David Brownellee86b8d2009-11-07 16:27:01 -05002837 buf += eccsize;
2838
2839 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002840 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2841 false);
2842 if (ret)
2843 return ret;
2844
David Brownellee86b8d2009-11-07 16:27:01 -05002845 oob += chip->ecc.prepad;
2846 }
2847
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002848 ret = nand_write_data_op(chip, oob, eccbytes, false);
2849 if (ret)
2850 return ret;
2851
David Brownellee86b8d2009-11-07 16:27:01 -05002852 oob += eccbytes;
2853
2854 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002855 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2856 false);
2857 if (ret)
2858 return ret;
2859
David Brownellee86b8d2009-11-07 16:27:01 -05002860 oob += chip->ecc.postpad;
2861 }
2862 }
2863
2864 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002865 if (size) {
2866 ret = nand_write_data_op(chip, oob, size, false);
2867 if (ret)
2868 return ret;
2869 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002870
2871 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002872}
2873/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002874 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2875 * @mtd: mtd info structure
2876 * @chip: nand chip info structure
2877 * @buf: data buffer
2878 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002879 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002880 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002881static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002882 const uint8_t *buf, int oob_required,
2883 int page)
William Juul52c07962007-10-31 13:53:06 +01002884{
2885 int i, eccsize = chip->ecc.size;
2886 int eccbytes = chip->ecc.bytes;
2887 int eccsteps = chip->ecc.steps;
2888 uint8_t *ecc_calc = chip->buffers->ecccalc;
2889 const uint8_t *p = buf;
2890 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002891
Sergey Lapin3a38a552013-01-14 03:46:50 +00002892 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002893 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2894 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002895
William Juul52c07962007-10-31 13:53:06 +01002896 for (i = 0; i < chip->ecc.total; i++)
2897 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002898
Scott Wood46e13102016-05-30 13:57:57 -05002899 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002900}
2901
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002902/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002903 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2904 * @mtd: mtd info structure
2905 * @chip: nand chip info structure
2906 * @buf: data buffer
2907 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002908 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002909 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002910static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002911 const uint8_t *buf, int oob_required,
2912 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002913{
William Juul52c07962007-10-31 13:53:06 +01002914 int i, eccsize = chip->ecc.size;
2915 int eccbytes = chip->ecc.bytes;
2916 int eccsteps = chip->ecc.steps;
2917 uint8_t *ecc_calc = chip->buffers->ecccalc;
2918 const uint8_t *p = buf;
2919 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002920 int ret;
William Juul52c07962007-10-31 13:53:06 +01002921
2922 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2923 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002924
2925 ret = nand_write_data_op(chip, p, eccsize, false);
2926 if (ret)
2927 return ret;
2928
William Juul52c07962007-10-31 13:53:06 +01002929 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2930 }
2931
2932 for (i = 0; i < chip->ecc.total; i++)
2933 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2934
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002935 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2936 if (ret)
2937 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002938
2939 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002940}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002941
Heiko Schocherf5895d12014-06-24 10:10:04 +02002942
2943/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002944 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002945 * @mtd: mtd info structure
2946 * @chip: nand chip info structure
2947 * @offset: column address of subpage within the page
2948 * @data_len: data length
2949 * @buf: data buffer
2950 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002951 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002952 */
2953static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2954 struct nand_chip *chip, uint32_t offset,
2955 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002956 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002957{
2958 uint8_t *oob_buf = chip->oob_poi;
2959 uint8_t *ecc_calc = chip->buffers->ecccalc;
2960 int ecc_size = chip->ecc.size;
2961 int ecc_bytes = chip->ecc.bytes;
2962 int ecc_steps = chip->ecc.steps;
2963 uint32_t *eccpos = chip->ecc.layout->eccpos;
2964 uint32_t start_step = offset / ecc_size;
2965 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2966 int oob_bytes = mtd->oobsize / ecc_steps;
2967 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002968 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002969
2970 for (step = 0; step < ecc_steps; step++) {
2971 /* configure controller for WRITE access */
2972 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2973
2974 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002975 ret = nand_write_data_op(chip, buf, ecc_size, false);
2976 if (ret)
2977 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002978
2979 /* mask ECC of un-touched subpages by padding 0xFF */
2980 if ((step < start_step) || (step > end_step))
2981 memset(ecc_calc, 0xff, ecc_bytes);
2982 else
2983 chip->ecc.calculate(mtd, buf, ecc_calc);
2984
2985 /* mask OOB of un-touched subpages by padding 0xFF */
2986 /* if oob_required, preserve OOB metadata of written subpage */
2987 if (!oob_required || (step < start_step) || (step > end_step))
2988 memset(oob_buf, 0xff, oob_bytes);
2989
2990 buf += ecc_size;
2991 ecc_calc += ecc_bytes;
2992 oob_buf += oob_bytes;
2993 }
2994
2995 /* copy calculated ECC for whole page to chip->buffer->oob */
2996 /* this include masked-value(0xFF) for unwritten subpages */
2997 ecc_calc = chip->buffers->ecccalc;
2998 for (i = 0; i < chip->ecc.total; i++)
2999 chip->oob_poi[eccpos[i]] = ecc_calc[i];
3000
3001 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003002 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3003 if (ret)
3004 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003005
3006 return 0;
3007}
3008
3009
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003010/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003011 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
3012 * @mtd: mtd info structure
3013 * @chip: nand chip info structure
3014 * @buf: data buffer
3015 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003016 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003017 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003018 * The hw generator calculates the error syndrome automatically. Therefore we
3019 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003020 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003021static int nand_write_page_syndrome(struct mtd_info *mtd,
3022 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003023 const uint8_t *buf, int oob_required,
3024 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003025{
William Juul52c07962007-10-31 13:53:06 +01003026 int i, eccsize = chip->ecc.size;
3027 int eccbytes = chip->ecc.bytes;
3028 int eccsteps = chip->ecc.steps;
3029 const uint8_t *p = buf;
3030 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003031 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003032
William Juul52c07962007-10-31 13:53:06 +01003033 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003034 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003035
3036 ret = nand_write_data_op(chip, p, eccsize, false);
3037 if (ret)
3038 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003039
William Juul52c07962007-10-31 13:53:06 +01003040 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003041 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3042 false);
3043 if (ret)
3044 return ret;
3045
William Juul52c07962007-10-31 13:53:06 +01003046 oob += chip->ecc.prepad;
3047 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003048
William Juul52c07962007-10-31 13:53:06 +01003049 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003050
3051 ret = nand_write_data_op(chip, oob, eccbytes, false);
3052 if (ret)
3053 return ret;
3054
William Juul52c07962007-10-31 13:53:06 +01003055 oob += eccbytes;
3056
3057 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003058 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3059 false);
3060 if (ret)
3061 return ret;
3062
William Juul52c07962007-10-31 13:53:06 +01003063 oob += chip->ecc.postpad;
3064 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003065 }
3066
William Juul52c07962007-10-31 13:53:06 +01003067 /* Calculate remaining oob bytes */
3068 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003069 if (i) {
3070 ret = nand_write_data_op(chip, oob, i, false);
3071 if (ret)
3072 return ret;
3073 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003074
3075 return 0;
William Juul52c07962007-10-31 13:53:06 +01003076}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003077
William Juul52c07962007-10-31 13:53:06 +01003078/**
3079 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003080 * @mtd: MTD device structure
3081 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003082 * @offset: address offset within the page
3083 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003084 * @buf: the data to write
3085 * @oob_required: must write chip->oob_poi to OOB
3086 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003087 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003088 */
3089static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003090 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003091 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003092{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003093 int status, subpage;
3094
3095 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3096 chip->ecc.write_subpage)
3097 subpage = offset || (data_len < mtd->writesize);
3098 else
3099 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003100
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003101 if (nand_standard_page_accessors(&chip->ecc)) {
3102 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3103 if (status)
3104 return status;
3105 }
William Juul52c07962007-10-31 13:53:06 +01003106
3107 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003108 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003109 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003110 else if (subpage)
3111 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003112 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003113 else
Scott Wood46e13102016-05-30 13:57:57 -05003114 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3115 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003116
3117 if (status < 0)
3118 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003119
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003120 if (nand_standard_page_accessors(&chip->ecc))
3121 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003122
William Juul52c07962007-10-31 13:53:06 +01003123 return 0;
3124}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003125
William Juul52c07962007-10-31 13:53:06 +01003126/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003127 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3128 * @mtd: MTD device structure
3129 * @oob: oob data buffer
3130 * @len: oob data write length
3131 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003132 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003133static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3134 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003135{
Scott Wood17fed142016-05-30 13:57:56 -05003136 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003137
3138 /*
3139 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3140 * data from a previous OOB read.
3141 */
3142 memset(chip->oob_poi, 0xff, mtd->oobsize);
3143
Christian Hitz13fc0e22011-10-12 09:32:01 +02003144 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003145
Sergey Lapin3a38a552013-01-14 03:46:50 +00003146 case MTD_OPS_PLACE_OOB:
3147 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003148 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3149 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003150
Sergey Lapin3a38a552013-01-14 03:46:50 +00003151 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003152 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3153 uint32_t boffs = 0, woffs = ops->ooboffs;
3154 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003155
Christian Hitz13fc0e22011-10-12 09:32:01 +02003156 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003157 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003158 if (unlikely(woffs)) {
3159 if (woffs >= free->length) {
3160 woffs -= free->length;
3161 continue;
3162 }
3163 boffs = free->offset + woffs;
3164 bytes = min_t(size_t, len,
3165 (free->length - woffs));
3166 woffs = 0;
3167 } else {
3168 bytes = min_t(size_t, len, free->length);
3169 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003170 }
William Juul52c07962007-10-31 13:53:06 +01003171 memcpy(chip->oob_poi + boffs, oob, bytes);
3172 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003173 }
William Juul52c07962007-10-31 13:53:06 +01003174 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003175 }
William Juul52c07962007-10-31 13:53:06 +01003176 default:
3177 BUG();
3178 }
3179 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003180}
3181
Christian Hitzb8a6b372011-10-12 09:32:02 +02003182#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003183
3184/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003185 * nand_do_write_ops - [INTERN] NAND write with ECC
3186 * @mtd: MTD device structure
3187 * @to: offset to write to
3188 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003189 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003190 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003191 */
William Juul52c07962007-10-31 13:53:06 +01003192static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3193 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003194{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003195 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003196 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003197 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003198
3199 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003200 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003201
William Juul52c07962007-10-31 13:53:06 +01003202 uint8_t *oob = ops->oobbuf;
3203 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003204 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003205 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003206
William Juul52c07962007-10-31 13:53:06 +01003207 ops->retlen = 0;
3208 if (!writelen)
3209 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003210
Heiko Schocherf5895d12014-06-24 10:10:04 +02003211 /* Reject writes, which are not page aligned */
3212 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003213 pr_notice("%s: attempt to write non page aligned data\n",
3214 __func__);
William Juul52c07962007-10-31 13:53:06 +01003215 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003216 }
3217
3218 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003219
William Juul52c07962007-10-31 13:53:06 +01003220 chipnr = (int)(to >> chip->chip_shift);
3221 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003222
3223 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003224 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003225 ret = -EIO;
3226 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003227 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003228
William Juul52c07962007-10-31 13:53:06 +01003229 realpage = (int)(to >> chip->page_shift);
3230 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003231
William Juul52c07962007-10-31 13:53:06 +01003232 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003233 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3234 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003235 chip->pagebuf = -1;
3236
Christian Hitzb8a6b372011-10-12 09:32:02 +02003237 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003238 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3239 ret = -EINVAL;
3240 goto err_out;
3241 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003242
Christian Hitz13fc0e22011-10-12 09:32:01 +02003243 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003244 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003245 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003246 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003247 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003248
3249 if (part_pagewr)
3250 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003251 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3252 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3253 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003254 else
3255 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003256
Heiko Schocherf5895d12014-06-24 10:10:04 +02003257 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003258 /* Partial page write?, or need to use bounce buffer */
3259 if (use_bufpoi) {
3260 pr_debug("%s: using write bounce buffer for buf@%p\n",
3261 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003262 if (part_pagewr)
3263 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003264 chip->pagebuf = -1;
3265 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3266 memcpy(&chip->buffers->databuf[column], buf, bytes);
3267 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003268 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003269
Christian Hitzb8a6b372011-10-12 09:32:02 +02003270 if (unlikely(oob)) {
3271 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003272 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003273 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003274 } else {
3275 /* We still need to erase leftover OOB data */
3276 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003277 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003278 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003279 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003280 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003281 if (ret)
3282 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003283
William Juul52c07962007-10-31 13:53:06 +01003284 writelen -= bytes;
3285 if (!writelen)
3286 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003287
Heiko Schocherf5895d12014-06-24 10:10:04 +02003288 column = 0;
3289 buf += bytes;
3290 realpage++;
3291
3292 page = realpage & chip->pagemask;
3293 /* Check, if we cross a chip boundary */
3294 if (!page) {
3295 chipnr++;
3296 chip->select_chip(mtd, -1);
3297 chip->select_chip(mtd, chipnr);
3298 }
3299 }
3300
3301 ops->retlen = ops->len - writelen;
3302 if (unlikely(oob))
3303 ops->oobretlen = ops->ooblen;
3304
3305err_out:
3306 chip->select_chip(mtd, -1);
3307 return ret;
3308}
3309
3310/**
3311 * panic_nand_write - [MTD Interface] NAND write with ECC
3312 * @mtd: MTD device structure
3313 * @to: offset to write to
3314 * @len: number of bytes to write
3315 * @retlen: pointer to variable to store the number of written bytes
3316 * @buf: the data to write
3317 *
3318 * NAND write with ECC. Used when performing writes in interrupt context, this
3319 * may for example be called by mtdoops when writing an oops while in panic.
3320 */
3321static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3322 size_t *retlen, const uint8_t *buf)
3323{
Scott Wood17fed142016-05-30 13:57:56 -05003324 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003325 struct mtd_oob_ops ops;
3326 int ret;
3327
3328 /* Wait for the device to get ready */
3329 panic_nand_wait(mtd, chip, 400);
3330
3331 /* Grab the device */
3332 panic_nand_get_device(chip, mtd, FL_WRITING);
3333
Scott Wood3ea94ed2015-06-26 19:03:26 -05003334 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003335 ops.len = len;
3336 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003337 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003338
Heiko Schocherf5895d12014-06-24 10:10:04 +02003339 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003340
Sergey Lapin3a38a552013-01-14 03:46:50 +00003341 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003342 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003343}
3344
3345/**
William Juul52c07962007-10-31 13:53:06 +01003346 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003347 * @mtd: MTD device structure
3348 * @to: offset to write to
3349 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003350 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003351 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003352 */
William Juul52c07962007-10-31 13:53:06 +01003353static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3354 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003355{
William Juul52c07962007-10-31 13:53:06 +01003356 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003357 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003358
Heiko Schocherf5895d12014-06-24 10:10:04 +02003359 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003360 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003361
Scott Wood52ab7ce2016-05-30 13:57:58 -05003362 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003363
3364 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003365 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003366 pr_debug("%s: attempt to write past end of page\n",
3367 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003368 return -EINVAL;
3369 }
3370
William Juul52c07962007-10-31 13:53:06 +01003371 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003372 pr_debug("%s: attempt to start write outside oob\n",
3373 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003374 return -EINVAL;
3375 }
3376
Christian Hitz13fc0e22011-10-12 09:32:01 +02003377 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003378 if (unlikely(to >= mtd->size ||
3379 ops->ooboffs + ops->ooblen >
3380 ((mtd->size >> chip->page_shift) -
3381 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003382 pr_debug("%s: attempt to write beyond end of device\n",
3383 __func__);
William Juul52c07962007-10-31 13:53:06 +01003384 return -EINVAL;
3385 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003386
William Juul52c07962007-10-31 13:53:06 +01003387 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003388
3389 /*
3390 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3391 * of my DiskOnChip 2000 test units) will clear the whole data page too
3392 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3393 * it in the doc2000 driver in August 1999. dwmw2.
3394 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003395 nand_reset(chip, chipnr);
3396
3397 chip->select_chip(mtd, chipnr);
3398
3399 /* Shift to get page */
3400 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003401
3402 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003403 if (nand_check_wp(mtd)) {
3404 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003405 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003406 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003407
William Juul52c07962007-10-31 13:53:06 +01003408 /* Invalidate the page cache, if we write to the cached page */
3409 if (page == chip->pagebuf)
3410 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003411
Sergey Lapin3a38a552013-01-14 03:46:50 +00003412 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3413
3414 if (ops->mode == MTD_OPS_RAW)
3415 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3416 else
3417 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003418
Heiko Schocherf5895d12014-06-24 10:10:04 +02003419 chip->select_chip(mtd, -1);
3420
William Juul52c07962007-10-31 13:53:06 +01003421 if (status)
3422 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003423
William Juul52c07962007-10-31 13:53:06 +01003424 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003425
William Juul52c07962007-10-31 13:53:06 +01003426 return 0;
3427}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003428
William Juul52c07962007-10-31 13:53:06 +01003429/**
3430 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003431 * @mtd: MTD device structure
3432 * @to: offset to write to
3433 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003434 */
3435static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3436 struct mtd_oob_ops *ops)
3437{
William Juul52c07962007-10-31 13:53:06 +01003438 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003439
William Juul52c07962007-10-31 13:53:06 +01003440 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003441
William Juul52c07962007-10-31 13:53:06 +01003442 /* Do not allow writes past end of device */
3443 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003444 pr_debug("%s: attempt to write beyond end of device\n",
3445 __func__);
William Juul52c07962007-10-31 13:53:06 +01003446 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003447 }
William Juul52c07962007-10-31 13:53:06 +01003448
Heiko Schocherf5895d12014-06-24 10:10:04 +02003449 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003450
Christian Hitz13fc0e22011-10-12 09:32:01 +02003451 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003452 case MTD_OPS_PLACE_OOB:
3453 case MTD_OPS_AUTO_OOB:
3454 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003455 break;
3456
3457 default:
3458 goto out;
3459 }
3460
3461 if (!ops->datbuf)
3462 ret = nand_do_write_oob(mtd, to, ops);
3463 else
3464 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003465
Christian Hitz13fc0e22011-10-12 09:32:01 +02003466out:
William Juul52c07962007-10-31 13:53:06 +01003467 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003468 return ret;
3469}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003470
3471/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003472 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003473 * @mtd: MTD device structure
3474 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003475 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003476 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003477 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003478static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003479{
Scott Wood17fed142016-05-30 13:57:56 -05003480 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003481 unsigned int eraseblock;
3482
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003483 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003484 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003485
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003486 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003487}
3488
3489/**
3490 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003491 * @mtd: MTD device structure
3492 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003493 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003494 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003495 */
William Juul52c07962007-10-31 13:53:06 +01003496static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003497{
William Juul52c07962007-10-31 13:53:06 +01003498 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003499}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003500
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003501/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003502 * nand_erase_nand - [INTERN] erase block(s)
3503 * @mtd: MTD device structure
3504 * @instr: erase instruction
3505 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003506 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003507 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003508 */
William Juul52c07962007-10-31 13:53:06 +01003509int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3510 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003511{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003512 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003513 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003514 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003515
Heiko Schocherf5895d12014-06-24 10:10:04 +02003516 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3517 __func__, (unsigned long long)instr->addr,
3518 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003519
Christian Hitzb8a6b372011-10-12 09:32:02 +02003520 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003521 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003522
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003523 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003524 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003525
3526 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003527 page = (int)(instr->addr >> chip->page_shift);
3528 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003529
3530 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003531 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003532
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003533 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003534 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003535
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003536 /* Check, if it is write protected */
3537 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003538 pr_debug("%s: device is write protected!\n",
3539 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003540 instr->state = MTD_ERASE_FAILED;
3541 goto erase_exit;
3542 }
3543
3544 /* Loop through the pages */
3545 len = instr->len;
3546
3547 instr->state = MTD_ERASING;
3548
3549 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06003550 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003551
Sergey Lapin3a38a552013-01-14 03:46:50 +00003552 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003553 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003554 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003555 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003556 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003557 instr->state = MTD_ERASE_FAILED;
3558 goto erase_exit;
3559 }
William Juul52c07962007-10-31 13:53:06 +01003560
3561 /*
3562 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003563 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003564 */
3565 if (page <= chip->pagebuf && chip->pagebuf <
3566 (page + pages_per_block))
3567 chip->pagebuf = -1;
3568
Scott Wood3ea94ed2015-06-26 19:03:26 -05003569 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003570
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003571 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003572 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003573 pr_debug("%s: failed erase, page 0x%08x\n",
3574 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003575 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003576 instr->fail_addr =
3577 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003578 goto erase_exit;
3579 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003580
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003581 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003582 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003583 page += pages_per_block;
3584
3585 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003586 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003587 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003588 chip->select_chip(mtd, -1);
3589 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003590 }
3591 }
3592 instr->state = MTD_ERASE_DONE;
3593
Christian Hitz13fc0e22011-10-12 09:32:01 +02003594erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003595
3596 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003597
3598 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003599 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003600 nand_release_device(mtd);
3601
Scott Wood3628f002008-10-24 16:20:43 -05003602 /* Do call back function */
3603 if (!ret)
3604 mtd_erase_callback(instr);
3605
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003606 /* Return more or less happy */
3607 return ret;
3608}
3609
3610/**
3611 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003612 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003613 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003614 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003615 */
William Juul52c07962007-10-31 13:53:06 +01003616static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003617{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003618 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003619
3620 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003621 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003622 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003623 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003624}
3625
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003626/**
William Juul52c07962007-10-31 13:53:06 +01003627 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003628 * @mtd: MTD device structure
3629 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003630 */
William Juul52c07962007-10-31 13:53:06 +01003631static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003632{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003633 struct nand_chip *chip = mtd_to_nand(mtd);
3634 int chipnr = (int)(offs >> chip->chip_shift);
3635 int ret;
3636
3637 /* Select the NAND device */
3638 nand_get_device(mtd, FL_READING);
3639 chip->select_chip(mtd, chipnr);
3640
3641 ret = nand_block_checkbad(mtd, offs, 0);
3642
3643 chip->select_chip(mtd, -1);
3644 nand_release_device(mtd);
3645
3646 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003647}
3648
3649/**
William Juul52c07962007-10-31 13:53:06 +01003650 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003651 * @mtd: MTD device structure
3652 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003653 */
William Juul52c07962007-10-31 13:53:06 +01003654static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003655{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003656 int ret;
3657
Christian Hitzb8a6b372011-10-12 09:32:02 +02003658 ret = nand_block_isbad(mtd, ofs);
3659 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003660 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003661 if (ret > 0)
3662 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003663 return ret;
3664 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003665
Heiko Schocherf5895d12014-06-24 10:10:04 +02003666 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003667}
3668
Heiko Schocherf5895d12014-06-24 10:10:04 +02003669/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003670 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3671 * @mtd: MTD device structure
3672 * @chip: nand chip info structure
3673 * @addr: feature address.
3674 * @subfeature_param: the subfeature parameters, a four bytes array.
3675 */
3676static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3677 int addr, uint8_t *subfeature_param)
3678{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003679#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3680 if (!chip->onfi_version ||
3681 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3682 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003683 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003684#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003685
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003686 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003687}
3688
3689/**
3690 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3691 * @mtd: MTD device structure
3692 * @chip: nand chip info structure
3693 * @addr: feature address.
3694 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003695 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003696static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3697 int addr, uint8_t *subfeature_param)
3698{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003699#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3700 if (!chip->onfi_version ||
3701 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3702 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003703 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003704#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003705
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003706 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003707}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003708
Sergey Lapin3a38a552013-01-14 03:46:50 +00003709/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003710static void nand_set_defaults(struct nand_chip *chip, int busw)
3711{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003712 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003713 if (!chip->chip_delay)
3714 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003715
3716 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003717 if (chip->cmdfunc == NULL)
3718 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003719
3720 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003721 if (chip->waitfunc == NULL)
3722 chip->waitfunc = nand_wait;
3723
3724 if (!chip->select_chip)
3725 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003726
3727 /* set for ONFI nand */
3728 if (!chip->onfi_set_features)
3729 chip->onfi_set_features = nand_onfi_set_features;
3730 if (!chip->onfi_get_features)
3731 chip->onfi_get_features = nand_onfi_get_features;
3732
3733 /* If called twice, pointers that depend on busw may need to be reset */
3734 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003735 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3736 if (!chip->read_word)
3737 chip->read_word = nand_read_word;
3738 if (!chip->block_bad)
3739 chip->block_bad = nand_block_bad;
3740 if (!chip->block_markbad)
3741 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003742 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003743 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003744 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3745 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3746 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003747 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003748 if (!chip->scan_bbt)
3749 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003750
3751 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003752 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003753 spin_lock_init(&chip->controller->lock);
3754 init_waitqueue_head(&chip->controller->wq);
3755 }
3756
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003757 if (!chip->buf_align)
3758 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003759}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003760
Sergey Lapin3a38a552013-01-14 03:46:50 +00003761/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003762static void sanitize_string(char *s, size_t len)
3763{
3764 ssize_t i;
3765
Sergey Lapin3a38a552013-01-14 03:46:50 +00003766 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003767 s[len - 1] = 0;
3768
Sergey Lapin3a38a552013-01-14 03:46:50 +00003769 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003770 for (i = 0; i < len - 1; i++) {
3771 if (s[i] < ' ' || s[i] > 127)
3772 s[i] = '?';
3773 }
3774
Sergey Lapin3a38a552013-01-14 03:46:50 +00003775 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003776 strim(s);
3777}
3778
Florian Fainellic98a9352011-02-25 00:01:34 +00003779static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3780{
3781 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003782 while (len--) {
3783 crc ^= *p++ << 8;
3784 for (i = 0; i < 8; i++)
3785 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3786 }
3787
3788 return crc;
3789}
3790
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003791#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003792/* Parse the Extended Parameter Page. */
3793static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3794 struct nand_chip *chip, struct nand_onfi_params *p)
3795{
3796 struct onfi_ext_param_page *ep;
3797 struct onfi_ext_section *s;
3798 struct onfi_ext_ecc_info *ecc;
3799 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003800 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003801 int len;
3802 int i;
3803
3804 len = le16_to_cpu(p->ext_param_page_length) * 16;
3805 ep = kmalloc(len, GFP_KERNEL);
3806 if (!ep)
3807 return -ENOMEM;
3808
3809 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003810 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3811 if (ret)
3812 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003813
3814 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003815 ret = nand_change_read_column_op(chip,
3816 sizeof(*p) * p->num_of_param_pages,
3817 ep, len, true);
3818 if (ret)
3819 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003820
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003821 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003822 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3823 != le16_to_cpu(ep->crc))) {
3824 pr_debug("fail in the CRC.\n");
3825 goto ext_out;
3826 }
3827
3828 /*
3829 * Check the signature.
3830 * Do not strictly follow the ONFI spec, maybe changed in future.
3831 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003832 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003833 pr_debug("The signature is invalid.\n");
3834 goto ext_out;
3835 }
3836
3837 /* find the ECC section. */
3838 cursor = (uint8_t *)(ep + 1);
3839 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3840 s = ep->sections + i;
3841 if (s->type == ONFI_SECTION_TYPE_2)
3842 break;
3843 cursor += s->length * 16;
3844 }
3845 if (i == ONFI_EXT_SECTION_MAX) {
3846 pr_debug("We can not find the ECC section.\n");
3847 goto ext_out;
3848 }
3849
3850 /* get the info we want. */
3851 ecc = (struct onfi_ext_ecc_info *)cursor;
3852
3853 if (!ecc->codeword_size) {
3854 pr_debug("Invalid codeword size\n");
3855 goto ext_out;
3856 }
3857
3858 chip->ecc_strength_ds = ecc->ecc_bits;
3859 chip->ecc_step_ds = 1 << ecc->codeword_size;
3860 ret = 0;
3861
3862ext_out:
3863 kfree(ep);
3864 return ret;
3865}
3866
3867static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3868{
Scott Wood17fed142016-05-30 13:57:56 -05003869 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003870 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3871
3872 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3873 feature);
3874}
3875
3876/*
3877 * Configure chip properties from Micron vendor-specific ONFI table
3878 */
3879static void nand_onfi_detect_micron(struct nand_chip *chip,
3880 struct nand_onfi_params *p)
3881{
3882 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3883
3884 if (le16_to_cpu(p->vendor_revision) < 1)
3885 return;
3886
3887 chip->read_retries = micron->read_retry_options;
3888 chip->setup_read_retry = nand_setup_read_retry_micron;
3889}
3890
Florian Fainellic98a9352011-02-25 00:01:34 +00003891/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003892 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003893 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003894static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003895 int *busw)
3896{
3897 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003898 char id[4];
3899 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003900
Sergey Lapin3a38a552013-01-14 03:46:50 +00003901 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003902 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3903 if (ret || strncmp(id, "ONFI", 4))
3904 return 0;
3905
3906 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3907 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003908 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003909
Florian Fainellic98a9352011-02-25 00:01:34 +00003910 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003911 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3912 if (ret)
3913 return 0;
3914
Florian Fainellic98a9352011-02-25 00:01:34 +00003915 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003916 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003917 break;
3918 }
3919 }
3920
Heiko Schocherf5895d12014-06-24 10:10:04 +02003921 if (i == 3) {
3922 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003923 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003924 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003925
Sergey Lapin3a38a552013-01-14 03:46:50 +00003926 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003927 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003928 if (val & (1 << 5))
3929 chip->onfi_version = 23;
3930 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003931 chip->onfi_version = 22;
3932 else if (val & (1 << 3))
3933 chip->onfi_version = 21;
3934 else if (val & (1 << 2))
3935 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003936 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003937 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003938
3939 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003940 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003941 return 0;
3942 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003943
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003944 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3945 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003946 if (!mtd->name)
3947 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003948
Florian Fainellic98a9352011-02-25 00:01:34 +00003949 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003950
3951 /*
3952 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3953 * (don't ask me who thought of this...). MTD assumes that these
3954 * dimensions will be power-of-2, so just truncate the remaining area.
3955 */
3956 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3957 mtd->erasesize *= mtd->writesize;
3958
Florian Fainellic98a9352011-02-25 00:01:34 +00003959 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003960
3961 /* See erasesize comment */
3962 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003963 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003964 chip->bits_per_cell = p->bits_per_cell;
3965
3966 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003967 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003968 else
3969 *busw = 0;
3970
3971 if (p->ecc_bits != 0xff) {
3972 chip->ecc_strength_ds = p->ecc_bits;
3973 chip->ecc_step_ds = 512;
3974 } else if (chip->onfi_version >= 21 &&
3975 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3976
3977 /*
3978 * The nand_flash_detect_ext_param_page() uses the
3979 * Change Read Column command which maybe not supported
3980 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3981 * now. We do not replace user supplied command function.
3982 */
3983 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3984 chip->cmdfunc = nand_command_lp;
3985
3986 /* The Extended Parameter Page is supported since ONFI 2.1. */
3987 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3988 pr_warn("Failed to detect ONFI extended param page\n");
3989 } else {
3990 pr_warn("Could not retrieve ONFI ECC requirements\n");
3991 }
3992
3993 if (p->jedec_id == NAND_MFR_MICRON)
3994 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003995
3996 return 1;
3997}
3998#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02003999static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00004000 int *busw)
4001{
4002 return 0;
4003}
4004#endif
4005
William Juul52c07962007-10-31 13:53:06 +01004006/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004007 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4008 */
4009static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
4010 int *busw)
4011{
4012 struct nand_jedec_params *p = &chip->jedec_params;
4013 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004014 char id[5];
4015 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004016
4017 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004018 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4019 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004020 return 0;
4021
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004022 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4023 if (ret)
4024 return 0;
4025
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004026 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004027 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4028 if (ret)
4029 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004030
4031 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4032 le16_to_cpu(p->crc))
4033 break;
4034 }
4035
4036 if (i == 3) {
4037 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4038 return 0;
4039 }
4040
4041 /* Check version */
4042 val = le16_to_cpu(p->revision);
4043 if (val & (1 << 2))
4044 chip->jedec_version = 10;
4045 else if (val & (1 << 1))
4046 chip->jedec_version = 1; /* vendor specific version */
4047
4048 if (!chip->jedec_version) {
4049 pr_info("unsupported JEDEC version: %d\n", val);
4050 return 0;
4051 }
4052
4053 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4054 sanitize_string(p->model, sizeof(p->model));
4055 if (!mtd->name)
4056 mtd->name = p->model;
4057
4058 mtd->writesize = le32_to_cpu(p->byte_per_page);
4059
4060 /* Please reference to the comment for nand_flash_detect_onfi. */
4061 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4062 mtd->erasesize *= mtd->writesize;
4063
4064 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4065
4066 /* Please reference to the comment for nand_flash_detect_onfi. */
4067 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4068 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4069 chip->bits_per_cell = p->bits_per_cell;
4070
4071 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
4072 *busw = NAND_BUSWIDTH_16;
4073 else
4074 *busw = 0;
4075
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 */
4163static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
4164 u8 id_data[8], int *busw)
4165{
4166 int extid, id_len;
4167 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004168 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004169 /* The 4th id byte is the important one */
4170 extid = id_data[3];
4171
4172 id_len = nand_id_len(id_data, 8);
4173
4174 /*
4175 * Field definitions are in the following datasheets:
4176 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
4177 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
4178 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
4179 *
4180 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
4181 * ID to decide what to do.
4182 */
4183 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02004184 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004185 /* Calc pagesize */
4186 mtd->writesize = 2048 << (extid & 0x03);
4187 extid >>= 2;
4188 /* Calc oobsize */
4189 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4190 case 1:
4191 mtd->oobsize = 128;
4192 break;
4193 case 2:
4194 mtd->oobsize = 218;
4195 break;
4196 case 3:
4197 mtd->oobsize = 400;
4198 break;
4199 case 4:
4200 mtd->oobsize = 436;
4201 break;
4202 case 5:
4203 mtd->oobsize = 512;
4204 break;
4205 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004206 mtd->oobsize = 640;
4207 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004208 case 7:
4209 default: /* Other cases are "reserved" (unknown) */
4210 mtd->oobsize = 1024;
4211 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004212 }
4213 extid >>= 2;
4214 /* Calc blocksize */
4215 mtd->erasesize = (128 * 1024) <<
4216 (((extid >> 1) & 0x04) | (extid & 0x03));
4217 *busw = 0;
4218 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02004219 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004220 unsigned int tmp;
4221
4222 /* Calc pagesize */
4223 mtd->writesize = 2048 << (extid & 0x03);
4224 extid >>= 2;
4225 /* Calc oobsize */
4226 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4227 case 0:
4228 mtd->oobsize = 128;
4229 break;
4230 case 1:
4231 mtd->oobsize = 224;
4232 break;
4233 case 2:
4234 mtd->oobsize = 448;
4235 break;
4236 case 3:
4237 mtd->oobsize = 64;
4238 break;
4239 case 4:
4240 mtd->oobsize = 32;
4241 break;
4242 case 5:
4243 mtd->oobsize = 16;
4244 break;
4245 default:
4246 mtd->oobsize = 640;
4247 break;
4248 }
4249 extid >>= 2;
4250 /* Calc blocksize */
4251 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
4252 if (tmp < 0x03)
4253 mtd->erasesize = (128 * 1024) << tmp;
4254 else if (tmp == 0x03)
4255 mtd->erasesize = 768 * 1024;
4256 else
4257 mtd->erasesize = (64 * 1024) << tmp;
4258 *busw = 0;
4259 } else {
4260 /* Calc pagesize */
4261 mtd->writesize = 1024 << (extid & 0x03);
4262 extid >>= 2;
4263 /* Calc oobsize */
4264 mtd->oobsize = (8 << (extid & 0x01)) *
4265 (mtd->writesize >> 9);
4266 extid >>= 2;
4267 /* Calc blocksize. Blocksize is multiples of 64KiB */
4268 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4269 extid >>= 2;
4270 /* Get buswidth information */
4271 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004272
4273 /*
4274 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
4275 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
4276 * follows:
4277 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
4278 * 110b -> 24nm
4279 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
4280 */
4281 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
4282 nand_is_slc(chip) &&
4283 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
4284 !(id_data[4] & 0x80) /* !BENAND */) {
4285 mtd->oobsize = 32 * mtd->writesize >> 9;
4286 }
4287
Sergey Lapin3a38a552013-01-14 03:46:50 +00004288 }
4289}
4290
Heiko Schocherf5895d12014-06-24 10:10:04 +02004291/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004292 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4293 * decodes a matching ID table entry and assigns the MTD size parameters for
4294 * the chip.
4295 */
4296static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004297 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00004298 int *busw)
4299{
4300 int maf_id = id_data[0];
4301
4302 mtd->erasesize = type->erasesize;
4303 mtd->writesize = type->pagesize;
4304 mtd->oobsize = mtd->writesize / 32;
4305 *busw = type->options & NAND_BUSWIDTH_16;
4306
Heiko Schocherf5895d12014-06-24 10:10:04 +02004307 /* All legacy ID NAND are small-page, SLC */
4308 chip->bits_per_cell = 1;
4309
Sergey Lapin3a38a552013-01-14 03:46:50 +00004310 /*
4311 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
4312 * some Spansion chips have erasesize that conflicts with size
4313 * listed in nand_ids table.
4314 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
4315 */
4316 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
4317 && id_data[6] == 0x00 && id_data[7] == 0x00
4318 && mtd->writesize == 512) {
4319 mtd->erasesize = 128 * 1024;
4320 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
4321 }
4322}
4323
Heiko Schocherf5895d12014-06-24 10:10:04 +02004324/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004325 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4326 * heuristic patterns using various detected parameters (e.g., manufacturer,
4327 * page size, cell-type information).
4328 */
4329static void nand_decode_bbm_options(struct mtd_info *mtd,
4330 struct nand_chip *chip, u8 id_data[8])
4331{
4332 int maf_id = id_data[0];
4333
4334 /* Set the bad block position */
4335 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4336 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4337 else
4338 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4339
4340 /*
4341 * Bad block marker is stored in the last page of each block on Samsung
4342 * and Hynix MLC devices; stored in first two pages of each block on
4343 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4344 * AMD/Spansion, and Macronix. All others scan only the first page.
4345 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004346 if (!nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00004347 (maf_id == NAND_MFR_SAMSUNG ||
4348 maf_id == NAND_MFR_HYNIX))
4349 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004350 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00004351 (maf_id == NAND_MFR_SAMSUNG ||
4352 maf_id == NAND_MFR_HYNIX ||
4353 maf_id == NAND_MFR_TOSHIBA ||
4354 maf_id == NAND_MFR_AMD ||
4355 maf_id == NAND_MFR_MACRONIX)) ||
4356 (mtd->writesize == 2048 &&
4357 maf_id == NAND_MFR_MICRON))
4358 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4359}
4360
Heiko Schocherf5895d12014-06-24 10:10:04 +02004361static inline bool is_full_id_nand(struct nand_flash_dev *type)
4362{
4363 return type->id_len;
4364}
4365
4366static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
4367 struct nand_flash_dev *type, u8 *id_data, int *busw)
4368{
Heiko Schocherf5895d12014-06-24 10:10:04 +02004369 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004370 mtd->writesize = type->pagesize;
4371 mtd->erasesize = type->erasesize;
4372 mtd->oobsize = type->oobsize;
4373
4374 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
4375 chip->chipsize = (uint64_t)type->chipsize << 20;
4376 chip->options |= type->options;
4377 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4378 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004379 chip->onfi_timing_mode_default =
4380 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004381
4382 *busw = type->options & NAND_BUSWIDTH_16;
4383
4384 if (!mtd->name)
4385 mtd->name = type->name;
4386
4387 return true;
4388 }
4389 return false;
4390}
4391
Sergey Lapin3a38a552013-01-14 03:46:50 +00004392/*
4393 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004394 */
Jörg Krause929fb442018-01-14 19:26:37 +01004395struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01004396 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00004397 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004398 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004399{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004400 int busw, ret;
4401 int maf_idx;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004402 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004403
Karl Beldanb6322fc2008-09-15 16:08:03 +02004404 /*
4405 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004406 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004407 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004408 ret = nand_reset(chip, 0);
4409 if (ret)
4410 return ERR_PTR(ret);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004411
4412 /* Select the device */
4413 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004414
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004415 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004416 ret = nand_readid_op(chip, 0, id_data, 2);
4417 if (ret)
4418 return ERR_PTR(ret);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004419
4420 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004421 *maf_id = id_data[0];
4422 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004423
Sergey Lapin3a38a552013-01-14 03:46:50 +00004424 /*
4425 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004426 * interface concerns can cause random data which looks like a
4427 * possibly credible NAND flash to appear. If the two results do
4428 * not match, ignore the device completely.
4429 */
4430
Sergey Lapin3a38a552013-01-14 03:46:50 +00004431 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004432 ret = nand_readid_op(chip, 0, id_data, 8);
4433 if (ret)
4434 return ERR_PTR(ret);
Scott Wood3628f002008-10-24 16:20:43 -05004435
Christian Hitzb8a6b372011-10-12 09:32:02 +02004436 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004437 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004438 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05004439 return ERR_PTR(-ENODEV);
4440 }
4441
Lei Wen75bde942011-01-06 09:48:18 +08004442 if (!type)
4443 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004444
Heiko Schocherf5895d12014-06-24 10:10:04 +02004445 for (; type->name != NULL; type++) {
4446 if (is_full_id_nand(type)) {
4447 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4448 goto ident_done;
4449 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004450 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004451 }
4452 }
Lei Wen75bde942011-01-06 09:48:18 +08004453
Christian Hitzb8a6b372011-10-12 09:32:02 +02004454 chip->onfi_version = 0;
4455 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004456 /* Check if the chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00004457 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004458 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004459
4460 /* Check if the chip is JEDEC compliant */
4461 if (nand_flash_detect_jedec(mtd, chip, &busw))
4462 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004463 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004464
Christian Hitzb8a6b372011-10-12 09:32:02 +02004465 if (!type->name)
4466 return ERR_PTR(-ENODEV);
4467
William Juul52c07962007-10-31 13:53:06 +01004468 if (!mtd->name)
4469 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004470
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004471 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004472
Scott Wood52ab7ce2016-05-30 13:57:58 -05004473 if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004474 /* Decode parameters from extended ID */
4475 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004476 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004477 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004478 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004479 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004480 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004481
Sergey Lapin3a38a552013-01-14 03:46:50 +00004482 /*
4483 * Check if chip is not a Samsung device. Do not clear the
4484 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02004485 */
4486 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4487 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4488ident_done:
4489
William Juul52c07962007-10-31 13:53:06 +01004490 /* Try to identify manufacturer */
4491 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
4492 if (nand_manuf_ids[maf_idx].id == *maf_id)
4493 break;
4494 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004495
Heiko Schocherf5895d12014-06-24 10:10:04 +02004496 if (chip->options & NAND_BUSWIDTH_AUTO) {
4497 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4498 chip->options |= busw;
4499 nand_set_defaults(chip, busw);
4500 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4501 /*
4502 * Check, if buswidth is correct. Hardware drivers should set
4503 * chip correct!
4504 */
4505 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4506 *maf_id, *dev_id);
4507 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4508 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004509 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4510 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01004511 return ERR_PTR(-EINVAL);
4512 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004513
Sergey Lapin3a38a552013-01-14 03:46:50 +00004514 nand_decode_bbm_options(mtd, chip, id_data);
4515
William Juul52c07962007-10-31 13:53:06 +01004516 /* Calculate the address shift from the page size */
4517 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004518 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004519 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004520
William Juul52c07962007-10-31 13:53:06 +01004521 chip->bbt_erase_shift = chip->phys_erase_shift =
4522 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004523 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004524 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004525 else {
4526 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4527 chip->chip_shift += 32 - 1;
4528 }
4529
Masahiro Yamada984926b2017-11-22 02:38:31 +09004530 if (chip->chip_shift - chip->page_shift > 16)
4531 chip->options |= NAND_ROW_ADDR_3;
4532
Christian Hitzb8a6b372011-10-12 09:32:02 +02004533 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004534 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004535
Sergey Lapin3a38a552013-01-14 03:46:50 +00004536 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004537 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4538 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004539
Heiko Schocherf5895d12014-06-24 10:10:04 +02004540 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4541 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004542
Christian Hitzb8a6b372011-10-12 09:32:02 +02004543#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004544 if (chip->onfi_version)
4545 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4546 chip->onfi_params.model);
4547 else if (chip->jedec_version)
4548 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4549 chip->jedec_params.model);
4550 else
4551 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4552 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004553#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004554 if (chip->jedec_version)
4555 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4556 chip->jedec_params.model);
4557 else
4558 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4559 type->name);
4560
4561 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4562 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004563#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004564
Scott Wood3ea94ed2015-06-26 19:03:26 -05004565 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004566 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004567 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01004568 return type;
4569}
Jörg Krause929fb442018-01-14 19:26:37 +01004570EXPORT_SYMBOL(nand_get_flash_type);
William Juul52c07962007-10-31 13:53:06 +01004571
Brian Norrisba6463d2016-06-15 21:09:22 +02004572#if CONFIG_IS_ENABLED(OF_CONTROL)
4573DECLARE_GLOBAL_DATA_PTR;
4574
4575static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
4576{
4577 int ret, ecc_mode = -1, ecc_strength, ecc_step;
4578 const void *blob = gd->fdt_blob;
4579 const char *str;
4580
4581 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
4582 if (ret == 16)
4583 chip->options |= NAND_BUSWIDTH_16;
4584
4585 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
4586 chip->bbt_options |= NAND_BBT_USE_FLASH;
4587
4588 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
4589 if (str) {
4590 if (!strcmp(str, "none"))
4591 ecc_mode = NAND_ECC_NONE;
4592 else if (!strcmp(str, "soft"))
4593 ecc_mode = NAND_ECC_SOFT;
4594 else if (!strcmp(str, "hw"))
4595 ecc_mode = NAND_ECC_HW;
4596 else if (!strcmp(str, "hw_syndrome"))
4597 ecc_mode = NAND_ECC_HW_SYNDROME;
4598 else if (!strcmp(str, "hw_oob_first"))
4599 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4600 else if (!strcmp(str, "soft_bch"))
4601 ecc_mode = NAND_ECC_SOFT_BCH;
4602 }
4603
4604
4605 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
4606 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
4607
4608 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4609 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4610 pr_err("must set both strength and step size in DT\n");
4611 return -EINVAL;
4612 }
4613
4614 if (ecc_mode >= 0)
4615 chip->ecc.mode = ecc_mode;
4616
4617 if (ecc_strength >= 0)
4618 chip->ecc.strength = ecc_strength;
4619
4620 if (ecc_step > 0)
4621 chip->ecc.size = ecc_step;
4622
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004623 if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL))
4624 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4625
Brian Norrisba6463d2016-06-15 21:09:22 +02004626 return 0;
4627}
4628#else
4629static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
4630{
4631 return 0;
4632}
4633#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4634
William Juul52c07962007-10-31 13:53:06 +01004635/**
4636 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004637 * @mtd: MTD device structure
4638 * @maxchips: number of chips to scan for
4639 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004640 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004641 * This is the first phase of the normal nand_scan() function. It reads the
4642 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004643 *
William Juul52c07962007-10-31 13:53:06 +01004644 */
Lei Wen75bde942011-01-06 09:48:18 +08004645int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004646 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004647{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004648 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004649 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004650 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02004651 int ret;
4652
4653 if (chip->flash_node) {
4654 ret = nand_dt_init(mtd, chip, chip->flash_node);
4655 if (ret)
4656 return ret;
4657 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004658
William Juul52c07962007-10-31 13:53:06 +01004659 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004660 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004661
4662 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004663 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4664 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004665
4666 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004667 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4668 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004669 chip->select_chip(mtd, -1);
4670 return PTR_ERR(type);
4671 }
4672
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004673 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004674 ret = nand_init_data_interface(chip);
4675 if (ret)
4676 return ret;
4677
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004678 /*
4679 * Setup the data interface correctly on the chip and controller side.
4680 * This explicit call to nand_setup_data_interface() is only required
4681 * for the first die, because nand_reset() has been called before
4682 * ->data_interface and ->default_onfi_timing_mode were set.
4683 * For the other dies, nand_reset() will automatically switch to the
4684 * best mode for us.
4685 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004686 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004687 if (ret)
4688 return ret;
4689
Heiko Schocherf5895d12014-06-24 10:10:04 +02004690 chip->select_chip(mtd, -1);
4691
William Juul52c07962007-10-31 13:53:06 +01004692 /* Check for a chip array */
4693 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004694 u8 id[2];
4695
Karl Beldanb6322fc2008-09-15 16:08:03 +02004696 /* See comment in nand_get_flash_type for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004697 nand_reset(chip, i);
4698
4699 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004700 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004701 nand_readid_op(chip, 0, id, sizeof(id));
4702
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004703 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004704 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004705 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004706 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004707 }
4708 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004709 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004710
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004711#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004712 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004713 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004714#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004715
William Juul52c07962007-10-31 13:53:06 +01004716 /* Store the number of chips and calc total size for mtd */
4717 chip->numchips = i;
4718 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004719
William Juul52c07962007-10-31 13:53:06 +01004720 return 0;
4721}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004722EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004723
Masahiro Yamada820eb482017-11-22 02:38:29 +09004724/**
4725 * nand_check_ecc_caps - check the sanity of preset ECC settings
4726 * @chip: nand chip info structure
4727 * @caps: ECC caps info structure
4728 * @oobavail: OOB size that the ECC engine can use
4729 *
4730 * When ECC step size and strength are already set, check if they are supported
4731 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4732 * On success, the calculated ECC bytes is set.
4733 */
4734int nand_check_ecc_caps(struct nand_chip *chip,
4735 const struct nand_ecc_caps *caps, int oobavail)
4736{
4737 struct mtd_info *mtd = nand_to_mtd(chip);
4738 const struct nand_ecc_step_info *stepinfo;
4739 int preset_step = chip->ecc.size;
4740 int preset_strength = chip->ecc.strength;
4741 int nsteps, ecc_bytes;
4742 int i, j;
4743
4744 if (WARN_ON(oobavail < 0))
4745 return -EINVAL;
4746
4747 if (!preset_step || !preset_strength)
4748 return -ENODATA;
4749
4750 nsteps = mtd->writesize / preset_step;
4751
4752 for (i = 0; i < caps->nstepinfos; i++) {
4753 stepinfo = &caps->stepinfos[i];
4754
4755 if (stepinfo->stepsize != preset_step)
4756 continue;
4757
4758 for (j = 0; j < stepinfo->nstrengths; j++) {
4759 if (stepinfo->strengths[j] != preset_strength)
4760 continue;
4761
4762 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4763 preset_strength);
4764 if (WARN_ON_ONCE(ecc_bytes < 0))
4765 return ecc_bytes;
4766
4767 if (ecc_bytes * nsteps > oobavail) {
4768 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4769 preset_step, preset_strength);
4770 return -ENOSPC;
4771 }
4772
4773 chip->ecc.bytes = ecc_bytes;
4774
4775 return 0;
4776 }
4777 }
4778
4779 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4780 preset_step, preset_strength);
4781
4782 return -ENOTSUPP;
4783}
4784EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4785
4786/**
4787 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4788 * @chip: nand chip info structure
4789 * @caps: ECC engine caps info structure
4790 * @oobavail: OOB size that the ECC engine can use
4791 *
4792 * If a chip's ECC requirement is provided, try to meet it with the least
4793 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4794 * On success, the chosen ECC settings are set.
4795 */
4796int nand_match_ecc_req(struct nand_chip *chip,
4797 const struct nand_ecc_caps *caps, int oobavail)
4798{
4799 struct mtd_info *mtd = nand_to_mtd(chip);
4800 const struct nand_ecc_step_info *stepinfo;
4801 int req_step = chip->ecc_step_ds;
4802 int req_strength = chip->ecc_strength_ds;
4803 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4804 int best_step, best_strength, best_ecc_bytes;
4805 int best_ecc_bytes_total = INT_MAX;
4806 int i, j;
4807
4808 if (WARN_ON(oobavail < 0))
4809 return -EINVAL;
4810
4811 /* No information provided by the NAND chip */
4812 if (!req_step || !req_strength)
4813 return -ENOTSUPP;
4814
4815 /* number of correctable bits the chip requires in a page */
4816 req_corr = mtd->writesize / req_step * req_strength;
4817
4818 for (i = 0; i < caps->nstepinfos; i++) {
4819 stepinfo = &caps->stepinfos[i];
4820 step_size = stepinfo->stepsize;
4821
4822 for (j = 0; j < stepinfo->nstrengths; j++) {
4823 strength = stepinfo->strengths[j];
4824
4825 /*
4826 * If both step size and strength are smaller than the
4827 * chip's requirement, it is not easy to compare the
4828 * resulted reliability.
4829 */
4830 if (step_size < req_step && strength < req_strength)
4831 continue;
4832
4833 if (mtd->writesize % step_size)
4834 continue;
4835
4836 nsteps = mtd->writesize / step_size;
4837
4838 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4839 if (WARN_ON_ONCE(ecc_bytes < 0))
4840 continue;
4841 ecc_bytes_total = ecc_bytes * nsteps;
4842
4843 if (ecc_bytes_total > oobavail ||
4844 strength * nsteps < req_corr)
4845 continue;
4846
4847 /*
4848 * We assume the best is to meet the chip's requrement
4849 * with the least number of ECC bytes.
4850 */
4851 if (ecc_bytes_total < best_ecc_bytes_total) {
4852 best_ecc_bytes_total = ecc_bytes_total;
4853 best_step = step_size;
4854 best_strength = strength;
4855 best_ecc_bytes = ecc_bytes;
4856 }
4857 }
4858 }
4859
4860 if (best_ecc_bytes_total == INT_MAX)
4861 return -ENOTSUPP;
4862
4863 chip->ecc.size = best_step;
4864 chip->ecc.strength = best_strength;
4865 chip->ecc.bytes = best_ecc_bytes;
4866
4867 return 0;
4868}
4869EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4870
4871/**
4872 * nand_maximize_ecc - choose the max ECC strength available
4873 * @chip: nand chip info structure
4874 * @caps: ECC engine caps info structure
4875 * @oobavail: OOB size that the ECC engine can use
4876 *
4877 * Choose the max ECC strength that is supported on the controller, and can fit
4878 * within the chip's OOB. On success, the chosen ECC settings are set.
4879 */
4880int nand_maximize_ecc(struct nand_chip *chip,
4881 const struct nand_ecc_caps *caps, int oobavail)
4882{
4883 struct mtd_info *mtd = nand_to_mtd(chip);
4884 const struct nand_ecc_step_info *stepinfo;
4885 int step_size, strength, nsteps, ecc_bytes, corr;
4886 int best_corr = 0;
4887 int best_step = 0;
4888 int best_strength, best_ecc_bytes;
4889 int i, j;
4890
4891 if (WARN_ON(oobavail < 0))
4892 return -EINVAL;
4893
4894 for (i = 0; i < caps->nstepinfos; i++) {
4895 stepinfo = &caps->stepinfos[i];
4896 step_size = stepinfo->stepsize;
4897
4898 /* If chip->ecc.size is already set, respect it */
4899 if (chip->ecc.size && step_size != chip->ecc.size)
4900 continue;
4901
4902 for (j = 0; j < stepinfo->nstrengths; j++) {
4903 strength = stepinfo->strengths[j];
4904
4905 if (mtd->writesize % step_size)
4906 continue;
4907
4908 nsteps = mtd->writesize / step_size;
4909
4910 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4911 if (WARN_ON_ONCE(ecc_bytes < 0))
4912 continue;
4913
4914 if (ecc_bytes * nsteps > oobavail)
4915 continue;
4916
4917 corr = strength * nsteps;
4918
4919 /*
4920 * If the number of correctable bits is the same,
4921 * bigger step_size has more reliability.
4922 */
4923 if (corr > best_corr ||
4924 (corr == best_corr && step_size > best_step)) {
4925 best_corr = corr;
4926 best_step = step_size;
4927 best_strength = strength;
4928 best_ecc_bytes = ecc_bytes;
4929 }
4930 }
4931 }
4932
4933 if (!best_corr)
4934 return -ENOTSUPP;
4935
4936 chip->ecc.size = best_step;
4937 chip->ecc.strength = best_strength;
4938 chip->ecc.bytes = best_ecc_bytes;
4939
4940 return 0;
4941}
4942EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4943
Scott Wood3ea94ed2015-06-26 19:03:26 -05004944/*
4945 * Check if the chip configuration meet the datasheet requirements.
4946
4947 * If our configuration corrects A bits per B bytes and the minimum
4948 * required correction level is X bits per Y bytes, then we must ensure
4949 * both of the following are true:
4950 *
4951 * (1) A / B >= X / Y
4952 * (2) A >= X
4953 *
4954 * Requirement (1) ensures we can correct for the required bitflip density.
4955 * Requirement (2) ensures we can correct even when all bitflips are clumped
4956 * in the same sector.
4957 */
4958static bool nand_ecc_strength_good(struct mtd_info *mtd)
4959{
Scott Wood17fed142016-05-30 13:57:56 -05004960 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004961 struct nand_ecc_ctrl *ecc = &chip->ecc;
4962 int corr, ds_corr;
4963
4964 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4965 /* Not enough information */
4966 return true;
4967
4968 /*
4969 * We get the number of corrected bits per page to compare
4970 * the correction density.
4971 */
4972 corr = (mtd->writesize * ecc->strength) / ecc->size;
4973 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4974
4975 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4976}
William Juul52c07962007-10-31 13:53:06 +01004977
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004978static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4979{
4980 struct nand_ecc_ctrl *ecc = &chip->ecc;
4981
4982 if (nand_standard_page_accessors(ecc))
4983 return false;
4984
4985 /*
4986 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4987 * controller driver implements all the page accessors because
4988 * default helpers are not suitable when the core does not
4989 * send the READ0/PAGEPROG commands.
4990 */
4991 return (!ecc->read_page || !ecc->write_page ||
4992 !ecc->read_page_raw || !ecc->write_page_raw ||
4993 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4994 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4995 ecc->hwctl && ecc->calculate));
4996}
4997
William Juul52c07962007-10-31 13:53:06 +01004998/**
4999 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005000 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01005001 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005002 * This is the second phase of the normal nand_scan() function. It fills out
5003 * all the uninitialized function pointers with the defaults and scans for a
5004 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01005005 */
5006int nand_scan_tail(struct mtd_info *mtd)
5007{
5008 int i;
Scott Wood17fed142016-05-30 13:57:56 -05005009 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005010 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005011 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005012
Sergey Lapin3a38a552013-01-14 03:46:50 +00005013 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
5014 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
5015 !(chip->bbt_options & NAND_BBT_USE_FLASH));
5016
Marc Gonzalezc3a29852017-11-22 02:38:22 +09005017 if (invalid_ecc_page_accessors(chip)) {
5018 pr_err("Invalid ECC page accessors setup\n");
5019 return -EINVAL;
5020 }
5021
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005022 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005023 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005024 chip->buffers = nbuf;
5025 } else {
5026 if (!chip->buffers)
5027 return -ENOMEM;
5028 }
William Juul52c07962007-10-31 13:53:06 +01005029
5030 /* Set the internal oob buffer location, just after the page data */
5031 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
5032
5033 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005034 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01005035 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005036 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005037 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02005038#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005039 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005040 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005041 break;
5042 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005043 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005044 break;
5045 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005046 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005047 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005048 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005049 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005050 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01005051#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005052 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005053 pr_warn("No oob scheme defined for oobsize %d\n",
5054 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005055 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005056 }
5057 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005058
William Juul52c07962007-10-31 13:53:06 +01005059 if (!chip->write_page)
5060 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005061
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005062 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005063 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01005064 * selected and we have 256 byte pagesize fallback to software ECC
5065 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005066
Heiko Schocherf5895d12014-06-24 10:10:04 +02005067 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005068 case NAND_ECC_HW_OOB_FIRST:
5069 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005070 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005071 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005072 BUG();
5073 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005074 if (!ecc->read_page)
5075 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005076
William Juul52c07962007-10-31 13:53:06 +01005077 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005078 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005079 if (!ecc->read_page)
5080 ecc->read_page = nand_read_page_hwecc;
5081 if (!ecc->write_page)
5082 ecc->write_page = nand_write_page_hwecc;
5083 if (!ecc->read_page_raw)
5084 ecc->read_page_raw = nand_read_page_raw;
5085 if (!ecc->write_page_raw)
5086 ecc->write_page_raw = nand_write_page_raw;
5087 if (!ecc->read_oob)
5088 ecc->read_oob = nand_read_oob_std;
5089 if (!ecc->write_oob)
5090 ecc->write_oob = nand_write_oob_std;
5091 if (!ecc->read_subpage)
5092 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05005093 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02005094 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005095
William Juul52c07962007-10-31 13:53:06 +01005096 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005097 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5098 (!ecc->read_page ||
5099 ecc->read_page == nand_read_page_hwecc ||
5100 !ecc->write_page ||
5101 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005102 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005103 BUG();
5104 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005105 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005106 if (!ecc->read_page)
5107 ecc->read_page = nand_read_page_syndrome;
5108 if (!ecc->write_page)
5109 ecc->write_page = nand_write_page_syndrome;
5110 if (!ecc->read_page_raw)
5111 ecc->read_page_raw = nand_read_page_raw_syndrome;
5112 if (!ecc->write_page_raw)
5113 ecc->write_page_raw = nand_write_page_raw_syndrome;
5114 if (!ecc->read_oob)
5115 ecc->read_oob = nand_read_oob_syndrome;
5116 if (!ecc->write_oob)
5117 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005118
Heiko Schocherf5895d12014-06-24 10:10:04 +02005119 if (mtd->writesize >= ecc->size) {
5120 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005121 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5122 BUG();
5123 }
William Juul52c07962007-10-31 13:53:06 +01005124 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005125 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005126 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5127 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005128 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005129
William Juul52c07962007-10-31 13:53:06 +01005130 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005131 ecc->calculate = nand_calculate_ecc;
5132 ecc->correct = nand_correct_data;
5133 ecc->read_page = nand_read_page_swecc;
5134 ecc->read_subpage = nand_read_subpage;
5135 ecc->write_page = nand_write_page_swecc;
5136 ecc->read_page_raw = nand_read_page_raw;
5137 ecc->write_page_raw = nand_write_page_raw;
5138 ecc->read_oob = nand_read_oob_std;
5139 ecc->write_oob = nand_write_oob_std;
5140 if (!ecc->size)
5141 ecc->size = 256;
5142 ecc->bytes = 3;
5143 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005144 break;
5145
Christian Hitz55f7bca2011-10-12 09:31:59 +02005146 case NAND_ECC_SOFT_BCH:
5147 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005148 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005149 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005150 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005151 ecc->calculate = nand_bch_calculate_ecc;
5152 ecc->correct = nand_bch_correct_data;
5153 ecc->read_page = nand_read_page_swecc;
5154 ecc->read_subpage = nand_read_subpage;
5155 ecc->write_page = nand_write_page_swecc;
5156 ecc->read_page_raw = nand_read_page_raw;
5157 ecc->write_page_raw = nand_write_page_raw;
5158 ecc->read_oob = nand_read_oob_std;
5159 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005160 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005161 * Board driver should supply ecc.size and ecc.strength values
5162 * to select how many bits are correctable. Otherwise, default
5163 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005164 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005165 if (!ecc->size && (mtd->oobsize >= 64)) {
5166 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005167 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005168 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005169
5170 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005171 ecc->bytes = 0;
5172 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005173 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005174 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005175 BUG();
5176 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005177 break;
5178
William Juul52c07962007-10-31 13:53:06 +01005179 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005180 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005181 ecc->read_page = nand_read_page_raw;
5182 ecc->write_page = nand_write_page_raw;
5183 ecc->read_oob = nand_read_oob_std;
5184 ecc->read_page_raw = nand_read_page_raw;
5185 ecc->write_page_raw = nand_write_page_raw;
5186 ecc->write_oob = nand_write_oob_std;
5187 ecc->size = mtd->writesize;
5188 ecc->bytes = 0;
5189 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005190 break;
5191
5192 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005193 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005194 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005195 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005196
Sergey Lapin3a38a552013-01-14 03:46:50 +00005197 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005198 if (!ecc->read_oob_raw)
5199 ecc->read_oob_raw = ecc->read_oob;
5200 if (!ecc->write_oob_raw)
5201 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005202
William Juul52c07962007-10-31 13:53:06 +01005203 /*
5204 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005205 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005206 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005207 mtd->oobavail = 0;
5208 if (ecc->layout) {
5209 for (i = 0; ecc->layout->oobfree[i].length; i++)
5210 mtd->oobavail += ecc->layout->oobfree[i].length;
5211 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005212
Scott Wood3ea94ed2015-06-26 19:03:26 -05005213 /* ECC sanity check: warn if it's too weak */
5214 if (!nand_ecc_strength_good(mtd))
5215 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5216 mtd->name);
5217
William Juul52c07962007-10-31 13:53:06 +01005218 /*
5219 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005220 * mode.
William Juul52c07962007-10-31 13:53:06 +01005221 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005222 ecc->steps = mtd->writesize / ecc->size;
5223 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005224 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005225 BUG();
5226 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005227 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005228
Sergey Lapin3a38a552013-01-14 03:46:50 +00005229 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005230 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5231 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005232 case 2:
5233 mtd->subpage_sft = 1;
5234 break;
5235 case 4:
5236 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005237 case 16:
William Juul52c07962007-10-31 13:53:06 +01005238 mtd->subpage_sft = 2;
5239 break;
5240 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005241 }
William Juul52c07962007-10-31 13:53:06 +01005242 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005243
William Juul52c07962007-10-31 13:53:06 +01005244 /* Initialize state */
5245 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005246
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005247 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005248 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005249
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005250 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005251 switch (ecc->mode) {
5252 case NAND_ECC_SOFT:
5253 case NAND_ECC_SOFT_BCH:
5254 if (chip->page_shift > 9)
5255 chip->options |= NAND_SUBPAGE_READ;
5256 break;
5257
5258 default:
5259 break;
5260 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005261
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005262 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005263 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005264 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5265 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005266 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005267 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005268 mtd->_read_oob = nand_read_oob;
5269 mtd->_write_oob = nand_write_oob;
5270 mtd->_sync = nand_sync;
5271 mtd->_lock = NULL;
5272 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005273 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005274 mtd->_block_isbad = nand_block_isbad;
5275 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005276 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005277
Sergey Lapin3a38a552013-01-14 03:46:50 +00005278 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005279 mtd->ecclayout = ecc->layout;
5280 mtd->ecc_strength = ecc->strength;
5281 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005282 /*
5283 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5284 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5285 * properly set.
5286 */
5287 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005288 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005289
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005290 return 0;
William Juul52c07962007-10-31 13:53:06 +01005291}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005292EXPORT_SYMBOL(nand_scan_tail);
5293
William Juul52c07962007-10-31 13:53:06 +01005294/**
5295 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005296 * @mtd: MTD device structure
5297 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005298 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005299 * This fills out all the uninitialized function pointers with the defaults.
5300 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005301 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005302 */
5303int nand_scan(struct mtd_info *mtd, int maxchips)
5304{
5305 int ret;
5306
Lei Wen75bde942011-01-06 09:48:18 +08005307 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005308 if (!ret)
5309 ret = nand_scan_tail(mtd);
5310 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005311}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005312EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005313
Heiko Schocherf5895d12014-06-24 10:10:04 +02005314MODULE_LICENSE("GPL");
5315MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5316MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5317MODULE_DESCRIPTION("Generic NAND flash driver code");