xref: /netbsd-src/sys/dev/nor/cfi.h (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: cfi.h,v 1.5 2011/08/02 01:11:08 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 
18 typedef enum {
19 	CFI_STATE_DATA_ARRAY = 0,
20 	CFI_STATE_QUERY,
21 	/* TBD */
22 } cfi_state_t;
23 
24 
25 struct 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  */
33 struct 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
78 static inline const char *
79 cfi_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  */
114 struct 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  */
126 struct 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 
136 struct cfi;	/* fwd ref */
137 
138 struct 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 #ifdef CFI_0002_STATS
151 struct cfi_0002_stats {
152 	u_long read_page;
153 	u_long program_page;
154 	u_long erase_all;
155 	u_long erase_block;
156 	u_long busy;
157 	u_long busy_usec_min;
158 	u_long busy_usec_max;
159 	struct timeval busy_poll_tv;
160 	struct timeval busy_yield_tv;
161 	u_long busy_poll;
162 	u_long busy_yield;
163 	u_long busy_yield_hit;
164 	u_long busy_yield_miss;
165 	u_long busy_yield_timo;
166 };
167 
168 extern void cfi_0002_stats_reset(struct cfi *);
169 extern void cfi_0002_stats_print(struct cfi *);
170 #define CFI_0002_STATS_INIT(dev, cfi)			\
171     do {						\
172 	aprint_normal_dev(dev, "cfi=%p\n", cfi);	\
173 	cfi_0002_stats_reset(cfi);			\
174     } while (0)
175 #define CFI_0002_STATS_INC(cfi, field)	(cfi)->cfi_0002_stats.field++
176 
177 #else
178 
179 #define CFI_0002_STATS_INIT(dev, cfi)
180 #define CFI_0002_STATS_INC(cfi, field)
181 
182 #endif	/* CFI_0002_STATS */
183 
184 struct cfi {
185 	bus_space_tag_t		cfi_bst;
186 	bus_space_handle_t	cfi_bsh;
187 	cfi_state_t		cfi_state;
188 	uint8_t			cfi_portwidth;	/* port width, 1<<N bytes */
189 	uint8_t			cfi_chipwidth;	/* chip width, 1<<N bytes */
190 	bool			cfi_emulated;	/* ary data are faked */
191 	struct cfi_query_data	cfi_qry_data;	/* CFI Query data */
192 	struct cfi_jedec_id_data
193 				cfi_id_data;	/* JEDEC ID data */
194 	const char	       *cfi_name;	/* optional chip name */
195 	const struct cfi_opmodes
196 			       *cfi_opmode;
197 	struct cfi_ops		cfi_ops;	/* chip dependent functions */
198 	u_long			cfi_yield_time;	/* thresh. for yield in wait */
199 #ifdef CFI_0002_STATS
200 	struct cfi_0002_stats	cfi_0002_stats;
201 #endif
202 };
203 
204 /*
205  * struct cfi_jedec_tab is an amalgamation of
206  * - info to identify a chip based on JEDEC ID data, and
207  * - values needed to fill in struct cfi (i.e. fields we depend on)
208  */
209 struct cfi_jedec_tab {
210 	/* ID */
211 	const char     *jt_name;
212 	uint32_t	jt_mid;
213 	uint32_t	jt_did;
214 	/* cmdset */
215 	uint16_t	jt_id_pri;
216 	uint16_t	jt_id_alt;
217 	/* geometry */
218 	uint8_t		jt_device_size;			/* 1<<N bytes */
219 	uint16_t	jt_interface_code_desc;
220 	uint8_t		jt_write_nbyte_size_max;	/* 1<<N bytes */
221 	uint8_t		jt_erase_blk_regions;
222 	struct cfi_erase_blk_info
223 			jt_erase_blk_info[4];
224 	/* timing */
225 	uint8_t		jt_write_word_time_typ;		/* 1<<N usec */
226 	uint8_t		jt_write_nbyte_time_typ;	/* 1<<N msec */
227 	uint8_t		jt_erase_blk_time_typ;		/* 1<<N msec */
228 	uint8_t		jt_erase_chip_time_typ;		/* 1<<N msec */
229 	uint8_t		jt_write_word_time_max;		/* typ<<N usec */
230 	uint8_t		jt_write_nbyte_time_max;	/* typ<<N msec */
231 	uint8_t		jt_erase_blk_time_max;		/* typ<<N msec */
232 	uint8_t		jt_erase_chip_time_max;		/* typ<<N msec */
233 	/* XXX operation mode, perhaps cannot be tabulated */
234 	const struct cfi_opmodes
235 		       *jt_opmode;
236 };
237 
238 
239 enum {
240 	CFI_ADDRESS_ANY = 0x00,		    /* XXX "don't care" */
241 
242 	CFI_RESET_DATA = 0xf0,
243 	CFI_ALT_RESET_DATA = 0xff,
244 
245 	CFI_QUERY_MODE_ADDRESS = 0x55,	    /* some devices accept anything */
246 	CFI_QUERY_MODE_ALT_ADDRESS = 0x555,
247 	CFI_QUERY_DATA = 0x98,
248 };
249 
250 static inline void
251 cfi_reset(struct cfi * const cfi)
252 {
253 	KASSERT(cfi->cfi_ops.cfi_reset != NULL);
254 	cfi->cfi_ops.cfi_reset(cfi);
255 }
256 
257 static inline int
258 cfi_erase_sector(struct cfi * const cfi, flash_off_t offset)
259 {
260 	KASSERT(cfi->cfi_ops.cfi_erase_sector != NULL);
261 	return cfi->cfi_ops.cfi_erase_sector(cfi, offset);
262 }
263 
264 static inline int
265 cfi_program_word(struct cfi * const cfi, flash_off_t offset)
266 {
267 	KASSERT(cfi->cfi_ops.cfi_program_word != NULL);
268 	return cfi->cfi_ops.cfi_program_word(cfi, offset);
269 }
270 
271 extern const struct nor_interface nor_interface_cfi;
272 
273 extern bool cfi_probe(struct cfi * const);
274 extern bool cfi_identify(struct cfi * const);
275 extern void cfi_print(device_t, struct cfi * const);
276 extern void cfi_reset_default(struct cfi * const);
277 extern void cfi_reset_std(struct cfi * const);
278 extern void cfi_reset_alt(struct cfi * const);
279 extern void cfi_cmd(struct cfi * const, bus_size_t, uint32_t);
280 
281 #endif	/* _CFI_H_ */
282