OpenOCD
nand/core.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de> *
5  * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
6  * *
7  * Partially based on linux/include/linux/mtd/nand.h *
8  * Copyright (C) 2000 David Woodhouse <dwmw2@mvhi.com> *
9  * Copyright (C) 2000 Steven J. Hill <sjhill@realitydiluted.com> *
10  * Copyright (C) 2000 Thomas Gleixner <tglx@linutronix.de> *
11  ***************************************************************************/
12 
13 #ifndef OPENOCD_FLASH_NAND_CORE_H
14 #define OPENOCD_FLASH_NAND_CORE_H
15 
16 #include <flash/common.h>
17 
21 struct nand_block {
23  uint32_t offset;
24 
26  uint32_t size;
27 
29  int is_erased;
30 
32  int is_bad;
33 };
34 
35 struct nand_oobfree {
36  int offset;
37  int length;
38 };
39 
41  int eccbytes;
42  int eccpos[64];
43  int oobavail;
44  struct nand_oobfree oobfree[2];
45 };
46 
47 struct nand_device {
48  const char *name;
49  struct target *target;
53  struct nand_info *device;
54  int bus_width;
56  int page_size;
58  bool use_raw;
60  struct nand_block *blocks;
61  struct nand_device *next;
62 };
63 
64 /* NAND Flash Manufacturer ID Codes
65  */
66 enum {
75 };
76 
78  int id;
79  const char *name;
80 };
81 
82 struct nand_info {
83  int mfr_id;
84  int id;
85  int page_size;
86  int chip_size;
88  int options;
89  const char *name;
90 };
91 
92 /* Option constants for bizarre dysfunctionality and real features
93  */
94 enum {
95  /* Chip can not auto increment pages */
96  NAND_NO_AUTOINCR = 0x00000001,
97 
98  /* Buswitdh is 16 bit */
99  NAND_BUSWIDTH_16 = 0x00000002,
100 
101  /* Device supports partial programming without padding */
102  NAND_NO_PADDING = 0x00000004,
103 
104  /* Chip has cache program function */
105  NAND_CACHEPRG = 0x00000008,
106 
107  /* Chip has copy back function */
108  NAND_COPYBACK = 0x00000010,
109 
110  /* AND Chip which has 4 banks and a confusing page / block
111  * assignment. See Renesas datasheet for further information */
112  NAND_IS_AND = 0x00000020,
113 
114  /* Chip has a array of 4 pages which can be read without
115  * additional ready /busy waits */
116  NAND_4PAGE_ARRAY = 0x00000040,
117 
118  /* Chip requires that BBT is periodically rewritten to prevent
119  * bits from adjacent blocks from 'leaking' in altering data.
120  * This happens with the Renesas AG-AND chips, possibly others. */
121  BBT_AUTO_REFRESH = 0x00000080,
122 
123  /* Chip does not require ready check on read. True
124  * for all large page devices, as they do not support
125  * autoincrement.*/
126  NAND_NO_READRDY = 0x00000100,
127 
128  /* Options valid for Samsung large page devices */
130 
131  /* Options for new chips with large page size. The pagesize and the
132  * erasesize is determined from the extended id bytes
133  */
136 };
137 
138 enum {
139  /* Standard NAND flash commands */
153 
154  /* Extended commands for large page devices */
158 };
159 
160 /* Status bits */
161 enum {
167 };
168 
169 /* OOB (spare) data formats */
171  NAND_OOB_NONE = 0x0, /* no OOB data at all */
172  NAND_OOB_RAW = 0x1, /* raw OOB data (16 bytes for 512b page sizes, 64 bytes for
173  *2048b page sizes) */
174  NAND_OOB_ONLY = 0x2, /* only OOB data */
175  NAND_OOB_SW_ECC = 0x10, /* when writing, use SW ECC (as opposed to no ECC) */
176  NAND_OOB_HW_ECC = 0x20, /* when writing, use HW ECC (as opposed to no ECC) */
177  NAND_OOB_SW_ECC_KW = 0x40, /* when writing, use Marvell's Kirkwood bootrom format */
178  NAND_OOB_JFFS2 = 0x100, /* when writing, use JFFS2 OOB layout */
179  NAND_OOB_YAFFS2 = 0x100,/* when writing, use YAFFS2 OOB layout */
180 };
181 
182 
183 struct nand_device *get_nand_device_by_num(int num);
184 
185 int nand_page_command(struct nand_device *nand, uint32_t page,
186  uint8_t cmd, bool oob_only);
187 
188 int nand_read_data_page(struct nand_device *nand, uint8_t *data, uint32_t size);
189 int nand_write_data_page(struct nand_device *nand,
190  uint8_t *data, uint32_t size);
191 
192 int nand_write_finish(struct nand_device *nand);
193 
194 int nand_read_page_raw(struct nand_device *nand, uint32_t page,
195  uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
196 int nand_write_page_raw(struct nand_device *nand, uint32_t page,
197  uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
198 
199 int nand_read_status(struct nand_device *nand, uint8_t *status);
200 
201 int nand_calculate_ecc(struct nand_device *nand,
202  const uint8_t *dat, uint8_t *ecc_code);
203 int nand_calculate_ecc_kw(struct nand_device *nand,
204  const uint8_t *dat, uint8_t *ecc_code);
205 
206 int nand_register_commands(struct command_context *cmd_ctx);
207 
209 COMMAND_HELPER(nand_command_get_device, unsigned name_index,
210  struct nand_device **nand);
211 
212 
213 #define ERROR_NAND_DEVICE_INVALID (-1100)
214 #define ERROR_NAND_OPERATION_FAILED (-1101)
215 #define ERROR_NAND_OPERATION_TIMEOUT (-1102)
216 #define ERROR_NAND_OPERATION_NOT_SUPPORTED (-1103)
217 #define ERROR_NAND_DEVICE_NOT_PROBED (-1104)
218 #define ERROR_NAND_ERROR_CORRECTION_FAILED (-1105)
219 #define ERROR_NAND_NO_BUFFER (-1106)
220 
221 #endif /* OPENOCD_FLASH_NAND_CORE_H */
int nand_write_data_page(struct nand_device *nand, uint8_t *data, uint32_t size)
int nand_read_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
@ NAND_MFR_NATIONAL
Definition: nand/core.h:70
@ NAND_MFR_RENESAS
Definition: nand/core.h:71
@ NAND_MFR_STMICRO
Definition: nand/core.h:72
@ NAND_MFR_MICRON
Definition: nand/core.h:74
@ NAND_MFR_SAMSUNG
Definition: nand/core.h:68
@ NAND_MFR_FUJITSU
Definition: nand/core.h:69
@ NAND_MFR_TOSHIBA
Definition: nand/core.h:67
@ NAND_MFR_HYNIX
Definition: nand/core.h:73
int nand_calculate_ecc_kw(struct nand_device *nand, const uint8_t *dat, uint8_t *ecc_code)
Definition: ecc_kw.c:93
COMMAND_HELPER(nand_command_get_device, unsigned name_index, struct nand_device **nand)
helper for parsing a nand device command argument string
int nand_read_data_page(struct nand_device *nand, uint8_t *data, uint32_t size)
int nand_calculate_ecc(struct nand_device *nand, const uint8_t *dat, uint8_t *ecc_code)
Definition: ecc.c:48
@ NAND_CMD_CACHEDPROG
Definition: nand/core.h:157
@ NAND_CMD_RNDOUTSTART
Definition: nand/core.h:156
@ NAND_CMD_SEQIN
Definition: nand/core.h:148
@ NAND_CMD_STATUS_MULTI
Definition: nand/core.h:147
@ NAND_CMD_READSTART
Definition: nand/core.h:155
@ NAND_CMD_READOOB
Definition: nand/core.h:144
@ NAND_CMD_RNDIN
Definition: nand/core.h:149
@ NAND_CMD_READ0
Definition: nand/core.h:140
@ NAND_CMD_READID
Definition: nand/core.h:150
@ NAND_CMD_STATUS
Definition: nand/core.h:146
@ NAND_CMD_READ1
Definition: nand/core.h:141
@ NAND_CMD_ERASE2
Definition: nand/core.h:151
@ NAND_CMD_RNDOUT
Definition: nand/core.h:142
@ NAND_CMD_RESET
Definition: nand/core.h:152
@ NAND_CMD_PAGEPROG
Definition: nand/core.h:143
@ NAND_CMD_ERASE1
Definition: nand/core.h:145
int nand_read_status(struct nand_device *nand, uint8_t *status)
@ NAND_STATUS_WP
Definition: nand/core.h:166
@ NAND_STATUS_TRUE_READY
Definition: nand/core.h:164
@ NAND_STATUS_FAIL
Definition: nand/core.h:162
@ NAND_STATUS_READY
Definition: nand/core.h:165
@ NAND_STATUS_FAIL_N1
Definition: nand/core.h:163
int nand_write_finish(struct nand_device *nand)
int nand_write_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
@ LP_OPTIONS
Definition: nand/core.h:134
@ NAND_NO_READRDY
Definition: nand/core.h:126
@ BBT_AUTO_REFRESH
Definition: nand/core.h:121
@ NAND_NO_AUTOINCR
Definition: nand/core.h:96
@ NAND_SAMSUNG_LP_OPTIONS
Definition: nand/core.h:129
@ LP_OPTIONS16
Definition: nand/core.h:135
@ NAND_NO_PADDING
Definition: nand/core.h:102
@ NAND_CACHEPRG
Definition: nand/core.h:105
@ NAND_COPYBACK
Definition: nand/core.h:108
@ NAND_IS_AND
Definition: nand/core.h:112
@ NAND_4PAGE_ARRAY
Definition: nand/core.h:116
@ NAND_BUSWIDTH_16
Definition: nand/core.h:99
struct nand_device * get_nand_device_by_num(int num)
int nand_register_commands(struct command_context *cmd_ctx)
oob_formats
Definition: nand/core.h:170
@ NAND_OOB_JFFS2
Definition: nand/core.h:178
@ NAND_OOB_NONE
Definition: nand/core.h:171
@ NAND_OOB_SW_ECC_KW
Definition: nand/core.h:177
@ NAND_OOB_ONLY
Definition: nand/core.h:174
@ NAND_OOB_HW_ECC
Definition: nand/core.h:176
@ NAND_OOB_YAFFS2
Definition: nand/core.h:179
@ NAND_OOB_SW_ECC
Definition: nand/core.h:175
@ NAND_OOB_RAW
Definition: nand/core.h:172
int nand_page_command(struct nand_device *nand, uint32_t page, uint8_t cmd, bool oob_only)
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
Representation of a single NAND block in a NAND device.
Definition: nand/core.h:21
int is_erased
True if the block has been erased.
Definition: nand/core.h:29
uint32_t offset
Offset to the block.
Definition: nand/core.h:23
int is_bad
True if the block is bad.
Definition: nand/core.h:32
uint32_t size
Size of the block.
Definition: nand/core.h:26
struct nand_flash_controller * controller
Definition: nand/core.h:50
void * controller_priv
Definition: nand/core.h:51
int page_size
Definition: nand/core.h:56
struct nand_device * next
Definition: nand/core.h:61
int num_blocks
Definition: nand/core.h:59
struct nand_block * blocks
Definition: nand/core.h:60
bool use_raw
Definition: nand/core.h:58
int address_cycles
Definition: nand/core.h:55
int bus_width
Definition: nand/core.h:54
int erase_size
Definition: nand/core.h:57
const char * name
Definition: nand/core.h:48
struct target * target
Definition: nand/core.h:49
struct nand_manufacturer * manufacturer
Definition: nand/core.h:52
struct nand_info * device
Definition: nand/core.h:53
int eccpos[64]
Definition: nand/core.h:42
struct nand_oobfree oobfree[2]
Definition: nand/core.h:44
Interface for NAND flash controllers.
Definition: nand/driver.h:23
int chip_size
Definition: nand/core.h:86
int erase_size
Definition: nand/core.h:87
int mfr_id
Definition: nand/core.h:83
int id
Definition: nand/core.h:84
int page_size
Definition: nand/core.h:85
const char * name
Definition: nand/core.h:89
int options
Definition: nand/core.h:88
const char * name
Definition: nand/core.h:79
Definition: target.h:120
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1