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