| 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> |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 19 | #include "flash.h" |
| Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 20 | #include "spi_command.h" |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 21 | #include "spi.h" |
| 22 | #include "chipdrivers.h" |
| 23 | |
| 24 | static int spi_sfdp_read_sfdp_chunk(struct flashctx *flash, uint32_t address, uint8_t *buf, int len) |
| 25 | { |
| 26 | int i, ret; |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 27 | uint8_t *newbuf; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 28 | const unsigned char cmd[JEDEC_SFDP_OUTSIZE] = { |
| 29 | JEDEC_SFDP, |
| 30 | (address >> 16) & 0xff, |
| 31 | (address >> 8) & 0xff, |
| 32 | (address >> 0) & 0xff, |
| 33 | /* FIXME: the following dummy byte explodes on some programmers. |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 34 | * One workaround is to read the dummy byte |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 35 | * instead and discard its value. |
| 36 | */ |
| 37 | 0 |
| 38 | }; |
| 39 | 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] | 40 | newbuf = malloc(len + 1); |
| 41 | if (!newbuf) |
| 42 | return SPI_PROGRAMMER_ERROR; |
| 43 | ret = spi_send_command(flash, sizeof(cmd) - 1, len + 1, cmd, newbuf); |
| 44 | memmove(buf, newbuf + 1, len); |
| 45 | free(newbuf); |
| 46 | if (ret) |
| 47 | return ret; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 48 | for (i = 0; i < len; i++) |
| 49 | msg_cspew(" 0x%02x", buf[i]); |
| 50 | msg_cspew("\n"); |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 51 | return 0; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static int spi_sfdp_read_sfdp(struct flashctx *flash, uint32_t address, uint8_t *buf, int len) |
| 55 | { |
| Carl-Daniel Hailfinger | 2d25124 | 2012-02-24 23:49:30 +0000 | [diff] [blame] | 56 | /* FIXME: There are different upper bounds for the number of bytes to |
| 57 | * read on the various programmers (even depending on the rest of the |
| 58 | * structure of the transaction). 2 is a safe bet. */ |
| 59 | int maxstep = 2; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 60 | int ret = 0; |
| 61 | while (len > 0) { |
| 62 | int step = min(len, maxstep); |
| 63 | ret = spi_sfdp_read_sfdp_chunk(flash, address, buf, step); |
| 64 | if (ret) |
| 65 | return ret; |
| 66 | address += step; |
| 67 | buf += step; |
| 68 | len -= step; |
| 69 | } |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | struct sfdp_tbl_hdr { |
| 74 | uint8_t id; |
| 75 | uint8_t v_minor; |
| 76 | uint8_t v_major; |
| 77 | uint8_t len; |
| 78 | uint32_t ptp; /* 24b pointer */ |
| 79 | }; |
| 80 | |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 81 | 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] | 82 | { |
| 83 | int i; |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 84 | uint32_t total_size = chip->total_size * 1024; |
| Thomas Heijligen | 3561451 | 2022-09-19 23:46:58 +0200 | [diff] [blame] | 85 | erasefunc_t *erasefn = spi25_get_erasefn_from_opcode(opcode); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 86 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 87 | if (erasefn == NULL || total_size == 0 || block_size == 0 || |
| 88 | total_size % block_size != 0) { |
| 89 | msg_cdbg("%s: invalid input, please report to " |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 90 | "flashprog@flashprog.org\n", __func__); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | for (i = 0; i < NUM_ERASEFUNCTIONS; i++) { |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 95 | struct block_eraser *eraser = &chip->block_erasers[i]; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 96 | /* Check for duplicates (including (some) non-uniform ones). */ |
| 97 | if (eraser->eraseblocks[0].size == block_size && |
| 98 | eraser->block_erase == erasefn) { |
| 99 | msg_cdbg2(" Tried to add a duplicate block eraser: " |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 100 | "%d x %d B with opcode 0x%02x.\n", |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 101 | total_size/block_size, block_size, opcode); |
| 102 | return 1; |
| 103 | } |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 104 | if (eraser->eraseblocks[0].size != 0 || |
| 105 | eraser->block_erase != NULL) { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 106 | msg_cspew(" Block Eraser %d is already occupied.\n", |
| 107 | i); |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | eraser->block_erase = erasefn; |
| 112 | eraser->eraseblocks[0].size = block_size; |
| 113 | eraser->eraseblocks[0].count = total_size/block_size; |
| 114 | msg_cdbg2(" Block eraser %d: %d x %d B with opcode " |
| 115 | "0x%02x\n", i, total_size/block_size, block_size, |
| 116 | opcode); |
| 117 | return 0; |
| 118 | } |
| Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 119 | 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] | 120 | "Please report this at flashprog@flashprog.org\n", |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 121 | __func__, i); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 125 | 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] | 126 | { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 127 | uint8_t opcode_4k_erase = 0xFF; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 128 | uint32_t tmp32; |
| 129 | uint8_t tmp8; |
| 130 | uint32_t total_size; /* in bytes */ |
| 131 | uint32_t block_size; |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 132 | int j; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 133 | |
| 134 | msg_cdbg("Parsing JEDEC flash parameter table... "); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 135 | msg_cdbg2("\n"); |
| Elyes HAOUAS | 0cacb11 | 2019-02-04 12:16:38 +0100 | [diff] [blame] | 136 | |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 137 | /* 1. double word */ |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 138 | tmp32 = ((unsigned int)buf[(4 * 0) + 0]); |
| 139 | tmp32 |= ((unsigned int)buf[(4 * 0) + 1]) << 8; |
| 140 | tmp32 |= ((unsigned int)buf[(4 * 0) + 2]) << 16; |
| 141 | tmp32 |= ((unsigned int)buf[(4 * 0) + 3]) << 24; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 142 | |
| 143 | tmp8 = (tmp32 >> 17) & 0x3; |
| 144 | switch (tmp8) { |
| 145 | case 0x0: |
| 146 | msg_cdbg2(" 3-Byte only addressing.\n"); |
| 147 | break; |
| 148 | case 0x1: |
| 149 | msg_cdbg2(" 3-Byte (and optionally 4-Byte) addressing.\n"); |
| 150 | break; |
| 151 | case 0x2: |
| 152 | msg_cdbg(" 4-Byte only addressing (not supported by " |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 153 | "flashprog).\n"); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 154 | return 1; |
| 155 | default: |
| 156 | msg_cdbg(" Required addressing mode (0x%x) not supported.\n", |
| 157 | tmp8); |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | msg_cdbg2(" Status register is "); |
| 162 | if (tmp32 & (1 << 3)) { |
| 163 | msg_cdbg2("volatile and writes to the status register have to " |
| 164 | "be enabled with "); |
| 165 | if (tmp32 & (1 << 4)) { |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 166 | chip->feature_bits = FEATURE_WRSR_WREN; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 167 | msg_cdbg2("WREN (0x06).\n"); |
| 168 | } else { |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 169 | chip->feature_bits = FEATURE_WRSR_EWSR; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 170 | msg_cdbg2("EWSR (0x50).\n"); |
| 171 | } |
| Steven Zakulec | 3603a28 | 2012-05-02 20:07:57 +0000 | [diff] [blame] | 172 | } else { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 173 | msg_cdbg2("non-volatile and the standard does not allow " |
| 174 | "vendors to tell us whether EWSR/WREN is needed for " |
| 175 | "status register writes - assuming EWSR.\n"); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 176 | chip->feature_bits = FEATURE_WRSR_EWSR; |
| Steven Zakulec | 3603a28 | 2012-05-02 20:07:57 +0000 | [diff] [blame] | 177 | } |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 178 | |
| 179 | msg_cdbg2(" Write chunk size is "); |
| 180 | if (tmp32 & (1 << 2)) { |
| 181 | msg_cdbg2("at least 64 B.\n"); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 182 | chip->page_size = 64; |
| 183 | chip->write = spi_chip_write_256; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 184 | } else { |
| 185 | msg_cdbg2("1 B only.\n"); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 186 | chip->page_size = 256; |
| 187 | chip->write = spi_chip_write_1; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if ((tmp32 & 0x3) == 0x1) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 191 | opcode_4k_erase = (tmp32 >> 8) & 0xFF; |
| 192 | msg_cspew(" 4kB erase opcode is 0x%02x.\n", opcode_4k_erase); |
| 193 | /* add the eraser later, because we don't know total_size yet */ |
| 194 | } else |
| 195 | msg_cspew(" 4kB erase opcode is not defined.\n"); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 196 | |
| 197 | /* 2. double word */ |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 198 | tmp32 = ((unsigned int)buf[(4 * 1) + 0]); |
| 199 | tmp32 |= ((unsigned int)buf[(4 * 1) + 1]) << 8; |
| 200 | tmp32 |= ((unsigned int)buf[(4 * 1) + 2]) << 16; |
| 201 | tmp32 |= ((unsigned int)buf[(4 * 1) + 3]) << 24; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 202 | |
| 203 | if (tmp32 & (1 << 31)) { |
| 204 | msg_cdbg("Flash chip size >= 4 Gb/512 MB not supported.\n"); |
| 205 | return 1; |
| 206 | } |
| 207 | total_size = ((tmp32 & 0x7FFFFFFF) + 1) / 8; |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 208 | chip->total_size = total_size / 1024; |
| 209 | msg_cdbg2(" Flash chip size is %d kB.\n", chip->total_size); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 210 | if (total_size > (1 << 24)) { |
| Arthur Heymans | 82834c9 | 2025-06-27 08:33:56 +0200 | [diff] [blame] | 211 | msg_cdbg2("Flash chip size is bigger than what 3-Byte addressing " |
| 212 | "can access, checking for 4-byte addressing support.\n"); |
| 213 | |
| 214 | /* Check if we have the 16th DWORD (4-byte addressing info) */ |
| 215 | if (len < 16 * 4) { |
| 216 | msg_cdbg("Flash chip size requires 4-byte addressing but " |
| 217 | "SFDP table too short to contain 4BA information.\n"); |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | /* Parse 16th DWORD (offset 15 * 4 = 60) */ |
| 222 | tmp32 = ((unsigned int)buf[(4 * 15) + 0]); |
| 223 | tmp32 |= ((unsigned int)buf[(4 * 15) + 1]) << 8; |
| 224 | tmp32 |= ((unsigned int)buf[(4 * 15) + 2]) << 16; |
| 225 | tmp32 |= ((unsigned int)buf[(4 * 15) + 3]) << 24; |
| 226 | |
| 227 | uint8_t enter_4ba = (tmp32 >> 24) & 0xFF; |
| 228 | uint16_t exit_4ba = (tmp32 >> 14) & 0x3FF; |
| 229 | |
| 230 | msg_cdbg2(" 4BA Enter methods: 0x%02x, Exit methods: 0x%03x\n", |
| 231 | enter_4ba, exit_4ba); |
| 232 | |
| 233 | /* Sanity check: validate exit methods correspond to enter methods */ |
| 234 | if ((enter_4ba & 0x01) && !(exit_4ba & 0x01)) { |
| 235 | msg_cwarn(" Warning: Enter via B7h supported but exit via E9h not supported\n"); |
| 236 | } |
| 237 | if ((enter_4ba & 0x02) && !(exit_4ba & 0x02)) { |
| 238 | msg_cwarn(" Warning: Enter via WREN+B7h supported but exit via WREN+E9h not supported\n"); |
| 239 | } |
| 240 | if ((enter_4ba & 0x04) && !(exit_4ba & 0x04)) { |
| 241 | msg_cwarn(" Warning: Extended address register enter supported but exit not supported\n"); |
| 242 | } |
| 243 | if ((enter_4ba & 0x08) && !(exit_4ba & 0x08)) { |
| 244 | msg_cwarn(" Warning: Bank register enter supported but exit not supported\n"); |
| 245 | } |
| 246 | if ((enter_4ba & 0x10) && !(exit_4ba & 0x10)) { |
| 247 | msg_cwarn(" Warning: Nonvolatile config register enter supported but exit not supported\n"); |
| 248 | } |
| 249 | |
| 250 | /* Parse Enter 4-Byte Addressing methods */ |
| 251 | if (enter_4ba & 0x01) { |
| 252 | /* Issue instruction B7h (no WREN required) */ |
| 253 | chip->feature_bits |= FEATURE_4BA_ENTER; |
| 254 | msg_cdbg2(" Supports 4BA enter via B7h instruction\n"); |
| 255 | } |
| 256 | if (enter_4ba & 0x02) { |
| 257 | /* Issue WREN (06h), then B7h */ |
| 258 | chip->feature_bits |= FEATURE_4BA_ENTER_WREN; |
| 259 | /* If both bits are set, clear the conflicting FEATURE_4BA_ENTER */ |
| 260 | if (enter_4ba & 0x01) { |
| 261 | msg_cwarn(" Warning: Both B7h (no WREN) and WREN+B7h methods specified - this should not happen\n"); |
| 262 | chip->feature_bits &= ~FEATURE_4BA_ENTER; |
| 263 | } |
| 264 | msg_cdbg2(" Supports 4BA enter via WREN + B7h\n"); |
| 265 | } |
| 266 | if (enter_4ba & 0x04) { |
| 267 | /* Extended address register (C8h read, C5h write) */ |
| 268 | chip->feature_bits |= FEATURE_4BA_EAR_C5C8; |
| 269 | msg_cdbg2(" Supports extended address register (C5h/C8h)\n"); |
| 270 | } |
| 271 | if (enter_4ba & 0x08) { |
| 272 | /* Bank register (16h read, 17h write) */ |
| 273 | chip->feature_bits |= FEATURE_4BA_EAR_1716 | FEATURE_4BA_ENTER_EAR7; |
| 274 | msg_cdbg2(" Supports bank register (17h/16h)\n"); |
| 275 | } |
| 276 | if (enter_4ba & 0x20) { |
| 277 | /* Dedicated 4-byte instruction set */ |
| 278 | msg_cdbg(" Supports dedicated 4-byte instruction set (not supported by flashprog yet)\n"); |
| 279 | } |
| 280 | if (enter_4ba & 0x40) { |
| 281 | /* Always operates in 4-byte mode */ |
| 282 | msg_cdbg(" Always operates in 4-byte address mode\n" |
| 283 | " not supported by flashprog.\n"); |
| 284 | return 1; |
| 285 | |
| 286 | } |
| 287 | |
| 288 | /* Check if any 4BA method is supported */ |
| 289 | if (!(chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | |
| 290 | FEATURE_4BA_EAR_C5C8 | FEATURE_4BA_EAR_1716 | |
| 291 | FEATURE_4BA_NATIVE))) { |
| 292 | msg_cdbg("Flash chip size requires 4-byte addressing but " |
| 293 | "no supported 4BA methods found in SFDP.\n"); |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | chip->prepare_access = spi_prepare_io; |
| 298 | chip->finish_access = spi_finish_io; |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 301 | if (opcode_4k_erase != 0xFF) |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 302 | sfdp_add_uniform_eraser(chip, opcode_4k_erase, 4 * 1024); |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 303 | |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 304 | /* FIXME: double words 3-7 contain unused fast read information */ |
| 305 | |
| 306 | if (len == 4 * 4) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 307 | msg_cdbg(" It seems like this chip supports the preliminary " |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 308 | "Intel version of SFDP, skipping processing of double " |
| 309 | "words 3-9.\n"); |
| 310 | goto done; |
| 311 | } |
| 312 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 313 | /* 8. double word */ |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 314 | for (j = 0; j < 4; j++) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 315 | /* 7 double words from the start + 2 bytes for every eraser */ |
| 316 | tmp8 = buf[(4 * 7) + (j * 2)]; |
| 317 | msg_cspew(" Erase Sector Type %d Size: 0x%02x\n", j + 1, |
| 318 | tmp8); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 319 | if (tmp8 == 0) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 320 | msg_cspew(" Erase Sector Type %d is unused.\n", j); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 321 | continue; |
| 322 | } |
| 323 | if (tmp8 >= 31) { |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 324 | msg_cdbg2(" Block size of erase Sector Type %d (2^%d) " |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 325 | "is too big for flashprog.\n", j, tmp8); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 326 | continue; |
| 327 | } |
| 328 | block_size = 1 << (tmp8); /* block_size = 2 ^ field */ |
| 329 | |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 330 | tmp8 = buf[(4 * 7) + (j * 2) + 1]; |
| 331 | msg_cspew(" Erase Sector Type %d Opcode: 0x%02x\n", j + 1, |
| 332 | tmp8); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 333 | sfdp_add_uniform_eraser(chip, tmp8, block_size); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | done: |
| 337 | msg_cdbg("done.\n"); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | int probe_spi_sfdp(struct flashctx *flash) |
| 342 | { |
| 343 | int ret = 0; |
| 344 | uint8_t buf[8]; |
| 345 | uint32_t tmp32; |
| 346 | uint8_t nph; |
| 347 | /* need to limit the table loop by comparing i to uint8_t nph hence: */ |
| 348 | uint16_t i; |
| 349 | struct sfdp_tbl_hdr *hdrs; |
| 350 | uint8_t *hbuf; |
| 351 | uint8_t *tbuf; |
| 352 | |
| 353 | if (spi_sfdp_read_sfdp(flash, 0x00, buf, 4)) { |
| 354 | msg_cdbg("Receiving SFDP signature failed.\n"); |
| 355 | return 0; |
| 356 | } |
| 357 | tmp32 = buf[0]; |
| 358 | tmp32 |= ((unsigned int)buf[1]) << 8; |
| 359 | tmp32 |= ((unsigned int)buf[2]) << 16; |
| 360 | tmp32 |= ((unsigned int)buf[3]) << 24; |
| 361 | |
| 362 | if (tmp32 != 0x50444653) { |
| 363 | msg_cdbg2("Signature = 0x%08x (should be 0x50444653)\n", tmp32); |
| 364 | msg_cdbg("No SFDP signature found.\n"); |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | if (spi_sfdp_read_sfdp(flash, 0x04, buf, 3)) { |
| 369 | msg_cdbg("Receiving SFDP revision and number of parameter " |
| 370 | "headers (NPH) failed. "); |
| 371 | return 0; |
| 372 | } |
| 373 | msg_cdbg2("SFDP revision = %d.%d\n", buf[1], buf[0]); |
| 374 | if (buf[1] != 0x01) { |
| 375 | msg_cdbg("The chip supports an unknown version of SFDP. " |
| 376 | "Aborting SFDP probe!\n"); |
| 377 | return 0; |
| 378 | } |
| 379 | nph = buf[2]; |
| 380 | msg_cdbg2("SFDP number of parameter headers is %d (NPH = %d).\n", |
| 381 | nph + 1, nph); |
| 382 | |
| 383 | /* Fetch all parameter headers, even if we don't use them all (yet). */ |
| 384 | hbuf = malloc((nph + 1) * 8); |
| Angel Pons | 690a944 | 2021-06-07 12:33:53 +0200 | [diff] [blame] | 385 | hdrs = malloc((nph + 1) * sizeof(*hdrs)); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 386 | if (hbuf == NULL || hdrs == NULL ) { |
| 387 | msg_gerr("Out of memory!\n"); |
| 388 | goto cleanup_hdrs; |
| 389 | } |
| 390 | if (spi_sfdp_read_sfdp(flash, 0x08, hbuf, (nph + 1) * 8)) { |
| 391 | msg_cdbg("Receiving SFDP parameter table headers failed.\n"); |
| 392 | goto cleanup_hdrs; |
| 393 | } |
| 394 | |
| 395 | for (i = 0; i <= nph; i++) { |
| 396 | uint16_t len; |
| 397 | hdrs[i].id = hbuf[(8 * i) + 0]; |
| 398 | hdrs[i].v_minor = hbuf[(8 * i) + 1]; |
| 399 | hdrs[i].v_major = hbuf[(8 * i) + 2]; |
| 400 | hdrs[i].len = hbuf[(8 * i) + 3]; |
| 401 | hdrs[i].ptp = hbuf[(8 * i) + 4]; |
| 402 | hdrs[i].ptp |= ((unsigned int)hbuf[(8 * i) + 5]) << 8; |
| 403 | hdrs[i].ptp |= ((unsigned int)hbuf[(8 * i) + 6]) << 16; |
| 404 | msg_cdbg2("\nSFDP parameter table header %d/%d:\n", i, nph); |
| 405 | msg_cdbg2(" ID 0x%02x, version %d.%d\n", hdrs[i].id, |
| 406 | hdrs[i].v_major, hdrs[i].v_minor); |
| 407 | len = hdrs[i].len * 4; |
| 408 | tmp32 = hdrs[i].ptp; |
| 409 | msg_cdbg2(" Length %d B, Parameter Table Pointer 0x%06x\n", |
| 410 | len, tmp32); |
| 411 | |
| 412 | if (tmp32 + len >= (1 << 24)) { |
| 413 | msg_cdbg("SFDP Parameter Table %d supposedly overflows " |
| 414 | "addressable SFDP area. This most\nprobably " |
| 415 | "indicates a corrupt SFDP parameter table " |
| 416 | "header. Skipping it.\n", i); |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | tbuf = malloc(len); |
| 421 | if (tbuf == NULL) { |
| 422 | msg_gerr("Out of memory!\n"); |
| 423 | goto cleanup_hdrs; |
| 424 | } |
| 425 | if (spi_sfdp_read_sfdp(flash, tmp32, tbuf, len)){ |
| 426 | msg_cdbg("Fetching SFDP parameter table %d failed.\n", |
| 427 | i); |
| 428 | free(tbuf); |
| 429 | continue; |
| 430 | } |
| 431 | msg_cspew(" Parameter table contents:\n"); |
| 432 | for (tmp32 = 0; tmp32 < len; tmp32++) { |
| 433 | if ((tmp32 % 8) == 0) { |
| 434 | msg_cspew(" 0x%04x: ", tmp32); |
| 435 | } |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 436 | msg_cspew(" %02x", tbuf[tmp32]); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 437 | if ((tmp32 % 8) == 7) { |
| 438 | msg_cspew("\n"); |
| 439 | continue; |
| 440 | } |
| 441 | if ((tmp32 % 8) == 3) { |
| 442 | msg_cspew(" "); |
| 443 | continue; |
| 444 | } |
| 445 | } |
| Stefan Tauner | 75adf32 | 2012-02-22 00:14:14 +0000 | [diff] [blame] | 446 | msg_cspew("\n"); |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 447 | |
| 448 | if (i == 0) { /* Mandatory JEDEC SFDP parameter table */ |
| 449 | if (hdrs[i].id != 0) |
| 450 | msg_cdbg("ID of the mandatory JEDEC SFDP " |
| 451 | "parameter table is not 0 as demanded " |
| 452 | "by JESD216 (warning only).\n"); |
| 453 | |
| 454 | if (hdrs[i].v_major != 0x01) { |
| 455 | msg_cdbg("The chip contains an unknown " |
| 456 | "version of the JEDEC flash " |
| 457 | "parameters table, skipping it.\n"); |
| Michael Niewöhner | de307c0 | 2021-12-11 22:15:06 +0100 | [diff] [blame] | 458 | } else if (len != 4 * 4 && len < 9 * 4) { |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 459 | msg_cdbg("Length of the mandatory JEDEC SFDP " |
| 460 | "parameter table is wrong (%d B), " |
| 461 | "skipping it.\n", len); |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 462 | } else if (sfdp_fill_flash(flash->chip, tbuf, len) == 0) |
| Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 463 | ret = 1; |
| 464 | } |
| 465 | free(tbuf); |
| 466 | } |
| 467 | |
| 468 | cleanup_hdrs: |
| 469 | free(hdrs); |
| 470 | free(hbuf); |
| 471 | return ret; |
| 472 | } |