cfi.h revision 1.4
1/*	$NetBSD: cfi.h,v 1.4 2011/07/23 06:27:09 cliff Exp $	*/
2
3#ifndef _CFI_H_
4#define _CFI_H_
5
6#include <dev/nor/cfi_0002.h>
7#include <sys/bus.h>
8
9/*
10 * minimum size to bus_space_map for probe/identify QRY:
11 * larget offset needed is CFI_QUERY_MODE_ALT_ADDRESS
12 * scaled by maximum attempted port width, so
13 *	min >= (0x555 * sizeof(uint32_t))
14 */
15#define CFI_QRY_MIN_MAP_SIZE	0x2000
16
17
18typedef enum {
19	CFI_STATE_DATA_ARRAY = 0,
20	CFI_STATE_QUERY,
21	/* TBD */
22} cfi_state_t;
23
24
25struct cfi_erase_blk_info {
26	uint16_t z;			/* Erase Blocks are z * 256 bytes */
27	uint16_t y;			/* y+1 = #Erase Blocks in region */
28};
29
30/*
31 * CFI Query structure
32 */
33struct cfi_query_data {
34    /* Query info */
35    uint8_t	qry[3];			/* { 'Q', 'R', 'Y' } */
36    uint16_t	id_pri;			/* primary comand set ID */
37    uint16_t	addr_pri;		/* primary table addr */
38    uint16_t	id_alt;			/* alternate command set ID */
39    uint16_t	addr_alt;		/* alternate table addr */
40    /* System Interface info */
41    uint8_t	vcc_min;		/* min Vcc */
42    uint8_t	vcc_max;		/* max Vcc */
43    uint8_t	vpp_min;		/* min Vpp */
44    uint8_t	vpp_max;		/* max Vpp */
45    uint8_t	write_word_time_typ;	/* typ 1-word timeout, 1<<N usec */
46    uint8_t	write_nbyte_time_typ;	/* typ multi-byte timeout, 1<<N usec */
47    uint8_t	erase_blk_time_typ;	/* typ 1-blk erase timeout, 1<<N msec */
48    uint8_t	erase_chip_time_typ;	/* typ chip erase timeout, 1<<N msec */
49    uint8_t	write_word_time_max;	/* max 1-word timeout, typ<<N */
50    uint8_t	write_nbyte_time_max;	/* max multi-byte timeout, typ<<N */
51    uint8_t	erase_blk_time_max;	/* max 1-blk erase timeout, typ<<N */
52    uint8_t	erase_chip_time_max;	/* max chip erase timeout, typ<<N */
53    /* Device Geometry Definition */
54    uint8_t	device_size;		/* 1<<N bytes */
55    uint16_t	interface_code_desc;	/* JEP137 interface code description */
56    uint16_t	write_nbyte_size_max;	/* max size of multi-byte write, 1<<N */
57    uint8_t	erase_blk_regions;	/* number of erase block regions */
58    struct cfi_erase_blk_info
59		erase_blk_info[4];	/* describe erase block regions */
60    /* Vendor-specific Primary command set info */
61    union {
62	struct cmdset_0002_query_data cmd_0002;
63    } pri;
64#ifdef NOTYET
65    /* Vendor-specific Alternate command set info */
66    union {
67	/* some command set structure here */
68    } pri;
69#endif
70};
71
72/*
73 * decode interface_code_desc
74 */
75#define CFI_IFCODE_X8		0
76#define CFI_IFCODE_X16		1
77#define CFI_IFCODE_X8X16	2
78static inline const char *
79cfi_interface_desc_str(uint16_t icd)
80{
81	switch(icd) {
82	case CFI_IFCODE_X8:
83		return "x8";
84	case CFI_IFCODE_X16:
85		return "x16";
86	case CFI_IFCODE_X8X16:
87		return "x8/x16";
88	default:
89		return "";
90	}
91}
92
93/*
94 * id_pri: CFI Command set and control assignments
95 */
96#define CFI_ID_PRI_NONE		0x0000
97#define CFI_ID_PRI_INTEL_EXT	0x0001
98#define CFI_ID_PRI_AMD_STD	0x0002
99#define CFI_ID_PRI_INTEL_STD	0x0003
100#define CFI_ID_PRI_AMD_EXT	0x0004
101#define CFI_ID_PRI_WINBOND	0x0005
102#define CFI_ID_PRI_ST_ADV	0x0020
103#define CFI_ID_PRI_MITSU_ADV	0x0100
104#define CFI_ID_PRI_MITSU_EXT	0x0101
105#define CFI_ID_PRI_SST_PAGE	0x0102
106#define CFI_ID_PRI_SST_OLD	0x0701
107#define CFI_ID_PRI_INTEL_PERF	0x0200
108#define CFI_ID_PRI_INTEL_DATA	0x0210
109#define CFI_ID_PRI_RESV		0xffff	/* not allowed, reserved */
110
111/*
112 * JEDEC ID (autoselect) data
113 */
114struct cfi_jedec_id_data {
115	uint16_t	id_mid;		/* manufacturer ID */
116	uint16_t	id_did[3];	/* device ID */
117	uint16_t	id_prot_state;
118	uint16_t	id_indicators;
119	uint8_t		id_swb_lo;	/* lower software bits */
120	uint8_t		id_swb_hi;	/* upper software bits */
121};
122
123/*
124 * table entry used to determine operating mode by QRY signature
125 */
126struct cfi_opmodes {
127	uint8_t		portwidth;	/* (1<<N) bytes */
128	uint8_t		chipwidth;	/* (1<<N) bytes */
129	uint8_t		interleave;	/* (1<<N) bytes */
130	uint8_t		qsa;		/* Query Start Address (in bytes) */
131	uint8_t		len;		/* signature length */
132	const uint8_t  *sig;		/* signature */
133	const char     *str;		/* descriptive string */
134};
135
136struct cfi;	/* fwd ref */
137
138struct cfi_ops {
139	void	(*cfi_reset)(struct cfi *);
140	int 	(*cfi_busy)(struct cfi *, flash_off_t);
141	int	(*cfi_program_word)(struct cfi *, flash_off_t);
142	int	(*cfi_erase_sector)(struct cfi *, flash_off_t);
143};
144
145/* NOTE:
146 * CFI_0002_STATS are just meant temporarily for debugging
147 * not for long-term use. Some event counters at the flash and nor
148 * layers might be helpful eventually
149 */
150#define CFI_0002_STATS	/* XXX TMP */
151#ifdef CFI_0002_STATS
152struct cfi_0002_stats {
153	u_long read_page;
154	u_long program_page;
155	u_long erase_all;
156	u_long erase_block;
157	u_long busy;
158	u_long busy_usec_min;
159	u_long busy_usec_max;
160	struct timeval busy_poll_tv;
161	struct timeval busy_yield_tv;
162	u_long busy_poll;
163	u_long busy_yield;
164	u_long busy_yield_hit;
165	u_long busy_yield_miss;
166	u_long busy_yield_timo;
167};
168
169extern void cfi_0002_stats_reset(struct cfi *);
170extern void cfi_0002_stats_print(struct cfi *);
171#define CFI_0002_STATS_INIT(dev, cfi)			\
172    do {						\
173	aprint_normal_dev(dev, "cfi=%p\n", cfi);	\
174	cfi_0002_stats_reset(cfi);			\
175    } while (0)
176#define CFI_0002_STATS_INC(cfi, field)	(cfi)->cfi_0002_stats.field++
177
178#else
179
180#define CFI_0002_STATS_INIT(cfi)
181#define CFI_0002_STATS_INC(cfi, field)
182
183#endif	/* CFI_0002_STATS */
184
185struct cfi {
186	bus_space_tag_t		cfi_bst;
187	bus_space_handle_t	cfi_bsh;
188	cfi_state_t		cfi_state;
189	uint8_t			cfi_portwidth;	/* port width, 1<<N bytes */
190	uint8_t			cfi_chipwidth;	/* chip width, 1<<N bytes */
191	bool			cfi_emulated;	/* ary data are faked */
192	struct cfi_query_data	cfi_qry_data;	/* CFI Query data */
193	struct cfi_jedec_id_data
194				cfi_id_data;	/* JEDEC ID data */
195	const char	       *cfi_name;	/* optional chip name */
196	const struct cfi_opmodes
197			       *cfi_opmode;
198	struct cfi_ops		cfi_ops;	/* chip dependent functions */
199	u_long			cfi_yield_time;	/* thresh. for yield in wait */
200#ifdef CFI_0002_STATS
201	struct cfi_0002_stats	cfi_0002_stats;
202#endif
203};
204
205/*
206 * struct cfi_jedec_tab is an amalgamation of
207 * - info to identify a chip based on JEDEC ID data, and
208 * - values needed to fill in struct cfi (i.e. fields we depend on)
209 */
210struct cfi_jedec_tab {
211	/* ID */
212	const char     *jt_name;
213	uint32_t	jt_mid;
214	uint32_t	jt_did;
215	/* cmdset */
216	uint16_t	jt_id_pri;
217	uint16_t	jt_id_alt;
218	/* geometry */
219	uint8_t		jt_device_size;			/* 1<<N bytes */
220	uint16_t	jt_interface_code_desc;
221	uint8_t		jt_write_nbyte_size_max;	/* 1<<N bytes */
222	uint8_t		jt_erase_blk_regions;
223	struct cfi_erase_blk_info
224			jt_erase_blk_info[4];
225	/* timing */
226	uint8_t		jt_write_word_time_typ;		/* 1<<N usec */
227	uint8_t		jt_write_nbyte_time_typ;	/* 1<<N msec */
228	uint8_t		jt_erase_blk_time_typ;		/* 1<<N msec */
229	uint8_t		jt_erase_chip_time_typ;		/* 1<<N msec */
230	uint8_t		jt_write_word_time_max;		/* typ<<N usec */
231	uint8_t		jt_write_nbyte_time_max;	/* typ<<N msec */
232	uint8_t		jt_erase_blk_time_max;		/* typ<<N msec */
233	uint8_t		jt_erase_chip_time_max;		/* typ<<N msec */
234	/* XXX operation mode, perhaps cannot be tabulated */
235	const struct cfi_opmodes
236		       *jt_opmode;
237};
238
239
240enum {
241	CFI_ADDRESS_ANY = 0x00,		    /* XXX "don't care" */
242
243	CFI_RESET_DATA = 0xf0,
244	CFI_ALT_RESET_DATA = 0xff,
245
246	CFI_QUERY_MODE_ADDRESS = 0x55,	    /* some devices accept anything */
247	CFI_QUERY_MODE_ALT_ADDRESS = 0x555,
248	CFI_QUERY_DATA = 0x98,
249};
250
251static inline void
252cfi_reset(struct cfi * const cfi)
253{
254	KASSERT(cfi->cfi_ops.cfi_reset != NULL);
255	cfi->cfi_ops.cfi_reset(cfi);
256}
257
258static inline int
259cfi_erase_sector(struct cfi * const cfi, flash_off_t offset)
260{
261	KASSERT(cfi->cfi_ops.cfi_erase_sector != NULL);
262	return cfi->cfi_ops.cfi_erase_sector(cfi, offset);
263}
264
265static inline int
266cfi_program_word(struct cfi * const cfi, flash_off_t offset)
267{
268	KASSERT(cfi->cfi_ops.cfi_program_word != NULL);
269	return cfi->cfi_ops.cfi_program_word(cfi, offset);
270}
271
272extern const struct nor_interface nor_interface_cfi;
273
274extern bool cfi_probe(struct cfi * const);
275extern bool cfi_identify(struct cfi * const);
276extern void cfi_print(device_t, struct cfi * const);
277extern void cfi_reset_default(struct cfi * const);
278extern void cfi_reset_std(struct cfi * const);
279extern void cfi_reset_alt(struct cfi * const);
280extern void cfi_cmd(struct cfi * const, bus_size_t, uint32_t);
281
282#endif	/* _CFI_H_ */
283