blob: 698968bc11636e2dafadada2078839a59af8e129 [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
Simon Glass0f2af882020-05-10 11:40:05 -060035#include <log.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020036#include <malloc.h>
37#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070038#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060039#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060040#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060041#include <linux/delay.h>
William Juul52c07962007-10-31 13:53:06 +010042#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000043#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020044#include <linux/mtd/mtd.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090045#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020046#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020047#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020048#ifdef CONFIG_MTD_PARTITIONS
49#include <linux/mtd/partitions.h>
50#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090052#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020053
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020054/* Define default oob placement schemes for large and small page devices */
Gregory CLEMENTe5b96312019-04-17 11:22:05 +020055#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
William Juul52c07962007-10-31 13:53:06 +010056static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020057 .eccbytes = 3,
58 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010059 .oobfree = {
60 {.offset = 3,
61 .length = 2},
62 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020063 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020064};
65
William Juul52c07962007-10-31 13:53:06 +010066static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020067 .eccbytes = 6,
68 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010069 .oobfree = {
70 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020071 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020072};
73
William Juul52c07962007-10-31 13:53:06 +010074static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020075 .eccbytes = 24,
76 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010077 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
80 .oobfree = {
81 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020082 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020083};
84
William Juul52c07962007-10-31 13:53:06 +010085static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020086 .eccbytes = 48,
87 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020088 80, 81, 82, 83, 84, 85, 86, 87,
89 88, 89, 90, 91, 92, 93, 94, 95,
90 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010091 104, 105, 106, 107, 108, 109, 110, 111,
92 112, 113, 114, 115, 116, 117, 118, 119,
93 120, 121, 122, 123, 124, 125, 126, 127},
94 .oobfree = {
95 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020096 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020097};
Stefan Agnerbd186142018-12-06 14:57:09 +010098#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020099
Heiko Schocherf5895d12014-06-24 10:10:04 +0200100static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +0100101
102static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
103 struct mtd_oob_ops *ops);
104
Heiko Schocherf5895d12014-06-24 10:10:04 +0200105/*
106 * For devices which display every fart in the system on a separate LED. Is
107 * compiled away when LED support is disabled.
108 */
109DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200110
Christian Hitzb8a6b372011-10-12 09:32:02 +0200111static int check_offs_len(struct mtd_info *mtd,
112 loff_t ofs, uint64_t len)
113{
Scott Wood17fed142016-05-30 13:57:56 -0500114 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200115 int ret = 0;
116
117 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200118 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
119 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200120 ret = -EINVAL;
121 }
122
123 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200124 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
125 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200126 ret = -EINVAL;
127 }
128
Christian Hitzb8a6b372011-10-12 09:32:02 +0200129 return ret;
130}
131
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200132/**
133 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000134 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200135 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200136 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200137 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200138static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100139{
Scott Wood17fed142016-05-30 13:57:56 -0500140 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200141
142 /* De-select the NAND device */
143 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100144}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200145
146/**
147 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000148 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200150 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200151 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000152uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200153{
Scott Wood17fed142016-05-30 13:57:56 -0500154 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100155 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200156}
157
158/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200159 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000160 * @mtd: MTD device structure
161 *
162 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200163 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200164 */
William Juul52c07962007-10-31 13:53:06 +0100165static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200166{
Scott Wood17fed142016-05-30 13:57:56 -0500167 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100168 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200169}
170
171/**
172 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000173 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200174 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000175 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200176 */
177static u16 nand_read_word(struct mtd_info *mtd)
178{
Scott Wood17fed142016-05-30 13:57:56 -0500179 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100180 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200181}
182
183/**
184 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000185 * @mtd: MTD device structure
186 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200187 *
188 * Default select function for 1 chip devices.
189 */
William Juul52c07962007-10-31 13:53:06 +0100190static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200191{
Scott Wood17fed142016-05-30 13:57:56 -0500192 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100193
194 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200195 case -1:
William Juul52c07962007-10-31 13:53:06 +0100196 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200197 break;
198 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200199 break;
200
201 default:
202 BUG();
203 }
204}
205
206/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200207 * nand_write_byte - [DEFAULT] write single byte to chip
208 * @mtd: MTD device structure
209 * @byte: value to write
210 *
211 * Default function to write a byte to I/O[7:0]
212 */
213static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
214{
Scott Wood17fed142016-05-30 13:57:56 -0500215 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200216
217 chip->write_buf(mtd, &byte, 1);
218}
219
220/**
221 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
222 * @mtd: MTD device structure
223 * @byte: value to write
224 *
225 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
226 */
227static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
228{
Scott Wood17fed142016-05-30 13:57:56 -0500229 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200230 uint16_t word = byte;
231
232 /*
233 * It's not entirely clear what should happen to I/O[15:8] when writing
234 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
235 *
236 * When the host supports a 16-bit bus width, only data is
237 * transferred at the 16-bit width. All address and command line
238 * transfers shall use only the lower 8-bits of the data bus. During
239 * command transfers, the host may place any value on the upper
240 * 8-bits of the data bus. During address transfers, the host shall
241 * set the upper 8-bits of the data bus to 00h.
242 *
243 * One user of the write_byte callback is nand_onfi_set_features. The
244 * four parameters are specified to be written to I/O[7:0], but this is
245 * neither an address nor a command transfer. Let's assume a 0 on the
246 * upper I/O lines is OK.
247 */
248 chip->write_buf(mtd, (uint8_t *)&word, 2);
249}
250
Heiko Schocherf5895d12014-06-24 10:10:04 +0200251static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
252{
253 int i;
254
255 for (i = 0; i < len; i++)
256 writeb(buf[i], addr);
257}
258static void ioread8_rep(void *addr, uint8_t *buf, int len)
259{
260 int i;
261
262 for (i = 0; i < len; i++)
263 buf[i] = readb(addr);
264}
265
266static void ioread16_rep(void *addr, void *buf, int len)
267{
268 int i;
269 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200270
Heiko Schocherf5895d12014-06-24 10:10:04 +0200271 for (i = 0; i < len; i++)
272 p[i] = readw(addr);
273}
274
275static void iowrite16_rep(void *addr, void *buf, int len)
276{
277 int i;
278 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200279
280 for (i = 0; i < len; i++)
281 writew(p[i], addr);
282}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200283
284/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200285 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000286 * @mtd: MTD device structure
287 * @buf: data buffer
288 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200289 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000290 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200291 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000292void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200293{
Scott Wood17fed142016-05-30 13:57:56 -0500294 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200295
Heiko Schocherf5895d12014-06-24 10:10:04 +0200296 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200297}
298
299/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200300 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000301 * @mtd: MTD device structure
302 * @buf: buffer to store date
303 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200304 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000305 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200306 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400307void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200308{
Scott Wood17fed142016-05-30 13:57:56 -0500309 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200310
Heiko Schocherf5895d12014-06-24 10:10:04 +0200311 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200312}
313
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200314/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200315 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000316 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200317 * @buf: data buffer
318 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200319 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200320 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200321 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200322void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200323{
Scott Wood17fed142016-05-30 13:57:56 -0500324 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200325 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200326
Heiko Schocherf5895d12014-06-24 10:10:04 +0200327 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200328}
329
330/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200331 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000332 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200333 * @buf: buffer to store date
334 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200335 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200336 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200337 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200338void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200339{
Scott Wood17fed142016-05-30 13:57:56 -0500340 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200341 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200342
Heiko Schocherf5895d12014-06-24 10:10:04 +0200343 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200344}
345
346/**
347 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000348 * @mtd: MTD device structure
349 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200350 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200351 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200352 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500353static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200354{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500355 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500356 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200357 u16 bad;
358
Sergey Lapin3a38a552013-01-14 03:46:50 +0000359 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200360 ofs += mtd->erasesize - mtd->writesize;
361
William Juul52c07962007-10-31 13:53:06 +0100362 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200363
Sergey Lapin3a38a552013-01-14 03:46:50 +0000364 do {
365 if (chip->options & NAND_BUSWIDTH_16) {
366 chip->cmdfunc(mtd, NAND_CMD_READOOB,
367 chip->badblockpos & 0xFE, page);
368 bad = cpu_to_le16(chip->read_word(mtd));
369 if (chip->badblockpos & 0x1)
370 bad >>= 8;
371 else
372 bad &= 0xFF;
373 } else {
374 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
375 page);
376 bad = chip->read_byte(mtd);
377 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200378
Sergey Lapin3a38a552013-01-14 03:46:50 +0000379 if (likely(chip->badblockbits == 8))
380 res = bad != 0xFF;
381 else
382 res = hweight8(bad) < chip->badblockbits;
383 ofs += mtd->writesize;
384 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
385 i++;
386 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200387
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200388 return res;
389}
390
391/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200392 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000393 * @mtd: MTD device structure
394 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200395 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000396 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200397 * specific driver. It provides the details for writing a bad block marker to a
398 * block.
399 */
400static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
401{
Scott Wood17fed142016-05-30 13:57:56 -0500402 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200403 struct mtd_oob_ops ops;
404 uint8_t buf[2] = { 0, 0 };
405 int ret = 0, res, i = 0;
406
Scott Wood3ea94ed2015-06-26 19:03:26 -0500407 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200408 ops.oobbuf = buf;
409 ops.ooboffs = chip->badblockpos;
410 if (chip->options & NAND_BUSWIDTH_16) {
411 ops.ooboffs &= ~0x01;
412 ops.len = ops.ooblen = 2;
413 } else {
414 ops.len = ops.ooblen = 1;
415 }
416 ops.mode = MTD_OPS_PLACE_OOB;
417
418 /* Write to first/last page(s) if necessary */
419 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
420 ofs += mtd->erasesize - mtd->writesize;
421 do {
422 res = nand_do_write_oob(mtd, ofs, &ops);
423 if (!ret)
424 ret = res;
425
426 i++;
427 ofs += mtd->writesize;
428 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
429
430 return ret;
431}
432
433/**
434 * nand_block_markbad_lowlevel - mark a block bad
435 * @mtd: MTD device structure
436 * @ofs: offset from device start
437 *
438 * This function performs the generic NAND bad block marking steps (i.e., bad
439 * block table(s) and/or marker(s)). We only allow the hardware driver to
440 * specify how to write bad block markers to OOB (chip->block_markbad).
441 *
442 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000443 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200444 * (2) write bad block marker to OOB area of affected block (unless flag
445 * NAND_BBT_NO_OOB_BBM is present)
446 * (3) update the BBT
447 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000448 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200449*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200450static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200451{
Scott Wood17fed142016-05-30 13:57:56 -0500452 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200453 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200454
Heiko Schocherf5895d12014-06-24 10:10:04 +0200455 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000456 struct erase_info einfo;
457
458 /* Attempt erase before marking OOB */
459 memset(&einfo, 0, sizeof(einfo));
460 einfo.mtd = mtd;
461 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200462 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000463 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200464
Heiko Schocherf5895d12014-06-24 10:10:04 +0200465 /* Write bad block marker to OOB */
466 nand_get_device(mtd, FL_WRITING);
467 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500468 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100469 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000470
Heiko Schocherf5895d12014-06-24 10:10:04 +0200471 /* Mark block bad in BBT */
472 if (chip->bbt) {
473 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000474 if (!ret)
475 ret = res;
476 }
477
William Juul52c07962007-10-31 13:53:06 +0100478 if (!ret)
479 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500480
William Juul52c07962007-10-31 13:53:06 +0100481 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200482}
483
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200484/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200485 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000486 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200487 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000488 * Check, if the device is write protected. The function expects, that the
489 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200490 */
William Juul52c07962007-10-31 13:53:06 +0100491static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200492{
Scott Wood17fed142016-05-30 13:57:56 -0500493 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100494 u8 status;
495 int ret;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200496
Sergey Lapin3a38a552013-01-14 03:46:50 +0000497 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200498 if (chip->options & NAND_BROKEN_XD)
499 return 0;
500
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200501 /* Check the WP bit */
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100502 ret = nand_status_op(chip, &status);
503 if (ret)
504 return ret;
505
506 return status & NAND_STATUS_WP ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200507}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100508
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200509/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500510 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000511 * @mtd: MTD device structure
512 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300513 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500514 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300515 */
516static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
517{
Scott Wood17fed142016-05-30 13:57:56 -0500518 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300519
520 if (!chip->bbt)
521 return 0;
522 /* Return info from the table */
523 return nand_isreserved_bbt(mtd, ofs);
524}
525
526/**
527 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
528 * @mtd: MTD device structure
529 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000530 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200531 *
532 * Check, if the block is bad. Either by reading the bad block table or
533 * calling of the scan function.
534 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500535static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200536{
Scott Wood17fed142016-05-30 13:57:56 -0500537 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200538
Masahiro Yamada8d100542014-12-26 22:20:58 +0900539 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
540 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200541 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900542 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200543 }
544
William Juul52c07962007-10-31 13:53:06 +0100545 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500546 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200547
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200548 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100549 return nand_isbad_bbt(mtd, ofs, allowbbt);
550}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200551
Scott Wood52ab7ce2016-05-30 13:57:58 -0500552/**
553 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
554 * @mtd: MTD device structure
555 *
556 * Wait for the ready pin after a command, and warn if a timeout occurs.
557 */
William Juul52c07962007-10-31 13:53:06 +0100558void nand_wait_ready(struct mtd_info *mtd)
559{
Scott Wood17fed142016-05-30 13:57:56 -0500560 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500561 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000562 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100563
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000564 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000565 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000566 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100567 if (chip->dev_ready)
568 if (chip->dev_ready(mtd))
569 break;
570 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500571
572 if (!chip->dev_ready(mtd))
573 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200574}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200575EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200576
577/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500578 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
579 * @mtd: MTD device structure
580 * @timeo: Timeout in ms
581 *
582 * Wait for status ready (i.e. command done) or timeout.
583 */
584static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
585{
Scott Wood17fed142016-05-30 13:57:56 -0500586 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500587 u32 time_start;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100588 int ret;
Scott Wood3ea94ed2015-06-26 19:03:26 -0500589
590 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
591 time_start = get_timer(0);
592 while (get_timer(time_start) < timeo) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100593 u8 status;
594
595 ret = nand_read_data_op(chip, &status, sizeof(status), true);
596 if (ret)
597 return;
598
599 if (status & NAND_STATUS_READY)
Scott Wood3ea94ed2015-06-26 19:03:26 -0500600 break;
601 WATCHDOG_RESET();
602 }
603};
604
605/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200606 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000607 * @mtd: MTD device structure
608 * @command: the command to be sent
609 * @column: the column address for this command, -1 if none
610 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000612 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200613 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200614 */
William Juul52c07962007-10-31 13:53:06 +0100615static void nand_command(struct mtd_info *mtd, unsigned int command,
616 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617{
Scott Wood17fed142016-05-30 13:57:56 -0500618 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100619 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200620
Sergey Lapin3a38a552013-01-14 03:46:50 +0000621 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200622 if (command == NAND_CMD_SEQIN) {
623 int readcmd;
624
William Juul52c07962007-10-31 13:53:06 +0100625 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200626 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100627 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200628 readcmd = NAND_CMD_READOOB;
629 } else if (column < 256) {
630 /* First 256 bytes --> READ0 */
631 readcmd = NAND_CMD_READ0;
632 } else {
633 column -= 256;
634 readcmd = NAND_CMD_READ1;
635 }
William Juul52c07962007-10-31 13:53:06 +0100636 chip->cmd_ctrl(mtd, readcmd, ctrl);
637 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200638 }
William Juul52c07962007-10-31 13:53:06 +0100639 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200640
Sergey Lapin3a38a552013-01-14 03:46:50 +0000641 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100642 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
643 /* Serially input address */
644 if (column != -1) {
645 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200646 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530647 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100648 column >>= 1;
649 chip->cmd_ctrl(mtd, column, ctrl);
650 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200651 }
William Juul52c07962007-10-31 13:53:06 +0100652 if (page_addr != -1) {
653 chip->cmd_ctrl(mtd, page_addr, ctrl);
654 ctrl &= ~NAND_CTRL_CHANGE;
655 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900656 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100657 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
658 }
659 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200660
661 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000662 * Program and erase have their own busy handlers status and sequential
663 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100664 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200665 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200666
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200667 case NAND_CMD_PAGEPROG:
668 case NAND_CMD_ERASE1:
669 case NAND_CMD_ERASE2:
670 case NAND_CMD_SEQIN:
671 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900672 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900673 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200674 return;
675
676 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100677 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200678 break;
William Juul52c07962007-10-31 13:53:06 +0100679 udelay(chip->chip_delay);
680 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
681 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
682 chip->cmd_ctrl(mtd,
683 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500684 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
685 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686 return;
687
William Juul52c07962007-10-31 13:53:06 +0100688 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200689 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200690 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200691 * If we don't have access to the busy pin, we apply the given
692 * command delay
William Juul52c07962007-10-31 13:53:06 +0100693 */
694 if (!chip->dev_ready) {
695 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200696 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200697 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200698 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000699 /*
700 * Apply this short delay always to ensure that we do wait tWB in
701 * any case on any machine.
702 */
William Juul52c07962007-10-31 13:53:06 +0100703 ndelay(100);
704
705 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200706}
707
708/**
709 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000710 * @mtd: MTD device structure
711 * @command: the command to be sent
712 * @column: the column address for this command, -1 if none
713 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200714 *
William Juul52c07962007-10-31 13:53:06 +0100715 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000716 * devices. We don't have the separate regions as we have in the small page
717 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200718 */
William Juul52c07962007-10-31 13:53:06 +0100719static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
720 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200721{
Scott Wood17fed142016-05-30 13:57:56 -0500722 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200723
724 /* Emulate NAND_CMD_READOOB */
725 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100726 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200727 command = NAND_CMD_READ0;
728 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200729
William Juul52c07962007-10-31 13:53:06 +0100730 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200731 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732
733 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100734 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200735
736 /* Serially input address */
737 if (column != -1) {
738 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200739 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530740 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200741 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100742 chip->cmd_ctrl(mtd, column, ctrl);
743 ctrl &= ~NAND_CTRL_CHANGE;
744 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200745 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200746 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100747 chip->cmd_ctrl(mtd, page_addr, ctrl);
748 chip->cmd_ctrl(mtd, page_addr >> 8,
749 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900750 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100751 chip->cmd_ctrl(mtd, page_addr >> 16,
752 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200753 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200754 }
William Juul52c07962007-10-31 13:53:06 +0100755 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200756
757 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000758 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500759 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100760 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200761 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200762
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200763 case NAND_CMD_CACHEDPROG:
764 case NAND_CMD_PAGEPROG:
765 case NAND_CMD_ERASE1:
766 case NAND_CMD_ERASE2:
767 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100768 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200769 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900770 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900771 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200772 return;
773
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200774 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100775 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200776 break;
William Juul52c07962007-10-31 13:53:06 +0100777 udelay(chip->chip_delay);
778 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
779 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
780 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
781 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500782 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
783 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100784 return;
785
786 case NAND_CMD_RNDOUT:
787 /* No ready / busy check necessary */
788 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
789 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
790 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
791 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200792 return;
793
794 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100795 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
796 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
797 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
798 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200799
William Juul52c07962007-10-31 13:53:06 +0100800 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200801 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200802 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200803 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000804 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100805 */
806 if (!chip->dev_ready) {
807 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200808 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200809 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200810 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200811
Sergey Lapin3a38a552013-01-14 03:46:50 +0000812 /*
813 * Apply this short delay always to ensure that we do wait tWB in
814 * any case on any machine.
815 */
William Juul52c07962007-10-31 13:53:06 +0100816 ndelay(100);
817
818 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200819}
820
821/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200822 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000823 * @chip: the nand chip descriptor
824 * @mtd: MTD device structure
825 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200826 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200827 * Used when in panic, no locks are taken.
828 */
829static void panic_nand_get_device(struct nand_chip *chip,
830 struct mtd_info *mtd, int new_state)
831{
832 /* Hardware controller shared among independent devices */
833 chip->controller->active = chip;
834 chip->state = new_state;
835}
836
837/**
838 * nand_get_device - [GENERIC] Get chip for selected access
839 * @mtd: MTD device structure
840 * @new_state: the state which is requested
841 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200842 * Get the device and lock it for exclusive access
843 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200844static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200845nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100846{
Scott Wood17fed142016-05-30 13:57:56 -0500847 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200848 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100849 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200850}
851
852/**
853 * panic_nand_wait - [GENERIC] wait until the command is done
854 * @mtd: MTD device structure
855 * @chip: NAND chip structure
856 * @timeo: timeout
857 *
858 * Wait for command done. This is a helper function for nand_wait used when
859 * we are in interrupt context. May happen when in panic and trying to write
860 * an oops through mtdoops.
861 */
862static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
863 unsigned long timeo)
864{
865 int i;
866 for (i = 0; i < timeo; i++) {
867 if (chip->dev_ready) {
868 if (chip->dev_ready(mtd))
869 break;
870 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100871 int ret;
872 u8 status;
873
874 ret = nand_read_data_op(chip, &status, sizeof(status),
875 true);
876 if (ret)
877 return;
878
879 if (status & NAND_STATUS_READY)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200880 break;
881 }
882 mdelay(1);
883 }
William Juul52c07962007-10-31 13:53:06 +0100884}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200885
886/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000887 * nand_wait - [DEFAULT] wait until the command is done
888 * @mtd: MTD device structure
889 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200890 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500891 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100892 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200893static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200894{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500895 unsigned long timeo = 400;
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100896 u8 status;
897 int ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100898
Heiko Schocherf5895d12014-06-24 10:10:04 +0200899 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100900
Heiko Schocherf5895d12014-06-24 10:10:04 +0200901 /*
902 * Apply this short delay always to ensure that we do wait tWB in any
903 * case on any machine.
904 */
905 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100906
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100907 ret = nand_status_op(chip, NULL);
908 if (ret)
909 return ret;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100910
Heiko Schocherf5895d12014-06-24 10:10:04 +0200911 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
912 u32 time_start;
913
914 time_start = get_timer(0);
915 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200916 if (chip->dev_ready) {
917 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100918 break;
919 } else {
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100920 ret = nand_read_data_op(chip, &status,
921 sizeof(status), true);
922 if (ret)
923 return ret;
924
925 if (status & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100926 break;
927 }
928 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200929 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100930
Boris Brezillon16ee8f62019-03-15 15:14:32 +0100931 ret = nand_read_data_op(chip, &status, sizeof(status), true);
932 if (ret)
933 return ret;
934
Heiko Schocherf5895d12014-06-24 10:10:04 +0200935 /* This can happen if in case of timeout or buggy dev_ready */
936 WARN_ON(!(status & NAND_STATUS_READY));
937 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200938}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500939
Scott Wood52ab7ce2016-05-30 13:57:58 -0500940/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900941 * nand_reset_data_interface - Reset data interface and timings
942 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900943 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900944 *
945 * Reset the Data interface and timings to ONFI mode 0.
946 *
947 * Returns 0 for success or negative error code otherwise.
948 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900949static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900950{
951 struct mtd_info *mtd = nand_to_mtd(chip);
952 const struct nand_data_interface *conf;
953 int ret;
954
955 if (!chip->setup_data_interface)
956 return 0;
957
958 /*
959 * The ONFI specification says:
960 * "
961 * To transition from NV-DDR or NV-DDR2 to the SDR data
962 * interface, the host shall use the Reset (FFh) command
963 * using SDR timing mode 0. A device in any timing mode is
964 * required to recognize Reset (FFh) command issued in SDR
965 * timing mode 0.
966 * "
967 *
968 * Configure the data interface in SDR mode and set the
969 * timings to timing mode 0.
970 */
971
972 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900973 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900974 if (ret)
975 pr_err("Failed to configure data interface to SDR timing mode 0\n");
976
977 return ret;
978}
979
980/**
981 * nand_setup_data_interface - Setup the best data interface and timings
982 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900983 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900984 *
985 * Find and configure the best data interface and NAND timings supported by
986 * the chip and the driver.
987 * First tries to retrieve supported timing modes from ONFI information,
988 * and if the NAND chip does not support ONFI, relies on the
989 * ->onfi_timing_mode_default specified in the nand_ids table.
990 *
991 * Returns 0 for success or negative error code otherwise.
992 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900993static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900994{
995 struct mtd_info *mtd = nand_to_mtd(chip);
996 int ret;
997
998 if (!chip->setup_data_interface || !chip->data_interface)
999 return 0;
1000
1001 /*
1002 * Ensure the timing mode has been changed on the chip side
1003 * before changing timings on the controller side.
1004 */
1005 if (chip->onfi_version) {
1006 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1007 chip->onfi_timing_mode_default,
1008 };
1009
1010 ret = chip->onfi_set_features(mtd, chip,
1011 ONFI_FEATURE_ADDR_TIMING_MODE,
1012 tmode_param);
1013 if (ret)
1014 goto err;
1015 }
1016
Boris Brezillon32935f42017-11-22 02:38:28 +09001017 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001018err:
1019 return ret;
1020}
1021
1022/**
1023 * nand_init_data_interface - find the best data interface and timings
1024 * @chip: The NAND chip
1025 *
1026 * Find the best data interface and NAND timings supported by the chip
1027 * and the driver.
1028 * First tries to retrieve supported timing modes from ONFI information,
1029 * and if the NAND chip does not support ONFI, relies on the
1030 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1031 * function nand_chip->data_interface is initialized with the best timing mode
1032 * available.
1033 *
1034 * Returns 0 for success or negative error code otherwise.
1035 */
1036static int nand_init_data_interface(struct nand_chip *chip)
1037{
1038 struct mtd_info *mtd = nand_to_mtd(chip);
1039 int modes, mode, ret;
1040
1041 if (!chip->setup_data_interface)
1042 return 0;
1043
1044 /*
1045 * First try to identify the best timings from ONFI parameters and
1046 * if the NAND does not support ONFI, fallback to the default ONFI
1047 * timing mode.
1048 */
1049 modes = onfi_get_async_timing_mode(chip);
1050 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1051 if (!chip->onfi_timing_mode_default)
1052 return 0;
1053
1054 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1055 }
1056
1057 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1058 GFP_KERNEL);
1059 if (!chip->data_interface)
1060 return -ENOMEM;
1061
1062 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1063 ret = onfi_init_data_interface(chip, chip->data_interface,
1064 NAND_SDR_IFACE, mode);
1065 if (ret)
1066 continue;
1067
Boris Brezillon32935f42017-11-22 02:38:28 +09001068 /* Pass -1 to only */
1069 ret = chip->setup_data_interface(mtd,
1070 NAND_DATA_IFACE_CHECK_ONLY,
1071 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001072 if (!ret) {
1073 chip->onfi_timing_mode_default = mode;
1074 break;
1075 }
1076 }
1077
1078 return 0;
1079}
1080
1081static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1082{
1083 kfree(chip->data_interface);
1084}
1085
1086/**
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001087 * nand_read_page_op - Do a READ PAGE operation
1088 * @chip: The NAND chip
1089 * @page: page to read
1090 * @offset_in_page: offset within the page
1091 * @buf: buffer used to store the data
1092 * @len: length of the buffer
1093 *
1094 * This function issues a READ PAGE operation.
1095 * This function does not select/unselect the CS line.
1096 *
1097 * Returns 0 on success, a negative error code otherwise.
1098 */
1099int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1100 unsigned int offset_in_page, void *buf, unsigned int len)
1101{
1102 struct mtd_info *mtd = nand_to_mtd(chip);
1103
1104 if (len && !buf)
1105 return -EINVAL;
1106
1107 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1108 return -EINVAL;
1109
1110 chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page);
1111 if (len)
1112 chip->read_buf(mtd, buf, len);
1113
1114 return 0;
1115}
1116EXPORT_SYMBOL_GPL(nand_read_page_op);
1117
1118/**
1119 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1120 * @chip: The NAND chip
1121 * @page: parameter page to read
1122 * @buf: buffer used to store the data
1123 * @len: length of the buffer
1124 *
1125 * This function issues a READ PARAMETER PAGE operation.
1126 * This function does not select/unselect the CS line.
1127 *
1128 * Returns 0 on success, a negative error code otherwise.
1129 */
1130static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1131 unsigned int len)
1132{
1133 struct mtd_info *mtd = nand_to_mtd(chip);
1134 unsigned int i;
1135 u8 *p = buf;
1136
1137 if (len && !buf)
1138 return -EINVAL;
1139
1140 chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1);
1141 for (i = 0; i < len; i++)
1142 p[i] = chip->read_byte(mtd);
1143
1144 return 0;
1145}
1146
1147/**
1148 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1149 * @chip: The NAND chip
1150 * @offset_in_page: offset within the page
1151 * @buf: buffer used to store the data
1152 * @len: length of the buffer
1153 * @force_8bit: force 8-bit bus access
1154 *
1155 * This function issues a CHANGE READ COLUMN operation.
1156 * This function does not select/unselect the CS line.
1157 *
1158 * Returns 0 on success, a negative error code otherwise.
1159 */
1160int nand_change_read_column_op(struct nand_chip *chip,
1161 unsigned int offset_in_page, void *buf,
1162 unsigned int len, bool force_8bit)
1163{
1164 struct mtd_info *mtd = nand_to_mtd(chip);
1165
1166 if (len && !buf)
1167 return -EINVAL;
1168
1169 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1170 return -EINVAL;
1171
1172 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1);
1173 if (len)
1174 chip->read_buf(mtd, buf, len);
1175
1176 return 0;
1177}
1178EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1179
1180/**
1181 * nand_read_oob_op - Do a READ OOB operation
1182 * @chip: The NAND chip
1183 * @page: page to read
1184 * @offset_in_oob: offset within the OOB area
1185 * @buf: buffer used to store the data
1186 * @len: length of the buffer
1187 *
1188 * This function issues a READ OOB operation.
1189 * This function does not select/unselect the CS line.
1190 *
1191 * Returns 0 on success, a negative error code otherwise.
1192 */
1193int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1194 unsigned int offset_in_oob, void *buf, unsigned int len)
1195{
1196 struct mtd_info *mtd = nand_to_mtd(chip);
1197
1198 if (len && !buf)
1199 return -EINVAL;
1200
1201 if (offset_in_oob + len > mtd->oobsize)
1202 return -EINVAL;
1203
1204 chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page);
1205 if (len)
1206 chip->read_buf(mtd, buf, len);
1207
1208 return 0;
1209}
1210EXPORT_SYMBOL_GPL(nand_read_oob_op);
1211
1212/**
1213 * nand_prog_page_begin_op - starts a PROG PAGE operation
1214 * @chip: The NAND chip
1215 * @page: page to write
1216 * @offset_in_page: offset within the page
1217 * @buf: buffer containing the data to write to the page
1218 * @len: length of the buffer
1219 *
1220 * This function issues the first half of a PROG PAGE operation.
1221 * This function does not select/unselect the CS line.
1222 *
1223 * Returns 0 on success, a negative error code otherwise.
1224 */
1225int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1226 unsigned int offset_in_page, const void *buf,
1227 unsigned int len)
1228{
1229 struct mtd_info *mtd = nand_to_mtd(chip);
1230
1231 if (len && !buf)
1232 return -EINVAL;
1233
1234 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1235 return -EINVAL;
1236
1237 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1238
1239 if (buf)
1240 chip->write_buf(mtd, buf, len);
1241
1242 return 0;
1243}
1244EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1245
1246/**
1247 * nand_prog_page_end_op - ends a PROG PAGE operation
1248 * @chip: The NAND chip
1249 *
1250 * This function issues the second half of a PROG PAGE operation.
1251 * This function does not select/unselect the CS line.
1252 *
1253 * Returns 0 on success, a negative error code otherwise.
1254 */
1255int nand_prog_page_end_op(struct nand_chip *chip)
1256{
1257 struct mtd_info *mtd = nand_to_mtd(chip);
1258 int status;
1259
1260 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1261
1262 status = chip->waitfunc(mtd, chip);
1263 if (status & NAND_STATUS_FAIL)
1264 return -EIO;
1265
1266 return 0;
1267}
1268EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1269
1270/**
1271 * nand_prog_page_op - Do a full PROG PAGE operation
1272 * @chip: The NAND chip
1273 * @page: page to write
1274 * @offset_in_page: offset within the page
1275 * @buf: buffer containing the data to write to the page
1276 * @len: length of the buffer
1277 *
1278 * This function issues a full PROG PAGE operation.
1279 * This function does not select/unselect the CS line.
1280 *
1281 * Returns 0 on success, a negative error code otherwise.
1282 */
1283int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1284 unsigned int offset_in_page, const void *buf,
1285 unsigned int len)
1286{
1287 struct mtd_info *mtd = nand_to_mtd(chip);
1288 int status;
1289
1290 if (!len || !buf)
1291 return -EINVAL;
1292
1293 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1294 return -EINVAL;
1295
1296 chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page);
1297 chip->write_buf(mtd, buf, len);
1298 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1299
1300 status = chip->waitfunc(mtd, chip);
1301 if (status & NAND_STATUS_FAIL)
1302 return -EIO;
1303
1304 return 0;
1305}
1306EXPORT_SYMBOL_GPL(nand_prog_page_op);
1307
1308/**
1309 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1310 * @chip: The NAND chip
1311 * @offset_in_page: offset within the page
1312 * @buf: buffer containing the data to send to the NAND
1313 * @len: length of the buffer
1314 * @force_8bit: force 8-bit bus access
1315 *
1316 * This function issues a CHANGE WRITE COLUMN operation.
1317 * This function does not select/unselect the CS line.
1318 *
1319 * Returns 0 on success, a negative error code otherwise.
1320 */
1321int nand_change_write_column_op(struct nand_chip *chip,
1322 unsigned int offset_in_page,
1323 const void *buf, unsigned int len,
1324 bool force_8bit)
1325{
1326 struct mtd_info *mtd = nand_to_mtd(chip);
1327
1328 if (len && !buf)
1329 return -EINVAL;
1330
1331 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1332 return -EINVAL;
1333
1334 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1);
1335 if (len)
1336 chip->write_buf(mtd, buf, len);
1337
1338 return 0;
1339}
1340EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1341
1342/**
1343 * nand_readid_op - Do a READID operation
1344 * @chip: The NAND chip
1345 * @addr: address cycle to pass after the READID command
1346 * @buf: buffer used to store the ID
1347 * @len: length of the buffer
1348 *
1349 * This function sends a READID command and reads back the ID returned by the
1350 * NAND.
1351 * This function does not select/unselect the CS line.
1352 *
1353 * Returns 0 on success, a negative error code otherwise.
1354 */
1355int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1356 unsigned int len)
1357{
1358 struct mtd_info *mtd = nand_to_mtd(chip);
1359 unsigned int i;
1360 u8 *id = buf;
1361
1362 if (len && !buf)
1363 return -EINVAL;
1364
1365 chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1);
1366
1367 for (i = 0; i < len; i++)
1368 id[i] = chip->read_byte(mtd);
1369
1370 return 0;
1371}
1372EXPORT_SYMBOL_GPL(nand_readid_op);
1373
1374/**
1375 * nand_status_op - Do a STATUS operation
1376 * @chip: The NAND chip
1377 * @status: out variable to store the NAND status
1378 *
1379 * This function sends a STATUS command and reads back the status returned by
1380 * the NAND.
1381 * This function does not select/unselect the CS line.
1382 *
1383 * Returns 0 on success, a negative error code otherwise.
1384 */
1385int nand_status_op(struct nand_chip *chip, u8 *status)
1386{
1387 struct mtd_info *mtd = nand_to_mtd(chip);
1388
1389 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1390 if (status)
1391 *status = chip->read_byte(mtd);
1392
1393 return 0;
1394}
1395EXPORT_SYMBOL_GPL(nand_status_op);
1396
1397/**
1398 * nand_exit_status_op - Exit a STATUS operation
1399 * @chip: The NAND chip
1400 *
1401 * This function sends a READ0 command to cancel the effect of the STATUS
1402 * command to avoid reading only the status until a new read command is sent.
1403 *
1404 * This function does not select/unselect the CS line.
1405 *
1406 * Returns 0 on success, a negative error code otherwise.
1407 */
1408int nand_exit_status_op(struct nand_chip *chip)
1409{
1410 struct mtd_info *mtd = nand_to_mtd(chip);
1411
1412 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
1413
1414 return 0;
1415}
1416EXPORT_SYMBOL_GPL(nand_exit_status_op);
1417
1418/**
1419 * nand_erase_op - Do an erase operation
1420 * @chip: The NAND chip
1421 * @eraseblock: block to erase
1422 *
1423 * This function sends an ERASE command and waits for the NAND to be ready
1424 * before returning.
1425 * This function does not select/unselect the CS line.
1426 *
1427 * Returns 0 on success, a negative error code otherwise.
1428 */
1429int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
1430{
1431 struct mtd_info *mtd = nand_to_mtd(chip);
1432 unsigned int page = eraseblock <<
1433 (chip->phys_erase_shift - chip->page_shift);
1434 int status;
1435
1436 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1437 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1438
1439 status = chip->waitfunc(mtd, chip);
1440 if (status < 0)
1441 return status;
1442
1443 if (status & NAND_STATUS_FAIL)
1444 return -EIO;
1445
1446 return 0;
1447}
1448EXPORT_SYMBOL_GPL(nand_erase_op);
1449
1450/**
1451 * nand_set_features_op - Do a SET FEATURES operation
1452 * @chip: The NAND chip
1453 * @feature: feature id
1454 * @data: 4 bytes of data
1455 *
1456 * This function sends a SET FEATURES command and waits for the NAND to be
1457 * ready before returning.
1458 * This function does not select/unselect the CS line.
1459 *
1460 * Returns 0 on success, a negative error code otherwise.
1461 */
1462static int nand_set_features_op(struct nand_chip *chip, u8 feature,
1463 const void *data)
1464{
1465 struct mtd_info *mtd = nand_to_mtd(chip);
1466 const u8 *params = data;
1467 int i, status;
1468
1469 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1);
1470 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1471 chip->write_byte(mtd, params[i]);
1472
1473 status = chip->waitfunc(mtd, chip);
1474 if (status & NAND_STATUS_FAIL)
1475 return -EIO;
1476
1477 return 0;
1478}
1479
1480/**
1481 * nand_get_features_op - Do a GET FEATURES operation
1482 * @chip: The NAND chip
1483 * @feature: feature id
1484 * @data: 4 bytes of data
1485 *
1486 * This function sends a GET FEATURES command and waits for the NAND to be
1487 * ready before returning.
1488 * This function does not select/unselect the CS line.
1489 *
1490 * Returns 0 on success, a negative error code otherwise.
1491 */
1492static int nand_get_features_op(struct nand_chip *chip, u8 feature,
1493 void *data)
1494{
1495 struct mtd_info *mtd = nand_to_mtd(chip);
1496 u8 *params = data;
1497 int i;
1498
1499 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1);
1500 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1501 params[i] = chip->read_byte(mtd);
1502
1503 return 0;
1504}
1505
1506/**
1507 * nand_reset_op - Do a reset operation
1508 * @chip: The NAND chip
1509 *
1510 * This function sends a RESET command and waits for the NAND to be ready
1511 * before returning.
1512 * This function does not select/unselect the CS line.
1513 *
1514 * Returns 0 on success, a negative error code otherwise.
1515 */
1516int nand_reset_op(struct nand_chip *chip)
1517{
1518 struct mtd_info *mtd = nand_to_mtd(chip);
1519
1520 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1521
1522 return 0;
1523}
1524EXPORT_SYMBOL_GPL(nand_reset_op);
1525
1526/**
1527 * nand_read_data_op - Read data from the NAND
1528 * @chip: The NAND chip
1529 * @buf: buffer used to store the data
1530 * @len: length of the buffer
1531 * @force_8bit: force 8-bit bus access
1532 *
1533 * This function does a raw data read on the bus. Usually used after launching
1534 * another NAND operation like nand_read_page_op().
1535 * This function does not select/unselect the CS line.
1536 *
1537 * Returns 0 on success, a negative error code otherwise.
1538 */
1539int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1540 bool force_8bit)
1541{
1542 struct mtd_info *mtd = nand_to_mtd(chip);
1543
1544 if (!len || !buf)
1545 return -EINVAL;
1546
1547 if (force_8bit) {
1548 u8 *p = buf;
1549 unsigned int i;
1550
1551 for (i = 0; i < len; i++)
1552 p[i] = chip->read_byte(mtd);
1553 } else {
1554 chip->read_buf(mtd, buf, len);
1555 }
1556
1557 return 0;
1558}
1559EXPORT_SYMBOL_GPL(nand_read_data_op);
1560
1561/**
1562 * nand_write_data_op - Write data from the NAND
1563 * @chip: The NAND chip
1564 * @buf: buffer containing the data to send on the bus
1565 * @len: length of the buffer
1566 * @force_8bit: force 8-bit bus access
1567 *
1568 * This function does a raw data write on the bus. Usually used after launching
1569 * another NAND operation like nand_write_page_begin_op().
1570 * This function does not select/unselect the CS line.
1571 *
1572 * Returns 0 on success, a negative error code otherwise.
1573 */
1574int nand_write_data_op(struct nand_chip *chip, const void *buf,
1575 unsigned int len, bool force_8bit)
1576{
1577 struct mtd_info *mtd = nand_to_mtd(chip);
1578
1579 if (!len || !buf)
1580 return -EINVAL;
1581
1582 if (force_8bit) {
1583 const u8 *p = buf;
1584 unsigned int i;
1585
1586 for (i = 0; i < len; i++)
1587 chip->write_byte(mtd, p[i]);
1588 } else {
1589 chip->write_buf(mtd, buf, len);
1590 }
1591
1592 return 0;
1593}
1594EXPORT_SYMBOL_GPL(nand_write_data_op);
1595
1596/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001597 * nand_reset - Reset and initialize a NAND device
1598 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001599 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001600 *
1601 * Returns 0 for success or negative error code otherwise
1602 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001603int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001604{
1605 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001606 int ret;
1607
Boris Brezillon32935f42017-11-22 02:38:28 +09001608 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001609 if (ret)
1610 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001611
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001612 /*
1613 * The CS line has to be released before we can apply the new NAND
1614 * interface settings, hence this weird ->select_chip() dance.
1615 */
1616 chip->select_chip(mtd, chipnr);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001617 ret = nand_reset_op(chip);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001618 chip->select_chip(mtd, -1);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001619 if (ret)
1620 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001621
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001622 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001623 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001624 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001625 if (ret)
1626 return ret;
1627
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001628 return 0;
1629}
1630
1631/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001632 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1633 * @buf: buffer to test
1634 * @len: buffer length
1635 * @bitflips_threshold: maximum number of bitflips
1636 *
1637 * Check if a buffer contains only 0xff, which means the underlying region
1638 * has been erased and is ready to be programmed.
1639 * The bitflips_threshold specify the maximum number of bitflips before
1640 * considering the region is not erased.
1641 * Note: The logic of this function has been extracted from the memweight
1642 * implementation, except that nand_check_erased_buf function exit before
1643 * testing the whole buffer if the number of bitflips exceed the
1644 * bitflips_threshold value.
1645 *
1646 * Returns a positive number of bitflips less than or equal to
1647 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1648 * threshold.
1649 */
1650static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1651{
1652 const unsigned char *bitmap = buf;
1653 int bitflips = 0;
1654 int weight;
1655
1656 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1657 len--, bitmap++) {
1658 weight = hweight8(*bitmap);
1659 bitflips += BITS_PER_BYTE - weight;
1660 if (unlikely(bitflips > bitflips_threshold))
1661 return -EBADMSG;
1662 }
1663
1664 for (; len >= 4; len -= 4, bitmap += 4) {
1665 weight = hweight32(*((u32 *)bitmap));
1666 bitflips += 32 - weight;
1667 if (unlikely(bitflips > bitflips_threshold))
1668 return -EBADMSG;
1669 }
1670
1671 for (; len > 0; len--, bitmap++) {
1672 weight = hweight8(*bitmap);
1673 bitflips += BITS_PER_BYTE - weight;
1674 if (unlikely(bitflips > bitflips_threshold))
1675 return -EBADMSG;
1676 }
1677
1678 return bitflips;
1679}
1680
1681/**
1682 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1683 * 0xff data
1684 * @data: data buffer to test
1685 * @datalen: data length
1686 * @ecc: ECC buffer
1687 * @ecclen: ECC length
1688 * @extraoob: extra OOB buffer
1689 * @extraooblen: extra OOB length
1690 * @bitflips_threshold: maximum number of bitflips
1691 *
1692 * Check if a data buffer and its associated ECC and OOB data contains only
1693 * 0xff pattern, which means the underlying region has been erased and is
1694 * ready to be programmed.
1695 * The bitflips_threshold specify the maximum number of bitflips before
1696 * considering the region as not erased.
1697 *
1698 * Note:
1699 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1700 * different from the NAND page size. When fixing bitflips, ECC engines will
1701 * report the number of errors per chunk, and the NAND core infrastructure
1702 * expect you to return the maximum number of bitflips for the whole page.
1703 * This is why you should always use this function on a single chunk and
1704 * not on the whole page. After checking each chunk you should update your
1705 * max_bitflips value accordingly.
1706 * 2/ When checking for bitflips in erased pages you should not only check
1707 * the payload data but also their associated ECC data, because a user might
1708 * have programmed almost all bits to 1 but a few. In this case, we
1709 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1710 * this case.
1711 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1712 * data are protected by the ECC engine.
1713 * It could also be used if you support subpages and want to attach some
1714 * extra OOB data to an ECC chunk.
1715 *
1716 * Returns a positive number of bitflips less than or equal to
1717 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1718 * threshold. In case of success, the passed buffers are filled with 0xff.
1719 */
1720int nand_check_erased_ecc_chunk(void *data, int datalen,
1721 void *ecc, int ecclen,
1722 void *extraoob, int extraooblen,
1723 int bitflips_threshold)
1724{
1725 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1726
1727 data_bitflips = nand_check_erased_buf(data, datalen,
1728 bitflips_threshold);
1729 if (data_bitflips < 0)
1730 return data_bitflips;
1731
1732 bitflips_threshold -= data_bitflips;
1733
1734 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1735 if (ecc_bitflips < 0)
1736 return ecc_bitflips;
1737
1738 bitflips_threshold -= ecc_bitflips;
1739
1740 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1741 bitflips_threshold);
1742 if (extraoob_bitflips < 0)
1743 return extraoob_bitflips;
1744
1745 if (data_bitflips)
1746 memset(data, 0xff, datalen);
1747
1748 if (ecc_bitflips)
1749 memset(ecc, 0xff, ecclen);
1750
1751 if (extraoob_bitflips)
1752 memset(extraoob, 0xff, extraooblen);
1753
1754 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1755}
1756EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001757
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001758/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001759 * nand_read_page_raw - [INTERN] read raw page data without ecc
1760 * @mtd: mtd info structure
1761 * @chip: nand chip info structure
1762 * @buf: buffer to store read data
1763 * @oob_required: caller requires OOB data read to chip->oob_poi
1764 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001765 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001766 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001767 */
William Juul52c07962007-10-31 13:53:06 +01001768static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001769 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001770{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001771 int ret;
1772
1773 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
1774 if (ret)
1775 return ret;
1776
1777 if (oob_required) {
1778 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
1779 false);
1780 if (ret)
1781 return ret;
1782 }
1783
William Juul52c07962007-10-31 13:53:06 +01001784 return 0;
1785}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001786
William Juul52c07962007-10-31 13:53:06 +01001787/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001788 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1789 * @mtd: mtd info structure
1790 * @chip: nand chip info structure
1791 * @buf: buffer to store read data
1792 * @oob_required: caller requires OOB data read to chip->oob_poi
1793 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001794 *
1795 * We need a special oob layout and handling even when OOB isn't used.
1796 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001797static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001798 struct nand_chip *chip, uint8_t *buf,
1799 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001800{
1801 int eccsize = chip->ecc.size;
1802 int eccbytes = chip->ecc.bytes;
1803 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001804 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05001805
1806 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001807 ret = nand_read_data_op(chip, buf, eccsize, false);
1808 if (ret)
1809 return ret;
1810
David Brownellee86b8d2009-11-07 16:27:01 -05001811 buf += eccsize;
1812
1813 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001814 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
1815 false);
1816 if (ret)
1817 return ret;
1818
David Brownellee86b8d2009-11-07 16:27:01 -05001819 oob += chip->ecc.prepad;
1820 }
1821
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001822 ret = nand_read_data_op(chip, oob, eccbytes, false);
1823 if (ret)
1824 return ret;
1825
David Brownellee86b8d2009-11-07 16:27:01 -05001826 oob += eccbytes;
1827
1828 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001829 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
1830 false);
1831 if (ret)
1832 return ret;
1833
David Brownellee86b8d2009-11-07 16:27:01 -05001834 oob += chip->ecc.postpad;
1835 }
1836 }
1837
1838 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001839 if (size) {
1840 ret = nand_read_data_op(chip, oob, size, false);
1841 if (ret)
1842 return ret;
1843 }
David Brownellee86b8d2009-11-07 16:27:01 -05001844
1845 return 0;
1846}
1847
1848/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001849 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1850 * @mtd: mtd info structure
1851 * @chip: nand chip info structure
1852 * @buf: buffer to store read data
1853 * @oob_required: caller requires OOB data read to chip->oob_poi
1854 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001855 */
1856static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001857 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001858{
1859 int i, eccsize = chip->ecc.size;
1860 int eccbytes = chip->ecc.bytes;
1861 int eccsteps = chip->ecc.steps;
1862 uint8_t *p = buf;
1863 uint8_t *ecc_calc = chip->buffers->ecccalc;
1864 uint8_t *ecc_code = chip->buffers->ecccode;
1865 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001866 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001867
Sergey Lapin3a38a552013-01-14 03:46:50 +00001868 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001869
William Juul52c07962007-10-31 13:53:06 +01001870 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1871 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001872
William Juul52c07962007-10-31 13:53:06 +01001873 for (i = 0; i < chip->ecc.total; i++)
1874 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001875
William Juul52c07962007-10-31 13:53:06 +01001876 eccsteps = chip->ecc.steps;
1877 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001878
William Juul52c07962007-10-31 13:53:06 +01001879 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1880 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001881
William Juul52c07962007-10-31 13:53:06 +01001882 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001883 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001884 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001885 } else {
William Juul52c07962007-10-31 13:53:06 +01001886 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001887 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1888 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001889 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001890 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001891}
1892
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001893/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001894 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001895 * @mtd: mtd info structure
1896 * @chip: nand chip info structure
1897 * @data_offs: offset of requested data within the page
1898 * @readlen: data length
1899 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001900 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001901 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001902static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001903 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1904 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001905{
1906 int start_step, end_step, num_steps;
1907 uint32_t *eccpos = chip->ecc.layout->eccpos;
1908 uint8_t *p;
1909 int data_col_addr, i, gaps = 0;
1910 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1911 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001912 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001913 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001914 int ret;
Scott Wood3628f002008-10-24 16:20:43 -05001915
Sergey Lapin3a38a552013-01-14 03:46:50 +00001916 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001917 start_step = data_offs / chip->ecc.size;
1918 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1919 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001920 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001921
Sergey Lapin3a38a552013-01-14 03:46:50 +00001922 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001923 datafrag_len = num_steps * chip->ecc.size;
1924 eccfrag_len = num_steps * chip->ecc.bytes;
1925
1926 data_col_addr = start_step * chip->ecc.size;
1927 /* If we read not a page aligned data */
1928 if (data_col_addr != 0)
1929 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1930
1931 p = bufpoi + data_col_addr;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001932 ret = nand_read_data_op(chip, p, datafrag_len, false);
1933 if (ret)
1934 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001935
Sergey Lapin3a38a552013-01-14 03:46:50 +00001936 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001937 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1938 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1939
Sergey Lapin3a38a552013-01-14 03:46:50 +00001940 /*
1941 * The performance is faster if we position offsets according to
1942 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1943 */
Scott Wood3628f002008-10-24 16:20:43 -05001944 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001945 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001946 gaps = 1;
1947 break;
1948 }
1949 }
1950 if (gaps) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001951 ret = nand_change_read_column_op(chip, mtd->writesize,
1952 chip->oob_poi, mtd->oobsize,
1953 false);
1954 if (ret)
1955 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001956 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001957 /*
1958 * Send the command to read the particular ECC bytes take care
1959 * about buswidth alignment in read_buf.
1960 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001961 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001962 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001963 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001964 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001965 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001966 aligned_len++;
1967
Boris Brezillon16ee8f62019-03-15 15:14:32 +01001968 ret = nand_change_read_column_op(chip,
1969 mtd->writesize + aligned_pos,
1970 &chip->oob_poi[aligned_pos],
1971 aligned_len, false);
1972 if (ret)
1973 return ret;
Scott Wood3628f002008-10-24 16:20:43 -05001974 }
1975
1976 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001977 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001978
1979 p = bufpoi + data_col_addr;
1980 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1981 int stat;
1982
Christian Hitzb8a6b372011-10-12 09:32:02 +02001983 stat = chip->ecc.correct(mtd, p,
1984 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001985 if (stat == -EBADMSG &&
1986 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1987 /* check for empty pages with bitflips */
1988 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1989 &chip->buffers->ecccode[i],
1990 chip->ecc.bytes,
1991 NULL, 0,
1992 chip->ecc.strength);
1993 }
1994
Heiko Schocherf5895d12014-06-24 10:10:04 +02001995 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001996 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001997 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001998 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001999 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2000 }
Scott Wood3628f002008-10-24 16:20:43 -05002001 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002002 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05002003}
2004
2005/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002006 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
2007 * @mtd: mtd info structure
2008 * @chip: nand chip info structure
2009 * @buf: buffer to store read data
2010 * @oob_required: caller requires OOB data read to chip->oob_poi
2011 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002012 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002013 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002014 */
William Juul52c07962007-10-31 13:53:06 +01002015static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002016 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002017{
William Juul52c07962007-10-31 13:53:06 +01002018 int i, eccsize = chip->ecc.size;
2019 int eccbytes = chip->ecc.bytes;
2020 int eccsteps = chip->ecc.steps;
2021 uint8_t *p = buf;
2022 uint8_t *ecc_calc = chip->buffers->ecccalc;
2023 uint8_t *ecc_code = chip->buffers->ecccode;
2024 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002025 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002026 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002027
William Juul52c07962007-10-31 13:53:06 +01002028 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2029 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002030
2031 ret = nand_read_data_op(chip, p, eccsize, false);
2032 if (ret)
2033 return ret;
2034
William Juul52c07962007-10-31 13:53:06 +01002035 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2036 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002037
2038 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2039 if (ret)
2040 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002041
William Juul52c07962007-10-31 13:53:06 +01002042 for (i = 0; i < chip->ecc.total; i++)
2043 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002044
William Juul52c07962007-10-31 13:53:06 +01002045 eccsteps = chip->ecc.steps;
2046 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002047
William Juul52c07962007-10-31 13:53:06 +01002048 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2049 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002050
William Juul52c07962007-10-31 13:53:06 +01002051 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002052 if (stat == -EBADMSG &&
2053 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2054 /* check for empty pages with bitflips */
2055 stat = nand_check_erased_ecc_chunk(p, eccsize,
2056 &ecc_code[i], eccbytes,
2057 NULL, 0,
2058 chip->ecc.strength);
2059 }
2060
Heiko Schocherf5895d12014-06-24 10:10:04 +02002061 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01002062 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002063 } else {
William Juul52c07962007-10-31 13:53:06 +01002064 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002065 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2066 }
William Juul52c07962007-10-31 13:53:06 +01002067 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002068 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002069}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002070
William Juul52c07962007-10-31 13:53:06 +01002071/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002072 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
2073 * @mtd: mtd info structure
2074 * @chip: nand chip info structure
2075 * @buf: buffer to store read data
2076 * @oob_required: caller requires OOB data read to chip->oob_poi
2077 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002078 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002079 * Hardware ECC for large page chips, require OOB to be read first. For this
2080 * ECC mode, the write_page method is re-used from ECC_HW. These methods
2081 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
2082 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
2083 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002084 */
2085static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002086 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002087{
2088 int i, eccsize = chip->ecc.size;
2089 int eccbytes = chip->ecc.bytes;
2090 int eccsteps = chip->ecc.steps;
2091 uint8_t *p = buf;
2092 uint8_t *ecc_code = chip->buffers->ecccode;
2093 uint32_t *eccpos = chip->ecc.layout->eccpos;
2094 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002095 unsigned int max_bitflips = 0;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002096 int ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002097
2098 /* Read the OOB area first */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002099 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
2100 if (ret)
2101 return ret;
2102
2103 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2104 if (ret)
2105 return ret;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002106
2107 for (i = 0; i < chip->ecc.total; i++)
2108 ecc_code[i] = chip->oob_poi[eccpos[i]];
2109
2110 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2111 int stat;
2112
2113 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002114
2115 ret = nand_read_data_op(chip, p, eccsize, false);
2116 if (ret)
2117 return ret;
2118
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002119 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2120
2121 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05002122 if (stat == -EBADMSG &&
2123 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2124 /* check for empty pages with bitflips */
2125 stat = nand_check_erased_ecc_chunk(p, eccsize,
2126 &ecc_code[i], eccbytes,
2127 NULL, 0,
2128 chip->ecc.strength);
2129 }
2130
Heiko Schocherf5895d12014-06-24 10:10:04 +02002131 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002132 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002133 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002134 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002135 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2136 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002137 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002138 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002139}
2140
2141/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002142 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
2143 * @mtd: mtd info structure
2144 * @chip: nand chip info structure
2145 * @buf: buffer to store read data
2146 * @oob_required: caller requires OOB data read to chip->oob_poi
2147 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002148 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002149 * The hw generator calculates the error syndrome automatically. Therefore we
2150 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01002151 */
2152static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002153 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01002154{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002155 int ret, i, eccsize = chip->ecc.size;
William Juul52c07962007-10-31 13:53:06 +01002156 int eccbytes = chip->ecc.bytes;
2157 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002158 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01002159 uint8_t *p = buf;
2160 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002161 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01002162
2163 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2164 int stat;
2165
2166 chip->ecc.hwctl(mtd, NAND_ECC_READ);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002167
2168 ret = nand_read_data_op(chip, p, eccsize, false);
2169 if (ret)
2170 return ret;
William Juul52c07962007-10-31 13:53:06 +01002171
2172 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002173 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
2174 false);
2175 if (ret)
2176 return ret;
2177
William Juul52c07962007-10-31 13:53:06 +01002178 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002179 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002180
William Juul52c07962007-10-31 13:53:06 +01002181 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002182
2183 ret = nand_read_data_op(chip, oob, eccbytes, false);
2184 if (ret)
2185 return ret;
2186
William Juul52c07962007-10-31 13:53:06 +01002187 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002188
William Juul52c07962007-10-31 13:53:06 +01002189 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002190
William Juul52c07962007-10-31 13:53:06 +01002191 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002192 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
2193 false);
2194 if (ret)
2195 return ret;
2196
William Juul52c07962007-10-31 13:53:06 +01002197 oob += chip->ecc.postpad;
2198 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05002199
2200 if (stat == -EBADMSG &&
2201 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
2202 /* check for empty pages with bitflips */
2203 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
2204 oob - eccpadbytes,
2205 eccpadbytes,
2206 NULL, 0,
2207 chip->ecc.strength);
2208 }
2209
2210 if (stat < 0) {
2211 mtd->ecc_stats.failed++;
2212 } else {
2213 mtd->ecc_stats.corrected += stat;
2214 max_bitflips = max_t(unsigned int, max_bitflips, stat);
2215 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002216 }
William Juul52c07962007-10-31 13:53:06 +01002217
2218 /* Calculate remaining oob bytes */
2219 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002220 if (i) {
2221 ret = nand_read_data_op(chip, oob, i, false);
2222 if (ret)
2223 return ret;
2224 }
William Juul52c07962007-10-31 13:53:06 +01002225
Heiko Schocherf5895d12014-06-24 10:10:04 +02002226 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002227}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002228
2229/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002230 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
2231 * @chip: nand chip structure
2232 * @oob: oob destination address
2233 * @ops: oob ops structure
2234 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01002235 */
2236static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
2237 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002238{
Christian Hitz13fc0e22011-10-12 09:32:01 +02002239 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002240
Sergey Lapin3a38a552013-01-14 03:46:50 +00002241 case MTD_OPS_PLACE_OOB:
2242 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002243 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
2244 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002245
Sergey Lapin3a38a552013-01-14 03:46:50 +00002246 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002247 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2248 uint32_t boffs = 0, roffs = ops->ooboffs;
2249 size_t bytes = 0;
2250
Christian Hitz13fc0e22011-10-12 09:32:01 +02002251 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002252 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002253 if (unlikely(roffs)) {
2254 if (roffs >= free->length) {
2255 roffs -= free->length;
2256 continue;
2257 }
2258 boffs = free->offset + roffs;
2259 bytes = min_t(size_t, len,
2260 (free->length - roffs));
2261 roffs = 0;
2262 } else {
2263 bytes = min_t(size_t, len, free->length);
2264 boffs = free->offset;
2265 }
2266 memcpy(oob, chip->oob_poi + boffs, bytes);
2267 oob += bytes;
2268 }
2269 return oob;
2270 }
2271 default:
2272 BUG();
2273 }
2274 return NULL;
2275}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002276
2277/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02002278 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
2279 * @mtd: MTD device structure
2280 * @retry_mode: the retry mode to use
2281 *
2282 * Some vendors supply a special command to shift the Vt threshold, to be used
2283 * when there are too many bitflips in a page (i.e., ECC error). After setting
2284 * a new threshold, the host should retry reading the page.
2285 */
2286static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2287{
Scott Wood17fed142016-05-30 13:57:56 -05002288 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002289
2290 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2291
2292 if (retry_mode >= chip->read_retries)
2293 return -EINVAL;
2294
2295 if (!chip->setup_read_retry)
2296 return -EOPNOTSUPP;
2297
2298 return chip->setup_read_retry(mtd, retry_mode);
2299}
2300
2301/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002302 * nand_do_read_ops - [INTERN] Read data with ECC
2303 * @mtd: MTD device structure
2304 * @from: offset to read from
2305 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002306 *
William Juul52c07962007-10-31 13:53:06 +01002307 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002308 */
William Juul52c07962007-10-31 13:53:06 +01002309static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2310 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002311{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002312 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05002313 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002314 int ret = 0;
2315 uint32_t readlen = ops->len;
2316 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002317 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002318
William Juul52c07962007-10-31 13:53:06 +01002319 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002320 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01002321 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002322 int retry_mode = 0;
2323 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002324
William Juul52c07962007-10-31 13:53:06 +01002325 chipnr = (int)(from >> chip->chip_shift);
2326 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002327
William Juul52c07962007-10-31 13:53:06 +01002328 realpage = (int)(from >> chip->page_shift);
2329 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002330
William Juul52c07962007-10-31 13:53:06 +01002331 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002332
William Juul52c07962007-10-31 13:53:06 +01002333 buf = ops->datbuf;
2334 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002335 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002336
Christian Hitz13fc0e22011-10-12 09:32:01 +02002337 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002338 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06002339
Heiko Schocherf5895d12014-06-24 10:10:04 +02002340 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01002341 bytes = min(mtd->writesize - col, readlen);
2342 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002343
Scott Wood3ea94ed2015-06-26 19:03:26 -05002344 if (!aligned)
2345 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002346 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2347 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2348 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002349 else
2350 use_bufpoi = 0;
2351
Sergey Lapin3a38a552013-01-14 03:46:50 +00002352 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01002353 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002354 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2355
2356 if (use_bufpoi && aligned)
2357 pr_debug("%s: using read bounce buffer for buf@%p\n",
2358 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002359
Heiko Schocherf5895d12014-06-24 10:10:04 +02002360read_retry:
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002361 if (nand_standard_page_accessors(&chip->ecc)) {
2362 ret = nand_read_page_op(chip, page, 0, NULL, 0);
2363 if (ret)
2364 break;
2365 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002366
Paul Burton700a76c2013-09-04 15:16:56 +01002367 /*
2368 * Now read the page into the buffer. Absent an error,
2369 * the read methods return max bitflips per ecc step.
2370 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002371 if (unlikely(ops->mode == MTD_OPS_RAW))
2372 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2373 oob_required,
2374 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002375 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002376 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02002377 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002378 col, bytes, bufpoi,
2379 page);
William Juul52c07962007-10-31 13:53:06 +01002380 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04002381 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002382 oob_required, page);
2383 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05002384 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002385 /* Invalidate page cache */
2386 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01002387 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002388 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002389
Paul Burton700a76c2013-09-04 15:16:56 +01002390 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2391
William Juul52c07962007-10-31 13:53:06 +01002392 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002393 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00002394 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02002395 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01002396 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05002397 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01002398 chip->pagebuf_bitflips = ret;
2399 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002400 /* Invalidate page cache */
2401 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01002402 }
William Juul52c07962007-10-31 13:53:06 +01002403 memcpy(buf, chip->buffers->databuf + col, bytes);
2404 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002405
William Juul52c07962007-10-31 13:53:06 +01002406 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02002407 int toread = min(oobreadlen, max_oobsize);
2408
2409 if (toread) {
2410 oob = nand_transfer_oob(chip,
2411 oob, ops, toread);
2412 oobreadlen -= toread;
2413 }
William Juul52c07962007-10-31 13:53:06 +01002414 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002415
2416 if (chip->options & NAND_NEED_READRDY) {
2417 /* Apply delay or wait for ready/busy pin */
2418 if (!chip->dev_ready)
2419 udelay(chip->chip_delay);
2420 else
2421 nand_wait_ready(mtd);
2422 }
2423
2424 if (mtd->ecc_stats.failed - ecc_failures) {
2425 if (retry_mode + 1 < chip->read_retries) {
2426 retry_mode++;
2427 ret = nand_setup_read_retry(mtd,
2428 retry_mode);
2429 if (ret < 0)
2430 break;
2431
2432 /* Reset failures; retry */
2433 mtd->ecc_stats.failed = ecc_failures;
2434 goto read_retry;
2435 } else {
2436 /* No more retry modes; real failure */
2437 ecc_fail = true;
2438 }
2439 }
2440
2441 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01002442 } else {
2443 memcpy(buf, chip->buffers->databuf + col, bytes);
2444 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01002445 max_bitflips = max_t(unsigned int, max_bitflips,
2446 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01002447 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002448
William Juul52c07962007-10-31 13:53:06 +01002449 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002450
Heiko Schocherf5895d12014-06-24 10:10:04 +02002451 /* Reset to retry mode 0 */
2452 if (retry_mode) {
2453 ret = nand_setup_read_retry(mtd, 0);
2454 if (ret < 0)
2455 break;
2456 retry_mode = 0;
2457 }
2458
William Juul52c07962007-10-31 13:53:06 +01002459 if (!readlen)
2460 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002461
Sergey Lapin3a38a552013-01-14 03:46:50 +00002462 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01002463 col = 0;
2464 /* Increment page address */
2465 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002466
William Juul52c07962007-10-31 13:53:06 +01002467 page = realpage & chip->pagemask;
2468 /* Check, if we cross a chip boundary */
2469 if (!page) {
2470 chipnr++;
2471 chip->select_chip(mtd, -1);
2472 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002473 }
William Juul52c07962007-10-31 13:53:06 +01002474 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002475 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002476
William Juul52c07962007-10-31 13:53:06 +01002477 ops->retlen = ops->len - (size_t) readlen;
2478 if (oob)
2479 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002480
Heiko Schocherf5895d12014-06-24 10:10:04 +02002481 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01002482 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002483
Heiko Schocherf5895d12014-06-24 10:10:04 +02002484 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01002485 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002486
Paul Burton700a76c2013-09-04 15:16:56 +01002487 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01002488}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002489
William Juul52c07962007-10-31 13:53:06 +01002490/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002491 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
2492 * @mtd: mtd info structure
2493 * @chip: nand chip info structure
2494 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002495 */
2496static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002497 int page)
William Juul52c07962007-10-31 13:53:06 +01002498{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002499 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002500}
2501
2502/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002503 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01002504 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00002505 * @mtd: mtd info structure
2506 * @chip: nand chip info structure
2507 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01002508 */
2509static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00002510 int page)
William Juul52c07962007-10-31 13:53:06 +01002511{
William Juul52c07962007-10-31 13:53:06 +01002512 int length = mtd->oobsize;
2513 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2514 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002515 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002516 int i, toread, sndrnd = 0, pos, ret;
William Juul52c07962007-10-31 13:53:06 +01002517
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002518 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
2519 if (ret)
2520 return ret;
2521
William Juul52c07962007-10-31 13:53:06 +01002522 for (i = 0; i < chip->ecc.steps; i++) {
2523 if (sndrnd) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002524 int ret;
2525
William Juul52c07962007-10-31 13:53:06 +01002526 pos = eccsize + i * (eccsize + chunk);
2527 if (mtd->writesize > 512)
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002528 ret = nand_change_read_column_op(chip, pos,
2529 NULL, 0,
2530 false);
William Juul52c07962007-10-31 13:53:06 +01002531 else
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002532 ret = nand_read_page_op(chip, page, pos, NULL,
2533 0);
2534
2535 if (ret)
2536 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002537 } else
William Juul52c07962007-10-31 13:53:06 +01002538 sndrnd = 1;
2539 toread = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002540
2541 ret = nand_read_data_op(chip, bufpoi, toread, false);
2542 if (ret)
2543 return ret;
2544
William Juul52c07962007-10-31 13:53:06 +01002545 bufpoi += toread;
2546 length -= toread;
2547 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002548 if (length > 0) {
2549 ret = nand_read_data_op(chip, bufpoi, length, false);
2550 if (ret)
2551 return ret;
2552 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002553
Sergey Lapin3a38a552013-01-14 03:46:50 +00002554 return 0;
William Juul52c07962007-10-31 13:53:06 +01002555}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002556
William Juul52c07962007-10-31 13:53:06 +01002557/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002558 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
2559 * @mtd: mtd info structure
2560 * @chip: nand chip info structure
2561 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002562 */
2563static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
2564 int page)
2565{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002566 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
2567 mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01002568}
2569
2570/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002571 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
2572 * with syndrome - only for large page flash
2573 * @mtd: mtd info structure
2574 * @chip: nand chip info structure
2575 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002576 */
2577static int nand_write_oob_syndrome(struct mtd_info *mtd,
2578 struct nand_chip *chip, int page)
2579{
2580 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2581 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002582 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
William Juul52c07962007-10-31 13:53:06 +01002583 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002584
2585 /*
William Juul52c07962007-10-31 13:53:06 +01002586 * data-ecc-data-ecc ... ecc-oob
2587 * or
2588 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002589 */
William Juul52c07962007-10-31 13:53:06 +01002590 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2591 pos = steps * (eccsize + chunk);
2592 steps = 0;
2593 } else
2594 pos = eccsize;
2595
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002596 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
2597 if (ret)
2598 return ret;
2599
William Juul52c07962007-10-31 13:53:06 +01002600 for (i = 0; i < steps; i++) {
2601 if (sndcmd) {
2602 if (mtd->writesize <= 512) {
2603 uint32_t fill = 0xFFFFFFFF;
2604
2605 len = eccsize;
2606 while (len > 0) {
2607 int num = min_t(int, len, 4);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002608
2609 ret = nand_write_data_op(chip, &fill,
2610 num, false);
2611 if (ret)
2612 return ret;
2613
William Juul52c07962007-10-31 13:53:06 +01002614 len -= num;
2615 }
2616 } else {
2617 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002618 ret = nand_change_write_column_op(chip, pos,
2619 NULL, 0,
2620 false);
2621 if (ret)
2622 return ret;
William Juul52c07962007-10-31 13:53:06 +01002623 }
2624 } else
2625 sndcmd = 1;
2626 len = min_t(int, length, chunk);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002627
2628 ret = nand_write_data_op(chip, bufpoi, len, false);
2629 if (ret)
2630 return ret;
2631
William Juul52c07962007-10-31 13:53:06 +01002632 bufpoi += len;
2633 length -= len;
2634 }
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002635 if (length > 0) {
2636 ret = nand_write_data_op(chip, bufpoi, length, false);
2637 if (ret)
2638 return ret;
2639 }
William Juul52c07962007-10-31 13:53:06 +01002640
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002641 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002642}
2643
2644/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002645 * nand_do_read_oob - [INTERN] NAND read out-of-band
2646 * @mtd: MTD device structure
2647 * @from: offset to read from
2648 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002649 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002650 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002651 */
William Juul52c07962007-10-31 13:53:06 +01002652static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2653 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002654{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002655 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002656 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002657 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002658 int readlen = ops->ooblen;
2659 int len;
2660 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002661 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002662
Heiko Schocherf5895d12014-06-24 10:10:04 +02002663 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002664 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002665
Sergey Lapin3a38a552013-01-14 03:46:50 +00002666 stats = mtd->ecc_stats;
2667
Scott Wood52ab7ce2016-05-30 13:57:58 -05002668 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002669
William Juul52c07962007-10-31 13:53:06 +01002670 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002671 pr_debug("%s: attempt to start read outside oob\n",
2672 __func__);
William Juul52c07962007-10-31 13:53:06 +01002673 return -EINVAL;
2674 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002675
2676 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002677 if (unlikely(from >= mtd->size ||
2678 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2679 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002680 pr_debug("%s: attempt to read beyond end of device\n",
2681 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002682 return -EINVAL;
2683 }
2684
William Juul52c07962007-10-31 13:53:06 +01002685 chipnr = (int)(from >> chip->chip_shift);
2686 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002687
William Juul52c07962007-10-31 13:53:06 +01002688 /* Shift to get page */
2689 realpage = (int)(from >> chip->page_shift);
2690 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002691
Christian Hitz13fc0e22011-10-12 09:32:01 +02002692 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002693 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002694
Sergey Lapin3a38a552013-01-14 03:46:50 +00002695 if (ops->mode == MTD_OPS_RAW)
2696 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2697 else
2698 ret = chip->ecc.read_oob(mtd, chip, page);
2699
2700 if (ret < 0)
2701 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002702
William Juul52c07962007-10-31 13:53:06 +01002703 len = min(len, readlen);
2704 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002705
Heiko Schocherf5895d12014-06-24 10:10:04 +02002706 if (chip->options & NAND_NEED_READRDY) {
2707 /* Apply delay or wait for ready/busy pin */
2708 if (!chip->dev_ready)
2709 udelay(chip->chip_delay);
2710 else
2711 nand_wait_ready(mtd);
2712 }
2713
William Juul52c07962007-10-31 13:53:06 +01002714 readlen -= len;
2715 if (!readlen)
2716 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002717
William Juul52c07962007-10-31 13:53:06 +01002718 /* Increment page address */
2719 realpage++;
2720
2721 page = realpage & chip->pagemask;
2722 /* Check, if we cross a chip boundary */
2723 if (!page) {
2724 chipnr++;
2725 chip->select_chip(mtd, -1);
2726 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002727 }
William Juul52c07962007-10-31 13:53:06 +01002728 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002729 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002730
Sergey Lapin3a38a552013-01-14 03:46:50 +00002731 ops->oobretlen = ops->ooblen - readlen;
2732
2733 if (ret < 0)
2734 return ret;
2735
2736 if (mtd->ecc_stats.failed - stats.failed)
2737 return -EBADMSG;
2738
2739 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002740}
2741
2742/**
William Juul52c07962007-10-31 13:53:06 +01002743 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002744 * @mtd: MTD device structure
2745 * @from: offset to read from
2746 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002747 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002748 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002749 */
William Juul52c07962007-10-31 13:53:06 +01002750static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2751 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002752{
William Juul52c07962007-10-31 13:53:06 +01002753 int ret = -ENOTSUPP;
2754
2755 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002756
2757 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002758 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002759 pr_debug("%s: attempt to read beyond end of device\n",
2760 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002761 return -EINVAL;
2762 }
2763
Heiko Schocherf5895d12014-06-24 10:10:04 +02002764 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002765
Christian Hitz13fc0e22011-10-12 09:32:01 +02002766 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002767 case MTD_OPS_PLACE_OOB:
2768 case MTD_OPS_AUTO_OOB:
2769 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002770 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002771
William Juul52c07962007-10-31 13:53:06 +01002772 default:
2773 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002774 }
2775
William Juul52c07962007-10-31 13:53:06 +01002776 if (!ops->datbuf)
2777 ret = nand_do_read_oob(mtd, from, ops);
2778 else
2779 ret = nand_do_read_ops(mtd, from, ops);
2780
Christian Hitz13fc0e22011-10-12 09:32:01 +02002781out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002782 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002783 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002784}
2785
2786
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002787/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002788 * nand_write_page_raw - [INTERN] raw page write function
2789 * @mtd: mtd info structure
2790 * @chip: nand chip info structure
2791 * @buf: data buffer
2792 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002793 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002794 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002795 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002796 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002797static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002798 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002799{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002800 int ret;
2801
2802 ret = nand_write_data_op(chip, buf, mtd->writesize, false);
2803 if (ret)
2804 return ret;
2805
2806 if (oob_required) {
2807 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
2808 false);
2809 if (ret)
2810 return ret;
2811 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002812
2813 return 0;
William Juul52c07962007-10-31 13:53:06 +01002814}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002815
William Juul52c07962007-10-31 13:53:06 +01002816/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002817 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2818 * @mtd: mtd info structure
2819 * @chip: nand chip info structure
2820 * @buf: data buffer
2821 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002822 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002823 *
2824 * We need a special oob layout and handling even when ECC isn't checked.
2825 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002826static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002827 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002828 const uint8_t *buf, int oob_required,
2829 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002830{
2831 int eccsize = chip->ecc.size;
2832 int eccbytes = chip->ecc.bytes;
2833 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002834 int steps, size, ret;
David Brownellee86b8d2009-11-07 16:27:01 -05002835
2836 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002837 ret = nand_write_data_op(chip, buf, eccsize, false);
2838 if (ret)
2839 return ret;
2840
David Brownellee86b8d2009-11-07 16:27:01 -05002841 buf += eccsize;
2842
2843 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002844 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
2845 false);
2846 if (ret)
2847 return ret;
2848
David Brownellee86b8d2009-11-07 16:27:01 -05002849 oob += chip->ecc.prepad;
2850 }
2851
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002852 ret = nand_write_data_op(chip, oob, eccbytes, false);
2853 if (ret)
2854 return ret;
2855
David Brownellee86b8d2009-11-07 16:27:01 -05002856 oob += eccbytes;
2857
2858 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002859 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
2860 false);
2861 if (ret)
2862 return ret;
2863
David Brownellee86b8d2009-11-07 16:27:01 -05002864 oob += chip->ecc.postpad;
2865 }
2866 }
2867
2868 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002869 if (size) {
2870 ret = nand_write_data_op(chip, oob, size, false);
2871 if (ret)
2872 return ret;
2873 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00002874
2875 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002876}
2877/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002878 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2879 * @mtd: mtd info structure
2880 * @chip: nand chip info structure
2881 * @buf: data buffer
2882 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002883 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002884 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002885static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002886 const uint8_t *buf, int oob_required,
2887 int page)
William Juul52c07962007-10-31 13:53:06 +01002888{
2889 int i, eccsize = chip->ecc.size;
2890 int eccbytes = chip->ecc.bytes;
2891 int eccsteps = chip->ecc.steps;
2892 uint8_t *ecc_calc = chip->buffers->ecccalc;
2893 const uint8_t *p = buf;
2894 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002895
Sergey Lapin3a38a552013-01-14 03:46:50 +00002896 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002897 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2898 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002899
William Juul52c07962007-10-31 13:53:06 +01002900 for (i = 0; i < chip->ecc.total; i++)
2901 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002902
Scott Wood46e13102016-05-30 13:57:57 -05002903 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002904}
2905
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002906/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002907 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2908 * @mtd: mtd info structure
2909 * @chip: nand chip info structure
2910 * @buf: data buffer
2911 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002912 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002913 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002914static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002915 const uint8_t *buf, int oob_required,
2916 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002917{
William Juul52c07962007-10-31 13:53:06 +01002918 int i, eccsize = chip->ecc.size;
2919 int eccbytes = chip->ecc.bytes;
2920 int eccsteps = chip->ecc.steps;
2921 uint8_t *ecc_calc = chip->buffers->ecccalc;
2922 const uint8_t *p = buf;
2923 uint32_t *eccpos = chip->ecc.layout->eccpos;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002924 int ret;
William Juul52c07962007-10-31 13:53:06 +01002925
2926 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2927 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002928
2929 ret = nand_write_data_op(chip, p, eccsize, false);
2930 if (ret)
2931 return ret;
2932
William Juul52c07962007-10-31 13:53:06 +01002933 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2934 }
2935
2936 for (i = 0; i < chip->ecc.total; i++)
2937 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2938
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002939 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
2940 if (ret)
2941 return ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002942
2943 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002944}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002945
Heiko Schocherf5895d12014-06-24 10:10:04 +02002946
2947/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002948 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002949 * @mtd: mtd info structure
2950 * @chip: nand chip info structure
2951 * @offset: column address of subpage within the page
2952 * @data_len: data length
2953 * @buf: data buffer
2954 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002955 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002956 */
2957static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2958 struct nand_chip *chip, uint32_t offset,
2959 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002960 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002961{
2962 uint8_t *oob_buf = chip->oob_poi;
2963 uint8_t *ecc_calc = chip->buffers->ecccalc;
2964 int ecc_size = chip->ecc.size;
2965 int ecc_bytes = chip->ecc.bytes;
2966 int ecc_steps = chip->ecc.steps;
2967 uint32_t *eccpos = chip->ecc.layout->eccpos;
2968 uint32_t start_step = offset / ecc_size;
2969 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2970 int oob_bytes = mtd->oobsize / ecc_steps;
2971 int step, i;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002972 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002973
2974 for (step = 0; step < ecc_steps; step++) {
2975 /* configure controller for WRITE access */
2976 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2977
2978 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01002979 ret = nand_write_data_op(chip, buf, ecc_size, false);
2980 if (ret)
2981 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002982
2983 /* mask ECC of un-touched subpages by padding 0xFF */
2984 if ((step < start_step) || (step > end_step))
2985 memset(ecc_calc, 0xff, ecc_bytes);
2986 else
2987 chip->ecc.calculate(mtd, buf, ecc_calc);
2988
2989 /* mask OOB of un-touched subpages by padding 0xFF */
2990 /* if oob_required, preserve OOB metadata of written subpage */
2991 if (!oob_required || (step < start_step) || (step > end_step))
2992 memset(oob_buf, 0xff, oob_bytes);
2993
2994 buf += ecc_size;
2995 ecc_calc += ecc_bytes;
2996 oob_buf += oob_bytes;
2997 }
2998
2999 /* copy calculated ECC for whole page to chip->buffer->oob */
3000 /* this include masked-value(0xFF) for unwritten subpages */
3001 ecc_calc = chip->buffers->ecccalc;
3002 for (i = 0; i < chip->ecc.total; i++)
3003 chip->oob_poi[eccpos[i]] = ecc_calc[i];
3004
3005 /* write OOB buffer to NAND device */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003006 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3007 if (ret)
3008 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003009
3010 return 0;
3011}
3012
3013
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003014/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003015 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
3016 * @mtd: mtd info structure
3017 * @chip: nand chip info structure
3018 * @buf: data buffer
3019 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05003020 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003021 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003022 * The hw generator calculates the error syndrome automatically. Therefore we
3023 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003024 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003025static int nand_write_page_syndrome(struct mtd_info *mtd,
3026 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05003027 const uint8_t *buf, int oob_required,
3028 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003029{
William Juul52c07962007-10-31 13:53:06 +01003030 int i, eccsize = chip->ecc.size;
3031 int eccbytes = chip->ecc.bytes;
3032 int eccsteps = chip->ecc.steps;
3033 const uint8_t *p = buf;
3034 uint8_t *oob = chip->oob_poi;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003035 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003036
William Juul52c07962007-10-31 13:53:06 +01003037 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
William Juul52c07962007-10-31 13:53:06 +01003038 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003039
3040 ret = nand_write_data_op(chip, p, eccsize, false);
3041 if (ret)
3042 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003043
William Juul52c07962007-10-31 13:53:06 +01003044 if (chip->ecc.prepad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003045 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
3046 false);
3047 if (ret)
3048 return ret;
3049
William Juul52c07962007-10-31 13:53:06 +01003050 oob += chip->ecc.prepad;
3051 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003052
William Juul52c07962007-10-31 13:53:06 +01003053 chip->ecc.calculate(mtd, p, oob);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003054
3055 ret = nand_write_data_op(chip, oob, eccbytes, false);
3056 if (ret)
3057 return ret;
3058
William Juul52c07962007-10-31 13:53:06 +01003059 oob += eccbytes;
3060
3061 if (chip->ecc.postpad) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003062 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
3063 false);
3064 if (ret)
3065 return ret;
3066
William Juul52c07962007-10-31 13:53:06 +01003067 oob += chip->ecc.postpad;
3068 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003069 }
3070
William Juul52c07962007-10-31 13:53:06 +01003071 /* Calculate remaining oob bytes */
3072 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003073 if (i) {
3074 ret = nand_write_data_op(chip, oob, i, false);
3075 if (ret)
3076 return ret;
3077 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003078
3079 return 0;
William Juul52c07962007-10-31 13:53:06 +01003080}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003081
William Juul52c07962007-10-31 13:53:06 +01003082/**
3083 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00003084 * @mtd: MTD device structure
3085 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02003086 * @offset: address offset within the page
3087 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00003088 * @buf: the data to write
3089 * @oob_required: must write chip->oob_poi to OOB
3090 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00003091 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01003092 */
3093static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003094 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003095 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01003096{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003097 int status, subpage;
3098
3099 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3100 chip->ecc.write_subpage)
3101 subpage = offset || (data_len < mtd->writesize);
3102 else
3103 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003104
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003105 if (nand_standard_page_accessors(&chip->ecc)) {
3106 status = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
3107 if (status)
3108 return status;
3109 }
William Juul52c07962007-10-31 13:53:06 +01003110
3111 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02003112 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05003113 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003114 else if (subpage)
3115 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05003116 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01003117 else
Scott Wood46e13102016-05-30 13:57:57 -05003118 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
3119 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003120
3121 if (status < 0)
3122 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003123
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003124 if (nand_standard_page_accessors(&chip->ecc))
3125 return nand_prog_page_end_op(chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003126
William Juul52c07962007-10-31 13:53:06 +01003127 return 0;
3128}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003129
William Juul52c07962007-10-31 13:53:06 +01003130/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003131 * nand_fill_oob - [INTERN] Transfer client buffer to oob
3132 * @mtd: MTD device structure
3133 * @oob: oob data buffer
3134 * @len: oob data write length
3135 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01003136 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003137static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
3138 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01003139{
Scott Wood17fed142016-05-30 13:57:56 -05003140 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003141
3142 /*
3143 * Initialise to all 0xFF, to avoid the possibility of left over OOB
3144 * data from a previous OOB read.
3145 */
3146 memset(chip->oob_poi, 0xff, mtd->oobsize);
3147
Christian Hitz13fc0e22011-10-12 09:32:01 +02003148 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003149
Sergey Lapin3a38a552013-01-14 03:46:50 +00003150 case MTD_OPS_PLACE_OOB:
3151 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003152 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
3153 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003154
Sergey Lapin3a38a552013-01-14 03:46:50 +00003155 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01003156 struct nand_oobfree *free = chip->ecc.layout->oobfree;
3157 uint32_t boffs = 0, woffs = ops->ooboffs;
3158 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003159
Christian Hitz13fc0e22011-10-12 09:32:01 +02003160 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003161 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01003162 if (unlikely(woffs)) {
3163 if (woffs >= free->length) {
3164 woffs -= free->length;
3165 continue;
3166 }
3167 boffs = free->offset + woffs;
3168 bytes = min_t(size_t, len,
3169 (free->length - woffs));
3170 woffs = 0;
3171 } else {
3172 bytes = min_t(size_t, len, free->length);
3173 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003174 }
William Juul52c07962007-10-31 13:53:06 +01003175 memcpy(chip->oob_poi + boffs, oob, bytes);
3176 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003177 }
William Juul52c07962007-10-31 13:53:06 +01003178 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003179 }
William Juul52c07962007-10-31 13:53:06 +01003180 default:
3181 BUG();
3182 }
3183 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003184}
3185
Christian Hitzb8a6b372011-10-12 09:32:02 +02003186#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003187
3188/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003189 * nand_do_write_ops - [INTERN] NAND write with ECC
3190 * @mtd: MTD device structure
3191 * @to: offset to write to
3192 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003193 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003194 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003195 */
William Juul52c07962007-10-31 13:53:06 +01003196static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
3197 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003198{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003199 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05003200 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01003201 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003202
3203 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05003204 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003205
William Juul52c07962007-10-31 13:53:06 +01003206 uint8_t *oob = ops->oobbuf;
3207 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003208 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003209 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003210
William Juul52c07962007-10-31 13:53:06 +01003211 ops->retlen = 0;
3212 if (!writelen)
3213 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003214
Heiko Schocherf5895d12014-06-24 10:10:04 +02003215 /* Reject writes, which are not page aligned */
3216 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003217 pr_notice("%s: attempt to write non page aligned data\n",
3218 __func__);
William Juul52c07962007-10-31 13:53:06 +01003219 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003220 }
3221
3222 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003223
William Juul52c07962007-10-31 13:53:06 +01003224 chipnr = (int)(to >> chip->chip_shift);
3225 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003226
3227 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01003228 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003229 ret = -EIO;
3230 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01003231 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003232
William Juul52c07962007-10-31 13:53:06 +01003233 realpage = (int)(to >> chip->page_shift);
3234 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003235
William Juul52c07962007-10-31 13:53:06 +01003236 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003237 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
3238 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01003239 chip->pagebuf = -1;
3240
Christian Hitzb8a6b372011-10-12 09:32:02 +02003241 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003242 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
3243 ret = -EINVAL;
3244 goto err_out;
3245 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02003246
Christian Hitz13fc0e22011-10-12 09:32:01 +02003247 while (1) {
William Juul52c07962007-10-31 13:53:06 +01003248 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01003249 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003250 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02003251 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003252
3253 if (part_pagewr)
3254 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003255 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
3256 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
3257 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003258 else
3259 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01003260
Heiko Schocherf5895d12014-06-24 10:10:04 +02003261 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05003262 /* Partial page write?, or need to use bounce buffer */
3263 if (use_bufpoi) {
3264 pr_debug("%s: using write bounce buffer for buf@%p\n",
3265 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003266 if (part_pagewr)
3267 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01003268 chip->pagebuf = -1;
3269 memset(chip->buffers->databuf, 0xff, mtd->writesize);
3270 memcpy(&chip->buffers->databuf[column], buf, bytes);
3271 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003272 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003273
Christian Hitzb8a6b372011-10-12 09:32:02 +02003274 if (unlikely(oob)) {
3275 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003276 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003277 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003278 } else {
3279 /* We still need to erase leftover OOB data */
3280 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003281 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003282 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09003283 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003284 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01003285 if (ret)
3286 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003287
William Juul52c07962007-10-31 13:53:06 +01003288 writelen -= bytes;
3289 if (!writelen)
3290 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003291
Heiko Schocherf5895d12014-06-24 10:10:04 +02003292 column = 0;
3293 buf += bytes;
3294 realpage++;
3295
3296 page = realpage & chip->pagemask;
3297 /* Check, if we cross a chip boundary */
3298 if (!page) {
3299 chipnr++;
3300 chip->select_chip(mtd, -1);
3301 chip->select_chip(mtd, chipnr);
3302 }
3303 }
3304
3305 ops->retlen = ops->len - writelen;
3306 if (unlikely(oob))
3307 ops->oobretlen = ops->ooblen;
3308
3309err_out:
3310 chip->select_chip(mtd, -1);
3311 return ret;
3312}
3313
3314/**
3315 * panic_nand_write - [MTD Interface] NAND write with ECC
3316 * @mtd: MTD device structure
3317 * @to: offset to write to
3318 * @len: number of bytes to write
3319 * @retlen: pointer to variable to store the number of written bytes
3320 * @buf: the data to write
3321 *
3322 * NAND write with ECC. Used when performing writes in interrupt context, this
3323 * may for example be called by mtdoops when writing an oops while in panic.
3324 */
3325static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3326 size_t *retlen, const uint8_t *buf)
3327{
Scott Wood17fed142016-05-30 13:57:56 -05003328 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003329 struct mtd_oob_ops ops;
3330 int ret;
3331
3332 /* Wait for the device to get ready */
3333 panic_nand_wait(mtd, chip, 400);
3334
3335 /* Grab the device */
3336 panic_nand_get_device(chip, mtd, FL_WRITING);
3337
Scott Wood3ea94ed2015-06-26 19:03:26 -05003338 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02003339 ops.len = len;
3340 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003341 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01003342
Heiko Schocherf5895d12014-06-24 10:10:04 +02003343 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003344
Sergey Lapin3a38a552013-01-14 03:46:50 +00003345 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01003346 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003347}
3348
3349/**
William Juul52c07962007-10-31 13:53:06 +01003350 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003351 * @mtd: MTD device structure
3352 * @to: offset to write to
3353 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003354 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003355 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003356 */
William Juul52c07962007-10-31 13:53:06 +01003357static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3358 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003359{
William Juul52c07962007-10-31 13:53:06 +01003360 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05003361 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003362
Heiko Schocherf5895d12014-06-24 10:10:04 +02003363 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02003364 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003365
Scott Wood52ab7ce2016-05-30 13:57:58 -05003366 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003367
3368 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01003369 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003370 pr_debug("%s: attempt to write past end of page\n",
3371 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003372 return -EINVAL;
3373 }
3374
William Juul52c07962007-10-31 13:53:06 +01003375 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003376 pr_debug("%s: attempt to start write outside oob\n",
3377 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003378 return -EINVAL;
3379 }
3380
Christian Hitz13fc0e22011-10-12 09:32:01 +02003381 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01003382 if (unlikely(to >= mtd->size ||
3383 ops->ooboffs + ops->ooblen >
3384 ((mtd->size >> chip->page_shift) -
3385 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003386 pr_debug("%s: attempt to write beyond end of device\n",
3387 __func__);
William Juul52c07962007-10-31 13:53:06 +01003388 return -EINVAL;
3389 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003390
William Juul52c07962007-10-31 13:53:06 +01003391 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01003392
3393 /*
3394 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3395 * of my DiskOnChip 2000 test units) will clear the whole data page too
3396 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3397 * it in the doc2000 driver in August 1999. dwmw2.
3398 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003399 nand_reset(chip, chipnr);
3400
3401 chip->select_chip(mtd, chipnr);
3402
3403 /* Shift to get page */
3404 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003405
3406 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003407 if (nand_check_wp(mtd)) {
3408 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01003409 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003410 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003411
William Juul52c07962007-10-31 13:53:06 +01003412 /* Invalidate the page cache, if we write to the cached page */
3413 if (page == chip->pagebuf)
3414 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003415
Sergey Lapin3a38a552013-01-14 03:46:50 +00003416 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3417
3418 if (ops->mode == MTD_OPS_RAW)
3419 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3420 else
3421 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003422
Heiko Schocherf5895d12014-06-24 10:10:04 +02003423 chip->select_chip(mtd, -1);
3424
William Juul52c07962007-10-31 13:53:06 +01003425 if (status)
3426 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003427
William Juul52c07962007-10-31 13:53:06 +01003428 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003429
William Juul52c07962007-10-31 13:53:06 +01003430 return 0;
3431}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003432
William Juul52c07962007-10-31 13:53:06 +01003433/**
3434 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00003435 * @mtd: MTD device structure
3436 * @to: offset to write to
3437 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01003438 */
3439static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3440 struct mtd_oob_ops *ops)
3441{
William Juul52c07962007-10-31 13:53:06 +01003442 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003443
William Juul52c07962007-10-31 13:53:06 +01003444 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003445
William Juul52c07962007-10-31 13:53:06 +01003446 /* Do not allow writes past end of device */
3447 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003448 pr_debug("%s: attempt to write beyond end of device\n",
3449 __func__);
William Juul52c07962007-10-31 13:53:06 +01003450 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003451 }
William Juul52c07962007-10-31 13:53:06 +01003452
Heiko Schocherf5895d12014-06-24 10:10:04 +02003453 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01003454
Christian Hitz13fc0e22011-10-12 09:32:01 +02003455 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003456 case MTD_OPS_PLACE_OOB:
3457 case MTD_OPS_AUTO_OOB:
3458 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01003459 break;
3460
3461 default:
3462 goto out;
3463 }
3464
3465 if (!ops->datbuf)
3466 ret = nand_do_write_oob(mtd, to, ops);
3467 else
3468 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003469
Christian Hitz13fc0e22011-10-12 09:32:01 +02003470out:
William Juul52c07962007-10-31 13:53:06 +01003471 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003472 return ret;
3473}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003474
3475/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05003476 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00003477 * @mtd: MTD device structure
3478 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003479 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05003480 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003481 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05003482static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003483{
Scott Wood17fed142016-05-30 13:57:56 -05003484 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003485 unsigned int eraseblock;
3486
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003487 /* Send commands to erase a block */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003488 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003489
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003490 return nand_erase_op(chip, eraseblock);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003491}
3492
3493/**
3494 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003495 * @mtd: MTD device structure
3496 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003497 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003498 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003499 */
William Juul52c07962007-10-31 13:53:06 +01003500static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003501{
William Juul52c07962007-10-31 13:53:06 +01003502 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003503}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003504
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003505/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003506 * nand_erase_nand - [INTERN] erase block(s)
3507 * @mtd: MTD device structure
3508 * @instr: erase instruction
3509 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003510 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003511 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003512 */
William Juul52c07962007-10-31 13:53:06 +01003513int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3514 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003515{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003516 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05003517 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003518 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003519
Heiko Schocherf5895d12014-06-24 10:10:04 +02003520 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3521 __func__, (unsigned long long)instr->addr,
3522 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003523
Christian Hitzb8a6b372011-10-12 09:32:02 +02003524 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003525 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003526
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003527 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003528 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003529
3530 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01003531 page = (int)(instr->addr >> chip->page_shift);
3532 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003533
3534 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01003535 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01003536
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003537 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01003538 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003539
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003540 /* Check, if it is write protected */
3541 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003542 pr_debug("%s: device is write protected!\n",
3543 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003544 instr->state = MTD_ERASE_FAILED;
3545 goto erase_exit;
3546 }
3547
3548 /* Loop through the pages */
3549 len = instr->len;
3550
3551 instr->state = MTD_ERASING;
3552
3553 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06003554 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02003555
Sergey Lapin3a38a552013-01-14 03:46:50 +00003556 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09003557 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05003558 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003559 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003560 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003561 instr->state = MTD_ERASE_FAILED;
3562 goto erase_exit;
3563 }
William Juul52c07962007-10-31 13:53:06 +01003564
3565 /*
3566 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00003567 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01003568 */
3569 if (page <= chip->pagebuf && chip->pagebuf <
3570 (page + pages_per_block))
3571 chip->pagebuf = -1;
3572
Scott Wood3ea94ed2015-06-26 19:03:26 -05003573 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003574
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003575 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01003576 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003577 pr_debug("%s: failed erase, page 0x%08x\n",
3578 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003579 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003580 instr->fail_addr =
3581 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003582 goto erase_exit;
3583 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003584
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003585 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003586 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003587 page += pages_per_block;
3588
3589 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01003590 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003591 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01003592 chip->select_chip(mtd, -1);
3593 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003594 }
3595 }
3596 instr->state = MTD_ERASE_DONE;
3597
Christian Hitz13fc0e22011-10-12 09:32:01 +02003598erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003599
3600 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003601
3602 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003603 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003604 nand_release_device(mtd);
3605
Scott Wood3628f002008-10-24 16:20:43 -05003606 /* Do call back function */
3607 if (!ret)
3608 mtd_erase_callback(instr);
3609
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003610 /* Return more or less happy */
3611 return ret;
3612}
3613
3614/**
3615 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00003616 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003617 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003618 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003619 */
William Juul52c07962007-10-31 13:53:06 +01003620static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003621{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003622 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003623
3624 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003625 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003626 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01003627 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003628}
3629
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003630/**
William Juul52c07962007-10-31 13:53:06 +01003631 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003632 * @mtd: MTD device structure
3633 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003634 */
William Juul52c07962007-10-31 13:53:06 +01003635static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003636{
Scott Wood52ab7ce2016-05-30 13:57:58 -05003637 struct nand_chip *chip = mtd_to_nand(mtd);
3638 int chipnr = (int)(offs >> chip->chip_shift);
3639 int ret;
3640
3641 /* Select the NAND device */
3642 nand_get_device(mtd, FL_READING);
3643 chip->select_chip(mtd, chipnr);
3644
3645 ret = nand_block_checkbad(mtd, offs, 0);
3646
3647 chip->select_chip(mtd, -1);
3648 nand_release_device(mtd);
3649
3650 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003651}
3652
3653/**
William Juul52c07962007-10-31 13:53:06 +01003654 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003655 * @mtd: MTD device structure
3656 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003657 */
William Juul52c07962007-10-31 13:53:06 +01003658static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003659{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003660 int ret;
3661
Christian Hitzb8a6b372011-10-12 09:32:02 +02003662 ret = nand_block_isbad(mtd, ofs);
3663 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003664 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003665 if (ret > 0)
3666 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003667 return ret;
3668 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003669
Heiko Schocherf5895d12014-06-24 10:10:04 +02003670 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003671}
3672
Heiko Schocherf5895d12014-06-24 10:10:04 +02003673/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003674 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3675 * @mtd: MTD device structure
3676 * @chip: nand chip info structure
3677 * @addr: feature address.
3678 * @subfeature_param: the subfeature parameters, a four bytes array.
3679 */
3680static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3681 int addr, uint8_t *subfeature_param)
3682{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003683#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3684 if (!chip->onfi_version ||
3685 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3686 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003687 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003688#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003689
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003690 return nand_set_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003691}
3692
3693/**
3694 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3695 * @mtd: MTD device structure
3696 * @chip: nand chip info structure
3697 * @addr: feature address.
3698 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003699 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003700static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3701 int addr, uint8_t *subfeature_param)
3702{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003703#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3704 if (!chip->onfi_version ||
3705 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3706 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003707 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003708#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003709
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003710 return nand_get_features_op(chip, addr, subfeature_param);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003711}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003712
Sergey Lapin3a38a552013-01-14 03:46:50 +00003713/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003714static void nand_set_defaults(struct nand_chip *chip, int busw)
3715{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003716 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003717 if (!chip->chip_delay)
3718 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003719
3720 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003721 if (chip->cmdfunc == NULL)
3722 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003723
3724 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003725 if (chip->waitfunc == NULL)
3726 chip->waitfunc = nand_wait;
3727
3728 if (!chip->select_chip)
3729 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003730
3731 /* set for ONFI nand */
3732 if (!chip->onfi_set_features)
3733 chip->onfi_set_features = nand_onfi_set_features;
3734 if (!chip->onfi_get_features)
3735 chip->onfi_get_features = nand_onfi_get_features;
3736
3737 /* If called twice, pointers that depend on busw may need to be reset */
3738 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003739 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3740 if (!chip->read_word)
3741 chip->read_word = nand_read_word;
3742 if (!chip->block_bad)
3743 chip->block_bad = nand_block_bad;
3744 if (!chip->block_markbad)
3745 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003746 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003747 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003748 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3749 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3750 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003751 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003752 if (!chip->scan_bbt)
3753 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003754
3755 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003756 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003757 spin_lock_init(&chip->controller->lock);
3758 init_waitqueue_head(&chip->controller->wq);
3759 }
3760
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003761 if (!chip->buf_align)
3762 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003763}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003764
Sergey Lapin3a38a552013-01-14 03:46:50 +00003765/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003766static void sanitize_string(char *s, size_t len)
3767{
3768 ssize_t i;
3769
Sergey Lapin3a38a552013-01-14 03:46:50 +00003770 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003771 s[len - 1] = 0;
3772
Sergey Lapin3a38a552013-01-14 03:46:50 +00003773 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003774 for (i = 0; i < len - 1; i++) {
3775 if (s[i] < ' ' || s[i] > 127)
3776 s[i] = '?';
3777 }
3778
Sergey Lapin3a38a552013-01-14 03:46:50 +00003779 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003780 strim(s);
3781}
3782
Florian Fainellic98a9352011-02-25 00:01:34 +00003783static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3784{
3785 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003786 while (len--) {
3787 crc ^= *p++ << 8;
3788 for (i = 0; i < 8; i++)
3789 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3790 }
3791
3792 return crc;
3793}
3794
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003795#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003796/* Parse the Extended Parameter Page. */
3797static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3798 struct nand_chip *chip, struct nand_onfi_params *p)
3799{
3800 struct onfi_ext_param_page *ep;
3801 struct onfi_ext_section *s;
3802 struct onfi_ext_ecc_info *ecc;
3803 uint8_t *cursor;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003804 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003805 int len;
3806 int i;
3807
3808 len = le16_to_cpu(p->ext_param_page_length) * 16;
3809 ep = kmalloc(len, GFP_KERNEL);
3810 if (!ep)
3811 return -ENOMEM;
3812
3813 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003814 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3815 if (ret)
3816 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003817
3818 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003819 ret = nand_change_read_column_op(chip,
3820 sizeof(*p) * p->num_of_param_pages,
3821 ep, len, true);
3822 if (ret)
3823 goto ext_out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003824
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003825 ret = -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003826 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3827 != le16_to_cpu(ep->crc))) {
3828 pr_debug("fail in the CRC.\n");
3829 goto ext_out;
3830 }
3831
3832 /*
3833 * Check the signature.
3834 * Do not strictly follow the ONFI spec, maybe changed in future.
3835 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003836 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003837 pr_debug("The signature is invalid.\n");
3838 goto ext_out;
3839 }
3840
3841 /* find the ECC section. */
3842 cursor = (uint8_t *)(ep + 1);
3843 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3844 s = ep->sections + i;
3845 if (s->type == ONFI_SECTION_TYPE_2)
3846 break;
3847 cursor += s->length * 16;
3848 }
3849 if (i == ONFI_EXT_SECTION_MAX) {
3850 pr_debug("We can not find the ECC section.\n");
3851 goto ext_out;
3852 }
3853
3854 /* get the info we want. */
3855 ecc = (struct onfi_ext_ecc_info *)cursor;
3856
3857 if (!ecc->codeword_size) {
3858 pr_debug("Invalid codeword size\n");
3859 goto ext_out;
3860 }
3861
3862 chip->ecc_strength_ds = ecc->ecc_bits;
3863 chip->ecc_step_ds = 1 << ecc->codeword_size;
3864 ret = 0;
3865
3866ext_out:
3867 kfree(ep);
3868 return ret;
3869}
3870
3871static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3872{
Scott Wood17fed142016-05-30 13:57:56 -05003873 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003874 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3875
3876 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3877 feature);
3878}
3879
3880/*
3881 * Configure chip properties from Micron vendor-specific ONFI table
3882 */
3883static void nand_onfi_detect_micron(struct nand_chip *chip,
3884 struct nand_onfi_params *p)
3885{
3886 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3887
3888 if (le16_to_cpu(p->vendor_revision) < 1)
3889 return;
3890
3891 chip->read_retries = micron->read_retry_options;
3892 chip->setup_read_retry = nand_setup_read_retry_micron;
3893}
3894
Florian Fainellic98a9352011-02-25 00:01:34 +00003895/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003896 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003897 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003898static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003899 int *busw)
3900{
3901 struct nand_onfi_params *p = &chip->onfi_params;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003902 char id[4];
3903 int i, ret, val;
Florian Fainellic98a9352011-02-25 00:01:34 +00003904
Sergey Lapin3a38a552013-01-14 03:46:50 +00003905 /* Try ONFI for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003906 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
3907 if (ret || strncmp(id, "ONFI", 4))
3908 return 0;
3909
3910 ret = nand_read_param_page_op(chip, 0, NULL, 0);
3911 if (ret)
Florian Fainellic98a9352011-02-25 00:01:34 +00003912 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003913
Florian Fainellic98a9352011-02-25 00:01:34 +00003914 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01003915 ret = nand_read_data_op(chip, p, sizeof(*p), true);
3916 if (ret)
3917 return 0;
3918
Florian Fainellic98a9352011-02-25 00:01:34 +00003919 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003920 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003921 break;
3922 }
3923 }
3924
Heiko Schocherf5895d12014-06-24 10:10:04 +02003925 if (i == 3) {
3926 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003927 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003928 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003929
Sergey Lapin3a38a552013-01-14 03:46:50 +00003930 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003931 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003932 if (val & (1 << 5))
3933 chip->onfi_version = 23;
3934 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003935 chip->onfi_version = 22;
3936 else if (val & (1 << 3))
3937 chip->onfi_version = 21;
3938 else if (val & (1 << 2))
3939 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003940 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003941 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003942
3943 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003944 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003945 return 0;
3946 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003947
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003948 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3949 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003950 if (!mtd->name)
3951 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003952
Florian Fainellic98a9352011-02-25 00:01:34 +00003953 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003954
3955 /*
3956 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3957 * (don't ask me who thought of this...). MTD assumes that these
3958 * dimensions will be power-of-2, so just truncate the remaining area.
3959 */
3960 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3961 mtd->erasesize *= mtd->writesize;
3962
Florian Fainellic98a9352011-02-25 00:01:34 +00003963 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003964
3965 /* See erasesize comment */
3966 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003967 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003968 chip->bits_per_cell = p->bits_per_cell;
3969
3970 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003971 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003972 else
3973 *busw = 0;
3974
3975 if (p->ecc_bits != 0xff) {
3976 chip->ecc_strength_ds = p->ecc_bits;
3977 chip->ecc_step_ds = 512;
3978 } else if (chip->onfi_version >= 21 &&
3979 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3980
3981 /*
3982 * The nand_flash_detect_ext_param_page() uses the
3983 * Change Read Column command which maybe not supported
3984 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3985 * now. We do not replace user supplied command function.
3986 */
3987 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3988 chip->cmdfunc = nand_command_lp;
3989
3990 /* The Extended Parameter Page is supported since ONFI 2.1. */
3991 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3992 pr_warn("Failed to detect ONFI extended param page\n");
3993 } else {
3994 pr_warn("Could not retrieve ONFI ECC requirements\n");
3995 }
3996
3997 if (p->jedec_id == NAND_MFR_MICRON)
3998 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003999
4000 return 1;
4001}
4002#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02004003static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00004004 int *busw)
4005{
4006 return 0;
4007}
4008#endif
4009
William Juul52c07962007-10-31 13:53:06 +01004010/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004011 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4012 */
4013static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
4014 int *busw)
4015{
4016 struct nand_jedec_params *p = &chip->jedec_params;
4017 struct jedec_ecc_info *ecc;
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004018 char id[5];
4019 int i, val, ret;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004020
4021 /* Try JEDEC for unknown chip or LP */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004022 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4023 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004024 return 0;
4025
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004026 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4027 if (ret)
4028 return 0;
4029
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004030 for (i = 0; i < 3; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004031 ret = nand_read_data_op(chip, p, sizeof(*p), true);
4032 if (ret)
4033 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004034
4035 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4036 le16_to_cpu(p->crc))
4037 break;
4038 }
4039
4040 if (i == 3) {
4041 pr_err("Could not find valid JEDEC parameter page; aborting\n");
4042 return 0;
4043 }
4044
4045 /* Check version */
4046 val = le16_to_cpu(p->revision);
4047 if (val & (1 << 2))
4048 chip->jedec_version = 10;
4049 else if (val & (1 << 1))
4050 chip->jedec_version = 1; /* vendor specific version */
4051
4052 if (!chip->jedec_version) {
4053 pr_info("unsupported JEDEC version: %d\n", val);
4054 return 0;
4055 }
4056
4057 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4058 sanitize_string(p->model, sizeof(p->model));
4059 if (!mtd->name)
4060 mtd->name = p->model;
4061
4062 mtd->writesize = le32_to_cpu(p->byte_per_page);
4063
4064 /* Please reference to the comment for nand_flash_detect_onfi. */
4065 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4066 mtd->erasesize *= mtd->writesize;
4067
4068 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4069
4070 /* Please reference to the comment for nand_flash_detect_onfi. */
4071 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4072 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4073 chip->bits_per_cell = p->bits_per_cell;
4074
4075 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
4076 *busw = NAND_BUSWIDTH_16;
4077 else
4078 *busw = 0;
4079
4080 /* ECC info */
4081 ecc = &p->ecc_info[0];
4082
4083 if (ecc->codeword_size >= 9) {
4084 chip->ecc_strength_ds = ecc->ecc_bits;
4085 chip->ecc_step_ds = 1 << ecc->codeword_size;
4086 } else {
4087 pr_warn("Invalid codeword size\n");
4088 }
4089
4090 return 1;
4091}
4092
4093/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004094 * nand_id_has_period - Check if an ID string has a given wraparound period
4095 * @id_data: the ID string
4096 * @arrlen: the length of the @id_data array
4097 * @period: the period of repitition
4098 *
4099 * Check if an ID string is repeated within a given sequence of bytes at
4100 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02004101 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00004102 * if the repetition has a period of @period; otherwise, returns zero.
4103 */
4104static int nand_id_has_period(u8 *id_data, int arrlen, int period)
4105{
4106 int i, j;
4107 for (i = 0; i < period; i++)
4108 for (j = i + period; j < arrlen; j += period)
4109 if (id_data[i] != id_data[j])
4110 return 0;
4111 return 1;
4112}
4113
4114/*
4115 * nand_id_len - Get the length of an ID string returned by CMD_READID
4116 * @id_data: the ID string
4117 * @arrlen: the length of the @id_data array
4118
4119 * Returns the length of the ID string, according to known wraparound/trailing
4120 * zero patterns. If no pattern exists, returns the length of the array.
4121 */
4122static int nand_id_len(u8 *id_data, int arrlen)
4123{
4124 int last_nonzero, period;
4125
4126 /* Find last non-zero byte */
4127 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
4128 if (id_data[last_nonzero])
4129 break;
4130
4131 /* All zeros */
4132 if (last_nonzero < 0)
4133 return 0;
4134
4135 /* Calculate wraparound period */
4136 for (period = 1; period < arrlen; period++)
4137 if (nand_id_has_period(id_data, arrlen, period))
4138 break;
4139
4140 /* There's a repeated pattern */
4141 if (period < arrlen)
4142 return period;
4143
4144 /* There are trailing zeros */
4145 if (last_nonzero < arrlen - 1)
4146 return last_nonzero + 1;
4147
4148 /* No pattern detected */
4149 return arrlen;
4150}
4151
Heiko Schocherf5895d12014-06-24 10:10:04 +02004152/* Extract the bits of per cell from the 3rd byte of the extended ID */
4153static int nand_get_bits_per_cell(u8 cellinfo)
4154{
4155 int bits;
4156
4157 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
4158 bits >>= NAND_CI_CELLTYPE_SHIFT;
4159 return bits + 1;
4160}
4161
Sergey Lapin3a38a552013-01-14 03:46:50 +00004162/*
4163 * Many new NAND share similar device ID codes, which represent the size of the
4164 * chip. The rest of the parameters must be decoded according to generic or
4165 * manufacturer-specific "extended ID" decoding patterns.
4166 */
4167static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
4168 u8 id_data[8], int *busw)
4169{
4170 int extid, id_len;
4171 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004172 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00004173 /* The 4th id byte is the important one */
4174 extid = id_data[3];
4175
4176 id_len = nand_id_len(id_data, 8);
4177
4178 /*
4179 * Field definitions are in the following datasheets:
4180 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
4181 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
4182 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
4183 *
4184 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
4185 * ID to decide what to do.
4186 */
4187 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02004188 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004189 /* Calc pagesize */
4190 mtd->writesize = 2048 << (extid & 0x03);
4191 extid >>= 2;
4192 /* Calc oobsize */
4193 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4194 case 1:
4195 mtd->oobsize = 128;
4196 break;
4197 case 2:
4198 mtd->oobsize = 218;
4199 break;
4200 case 3:
4201 mtd->oobsize = 400;
4202 break;
4203 case 4:
4204 mtd->oobsize = 436;
4205 break;
4206 case 5:
4207 mtd->oobsize = 512;
4208 break;
4209 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004210 mtd->oobsize = 640;
4211 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004212 case 7:
4213 default: /* Other cases are "reserved" (unknown) */
4214 mtd->oobsize = 1024;
4215 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004216 }
4217 extid >>= 2;
4218 /* Calc blocksize */
4219 mtd->erasesize = (128 * 1024) <<
4220 (((extid >> 1) & 0x04) | (extid & 0x03));
4221 *busw = 0;
4222 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02004223 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004224 unsigned int tmp;
4225
4226 /* Calc pagesize */
4227 mtd->writesize = 2048 << (extid & 0x03);
4228 extid >>= 2;
4229 /* Calc oobsize */
4230 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
4231 case 0:
4232 mtd->oobsize = 128;
4233 break;
4234 case 1:
4235 mtd->oobsize = 224;
4236 break;
4237 case 2:
4238 mtd->oobsize = 448;
4239 break;
4240 case 3:
4241 mtd->oobsize = 64;
4242 break;
4243 case 4:
4244 mtd->oobsize = 32;
4245 break;
4246 case 5:
4247 mtd->oobsize = 16;
4248 break;
4249 default:
4250 mtd->oobsize = 640;
4251 break;
4252 }
4253 extid >>= 2;
4254 /* Calc blocksize */
4255 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
4256 if (tmp < 0x03)
4257 mtd->erasesize = (128 * 1024) << tmp;
4258 else if (tmp == 0x03)
4259 mtd->erasesize = 768 * 1024;
4260 else
4261 mtd->erasesize = (64 * 1024) << tmp;
4262 *busw = 0;
4263 } else {
4264 /* Calc pagesize */
4265 mtd->writesize = 1024 << (extid & 0x03);
4266 extid >>= 2;
4267 /* Calc oobsize */
4268 mtd->oobsize = (8 << (extid & 0x01)) *
4269 (mtd->writesize >> 9);
4270 extid >>= 2;
4271 /* Calc blocksize. Blocksize is multiples of 64KiB */
4272 mtd->erasesize = (64 * 1024) << (extid & 0x03);
4273 extid >>= 2;
4274 /* Get buswidth information */
4275 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004276
4277 /*
4278 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
4279 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
4280 * follows:
4281 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
4282 * 110b -> 24nm
4283 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
4284 */
4285 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
4286 nand_is_slc(chip) &&
4287 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
4288 !(id_data[4] & 0x80) /* !BENAND */) {
4289 mtd->oobsize = 32 * mtd->writesize >> 9;
4290 }
4291
Sergey Lapin3a38a552013-01-14 03:46:50 +00004292 }
4293}
4294
Heiko Schocherf5895d12014-06-24 10:10:04 +02004295/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004296 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
4297 * decodes a matching ID table entry and assigns the MTD size parameters for
4298 * the chip.
4299 */
4300static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004301 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00004302 int *busw)
4303{
4304 int maf_id = id_data[0];
4305
4306 mtd->erasesize = type->erasesize;
4307 mtd->writesize = type->pagesize;
4308 mtd->oobsize = mtd->writesize / 32;
4309 *busw = type->options & NAND_BUSWIDTH_16;
4310
Heiko Schocherf5895d12014-06-24 10:10:04 +02004311 /* All legacy ID NAND are small-page, SLC */
4312 chip->bits_per_cell = 1;
4313
Sergey Lapin3a38a552013-01-14 03:46:50 +00004314 /*
4315 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
4316 * some Spansion chips have erasesize that conflicts with size
4317 * listed in nand_ids table.
4318 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
4319 */
4320 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
4321 && id_data[6] == 0x00 && id_data[7] == 0x00
4322 && mtd->writesize == 512) {
4323 mtd->erasesize = 128 * 1024;
4324 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
4325 }
4326}
4327
Heiko Schocherf5895d12014-06-24 10:10:04 +02004328/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004329 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4330 * heuristic patterns using various detected parameters (e.g., manufacturer,
4331 * page size, cell-type information).
4332 */
4333static void nand_decode_bbm_options(struct mtd_info *mtd,
4334 struct nand_chip *chip, u8 id_data[8])
4335{
4336 int maf_id = id_data[0];
4337
4338 /* Set the bad block position */
4339 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4340 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4341 else
4342 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4343
4344 /*
4345 * Bad block marker is stored in the last page of each block on Samsung
4346 * and Hynix MLC devices; stored in first two pages of each block on
4347 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4348 * AMD/Spansion, and Macronix. All others scan only the first page.
4349 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004350 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 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004354 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00004355 (maf_id == NAND_MFR_SAMSUNG ||
4356 maf_id == NAND_MFR_HYNIX ||
4357 maf_id == NAND_MFR_TOSHIBA ||
4358 maf_id == NAND_MFR_AMD ||
4359 maf_id == NAND_MFR_MACRONIX)) ||
4360 (mtd->writesize == 2048 &&
4361 maf_id == NAND_MFR_MICRON))
4362 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4363}
4364
Heiko Schocherf5895d12014-06-24 10:10:04 +02004365static inline bool is_full_id_nand(struct nand_flash_dev *type)
4366{
4367 return type->id_len;
4368}
4369
4370static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
4371 struct nand_flash_dev *type, u8 *id_data, int *busw)
4372{
Heiko Schocherf5895d12014-06-24 10:10:04 +02004373 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004374 mtd->writesize = type->pagesize;
4375 mtd->erasesize = type->erasesize;
4376 mtd->oobsize = type->oobsize;
4377
4378 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
4379 chip->chipsize = (uint64_t)type->chipsize << 20;
4380 chip->options |= type->options;
4381 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4382 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004383 chip->onfi_timing_mode_default =
4384 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004385
4386 *busw = type->options & NAND_BUSWIDTH_16;
4387
4388 if (!mtd->name)
4389 mtd->name = type->name;
4390
4391 return true;
4392 }
4393 return false;
4394}
4395
Sergey Lapin3a38a552013-01-14 03:46:50 +00004396/*
4397 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01004398 */
Jörg Krause929fb442018-01-14 19:26:37 +01004399struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01004400 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00004401 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004402 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01004403{
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004404 int busw, ret;
4405 int maf_idx;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004406 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004407
Karl Beldanb6322fc2008-09-15 16:08:03 +02004408 /*
4409 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00004410 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02004411 */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004412 ret = nand_reset(chip, 0);
4413 if (ret)
4414 return ERR_PTR(ret);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004415
4416 /* Select the device */
4417 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02004418
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004419 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004420 ret = nand_readid_op(chip, 0, id_data, 2);
4421 if (ret)
4422 return ERR_PTR(ret);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004423
4424 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004425 *maf_id = id_data[0];
4426 *dev_id = id_data[1];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004427
Sergey Lapin3a38a552013-01-14 03:46:50 +00004428 /*
4429 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05004430 * interface concerns can cause random data which looks like a
4431 * possibly credible NAND flash to appear. If the two results do
4432 * not match, ignore the device completely.
4433 */
4434
Sergey Lapin3a38a552013-01-14 03:46:50 +00004435 /* Read entire ID string */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004436 ret = nand_readid_op(chip, 0, id_data, 8);
4437 if (ret)
4438 return ERR_PTR(ret);
Scott Wood3628f002008-10-24 16:20:43 -05004439
Christian Hitzb8a6b372011-10-12 09:32:02 +02004440 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004441 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004442 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05004443 return ERR_PTR(-ENODEV);
4444 }
4445
Lei Wen75bde942011-01-06 09:48:18 +08004446 if (!type)
4447 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004448
Heiko Schocherf5895d12014-06-24 10:10:04 +02004449 for (; type->name != NULL; type++) {
4450 if (is_full_id_nand(type)) {
4451 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4452 goto ident_done;
4453 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05004454 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004455 }
4456 }
Lei Wen75bde942011-01-06 09:48:18 +08004457
Christian Hitzb8a6b372011-10-12 09:32:02 +02004458 chip->onfi_version = 0;
4459 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004460 /* Check if the chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00004461 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02004462 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004463
4464 /* Check if the chip is JEDEC compliant */
4465 if (nand_flash_detect_jedec(mtd, chip, &busw))
4466 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02004467 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004468
Christian Hitzb8a6b372011-10-12 09:32:02 +02004469 if (!type->name)
4470 return ERR_PTR(-ENODEV);
4471
William Juul52c07962007-10-31 13:53:06 +01004472 if (!mtd->name)
4473 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004474
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004475 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004476
Scott Wood52ab7ce2016-05-30 13:57:58 -05004477 if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004478 /* Decode parameters from extended ID */
4479 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004480 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004481 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004482 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004483 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00004484 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00004485
Sergey Lapin3a38a552013-01-14 03:46:50 +00004486 /*
4487 * Check if chip is not a Samsung device. Do not clear the
4488 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02004489 */
4490 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4491 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4492ident_done:
4493
William Juul52c07962007-10-31 13:53:06 +01004494 /* Try to identify manufacturer */
4495 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
4496 if (nand_manuf_ids[maf_idx].id == *maf_id)
4497 break;
4498 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004499
Heiko Schocherf5895d12014-06-24 10:10:04 +02004500 if (chip->options & NAND_BUSWIDTH_AUTO) {
4501 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4502 chip->options |= busw;
4503 nand_set_defaults(chip, busw);
4504 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4505 /*
4506 * Check, if buswidth is correct. Hardware drivers should set
4507 * chip correct!
4508 */
4509 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4510 *maf_id, *dev_id);
4511 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4512 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00004513 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4514 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01004515 return ERR_PTR(-EINVAL);
4516 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004517
Sergey Lapin3a38a552013-01-14 03:46:50 +00004518 nand_decode_bbm_options(mtd, chip, id_data);
4519
William Juul52c07962007-10-31 13:53:06 +01004520 /* Calculate the address shift from the page size */
4521 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004522 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01004523 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004524
William Juul52c07962007-10-31 13:53:06 +01004525 chip->bbt_erase_shift = chip->phys_erase_shift =
4526 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04004527 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05004528 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004529 else {
4530 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4531 chip->chip_shift += 32 - 1;
4532 }
4533
Masahiro Yamada984926b2017-11-22 02:38:31 +09004534 if (chip->chip_shift - chip->page_shift > 16)
4535 chip->options |= NAND_ROW_ADDR_3;
4536
Christian Hitzb8a6b372011-10-12 09:32:02 +02004537 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004538 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004539
Sergey Lapin3a38a552013-01-14 03:46:50 +00004540 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01004541 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4542 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004543
Heiko Schocherf5895d12014-06-24 10:10:04 +02004544 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4545 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004546
Christian Hitzb8a6b372011-10-12 09:32:02 +02004547#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004548 if (chip->onfi_version)
4549 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4550 chip->onfi_params.model);
4551 else if (chip->jedec_version)
4552 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4553 chip->jedec_params.model);
4554 else
4555 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4556 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004557#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004558 if (chip->jedec_version)
4559 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4560 chip->jedec_params.model);
4561 else
4562 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4563 type->name);
4564
4565 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4566 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02004567#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004568
Scott Wood3ea94ed2015-06-26 19:03:26 -05004569 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02004570 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05004571 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01004572 return type;
4573}
Jörg Krause929fb442018-01-14 19:26:37 +01004574EXPORT_SYMBOL(nand_get_flash_type);
William Juul52c07962007-10-31 13:53:06 +01004575
Brian Norrisba6463d2016-06-15 21:09:22 +02004576#if CONFIG_IS_ENABLED(OF_CONTROL)
4577DECLARE_GLOBAL_DATA_PTR;
4578
4579static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
4580{
4581 int ret, ecc_mode = -1, ecc_strength, ecc_step;
4582 const void *blob = gd->fdt_blob;
4583 const char *str;
4584
4585 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
4586 if (ret == 16)
4587 chip->options |= NAND_BUSWIDTH_16;
4588
4589 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
4590 chip->bbt_options |= NAND_BBT_USE_FLASH;
4591
4592 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
4593 if (str) {
4594 if (!strcmp(str, "none"))
4595 ecc_mode = NAND_ECC_NONE;
4596 else if (!strcmp(str, "soft"))
4597 ecc_mode = NAND_ECC_SOFT;
4598 else if (!strcmp(str, "hw"))
4599 ecc_mode = NAND_ECC_HW;
4600 else if (!strcmp(str, "hw_syndrome"))
4601 ecc_mode = NAND_ECC_HW_SYNDROME;
4602 else if (!strcmp(str, "hw_oob_first"))
4603 ecc_mode = NAND_ECC_HW_OOB_FIRST;
4604 else if (!strcmp(str, "soft_bch"))
4605 ecc_mode = NAND_ECC_SOFT_BCH;
4606 }
4607
4608
4609 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
4610 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
4611
4612 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4613 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4614 pr_err("must set both strength and step size in DT\n");
4615 return -EINVAL;
4616 }
4617
4618 if (ecc_mode >= 0)
4619 chip->ecc.mode = ecc_mode;
4620
4621 if (ecc_strength >= 0)
4622 chip->ecc.strength = ecc_strength;
4623
4624 if (ecc_step > 0)
4625 chip->ecc.size = ecc_step;
4626
Boris Brezillonf1a54b02017-11-22 02:38:13 +09004627 if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL))
4628 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4629
Brian Norrisba6463d2016-06-15 21:09:22 +02004630 return 0;
4631}
4632#else
4633static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
4634{
4635 return 0;
4636}
4637#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
4638
William Juul52c07962007-10-31 13:53:06 +01004639/**
4640 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004641 * @mtd: MTD device structure
4642 * @maxchips: number of chips to scan for
4643 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01004644 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004645 * This is the first phase of the normal nand_scan() function. It reads the
4646 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004647 *
William Juul52c07962007-10-31 13:53:06 +01004648 */
Lei Wen75bde942011-01-06 09:48:18 +08004649int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004650 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004651{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004652 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004653 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004654 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02004655 int ret;
4656
4657 if (chip->flash_node) {
4658 ret = nand_dt_init(mtd, chip, chip->flash_node);
4659 if (ret)
4660 return ret;
4661 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004662
William Juul52c07962007-10-31 13:53:06 +01004663 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004664 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004665
4666 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004667 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4668 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004669
4670 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004671 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4672 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004673 chip->select_chip(mtd, -1);
4674 return PTR_ERR(type);
4675 }
4676
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004677 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004678 ret = nand_init_data_interface(chip);
4679 if (ret)
4680 return ret;
4681
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004682 /*
4683 * Setup the data interface correctly on the chip and controller side.
4684 * This explicit call to nand_setup_data_interface() is only required
4685 * for the first die, because nand_reset() has been called before
4686 * ->data_interface and ->default_onfi_timing_mode were set.
4687 * For the other dies, nand_reset() will automatically switch to the
4688 * best mode for us.
4689 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004690 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004691 if (ret)
4692 return ret;
4693
Heiko Schocherf5895d12014-06-24 10:10:04 +02004694 chip->select_chip(mtd, -1);
4695
William Juul52c07962007-10-31 13:53:06 +01004696 /* Check for a chip array */
4697 for (i = 1; i < maxchips; i++) {
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004698 u8 id[2];
4699
Karl Beldanb6322fc2008-09-15 16:08:03 +02004700 /* See comment in nand_get_flash_type for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004701 nand_reset(chip, i);
4702
4703 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004704 /* Send the command for reading device ID */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004705 nand_readid_op(chip, 0, id, sizeof(id));
4706
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004707 /* Read manufacturer and device IDs */
Boris Brezillon16ee8f62019-03-15 15:14:32 +01004708 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004709 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004710 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004711 }
4712 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004713 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004714
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004715#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004716 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004717 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004718#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004719
William Juul52c07962007-10-31 13:53:06 +01004720 /* Store the number of chips and calc total size for mtd */
4721 chip->numchips = i;
4722 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004723
William Juul52c07962007-10-31 13:53:06 +01004724 return 0;
4725}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004726EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004727
Masahiro Yamada820eb482017-11-22 02:38:29 +09004728/**
4729 * nand_check_ecc_caps - check the sanity of preset ECC settings
4730 * @chip: nand chip info structure
4731 * @caps: ECC caps info structure
4732 * @oobavail: OOB size that the ECC engine can use
4733 *
4734 * When ECC step size and strength are already set, check if they are supported
4735 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4736 * On success, the calculated ECC bytes is set.
4737 */
4738int nand_check_ecc_caps(struct nand_chip *chip,
4739 const struct nand_ecc_caps *caps, int oobavail)
4740{
4741 struct mtd_info *mtd = nand_to_mtd(chip);
4742 const struct nand_ecc_step_info *stepinfo;
4743 int preset_step = chip->ecc.size;
4744 int preset_strength = chip->ecc.strength;
4745 int nsteps, ecc_bytes;
4746 int i, j;
4747
4748 if (WARN_ON(oobavail < 0))
4749 return -EINVAL;
4750
4751 if (!preset_step || !preset_strength)
4752 return -ENODATA;
4753
4754 nsteps = mtd->writesize / preset_step;
4755
4756 for (i = 0; i < caps->nstepinfos; i++) {
4757 stepinfo = &caps->stepinfos[i];
4758
4759 if (stepinfo->stepsize != preset_step)
4760 continue;
4761
4762 for (j = 0; j < stepinfo->nstrengths; j++) {
4763 if (stepinfo->strengths[j] != preset_strength)
4764 continue;
4765
4766 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4767 preset_strength);
4768 if (WARN_ON_ONCE(ecc_bytes < 0))
4769 return ecc_bytes;
4770
4771 if (ecc_bytes * nsteps > oobavail) {
4772 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4773 preset_step, preset_strength);
4774 return -ENOSPC;
4775 }
4776
4777 chip->ecc.bytes = ecc_bytes;
4778
4779 return 0;
4780 }
4781 }
4782
4783 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4784 preset_step, preset_strength);
4785
4786 return -ENOTSUPP;
4787}
4788EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4789
4790/**
4791 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4792 * @chip: nand chip info structure
4793 * @caps: ECC engine caps info structure
4794 * @oobavail: OOB size that the ECC engine can use
4795 *
4796 * If a chip's ECC requirement is provided, try to meet it with the least
4797 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4798 * On success, the chosen ECC settings are set.
4799 */
4800int nand_match_ecc_req(struct nand_chip *chip,
4801 const struct nand_ecc_caps *caps, int oobavail)
4802{
4803 struct mtd_info *mtd = nand_to_mtd(chip);
4804 const struct nand_ecc_step_info *stepinfo;
4805 int req_step = chip->ecc_step_ds;
4806 int req_strength = chip->ecc_strength_ds;
4807 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4808 int best_step, best_strength, best_ecc_bytes;
4809 int best_ecc_bytes_total = INT_MAX;
4810 int i, j;
4811
4812 if (WARN_ON(oobavail < 0))
4813 return -EINVAL;
4814
4815 /* No information provided by the NAND chip */
4816 if (!req_step || !req_strength)
4817 return -ENOTSUPP;
4818
4819 /* number of correctable bits the chip requires in a page */
4820 req_corr = mtd->writesize / req_step * req_strength;
4821
4822 for (i = 0; i < caps->nstepinfos; i++) {
4823 stepinfo = &caps->stepinfos[i];
4824 step_size = stepinfo->stepsize;
4825
4826 for (j = 0; j < stepinfo->nstrengths; j++) {
4827 strength = stepinfo->strengths[j];
4828
4829 /*
4830 * If both step size and strength are smaller than the
4831 * chip's requirement, it is not easy to compare the
4832 * resulted reliability.
4833 */
4834 if (step_size < req_step && strength < req_strength)
4835 continue;
4836
4837 if (mtd->writesize % step_size)
4838 continue;
4839
4840 nsteps = mtd->writesize / step_size;
4841
4842 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4843 if (WARN_ON_ONCE(ecc_bytes < 0))
4844 continue;
4845 ecc_bytes_total = ecc_bytes * nsteps;
4846
4847 if (ecc_bytes_total > oobavail ||
4848 strength * nsteps < req_corr)
4849 continue;
4850
4851 /*
4852 * We assume the best is to meet the chip's requrement
4853 * with the least number of ECC bytes.
4854 */
4855 if (ecc_bytes_total < best_ecc_bytes_total) {
4856 best_ecc_bytes_total = ecc_bytes_total;
4857 best_step = step_size;
4858 best_strength = strength;
4859 best_ecc_bytes = ecc_bytes;
4860 }
4861 }
4862 }
4863
4864 if (best_ecc_bytes_total == INT_MAX)
4865 return -ENOTSUPP;
4866
4867 chip->ecc.size = best_step;
4868 chip->ecc.strength = best_strength;
4869 chip->ecc.bytes = best_ecc_bytes;
4870
4871 return 0;
4872}
4873EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4874
4875/**
4876 * nand_maximize_ecc - choose the max ECC strength available
4877 * @chip: nand chip info structure
4878 * @caps: ECC engine caps info structure
4879 * @oobavail: OOB size that the ECC engine can use
4880 *
4881 * Choose the max ECC strength that is supported on the controller, and can fit
4882 * within the chip's OOB. On success, the chosen ECC settings are set.
4883 */
4884int nand_maximize_ecc(struct nand_chip *chip,
4885 const struct nand_ecc_caps *caps, int oobavail)
4886{
4887 struct mtd_info *mtd = nand_to_mtd(chip);
4888 const struct nand_ecc_step_info *stepinfo;
4889 int step_size, strength, nsteps, ecc_bytes, corr;
4890 int best_corr = 0;
4891 int best_step = 0;
4892 int best_strength, best_ecc_bytes;
4893 int i, j;
4894
4895 if (WARN_ON(oobavail < 0))
4896 return -EINVAL;
4897
4898 for (i = 0; i < caps->nstepinfos; i++) {
4899 stepinfo = &caps->stepinfos[i];
4900 step_size = stepinfo->stepsize;
4901
4902 /* If chip->ecc.size is already set, respect it */
4903 if (chip->ecc.size && step_size != chip->ecc.size)
4904 continue;
4905
4906 for (j = 0; j < stepinfo->nstrengths; j++) {
4907 strength = stepinfo->strengths[j];
4908
4909 if (mtd->writesize % step_size)
4910 continue;
4911
4912 nsteps = mtd->writesize / step_size;
4913
4914 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4915 if (WARN_ON_ONCE(ecc_bytes < 0))
4916 continue;
4917
4918 if (ecc_bytes * nsteps > oobavail)
4919 continue;
4920
4921 corr = strength * nsteps;
4922
4923 /*
4924 * If the number of correctable bits is the same,
4925 * bigger step_size has more reliability.
4926 */
4927 if (corr > best_corr ||
4928 (corr == best_corr && step_size > best_step)) {
4929 best_corr = corr;
4930 best_step = step_size;
4931 best_strength = strength;
4932 best_ecc_bytes = ecc_bytes;
4933 }
4934 }
4935 }
4936
4937 if (!best_corr)
4938 return -ENOTSUPP;
4939
4940 chip->ecc.size = best_step;
4941 chip->ecc.strength = best_strength;
4942 chip->ecc.bytes = best_ecc_bytes;
4943
4944 return 0;
4945}
4946EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4947
Scott Wood3ea94ed2015-06-26 19:03:26 -05004948/*
4949 * Check if the chip configuration meet the datasheet requirements.
4950
4951 * If our configuration corrects A bits per B bytes and the minimum
4952 * required correction level is X bits per Y bytes, then we must ensure
4953 * both of the following are true:
4954 *
4955 * (1) A / B >= X / Y
4956 * (2) A >= X
4957 *
4958 * Requirement (1) ensures we can correct for the required bitflip density.
4959 * Requirement (2) ensures we can correct even when all bitflips are clumped
4960 * in the same sector.
4961 */
4962static bool nand_ecc_strength_good(struct mtd_info *mtd)
4963{
Scott Wood17fed142016-05-30 13:57:56 -05004964 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004965 struct nand_ecc_ctrl *ecc = &chip->ecc;
4966 int corr, ds_corr;
4967
4968 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4969 /* Not enough information */
4970 return true;
4971
4972 /*
4973 * We get the number of corrected bits per page to compare
4974 * the correction density.
4975 */
4976 corr = (mtd->writesize * ecc->strength) / ecc->size;
4977 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4978
4979 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4980}
William Juul52c07962007-10-31 13:53:06 +01004981
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004982static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4983{
4984 struct nand_ecc_ctrl *ecc = &chip->ecc;
4985
4986 if (nand_standard_page_accessors(ecc))
4987 return false;
4988
4989 /*
4990 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4991 * controller driver implements all the page accessors because
4992 * default helpers are not suitable when the core does not
4993 * send the READ0/PAGEPROG commands.
4994 */
4995 return (!ecc->read_page || !ecc->write_page ||
4996 !ecc->read_page_raw || !ecc->write_page_raw ||
4997 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4998 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4999 ecc->hwctl && ecc->calculate));
5000}
5001
William Juul52c07962007-10-31 13:53:06 +01005002/**
5003 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005004 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01005005 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005006 * This is the second phase of the normal nand_scan() function. It fills out
5007 * all the uninitialized function pointers with the defaults and scans for a
5008 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01005009 */
5010int nand_scan_tail(struct mtd_info *mtd)
5011{
5012 int i;
Scott Wood17fed142016-05-30 13:57:56 -05005013 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005014 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005015 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005016
Sergey Lapin3a38a552013-01-14 03:46:50 +00005017 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
5018 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
5019 !(chip->bbt_options & NAND_BBT_USE_FLASH));
5020
Marc Gonzalezc3a29852017-11-22 02:38:22 +09005021 if (invalid_ecc_page_accessors(chip)) {
5022 pr_err("Invalid ECC page accessors setup\n");
5023 return -EINVAL;
5024 }
5025
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005026 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005027 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005028 chip->buffers = nbuf;
5029 } else {
5030 if (!chip->buffers)
5031 return -ENOMEM;
5032 }
William Juul52c07962007-10-31 13:53:06 +01005033
5034 /* Set the internal oob buffer location, just after the page data */
5035 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
5036
5037 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005038 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01005039 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005040 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005041 switch (mtd->oobsize) {
Gregory CLEMENTe5b96312019-04-17 11:22:05 +02005042#ifndef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005043 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005044 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005045 break;
5046 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005047 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005048 break;
5049 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005050 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005051 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005052 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005053 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02005054 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01005055#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005056 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005057 pr_warn("No oob scheme defined for oobsize %d\n",
5058 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005059 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005060 }
5061 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005062
William Juul52c07962007-10-31 13:53:06 +01005063 if (!chip->write_page)
5064 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005065
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005066 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00005067 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01005068 * selected and we have 256 byte pagesize fallback to software ECC
5069 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005070
Heiko Schocherf5895d12014-06-24 10:10:04 +02005071 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005072 case NAND_ECC_HW_OOB_FIRST:
5073 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005074 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005075 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005076 BUG();
5077 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005078 if (!ecc->read_page)
5079 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04005080
William Juul52c07962007-10-31 13:53:06 +01005081 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00005082 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005083 if (!ecc->read_page)
5084 ecc->read_page = nand_read_page_hwecc;
5085 if (!ecc->write_page)
5086 ecc->write_page = nand_write_page_hwecc;
5087 if (!ecc->read_page_raw)
5088 ecc->read_page_raw = nand_read_page_raw;
5089 if (!ecc->write_page_raw)
5090 ecc->write_page_raw = nand_write_page_raw;
5091 if (!ecc->read_oob)
5092 ecc->read_oob = nand_read_oob_std;
5093 if (!ecc->write_oob)
5094 ecc->write_oob = nand_write_oob_std;
5095 if (!ecc->read_subpage)
5096 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05005097 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02005098 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005099
William Juul52c07962007-10-31 13:53:06 +01005100 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005101 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
5102 (!ecc->read_page ||
5103 ecc->read_page == nand_read_page_hwecc ||
5104 !ecc->write_page ||
5105 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05005106 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01005107 BUG();
5108 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00005109 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005110 if (!ecc->read_page)
5111 ecc->read_page = nand_read_page_syndrome;
5112 if (!ecc->write_page)
5113 ecc->write_page = nand_write_page_syndrome;
5114 if (!ecc->read_page_raw)
5115 ecc->read_page_raw = nand_read_page_raw_syndrome;
5116 if (!ecc->write_page_raw)
5117 ecc->write_page_raw = nand_write_page_raw_syndrome;
5118 if (!ecc->read_oob)
5119 ecc->read_oob = nand_read_oob_syndrome;
5120 if (!ecc->write_oob)
5121 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005122
Heiko Schocherf5895d12014-06-24 10:10:04 +02005123 if (mtd->writesize >= ecc->size) {
5124 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005125 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
5126 BUG();
5127 }
William Juul52c07962007-10-31 13:53:06 +01005128 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005129 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005130 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
5131 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005132 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005133
William Juul52c07962007-10-31 13:53:06 +01005134 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005135 ecc->calculate = nand_calculate_ecc;
5136 ecc->correct = nand_correct_data;
5137 ecc->read_page = nand_read_page_swecc;
5138 ecc->read_subpage = nand_read_subpage;
5139 ecc->write_page = nand_write_page_swecc;
5140 ecc->read_page_raw = nand_read_page_raw;
5141 ecc->write_page_raw = nand_write_page_raw;
5142 ecc->read_oob = nand_read_oob_std;
5143 ecc->write_oob = nand_write_oob_std;
5144 if (!ecc->size)
5145 ecc->size = 256;
5146 ecc->bytes = 3;
5147 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005148 break;
5149
Christian Hitz55f7bca2011-10-12 09:31:59 +02005150 case NAND_ECC_SOFT_BCH:
5151 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02005152 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005153 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02005154 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005155 ecc->calculate = nand_bch_calculate_ecc;
5156 ecc->correct = nand_bch_correct_data;
5157 ecc->read_page = nand_read_page_swecc;
5158 ecc->read_subpage = nand_read_subpage;
5159 ecc->write_page = nand_write_page_swecc;
5160 ecc->read_page_raw = nand_read_page_raw;
5161 ecc->write_page_raw = nand_write_page_raw;
5162 ecc->read_oob = nand_read_oob_std;
5163 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005164 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05005165 * Board driver should supply ecc.size and ecc.strength values
5166 * to select how many bits are correctable. Otherwise, default
5167 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02005168 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005169 if (!ecc->size && (mtd->oobsize >= 64)) {
5170 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05005171 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02005172 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05005173
5174 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005175 ecc->bytes = 0;
5176 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02005177 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005178 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005179 BUG();
5180 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02005181 break;
5182
William Juul52c07962007-10-31 13:53:06 +01005183 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05005184 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02005185 ecc->read_page = nand_read_page_raw;
5186 ecc->write_page = nand_write_page_raw;
5187 ecc->read_oob = nand_read_oob_std;
5188 ecc->read_page_raw = nand_read_page_raw;
5189 ecc->write_page_raw = nand_write_page_raw;
5190 ecc->write_oob = nand_write_oob_std;
5191 ecc->size = mtd->writesize;
5192 ecc->bytes = 0;
5193 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005194 break;
5195
5196 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02005197 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01005198 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005199 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005200
Sergey Lapin3a38a552013-01-14 03:46:50 +00005201 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005202 if (!ecc->read_oob_raw)
5203 ecc->read_oob_raw = ecc->read_oob;
5204 if (!ecc->write_oob_raw)
5205 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005206
William Juul52c07962007-10-31 13:53:06 +01005207 /*
5208 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00005209 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01005210 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05005211 mtd->oobavail = 0;
5212 if (ecc->layout) {
5213 for (i = 0; ecc->layout->oobfree[i].length; i++)
5214 mtd->oobavail += ecc->layout->oobfree[i].length;
5215 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005216
Scott Wood3ea94ed2015-06-26 19:03:26 -05005217 /* ECC sanity check: warn if it's too weak */
5218 if (!nand_ecc_strength_good(mtd))
5219 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5220 mtd->name);
5221
William Juul52c07962007-10-31 13:53:06 +01005222 /*
5223 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00005224 * mode.
William Juul52c07962007-10-31 13:53:06 +01005225 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005226 ecc->steps = mtd->writesize / ecc->size;
5227 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00005228 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01005229 BUG();
5230 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02005231 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005232
Sergey Lapin3a38a552013-01-14 03:46:50 +00005233 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005234 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5235 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01005236 case 2:
5237 mtd->subpage_sft = 1;
5238 break;
5239 case 4:
5240 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05005241 case 16:
William Juul52c07962007-10-31 13:53:06 +01005242 mtd->subpage_sft = 2;
5243 break;
5244 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005245 }
William Juul52c07962007-10-31 13:53:06 +01005246 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005247
William Juul52c07962007-10-31 13:53:06 +01005248 /* Initialize state */
5249 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005250
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005251 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01005252 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005253
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005254 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05005255 switch (ecc->mode) {
5256 case NAND_ECC_SOFT:
5257 case NAND_ECC_SOFT_BCH:
5258 if (chip->page_shift > 9)
5259 chip->options |= NAND_SUBPAGE_READ;
5260 break;
5261
5262 default:
5263 break;
5264 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00005265
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005266 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005267 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02005268 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5269 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005270 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005271 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005272 mtd->_read_oob = nand_read_oob;
5273 mtd->_write_oob = nand_write_oob;
5274 mtd->_sync = nand_sync;
5275 mtd->_lock = NULL;
5276 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03005277 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005278 mtd->_block_isbad = nand_block_isbad;
5279 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02005280 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005281
Sergey Lapin3a38a552013-01-14 03:46:50 +00005282 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02005283 mtd->ecclayout = ecc->layout;
5284 mtd->ecc_strength = ecc->strength;
5285 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00005286 /*
5287 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5288 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5289 * properly set.
5290 */
5291 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05005292 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01005293
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02005294 return 0;
William Juul52c07962007-10-31 13:53:06 +01005295}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005296EXPORT_SYMBOL(nand_scan_tail);
5297
William Juul52c07962007-10-31 13:53:06 +01005298/**
5299 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00005300 * @mtd: MTD device structure
5301 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01005302 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005303 * This fills out all the uninitialized function pointers with the defaults.
5304 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05005305 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01005306 */
5307int nand_scan(struct mtd_info *mtd, int maxchips)
5308{
5309 int ret;
5310
Lei Wen75bde942011-01-06 09:48:18 +08005311 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01005312 if (!ret)
5313 ret = nand_scan_tail(mtd);
5314 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005315}
Heiko Schocherf5895d12014-06-24 10:10:04 +02005316EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02005317
Heiko Schocherf5895d12014-06-24 10:10:04 +02005318MODULE_LICENSE("GPL");
5319MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5320MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5321MODULE_DESCRIPTION("Generic NAND flash driver code");