1 /*
2 * Copyright (c) 2020 Peter Bigot Consulting, LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/flash.h>
10 #include <jesd216.h>
11 #include <stdio.h>
12 #include <inttypes.h>
13 #include <string.h>
14
15 #if DT_HAS_COMPAT_STATUS_OKAY(jedec_spi_nor)
16 #define FLASH_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(jedec_spi_nor)
17 #elif DT_HAS_COMPAT_STATUS_OKAY(nordic_qspi_nor)
18 #define FLASH_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(nordic_qspi_nor)
19 #elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_qspi_nor)
20 #define FLASH_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(st_stm32_qspi_nor)
21 #elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_ospi_nor)
22 #define FLASH_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(st_stm32_ospi_nor)
23 #elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_xspi_nor)
24 #define FLASH_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(st_stm32_xspi_nor)
25 #elif DT_HAS_COMPAT_STATUS_OKAY(nxp_s32_qspi_nor)
26 #define FLASH_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(nxp_s32_qspi_nor)
27 #elif DT_HAS_COMPAT_STATUS_OKAY(nxp_imx_flexspi_nor)
28 #define FLASH_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(nxp_imx_flexspi_nor)
29 #else
30 #error Unsupported flash driver
31 #define FLASH_NODE DT_INVALID_NODE
32 #endif
33
34 typedef void (*dw_extractor)(const struct jesd216_param_header *php,
35 const struct jesd216_bfp *bfp);
36
37 static const char * const mode_tags[] = {
38 [JESD216_MODE_044] = "QSPI XIP",
39 [JESD216_MODE_088] = "OSPI XIP",
40 [JESD216_MODE_111] = "1-1-1",
41 [JESD216_MODE_112] = "1-1-2",
42 [JESD216_MODE_114] = "1-1-4",
43 [JESD216_MODE_118] = "1-1-8",
44 [JESD216_MODE_122] = "1-2-2",
45 [JESD216_MODE_144] = "1-4-4",
46 [JESD216_MODE_188] = "1-8-8",
47 [JESD216_MODE_222] = "2-2-2",
48 [JESD216_MODE_444] = "4-4-4",
49 };
50
summarize_dw1(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)51 static void summarize_dw1(const struct jesd216_param_header *php,
52 const struct jesd216_bfp *bfp)
53 {
54 uint32_t dw1 = sys_le32_to_cpu(bfp->dw1);
55
56 printf("DTR Clocking %ssupported\n",
57 (dw1 & JESD216_SFDP_BFP_DW1_DTRCLK_FLG) ? "" : "not ");
58
59 static const char *const addr_bytes[] = {
60 [JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_3B] = "3-Byte only",
61 [JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_3B4B] = "3- or 4-Byte",
62 [JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_4B] = "4-Byte only",
63 [3] = "Reserved",
64 };
65
66 printf("Addressing: %s\n", addr_bytes[(dw1 & JESD216_SFDP_BFP_DW1_ADDRBYTES_MASK)
67 >> JESD216_SFDP_BFP_DW1_ADDRBYTES_SHFT]);
68
69 static const char *const bsersz[] = {
70 [0] = "Reserved 00b",
71 [JESD216_SFDP_BFP_DW1_BSERSZ_VAL_4KSUP] = "uniform",
72 [2] = "Reserved 01b",
73 [JESD216_SFDP_BFP_DW1_BSERSZ_VAL_4KNOTSUP] = "not uniform",
74 };
75
76 printf("4-KiBy erase: %s\n", bsersz[(dw1 & JESD216_SFDP_BFP_DW1_BSERSZ_MASK)
77 >> JESD216_SFDP_BFP_DW1_BSERSZ_SHFT]);
78
79 for (size_t mode = 0; mode < ARRAY_SIZE(mode_tags); ++mode) {
80 const char *tag = mode_tags[mode];
81
82 if (tag) {
83 struct jesd216_instr cmd;
84 int rc = jesd216_bfp_read_support(php, bfp,
85 (enum jesd216_mode_type)mode,
86 &cmd);
87
88 if (rc == 0) {
89 printf("Support %s\n", tag);
90 } else if (rc > 0) {
91 printf("Support %s: instr %02Xh, %u mode clocks, %u waits\n",
92 tag, cmd.instr, cmd.mode_clocks, cmd.wait_states);
93 }
94 }
95 }
96 }
97
summarize_dw2(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)98 static void summarize_dw2(const struct jesd216_param_header *php,
99 const struct jesd216_bfp *bfp)
100 {
101 printf("Flash density: %u bytes\n", (uint32_t)(jesd216_bfp_density(bfp) / 8));
102 }
103
summarize_dw89(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)104 static void summarize_dw89(const struct jesd216_param_header *php,
105 const struct jesd216_bfp *bfp)
106 {
107 struct jesd216_erase_type etype;
108 uint32_t typ_ms;
109 int typ_max_mul;
110
111 for (uint8_t idx = 1; idx < JESD216_NUM_ERASE_TYPES; ++idx) {
112 if (jesd216_bfp_erase(bfp, idx, &etype) == 0) {
113 typ_max_mul = jesd216_bfp_erase_type_times(php, bfp,
114 idx, &typ_ms);
115
116 printf("ET%u: instr %02Xh for %u By", idx, etype.cmd,
117 (uint32_t)BIT(etype.exp));
118 if (typ_max_mul > 0) {
119 printf("; typ %u ms, max %u ms",
120 typ_ms, typ_max_mul * typ_ms);
121 }
122 printf("\n");
123 }
124 }
125 }
126
summarize_dw11(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)127 static void summarize_dw11(const struct jesd216_param_header *php,
128 const struct jesd216_bfp *bfp)
129 {
130 struct jesd216_bfp_dw11 dw11;
131
132 if (jesd216_bfp_decode_dw11(php, bfp, &dw11) != 0) {
133 return;
134 }
135
136 printf("Chip erase: typ %u ms, max %u ms\n",
137 dw11.chip_erase_ms, dw11.typ_max_factor * dw11.chip_erase_ms);
138
139 printf("Byte program: type %u + %u * B us, max %u + %u * B us\n",
140 dw11.byte_prog_first_us, dw11.byte_prog_addl_us,
141 dw11.typ_max_factor * dw11.byte_prog_first_us,
142 dw11.typ_max_factor * dw11.byte_prog_addl_us);
143
144 printf("Page program: typ %u us, max %u us\n",
145 dw11.page_prog_us,
146 dw11.typ_max_factor * dw11.page_prog_us);
147
148 printf("Page program size: %u By\n", dw11.page_size);
149 }
150
summarize_dw12(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)151 static void summarize_dw12(const struct jesd216_param_header *php,
152 const struct jesd216_bfp *bfp)
153 {
154 uint32_t dw12 = sys_le32_to_cpu(bfp->dw10[2]);
155 uint32_t dw13 = sys_le32_to_cpu(bfp->dw10[3]);
156
157 /* Inverted logic flag: 1 means not supported */
158 if ((dw12 & JESD216_SFDP_BFP_DW12_SUSPRESSUP_FLG) != 0) {
159 return;
160 }
161
162 uint8_t susp_instr = dw13 >> 24;
163 uint8_t resm_instr = dw13 >> 16;
164 uint8_t psusp_instr = dw13 >> 8;
165 uint8_t presm_instr = dw13 >> 0;
166
167 printf("Suspend: %02Xh ; Resume: %02Xh\n",
168 susp_instr, resm_instr);
169 if ((susp_instr != psusp_instr)
170 || (resm_instr != presm_instr)) {
171 printf("Program suspend: %02Xh ; Resume: %02Xh\n",
172 psusp_instr, presm_instr);
173 }
174 }
175
summarize_dw14(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)176 static void summarize_dw14(const struct jesd216_param_header *php,
177 const struct jesd216_bfp *bfp)
178 {
179 struct jesd216_bfp_dw14 dw14;
180
181 if (jesd216_bfp_decode_dw14(php, bfp, &dw14) != 0) {
182 return;
183 }
184 printf("DPD: Enter %02Xh, exit %02Xh ; delay %u ns ; poll 0x%02x\n",
185 dw14.enter_dpd_instr, dw14.exit_dpd_instr,
186 dw14.exit_delay_ns, dw14.poll_options);
187 }
188
summarize_dw15(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)189 static void summarize_dw15(const struct jesd216_param_header *php,
190 const struct jesd216_bfp *bfp)
191 {
192 struct jesd216_bfp_dw15 dw15;
193
194 if (jesd216_bfp_decode_dw15(php, bfp, &dw15) != 0) {
195 return;
196 }
197 printf("HOLD or RESET Disable: %ssupported\n",
198 dw15.hold_reset_disable ? "" : "un");
199 printf("QER: %u\n", dw15.qer);
200 if (dw15.support_044) {
201 printf("0-4-4 Mode methods: entry 0x%01x ; exit 0x%02x\n",
202 dw15.entry_044, dw15.exit_044);
203 } else {
204 printf("0-4-4 Mode: not supported");
205 }
206 printf("4-4-4 Mode sequences: enable 0x%02x ; disable 0x%01x\n",
207 dw15.enable_444, dw15.disable_444);
208 }
209
summarize_dw16(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)210 static void summarize_dw16(const struct jesd216_param_header *php,
211 const struct jesd216_bfp *bfp)
212 {
213 struct jesd216_bfp_dw16 dw16;
214
215 if (jesd216_bfp_decode_dw16(php, bfp, &dw16) != 0) {
216 return;
217 }
218
219 uint8_t addr_support = jesd216_bfp_addrbytes(bfp);
220
221 /* Don't display bits when 4-byte addressing is not supported. */
222 if (addr_support != JESD216_SFDP_BFP_DW1_ADDRBYTES_VAL_3B) {
223 printf("4-byte addressing support: enter 0x%02x, exit 0x%03x\n",
224 dw16.enter_4ba, dw16.exit_4ba);
225 }
226 printf("Soft Reset and Rescue Sequence support: 0x%02x\n",
227 dw16.srrs_support);
228 printf("Status Register 1 support: 0x%02x\n",
229 dw16.sr1_interface);
230 }
231
232 /* Indexed from 1 to match JESD216 data word numbering */
233 static const dw_extractor extractor[] = {
234 [1] = summarize_dw1,
235 [2] = summarize_dw2,
236 [8] = summarize_dw89,
237 [11] = summarize_dw11,
238 [12] = summarize_dw12,
239 [14] = summarize_dw14,
240 [15] = summarize_dw15,
241 [16] = summarize_dw16,
242 };
243
dump_bfp(const struct jesd216_param_header * php,const struct jesd216_bfp * bfp)244 static void dump_bfp(const struct jesd216_param_header *php,
245 const struct jesd216_bfp *bfp)
246 {
247 uint8_t dw = 1;
248 uint8_t limit = MIN(1U + php->len_dw, ARRAY_SIZE(extractor));
249
250 printf("Summary of BFP content:\n");
251 while (dw < limit) {
252 dw_extractor ext = extractor[dw];
253
254 if (ext != 0) {
255 ext(php, bfp);
256 }
257 ++dw;
258 }
259 }
260
dump_bytes(const struct jesd216_param_header * php,const uint32_t * dw)261 static void dump_bytes(const struct jesd216_param_header *php,
262 const uint32_t *dw)
263 {
264 char buffer[4 * 3 + 1]; /* B1 B2 B3 B4 */
265 uint8_t nw = 0;
266
267 printf(" [\n\t");
268 while (nw < php->len_dw) {
269 const uint8_t *u8p = (const uint8_t *)&dw[nw];
270 ++nw;
271
272 bool emit_nl = (nw == php->len_dw) || ((nw % 4) == 0);
273
274 sprintf(buffer, "%02x %02x %02x %02x",
275 u8p[0], u8p[1], u8p[2], u8p[3]);
276 if (emit_nl) {
277 printf("%s\n\t", buffer);
278 } else {
279 printf("%s ", buffer);
280 }
281 }
282 printf("];\n");
283 }
284
main(void)285 int main(void)
286 {
287 const struct device *const dev = DEVICE_DT_GET(FLASH_NODE);
288
289 if (!device_is_ready(dev)) {
290 printf("%s: device not ready\n", dev->name);
291 return 0;
292 }
293
294 const uint8_t decl_nph = 5;
295 union {
296 uint8_t raw[JESD216_SFDP_SIZE(decl_nph)];
297 struct jesd216_sfdp_header sfdp;
298 } u;
299 const struct jesd216_sfdp_header *hp = &u.sfdp;
300 int rc = flash_sfdp_read(dev, 0, u.raw, sizeof(u.raw));
301
302 if (rc != 0) {
303 printf("Read SFDP not supported: device not JESD216-compliant "
304 "(err %d)\n", rc);
305 return 0;
306 }
307
308 uint32_t magic = jesd216_sfdp_magic(hp);
309
310 if (magic != JESD216_SFDP_MAGIC) {
311 printf("SFDP magic %08x invalid", magic);
312 return 0;
313 }
314
315 printf("%s: SFDP v %u.%u AP %x with %u PH\n", dev->name,
316 hp->rev_major, hp->rev_minor, hp->access, 1 + hp->nph);
317
318 const struct jesd216_param_header *php = hp->phdr;
319 const struct jesd216_param_header *phpe = php + MIN(decl_nph, 1 + hp->nph);
320
321 while (php != phpe) {
322 uint16_t id = jesd216_param_id(php);
323 uint32_t addr = jesd216_param_addr(php);
324
325 printf("PH%u: %04x rev %u.%u: %u DW @ %x\n",
326 (uint32_t)(php - hp->phdr), id, php->rev_major, php->rev_minor,
327 php->len_dw, addr);
328
329 uint32_t dw[php->len_dw];
330
331 rc = flash_sfdp_read(dev, addr, dw, sizeof(dw));
332 if (rc != 0) {
333 printf("Read failed: %d\n", rc);
334 return 0;
335 }
336
337 if (id == JESD216_SFDP_PARAM_ID_BFP) {
338 const struct jesd216_bfp *bfp = (struct jesd216_bfp *)dw;
339
340 dump_bfp(php, bfp);
341 printf("size = <%u> bits;\n", (uint32_t)jesd216_bfp_density(bfp));
342 printf("sfdp-bfp =");
343 } else {
344 printf("sfdp-%04x =", id);
345 }
346
347 dump_bytes(php, dw);
348
349 ++php;
350 }
351
352 uint8_t id[3];
353
354 rc = flash_read_jedec_id(dev, id);
355 if (rc == 0) {
356 printf("jedec-id = [%02x %02x %02x];\n",
357 id[0], id[1], id[2]);
358 } else {
359 printf("JEDEC ID read failed: %d\n", rc);
360 }
361 return 0;
362 }
363