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