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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | #include "flash.h" |
| 23 | #include "spi.h" |
| 24 | #include "chipdrivers.h" |
| 25 | |
| 26 | static int spi_sfdp_read_sfdp_chunk(struct flashctx *flash, uint32_t address, uint8_t *buf, int len) |
| 27 | { |
| 28 | int i, ret; |
| 29 | const unsigned char cmd[JEDEC_SFDP_OUTSIZE] = { |
| 30 | JEDEC_SFDP, |
| 31 | (address >> 16) & 0xff, |
| 32 | (address >> 8) & 0xff, |
| 33 | (address >> 0) & 0xff, |
| 34 | /* FIXME: the following dummy byte explodes on some programmers. |
| 35 | * One possible workaround would be to read the dummy byte |
| 36 | * instead and discard its value. |
| 37 | */ |
| 38 | 0 |
| 39 | }; |
| 40 | msg_cspew("%s: addr=0x%x, len=%d, data:\n", __func__, address, len); |
| 41 | ret = spi_send_command(flash, sizeof(cmd), len, cmd, buf); |
| 42 | for (i = 0; i < len; i++) |
| 43 | msg_cspew(" 0x%02x", buf[i]); |
| 44 | msg_cspew("\n"); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | static int spi_sfdp_read_sfdp(struct flashctx *flash, uint32_t address, uint8_t *buf, int len) |
| 49 | { |
| 50 | /* FIXME: this is wrong. There are different upper bounds for the number |
| 51 | * of bytes to read on the various programmers (even depending on the |
| 52 | * rest of the structure of the transaction).*/ |
| 53 | int maxstep = 8; |
| 54 | int ret = 0; |
| 55 | while (len > 0) { |
| 56 | int step = min(len, maxstep); |
| 57 | ret = spi_sfdp_read_sfdp_chunk(flash, address, buf, step); |
| 58 | if (ret) |
| 59 | return ret; |
| 60 | address += step; |
| 61 | buf += step; |
| 62 | len -= step; |
| 63 | } |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | struct sfdp_tbl_hdr { |
| 68 | uint8_t id; |
| 69 | uint8_t v_minor; |
| 70 | uint8_t v_major; |
| 71 | uint8_t len; |
| 72 | uint32_t ptp; /* 24b pointer */ |
| 73 | }; |
| 74 | |
| 75 | static int sfdp_add_uniform_eraser(struct flashctx *flash, uint8_t opcode, uint32_t block_size) |
| 76 | { |
| 77 | int i; |
| 78 | uint32_t total_size = flash->total_size * 1024; |
| 79 | erasefunc_t *erasefn = spi_get_erasefn_from_opcode(opcode); |
| 80 | |
| 81 | if (erasefn == NULL || block_size == 0 || total_size % block_size != 0) { |
| 82 | msg_cdbg("%s: invalid input\n", __func__); |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | for (i = 0; i < NUM_ERASEFUNCTIONS; i++) { |
| 87 | struct block_eraser *eraser = &flash->block_erasers[i]; |
| 88 | /* Check for duplicates (including (some) non-uniform ones). */ |
| 89 | if (eraser->eraseblocks[0].size == block_size && |
| 90 | eraser->block_erase == erasefn) { |
| 91 | msg_cdbg2(" Tried to add a duplicate block eraser: " |
| 92 | "%d x %d B with opcode 0x%02x\n", |
| 93 | total_size/block_size, block_size, opcode); |
| 94 | return 1; |
| 95 | } |
| 96 | if (eraser->eraseblocks[0].size != 0 || !eraser->block_erase) { |
| 97 | msg_cspew(" Block Eraser %d is already occupied.\n", |
| 98 | i); |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | eraser->block_erase = erasefn; |
| 103 | eraser->eraseblocks[0].size = block_size; |
| 104 | eraser->eraseblocks[0].count = total_size/block_size; |
| 105 | msg_cdbg2(" Block eraser %d: %d x %d B with opcode " |
| 106 | "0x%02x\n", i, total_size/block_size, block_size, |
| 107 | opcode); |
| 108 | return 0; |
| 109 | } |
| 110 | msg_cinfo("%s: Not enough space to store another eraser (i=%d)." |
| 111 | " Please report this at flashrom@flashrom.org\n", |
| 112 | __func__, i); |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | static int sfdp_fill_flash(struct flashctx *flash, uint8_t *buf, uint16_t len) |
| 117 | { |
| 118 | uint32_t tmp32; |
| 119 | uint8_t tmp8; |
| 120 | uint32_t total_size; /* in bytes */ |
| 121 | uint32_t block_size; |
| 122 | int dw, j; |
| 123 | |
| 124 | msg_cdbg("Parsing JEDEC flash parameter table... "); |
| 125 | if (len != 9 * 4 && len != 4 * 4) { |
| 126 | msg_cdbg("%s: len out of spec\n", __func__); |
| 127 | return 1; |
| 128 | } |
| 129 | msg_cdbg2("\n"); |
| 130 | |
| 131 | /* 1. double word */ |
| 132 | dw = 0; |
| 133 | tmp32 = buf[(4 * dw) + 0]; |
| 134 | tmp32 |= ((unsigned int)buf[(4 * dw) + 1]) << 8; |
| 135 | tmp32 |= ((unsigned int)buf[(4 * dw) + 2]) << 16; |
| 136 | tmp32 |= ((unsigned int)buf[(4 * dw) + 3]) << 24; |
| 137 | |
| 138 | tmp8 = (tmp32 >> 17) & 0x3; |
| 139 | switch (tmp8) { |
| 140 | case 0x0: |
| 141 | msg_cdbg2(" 3-Byte only addressing.\n"); |
| 142 | break; |
| 143 | case 0x1: |
| 144 | msg_cdbg2(" 3-Byte (and optionally 4-Byte) addressing.\n"); |
| 145 | break; |
| 146 | case 0x2: |
| 147 | msg_cdbg(" 4-Byte only addressing (not supported by " |
| 148 | "flashrom).\n"); |
| 149 | return 1; |
| 150 | default: |
| 151 | msg_cdbg(" Required addressing mode (0x%x) not supported.\n", |
| 152 | tmp8); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | msg_cdbg2(" Status register is "); |
| 157 | if (tmp32 & (1 << 3)) { |
| 158 | msg_cdbg2("volatile and writes to the status register have to " |
| 159 | "be enabled with "); |
| 160 | if (tmp32 & (1 << 4)) { |
| 161 | flash->feature_bits = FEATURE_WRSR_WREN; |
| 162 | msg_cdbg2("WREN (0x06).\n"); |
| 163 | } else { |
| 164 | flash->feature_bits = FEATURE_WRSR_EWSR; |
| 165 | msg_cdbg2("EWSR (0x50).\n"); |
| 166 | } |
| 167 | } else |
| 168 | msg_cdbg2("non-volatile and the standard does not allow " |
| 169 | "vendors to tell us whether EWSR/WREN is needed for " |
| 170 | "status register writes - assuming EWSR.\n"); |
| 171 | |
| 172 | msg_cdbg2(" Write chunk size is "); |
| 173 | if (tmp32 & (1 << 2)) { |
| 174 | msg_cdbg2("at least 64 B.\n"); |
| 175 | flash->page_size = 64; |
| 176 | flash->write = spi_chip_write_256; |
| 177 | } else { |
| 178 | msg_cdbg2("1 B only.\n"); |
| 179 | flash->page_size = 256; |
| 180 | flash->write = spi_chip_write_1; |
| 181 | } |
| 182 | |
| 183 | if ((tmp32 & 0x3) == 0x1) { |
| 184 | sfdp_add_uniform_eraser(flash, (tmp32 >> 8) & 0xFF, 4 * 1024); |
| 185 | } |
| 186 | |
| 187 | /* 2. double word */ |
| 188 | dw = 1; |
| 189 | tmp32 = buf[(4 * dw) + 0]; |
| 190 | tmp32 |= ((unsigned int)buf[(4 * dw) + 1]) << 8; |
| 191 | tmp32 |= ((unsigned int)buf[(4 * dw) + 2]) << 16; |
| 192 | tmp32 |= ((unsigned int)buf[(4 * dw) + 3]) << 24; |
| 193 | |
| 194 | if (tmp32 & (1 << 31)) { |
| 195 | msg_cdbg("Flash chip size >= 4 Gb/512 MB not supported.\n"); |
| 196 | return 1; |
| 197 | } |
| 198 | total_size = ((tmp32 & 0x7FFFFFFF) + 1) / 8; |
| 199 | flash->total_size = total_size / 1024; |
| 200 | msg_cdbg2(" Flash chip size is %d kB.\n", flash->total_size); |
| 201 | if (total_size > (1 << 24)) { |
| 202 | msg_cdbg("Flash chip size is bigger than what 3-Byte addressing " |
| 203 | "can access.\n"); |
| 204 | return 1; |
| 205 | } |
| 206 | |
| 207 | /* FIXME: double words 3-7 contain unused fast read information */ |
| 208 | |
| 209 | if (len == 4 * 4) { |
| 210 | msg_cdbg("It seems like this chip supports the preliminary " |
| 211 | "Intel version of SFDP, skipping processing of double " |
| 212 | "words 3-9.\n"); |
| 213 | goto done; |
| 214 | } |
| 215 | |
| 216 | dw = 8; |
| 217 | for (j = 0; j < 4; j++) { |
| 218 | /* 8 double words from the start + 2 words for every eraser */ |
| 219 | tmp8 = buf[(4 * dw) + (2 * j)]; |
| 220 | if (tmp8 == 0) { |
| 221 | msg_cdbg2(" Block eraser %d is unused.\n", j); |
| 222 | continue; |
| 223 | } |
| 224 | if (tmp8 >= 31) { |
| 225 | msg_cdbg2(" Block size of eraser %d (2^%d) is too big " |
| 226 | "for flashrom.\n", j, tmp8); |
| 227 | continue; |
| 228 | } |
| 229 | block_size = 1 << (tmp8); /* block_size = 2 ^ field */ |
| 230 | |
| 231 | tmp8 = buf[(4 * dw) + (2 * j) + 1]; |
| 232 | sfdp_add_uniform_eraser(flash, tmp8, block_size); |
| 233 | } |
| 234 | |
| 235 | done: |
| 236 | msg_cdbg("done.\n"); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | int probe_spi_sfdp(struct flashctx *flash) |
| 241 | { |
| 242 | int ret = 0; |
| 243 | uint8_t buf[8]; |
| 244 | uint32_t tmp32; |
| 245 | uint8_t nph; |
| 246 | /* need to limit the table loop by comparing i to uint8_t nph hence: */ |
| 247 | uint16_t i; |
| 248 | struct sfdp_tbl_hdr *hdrs; |
| 249 | uint8_t *hbuf; |
| 250 | uint8_t *tbuf; |
| 251 | |
| 252 | if (spi_sfdp_read_sfdp(flash, 0x00, buf, 4)) { |
| 253 | msg_cdbg("Receiving SFDP signature failed.\n"); |
| 254 | return 0; |
| 255 | } |
| 256 | tmp32 = buf[0]; |
| 257 | tmp32 |= ((unsigned int)buf[1]) << 8; |
| 258 | tmp32 |= ((unsigned int)buf[2]) << 16; |
| 259 | tmp32 |= ((unsigned int)buf[3]) << 24; |
| 260 | |
| 261 | if (tmp32 != 0x50444653) { |
| 262 | msg_cdbg2("Signature = 0x%08x (should be 0x50444653)\n", tmp32); |
| 263 | msg_cdbg("No SFDP signature found.\n"); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | if (spi_sfdp_read_sfdp(flash, 0x04, buf, 3)) { |
| 268 | msg_cdbg("Receiving SFDP revision and number of parameter " |
| 269 | "headers (NPH) failed. "); |
| 270 | return 0; |
| 271 | } |
| 272 | msg_cdbg2("SFDP revision = %d.%d\n", buf[1], buf[0]); |
| 273 | if (buf[1] != 0x01) { |
| 274 | msg_cdbg("The chip supports an unknown version of SFDP. " |
| 275 | "Aborting SFDP probe!\n"); |
| 276 | return 0; |
| 277 | } |
| 278 | nph = buf[2]; |
| 279 | msg_cdbg2("SFDP number of parameter headers is %d (NPH = %d).\n", |
| 280 | nph + 1, nph); |
| 281 | |
| 282 | /* Fetch all parameter headers, even if we don't use them all (yet). */ |
| 283 | hbuf = malloc((nph + 1) * 8); |
| 284 | hdrs = malloc((nph + 1) * sizeof(struct sfdp_tbl_hdr)); |
| 285 | if (hbuf == NULL || hdrs == NULL ) { |
| 286 | msg_gerr("Out of memory!\n"); |
| 287 | goto cleanup_hdrs; |
| 288 | } |
| 289 | if (spi_sfdp_read_sfdp(flash, 0x08, hbuf, (nph + 1) * 8)) { |
| 290 | msg_cdbg("Receiving SFDP parameter table headers failed.\n"); |
| 291 | goto cleanup_hdrs; |
| 292 | } |
| 293 | |
| 294 | for (i = 0; i <= nph; i++) { |
| 295 | uint16_t len; |
| 296 | hdrs[i].id = hbuf[(8 * i) + 0]; |
| 297 | hdrs[i].v_minor = hbuf[(8 * i) + 1]; |
| 298 | hdrs[i].v_major = hbuf[(8 * i) + 2]; |
| 299 | hdrs[i].len = hbuf[(8 * i) + 3]; |
| 300 | hdrs[i].ptp = hbuf[(8 * i) + 4]; |
| 301 | hdrs[i].ptp |= ((unsigned int)hbuf[(8 * i) + 5]) << 8; |
| 302 | hdrs[i].ptp |= ((unsigned int)hbuf[(8 * i) + 6]) << 16; |
| 303 | msg_cdbg2("\nSFDP parameter table header %d/%d:\n", i, nph); |
| 304 | msg_cdbg2(" ID 0x%02x, version %d.%d\n", hdrs[i].id, |
| 305 | hdrs[i].v_major, hdrs[i].v_minor); |
| 306 | len = hdrs[i].len * 4; |
| 307 | tmp32 = hdrs[i].ptp; |
| 308 | msg_cdbg2(" Length %d B, Parameter Table Pointer 0x%06x\n", |
| 309 | len, tmp32); |
| 310 | |
| 311 | if (tmp32 + len >= (1 << 24)) { |
| 312 | msg_cdbg("SFDP Parameter Table %d supposedly overflows " |
| 313 | "addressable SFDP area. This most\nprobably " |
| 314 | "indicates a corrupt SFDP parameter table " |
| 315 | "header. Skipping it.\n", i); |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | tbuf = malloc(len); |
| 320 | if (tbuf == NULL) { |
| 321 | msg_gerr("Out of memory!\n"); |
| 322 | goto cleanup_hdrs; |
| 323 | } |
| 324 | if (spi_sfdp_read_sfdp(flash, tmp32, tbuf, len)){ |
| 325 | msg_cdbg("Fetching SFDP parameter table %d failed.\n", |
| 326 | i); |
| 327 | free(tbuf); |
| 328 | continue; |
| 329 | } |
| 330 | msg_cspew(" Parameter table contents:\n"); |
| 331 | for (tmp32 = 0; tmp32 < len; tmp32++) { |
| 332 | if ((tmp32 % 8) == 0) { |
| 333 | msg_cspew(" 0x%04x: ", tmp32); |
| 334 | } |
| 335 | msg_cspew(" %02x", buf[tmp32]); |
| 336 | if ((tmp32 % 8) == 7) { |
| 337 | msg_cspew("\n"); |
| 338 | continue; |
| 339 | } |
| 340 | if ((tmp32 % 8) == 3) { |
| 341 | msg_cspew(" "); |
| 342 | continue; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (i == 0) { /* Mandatory JEDEC SFDP parameter table */ |
| 347 | if (hdrs[i].id != 0) |
| 348 | msg_cdbg("ID of the mandatory JEDEC SFDP " |
| 349 | "parameter table is not 0 as demanded " |
| 350 | "by JESD216 (warning only).\n"); |
| 351 | |
| 352 | if (hdrs[i].v_major != 0x01) { |
| 353 | msg_cdbg("The chip contains an unknown " |
| 354 | "version of the JEDEC flash " |
| 355 | "parameters table, skipping it.\n"); |
| 356 | } else if (len != 9 * 4 && len != 4 * 4) { |
| 357 | msg_cdbg("Length of the mandatory JEDEC SFDP " |
| 358 | "parameter table is wrong (%d B), " |
| 359 | "skipping it.\n", len); |
| 360 | } else if (sfdp_fill_flash(flash, tbuf, len) == 0) |
| 361 | ret = 1; |
| 362 | } |
| 363 | free(tbuf); |
| 364 | } |
| 365 | |
| 366 | cleanup_hdrs: |
| 367 | free(hdrs); |
| 368 | free(hbuf); |
| 369 | return ret; |
| 370 | } |