1/*	$NetBSD: cfi.h,v 1.8 2022/01/05 16:01:54 andvar 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 command 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
123struct cfi;	/* fwd ref */
124
125struct cfi_ops {
126	void	(*cfi_reset)(struct cfi *);
127	int 	(*cfi_busy)(struct cfi *, flash_off_t);
128	int	(*cfi_program_word)(struct cfi *, flash_off_t);
129	int	(*cfi_erase_sector)(struct cfi *, flash_off_t);
130};
131
132/* NOTE:
133 * CFI_0002_STATS are just meant temporarily for debugging
134 * not for long-term use. Some event counters at the flash and nor
135 * layers might be helpful eventually
136 */
137#ifdef CFI_0002_STATS
138struct cfi_0002_stats {
139	u_long read_page;
140	u_long program_page;
141	u_long erase_all;
142	u_long erase_block;
143	u_long busy;
144	u_long busy_usec_min;
145	u_long busy_usec_max;
146	struct timeval busy_poll_tv;
147	struct timeval busy_yield_tv;
148	u_long busy_poll;
149	u_long busy_yield;
150	u_long busy_yield_hit;
151	u_long busy_yield_miss;
152	u_long busy_yield_timo;
153};
154
155extern void cfi_0002_stats_reset(struct cfi *);
156extern void cfi_0002_stats_print(struct cfi *);
157#define CFI_0002_STATS_INIT(dev, cfi)			\
158    do {						\
159	aprint_normal_dev(dev, "cfi=%p\n", cfi);	\
160	cfi_0002_stats_reset(cfi);			\
161    } while (0)
162#define CFI_0002_STATS_INC(cfi, field)	(cfi)->cfi_0002_stats.field++
163
164#else
165
166#define CFI_0002_STATS_INIT(dev, cfi)
167#define CFI_0002_STATS_INC(cfi, field)
168
169#endif	/* CFI_0002_STATS */
170
171struct cfi {
172	bus_space_tag_t		cfi_bst;
173	bus_space_handle_t	cfi_bsh;
174	cfi_state_t		cfi_state;
175	uint8_t			cfi_portwidth;	/* port width, 1<<N bytes */
176	uint8_t			cfi_chipwidth;	/* chip width, 1<<N bytes */
177	bool			cfi_emulated;	/* ary data are faked */
178	bus_size_t		cfi_unlock_addr1;
179	bus_size_t		cfi_unlock_addr2;
180	struct cfi_query_data	cfi_qry_data;	/* CFI Query data */
181	struct cfi_jedec_id_data
182				cfi_id_data;	/* JEDEC ID data */
183	const char	       *cfi_name;	/* optional chip name */
184	struct cfi_ops		cfi_ops;	/* chip dependent functions */
185	u_long			cfi_yield_time;	/* thresh. for yield in wait */
186#ifdef CFI_0002_STATS
187	struct cfi_0002_stats	cfi_0002_stats;
188#endif
189};
190
191/*
192 * struct cfi_jedec_tab is an amalgamation of
193 * - info to identify a chip based on JEDEC ID data, and
194 * - values needed to fill in struct cfi (i.e. fields we depend on)
195 */
196struct cfi_jedec_tab {
197	/* ID */
198	const char     *jt_name;
199	uint32_t	jt_mid;
200	uint32_t	jt_did;
201	/* cmdset */
202	uint16_t	jt_id_pri;
203	uint16_t	jt_id_alt;
204	/* geometry */
205	uint8_t		jt_device_size;			/* 1<<N bytes */
206	uint16_t	jt_interface_code_desc;
207	uint8_t		jt_write_nbyte_size_max;	/* 1<<N bytes */
208	uint8_t		jt_erase_blk_regions;
209	struct cfi_erase_blk_info
210			jt_erase_blk_info[4];
211	/* timing */
212	uint8_t		jt_write_word_time_typ;		/* 1<<N usec */
213	uint8_t		jt_write_nbyte_time_typ;	/* 1<<N msec */
214	uint8_t		jt_erase_blk_time_typ;		/* 1<<N msec */
215	uint8_t		jt_erase_chip_time_typ;		/* 1<<N msec */
216	uint8_t		jt_write_word_time_max;		/* typ<<N usec */
217	uint8_t		jt_write_nbyte_time_max;	/* typ<<N msec */
218	uint8_t		jt_erase_blk_time_max;		/* typ<<N msec */
219	uint8_t		jt_erase_chip_time_max;		/* typ<<N msec */
220};
221
222
223enum {
224	CFI_ADDR_ANY = 0x00*8,		    /* XXX "don't care" */
225	CFI_RESET_DATA = 0xf0,
226	CFI_ALT_RESET_DATA = 0xff,
227
228	CFI_QUERY_MODE_ADDR = 0x55*8,	    /* some devices accept anything */
229	CFI_QUERY_MODE_ALT_ADDR = 0x555*8,
230	CFI_QUERY_DATA = 0x98,
231
232	CFI_AMD_UNLOCK_ADDR1 = 0x555*8,
233	CFI_AMD_UNLOCK_ADDR2 = 0x555*4,
234};
235
236static __inline void
237cfi_reset(struct cfi * const cfi)
238{
239	KASSERT(cfi->cfi_ops.cfi_reset != NULL);
240	cfi->cfi_ops.cfi_reset(cfi);
241}
242
243static __inline int
244cfi_erase_sector(struct cfi * const cfi, flash_off_t offset)
245{
246	KASSERT(cfi->cfi_ops.cfi_erase_sector != NULL);
247	return cfi->cfi_ops.cfi_erase_sector(cfi, offset);
248}
249
250static __inline int
251cfi_program_word(struct cfi * const cfi, flash_off_t offset)
252{
253	KASSERT(cfi->cfi_ops.cfi_program_word != NULL);
254	return cfi->cfi_ops.cfi_program_word(cfi, offset);
255}
256
257extern const struct nor_interface nor_interface_cfi;
258
259extern bool cfi_probe(struct cfi * const);
260extern bool cfi_identify(struct cfi * const);
261extern void cfi_print(device_t, struct cfi * const);
262extern void cfi_reset_default(struct cfi * const);
263extern void cfi_reset_std(struct cfi * const);
264extern void cfi_reset_alt(struct cfi * const);
265extern void cfi_cmd(struct cfi * const, bus_size_t, uint32_t);
266
267#endif	/* _CFI_H_ */
268