xref: /OK3568_Linux_fs/u-boot/include/linux/mtd/rawnand.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
3*4882a593Smuzhiyun  *                        Steven J. Hill <sjhill@realitydiluted.com>
4*4882a593Smuzhiyun  *		          Thomas Gleixner <tglx@linutronix.de>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Info:
9*4882a593Smuzhiyun  *	Contains standard defines and IDs for NAND flash devices
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Changelog:
12*4882a593Smuzhiyun  *	See git changelog.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun #ifndef __LINUX_MTD_RAWNAND_H
15*4882a593Smuzhiyun #define __LINUX_MTD_RAWNAND_H
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <config.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/compat.h>
20*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
21*4882a593Smuzhiyun #include <linux/mtd/flashchip.h>
22*4882a593Smuzhiyun #include <linux/mtd/bbm.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun struct mtd_info;
25*4882a593Smuzhiyun struct nand_chip;
26*4882a593Smuzhiyun struct nand_flash_dev;
27*4882a593Smuzhiyun struct device_node;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* Get the flash and manufacturer id and lookup if the type is supported. */
30*4882a593Smuzhiyun struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
31*4882a593Smuzhiyun 					   struct nand_chip *chip,
32*4882a593Smuzhiyun 					   int *maf_id, int *dev_id,
33*4882a593Smuzhiyun 					   struct nand_flash_dev *type);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* Scan and identify a NAND device */
36*4882a593Smuzhiyun extern int nand_scan(struct mtd_info *mtd, int max_chips);
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * Separate phases of nand_scan(), allowing board driver to intervene
39*4882a593Smuzhiyun  * and override command or ECC setup according to flash type.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun extern int nand_scan_ident(struct mtd_info *mtd, int max_chips,
42*4882a593Smuzhiyun 			   struct nand_flash_dev *table);
43*4882a593Smuzhiyun extern int nand_scan_tail(struct mtd_info *mtd);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /* Free resources held by the NAND device */
46*4882a593Smuzhiyun extern void nand_release(struct mtd_info *mtd);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /* Internal helper for board drivers which need to override command function */
49*4882a593Smuzhiyun extern void nand_wait_ready(struct mtd_info *mtd);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun  * This constant declares the max. oobsize / page, which
53*4882a593Smuzhiyun  * is supported now. If you add a chip with bigger oobsize/page
54*4882a593Smuzhiyun  * adjust this accordingly.
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun #define NAND_MAX_OOBSIZE       1664
57*4882a593Smuzhiyun #define NAND_MAX_PAGESIZE      16384
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * Constants for hardware specific CLE/ALE/NCE function
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * These are bits which can be or'ed to set/clear multiple
63*4882a593Smuzhiyun  * bits in one go.
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun /* Select the chip by setting nCE to low */
66*4882a593Smuzhiyun #define NAND_NCE		0x01
67*4882a593Smuzhiyun /* Select the command latch by setting CLE to high */
68*4882a593Smuzhiyun #define NAND_CLE		0x02
69*4882a593Smuzhiyun /* Select the address latch by setting ALE to high */
70*4882a593Smuzhiyun #define NAND_ALE		0x04
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #define NAND_CTRL_CLE		(NAND_NCE | NAND_CLE)
73*4882a593Smuzhiyun #define NAND_CTRL_ALE		(NAND_NCE | NAND_ALE)
74*4882a593Smuzhiyun #define NAND_CTRL_CHANGE	0x80
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun  * Standard NAND flash commands
78*4882a593Smuzhiyun  */
79*4882a593Smuzhiyun #define NAND_CMD_READ0		0
80*4882a593Smuzhiyun #define NAND_CMD_READ1		1
81*4882a593Smuzhiyun #define NAND_CMD_RNDOUT		5
82*4882a593Smuzhiyun #define NAND_CMD_PAGEPROG	0x10
83*4882a593Smuzhiyun #define NAND_CMD_READOOB	0x50
84*4882a593Smuzhiyun #define NAND_CMD_ERASE1		0x60
85*4882a593Smuzhiyun #define NAND_CMD_STATUS		0x70
86*4882a593Smuzhiyun #define NAND_CMD_SEQIN		0x80
87*4882a593Smuzhiyun #define NAND_CMD_RNDIN		0x85
88*4882a593Smuzhiyun #define NAND_CMD_READID		0x90
89*4882a593Smuzhiyun #define NAND_CMD_ERASE2		0xd0
90*4882a593Smuzhiyun #define NAND_CMD_PARAM		0xec
91*4882a593Smuzhiyun #define NAND_CMD_GET_FEATURES	0xee
92*4882a593Smuzhiyun #define NAND_CMD_SET_FEATURES	0xef
93*4882a593Smuzhiyun #define NAND_CMD_RESET		0xff
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #define NAND_CMD_LOCK		0x2a
96*4882a593Smuzhiyun #define NAND_CMD_UNLOCK1	0x23
97*4882a593Smuzhiyun #define NAND_CMD_UNLOCK2	0x24
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /* Extended commands for large page devices */
100*4882a593Smuzhiyun #define NAND_CMD_READSTART	0x30
101*4882a593Smuzhiyun #define NAND_CMD_RNDOUTSTART	0xE0
102*4882a593Smuzhiyun #define NAND_CMD_CACHEDPROG	0x15
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /* Extended commands for AG-AND device */
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * Note: the command for NAND_CMD_DEPLETE1 is really 0x00 but
107*4882a593Smuzhiyun  *       there is no way to distinguish that from NAND_CMD_READ0
108*4882a593Smuzhiyun  *       until the remaining sequence of commands has been completed
109*4882a593Smuzhiyun  *       so add a high order bit and mask it off in the command.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun #define NAND_CMD_DEPLETE1	0x100
112*4882a593Smuzhiyun #define NAND_CMD_DEPLETE2	0x38
113*4882a593Smuzhiyun #define NAND_CMD_STATUS_MULTI	0x71
114*4882a593Smuzhiyun #define NAND_CMD_STATUS_ERROR	0x72
115*4882a593Smuzhiyun /* multi-bank error status (banks 0-3) */
116*4882a593Smuzhiyun #define NAND_CMD_STATUS_ERROR0	0x73
117*4882a593Smuzhiyun #define NAND_CMD_STATUS_ERROR1	0x74
118*4882a593Smuzhiyun #define NAND_CMD_STATUS_ERROR2	0x75
119*4882a593Smuzhiyun #define NAND_CMD_STATUS_ERROR3	0x76
120*4882a593Smuzhiyun #define NAND_CMD_STATUS_RESET	0x7f
121*4882a593Smuzhiyun #define NAND_CMD_STATUS_CLEAR	0xff
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun #define NAND_CMD_NONE		-1
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /* Status bits */
126*4882a593Smuzhiyun #define NAND_STATUS_FAIL	0x01
127*4882a593Smuzhiyun #define NAND_STATUS_FAIL_N1	0x02
128*4882a593Smuzhiyun #define NAND_STATUS_TRUE_READY	0x20
129*4882a593Smuzhiyun #define NAND_STATUS_READY	0x40
130*4882a593Smuzhiyun #define NAND_STATUS_WP		0x80
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun #define NAND_DATA_IFACE_CHECK_ONLY	-1
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun  * Constants for ECC_MODES
136*4882a593Smuzhiyun  */
137*4882a593Smuzhiyun typedef enum {
138*4882a593Smuzhiyun 	NAND_ECC_NONE,
139*4882a593Smuzhiyun 	NAND_ECC_SOFT,
140*4882a593Smuzhiyun 	NAND_ECC_HW,
141*4882a593Smuzhiyun 	NAND_ECC_HW_SYNDROME,
142*4882a593Smuzhiyun 	NAND_ECC_HW_OOB_FIRST,
143*4882a593Smuzhiyun 	NAND_ECC_SOFT_BCH,
144*4882a593Smuzhiyun } nand_ecc_modes_t;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /*
147*4882a593Smuzhiyun  * Constants for Hardware ECC
148*4882a593Smuzhiyun  */
149*4882a593Smuzhiyun /* Reset Hardware ECC for read */
150*4882a593Smuzhiyun #define NAND_ECC_READ		0
151*4882a593Smuzhiyun /* Reset Hardware ECC for write */
152*4882a593Smuzhiyun #define NAND_ECC_WRITE		1
153*4882a593Smuzhiyun /* Enable Hardware ECC before syndrome is read back from flash */
154*4882a593Smuzhiyun #define NAND_ECC_READSYN	2
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /*
157*4882a593Smuzhiyun  * Enable generic NAND 'page erased' check. This check is only done when
158*4882a593Smuzhiyun  * ecc.correct() returns -EBADMSG.
159*4882a593Smuzhiyun  * Set this flag if your implementation does not fix bitflips in erased
160*4882a593Smuzhiyun  * pages and you want to rely on the default implementation.
161*4882a593Smuzhiyun  */
162*4882a593Smuzhiyun #define NAND_ECC_GENERIC_ERASED_CHECK	BIT(0)
163*4882a593Smuzhiyun #define NAND_ECC_MAXIMIZE		BIT(1)
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun  * If your controller already sends the required NAND commands when
166*4882a593Smuzhiyun  * reading or writing a page, then the framework is not supposed to
167*4882a593Smuzhiyun  * send READ0 and SEQIN/PAGEPROG respectively.
168*4882a593Smuzhiyun  */
169*4882a593Smuzhiyun #define NAND_ECC_CUSTOM_PAGE_ACCESS	BIT(2)
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /* Bit mask for flags passed to do_nand_read_ecc */
172*4882a593Smuzhiyun #define NAND_GET_DEVICE		0x80
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /*
176*4882a593Smuzhiyun  * Option constants for bizarre disfunctionality and real
177*4882a593Smuzhiyun  * features.
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun /* Buswidth is 16 bit */
180*4882a593Smuzhiyun #define NAND_BUSWIDTH_16	0x00000002
181*4882a593Smuzhiyun /* Device supports partial programming without padding */
182*4882a593Smuzhiyun #define NAND_NO_PADDING		0x00000004
183*4882a593Smuzhiyun /* Chip has cache program function */
184*4882a593Smuzhiyun #define NAND_CACHEPRG		0x00000008
185*4882a593Smuzhiyun /* Chip has copy back function */
186*4882a593Smuzhiyun #define NAND_COPYBACK		0x00000010
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun  * Chip requires ready check on read (for auto-incremented sequential read).
189*4882a593Smuzhiyun  * True only for small page devices; large page devices do not support
190*4882a593Smuzhiyun  * autoincrement.
191*4882a593Smuzhiyun  */
192*4882a593Smuzhiyun #define NAND_NEED_READRDY	0x00000100
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /* Chip does not allow subpage writes */
195*4882a593Smuzhiyun #define NAND_NO_SUBPAGE_WRITE	0x00000200
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /* Device is one of 'new' xD cards that expose fake nand command set */
198*4882a593Smuzhiyun #define NAND_BROKEN_XD		0x00000400
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun /* Device behaves just like nand, but is readonly */
201*4882a593Smuzhiyun #define NAND_ROM		0x00000800
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /* Device supports subpage reads */
204*4882a593Smuzhiyun #define NAND_SUBPAGE_READ	0x00001000
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun  * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
208*4882a593Smuzhiyun  * patterns.
209*4882a593Smuzhiyun  */
210*4882a593Smuzhiyun #define NAND_NEED_SCRAMBLING	0x00002000
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /* Device needs 3rd row address cycle */
213*4882a593Smuzhiyun #define NAND_ROW_ADDR_3		0x00004000
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun /* Options valid for Samsung large page devices */
216*4882a593Smuzhiyun #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /* Macros to identify the above */
219*4882a593Smuzhiyun #define NAND_HAS_CACHEPROG(chip) ((chip->options & NAND_CACHEPRG))
220*4882a593Smuzhiyun #define NAND_HAS_SUBPAGE_READ(chip) ((chip->options & NAND_SUBPAGE_READ))
221*4882a593Smuzhiyun #define NAND_HAS_SUBPAGE_WRITE(chip) !((chip)->options & NAND_NO_SUBPAGE_WRITE)
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /* Non chip related options */
224*4882a593Smuzhiyun /* This option skips the bbt scan during initialization. */
225*4882a593Smuzhiyun #define NAND_SKIP_BBTSCAN	0x00010000
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * This option is defined if the board driver allocates its own buffers
228*4882a593Smuzhiyun  * (e.g. because it needs them DMA-coherent).
229*4882a593Smuzhiyun  */
230*4882a593Smuzhiyun #define NAND_OWN_BUFFERS	0x00020000
231*4882a593Smuzhiyun /* Chip may not exist, so silence any errors in scan */
232*4882a593Smuzhiyun #define NAND_SCAN_SILENT_NODEV	0x00040000
233*4882a593Smuzhiyun /*
234*4882a593Smuzhiyun  * Autodetect nand buswidth with readid/onfi.
235*4882a593Smuzhiyun  * This suppose the driver will configure the hardware in 8 bits mode
236*4882a593Smuzhiyun  * when calling nand_scan_ident, and update its configuration
237*4882a593Smuzhiyun  * before calling nand_scan_tail.
238*4882a593Smuzhiyun  */
239*4882a593Smuzhiyun #define NAND_BUSWIDTH_AUTO      0x00080000
240*4882a593Smuzhiyun /*
241*4882a593Smuzhiyun  * This option could be defined by controller drivers to protect against
242*4882a593Smuzhiyun  * kmap'ed, vmalloc'ed highmem buffers being passed from upper layers
243*4882a593Smuzhiyun  */
244*4882a593Smuzhiyun #define NAND_USE_BOUNCE_BUFFER	0x00100000
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /* Options set by nand scan */
247*4882a593Smuzhiyun /* bbt has already been read */
248*4882a593Smuzhiyun #define NAND_BBT_SCANNED	0x40000000
249*4882a593Smuzhiyun /* Nand scan has allocated controller struct */
250*4882a593Smuzhiyun #define NAND_CONTROLLER_ALLOC	0x80000000
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun /* Cell info constants */
253*4882a593Smuzhiyun #define NAND_CI_CHIPNR_MSK	0x03
254*4882a593Smuzhiyun #define NAND_CI_CELLTYPE_MSK	0x0C
255*4882a593Smuzhiyun #define NAND_CI_CELLTYPE_SHIFT	2
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun /* ONFI features */
258*4882a593Smuzhiyun #define ONFI_FEATURE_16_BIT_BUS		(1 << 0)
259*4882a593Smuzhiyun #define ONFI_FEATURE_EXT_PARAM_PAGE	(1 << 7)
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun /* ONFI timing mode, used in both asynchronous and synchronous mode */
262*4882a593Smuzhiyun #define ONFI_TIMING_MODE_0		(1 << 0)
263*4882a593Smuzhiyun #define ONFI_TIMING_MODE_1		(1 << 1)
264*4882a593Smuzhiyun #define ONFI_TIMING_MODE_2		(1 << 2)
265*4882a593Smuzhiyun #define ONFI_TIMING_MODE_3		(1 << 3)
266*4882a593Smuzhiyun #define ONFI_TIMING_MODE_4		(1 << 4)
267*4882a593Smuzhiyun #define ONFI_TIMING_MODE_5		(1 << 5)
268*4882a593Smuzhiyun #define ONFI_TIMING_MODE_UNKNOWN	(1 << 6)
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /* ONFI feature address */
271*4882a593Smuzhiyun #define ONFI_FEATURE_ADDR_TIMING_MODE	0x1
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun /* Vendor-specific feature address (Micron) */
274*4882a593Smuzhiyun #define ONFI_FEATURE_ADDR_READ_RETRY	0x89
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /* ONFI subfeature parameters length */
277*4882a593Smuzhiyun #define ONFI_SUBFEATURE_PARAM_LEN	4
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun /* ONFI optional commands SET/GET FEATURES supported? */
280*4882a593Smuzhiyun #define ONFI_OPT_CMD_SET_GET_FEATURES	(1 << 2)
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun struct nand_onfi_params {
283*4882a593Smuzhiyun 	/* rev info and features block */
284*4882a593Smuzhiyun 	/* 'O' 'N' 'F' 'I'  */
285*4882a593Smuzhiyun 	u8 sig[4];
286*4882a593Smuzhiyun 	__le16 revision;
287*4882a593Smuzhiyun 	__le16 features;
288*4882a593Smuzhiyun 	__le16 opt_cmd;
289*4882a593Smuzhiyun 	u8 reserved0[2];
290*4882a593Smuzhiyun 	__le16 ext_param_page_length; /* since ONFI 2.1 */
291*4882a593Smuzhiyun 	u8 num_of_param_pages;        /* since ONFI 2.1 */
292*4882a593Smuzhiyun 	u8 reserved1[17];
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	/* manufacturer information block */
295*4882a593Smuzhiyun 	char manufacturer[12];
296*4882a593Smuzhiyun 	char model[20];
297*4882a593Smuzhiyun 	u8 jedec_id;
298*4882a593Smuzhiyun 	__le16 date_code;
299*4882a593Smuzhiyun 	u8 reserved2[13];
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/* memory organization block */
302*4882a593Smuzhiyun 	__le32 byte_per_page;
303*4882a593Smuzhiyun 	__le16 spare_bytes_per_page;
304*4882a593Smuzhiyun 	__le32 data_bytes_per_ppage;
305*4882a593Smuzhiyun 	__le16 spare_bytes_per_ppage;
306*4882a593Smuzhiyun 	__le32 pages_per_block;
307*4882a593Smuzhiyun 	__le32 blocks_per_lun;
308*4882a593Smuzhiyun 	u8 lun_count;
309*4882a593Smuzhiyun 	u8 addr_cycles;
310*4882a593Smuzhiyun 	u8 bits_per_cell;
311*4882a593Smuzhiyun 	__le16 bb_per_lun;
312*4882a593Smuzhiyun 	__le16 block_endurance;
313*4882a593Smuzhiyun 	u8 guaranteed_good_blocks;
314*4882a593Smuzhiyun 	__le16 guaranteed_block_endurance;
315*4882a593Smuzhiyun 	u8 programs_per_page;
316*4882a593Smuzhiyun 	u8 ppage_attr;
317*4882a593Smuzhiyun 	u8 ecc_bits;
318*4882a593Smuzhiyun 	u8 interleaved_bits;
319*4882a593Smuzhiyun 	u8 interleaved_ops;
320*4882a593Smuzhiyun 	u8 reserved3[13];
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	/* electrical parameter block */
323*4882a593Smuzhiyun 	u8 io_pin_capacitance_max;
324*4882a593Smuzhiyun 	__le16 async_timing_mode;
325*4882a593Smuzhiyun 	__le16 program_cache_timing_mode;
326*4882a593Smuzhiyun 	__le16 t_prog;
327*4882a593Smuzhiyun 	__le16 t_bers;
328*4882a593Smuzhiyun 	__le16 t_r;
329*4882a593Smuzhiyun 	__le16 t_ccs;
330*4882a593Smuzhiyun 	__le16 src_sync_timing_mode;
331*4882a593Smuzhiyun 	u8 src_ssync_features;
332*4882a593Smuzhiyun 	__le16 clk_pin_capacitance_typ;
333*4882a593Smuzhiyun 	__le16 io_pin_capacitance_typ;
334*4882a593Smuzhiyun 	__le16 input_pin_capacitance_typ;
335*4882a593Smuzhiyun 	u8 input_pin_capacitance_max;
336*4882a593Smuzhiyun 	u8 driver_strength_support;
337*4882a593Smuzhiyun 	__le16 t_int_r;
338*4882a593Smuzhiyun 	__le16 t_adl;
339*4882a593Smuzhiyun 	u8 reserved4[8];
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	/* vendor */
342*4882a593Smuzhiyun 	__le16 vendor_revision;
343*4882a593Smuzhiyun 	u8 vendor[88];
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	__le16 crc;
346*4882a593Smuzhiyun } __packed;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun #define ONFI_CRC_BASE	0x4F4E
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun /* Extended ECC information Block Definition (since ONFI 2.1) */
351*4882a593Smuzhiyun struct onfi_ext_ecc_info {
352*4882a593Smuzhiyun 	u8 ecc_bits;
353*4882a593Smuzhiyun 	u8 codeword_size;
354*4882a593Smuzhiyun 	__le16 bb_per_lun;
355*4882a593Smuzhiyun 	__le16 block_endurance;
356*4882a593Smuzhiyun 	u8 reserved[2];
357*4882a593Smuzhiyun } __packed;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun #define ONFI_SECTION_TYPE_0	0	/* Unused section. */
360*4882a593Smuzhiyun #define ONFI_SECTION_TYPE_1	1	/* for additional sections. */
361*4882a593Smuzhiyun #define ONFI_SECTION_TYPE_2	2	/* for ECC information. */
362*4882a593Smuzhiyun struct onfi_ext_section {
363*4882a593Smuzhiyun 	u8 type;
364*4882a593Smuzhiyun 	u8 length;
365*4882a593Smuzhiyun } __packed;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun #define ONFI_EXT_SECTION_MAX 8
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /* Extended Parameter Page Definition (since ONFI 2.1) */
370*4882a593Smuzhiyun struct onfi_ext_param_page {
371*4882a593Smuzhiyun 	__le16 crc;
372*4882a593Smuzhiyun 	u8 sig[4];             /* 'E' 'P' 'P' 'S' */
373*4882a593Smuzhiyun 	u8 reserved0[10];
374*4882a593Smuzhiyun 	struct onfi_ext_section sections[ONFI_EXT_SECTION_MAX];
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	/*
377*4882a593Smuzhiyun 	 * The actual size of the Extended Parameter Page is in
378*4882a593Smuzhiyun 	 * @ext_param_page_length of nand_onfi_params{}.
379*4882a593Smuzhiyun 	 * The following are the variable length sections.
380*4882a593Smuzhiyun 	 * So we do not add any fields below. Please see the ONFI spec.
381*4882a593Smuzhiyun 	 */
382*4882a593Smuzhiyun } __packed;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun struct nand_onfi_vendor_micron {
385*4882a593Smuzhiyun 	u8 two_plane_read;
386*4882a593Smuzhiyun 	u8 read_cache;
387*4882a593Smuzhiyun 	u8 read_unique_id;
388*4882a593Smuzhiyun 	u8 dq_imped;
389*4882a593Smuzhiyun 	u8 dq_imped_num_settings;
390*4882a593Smuzhiyun 	u8 dq_imped_feat_addr;
391*4882a593Smuzhiyun 	u8 rb_pulldown_strength;
392*4882a593Smuzhiyun 	u8 rb_pulldown_strength_feat_addr;
393*4882a593Smuzhiyun 	u8 rb_pulldown_strength_num_settings;
394*4882a593Smuzhiyun 	u8 otp_mode;
395*4882a593Smuzhiyun 	u8 otp_page_start;
396*4882a593Smuzhiyun 	u8 otp_data_prot_addr;
397*4882a593Smuzhiyun 	u8 otp_num_pages;
398*4882a593Smuzhiyun 	u8 otp_feat_addr;
399*4882a593Smuzhiyun 	u8 read_retry_options;
400*4882a593Smuzhiyun 	u8 reserved[72];
401*4882a593Smuzhiyun 	u8 param_revision;
402*4882a593Smuzhiyun } __packed;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun struct jedec_ecc_info {
405*4882a593Smuzhiyun 	u8 ecc_bits;
406*4882a593Smuzhiyun 	u8 codeword_size;
407*4882a593Smuzhiyun 	__le16 bb_per_lun;
408*4882a593Smuzhiyun 	__le16 block_endurance;
409*4882a593Smuzhiyun 	u8 reserved[2];
410*4882a593Smuzhiyun } __packed;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun /* JEDEC features */
413*4882a593Smuzhiyun #define JEDEC_FEATURE_16_BIT_BUS	(1 << 0)
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun struct nand_jedec_params {
416*4882a593Smuzhiyun 	/* rev info and features block */
417*4882a593Smuzhiyun 	/* 'J' 'E' 'S' 'D'  */
418*4882a593Smuzhiyun 	u8 sig[4];
419*4882a593Smuzhiyun 	__le16 revision;
420*4882a593Smuzhiyun 	__le16 features;
421*4882a593Smuzhiyun 	u8 opt_cmd[3];
422*4882a593Smuzhiyun 	__le16 sec_cmd;
423*4882a593Smuzhiyun 	u8 num_of_param_pages;
424*4882a593Smuzhiyun 	u8 reserved0[18];
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	/* manufacturer information block */
427*4882a593Smuzhiyun 	char manufacturer[12];
428*4882a593Smuzhiyun 	char model[20];
429*4882a593Smuzhiyun 	u8 jedec_id[6];
430*4882a593Smuzhiyun 	u8 reserved1[10];
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	/* memory organization block */
433*4882a593Smuzhiyun 	__le32 byte_per_page;
434*4882a593Smuzhiyun 	__le16 spare_bytes_per_page;
435*4882a593Smuzhiyun 	u8 reserved2[6];
436*4882a593Smuzhiyun 	__le32 pages_per_block;
437*4882a593Smuzhiyun 	__le32 blocks_per_lun;
438*4882a593Smuzhiyun 	u8 lun_count;
439*4882a593Smuzhiyun 	u8 addr_cycles;
440*4882a593Smuzhiyun 	u8 bits_per_cell;
441*4882a593Smuzhiyun 	u8 programs_per_page;
442*4882a593Smuzhiyun 	u8 multi_plane_addr;
443*4882a593Smuzhiyun 	u8 multi_plane_op_attr;
444*4882a593Smuzhiyun 	u8 reserved3[38];
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	/* electrical parameter block */
447*4882a593Smuzhiyun 	__le16 async_sdr_speed_grade;
448*4882a593Smuzhiyun 	__le16 toggle_ddr_speed_grade;
449*4882a593Smuzhiyun 	__le16 sync_ddr_speed_grade;
450*4882a593Smuzhiyun 	u8 async_sdr_features;
451*4882a593Smuzhiyun 	u8 toggle_ddr_features;
452*4882a593Smuzhiyun 	u8 sync_ddr_features;
453*4882a593Smuzhiyun 	__le16 t_prog;
454*4882a593Smuzhiyun 	__le16 t_bers;
455*4882a593Smuzhiyun 	__le16 t_r;
456*4882a593Smuzhiyun 	__le16 t_r_multi_plane;
457*4882a593Smuzhiyun 	__le16 t_ccs;
458*4882a593Smuzhiyun 	__le16 io_pin_capacitance_typ;
459*4882a593Smuzhiyun 	__le16 input_pin_capacitance_typ;
460*4882a593Smuzhiyun 	__le16 clk_pin_capacitance_typ;
461*4882a593Smuzhiyun 	u8 driver_strength_support;
462*4882a593Smuzhiyun 	__le16 t_adl;
463*4882a593Smuzhiyun 	u8 reserved4[36];
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	/* ECC and endurance block */
466*4882a593Smuzhiyun 	u8 guaranteed_good_blocks;
467*4882a593Smuzhiyun 	__le16 guaranteed_block_endurance;
468*4882a593Smuzhiyun 	struct jedec_ecc_info ecc_info[4];
469*4882a593Smuzhiyun 	u8 reserved5[29];
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	/* reserved */
472*4882a593Smuzhiyun 	u8 reserved6[148];
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	/* vendor */
475*4882a593Smuzhiyun 	__le16 vendor_rev_num;
476*4882a593Smuzhiyun 	u8 reserved7[88];
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	/* CRC for Parameter Page */
479*4882a593Smuzhiyun 	__le16 crc;
480*4882a593Smuzhiyun } __packed;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun /**
483*4882a593Smuzhiyun  * struct nand_hw_control - Control structure for hardware controller (e.g ECC generator) shared among independent devices
484*4882a593Smuzhiyun  * @lock:               protection lock
485*4882a593Smuzhiyun  * @active:		the mtd device which holds the controller currently
486*4882a593Smuzhiyun  * @wq:			wait queue to sleep on if a NAND operation is in
487*4882a593Smuzhiyun  *			progress used instead of the per chip wait queue
488*4882a593Smuzhiyun  *			when a hw controller is available.
489*4882a593Smuzhiyun  */
490*4882a593Smuzhiyun struct nand_hw_control {
491*4882a593Smuzhiyun 	spinlock_t lock;
492*4882a593Smuzhiyun 	struct nand_chip *active;
493*4882a593Smuzhiyun };
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun /**
496*4882a593Smuzhiyun  * struct nand_ecc_step_info - ECC step information of ECC engine
497*4882a593Smuzhiyun  * @stepsize: data bytes per ECC step
498*4882a593Smuzhiyun  * @strengths: array of supported strengths
499*4882a593Smuzhiyun  * @nstrengths: number of supported strengths
500*4882a593Smuzhiyun  */
501*4882a593Smuzhiyun struct nand_ecc_step_info {
502*4882a593Smuzhiyun 	int stepsize;
503*4882a593Smuzhiyun 	const int *strengths;
504*4882a593Smuzhiyun 	int nstrengths;
505*4882a593Smuzhiyun };
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun /**
508*4882a593Smuzhiyun  * struct nand_ecc_caps - capability of ECC engine
509*4882a593Smuzhiyun  * @stepinfos: array of ECC step information
510*4882a593Smuzhiyun  * @nstepinfos: number of ECC step information
511*4882a593Smuzhiyun  * @calc_ecc_bytes: driver's hook to calculate ECC bytes per step
512*4882a593Smuzhiyun  */
513*4882a593Smuzhiyun struct nand_ecc_caps {
514*4882a593Smuzhiyun 	const struct nand_ecc_step_info *stepinfos;
515*4882a593Smuzhiyun 	int nstepinfos;
516*4882a593Smuzhiyun 	int (*calc_ecc_bytes)(int step_size, int strength);
517*4882a593Smuzhiyun };
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun /**
520*4882a593Smuzhiyun  * struct nand_ecc_ctrl - Control structure for ECC
521*4882a593Smuzhiyun  * @mode:	ECC mode
522*4882a593Smuzhiyun  * @steps:	number of ECC steps per page
523*4882a593Smuzhiyun  * @size:	data bytes per ECC step
524*4882a593Smuzhiyun  * @bytes:	ECC bytes per step
525*4882a593Smuzhiyun  * @strength:	max number of correctible bits per ECC step
526*4882a593Smuzhiyun  * @total:	total number of ECC bytes per page
527*4882a593Smuzhiyun  * @prepad:	padding information for syndrome based ECC generators
528*4882a593Smuzhiyun  * @postpad:	padding information for syndrome based ECC generators
529*4882a593Smuzhiyun  * @options:	ECC specific options (see NAND_ECC_XXX flags defined above)
530*4882a593Smuzhiyun  * @layout:	ECC layout control struct pointer
531*4882a593Smuzhiyun  * @priv:	pointer to private ECC control data
532*4882a593Smuzhiyun  * @hwctl:	function to control hardware ECC generator. Must only
533*4882a593Smuzhiyun  *		be provided if an hardware ECC is available
534*4882a593Smuzhiyun  * @calculate:	function for ECC calculation or readback from ECC hardware
535*4882a593Smuzhiyun  * @correct:	function for ECC correction, matching to ECC generator (sw/hw).
536*4882a593Smuzhiyun  *		Should return a positive number representing the number of
537*4882a593Smuzhiyun  *		corrected bitflips, -EBADMSG if the number of bitflips exceed
538*4882a593Smuzhiyun  *		ECC strength, or any other error code if the error is not
539*4882a593Smuzhiyun  *		directly related to correction.
540*4882a593Smuzhiyun  *		If -EBADMSG is returned the input buffers should be left
541*4882a593Smuzhiyun  *		untouched.
542*4882a593Smuzhiyun  * @read_page_raw:	function to read a raw page without ECC. This function
543*4882a593Smuzhiyun  *			should hide the specific layout used by the ECC
544*4882a593Smuzhiyun  *			controller and always return contiguous in-band and
545*4882a593Smuzhiyun  *			out-of-band data even if they're not stored
546*4882a593Smuzhiyun  *			contiguously on the NAND chip (e.g.
547*4882a593Smuzhiyun  *			NAND_ECC_HW_SYNDROME interleaves in-band and
548*4882a593Smuzhiyun  *			out-of-band data).
549*4882a593Smuzhiyun  * @write_page_raw:	function to write a raw page without ECC. This function
550*4882a593Smuzhiyun  *			should hide the specific layout used by the ECC
551*4882a593Smuzhiyun  *			controller and consider the passed data as contiguous
552*4882a593Smuzhiyun  *			in-band and out-of-band data. ECC controller is
553*4882a593Smuzhiyun  *			responsible for doing the appropriate transformations
554*4882a593Smuzhiyun  *			to adapt to its specific layout (e.g.
555*4882a593Smuzhiyun  *			NAND_ECC_HW_SYNDROME interleaves in-band and
556*4882a593Smuzhiyun  *			out-of-band data).
557*4882a593Smuzhiyun  * @read_page:	function to read a page according to the ECC generator
558*4882a593Smuzhiyun  *		requirements; returns maximum number of bitflips corrected in
559*4882a593Smuzhiyun  *		any single ECC step, 0 if bitflips uncorrectable, -EIO hw error
560*4882a593Smuzhiyun  * @read_subpage:	function to read parts of the page covered by ECC;
561*4882a593Smuzhiyun  *			returns same as read_page()
562*4882a593Smuzhiyun  * @write_subpage:	function to write parts of the page covered by ECC.
563*4882a593Smuzhiyun  * @write_page:	function to write a page according to the ECC generator
564*4882a593Smuzhiyun  *		requirements.
565*4882a593Smuzhiyun  * @write_oob_raw:	function to write chip OOB data without ECC
566*4882a593Smuzhiyun  * @read_oob_raw:	function to read chip OOB data without ECC
567*4882a593Smuzhiyun  * @read_oob:	function to read chip OOB data
568*4882a593Smuzhiyun  * @write_oob:	function to write chip OOB data
569*4882a593Smuzhiyun  */
570*4882a593Smuzhiyun struct nand_ecc_ctrl {
571*4882a593Smuzhiyun 	nand_ecc_modes_t mode;
572*4882a593Smuzhiyun 	int steps;
573*4882a593Smuzhiyun 	int size;
574*4882a593Smuzhiyun 	int bytes;
575*4882a593Smuzhiyun 	int total;
576*4882a593Smuzhiyun 	int strength;
577*4882a593Smuzhiyun 	int prepad;
578*4882a593Smuzhiyun 	int postpad;
579*4882a593Smuzhiyun 	unsigned int options;
580*4882a593Smuzhiyun 	struct nand_ecclayout	*layout;
581*4882a593Smuzhiyun 	void *priv;
582*4882a593Smuzhiyun 	void (*hwctl)(struct mtd_info *mtd, int mode);
583*4882a593Smuzhiyun 	int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
584*4882a593Smuzhiyun 			uint8_t *ecc_code);
585*4882a593Smuzhiyun 	int (*correct)(struct mtd_info *mtd, uint8_t *dat, uint8_t *read_ecc,
586*4882a593Smuzhiyun 			uint8_t *calc_ecc);
587*4882a593Smuzhiyun 	int (*read_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
588*4882a593Smuzhiyun 			uint8_t *buf, int oob_required, int page);
589*4882a593Smuzhiyun 	int (*write_page_raw)(struct mtd_info *mtd, struct nand_chip *chip,
590*4882a593Smuzhiyun 			const uint8_t *buf, int oob_required, int page);
591*4882a593Smuzhiyun 	int (*read_page)(struct mtd_info *mtd, struct nand_chip *chip,
592*4882a593Smuzhiyun 			uint8_t *buf, int oob_required, int page);
593*4882a593Smuzhiyun 	int (*read_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
594*4882a593Smuzhiyun 			uint32_t offs, uint32_t len, uint8_t *buf, int page);
595*4882a593Smuzhiyun 	int (*write_subpage)(struct mtd_info *mtd, struct nand_chip *chip,
596*4882a593Smuzhiyun 			uint32_t offset, uint32_t data_len,
597*4882a593Smuzhiyun 			const uint8_t *data_buf, int oob_required, int page);
598*4882a593Smuzhiyun 	int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
599*4882a593Smuzhiyun 			const uint8_t *buf, int oob_required, int page);
600*4882a593Smuzhiyun 	int (*write_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
601*4882a593Smuzhiyun 			int page);
602*4882a593Smuzhiyun 	int (*read_oob_raw)(struct mtd_info *mtd, struct nand_chip *chip,
603*4882a593Smuzhiyun 			int page);
604*4882a593Smuzhiyun 	int (*read_oob)(struct mtd_info *mtd, struct nand_chip *chip, int page);
605*4882a593Smuzhiyun 	int (*write_oob)(struct mtd_info *mtd, struct nand_chip *chip,
606*4882a593Smuzhiyun 			int page);
607*4882a593Smuzhiyun };
608*4882a593Smuzhiyun 
nand_standard_page_accessors(struct nand_ecc_ctrl * ecc)609*4882a593Smuzhiyun static inline int nand_standard_page_accessors(struct nand_ecc_ctrl *ecc)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	return !(ecc->options & NAND_ECC_CUSTOM_PAGE_ACCESS);
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun /**
615*4882a593Smuzhiyun  * struct nand_buffers - buffer structure for read/write
616*4882a593Smuzhiyun  * @ecccalc:	buffer pointer for calculated ECC, size is oobsize.
617*4882a593Smuzhiyun  * @ecccode:	buffer pointer for ECC read from flash, size is oobsize.
618*4882a593Smuzhiyun  * @databuf:	buffer pointer for data, size is (page size + oobsize).
619*4882a593Smuzhiyun  *
620*4882a593Smuzhiyun  * Do not change the order of buffers. databuf and oobrbuf must be in
621*4882a593Smuzhiyun  * consecutive order.
622*4882a593Smuzhiyun  */
623*4882a593Smuzhiyun struct nand_buffers {
624*4882a593Smuzhiyun 	uint8_t	ecccalc[ALIGN(NAND_MAX_OOBSIZE, ARCH_DMA_MINALIGN)];
625*4882a593Smuzhiyun 	uint8_t	ecccode[ALIGN(NAND_MAX_OOBSIZE, ARCH_DMA_MINALIGN)];
626*4882a593Smuzhiyun 	uint8_t databuf[ALIGN(NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE,
627*4882a593Smuzhiyun 			      ARCH_DMA_MINALIGN)];
628*4882a593Smuzhiyun };
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun /**
631*4882a593Smuzhiyun  * struct nand_sdr_timings - SDR NAND chip timings
632*4882a593Smuzhiyun  *
633*4882a593Smuzhiyun  * This struct defines the timing requirements of a SDR NAND chip.
634*4882a593Smuzhiyun  * These information can be found in every NAND datasheets and the timings
635*4882a593Smuzhiyun  * meaning are described in the ONFI specifications:
636*4882a593Smuzhiyun  * www.onfi.org/~/media/ONFI/specs/onfi_3_1_spec.pdf (chapter 4.15 Timing
637*4882a593Smuzhiyun  * Parameters)
638*4882a593Smuzhiyun  *
639*4882a593Smuzhiyun  * All these timings are expressed in picoseconds.
640*4882a593Smuzhiyun  *
641*4882a593Smuzhiyun  * @tBERS_max: Block erase time
642*4882a593Smuzhiyun  * @tCCS_min: Change column setup time
643*4882a593Smuzhiyun  * @tPROG_max: Page program time
644*4882a593Smuzhiyun  * @tR_max: Page read time
645*4882a593Smuzhiyun  * @tALH_min: ALE hold time
646*4882a593Smuzhiyun  * @tADL_min: ALE to data loading time
647*4882a593Smuzhiyun  * @tALS_min: ALE setup time
648*4882a593Smuzhiyun  * @tAR_min: ALE to RE# delay
649*4882a593Smuzhiyun  * @tCEA_max: CE# access time
650*4882a593Smuzhiyun  * @tCEH_min: CE# high hold time
651*4882a593Smuzhiyun  * @tCH_min:  CE# hold time
652*4882a593Smuzhiyun  * @tCHZ_max: CE# high to output hi-Z
653*4882a593Smuzhiyun  * @tCLH_min: CLE hold time
654*4882a593Smuzhiyun  * @tCLR_min: CLE to RE# delay
655*4882a593Smuzhiyun  * @tCLS_min: CLE setup time
656*4882a593Smuzhiyun  * @tCOH_min: CE# high to output hold
657*4882a593Smuzhiyun  * @tCS_min: CE# setup time
658*4882a593Smuzhiyun  * @tDH_min: Data hold time
659*4882a593Smuzhiyun  * @tDS_min: Data setup time
660*4882a593Smuzhiyun  * @tFEAT_max: Busy time for Set Features and Get Features
661*4882a593Smuzhiyun  * @tIR_min: Output hi-Z to RE# low
662*4882a593Smuzhiyun  * @tITC_max: Interface and Timing Mode Change time
663*4882a593Smuzhiyun  * @tRC_min: RE# cycle time
664*4882a593Smuzhiyun  * @tREA_max: RE# access time
665*4882a593Smuzhiyun  * @tREH_min: RE# high hold time
666*4882a593Smuzhiyun  * @tRHOH_min: RE# high to output hold
667*4882a593Smuzhiyun  * @tRHW_min: RE# high to WE# low
668*4882a593Smuzhiyun  * @tRHZ_max: RE# high to output hi-Z
669*4882a593Smuzhiyun  * @tRLOH_min: RE# low to output hold
670*4882a593Smuzhiyun  * @tRP_min: RE# pulse width
671*4882a593Smuzhiyun  * @tRR_min: Ready to RE# low (data only)
672*4882a593Smuzhiyun  * @tRST_max: Device reset time, measured from the falling edge of R/B# to the
673*4882a593Smuzhiyun  *	      rising edge of R/B#.
674*4882a593Smuzhiyun  * @tWB_max: WE# high to SR[6] low
675*4882a593Smuzhiyun  * @tWC_min: WE# cycle time
676*4882a593Smuzhiyun  * @tWH_min: WE# high hold time
677*4882a593Smuzhiyun  * @tWHR_min: WE# high to RE# low
678*4882a593Smuzhiyun  * @tWP_min: WE# pulse width
679*4882a593Smuzhiyun  * @tWW_min: WP# transition to WE# low
680*4882a593Smuzhiyun  */
681*4882a593Smuzhiyun struct nand_sdr_timings {
682*4882a593Smuzhiyun 	u64 tBERS_max;
683*4882a593Smuzhiyun 	u32 tCCS_min;
684*4882a593Smuzhiyun 	u64 tPROG_max;
685*4882a593Smuzhiyun 	u64 tR_max;
686*4882a593Smuzhiyun 	u32 tALH_min;
687*4882a593Smuzhiyun 	u32 tADL_min;
688*4882a593Smuzhiyun 	u32 tALS_min;
689*4882a593Smuzhiyun 	u32 tAR_min;
690*4882a593Smuzhiyun 	u32 tCEA_max;
691*4882a593Smuzhiyun 	u32 tCEH_min;
692*4882a593Smuzhiyun 	u32 tCH_min;
693*4882a593Smuzhiyun 	u32 tCHZ_max;
694*4882a593Smuzhiyun 	u32 tCLH_min;
695*4882a593Smuzhiyun 	u32 tCLR_min;
696*4882a593Smuzhiyun 	u32 tCLS_min;
697*4882a593Smuzhiyun 	u32 tCOH_min;
698*4882a593Smuzhiyun 	u32 tCS_min;
699*4882a593Smuzhiyun 	u32 tDH_min;
700*4882a593Smuzhiyun 	u32 tDS_min;
701*4882a593Smuzhiyun 	u32 tFEAT_max;
702*4882a593Smuzhiyun 	u32 tIR_min;
703*4882a593Smuzhiyun 	u32 tITC_max;
704*4882a593Smuzhiyun 	u32 tRC_min;
705*4882a593Smuzhiyun 	u32 tREA_max;
706*4882a593Smuzhiyun 	u32 tREH_min;
707*4882a593Smuzhiyun 	u32 tRHOH_min;
708*4882a593Smuzhiyun 	u32 tRHW_min;
709*4882a593Smuzhiyun 	u32 tRHZ_max;
710*4882a593Smuzhiyun 	u32 tRLOH_min;
711*4882a593Smuzhiyun 	u32 tRP_min;
712*4882a593Smuzhiyun 	u32 tRR_min;
713*4882a593Smuzhiyun 	u64 tRST_max;
714*4882a593Smuzhiyun 	u32 tWB_max;
715*4882a593Smuzhiyun 	u32 tWC_min;
716*4882a593Smuzhiyun 	u32 tWH_min;
717*4882a593Smuzhiyun 	u32 tWHR_min;
718*4882a593Smuzhiyun 	u32 tWP_min;
719*4882a593Smuzhiyun 	u32 tWW_min;
720*4882a593Smuzhiyun };
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun /**
723*4882a593Smuzhiyun  * enum nand_data_interface_type - NAND interface timing type
724*4882a593Smuzhiyun  * @NAND_SDR_IFACE:	Single Data Rate interface
725*4882a593Smuzhiyun  */
726*4882a593Smuzhiyun enum nand_data_interface_type {
727*4882a593Smuzhiyun 	NAND_SDR_IFACE,
728*4882a593Smuzhiyun };
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun /**
731*4882a593Smuzhiyun  * struct nand_data_interface - NAND interface timing
732*4882a593Smuzhiyun  * @type:	type of the timing
733*4882a593Smuzhiyun  * @timings:	The timing, type according to @type
734*4882a593Smuzhiyun  */
735*4882a593Smuzhiyun struct nand_data_interface {
736*4882a593Smuzhiyun 	enum nand_data_interface_type type;
737*4882a593Smuzhiyun 	union {
738*4882a593Smuzhiyun 		struct nand_sdr_timings sdr;
739*4882a593Smuzhiyun 	} timings;
740*4882a593Smuzhiyun };
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun /**
743*4882a593Smuzhiyun  * nand_get_sdr_timings - get SDR timing from data interface
744*4882a593Smuzhiyun  * @conf:	The data interface
745*4882a593Smuzhiyun  */
746*4882a593Smuzhiyun static inline const struct nand_sdr_timings *
nand_get_sdr_timings(const struct nand_data_interface * conf)747*4882a593Smuzhiyun nand_get_sdr_timings(const struct nand_data_interface *conf)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	if (conf->type != NAND_SDR_IFACE)
750*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	return &conf->timings.sdr;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun /**
756*4882a593Smuzhiyun  * struct nand_chip - NAND Private Flash Chip Data
757*4882a593Smuzhiyun  * @mtd:		MTD device registered to the MTD framework
758*4882a593Smuzhiyun  * @IO_ADDR_R:		[BOARDSPECIFIC] address to read the 8 I/O lines of the
759*4882a593Smuzhiyun  *			flash device
760*4882a593Smuzhiyun  * @IO_ADDR_W:		[BOARDSPECIFIC] address to write the 8 I/O lines of the
761*4882a593Smuzhiyun  *			flash device.
762*4882a593Smuzhiyun  * @flash_node:		[BOARDSPECIFIC] device node describing this instance
763*4882a593Smuzhiyun  * @read_byte:		[REPLACEABLE] read one byte from the chip
764*4882a593Smuzhiyun  * @read_word:		[REPLACEABLE] read one word from the chip
765*4882a593Smuzhiyun  * @write_byte:		[REPLACEABLE] write a single byte to the chip on the
766*4882a593Smuzhiyun  *			low 8 I/O lines
767*4882a593Smuzhiyun  * @write_buf:		[REPLACEABLE] write data from the buffer to the chip
768*4882a593Smuzhiyun  * @read_buf:		[REPLACEABLE] read data from the chip into the buffer
769*4882a593Smuzhiyun  * @select_chip:	[REPLACEABLE] select chip nr
770*4882a593Smuzhiyun  * @block_bad:		[REPLACEABLE] check if a block is bad, using OOB markers
771*4882a593Smuzhiyun  * @block_markbad:	[REPLACEABLE] mark a block bad
772*4882a593Smuzhiyun  * @cmd_ctrl:		[BOARDSPECIFIC] hardwarespecific function for controlling
773*4882a593Smuzhiyun  *			ALE/CLE/nCE. Also used to write command and address
774*4882a593Smuzhiyun  * @dev_ready:		[BOARDSPECIFIC] hardwarespecific function for accessing
775*4882a593Smuzhiyun  *			device ready/busy line. If set to NULL no access to
776*4882a593Smuzhiyun  *			ready/busy is available and the ready/busy information
777*4882a593Smuzhiyun  *			is read from the chip status register.
778*4882a593Smuzhiyun  * @cmdfunc:		[REPLACEABLE] hardwarespecific function for writing
779*4882a593Smuzhiyun  *			commands to the chip.
780*4882a593Smuzhiyun  * @waitfunc:		[REPLACEABLE] hardwarespecific function for wait on
781*4882a593Smuzhiyun  *			ready.
782*4882a593Smuzhiyun  * @setup_read_retry:	[FLASHSPECIFIC] flash (vendor) specific function for
783*4882a593Smuzhiyun  *			setting the read-retry mode. Mostly needed for MLC NAND.
784*4882a593Smuzhiyun  * @ecc:		[BOARDSPECIFIC] ECC control structure
785*4882a593Smuzhiyun  * @buffers:		buffer structure for read/write
786*4882a593Smuzhiyun  * @buf_align:		minimum buffer alignment required by a platform
787*4882a593Smuzhiyun  * @hwcontrol:		platform-specific hardware control structure
788*4882a593Smuzhiyun  * @erase:		[REPLACEABLE] erase function
789*4882a593Smuzhiyun  * @scan_bbt:		[REPLACEABLE] function to scan bad block table
790*4882a593Smuzhiyun  * @chip_delay:		[BOARDSPECIFIC] chip dependent delay for transferring
791*4882a593Smuzhiyun  *			data from array to read regs (tR).
792*4882a593Smuzhiyun  * @state:		[INTERN] the current state of the NAND device
793*4882a593Smuzhiyun  * @oob_poi:		"poison value buffer," used for laying out OOB data
794*4882a593Smuzhiyun  *			before writing
795*4882a593Smuzhiyun  * @page_shift:		[INTERN] number of address bits in a page (column
796*4882a593Smuzhiyun  *			address bits).
797*4882a593Smuzhiyun  * @phys_erase_shift:	[INTERN] number of address bits in a physical eraseblock
798*4882a593Smuzhiyun  * @bbt_erase_shift:	[INTERN] number of address bits in a bbt entry
799*4882a593Smuzhiyun  * @chip_shift:		[INTERN] number of address bits in one chip
800*4882a593Smuzhiyun  * @options:		[BOARDSPECIFIC] various chip options. They can partly
801*4882a593Smuzhiyun  *			be set to inform nand_scan about special functionality.
802*4882a593Smuzhiyun  *			See the defines for further explanation.
803*4882a593Smuzhiyun  * @bbt_options:	[INTERN] bad block specific options. All options used
804*4882a593Smuzhiyun  *			here must come from bbm.h. By default, these options
805*4882a593Smuzhiyun  *			will be copied to the appropriate nand_bbt_descr's.
806*4882a593Smuzhiyun  * @badblockpos:	[INTERN] position of the bad block marker in the oob
807*4882a593Smuzhiyun  *			area.
808*4882a593Smuzhiyun  * @badblockbits:	[INTERN] minimum number of set bits in a good block's
809*4882a593Smuzhiyun  *			bad block marker position; i.e., BBM == 11110111b is
810*4882a593Smuzhiyun  *			not bad when badblockbits == 7
811*4882a593Smuzhiyun  * @bits_per_cell:	[INTERN] number of bits per cell. i.e., 1 means SLC.
812*4882a593Smuzhiyun  * @ecc_strength_ds:	[INTERN] ECC correctability from the datasheet.
813*4882a593Smuzhiyun  *			Minimum amount of bit errors per @ecc_step_ds guaranteed
814*4882a593Smuzhiyun  *			to be correctable. If unknown, set to zero.
815*4882a593Smuzhiyun  * @ecc_step_ds:	[INTERN] ECC step required by the @ecc_strength_ds,
816*4882a593Smuzhiyun  *                      also from the datasheet. It is the recommended ECC step
817*4882a593Smuzhiyun  *			size, if known; if unknown, set to zero.
818*4882a593Smuzhiyun  * @onfi_timing_mode_default: [INTERN] default ONFI timing mode. This field is
819*4882a593Smuzhiyun  *			      set to the actually used ONFI mode if the chip is
820*4882a593Smuzhiyun  *			      ONFI compliant or deduced from the datasheet if
821*4882a593Smuzhiyun  *			      the NAND chip is not ONFI compliant.
822*4882a593Smuzhiyun  * @numchips:		[INTERN] number of physical chips
823*4882a593Smuzhiyun  * @chipsize:		[INTERN] the size of one chip for multichip arrays
824*4882a593Smuzhiyun  * @pagemask:		[INTERN] page number mask = number of (pages / chip) - 1
825*4882a593Smuzhiyun  * @pagebuf:		[INTERN] holds the pagenumber which is currently in
826*4882a593Smuzhiyun  *			data_buf.
827*4882a593Smuzhiyun  * @pagebuf_bitflips:	[INTERN] holds the bitflip count for the page which is
828*4882a593Smuzhiyun  *			currently in data_buf.
829*4882a593Smuzhiyun  * @subpagesize:	[INTERN] holds the subpagesize
830*4882a593Smuzhiyun  * @onfi_version:	[INTERN] holds the chip ONFI version (BCD encoded),
831*4882a593Smuzhiyun  *			non 0 if ONFI supported.
832*4882a593Smuzhiyun  * @jedec_version:	[INTERN] holds the chip JEDEC version (BCD encoded),
833*4882a593Smuzhiyun  *			non 0 if JEDEC supported.
834*4882a593Smuzhiyun  * @onfi_params:	[INTERN] holds the ONFI page parameter when ONFI is
835*4882a593Smuzhiyun  *			supported, 0 otherwise.
836*4882a593Smuzhiyun  * @jedec_params:	[INTERN] holds the JEDEC parameter page when JEDEC is
837*4882a593Smuzhiyun  *			supported, 0 otherwise.
838*4882a593Smuzhiyun  * @read_retries:	[INTERN] the number of read retry modes supported
839*4882a593Smuzhiyun  * @onfi_set_features:	[REPLACEABLE] set the features for ONFI nand
840*4882a593Smuzhiyun  * @onfi_get_features:	[REPLACEABLE] get the features for ONFI nand
841*4882a593Smuzhiyun  * @setup_data_interface: [OPTIONAL] setup the data interface and timing. If
842*4882a593Smuzhiyun  *			  chipnr is set to %NAND_DATA_IFACE_CHECK_ONLY this
843*4882a593Smuzhiyun  *			  means the configuration should not be applied but
844*4882a593Smuzhiyun  *			  only checked.
845*4882a593Smuzhiyun  * @bbt:		[INTERN] bad block table pointer
846*4882a593Smuzhiyun  * @bbt_td:		[REPLACEABLE] bad block table descriptor for flash
847*4882a593Smuzhiyun  *			lookup.
848*4882a593Smuzhiyun  * @bbt_md:		[REPLACEABLE] bad block table mirror descriptor
849*4882a593Smuzhiyun  * @badblock_pattern:	[REPLACEABLE] bad block scan pattern used for initial
850*4882a593Smuzhiyun  *			bad block scan.
851*4882a593Smuzhiyun  * @controller:		[REPLACEABLE] a pointer to a hardware controller
852*4882a593Smuzhiyun  *			structure which is shared among multiple independent
853*4882a593Smuzhiyun  *			devices.
854*4882a593Smuzhiyun  * @priv:		[OPTIONAL] pointer to private chip data
855*4882a593Smuzhiyun  * @write_page:		[REPLACEABLE] High-level page write function
856*4882a593Smuzhiyun  */
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun struct nand_chip {
859*4882a593Smuzhiyun 	struct mtd_info mtd;
860*4882a593Smuzhiyun 	void __iomem *IO_ADDR_R;
861*4882a593Smuzhiyun 	void __iomem *IO_ADDR_W;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	int flash_node;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	uint8_t (*read_byte)(struct mtd_info *mtd);
866*4882a593Smuzhiyun 	u16 (*read_word)(struct mtd_info *mtd);
867*4882a593Smuzhiyun 	void (*write_byte)(struct mtd_info *mtd, uint8_t byte);
868*4882a593Smuzhiyun 	void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
869*4882a593Smuzhiyun 	void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
870*4882a593Smuzhiyun 	void (*select_chip)(struct mtd_info *mtd, int chip);
871*4882a593Smuzhiyun 	int (*block_bad)(struct mtd_info *mtd, loff_t ofs);
872*4882a593Smuzhiyun 	int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
873*4882a593Smuzhiyun 	void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
874*4882a593Smuzhiyun 	int (*dev_ready)(struct mtd_info *mtd);
875*4882a593Smuzhiyun 	void (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column,
876*4882a593Smuzhiyun 			int page_addr);
877*4882a593Smuzhiyun 	int(*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);
878*4882a593Smuzhiyun 	int (*erase)(struct mtd_info *mtd, int page);
879*4882a593Smuzhiyun 	int (*scan_bbt)(struct mtd_info *mtd);
880*4882a593Smuzhiyun 	int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
881*4882a593Smuzhiyun 			uint32_t offset, int data_len, const uint8_t *buf,
882*4882a593Smuzhiyun 			int oob_required, int page, int raw);
883*4882a593Smuzhiyun 	int (*onfi_set_features)(struct mtd_info *mtd, struct nand_chip *chip,
884*4882a593Smuzhiyun 			int feature_addr, uint8_t *subfeature_para);
885*4882a593Smuzhiyun 	int (*onfi_get_features)(struct mtd_info *mtd, struct nand_chip *chip,
886*4882a593Smuzhiyun 			int feature_addr, uint8_t *subfeature_para);
887*4882a593Smuzhiyun 	int (*setup_read_retry)(struct mtd_info *mtd, int retry_mode);
888*4882a593Smuzhiyun 	int (*setup_data_interface)(struct mtd_info *mtd, int chipnr,
889*4882a593Smuzhiyun 				    const struct nand_data_interface *conf);
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	int chip_delay;
893*4882a593Smuzhiyun 	unsigned int options;
894*4882a593Smuzhiyun 	unsigned int bbt_options;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	int page_shift;
897*4882a593Smuzhiyun 	int phys_erase_shift;
898*4882a593Smuzhiyun 	int bbt_erase_shift;
899*4882a593Smuzhiyun 	int chip_shift;
900*4882a593Smuzhiyun 	int numchips;
901*4882a593Smuzhiyun 	uint64_t chipsize;
902*4882a593Smuzhiyun 	int pagemask;
903*4882a593Smuzhiyun 	int pagebuf;
904*4882a593Smuzhiyun 	unsigned int pagebuf_bitflips;
905*4882a593Smuzhiyun 	int subpagesize;
906*4882a593Smuzhiyun 	uint8_t bits_per_cell;
907*4882a593Smuzhiyun 	uint16_t ecc_strength_ds;
908*4882a593Smuzhiyun 	uint16_t ecc_step_ds;
909*4882a593Smuzhiyun 	int onfi_timing_mode_default;
910*4882a593Smuzhiyun 	int badblockpos;
911*4882a593Smuzhiyun 	int badblockbits;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	int onfi_version;
914*4882a593Smuzhiyun 	int jedec_version;
915*4882a593Smuzhiyun #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
916*4882a593Smuzhiyun 	struct nand_onfi_params	onfi_params;
917*4882a593Smuzhiyun #endif
918*4882a593Smuzhiyun 	struct nand_jedec_params jedec_params;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	struct nand_data_interface *data_interface;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	int read_retries;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	flstate_t state;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	uint8_t *oob_poi;
927*4882a593Smuzhiyun 	struct nand_hw_control *controller;
928*4882a593Smuzhiyun 	struct nand_ecclayout *ecclayout;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	struct nand_ecc_ctrl ecc;
931*4882a593Smuzhiyun 	struct nand_buffers *buffers;
932*4882a593Smuzhiyun 	unsigned long buf_align;
933*4882a593Smuzhiyun 	struct nand_hw_control hwcontrol;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	uint8_t *bbt;
936*4882a593Smuzhiyun 	struct nand_bbt_descr *bbt_td;
937*4882a593Smuzhiyun 	struct nand_bbt_descr *bbt_md;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	struct nand_bbt_descr *badblock_pattern;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	void *priv;
942*4882a593Smuzhiyun };
943*4882a593Smuzhiyun 
mtd_to_nand(struct mtd_info * mtd)944*4882a593Smuzhiyun static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun 	return container_of(mtd, struct nand_chip, mtd);
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
nand_to_mtd(struct nand_chip * chip)949*4882a593Smuzhiyun static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun 	return &chip->mtd;
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun 
nand_get_controller_data(struct nand_chip * chip)954*4882a593Smuzhiyun static inline void *nand_get_controller_data(struct nand_chip *chip)
955*4882a593Smuzhiyun {
956*4882a593Smuzhiyun 	return chip->priv;
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun 
nand_set_controller_data(struct nand_chip * chip,void * priv)959*4882a593Smuzhiyun static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
960*4882a593Smuzhiyun {
961*4882a593Smuzhiyun 	chip->priv = priv;
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun /*
965*4882a593Smuzhiyun  * NAND Flash Manufacturer ID Codes
966*4882a593Smuzhiyun  */
967*4882a593Smuzhiyun #define NAND_MFR_TOSHIBA	0x98
968*4882a593Smuzhiyun #define NAND_MFR_SAMSUNG	0xec
969*4882a593Smuzhiyun #define NAND_MFR_FUJITSU	0x04
970*4882a593Smuzhiyun #define NAND_MFR_NATIONAL	0x8f
971*4882a593Smuzhiyun #define NAND_MFR_RENESAS	0x07
972*4882a593Smuzhiyun #define NAND_MFR_STMICRO	0x20
973*4882a593Smuzhiyun #define NAND_MFR_HYNIX		0xad
974*4882a593Smuzhiyun #define NAND_MFR_MICRON		0x2c
975*4882a593Smuzhiyun #define NAND_MFR_AMD		0x01
976*4882a593Smuzhiyun #define NAND_MFR_MACRONIX	0xc2
977*4882a593Smuzhiyun #define NAND_MFR_EON		0x92
978*4882a593Smuzhiyun #define NAND_MFR_SANDISK	0x45
979*4882a593Smuzhiyun #define NAND_MFR_INTEL		0x89
980*4882a593Smuzhiyun #define NAND_MFR_ATO		0x9b
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun /* The maximum expected count of bytes in the NAND ID sequence */
983*4882a593Smuzhiyun #define NAND_MAX_ID_LEN 8
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun /*
986*4882a593Smuzhiyun  * A helper for defining older NAND chips where the second ID byte fully
987*4882a593Smuzhiyun  * defined the chip, including the geometry (chip size, eraseblock size, page
988*4882a593Smuzhiyun  * size). All these chips have 512 bytes NAND page size.
989*4882a593Smuzhiyun  */
990*4882a593Smuzhiyun #define LEGACY_ID_NAND(nm, devid, chipsz, erasesz, opts)          \
991*4882a593Smuzhiyun 	{ .name = (nm), {{ .dev_id = (devid) }}, .pagesize = 512, \
992*4882a593Smuzhiyun 	  .chipsize = (chipsz), .erasesize = (erasesz), .options = (opts) }
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun /*
995*4882a593Smuzhiyun  * A helper for defining newer chips which report their page size and
996*4882a593Smuzhiyun  * eraseblock size via the extended ID bytes.
997*4882a593Smuzhiyun  *
998*4882a593Smuzhiyun  * The real difference between LEGACY_ID_NAND and EXTENDED_ID_NAND is that with
999*4882a593Smuzhiyun  * EXTENDED_ID_NAND, manufacturers overloaded the same device ID so that the
1000*4882a593Smuzhiyun  * device ID now only represented a particular total chip size (and voltage,
1001*4882a593Smuzhiyun  * buswidth), and the page size, eraseblock size, and OOB size could vary while
1002*4882a593Smuzhiyun  * using the same device ID.
1003*4882a593Smuzhiyun  */
1004*4882a593Smuzhiyun #define EXTENDED_ID_NAND(nm, devid, chipsz, opts)                      \
1005*4882a593Smuzhiyun 	{ .name = (nm), {{ .dev_id = (devid) }}, .chipsize = (chipsz), \
1006*4882a593Smuzhiyun 	  .options = (opts) }
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun #define NAND_ECC_INFO(_strength, _step)	\
1009*4882a593Smuzhiyun 			{ .strength_ds = (_strength), .step_ds = (_step) }
1010*4882a593Smuzhiyun #define NAND_ECC_STRENGTH(type)		((type)->ecc.strength_ds)
1011*4882a593Smuzhiyun #define NAND_ECC_STEP(type)		((type)->ecc.step_ds)
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun /**
1014*4882a593Smuzhiyun  * struct nand_flash_dev - NAND Flash Device ID Structure
1015*4882a593Smuzhiyun  * @name: a human-readable name of the NAND chip
1016*4882a593Smuzhiyun  * @dev_id: the device ID (the second byte of the full chip ID array)
1017*4882a593Smuzhiyun  * @mfr_id: manufecturer ID part of the full chip ID array (refers the same
1018*4882a593Smuzhiyun  *          memory address as @id[0])
1019*4882a593Smuzhiyun  * @dev_id: device ID part of the full chip ID array (refers the same memory
1020*4882a593Smuzhiyun  *          address as @id[1])
1021*4882a593Smuzhiyun  * @id: full device ID array
1022*4882a593Smuzhiyun  * @pagesize: size of the NAND page in bytes; if 0, then the real page size (as
1023*4882a593Smuzhiyun  *            well as the eraseblock size) is determined from the extended NAND
1024*4882a593Smuzhiyun  *            chip ID array)
1025*4882a593Smuzhiyun  * @chipsize: total chip size in MiB
1026*4882a593Smuzhiyun  * @erasesize: eraseblock size in bytes (determined from the extended ID if 0)
1027*4882a593Smuzhiyun  * @options: stores various chip bit options
1028*4882a593Smuzhiyun  * @id_len: The valid length of the @id.
1029*4882a593Smuzhiyun  * @oobsize: OOB size
1030*4882a593Smuzhiyun  * @ecc: ECC correctability and step information from the datasheet.
1031*4882a593Smuzhiyun  * @ecc.strength_ds: The ECC correctability from the datasheet, same as the
1032*4882a593Smuzhiyun  *                   @ecc_strength_ds in nand_chip{}.
1033*4882a593Smuzhiyun  * @ecc.step_ds: The ECC step required by the @ecc.strength_ds, same as the
1034*4882a593Smuzhiyun  *               @ecc_step_ds in nand_chip{}, also from the datasheet.
1035*4882a593Smuzhiyun  *               For example, the "4bit ECC for each 512Byte" can be set with
1036*4882a593Smuzhiyun  *               NAND_ECC_INFO(4, 512).
1037*4882a593Smuzhiyun  * @onfi_timing_mode_default: the default ONFI timing mode entered after a NAND
1038*4882a593Smuzhiyun  *			      reset. Should be deduced from timings described
1039*4882a593Smuzhiyun  *			      in the datasheet.
1040*4882a593Smuzhiyun  *
1041*4882a593Smuzhiyun  */
1042*4882a593Smuzhiyun struct nand_flash_dev {
1043*4882a593Smuzhiyun 	char *name;
1044*4882a593Smuzhiyun 	union {
1045*4882a593Smuzhiyun 		struct {
1046*4882a593Smuzhiyun 			uint8_t mfr_id;
1047*4882a593Smuzhiyun 			uint8_t dev_id;
1048*4882a593Smuzhiyun 		};
1049*4882a593Smuzhiyun 		uint8_t id[NAND_MAX_ID_LEN];
1050*4882a593Smuzhiyun 	};
1051*4882a593Smuzhiyun 	unsigned int pagesize;
1052*4882a593Smuzhiyun 	unsigned int chipsize;
1053*4882a593Smuzhiyun 	unsigned int erasesize;
1054*4882a593Smuzhiyun 	unsigned int options;
1055*4882a593Smuzhiyun 	uint16_t id_len;
1056*4882a593Smuzhiyun 	uint16_t oobsize;
1057*4882a593Smuzhiyun 	struct {
1058*4882a593Smuzhiyun 		uint16_t strength_ds;
1059*4882a593Smuzhiyun 		uint16_t step_ds;
1060*4882a593Smuzhiyun 	} ecc;
1061*4882a593Smuzhiyun 	int onfi_timing_mode_default;
1062*4882a593Smuzhiyun };
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun /**
1065*4882a593Smuzhiyun  * struct nand_manufacturers - NAND Flash Manufacturer ID Structure
1066*4882a593Smuzhiyun  * @name:	Manufacturer name
1067*4882a593Smuzhiyun  * @id:		manufacturer ID code of device.
1068*4882a593Smuzhiyun */
1069*4882a593Smuzhiyun struct nand_manufacturers {
1070*4882a593Smuzhiyun 	int id;
1071*4882a593Smuzhiyun 	char *name;
1072*4882a593Smuzhiyun };
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun extern struct nand_flash_dev nand_flash_ids[];
1075*4882a593Smuzhiyun extern struct nand_manufacturers nand_manuf_ids[];
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun extern int nand_default_bbt(struct mtd_info *mtd);
1078*4882a593Smuzhiyun extern int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs);
1079*4882a593Smuzhiyun extern int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs);
1080*4882a593Smuzhiyun extern int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt);
1081*4882a593Smuzhiyun extern int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
1082*4882a593Smuzhiyun 			   int allowbbt);
1083*4882a593Smuzhiyun extern int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
1084*4882a593Smuzhiyun 			size_t *retlen, uint8_t *buf);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun /*
1087*4882a593Smuzhiyun * Constants for oob configuration
1088*4882a593Smuzhiyun */
1089*4882a593Smuzhiyun #define NAND_SMALL_BADBLOCK_POS		5
1090*4882a593Smuzhiyun #define NAND_LARGE_BADBLOCK_POS		0
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun /**
1093*4882a593Smuzhiyun  * struct platform_nand_chip - chip level device structure
1094*4882a593Smuzhiyun  * @nr_chips:		max. number of chips to scan for
1095*4882a593Smuzhiyun  * @chip_offset:	chip number offset
1096*4882a593Smuzhiyun  * @nr_partitions:	number of partitions pointed to by partitions (or zero)
1097*4882a593Smuzhiyun  * @partitions:		mtd partition list
1098*4882a593Smuzhiyun  * @chip_delay:		R/B delay value in us
1099*4882a593Smuzhiyun  * @options:		Option flags, e.g. 16bit buswidth
1100*4882a593Smuzhiyun  * @bbt_options:	BBT option flags, e.g. NAND_BBT_USE_FLASH
1101*4882a593Smuzhiyun  * @part_probe_types:	NULL-terminated array of probe types
1102*4882a593Smuzhiyun  */
1103*4882a593Smuzhiyun struct platform_nand_chip {
1104*4882a593Smuzhiyun 	int nr_chips;
1105*4882a593Smuzhiyun 	int chip_offset;
1106*4882a593Smuzhiyun 	int nr_partitions;
1107*4882a593Smuzhiyun 	struct mtd_partition *partitions;
1108*4882a593Smuzhiyun 	int chip_delay;
1109*4882a593Smuzhiyun 	unsigned int options;
1110*4882a593Smuzhiyun 	unsigned int bbt_options;
1111*4882a593Smuzhiyun 	const char **part_probe_types;
1112*4882a593Smuzhiyun };
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun /* Keep gcc happy */
1115*4882a593Smuzhiyun struct platform_device;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun /**
1118*4882a593Smuzhiyun  * struct platform_nand_ctrl - controller level device structure
1119*4882a593Smuzhiyun  * @probe:		platform specific function to probe/setup hardware
1120*4882a593Smuzhiyun  * @remove:		platform specific function to remove/teardown hardware
1121*4882a593Smuzhiyun  * @hwcontrol:		platform specific hardware control structure
1122*4882a593Smuzhiyun  * @dev_ready:		platform specific function to read ready/busy pin
1123*4882a593Smuzhiyun  * @select_chip:	platform specific chip select function
1124*4882a593Smuzhiyun  * @cmd_ctrl:		platform specific function for controlling
1125*4882a593Smuzhiyun  *			ALE/CLE/nCE. Also used to write command and address
1126*4882a593Smuzhiyun  * @write_buf:		platform specific function for write buffer
1127*4882a593Smuzhiyun  * @read_buf:		platform specific function for read buffer
1128*4882a593Smuzhiyun  * @read_byte:		platform specific function to read one byte from chip
1129*4882a593Smuzhiyun  * @priv:		private data to transport driver specific settings
1130*4882a593Smuzhiyun  *
1131*4882a593Smuzhiyun  * All fields are optional and depend on the hardware driver requirements
1132*4882a593Smuzhiyun  */
1133*4882a593Smuzhiyun struct platform_nand_ctrl {
1134*4882a593Smuzhiyun 	int (*probe)(struct platform_device *pdev);
1135*4882a593Smuzhiyun 	void (*remove)(struct platform_device *pdev);
1136*4882a593Smuzhiyun 	void (*hwcontrol)(struct mtd_info *mtd, int cmd);
1137*4882a593Smuzhiyun 	int (*dev_ready)(struct mtd_info *mtd);
1138*4882a593Smuzhiyun 	void (*select_chip)(struct mtd_info *mtd, int chip);
1139*4882a593Smuzhiyun 	void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
1140*4882a593Smuzhiyun 	void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
1141*4882a593Smuzhiyun 	void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
1142*4882a593Smuzhiyun 	unsigned char (*read_byte)(struct mtd_info *mtd);
1143*4882a593Smuzhiyun 	void *priv;
1144*4882a593Smuzhiyun };
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun /**
1147*4882a593Smuzhiyun  * struct platform_nand_data - container structure for platform-specific data
1148*4882a593Smuzhiyun  * @chip:		chip level chip structure
1149*4882a593Smuzhiyun  * @ctrl:		controller level device structure
1150*4882a593Smuzhiyun  */
1151*4882a593Smuzhiyun struct platform_nand_data {
1152*4882a593Smuzhiyun 	struct platform_nand_chip chip;
1153*4882a593Smuzhiyun 	struct platform_nand_ctrl ctrl;
1154*4882a593Smuzhiyun };
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
1157*4882a593Smuzhiyun /* return the supported features. */
onfi_feature(struct nand_chip * chip)1158*4882a593Smuzhiyun static inline int onfi_feature(struct nand_chip *chip)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun 	return chip->onfi_version ? le16_to_cpu(chip->onfi_params.features) : 0;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun /* return the supported asynchronous timing mode. */
onfi_get_async_timing_mode(struct nand_chip * chip)1164*4882a593Smuzhiyun static inline int onfi_get_async_timing_mode(struct nand_chip *chip)
1165*4882a593Smuzhiyun {
1166*4882a593Smuzhiyun 	if (!chip->onfi_version)
1167*4882a593Smuzhiyun 		return ONFI_TIMING_MODE_UNKNOWN;
1168*4882a593Smuzhiyun 	return le16_to_cpu(chip->onfi_params.async_timing_mode);
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /* return the supported synchronous timing mode. */
onfi_get_sync_timing_mode(struct nand_chip * chip)1172*4882a593Smuzhiyun static inline int onfi_get_sync_timing_mode(struct nand_chip *chip)
1173*4882a593Smuzhiyun {
1174*4882a593Smuzhiyun 	if (!chip->onfi_version)
1175*4882a593Smuzhiyun 		return ONFI_TIMING_MODE_UNKNOWN;
1176*4882a593Smuzhiyun 	return le16_to_cpu(chip->onfi_params.src_sync_timing_mode);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun #endif
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun int onfi_init_data_interface(struct nand_chip *chip,
1181*4882a593Smuzhiyun 			     struct nand_data_interface *iface,
1182*4882a593Smuzhiyun 			     enum nand_data_interface_type type,
1183*4882a593Smuzhiyun 			     int timing_mode);
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun /*
1186*4882a593Smuzhiyun  * Check if it is a SLC nand.
1187*4882a593Smuzhiyun  * The !nand_is_slc() can be used to check the MLC/TLC nand chips.
1188*4882a593Smuzhiyun  * We do not distinguish the MLC and TLC now.
1189*4882a593Smuzhiyun  */
nand_is_slc(struct nand_chip * chip)1190*4882a593Smuzhiyun static inline bool nand_is_slc(struct nand_chip *chip)
1191*4882a593Smuzhiyun {
1192*4882a593Smuzhiyun 	return chip->bits_per_cell == 1;
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun /**
1196*4882a593Smuzhiyun  * Check if the opcode's address should be sent only on the lower 8 bits
1197*4882a593Smuzhiyun  * @command: opcode to check
1198*4882a593Smuzhiyun  */
nand_opcode_8bits(unsigned int command)1199*4882a593Smuzhiyun static inline int nand_opcode_8bits(unsigned int command)
1200*4882a593Smuzhiyun {
1201*4882a593Smuzhiyun 	switch (command) {
1202*4882a593Smuzhiyun 	case NAND_CMD_READID:
1203*4882a593Smuzhiyun 	case NAND_CMD_PARAM:
1204*4882a593Smuzhiyun 	case NAND_CMD_GET_FEATURES:
1205*4882a593Smuzhiyun 	case NAND_CMD_SET_FEATURES:
1206*4882a593Smuzhiyun 		return 1;
1207*4882a593Smuzhiyun 	default:
1208*4882a593Smuzhiyun 		break;
1209*4882a593Smuzhiyun 	}
1210*4882a593Smuzhiyun 	return 0;
1211*4882a593Smuzhiyun }
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun /* return the supported JEDEC features. */
jedec_feature(struct nand_chip * chip)1214*4882a593Smuzhiyun static inline int jedec_feature(struct nand_chip *chip)
1215*4882a593Smuzhiyun {
1216*4882a593Smuzhiyun 	return chip->jedec_version ? le16_to_cpu(chip->jedec_params.features)
1217*4882a593Smuzhiyun 		: 0;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun /* Standard NAND functions from nand_base.c */
1221*4882a593Smuzhiyun void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
1222*4882a593Smuzhiyun void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len);
1223*4882a593Smuzhiyun void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len);
1224*4882a593Smuzhiyun void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len);
1225*4882a593Smuzhiyun uint8_t nand_read_byte(struct mtd_info *mtd);
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun /* get timing characteristics from ONFI timing mode. */
1228*4882a593Smuzhiyun const struct nand_sdr_timings *onfi_async_timing_mode_to_sdr_timings(int mode);
1229*4882a593Smuzhiyun /* get data interface from ONFI timing mode 0, used after reset. */
1230*4882a593Smuzhiyun const struct nand_data_interface *nand_get_default_data_interface(void);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun int nand_check_erased_ecc_chunk(void *data, int datalen,
1233*4882a593Smuzhiyun 				void *ecc, int ecclen,
1234*4882a593Smuzhiyun 				void *extraoob, int extraooblen,
1235*4882a593Smuzhiyun 				int threshold);
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun int nand_check_ecc_caps(struct nand_chip *chip,
1238*4882a593Smuzhiyun 			const struct nand_ecc_caps *caps, int oobavail);
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun int nand_match_ecc_req(struct nand_chip *chip,
1241*4882a593Smuzhiyun 		       const struct nand_ecc_caps *caps,  int oobavail);
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun int nand_maximize_ecc(struct nand_chip *chip,
1244*4882a593Smuzhiyun 		      const struct nand_ecc_caps *caps, int oobavail);
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun /* Reset and initialize a NAND device */
1247*4882a593Smuzhiyun int nand_reset(struct nand_chip *chip, int chipnr);
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun /* NAND operation helpers */
1250*4882a593Smuzhiyun int nand_reset_op(struct nand_chip *chip);
1251*4882a593Smuzhiyun int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1252*4882a593Smuzhiyun 		   unsigned int len);
1253*4882a593Smuzhiyun int nand_status_op(struct nand_chip *chip, u8 *status);
1254*4882a593Smuzhiyun int nand_exit_status_op(struct nand_chip *chip);
1255*4882a593Smuzhiyun int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock);
1256*4882a593Smuzhiyun int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1257*4882a593Smuzhiyun 		      unsigned int offset_in_page, void *buf, unsigned int len);
1258*4882a593Smuzhiyun int nand_change_read_column_op(struct nand_chip *chip,
1259*4882a593Smuzhiyun 			       unsigned int offset_in_page, void *buf,
1260*4882a593Smuzhiyun 			       unsigned int len, bool force_8bit);
1261*4882a593Smuzhiyun int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1262*4882a593Smuzhiyun 		     unsigned int offset_in_page, void *buf, unsigned int len);
1263*4882a593Smuzhiyun int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1264*4882a593Smuzhiyun 			    unsigned int offset_in_page, const void *buf,
1265*4882a593Smuzhiyun 			    unsigned int len);
1266*4882a593Smuzhiyun int nand_prog_page_end_op(struct nand_chip *chip);
1267*4882a593Smuzhiyun int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1268*4882a593Smuzhiyun 		      unsigned int offset_in_page, const void *buf,
1269*4882a593Smuzhiyun 		      unsigned int len);
1270*4882a593Smuzhiyun int nand_change_write_column_op(struct nand_chip *chip,
1271*4882a593Smuzhiyun 				unsigned int offset_in_page, const void *buf,
1272*4882a593Smuzhiyun 				unsigned int len, bool force_8bit);
1273*4882a593Smuzhiyun int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
1274*4882a593Smuzhiyun 		      bool force_8bit);
1275*4882a593Smuzhiyun int nand_write_data_op(struct nand_chip *chip, const void *buf,
1276*4882a593Smuzhiyun 		       unsigned int len, bool force_8bit);
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun #endif /* __LINUX_MTD_RAWNAND_H */
1279