blob: 45303ae576d0881a26edc22cc7b263a96f6adb36 [file] [log] [blame]
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2011-2012 Stefan Tauner
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Taunerac1b4c82012-02-17 14:51:04 +000014 */
15
16#include <stdint.h>
17#include <stdlib.h>
Carl-Daniel Hailfinger2d251242012-02-24 23:49:30 +000018#include <string.h>
Nico Huber2e0a0032026-03-07 22:32:27 +010019
Stefan Taunerac1b4c82012-02-17 14:51:04 +000020#include "flash.h"
Nico Huberd5185632024-01-05 18:44:41 +010021#include "spi_command.h"
Stefan Taunerac1b4c82012-02-17 14:51:04 +000022#include "spi.h"
Nico Huber2e0a0032026-03-07 22:32:27 +010023#include "programmer.h"
24#include "flashchips.h"
Nico Huberfbc41d22026-02-22 23:04:01 +010025#include "chipdrivers/spi.h"
Nico Huber2e0a0032026-03-07 22:32:27 +010026#include "chipdrivers/probing.h"
Stefan Taunerac1b4c82012-02-17 14:51:04 +000027
Nico Huber2e0a0032026-03-07 22:32:27 +010028static int spi_sfdp_read_sfdp_chunk(const struct spi_master *spi, uint32_t address, uint8_t *buf, int len)
Stefan Taunerac1b4c82012-02-17 14:51:04 +000029{
30 int i, ret;
Carl-Daniel Hailfinger2d251242012-02-24 23:49:30 +000031 uint8_t *newbuf;
Stefan Taunerac1b4c82012-02-17 14:51:04 +000032 const unsigned char cmd[JEDEC_SFDP_OUTSIZE] = {
33 JEDEC_SFDP,
34 (address >> 16) & 0xff,
35 (address >> 8) & 0xff,
36 (address >> 0) & 0xff,
37 /* FIXME: the following dummy byte explodes on some programmers.
Carl-Daniel Hailfinger2d251242012-02-24 23:49:30 +000038 * One workaround is to read the dummy byte
Stefan Taunerac1b4c82012-02-17 14:51:04 +000039 * instead and discard its value.
40 */
41 0
42 };
43 msg_cspew("%s: addr=0x%x, len=%d, data:\n", __func__, address, len);
Carl-Daniel Hailfinger2d251242012-02-24 23:49:30 +000044 newbuf = malloc(len + 1);
45 if (!newbuf)
46 return SPI_PROGRAMMER_ERROR;
Nico Huber2e0a0032026-03-07 22:32:27 +010047 ret = spi->command(spi, sizeof(cmd) - 1, len + 1, cmd, newbuf);
Carl-Daniel Hailfinger2d251242012-02-24 23:49:30 +000048 memmove(buf, newbuf + 1, len);
49 free(newbuf);
50 if (ret)
51 return ret;
Stefan Taunerac1b4c82012-02-17 14:51:04 +000052 for (i = 0; i < len; i++)
53 msg_cspew(" 0x%02x", buf[i]);
54 msg_cspew("\n");
Carl-Daniel Hailfinger2d251242012-02-24 23:49:30 +000055 return 0;
Stefan Taunerac1b4c82012-02-17 14:51:04 +000056}
57
Nico Huber2e0a0032026-03-07 22:32:27 +010058static int spi_sfdp_read_sfdp(const struct spi_master *spi, uint32_t address, uint8_t *buf, int len)
Stefan Taunerac1b4c82012-02-17 14:51:04 +000059{
Carl-Daniel Hailfinger2d251242012-02-24 23:49:30 +000060 /* FIXME: There are different upper bounds for the number of bytes to
61 * read on the various programmers (even depending on the rest of the
62 * structure of the transaction). 2 is a safe bet. */
63 int maxstep = 2;
Stefan Taunerac1b4c82012-02-17 14:51:04 +000064 int ret = 0;
65 while (len > 0) {
66 int step = min(len, maxstep);
Nico Huber2e0a0032026-03-07 22:32:27 +010067 ret = spi_sfdp_read_sfdp_chunk(spi, address, buf, step);
Stefan Taunerac1b4c82012-02-17 14:51:04 +000068 if (ret)
69 return ret;
70 address += step;
71 buf += step;
72 len -= step;
73 }
74 return ret;
75}
76
77struct sfdp_tbl_hdr {
78 uint8_t id;
79 uint8_t v_minor;
80 uint8_t v_major;
81 uint8_t len;
82 uint32_t ptp; /* 24b pointer */
83};
84
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000085static int sfdp_add_uniform_eraser(struct flashchip *chip, uint8_t opcode, uint32_t block_size)
Stefan Taunerac1b4c82012-02-17 14:51:04 +000086{
87 int i;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000088 uint32_t total_size = chip->total_size * 1024;
Thomas Heijligen35614512022-09-19 23:46:58 +020089 erasefunc_t *erasefn = spi25_get_erasefn_from_opcode(opcode);
Stefan Taunerac1b4c82012-02-17 14:51:04 +000090
Stefan Tauner75adf322012-02-22 00:14:14 +000091 if (erasefn == NULL || total_size == 0 || block_size == 0 ||
92 total_size % block_size != 0) {
93 msg_cdbg("%s: invalid input, please report to "
Nico Huberc3b02dc2023-08-12 01:13:45 +020094 "flashprog@flashprog.org\n", __func__);
Stefan Taunerac1b4c82012-02-17 14:51:04 +000095 return 1;
96 }
97
98 for (i = 0; i < NUM_ERASEFUNCTIONS; i++) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000099 struct block_eraser *eraser = &chip->block_erasers[i];
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000100 /* Check for duplicates (including (some) non-uniform ones). */
101 if (eraser->eraseblocks[0].size == block_size &&
102 eraser->block_erase == erasefn) {
103 msg_cdbg2(" Tried to add a duplicate block eraser: "
Stefan Tauner75adf322012-02-22 00:14:14 +0000104 "%d x %d B with opcode 0x%02x.\n",
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000105 total_size/block_size, block_size, opcode);
106 return 1;
107 }
Stefan Tauner75adf322012-02-22 00:14:14 +0000108 if (eraser->eraseblocks[0].size != 0 ||
109 eraser->block_erase != NULL) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000110 msg_cspew(" Block Eraser %d is already occupied.\n",
111 i);
112 continue;
113 }
114
115 eraser->block_erase = erasefn;
116 eraser->eraseblocks[0].size = block_size;
117 eraser->eraseblocks[0].count = total_size/block_size;
118 msg_cdbg2(" Block eraser %d: %d x %d B with opcode "
119 "0x%02x\n", i, total_size/block_size, block_size,
120 opcode);
121 return 0;
122 }
Nico Huberac90af62022-12-18 00:22:47 +0000123 msg_cinfo("%s: Not enough space to store another eraser (i=%d).\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200124 "Please report this at flashprog@flashprog.org\n",
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000125 __func__, i);
126 return 1;
127}
128
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000129static int sfdp_fill_flash(struct flashchip *chip, uint8_t *buf, uint16_t len)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000130{
Stefan Tauner75adf322012-02-22 00:14:14 +0000131 uint8_t opcode_4k_erase = 0xFF;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000132 uint32_t tmp32;
133 uint8_t tmp8;
134 uint32_t total_size; /* in bytes */
135 uint32_t block_size;
Stefan Tauner75adf322012-02-22 00:14:14 +0000136 int j;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000137
138 msg_cdbg("Parsing JEDEC flash parameter table... ");
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000139 msg_cdbg2("\n");
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100140
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000141 /* 1. double word */
Stefan Tauner75adf322012-02-22 00:14:14 +0000142 tmp32 = ((unsigned int)buf[(4 * 0) + 0]);
143 tmp32 |= ((unsigned int)buf[(4 * 0) + 1]) << 8;
144 tmp32 |= ((unsigned int)buf[(4 * 0) + 2]) << 16;
145 tmp32 |= ((unsigned int)buf[(4 * 0) + 3]) << 24;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000146
147 tmp8 = (tmp32 >> 17) & 0x3;
148 switch (tmp8) {
149 case 0x0:
150 msg_cdbg2(" 3-Byte only addressing.\n");
151 break;
152 case 0x1:
153 msg_cdbg2(" 3-Byte (and optionally 4-Byte) addressing.\n");
154 break;
155 case 0x2:
156 msg_cdbg(" 4-Byte only addressing (not supported by "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200157 "flashprog).\n");
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000158 return 1;
159 default:
160 msg_cdbg(" Required addressing mode (0x%x) not supported.\n",
161 tmp8);
162 return 1;
163 }
164
165 msg_cdbg2(" Status register is ");
166 if (tmp32 & (1 << 3)) {
167 msg_cdbg2("volatile and writes to the status register have to "
168 "be enabled with ");
169 if (tmp32 & (1 << 4)) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000170 chip->feature_bits = FEATURE_WRSR_WREN;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000171 msg_cdbg2("WREN (0x06).\n");
172 } else {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000173 chip->feature_bits = FEATURE_WRSR_EWSR;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000174 msg_cdbg2("EWSR (0x50).\n");
175 }
Steven Zakulec3603a282012-05-02 20:07:57 +0000176 } else {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000177 msg_cdbg2("non-volatile and the standard does not allow "
178 "vendors to tell us whether EWSR/WREN is needed for "
179 "status register writes - assuming EWSR.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000180 chip->feature_bits = FEATURE_WRSR_EWSR;
Steven Zakulec3603a282012-05-02 20:07:57 +0000181 }
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000182
183 msg_cdbg2(" Write chunk size is ");
184 if (tmp32 & (1 << 2)) {
185 msg_cdbg2("at least 64 B.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000186 chip->page_size = 64;
187 chip->write = spi_chip_write_256;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000188 } else {
189 msg_cdbg2("1 B only.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000190 chip->page_size = 256;
191 chip->write = spi_chip_write_1;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000192 }
193
194 if ((tmp32 & 0x3) == 0x1) {
Stefan Tauner75adf322012-02-22 00:14:14 +0000195 opcode_4k_erase = (tmp32 >> 8) & 0xFF;
196 msg_cspew(" 4kB erase opcode is 0x%02x.\n", opcode_4k_erase);
197 /* add the eraser later, because we don't know total_size yet */
198 } else
199 msg_cspew(" 4kB erase opcode is not defined.\n");
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000200
201 /* 2. double word */
Stefan Tauner75adf322012-02-22 00:14:14 +0000202 tmp32 = ((unsigned int)buf[(4 * 1) + 0]);
203 tmp32 |= ((unsigned int)buf[(4 * 1) + 1]) << 8;
204 tmp32 |= ((unsigned int)buf[(4 * 1) + 2]) << 16;
205 tmp32 |= ((unsigned int)buf[(4 * 1) + 3]) << 24;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000206
207 if (tmp32 & (1 << 31)) {
208 msg_cdbg("Flash chip size >= 4 Gb/512 MB not supported.\n");
209 return 1;
210 }
211 total_size = ((tmp32 & 0x7FFFFFFF) + 1) / 8;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000212 chip->total_size = total_size / 1024;
213 msg_cdbg2(" Flash chip size is %d kB.\n", chip->total_size);
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000214 if (total_size > (1 << 24)) {
Arthur Heymans82834c92025-06-27 08:33:56 +0200215 msg_cdbg2("Flash chip size is bigger than what 3-Byte addressing "
216 "can access, checking for 4-byte addressing support.\n");
217
218 /* Check if we have the 16th DWORD (4-byte addressing info) */
219 if (len < 16 * 4) {
220 msg_cdbg("Flash chip size requires 4-byte addressing but "
221 "SFDP table too short to contain 4BA information.\n");
222 return 1;
223 }
224
225 /* Parse 16th DWORD (offset 15 * 4 = 60) */
226 tmp32 = ((unsigned int)buf[(4 * 15) + 0]);
227 tmp32 |= ((unsigned int)buf[(4 * 15) + 1]) << 8;
228 tmp32 |= ((unsigned int)buf[(4 * 15) + 2]) << 16;
229 tmp32 |= ((unsigned int)buf[(4 * 15) + 3]) << 24;
230
231 uint8_t enter_4ba = (tmp32 >> 24) & 0xFF;
232 uint16_t exit_4ba = (tmp32 >> 14) & 0x3FF;
233
234 msg_cdbg2(" 4BA Enter methods: 0x%02x, Exit methods: 0x%03x\n",
235 enter_4ba, exit_4ba);
236
237 /* Sanity check: validate exit methods correspond to enter methods */
238 if ((enter_4ba & 0x01) && !(exit_4ba & 0x01)) {
239 msg_cwarn(" Warning: Enter via B7h supported but exit via E9h not supported\n");
240 }
241 if ((enter_4ba & 0x02) && !(exit_4ba & 0x02)) {
242 msg_cwarn(" Warning: Enter via WREN+B7h supported but exit via WREN+E9h not supported\n");
243 }
244 if ((enter_4ba & 0x04) && !(exit_4ba & 0x04)) {
245 msg_cwarn(" Warning: Extended address register enter supported but exit not supported\n");
246 }
247 if ((enter_4ba & 0x08) && !(exit_4ba & 0x08)) {
248 msg_cwarn(" Warning: Bank register enter supported but exit not supported\n");
249 }
250 if ((enter_4ba & 0x10) && !(exit_4ba & 0x10)) {
251 msg_cwarn(" Warning: Nonvolatile config register enter supported but exit not supported\n");
252 }
253
254 /* Parse Enter 4-Byte Addressing methods */
255 if (enter_4ba & 0x01) {
256 /* Issue instruction B7h (no WREN required) */
257 chip->feature_bits |= FEATURE_4BA_ENTER;
258 msg_cdbg2(" Supports 4BA enter via B7h instruction\n");
259 }
260 if (enter_4ba & 0x02) {
261 /* Issue WREN (06h), then B7h */
262 chip->feature_bits |= FEATURE_4BA_ENTER_WREN;
263 /* If both bits are set, clear the conflicting FEATURE_4BA_ENTER */
264 if (enter_4ba & 0x01) {
265 msg_cwarn(" Warning: Both B7h (no WREN) and WREN+B7h methods specified - this should not happen\n");
266 chip->feature_bits &= ~FEATURE_4BA_ENTER;
267 }
268 msg_cdbg2(" Supports 4BA enter via WREN + B7h\n");
269 }
270 if (enter_4ba & 0x04) {
271 /* Extended address register (C8h read, C5h write) */
272 chip->feature_bits |= FEATURE_4BA_EAR_C5C8;
273 msg_cdbg2(" Supports extended address register (C5h/C8h)\n");
274 }
275 if (enter_4ba & 0x08) {
276 /* Bank register (16h read, 17h write) */
277 chip->feature_bits |= FEATURE_4BA_EAR_1716 | FEATURE_4BA_ENTER_EAR7;
278 msg_cdbg2(" Supports bank register (17h/16h)\n");
279 }
280 if (enter_4ba & 0x20) {
281 /* Dedicated 4-byte instruction set */
282 msg_cdbg(" Supports dedicated 4-byte instruction set (not supported by flashprog yet)\n");
283 }
284 if (enter_4ba & 0x40) {
285 /* Always operates in 4-byte mode */
286 msg_cdbg(" Always operates in 4-byte address mode\n"
287 " not supported by flashprog.\n");
288 return 1;
289
290 }
291
292 /* Check if any 4BA method is supported */
293 if (!(chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN |
294 FEATURE_4BA_EAR_C5C8 | FEATURE_4BA_EAR_1716 |
295 FEATURE_4BA_NATIVE))) {
296 msg_cdbg("Flash chip size requires 4-byte addressing but "
297 "no supported 4BA methods found in SFDP.\n");
298 return 1;
299 }
300
301 chip->prepare_access = spi_prepare_io;
302 chip->finish_access = spi_finish_io;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000303 }
304
Stefan Tauner75adf322012-02-22 00:14:14 +0000305 if (opcode_4k_erase != 0xFF)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000306 sfdp_add_uniform_eraser(chip, opcode_4k_erase, 4 * 1024);
Stefan Tauner75adf322012-02-22 00:14:14 +0000307
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000308 /* FIXME: double words 3-7 contain unused fast read information */
309
310 if (len == 4 * 4) {
Stefan Tauner75adf322012-02-22 00:14:14 +0000311 msg_cdbg(" It seems like this chip supports the preliminary "
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000312 "Intel version of SFDP, skipping processing of double "
313 "words 3-9.\n");
314 goto done;
315 }
316
Stefan Tauner75adf322012-02-22 00:14:14 +0000317 /* 8. double word */
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000318 for (j = 0; j < 4; j++) {
Stefan Tauner75adf322012-02-22 00:14:14 +0000319 /* 7 double words from the start + 2 bytes for every eraser */
320 tmp8 = buf[(4 * 7) + (j * 2)];
321 msg_cspew(" Erase Sector Type %d Size: 0x%02x\n", j + 1,
322 tmp8);
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000323 if (tmp8 == 0) {
Stefan Tauner75adf322012-02-22 00:14:14 +0000324 msg_cspew(" Erase Sector Type %d is unused.\n", j);
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000325 continue;
326 }
327 if (tmp8 >= 31) {
Stefan Tauner75adf322012-02-22 00:14:14 +0000328 msg_cdbg2(" Block size of erase Sector Type %d (2^%d) "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200329 "is too big for flashprog.\n", j, tmp8);
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000330 continue;
331 }
332 block_size = 1 << (tmp8); /* block_size = 2 ^ field */
333
Stefan Tauner75adf322012-02-22 00:14:14 +0000334 tmp8 = buf[(4 * 7) + (j * 2) + 1];
335 msg_cspew(" Erase Sector Type %d Opcode: 0x%02x\n", j + 1,
336 tmp8);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000337 sfdp_add_uniform_eraser(chip, tmp8, block_size);
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000338 }
339
340done:
341 msg_cdbg("done.\n");
342 return 0;
343}
344
Nico Huber2e0a0032026-03-07 22:32:27 +0100345int spi_prepare_sfdp(struct flashctx *flash, enum preparation_steps step)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000346{
Nico Huber2e0a0032026-03-07 22:32:27 +0100347 const struct spi_master *const spi = flash->mst.spi;
348 int ret;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000349 uint8_t buf[8];
350 uint32_t tmp32;
351 uint8_t nph;
352 /* need to limit the table loop by comparing i to uint8_t nph hence: */
353 uint16_t i;
354 struct sfdp_tbl_hdr *hdrs;
355 uint8_t *hbuf;
356 uint8_t *tbuf;
357
Nico Huber2e0a0032026-03-07 22:32:27 +0100358 if (step != PREPARE_POST_PROBE)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000359 return 0;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000360
Nico Huber2e0a0032026-03-07 22:32:27 +0100361 ret = spi_sfdp_read_sfdp(spi, 0x04, buf, 3);
362 if (ret) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000363 msg_cdbg("Receiving SFDP revision and number of parameter "
364 "headers (NPH) failed. ");
Nico Huber2e0a0032026-03-07 22:32:27 +0100365 return ret;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000366 }
Nico Huber2e0a0032026-03-07 22:32:27 +0100367
368 ret = SPI_GENERIC_ERROR;
369
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000370 msg_cdbg2("SFDP revision = %d.%d\n", buf[1], buf[0]);
371 if (buf[1] != 0x01) {
372 msg_cdbg("The chip supports an unknown version of SFDP. "
373 "Aborting SFDP probe!\n");
Nico Huber2e0a0032026-03-07 22:32:27 +0100374 return ret;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000375 }
376 nph = buf[2];
377 msg_cdbg2("SFDP number of parameter headers is %d (NPH = %d).\n",
378 nph + 1, nph);
379
380 /* Fetch all parameter headers, even if we don't use them all (yet). */
381 hbuf = malloc((nph + 1) * 8);
Angel Pons690a9442021-06-07 12:33:53 +0200382 hdrs = malloc((nph + 1) * sizeof(*hdrs));
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000383 if (hbuf == NULL || hdrs == NULL ) {
384 msg_gerr("Out of memory!\n");
385 goto cleanup_hdrs;
386 }
Nico Huber2e0a0032026-03-07 22:32:27 +0100387 if (spi_sfdp_read_sfdp(spi, 0x08, hbuf, (nph + 1) * 8)) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000388 msg_cdbg("Receiving SFDP parameter table headers failed.\n");
389 goto cleanup_hdrs;
390 }
391
392 for (i = 0; i <= nph; i++) {
393 uint16_t len;
394 hdrs[i].id = hbuf[(8 * i) + 0];
395 hdrs[i].v_minor = hbuf[(8 * i) + 1];
396 hdrs[i].v_major = hbuf[(8 * i) + 2];
397 hdrs[i].len = hbuf[(8 * i) + 3];
398 hdrs[i].ptp = hbuf[(8 * i) + 4];
399 hdrs[i].ptp |= ((unsigned int)hbuf[(8 * i) + 5]) << 8;
400 hdrs[i].ptp |= ((unsigned int)hbuf[(8 * i) + 6]) << 16;
401 msg_cdbg2("\nSFDP parameter table header %d/%d:\n", i, nph);
402 msg_cdbg2(" ID 0x%02x, version %d.%d\n", hdrs[i].id,
403 hdrs[i].v_major, hdrs[i].v_minor);
404 len = hdrs[i].len * 4;
405 tmp32 = hdrs[i].ptp;
406 msg_cdbg2(" Length %d B, Parameter Table Pointer 0x%06x\n",
407 len, tmp32);
408
409 if (tmp32 + len >= (1 << 24)) {
410 msg_cdbg("SFDP Parameter Table %d supposedly overflows "
411 "addressable SFDP area. This most\nprobably "
412 "indicates a corrupt SFDP parameter table "
413 "header. Skipping it.\n", i);
414 continue;
415 }
416
417 tbuf = malloc(len);
418 if (tbuf == NULL) {
419 msg_gerr("Out of memory!\n");
420 goto cleanup_hdrs;
421 }
Nico Huber2e0a0032026-03-07 22:32:27 +0100422 if (spi_sfdp_read_sfdp(spi, tmp32, tbuf, len)){
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000423 msg_cdbg("Fetching SFDP parameter table %d failed.\n",
424 i);
425 free(tbuf);
426 continue;
427 }
428 msg_cspew(" Parameter table contents:\n");
429 for (tmp32 = 0; tmp32 < len; tmp32++) {
430 if ((tmp32 % 8) == 0) {
431 msg_cspew(" 0x%04x: ", tmp32);
432 }
Stefan Tauner75adf322012-02-22 00:14:14 +0000433 msg_cspew(" %02x", tbuf[tmp32]);
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000434 if ((tmp32 % 8) == 7) {
435 msg_cspew("\n");
436 continue;
437 }
438 if ((tmp32 % 8) == 3) {
439 msg_cspew(" ");
440 continue;
441 }
442 }
Stefan Tauner75adf322012-02-22 00:14:14 +0000443 msg_cspew("\n");
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000444
445 if (i == 0) { /* Mandatory JEDEC SFDP parameter table */
446 if (hdrs[i].id != 0)
447 msg_cdbg("ID of the mandatory JEDEC SFDP "
448 "parameter table is not 0 as demanded "
449 "by JESD216 (warning only).\n");
450
451 if (hdrs[i].v_major != 0x01) {
452 msg_cdbg("The chip contains an unknown "
453 "version of the JEDEC flash "
454 "parameters table, skipping it.\n");
Michael Niewöhnerde307c02021-12-11 22:15:06 +0100455 } else if (len != 4 * 4 && len < 9 * 4) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000456 msg_cdbg("Length of the mandatory JEDEC SFDP "
457 "parameter table is wrong (%d B), "
458 "skipping it.\n", len);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000459 } else if (sfdp_fill_flash(flash->chip, tbuf, len) == 0)
Nico Huber2e0a0032026-03-07 22:32:27 +0100460 ret = 0;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000461 }
462 free(tbuf);
463 }
464
Nico Huber2e0a0032026-03-07 22:32:27 +0100465 if (ret == 0 && selfcheck_chip(flash->chip, -1)) {
Nico Huberd9aa81e2026-01-26 18:30:25 +0100466 msg_cerr("SFDP parsing resulted in invalid chip structure.\n");
Nico Huber2e0a0032026-03-07 22:32:27 +0100467 ret = SPI_FLASHPROG_BUG;
Nico Huberd9aa81e2026-01-26 18:30:25 +0100468 }
469
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000470cleanup_hdrs:
471 free(hdrs);
472 free(hbuf);
473 return ret;
474}
Nico Huber2e0a0032026-03-07 22:32:27 +0100475
476struct found_id *probe_spi_sfdp(const struct bus_probe *probe, const struct master_common *mst)
477{
478 uint8_t buf[8];
479 uint32_t tmp32;
480
481 if (spi_sfdp_read_sfdp((struct spi_master *)mst, 0x00, buf, 4)) {
482 msg_cdbg("Receiving SFDP signature failed.\n");
483 return NULL;
484 }
485 tmp32 = buf[0];
486 tmp32 |= ((unsigned int)buf[1]) << 8;
487 tmp32 |= ((unsigned int)buf[2]) << 16;
488 tmp32 |= ((unsigned int)buf[3]) << 24;
489
490 if (tmp32 != 0x50444653) {
491 msg_cdbg2("Signature = 0x%08x (should be 0x50444653)\n", tmp32);
492 msg_cdbg("No SFDP signature found.\n");
493 return NULL;
494 }
495
496 struct found_id *const found = calloc(1, sizeof(*found));
497 if (!found) {
498 msg_cerr("Out of memory!\n");
499 return NULL;
500 }
501
502 found->info.id.type = ID_SPI_SFDP;
503 found->info.id.manufacture = GENERIC_MANUF_ID;
504 found->info.id.model = SFDP_DEVICE_ID;
505
506 return found;
507}