| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 1 | /* |
| 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 Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 18 | #include <string.h> |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 19 | |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 20 | #include "flash.h" |
| Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 21 | #include "spi_command.h" |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 22 | #include "spi.h" |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 23 | #include "programmer.h" |
| 24 | #include "flashchips.h" |
| Nico Huber | fbc41d2 | 2026-02-22 23:04:01 +0100 | [diff] [blame] | 25 | #include "chipdrivers/spi.h" |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 26 | #include "chipdrivers/probing.h" |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 27 | |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 28 | static int spi_sfdp_read_sfdp_chunk(const struct spi_master *spi, uint32_t address, uint8_t *buf, int len) |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 29 | { |
| 30 | int i, ret; |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 31 | uint8_t *newbuf; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 32 | 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 Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 38 | * One workaround is to read the dummy byte |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 39 | * 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 Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 44 | newbuf = malloc(len + 1); |
| 45 | if (!newbuf) |
| 46 | return SPI_PROGRAMMER_ERROR; |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 47 | ret = spi->command(spi, sizeof(cmd) - 1, len + 1, cmd, newbuf); |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 48 | memmove(buf, newbuf + 1, len); |
| 49 | free(newbuf); |
| 50 | if (ret) |
| 51 | return ret; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 52 | for (i = 0; i < len; i++) |
| 53 | msg_cspew(" 0x%02x", buf[i]); |
| 54 | msg_cspew("\n"); |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 55 | return 0; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 58 | static int spi_sfdp_read_sfdp(const struct spi_master *spi, uint32_t address, uint8_t *buf, int len) |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 59 | { |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 60 | /* 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 Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 64 | int ret = 0; |
| 65 | while (len > 0) { |
| 66 | int step = min(len, maxstep); |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 67 | ret = spi_sfdp_read_sfdp_chunk(spi, address, buf, step); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 68 | if (ret) |
| 69 | return ret; |
| 70 | address += step; |
| 71 | buf += step; |
| 72 | len -= step; |
| 73 | } |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | struct 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 Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 85 | static int sfdp_add_uniform_eraser(struct flashchip *chip, uint8_t opcode, uint32_t block_size) |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 86 | { |
| 87 | int i; |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 88 | uint32_t total_size = chip->total_size * 1024; |
| Thomas Heijligen | 3561451 | 2022-09-19 23:46:58 +0200 | [diff] [blame] | 89 | erasefunc_t *erasefn = spi25_get_erasefn_from_opcode(opcode); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 90 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 91 | 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 94 | "flashprog@flashprog.org\n", __func__); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | for (i = 0; i < NUM_ERASEFUNCTIONS; i++) { |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 99 | struct block_eraser *eraser = &chip->block_erasers[i]; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 100 | /* 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 Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 104 | "%d x %d B with opcode 0x%02x.\n", |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 105 | total_size/block_size, block_size, opcode); |
| 106 | return 1; |
| 107 | } |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 108 | if (eraser->eraseblocks[0].size != 0 || |
| 109 | eraser->block_erase != NULL) { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 110 | 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 Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 123 | msg_cinfo("%s: Not enough space to store another eraser (i=%d).\n" |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 124 | "Please report this at flashprog@flashprog.org\n", |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 125 | __func__, i); |
| 126 | return 1; |
| 127 | } |
| 128 | |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 129 | static int sfdp_fill_flash(struct flashchip *chip, uint8_t *buf, uint16_t len) |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 130 | { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 131 | uint8_t opcode_4k_erase = 0xFF; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 132 | uint32_t tmp32; |
| 133 | uint8_t tmp8; |
| 134 | uint32_t total_size; /* in bytes */ |
| 135 | uint32_t block_size; |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 136 | int j; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 137 | |
| 138 | msg_cdbg("Parsing JEDEC flash parameter table... "); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 139 | msg_cdbg2("\n"); |
| Elyes HAOUAS | 0cacb11 | 2019-02-04 12:16:38 +0100 | [diff] [blame] | 140 | |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 141 | /* 1. double word */ |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 142 | 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 Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 146 | |
| 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 157 | "flashprog).\n"); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 158 | 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 Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 170 | chip->feature_bits = FEATURE_WRSR_WREN; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 171 | msg_cdbg2("WREN (0x06).\n"); |
| 172 | } else { |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 173 | chip->feature_bits = FEATURE_WRSR_EWSR; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 174 | msg_cdbg2("EWSR (0x50).\n"); |
| 175 | } |
| Steven Zakulec | 3603a28 | 2012-05-02 20:07:57 +0000 | [diff] [blame] | 176 | } else { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 177 | 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 Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 180 | chip->feature_bits = FEATURE_WRSR_EWSR; |
| Steven Zakulec | 3603a28 | 2012-05-02 20:07:57 +0000 | [diff] [blame] | 181 | } |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 182 | |
| 183 | msg_cdbg2(" Write chunk size is "); |
| 184 | if (tmp32 & (1 << 2)) { |
| 185 | msg_cdbg2("at least 64 B.\n"); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 186 | chip->page_size = 64; |
| 187 | chip->write = spi_chip_write_256; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 188 | } else { |
| 189 | msg_cdbg2("1 B only.\n"); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 190 | chip->page_size = 256; |
| 191 | chip->write = spi_chip_write_1; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if ((tmp32 & 0x3) == 0x1) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 195 | 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 Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 200 | |
| 201 | /* 2. double word */ |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 202 | 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 Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 206 | |
| 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 Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 212 | chip->total_size = total_size / 1024; |
| 213 | msg_cdbg2(" Flash chip size is %d kB.\n", chip->total_size); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 214 | if (total_size > (1 << 24)) { |
| Arthur Heymans | 82834c9 | 2025-06-27 08:33:56 +0200 | [diff] [blame] | 215 | 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 Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 305 | if (opcode_4k_erase != 0xFF) |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 306 | sfdp_add_uniform_eraser(chip, opcode_4k_erase, 4 * 1024); |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 307 | |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 308 | /* FIXME: double words 3-7 contain unused fast read information */ |
| 309 | |
| 310 | if (len == 4 * 4) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 311 | msg_cdbg(" It seems like this chip supports the preliminary " |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 312 | "Intel version of SFDP, skipping processing of double " |
| 313 | "words 3-9.\n"); |
| 314 | goto done; |
| 315 | } |
| 316 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 317 | /* 8. double word */ |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 318 | for (j = 0; j < 4; j++) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 319 | /* 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 Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 323 | if (tmp8 == 0) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 324 | msg_cspew(" Erase Sector Type %d is unused.\n", j); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | if (tmp8 >= 31) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 328 | msg_cdbg2(" Block size of erase Sector Type %d (2^%d) " |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 329 | "is too big for flashprog.\n", j, tmp8); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 330 | continue; |
| 331 | } |
| 332 | block_size = 1 << (tmp8); /* block_size = 2 ^ field */ |
| 333 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 334 | tmp8 = buf[(4 * 7) + (j * 2) + 1]; |
| 335 | msg_cspew(" Erase Sector Type %d Opcode: 0x%02x\n", j + 1, |
| 336 | tmp8); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 337 | sfdp_add_uniform_eraser(chip, tmp8, block_size); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | done: |
| 341 | msg_cdbg("done.\n"); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 345 | int spi_prepare_sfdp(struct flashctx *flash, enum preparation_steps step) |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 346 | { |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 347 | const struct spi_master *const spi = flash->mst.spi; |
| 348 | int ret; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 349 | 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 Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 358 | if (step != PREPARE_POST_PROBE) |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 359 | return 0; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 360 | |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 361 | ret = spi_sfdp_read_sfdp(spi, 0x04, buf, 3); |
| 362 | if (ret) { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 363 | msg_cdbg("Receiving SFDP revision and number of parameter " |
| 364 | "headers (NPH) failed. "); |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 365 | return ret; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 366 | } |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 367 | |
| 368 | ret = SPI_GENERIC_ERROR; |
| 369 | |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 370 | 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 Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 374 | return ret; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 375 | } |
| 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 Pons | 690a944 | 2021-06-07 12:33:53 +0200 | [diff] [blame] | 382 | hdrs = malloc((nph + 1) * sizeof(*hdrs)); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 383 | if (hbuf == NULL || hdrs == NULL ) { |
| 384 | msg_gerr("Out of memory!\n"); |
| 385 | goto cleanup_hdrs; |
| 386 | } |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 387 | if (spi_sfdp_read_sfdp(spi, 0x08, hbuf, (nph + 1) * 8)) { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 388 | 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 Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 422 | if (spi_sfdp_read_sfdp(spi, tmp32, tbuf, len)){ |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 423 | 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 Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 433 | msg_cspew(" %02x", tbuf[tmp32]); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 434 | if ((tmp32 % 8) == 7) { |
| 435 | msg_cspew("\n"); |
| 436 | continue; |
| 437 | } |
| 438 | if ((tmp32 % 8) == 3) { |
| 439 | msg_cspew(" "); |
| 440 | continue; |
| 441 | } |
| 442 | } |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 443 | msg_cspew("\n"); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 444 | |
| 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öhner | de307c0 | 2021-12-11 22:15:06 +0100 | [diff] [blame] | 455 | } else if (len != 4 * 4 && len < 9 * 4) { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 456 | msg_cdbg("Length of the mandatory JEDEC SFDP " |
| 457 | "parameter table is wrong (%d B), " |
| 458 | "skipping it.\n", len); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 459 | } else if (sfdp_fill_flash(flash->chip, tbuf, len) == 0) |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 460 | ret = 0; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 461 | } |
| 462 | free(tbuf); |
| 463 | } |
| 464 | |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 465 | if (ret == 0 && selfcheck_chip(flash->chip, -1)) { |
| Nico Huber | d9aa81e | 2026-01-26 18:30:25 +0100 | [diff] [blame] | 466 | msg_cerr("SFDP parsing resulted in invalid chip structure.\n"); |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 467 | ret = SPI_FLASHPROG_BUG; |
| Nico Huber | d9aa81e | 2026-01-26 18:30:25 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 470 | cleanup_hdrs: |
| 471 | free(hdrs); |
| 472 | free(hbuf); |
| 473 | return ret; |
| 474 | } |
| Nico Huber | 2e0a003 | 2026-03-07 22:32:27 +0100 | [diff] [blame^] | 475 | |
| 476 | struct 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 | } |