blob: 537c62424a1731a7f3f799eafb5b5a17ff017f2c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenke2211742002-11-02 23:30:20 +00002/*
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
4 * Steven J. Hill <sjhill@realitydiluted.com>
5 * Thomas Gleixner <tglx@linutronix.de>
wdenke2211742002-11-02 23:30:20 +00006 *
William Juulcfa460a2007-10-31 13:53:06 +01007 * Info:
8 * Contains standard defines and IDs for NAND flash devices
wdenke2211742002-11-02 23:30:20 +00009 *
William Juulcfa460a2007-10-31 13:53:06 +010010 * Changelog:
11 * See git changelog.
wdenke2211742002-11-02 23:30:20 +000012 */
Masahiro Yamada6ae39002017-11-30 13:45:24 +090013#ifndef __LINUX_MTD_RAWNAND_H
14#define __LINUX_MTD_RAWNAND_H
wdenke2211742002-11-02 23:30:20 +000015
Masahiro Yamadab5bf5cb2016-09-21 11:28:53 +090016#include <config.h>
William Juulcfa460a2007-10-31 13:53:06 +010017
Brian Norris892a8682019-03-15 15:14:30 +010018#include <dm/device.h>
Simon Glassc3dc39a2020-05-10 11:39:55 -060019#include <linux/bitops.h>
Masahiro Yamadab5bf5cb2016-09-21 11:28:53 +090020#include <linux/compat.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/flashchip.h>
23#include <linux/mtd/bbm.h>
Masahiro Yamadaba8c9ea2017-11-30 13:45:25 +090024#include <asm/cache.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010025
26struct mtd_info;
Jörg Krauseda37d092018-01-14 19:26:37 +010027struct nand_chip;
Lei Wen245eb902011-01-06 09:48:18 +080028struct nand_flash_dev;
Scott Woodceee07b2016-05-30 13:57:58 -050029struct device_node;
30
Jörg Krauseda37d092018-01-14 19:26:37 +010031/* Get the flash and manufacturer id and lookup if the type is supported. */
Michael Trimarchi6cda1dc2022-07-25 10:18:51 +020032int nand_detect(struct nand_chip *chip, int *maf_id, int *dev_id,
33 struct nand_flash_dev *type);
Jörg Krauseda37d092018-01-14 19:26:37 +010034
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010035/* Scan and identify a NAND device */
Sascha Hauer6a1ff762017-11-22 02:38:14 +090036int nand_scan(struct mtd_info *mtd, int max_chips);
Heiko Schocherff94bc42014-06-24 10:10:04 +020037/*
38 * Separate phases of nand_scan(), allowing board driver to intervene
39 * and override command or ECC setup according to flash type.
40 */
Sascha Hauer6a1ff762017-11-22 02:38:14 +090041int nand_scan_ident(struct mtd_info *mtd, int max_chips,
Heiko Schocherff94bc42014-06-24 10:10:04 +020042 struct nand_flash_dev *table);
Sascha Hauer6a1ff762017-11-22 02:38:14 +090043int nand_scan_tail(struct mtd_info *mtd);
William Juulcfa460a2007-10-31 13:53:06 +010044
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010045/* Free resources held by the NAND device */
Sascha Hauer6a1ff762017-11-22 02:38:14 +090046void nand_release(struct mtd_info *mtd);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010047
William Juulcfa460a2007-10-31 13:53:06 +010048/* Internal helper for board drivers which need to override command function */
Sascha Hauer6a1ff762017-11-22 02:38:14 +090049void nand_wait_ready(struct mtd_info *mtd);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010050
Christian Hitz2a8e0fc2011-10-12 09:32:02 +020051/*
52 * This constant declares the max. oobsize / page, which
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010053 * is supported now. If you add a chip with bigger oobsize/page
54 * adjust this accordingly.
55 */
Boris Brezillonc1fe6b52016-06-15 21:09:26 +020056#define NAND_MAX_OOBSIZE 1664
Siva Durga Prasad Paladugu2580a2a2015-04-28 18:16:03 +053057#define NAND_MAX_PAGESIZE 16384
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010058
59/*
60 * Constants for hardware specific CLE/ALE/NCE function
William Juulcfa460a2007-10-31 13:53:06 +010061 *
62 * These are bits which can be or'ed to set/clear multiple
63 * bits in one go.
64 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010065/* Select the chip by setting nCE to low */
William Juulcfa460a2007-10-31 13:53:06 +010066#define NAND_NCE 0x01
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010067/* Select the command latch by setting CLE to high */
William Juulcfa460a2007-10-31 13:53:06 +010068#define NAND_CLE 0x02
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010069/* Select the address latch by setting ALE to high */
William Juulcfa460a2007-10-31 13:53:06 +010070#define NAND_ALE 0x04
71
72#define NAND_CTRL_CLE (NAND_NCE | NAND_CLE)
73#define NAND_CTRL_ALE (NAND_NCE | NAND_ALE)
74#define NAND_CTRL_CHANGE 0x80
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010075
wdenke2211742002-11-02 23:30:20 +000076/*
77 * Standard NAND flash commands
78 */
79#define NAND_CMD_READ0 0
80#define NAND_CMD_READ1 1
William Juulcfa460a2007-10-31 13:53:06 +010081#define NAND_CMD_RNDOUT 5
wdenke2211742002-11-02 23:30:20 +000082#define NAND_CMD_PAGEPROG 0x10
83#define NAND_CMD_READOOB 0x50
84#define NAND_CMD_ERASE1 0x60
85#define NAND_CMD_STATUS 0x70
86#define NAND_CMD_SEQIN 0x80
William Juulcfa460a2007-10-31 13:53:06 +010087#define NAND_CMD_RNDIN 0x85
wdenke2211742002-11-02 23:30:20 +000088#define NAND_CMD_READID 0x90
89#define NAND_CMD_ERASE2 0xd0
Christian Hitz2a8e0fc2011-10-12 09:32:02 +020090#define NAND_CMD_PARAM 0xec
Sergey Lapindfe64e22013-01-14 03:46:50 +000091#define NAND_CMD_GET_FEATURES 0xee
92#define NAND_CMD_SET_FEATURES 0xef
wdenke2211742002-11-02 23:30:20 +000093#define NAND_CMD_RESET 0xff
94
Christian Hitz2a8e0fc2011-10-12 09:32:02 +020095#define NAND_CMD_LOCK 0x2a
96#define NAND_CMD_UNLOCK1 0x23
97#define NAND_CMD_UNLOCK2 0x24
98
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010099/* Extended commands for large page devices */
100#define NAND_CMD_READSTART 0x30
William Juulcfa460a2007-10-31 13:53:06 +0100101#define NAND_CMD_RNDOUTSTART 0xE0
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100102#define NAND_CMD_CACHEDPROG 0x15
103
William Juulcfa460a2007-10-31 13:53:06 +0100104/* Extended commands for AG-AND device */
105/*
106 * Note: the command for NAND_CMD_DEPLETE1 is really 0x00 but
107 * there is no way to distinguish that from NAND_CMD_READ0
108 * until the remaining sequence of commands has been completed
109 * so add a high order bit and mask it off in the command.
110 */
111#define NAND_CMD_DEPLETE1 0x100
112#define NAND_CMD_DEPLETE2 0x38
113#define NAND_CMD_STATUS_MULTI 0x71
114#define NAND_CMD_STATUS_ERROR 0x72
115/* multi-bank error status (banks 0-3) */
116#define NAND_CMD_STATUS_ERROR0 0x73
117#define NAND_CMD_STATUS_ERROR1 0x74
118#define NAND_CMD_STATUS_ERROR2 0x75
119#define NAND_CMD_STATUS_ERROR3 0x76
120#define NAND_CMD_STATUS_RESET 0x7f
121#define NAND_CMD_STATUS_CLEAR 0xff
122
123#define NAND_CMD_NONE -1
124
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100125/* Status bits */
126#define NAND_STATUS_FAIL 0x01
127#define NAND_STATUS_FAIL_N1 0x02
128#define NAND_STATUS_TRUE_READY 0x20
129#define NAND_STATUS_READY 0x40
130#define NAND_STATUS_WP 0x80
131
Boris Brezillon52cde352017-11-22 02:38:28 +0900132#define NAND_DATA_IFACE_CHECK_ONLY -1
133
wdenke2211742002-11-02 23:30:20 +0000134/*
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100135 * Constants for ECC_MODES
136 */
William Juulcfa460a2007-10-31 13:53:06 +0100137typedef enum {
138 NAND_ECC_NONE,
139 NAND_ECC_SOFT,
140 NAND_ECC_HW,
141 NAND_ECC_HW_SYNDROME,
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -0400142 NAND_ECC_HW_OOB_FIRST,
Christian Hitz4c6de852011-10-12 09:31:59 +0200143 NAND_ECC_SOFT_BCH,
William Juulcfa460a2007-10-31 13:53:06 +0100144} nand_ecc_modes_t;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100145
Rafał Miłecki323a73a2018-07-10 11:48:08 +0200146enum nand_ecc_algo {
147 NAND_ECC_UNKNOWN,
148 NAND_ECC_HAMMING,
149 NAND_ECC_BCH,
150};
151
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100152/*
153 * Constants for Hardware ECC
William Juulcfa460a2007-10-31 13:53:06 +0100154 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100155/* Reset Hardware ECC for read */
156#define NAND_ECC_READ 0
157/* Reset Hardware ECC for write */
158#define NAND_ECC_WRITE 1
Sergey Lapindfe64e22013-01-14 03:46:50 +0000159/* Enable Hardware ECC before syndrome is read back from flash */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100160#define NAND_ECC_READSYN 2
161
Scott Woodceee07b2016-05-30 13:57:58 -0500162/*
163 * Enable generic NAND 'page erased' check. This check is only done when
164 * ecc.correct() returns -EBADMSG.
165 * Set this flag if your implementation does not fix bitflips in erased
166 * pages and you want to rely on the default implementation.
167 */
168#define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
Boris Brezillon19d30de2017-11-22 02:38:13 +0900169#define NAND_ECC_MAXIMIZE BIT(1)
Marc Gonzalez1fb87de2017-11-22 02:38:22 +0900170/*
171 * If your controller already sends the required NAND commands when
172 * reading or writing a page, then the framework is not supposed to
173 * send READ0 and SEQIN/PAGEPROG respectively.
174 */
175#define NAND_ECC_CUSTOM_PAGE_ACCESS BIT(2)
Scott Woodceee07b2016-05-30 13:57:58 -0500176
William Juulcfa460a2007-10-31 13:53:06 +0100177/* Bit mask for flags passed to do_nand_read_ecc */
178#define NAND_GET_DEVICE 0x80
179
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200180/*
181 * Option constants for bizarre disfunctionality and real
182 * features.
183 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000184/* Buswidth is 16 bit */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100185#define NAND_BUSWIDTH_16 0x00000002
186/* Device supports partial programming without padding */
187#define NAND_NO_PADDING 0x00000004
188/* Chip has cache program function */
189#define NAND_CACHEPRG 0x00000008
190/* Chip has copy back function */
191#define NAND_COPYBACK 0x00000010
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200192/*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200193 * Chip requires ready check on read (for auto-incremented sequential read).
194 * True only for small page devices; large page devices do not support
195 * autoincrement.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200196 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200197#define NAND_NEED_READRDY 0x00000100
198
William Juulcfa460a2007-10-31 13:53:06 +0100199/* Chip does not allow subpage writes */
200#define NAND_NO_SUBPAGE_WRITE 0x00000200
201
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200202/* Device is one of 'new' xD cards that expose fake nand command set */
203#define NAND_BROKEN_XD 0x00000400
204
205/* Device behaves just like nand, but is readonly */
206#define NAND_ROM 0x00000800
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100207
Joe Hershbergerc788ecf2012-11-05 06:46:31 +0000208/* Device supports subpage reads */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200209#define NAND_SUBPAGE_READ 0x00001000
Joe Hershbergerc788ecf2012-11-05 06:46:31 +0000210
Scott Woodceee07b2016-05-30 13:57:58 -0500211/*
212 * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
213 * patterns.
214 */
215#define NAND_NEED_SCRAMBLING 0x00002000
216
Masahiro Yamadae6001372017-11-22 02:38:31 +0900217/* Device needs 3rd row address cycle */
218#define NAND_ROW_ADDR_3 0x00004000
219
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100220/* Options valid for Samsung large page devices */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200221#define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100222
223/* Macros to identify the above */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100224#define NAND_HAS_CACHEPROG(chip) ((chip->options & NAND_CACHEPRG))
Joe Hershbergerc788ecf2012-11-05 06:46:31 +0000225#define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
Marc Gonzalez1fb87de2017-11-22 02:38:22 +0900226#define NAND_HAS_SUBPAGE_WRITE(chip) !((chip)->options & NAND_NO_SUBPAGE_WRITE)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100227
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100228/* Non chip related options */
William Juulcfa460a2007-10-31 13:53:06 +0100229/* This option skips the bbt scan during initialization. */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000230#define NAND_SKIP_BBTSCAN 0x00010000
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200231/*
232 * This option is defined if the board driver allocates its own buffers
233 * (e.g. because it needs them DMA-coherent).
234 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000235#define NAND_OWN_BUFFERS 0x00020000
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200236/* Chip may not exist, so silence any errors in scan */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000237#define NAND_SCAN_SILENT_NODEV 0x00040000
Heiko Schocherff94bc42014-06-24 10:10:04 +0200238/*
239 * Autodetect nand buswidth with readid/onfi.
240 * This suppose the driver will configure the hardware in 8 bits mode
241 * when calling nand_scan_ident, and update its configuration
242 * before calling nand_scan_tail.
243 */
244#define NAND_BUSWIDTH_AUTO 0x00080000
Scott Woodceee07b2016-05-30 13:57:58 -0500245/*
246 * This option could be defined by controller drivers to protect against
247 * kmap'ed, vmalloc'ed highmem buffers being passed from upper layers
248 */
249#define NAND_USE_BOUNCE_BUFFER 0x00100000
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200250
Alexander Dahl29b7efa2024-03-20 10:02:10 +0100251/*
252 * Do not try to tweak the timings at runtime. This is needed when the
253 * controller initializes the timings on itself or when it relies on
254 * configuration done by the bootloader.
255 */
256#define NAND_KEEP_TIMINGS 0x00800000
257
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100258/* Options set by nand scan */
Scott Woodfb494542012-02-20 14:50:39 -0600259/* bbt has already been read */
260#define NAND_BBT_SCANNED 0x40000000
William Juulcfa460a2007-10-31 13:53:06 +0100261/* Nand scan has allocated controller struct */
262#define NAND_CONTROLLER_ALLOC 0x80000000
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100263
William Juulcfa460a2007-10-31 13:53:06 +0100264/* Cell info constants */
265#define NAND_CI_CHIPNR_MSK 0x03
266#define NAND_CI_CELLTYPE_MSK 0x0C
Heiko Schocherff94bc42014-06-24 10:10:04 +0200267#define NAND_CI_CELLTYPE_SHIFT 2
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100268
Heiko Schocherff94bc42014-06-24 10:10:04 +0200269/* ONFI features */
270#define ONFI_FEATURE_16_BIT_BUS (1 << 0)
271#define ONFI_FEATURE_EXT_PARAM_PAGE (1 << 7)
272
Sergey Lapindfe64e22013-01-14 03:46:50 +0000273/* ONFI timing mode, used in both asynchronous and synchronous mode */
274#define ONFI_TIMING_MODE_0 (1 << 0)
275#define ONFI_TIMING_MODE_1 (1 << 1)
276#define ONFI_TIMING_MODE_2 (1 << 2)
277#define ONFI_TIMING_MODE_3 (1 << 3)
278#define ONFI_TIMING_MODE_4 (1 << 4)
279#define ONFI_TIMING_MODE_5 (1 << 5)
280#define ONFI_TIMING_MODE_UNKNOWN (1 << 6)
281
282/* ONFI feature address */
283#define ONFI_FEATURE_ADDR_TIMING_MODE 0x1
284
Heiko Schocherff94bc42014-06-24 10:10:04 +0200285/* Vendor-specific feature address (Micron) */
286#define ONFI_FEATURE_ADDR_READ_RETRY 0x89
287
Sergey Lapindfe64e22013-01-14 03:46:50 +0000288/* ONFI subfeature parameters length */
289#define ONFI_SUBFEATURE_PARAM_LEN 4
290
Heiko Schocherff94bc42014-06-24 10:10:04 +0200291/* ONFI optional commands SET/GET FEATURES supported? */
292#define ONFI_OPT_CMD_SET_GET_FEATURES (1 << 2)
293
Florian Fainelli0272c712011-02-25 00:01:34 +0000294struct nand_onfi_params {
295 /* rev info and features block */
296 /* 'O' 'N' 'F' 'I' */
297 u8 sig[4];
298 __le16 revision;
299 __le16 features;
300 __le16 opt_cmd;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200301 u8 reserved0[2];
302 __le16 ext_param_page_length; /* since ONFI 2.1 */
303 u8 num_of_param_pages; /* since ONFI 2.1 */
304 u8 reserved1[17];
Florian Fainelli0272c712011-02-25 00:01:34 +0000305
306 /* manufacturer information block */
307 char manufacturer[12];
308 char model[20];
309 u8 jedec_id;
310 __le16 date_code;
311 u8 reserved2[13];
312
313 /* memory organization block */
314 __le32 byte_per_page;
315 __le16 spare_bytes_per_page;
316 __le32 data_bytes_per_ppage;
317 __le16 spare_bytes_per_ppage;
318 __le32 pages_per_block;
319 __le32 blocks_per_lun;
320 u8 lun_count;
321 u8 addr_cycles;
322 u8 bits_per_cell;
323 __le16 bb_per_lun;
324 __le16 block_endurance;
325 u8 guaranteed_good_blocks;
326 __le16 guaranteed_block_endurance;
327 u8 programs_per_page;
328 u8 ppage_attr;
329 u8 ecc_bits;
330 u8 interleaved_bits;
331 u8 interleaved_ops;
332 u8 reserved3[13];
333
334 /* electrical parameter block */
335 u8 io_pin_capacitance_max;
336 __le16 async_timing_mode;
337 __le16 program_cache_timing_mode;
338 __le16 t_prog;
339 __le16 t_bers;
340 __le16 t_r;
341 __le16 t_ccs;
342 __le16 src_sync_timing_mode;
Scott Woodceee07b2016-05-30 13:57:58 -0500343 u8 src_ssync_features;
Florian Fainelli0272c712011-02-25 00:01:34 +0000344 __le16 clk_pin_capacitance_typ;
345 __le16 io_pin_capacitance_typ;
346 __le16 input_pin_capacitance_typ;
347 u8 input_pin_capacitance_max;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200348 u8 driver_strength_support;
Florian Fainelli0272c712011-02-25 00:01:34 +0000349 __le16 t_int_r;
Scott Woodceee07b2016-05-30 13:57:58 -0500350 __le16 t_adl;
351 u8 reserved4[8];
Florian Fainelli0272c712011-02-25 00:01:34 +0000352
353 /* vendor */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200354 __le16 vendor_revision;
355 u8 vendor[88];
Florian Fainelli0272c712011-02-25 00:01:34 +0000356
357 __le16 crc;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200358} __packed;
Florian Fainelli0272c712011-02-25 00:01:34 +0000359
360#define ONFI_CRC_BASE 0x4F4E
361
Heiko Schocherff94bc42014-06-24 10:10:04 +0200362/* Extended ECC information Block Definition (since ONFI 2.1) */
363struct onfi_ext_ecc_info {
364 u8 ecc_bits;
365 u8 codeword_size;
366 __le16 bb_per_lun;
367 __le16 block_endurance;
368 u8 reserved[2];
369} __packed;
370
371#define ONFI_SECTION_TYPE_0 0 /* Unused section. */
372#define ONFI_SECTION_TYPE_1 1 /* for additional sections. */
373#define ONFI_SECTION_TYPE_2 2 /* for ECC information. */
374struct onfi_ext_section {
375 u8 type;
376 u8 length;
377} __packed;
378
379#define ONFI_EXT_SECTION_MAX 8
380
381/* Extended Parameter Page Definition (since ONFI 2.1) */
382struct onfi_ext_param_page {
383 __le16 crc;
384 u8 sig[4]; /* 'E' 'P' 'P' 'S' */
385 u8 reserved0[10];
386 struct onfi_ext_section sections[ONFI_EXT_SECTION_MAX];
387
388 /*
389 * The actual size of the Extended Parameter Page is in
390 * @ext_param_page_length of nand_onfi_params{}.
391 * The following are the variable length sections.
392 * So we do not add any fields below. Please see the ONFI spec.
393 */
394} __packed;
395
Heiko Schocher4e67c572014-07-15 16:08:43 +0200396struct jedec_ecc_info {
397 u8 ecc_bits;
398 u8 codeword_size;
399 __le16 bb_per_lun;
400 __le16 block_endurance;
401 u8 reserved[2];
402} __packed;
403
404/* JEDEC features */
405#define JEDEC_FEATURE_16_BIT_BUS (1 << 0)
406
407struct nand_jedec_params {
408 /* rev info and features block */
409 /* 'J' 'E' 'S' 'D' */
410 u8 sig[4];
411 __le16 revision;
412 __le16 features;
413 u8 opt_cmd[3];
414 __le16 sec_cmd;
415 u8 num_of_param_pages;
416 u8 reserved0[18];
417
418 /* manufacturer information block */
419 char manufacturer[12];
420 char model[20];
421 u8 jedec_id[6];
422 u8 reserved1[10];
423
424 /* memory organization block */
425 __le32 byte_per_page;
426 __le16 spare_bytes_per_page;
427 u8 reserved2[6];
428 __le32 pages_per_block;
429 __le32 blocks_per_lun;
430 u8 lun_count;
431 u8 addr_cycles;
432 u8 bits_per_cell;
433 u8 programs_per_page;
434 u8 multi_plane_addr;
435 u8 multi_plane_op_attr;
436 u8 reserved3[38];
437
438 /* electrical parameter block */
439 __le16 async_sdr_speed_grade;
440 __le16 toggle_ddr_speed_grade;
441 __le16 sync_ddr_speed_grade;
442 u8 async_sdr_features;
443 u8 toggle_ddr_features;
444 u8 sync_ddr_features;
445 __le16 t_prog;
446 __le16 t_bers;
447 __le16 t_r;
448 __le16 t_r_multi_plane;
449 __le16 t_ccs;
450 __le16 io_pin_capacitance_typ;
451 __le16 input_pin_capacitance_typ;
452 __le16 clk_pin_capacitance_typ;
453 u8 driver_strength_support;
Scott Woodceee07b2016-05-30 13:57:58 -0500454 __le16 t_adl;
Heiko Schocher4e67c572014-07-15 16:08:43 +0200455 u8 reserved4[36];
456
457 /* ECC and endurance block */
458 u8 guaranteed_good_blocks;
459 __le16 guaranteed_block_endurance;
460 struct jedec_ecc_info ecc_info[4];
461 u8 reserved5[29];
462
463 /* reserved */
464 u8 reserved6[148];
465
466 /* vendor */
467 __le16 vendor_rev_num;
468 u8 reserved7[88];
469
470 /* CRC for Parameter Page */
471 __le16 crc;
472} __packed;
473
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100474/**
William Juulcfa460a2007-10-31 13:53:06 +0100475 * struct nand_hw_control - Control structure for hardware controller (e.g ECC generator) shared among independent devices
476 * @lock: protection lock
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100477 * @active: the mtd device which holds the controller currently
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200478 * @wq: wait queue to sleep on if a NAND operation is in
479 * progress used instead of the per chip wait queue
480 * when a hw controller is available.
wdenkdc7c9a12003-03-26 06:55:25 +0000481 */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100482struct nand_hw_control {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200483 spinlock_t lock;
484 struct nand_chip *active;
William Juulcfa460a2007-10-31 13:53:06 +0100485};
486
Marc Gonzalez5df42b02019-03-15 15:14:31 +0100487static inline void nand_hw_control_init(struct nand_hw_control *nfc)
488{
489 nfc->active = NULL;
490 spin_lock_init(&nfc->lock);
491 init_waitqueue_head(&nfc->wq);
492}
493
Michael Trimarchibd876032022-07-20 18:22:05 +0200494/* The maximum expected count of bytes in the NAND ID sequence */
495#define NAND_MAX_ID_LEN 8
496
497/**
498 * struct nand_id - NAND id structure
499 * @data: buffer containing the id bytes.
500 * @len: ID length.
501 */
502struct nand_id {
503 u8 data[NAND_MAX_ID_LEN];
504 int len;
505};
506
William Juulcfa460a2007-10-31 13:53:06 +0100507/**
Masahiro Yamada470c29d2017-11-22 02:38:29 +0900508 * struct nand_ecc_step_info - ECC step information of ECC engine
509 * @stepsize: data bytes per ECC step
510 * @strengths: array of supported strengths
511 * @nstrengths: number of supported strengths
512 */
513struct nand_ecc_step_info {
514 int stepsize;
515 const int *strengths;
516 int nstrengths;
517};
518
519/**
520 * struct nand_ecc_caps - capability of ECC engine
521 * @stepinfos: array of ECC step information
522 * @nstepinfos: number of ECC step information
523 * @calc_ecc_bytes: driver's hook to calculate ECC bytes per step
524 */
525struct nand_ecc_caps {
526 const struct nand_ecc_step_info *stepinfos;
527 int nstepinfos;
528 int (*calc_ecc_bytes)(int step_size, int strength);
529};
530
Masahiro Yamadaa01549b2017-11-22 02:38:30 +0900531/* a shorthand to generate struct nand_ecc_caps with only one ECC stepsize */
532#define NAND_ECC_CAPS_SINGLE(__name, __calc, __step, ...) \
533static const int __name##_strengths[] = { __VA_ARGS__ }; \
534static const struct nand_ecc_step_info __name##_stepinfo = { \
535 .stepsize = __step, \
536 .strengths = __name##_strengths, \
537 .nstrengths = ARRAY_SIZE(__name##_strengths), \
538}; \
539static const struct nand_ecc_caps __name = { \
540 .stepinfos = &__name##_stepinfo, \
541 .nstepinfos = 1, \
542 .calc_ecc_bytes = __calc, \
543}
544
Masahiro Yamada470c29d2017-11-22 02:38:29 +0900545/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000546 * struct nand_ecc_ctrl - Control structure for ECC
547 * @mode: ECC mode
Rafał Miłecki323a73a2018-07-10 11:48:08 +0200548 * @algo: ECC algorithm
Sergey Lapindfe64e22013-01-14 03:46:50 +0000549 * @steps: number of ECC steps per page
550 * @size: data bytes per ECC step
551 * @bytes: ECC bytes per step
552 * @strength: max number of correctible bits per ECC step
553 * @total: total number of ECC bytes per page
554 * @prepad: padding information for syndrome based ECC generators
555 * @postpad: padding information for syndrome based ECC generators
Scott Woodceee07b2016-05-30 13:57:58 -0500556 * @options: ECC specific options (see NAND_ECC_XXX flags defined above)
William Juulcfa460a2007-10-31 13:53:06 +0100557 * @layout: ECC layout control struct pointer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000558 * @priv: pointer to private ECC control data
559 * @hwctl: function to control hardware ECC generator. Must only
William Juulcfa460a2007-10-31 13:53:06 +0100560 * be provided if an hardware ECC is available
Sergey Lapindfe64e22013-01-14 03:46:50 +0000561 * @calculate: function for ECC calculation or readback from ECC hardware
Scott Woodceee07b2016-05-30 13:57:58 -0500562 * @correct: function for ECC correction, matching to ECC generator (sw/hw).
563 * Should return a positive number representing the number of
564 * corrected bitflips, -EBADMSG if the number of bitflips exceed
565 * ECC strength, or any other error code if the error is not
566 * directly related to correction.
567 * If -EBADMSG is returned the input buffers should be left
568 * untouched.
Scott Woodd3963722015-06-26 19:03:26 -0500569 * @read_page_raw: function to read a raw page without ECC. This function
570 * should hide the specific layout used by the ECC
571 * controller and always return contiguous in-band and
572 * out-of-band data even if they're not stored
573 * contiguously on the NAND chip (e.g.
574 * NAND_ECC_HW_SYNDROME interleaves in-band and
575 * out-of-band data).
576 * @write_page_raw: function to write a raw page without ECC. This function
577 * should hide the specific layout used by the ECC
578 * controller and consider the passed data as contiguous
579 * in-band and out-of-band data. ECC controller is
580 * responsible for doing the appropriate transformations
581 * to adapt to its specific layout (e.g.
582 * NAND_ECC_HW_SYNDROME interleaves in-band and
583 * out-of-band data).
Sergey Lapindfe64e22013-01-14 03:46:50 +0000584 * @read_page: function to read a page according to the ECC generator
585 * requirements; returns maximum number of bitflips corrected in
586 * any single ECC step, 0 if bitflips uncorrectable, -EIO hw error
587 * @read_subpage: function to read parts of the page covered by ECC;
588 * returns same as read_page()
Heiko Schocherff94bc42014-06-24 10:10:04 +0200589 * @write_subpage: function to write parts of the page covered by ECC.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000590 * @write_page: function to write a page according to the ECC generator
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200591 * requirements.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000592 * @write_oob_raw: function to write chip OOB data without ECC
593 * @read_oob_raw: function to read chip OOB data without ECC
William Juulcfa460a2007-10-31 13:53:06 +0100594 * @read_oob: function to read chip OOB data
595 * @write_oob: function to write chip OOB data
596 */
597struct nand_ecc_ctrl {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200598 nand_ecc_modes_t mode;
Rafał Miłecki323a73a2018-07-10 11:48:08 +0200599 enum nand_ecc_algo algo;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200600 int steps;
601 int size;
602 int bytes;
603 int total;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000604 int strength;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200605 int prepad;
606 int postpad;
Scott Woodceee07b2016-05-30 13:57:58 -0500607 unsigned int options;
William Juulcfa460a2007-10-31 13:53:06 +0100608 struct nand_ecclayout *layout;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200609 void *priv;
610 void (*hwctl)(struct mtd_info *mtd, int mode);
611 int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
612 uint8_t *ecc_code);
613 int (*correct)(struct mtd_info *mtd, uint8_t *dat, uint8_t *read_ecc,
614 uint8_t *calc_ecc);
615 int (*read_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000616 uint8_t *buf, int oob_required, int page);
617 int (*write_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -0500618 const uint8_t *buf, int oob_required, int page);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200619 int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000620 uint8_t *buf, int oob_required, int page);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200621 int (*read_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher4e67c572014-07-15 16:08:43 +0200622 uint32_t offs, uint32_t len, uint8_t *buf, int page);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200623 int (*write_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
624 uint32_t offset, uint32_t data_len,
Scott Wood81c77252016-05-30 13:57:57 -0500625 const uint8_t *data_buf, int oob_required, int page);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000626 int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -0500627 const uint8_t *buf, int oob_required, int page);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000628 int (*write_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
629 int page);
630 int (*read_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
631 int page);
632 int (*read_oob)(struct mtd_info *mtd, struct nand_chip *chip, int page);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200633 int (*write_oob)(struct mtd_info *mtd, struct nand_chip *chip,
634 int page);
William Juulcfa460a2007-10-31 13:53:06 +0100635};
636
Marc Gonzalez1fb87de2017-11-22 02:38:22 +0900637static inline int nand_standard_page_accessors(struct nand_ecc_ctrl *ecc)
638{
639 return !(ecc->options & NAND_ECC_CUSTOM_PAGE_ACCESS);
640}
641
William Juulcfa460a2007-10-31 13:53:06 +0100642/**
643 * struct nand_buffers - buffer structure for read/write
Heiko Schocher4e67c572014-07-15 16:08:43 +0200644 * @ecccalc: buffer pointer for calculated ECC, size is oobsize.
645 * @ecccode: buffer pointer for ECC read from flash, size is oobsize.
646 * @databuf: buffer pointer for data, size is (page size + oobsize).
William Juulcfa460a2007-10-31 13:53:06 +0100647 *
648 * Do not change the order of buffers. databuf and oobrbuf must be in
649 * consecutive order.
650 */
651struct nand_buffers {
Simon Glassb5725952012-07-29 20:53:25 +0000652 uint8_t ecccalc[ALIGN(NAND_MAX_OOBSIZE, ARCH_DMA_MINALIGN)];
653 uint8_t ecccode[ALIGN(NAND_MAX_OOBSIZE, ARCH_DMA_MINALIGN)];
654 uint8_t databuf[ALIGN(NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE,
655 ARCH_DMA_MINALIGN)];
William Juulcfa460a2007-10-31 13:53:06 +0100656};
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100657
658/**
Sascha Hauer01042492017-11-22 02:38:16 +0900659 * struct nand_sdr_timings - SDR NAND chip timings
660 *
661 * This struct defines the timing requirements of a SDR NAND chip.
662 * These information can be found in every NAND datasheets and the timings
663 * meaning are described in the ONFI specifications:
664 * www.onfi.org/~/media/ONFI/specs/onfi_3_1_spec.pdf (chapter 4.15 Timing
665 * Parameters)
666 *
667 * All these timings are expressed in picoseconds.
668 *
Boris Brezillon6f84b262017-11-22 02:38:21 +0900669 * @tBERS_max: Block erase time
670 * @tCCS_min: Change column setup time
671 * @tPROG_max: Page program time
672 * @tR_max: Page read time
Sascha Hauer01042492017-11-22 02:38:16 +0900673 * @tALH_min: ALE hold time
674 * @tADL_min: ALE to data loading time
675 * @tALS_min: ALE setup time
676 * @tAR_min: ALE to RE# delay
677 * @tCEA_max: CE# access time
678 * @tCEH_min: CE# high hold time
679 * @tCH_min: CE# hold time
680 * @tCHZ_max: CE# high to output hi-Z
681 * @tCLH_min: CLE hold time
682 * @tCLR_min: CLE to RE# delay
683 * @tCLS_min: CLE setup time
684 * @tCOH_min: CE# high to output hold
685 * @tCS_min: CE# setup time
686 * @tDH_min: Data hold time
687 * @tDS_min: Data setup time
688 * @tFEAT_max: Busy time for Set Features and Get Features
689 * @tIR_min: Output hi-Z to RE# low
690 * @tITC_max: Interface and Timing Mode Change time
691 * @tRC_min: RE# cycle time
692 * @tREA_max: RE# access time
693 * @tREH_min: RE# high hold time
694 * @tRHOH_min: RE# high to output hold
695 * @tRHW_min: RE# high to WE# low
696 * @tRHZ_max: RE# high to output hi-Z
697 * @tRLOH_min: RE# low to output hold
698 * @tRP_min: RE# pulse width
699 * @tRR_min: Ready to RE# low (data only)
700 * @tRST_max: Device reset time, measured from the falling edge of R/B# to the
701 * rising edge of R/B#.
702 * @tWB_max: WE# high to SR[6] low
703 * @tWC_min: WE# cycle time
704 * @tWH_min: WE# high hold time
705 * @tWHR_min: WE# high to RE# low
706 * @tWP_min: WE# pulse width
707 * @tWW_min: WP# transition to WE# low
708 */
709struct nand_sdr_timings {
Boris Brezillon6f84b262017-11-22 02:38:21 +0900710 u64 tBERS_max;
711 u32 tCCS_min;
712 u64 tPROG_max;
713 u64 tR_max;
Sascha Hauer01042492017-11-22 02:38:16 +0900714 u32 tALH_min;
715 u32 tADL_min;
716 u32 tALS_min;
717 u32 tAR_min;
718 u32 tCEA_max;
719 u32 tCEH_min;
720 u32 tCH_min;
721 u32 tCHZ_max;
722 u32 tCLH_min;
723 u32 tCLR_min;
724 u32 tCLS_min;
725 u32 tCOH_min;
726 u32 tCS_min;
727 u32 tDH_min;
728 u32 tDS_min;
729 u32 tFEAT_max;
730 u32 tIR_min;
731 u32 tITC_max;
732 u32 tRC_min;
733 u32 tREA_max;
734 u32 tREH_min;
735 u32 tRHOH_min;
736 u32 tRHW_min;
737 u32 tRHZ_max;
738 u32 tRLOH_min;
739 u32 tRP_min;
740 u32 tRR_min;
741 u64 tRST_max;
742 u32 tWB_max;
743 u32 tWC_min;
744 u32 tWH_min;
745 u32 tWHR_min;
746 u32 tWP_min;
747 u32 tWW_min;
748};
749
750/**
751 * enum nand_data_interface_type - NAND interface timing type
752 * @NAND_SDR_IFACE: Single Data Rate interface
753 */
754enum nand_data_interface_type {
755 NAND_SDR_IFACE,
756};
757
758/**
759 * struct nand_data_interface - NAND interface timing
760 * @type: type of the timing
761 * @timings: The timing, type according to @type
762 */
763struct nand_data_interface {
764 enum nand_data_interface_type type;
765 union {
766 struct nand_sdr_timings sdr;
767 } timings;
768};
769
770/**
771 * nand_get_sdr_timings - get SDR timing from data interface
772 * @conf: The data interface
773 */
774static inline const struct nand_sdr_timings *
775nand_get_sdr_timings(const struct nand_data_interface *conf)
776{
777 if (conf->type != NAND_SDR_IFACE)
778 return ERR_PTR(-EINVAL);
779
780 return &conf->timings.sdr;
781}
782
783/**
Michael Trimarchib8cd2df2022-07-20 18:22:06 +0200784 * struct nand_manufacturer_ops - NAND Manufacturer operations
785 * @detect: detect the NAND memory organization and capabilities
786 * @init: initialize all vendor specific fields (like the ->read_retry()
787 * implementation) if any.
788 */
789struct nand_manufacturer_ops {
790 void (*detect)(struct nand_chip *chip);
791 int (*init)(struct nand_chip *chip);
792};
793
794/**
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100795 * struct nand_chip - NAND Private Flash Chip Data
Scott Woodceee07b2016-05-30 13:57:58 -0500796 * @mtd: MTD device registered to the MTD framework
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200797 * @IO_ADDR_R: [BOARDSPECIFIC] address to read the 8 I/O lines of the
798 * flash device
799 * @IO_ADDR_W: [BOARDSPECIFIC] address to write the 8 I/O lines of the
800 * flash device.
Brian Norris42bd19c2016-06-15 21:09:22 +0200801 * @flash_node: [BOARDSPECIFIC] device node describing this instance
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100802 * @read_byte: [REPLACEABLE] read one byte from the chip
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100803 * @read_word: [REPLACEABLE] read one word from the chip
Heiko Schocherff94bc42014-06-24 10:10:04 +0200804 * @write_byte: [REPLACEABLE] write a single byte to the chip on the
805 * low 8 I/O lines
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100806 * @write_buf: [REPLACEABLE] write data from the buffer to the chip
807 * @read_buf: [REPLACEABLE] read data from the chip into the buffer
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100808 * @select_chip: [REPLACEABLE] select chip nr
Heiko Schocherff94bc42014-06-24 10:10:04 +0200809 * @block_bad: [REPLACEABLE] check if a block is bad, using OOB markers
810 * @block_markbad: [REPLACEABLE] mark a block bad
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200811 * @cmd_ctrl: [BOARDSPECIFIC] hardwarespecific function for controlling
William Juulcfa460a2007-10-31 13:53:06 +0100812 * ALE/CLE/nCE. Also used to write command and address
Sergey Lapindfe64e22013-01-14 03:46:50 +0000813 * @dev_ready: [BOARDSPECIFIC] hardwarespecific function for accessing
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200814 * device ready/busy line. If set to NULL no access to
815 * ready/busy is available and the ready/busy information
816 * is read from the chip status register.
817 * @cmdfunc: [REPLACEABLE] hardwarespecific function for writing
818 * commands to the chip.
819 * @waitfunc: [REPLACEABLE] hardwarespecific function for wait on
820 * ready.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200821 * @setup_read_retry: [FLASHSPECIFIC] flash (vendor) specific function for
822 * setting the read-retry mode. Mostly needed for MLC NAND.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000823 * @ecc: [BOARDSPECIFIC] ECC control structure
William Juulcfa460a2007-10-31 13:53:06 +0100824 * @buffers: buffer structure for read/write
Masahiro Yamada436fb2b2017-11-22 02:38:27 +0900825 * @buf_align: minimum buffer alignment required by a platform
William Juulcfa460a2007-10-31 13:53:06 +0100826 * @hwcontrol: platform-specific hardware control structure
Scott Woodd3963722015-06-26 19:03:26 -0500827 * @erase: [REPLACEABLE] erase function
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100828 * @scan_bbt: [REPLACEABLE] function to scan bad block table
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200829 * @chip_delay: [BOARDSPECIFIC] chip dependent delay for transferring
830 * data from array to read regs (tR).
Wolfgang Denkb9365a22006-07-21 11:56:05 +0200831 * @state: [INTERN] the current state of the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000832 * @oob_poi: "poison value buffer," used for laying out OOB data
833 * before writing
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200834 * @page_shift: [INTERN] number of address bits in a page (column
835 * address bits).
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100836 * @phys_erase_shift: [INTERN] number of address bits in a physical eraseblock
837 * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
838 * @chip_shift: [INTERN] number of address bits in one chip
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200839 * @options: [BOARDSPECIFIC] various chip options. They can partly
840 * be set to inform nand_scan about special functionality.
841 * See the defines for further explanation.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000842 * @bbt_options: [INTERN] bad block specific options. All options used
843 * here must come from bbm.h. By default, these options
844 * will be copied to the appropriate nand_bbt_descr's.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200845 * @badblockpos: [INTERN] position of the bad block marker in the oob
846 * area.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000847 * @badblockbits: [INTERN] minimum number of set bits in a good block's
848 * bad block marker position; i.e., BBM == 11110111b is
849 * not bad when badblockbits == 7
Heiko Schocherff94bc42014-06-24 10:10:04 +0200850 * @bits_per_cell: [INTERN] number of bits per cell. i.e., 1 means SLC.
851 * @ecc_strength_ds: [INTERN] ECC correctability from the datasheet.
852 * Minimum amount of bit errors per @ecc_step_ds guaranteed
853 * to be correctable. If unknown, set to zero.
854 * @ecc_step_ds: [INTERN] ECC step required by the @ecc_strength_ds,
855 * also from the datasheet. It is the recommended ECC step
856 * size, if known; if unknown, set to zero.
Scott Woodd3963722015-06-26 19:03:26 -0500857 * @onfi_timing_mode_default: [INTERN] default ONFI timing mode. This field is
Boris Brezillon27c47922017-11-22 02:38:19 +0900858 * set to the actually used ONFI mode if the chip is
859 * ONFI compliant or deduced from the datasheet if
860 * the NAND chip is not ONFI compliant.
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100861 * @numchips: [INTERN] number of physical chips
862 * @chipsize: [INTERN] the size of one chip for multichip arrays
863 * @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200864 * @pagebuf: [INTERN] holds the pagenumber which is currently in
865 * data_buf.
Paul Burton40462e52013-09-04 15:16:56 +0100866 * @pagebuf_bitflips: [INTERN] holds the bitflip count for the page which is
867 * currently in data_buf.
William Juulcfa460a2007-10-31 13:53:06 +0100868 * @subpagesize: [INTERN] holds the subpagesize
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200869 * @onfi_version: [INTERN] holds the chip ONFI version (BCD encoded),
870 * non 0 if ONFI supported.
Heiko Schocher4e67c572014-07-15 16:08:43 +0200871 * @jedec_version: [INTERN] holds the chip JEDEC version (BCD encoded),
872 * non 0 if JEDEC supported.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200873 * @onfi_params: [INTERN] holds the ONFI page parameter when ONFI is
874 * supported, 0 otherwise.
Heiko Schocher4e67c572014-07-15 16:08:43 +0200875 * @jedec_params: [INTERN] holds the JEDEC parameter page when JEDEC is
876 * supported, 0 otherwise.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200877 * @read_retries: [INTERN] the number of read retry modes supported
878 * @onfi_set_features: [REPLACEABLE] set the features for ONFI nand
879 * @onfi_get_features: [REPLACEABLE] get the features for ONFI nand
Boris Brezillon52cde352017-11-22 02:38:28 +0900880 * @setup_data_interface: [OPTIONAL] setup the data interface and timing. If
881 * chipnr is set to %NAND_DATA_IFACE_CHECK_ONLY this
882 * means the configuration should not be applied but
883 * only checked.
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100884 * @bbt: [INTERN] bad block table pointer
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200885 * @bbt_td: [REPLACEABLE] bad block table descriptor for flash
886 * lookup.
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100887 * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200888 * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
889 * bad block scan.
890 * @controller: [REPLACEABLE] a pointer to a hardware controller
Sergey Lapindfe64e22013-01-14 03:46:50 +0000891 * structure which is shared among multiple independent
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200892 * devices.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000893 * @priv: [OPTIONAL] pointer to private chip data
William Juulcfa460a2007-10-31 13:53:06 +0100894 * @write_page: [REPLACEABLE] High-level page write function
Michael Trimarchib8cd2df2022-07-20 18:22:06 +0200895 * @manufacturer: [INTERN] Contains manufacturer information
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100896 */
wdenkdc7c9a12003-03-26 06:55:25 +0000897
898struct nand_chip {
Scott Woodb616d9b2016-05-30 13:57:55 -0500899 struct mtd_info mtd;
Michael Trimarchibd876032022-07-20 18:22:05 +0200900 struct nand_id id;
901
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200902 void __iomem *IO_ADDR_R;
903 void __iomem *IO_ADDR_W;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100904
Patrice Chotarda70c3f92021-09-13 16:25:53 +0200905 ofnode flash_node;
Brian Norris42bd19c2016-06-15 21:09:22 +0200906
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200907 uint8_t (*read_byte)(struct mtd_info *mtd);
908 u16 (*read_word)(struct mtd_info *mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200909 void (*write_byte)(struct mtd_info *mtd, uint8_t byte);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200910 void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
911 void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200912 void (*select_chip)(struct mtd_info *mtd, int chip);
Scott Woodceee07b2016-05-30 13:57:58 -0500913 int (*block_bad)(struct mtd_info *mtd, loff_t ofs);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200914 int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
915 void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200916 int (*dev_ready)(struct mtd_info *mtd);
917 void (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column,
918 int page_addr);
919 int(*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);
Scott Woodd3963722015-06-26 19:03:26 -0500920 int (*erase)(struct mtd_info *mtd, int page);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200921 int (*scan_bbt)(struct mtd_info *mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200922 int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200923 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon4d755962017-11-22 02:38:24 +0900924 int oob_required, int page, int raw);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000925 int (*onfi_set_features)(struct mtd_info *mtd, struct nand_chip *chip,
926 int feature_addr, uint8_t *subfeature_para);
927 int (*onfi_get_features)(struct mtd_info *mtd, struct nand_chip *chip,
928 int feature_addr, uint8_t *subfeature_para);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200929 int (*setup_read_retry)(struct mtd_info *mtd, int retry_mode);
Boris Brezillon52cde352017-11-22 02:38:28 +0900930 int (*setup_data_interface)(struct mtd_info *mtd, int chipnr,
931 const struct nand_data_interface *conf);
Boris Brezillon27c47922017-11-22 02:38:19 +0900932
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200933 int chip_delay;
934 unsigned int options;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000935 unsigned int bbt_options;
William Juulcfa460a2007-10-31 13:53:06 +0100936
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200937 int page_shift;
938 int phys_erase_shift;
939 int bbt_erase_shift;
940 int chip_shift;
941 int numchips;
942 uint64_t chipsize;
943 int pagemask;
944 int pagebuf;
Paul Burton40462e52013-09-04 15:16:56 +0100945 unsigned int pagebuf_bitflips;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200946 int subpagesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200947 uint8_t bits_per_cell;
948 uint16_t ecc_strength_ds;
949 uint16_t ecc_step_ds;
Scott Woodd3963722015-06-26 19:03:26 -0500950 int onfi_timing_mode_default;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200951 int badblockpos;
952 int badblockbits;
953
954 int onfi_version;
Heiko Schocher4e67c572014-07-15 16:08:43 +0200955 int jedec_version;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200956 struct nand_onfi_params onfi_params;
Heiko Schocher4e67c572014-07-15 16:08:43 +0200957 struct nand_jedec_params jedec_params;
Wolfgang Denk0a50b3c2021-09-27 17:42:38 +0200958
Boris Brezillon27c47922017-11-22 02:38:19 +0900959 struct nand_data_interface *data_interface;
960
Heiko Schocherff94bc42014-06-24 10:10:04 +0200961 int read_retries;
962
963 flstate_t state;
William Juulcfa460a2007-10-31 13:53:06 +0100964
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200965 uint8_t *oob_poi;
966 struct nand_hw_control *controller;
967 struct nand_ecclayout *ecclayout;
William Juulcfa460a2007-10-31 13:53:06 +0100968
969 struct nand_ecc_ctrl ecc;
970 struct nand_buffers *buffers;
Masahiro Yamada436fb2b2017-11-22 02:38:27 +0900971 unsigned long buf_align;
William Juulcfa460a2007-10-31 13:53:06 +0100972 struct nand_hw_control hwcontrol;
973
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200974 uint8_t *bbt;
975 struct nand_bbt_descr *bbt_td;
976 struct nand_bbt_descr *bbt_md;
William Juulcfa460a2007-10-31 13:53:06 +0100977
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200978 struct nand_bbt_descr *badblock_pattern;
William Juulcfa460a2007-10-31 13:53:06 +0100979
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200980 void *priv;
Michael Trimarchib8cd2df2022-07-20 18:22:06 +0200981
982 struct {
Michael Trimarchi8a67acf2022-07-26 18:33:11 +0200983 const struct nand_manufacturer *desc;
Michael Trimarchib8cd2df2022-07-20 18:22:06 +0200984 void *priv;
985 } manufacturer;
wdenkdc7c9a12003-03-26 06:55:25 +0000986};
987
Brian Norris892a8682019-03-15 15:14:30 +0100988static inline void nand_set_flash_node(struct nand_chip *chip,
989 ofnode node)
990{
Patrice Chotarda70c3f92021-09-13 16:25:53 +0200991 chip->flash_node = node;
Brian Norris892a8682019-03-15 15:14:30 +0100992}
993
994static inline ofnode nand_get_flash_node(struct nand_chip *chip)
995{
Patrice Chotarda70c3f92021-09-13 16:25:53 +0200996 return chip->flash_node;
Brian Norris892a8682019-03-15 15:14:30 +0100997}
998
Scott Wood17cb4b82016-05-30 13:57:56 -0500999static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
1000{
1001 return container_of(mtd, struct nand_chip, mtd);
1002}
1003
1004static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
1005{
1006 return &chip->mtd;
1007}
1008
1009static inline void *nand_get_controller_data(struct nand_chip *chip)
1010{
1011 return chip->priv;
1012}
1013
1014static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
1015{
1016 chip->priv = priv;
1017}
1018
Michael Trimarchib8cd2df2022-07-20 18:22:06 +02001019static inline void nand_set_manufacturer_data(struct nand_chip *chip,
1020 void *priv)
1021{
1022 chip->manufacturer.priv = priv;
1023}
1024
1025static inline void *nand_get_manufacturer_data(struct nand_chip *chip)
1026{
1027 return chip->manufacturer.priv;
1028}
1029
wdenkdc7c9a12003-03-26 06:55:25 +00001030/*
wdenke2211742002-11-02 23:30:20 +00001031 * NAND Flash Manufacturer ID Codes
1032 */
1033#define NAND_MFR_TOSHIBA 0x98
1034#define NAND_MFR_SAMSUNG 0xec
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001035#define NAND_MFR_FUJITSU 0x04
1036#define NAND_MFR_NATIONAL 0x8f
1037#define NAND_MFR_RENESAS 0x07
1038#define NAND_MFR_STMICRO 0x20
William Juulcfa460a2007-10-31 13:53:06 +01001039#define NAND_MFR_HYNIX 0xad
Ulf Samuelsson7ebb4472007-05-24 12:12:47 +02001040#define NAND_MFR_MICRON 0x2c
Scott Woodc45912d2008-10-24 16:20:43 -05001041#define NAND_MFR_AMD 0x01
Sergey Lapindfe64e22013-01-14 03:46:50 +00001042#define NAND_MFR_MACRONIX 0xc2
1043#define NAND_MFR_EON 0x92
Heiko Schocherff94bc42014-06-24 10:10:04 +02001044#define NAND_MFR_SANDISK 0x45
1045#define NAND_MFR_INTEL 0x89
Scott Woodd3963722015-06-26 19:03:26 -05001046#define NAND_MFR_ATO 0x9b
Heiko Schocherff94bc42014-06-24 10:10:04 +02001047
1048/* The maximum expected count of bytes in the NAND ID sequence */
1049#define NAND_MAX_ID_LEN 8
1050
1051/*
1052 * A helper for defining older NAND chips where the second ID byte fully
1053 * defined the chip, including the geometry (chip size, eraseblock size, page
1054 * size). All these chips have 512 bytes NAND page size.
1055 */
1056#define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts) \
1057 { .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
1058 .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
1059
1060/*
1061 * A helper for defining newer chips which report their page size and
1062 * eraseblock size via the extended ID bytes.
1063 *
1064 * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
1065 * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
1066 * device ID now only represented a particular total chip size (and voltage,
1067 * buswidth), and the page size, eraseblock size, and OOB size could vary while
1068 * using the same device ID.
1069 */
1070#define EXTENDED_ID_NAND(nm, devid, chipsz, opts) \
1071 { .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
1072 .options = (opts) }
1073
1074#define NAND_ECC_INFO(_strength, _step) \
1075 { .strength_ds = (_strength), .step_ds = (_step) }
1076#define NAND_ECC_STRENGTH(type) ((type)->ecc.strength_ds)
1077#define NAND_ECC_STEP(type) ((type)->ecc.step_ds)
wdenke2211742002-11-02 23:30:20 +00001078
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001079/**
1080 * struct nand_flash_dev - NAND Flash Device ID Structure
Heiko Schocherff94bc42014-06-24 10:10:04 +02001081 * @name: a human-readable name of the NAND chip
1082 * @dev_id: the device ID (the second byte of the full chip ID array)
1083 * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
1084 * memory address as @id[0])
1085 * @dev_id: device ID part of the full chip ID array (refers the same memory
1086 * address as @id[1])
1087 * @id: full device ID array
1088 * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
1089 * well as the eraseblock size) is determined from the extended NAND
1090 * chip ID array)
1091 * @chipsize: total chip size in MiB
1092 * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
1093 * @options: stores various chip bit options
1094 * @id_len: The valid length of the @id.
1095 * @oobsize: OOB size
Scott Woodd3963722015-06-26 19:03:26 -05001096 * @ecc: ECC correctability and step information from the datasheet.
Heiko Schocherff94bc42014-06-24 10:10:04 +02001097 * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
1098 * @ecc_strength_ds in nand_chip{}.
1099 * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
1100 * @ecc_step_ds in nand_chip{}, also from the datasheet.
1101 * For example, the "4bit ECC for each 512Byte" can be set with
1102 * NAND_ECC_INFO(4, 512).
Scott Woodd3963722015-06-26 19:03:26 -05001103 * @onfi_timing_mode_default: the default ONFI timing mode entered after a NAND
1104 * reset. Should be deduced from timings described
1105 * in the datasheet.
1106 *
wdenke2211742002-11-02 23:30:20 +00001107 */
1108struct nand_flash_dev {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001109 char *name;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001110 union {
1111 struct {
1112 uint8_t mfr_id;
1113 uint8_t dev_id;
1114 };
1115 uint8_t id[NAND_MAX_ID_LEN];
1116 };
1117 unsigned int pagesize;
1118 unsigned int chipsize;
1119 unsigned int erasesize;
1120 unsigned int options;
1121 uint16_t id_len;
1122 uint16_t oobsize;
1123 struct {
1124 uint16_t strength_ds;
1125 uint16_t step_ds;
1126 } ecc;
Scott Woodd3963722015-06-26 19:03:26 -05001127 int onfi_timing_mode_default;
wdenke2211742002-11-02 23:30:20 +00001128};
1129
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001130/**
Michael Trimarchi8a67acf2022-07-26 18:33:11 +02001131 * struct nand_manufacturer - NAND Flash Manufacturer ID Structure
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001132 * @name: Manufacturer name
Wolfgang Denkb9365a22006-07-21 11:56:05 +02001133 * @id: manufacturer ID code of device.
Michael Trimarchib8cd2df2022-07-20 18:22:06 +02001134 * @ops: manufacturer operations
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001135*/
Michael Trimarchi8a67acf2022-07-26 18:33:11 +02001136struct nand_manufacturer {
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001137 int id;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001138 char *name;
Michael Trimarchib8cd2df2022-07-20 18:22:06 +02001139 const struct nand_manufacturer_ops *ops;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001140};
1141
Heiko Schocherff94bc42014-06-24 10:10:04 +02001142extern struct nand_flash_dev nand_flash_ids[];
Michael Trimarchi8a67acf2022-07-26 18:33:11 +02001143extern struct nand_manufacturer nand_manuf_ids[];
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001144
Michael Trimarchi3de2cdb2022-07-20 18:22:11 +02001145extern const struct nand_manufacturer_ops toshiba_nand_manuf_ops;
Michael Trimarchia1286a12022-07-20 18:22:09 +02001146extern const struct nand_manufacturer_ops samsung_nand_manuf_ops;
Michael Trimarchi2811ed22022-07-20 18:22:10 +02001147extern const struct nand_manufacturer_ops hynix_nand_manuf_ops;
Michael Trimarchic596e012022-07-20 18:22:12 +02001148extern const struct nand_manufacturer_ops micron_nand_manuf_ops;
Michael Trimarchibd6adff2022-07-20 18:22:13 +02001149extern const struct nand_manufacturer_ops amd_nand_manuf_ops;
Michael Trimarchic7f7cce2022-07-20 18:22:14 +02001150extern const struct nand_manufacturer_ops macronix_nand_manuf_ops;
Michael Trimarchia1286a12022-07-20 18:22:09 +02001151
Sascha Hauer6a1ff762017-11-22 02:38:14 +09001152int nand_default_bbt(struct mtd_info *mtd);
1153int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs);
1154int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs);
1155int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt);
1156int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
William Juulcfa460a2007-10-31 13:53:06 +01001157 int allowbbt);
Sascha Hauer6a1ff762017-11-22 02:38:14 +09001158int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001159 size_t *retlen, uint8_t *buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001160
wdenkdc7c9a12003-03-26 06:55:25 +00001161/*
1162* Constants for oob configuration
1163*/
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001164#define NAND_SMALL_BADBLOCK_POS 5
1165#define NAND_LARGE_BADBLOCK_POS 0
wdenkdc7c9a12003-03-26 06:55:25 +00001166
William Juulcfa460a2007-10-31 13:53:06 +01001167/**
1168 * struct platform_nand_chip - chip level device structure
1169 * @nr_chips: max. number of chips to scan for
1170 * @chip_offset: chip number offset
1171 * @nr_partitions: number of partitions pointed to by partitions (or zero)
1172 * @partitions: mtd partition list
1173 * @chip_delay: R/B delay value in us
1174 * @options: Option flags, e.g. 16bit buswidth
Sergey Lapindfe64e22013-01-14 03:46:50 +00001175 * @bbt_options: BBT option flags, e.g. NAND_BBT_USE_FLASH
William Juulcfa460a2007-10-31 13:53:06 +01001176 * @part_probe_types: NULL-terminated array of probe types
William Juulcfa460a2007-10-31 13:53:06 +01001177 */
1178struct platform_nand_chip {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001179 int nr_chips;
1180 int chip_offset;
1181 int nr_partitions;
1182 struct mtd_partition *partitions;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001183 int chip_delay;
1184 unsigned int options;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001185 unsigned int bbt_options;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001186 const char **part_probe_types;
William Juulcfa460a2007-10-31 13:53:06 +01001187};
1188
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001189/* Keep gcc happy */
1190struct platform_device;
1191
William Juulcfa460a2007-10-31 13:53:06 +01001192/**
1193 * struct platform_nand_ctrl - controller level device structure
Heiko Schocherff94bc42014-06-24 10:10:04 +02001194 * @probe: platform specific function to probe/setup hardware
1195 * @remove: platform specific function to remove/teardown hardware
William Juulcfa460a2007-10-31 13:53:06 +01001196 * @hwcontrol: platform specific hardware control structure
1197 * @dev_ready: platform specific function to read ready/busy pin
1198 * @select_chip: platform specific chip select function
1199 * @cmd_ctrl: platform specific function for controlling
1200 * ALE/CLE/nCE. Also used to write command and address
Heiko Schocherff94bc42014-06-24 10:10:04 +02001201 * @write_buf: platform specific function for write buffer
1202 * @read_buf: platform specific function for read buffer
1203 * @read_byte: platform specific function to read one byte from chip
William Juulcfa460a2007-10-31 13:53:06 +01001204 * @priv: private data to transport driver specific settings
1205 *
1206 * All fields are optional and depend on the hardware driver requirements
1207 */
1208struct platform_nand_ctrl {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001209 int (*probe)(struct platform_device *pdev);
1210 void (*remove)(struct platform_device *pdev);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001211 void (*hwcontrol)(struct mtd_info *mtd, int cmd);
1212 int (*dev_ready)(struct mtd_info *mtd);
1213 void (*select_chip)(struct mtd_info *mtd, int chip);
1214 void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001215 void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
1216 void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001217 unsigned char (*read_byte)(struct mtd_info *mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001218 void *priv;
William Juulcfa460a2007-10-31 13:53:06 +01001219};
1220
1221/**
1222 * struct platform_nand_data - container structure for platform-specific data
1223 * @chip: chip level chip structure
1224 * @ctrl: controller level device structure
1225 */
1226struct platform_nand_data {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001227 struct platform_nand_chip chip;
1228 struct platform_nand_ctrl ctrl;
William Juulcfa460a2007-10-31 13:53:06 +01001229};
1230
Heiko Schocherff94bc42014-06-24 10:10:04 +02001231#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
1232/* return the supported features. */
1233static inline int onfi_feature(struct nand_chip *chip)
1234{
1235 return chip->onfi_version ? le16_to_cpu(chip->onfi_params.features) : 0;
1236}
Simon Schwarz82645f82011-10-31 06:34:44 +00001237
Sergey Lapindfe64e22013-01-14 03:46:50 +00001238/* return the supported asynchronous timing mode. */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001239static inline int onfi_get_async_timing_mode(struct nand_chip *chip)
1240{
1241 if (!chip->onfi_version)
1242 return ONFI_TIMING_MODE_UNKNOWN;
1243 return le16_to_cpu(chip->onfi_params.async_timing_mode);
1244}
1245
1246/* return the supported synchronous timing mode. */
1247static inline int onfi_get_sync_timing_mode(struct nand_chip *chip)
1248{
1249 if (!chip->onfi_version)
1250 return ONFI_TIMING_MODE_UNKNOWN;
1251 return le16_to_cpu(chip->onfi_params.src_sync_timing_mode);
1252}
Masahiro Yamada4c61f792017-11-22 02:38:12 +09001253#else
1254static inline int onfi_feature(struct nand_chip *chip)
1255{
1256 return 0;
1257}
1258
1259static inline int onfi_get_async_timing_mode(struct nand_chip *chip)
1260{
1261 return ONFI_TIMING_MODE_UNKNOWN;
1262}
1263
1264static inline int onfi_get_sync_timing_mode(struct nand_chip *chip)
1265{
1266 return ONFI_TIMING_MODE_UNKNOWN;
1267}
Sergey Lapindfe64e22013-01-14 03:46:50 +00001268#endif
1269
Sascha Hauer46deff52017-11-22 02:38:17 +09001270int onfi_init_data_interface(struct nand_chip *chip,
1271 struct nand_data_interface *iface,
1272 enum nand_data_interface_type type,
1273 int timing_mode);
1274
Heiko Schocherff94bc42014-06-24 10:10:04 +02001275/*
1276 * Check if it is a SLC nand.
1277 * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
1278 * We do not distinguish the MLC and TLC now.
1279 */
1280static inline bool nand_is_slc(struct nand_chip *chip)
1281{
1282 return chip->bits_per_cell == 1;
1283}
1284
Brian Norris27ce9e42014-05-06 00:46:17 +05301285/**
1286 * Check if the opcode's address should be sent only on the lower 8 bits
1287 * @command: opcode to check
1288 */
1289static inline int nand_opcode_8bits(unsigned int command)
1290{
David Mosberger6e1899e2014-05-06 00:46:18 +05301291 switch (command) {
1292 case NAND_CMD_READID:
1293 case NAND_CMD_PARAM:
1294 case NAND_CMD_GET_FEATURES:
1295 case NAND_CMD_SET_FEATURES:
1296 return 1;
1297 default:
1298 break;
1299 }
1300 return 0;
Brian Norris27ce9e42014-05-06 00:46:17 +05301301}
1302
Heiko Schocher4e67c572014-07-15 16:08:43 +02001303/* return the supported JEDEC features. */
1304static inline int jedec_feature(struct nand_chip *chip)
1305{
1306 return chip->jedec_version ? le16_to_cpu(chip->jedec_params.features)
1307 : 0;
1308}
1309
Heiko Schocherff94bc42014-06-24 10:10:04 +02001310/* Standard NAND functions from nand_base.c */
1311void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
1312void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len);
1313void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len);
1314void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len);
1315uint8_t nand_read_byte(struct mtd_info *mtd);
Scott Woodd3963722015-06-26 19:03:26 -05001316
Scott Woodd3963722015-06-26 19:03:26 -05001317/* get timing characteristics from ONFI timing mode. */
1318const struct nand_sdr_timings *onfi_async_timing_mode_to_sdr_timings(int mode);
Sascha Hauerb893e832017-11-22 02:38:18 +09001319/* get data interface from ONFI timing mode 0, used after reset. */
1320const struct nand_data_interface *nand_get_default_data_interface(void);
Scott Woodceee07b2016-05-30 13:57:58 -05001321
1322int nand_check_erased_ecc_chunk(void *data, int datalen,
1323 void *ecc, int ecclen,
1324 void *extraoob, int extraooblen,
1325 int threshold);
Sascha Hauer1728eb52017-11-22 02:38:15 +09001326
Masahiro Yamada470c29d2017-11-22 02:38:29 +09001327int nand_check_ecc_caps(struct nand_chip *chip,
1328 const struct nand_ecc_caps *caps, int oobavail);
1329
1330int nand_match_ecc_req(struct nand_chip *chip,
1331 const struct nand_ecc_caps *caps, int oobavail);
1332
1333int nand_maximize_ecc(struct nand_chip *chip,
1334 const struct nand_ecc_caps *caps, int oobavail);
1335
Sascha Hauer1728eb52017-11-22 02:38:15 +09001336/* Reset and initialize a NAND device */
Boris Brezillon3d841b32017-11-22 02:38:20 +09001337int nand_reset(struct nand_chip *chip, int chipnr);
Boris Brezillon9db29b32019-03-15 15:14:32 +01001338
1339/* NAND operation helpers */
1340int nand_reset_op(struct nand_chip *chip);
1341int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1342 unsigned int len);
1343int nand_status_op(struct nand_chip *chip, u8 *status);
1344int nand_exit_status_op(struct nand_chip *chip);
1345int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock);
1346int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1347 unsigned int offset_in_page, void *buf, unsigned int len);
1348int nand_change_read_column_op(struct nand_chip *chip,
1349 unsigned int offset_in_page, void *buf,
1350 unsigned int len, bool force_8bit);
1351int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1352 unsigned int offset_in_page, void *buf, unsigned int len);
1353int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1354 unsigned int offset_in_page, const void *buf,
1355 unsigned int len);
1356int nand_prog_page_end_op(struct nand_chip *chip);
1357int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1358 unsigned int offset_in_page, const void *buf,
1359 unsigned int len);
1360int nand_change_write_column_op(struct nand_chip *chip,
1361 unsigned int offset_in_page, const void *buf,
1362 unsigned int len, bool force_8bit);
1363int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1364 bool force_8bit);
1365int nand_write_data_op(struct nand_chip *chip, const void *buf,
1366 unsigned int len, bool force_8bit);
1367
Michael Trimarchibded7d82022-07-20 18:22:08 +02001368/* Default extended ID decoding function */
1369void nand_decode_ext_id(struct nand_chip *chip);
1370
Masahiro Yamada6ae39002017-11-30 13:45:24 +09001371#endif /* __LINUX_MTD_RAWNAND_H */