Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SPI flash interface |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
| 5 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
| 6 | * |
| 7 | * Licensed under the GPL-2 or later. |
| 8 | */ |
| 9 | |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 10 | #include <cbfs.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 11 | #include <cpu/x86/smm.h> |
| 12 | #include <delay.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 15 | #include <spi-generic.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 16 | #include <spi_flash.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 17 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 18 | #include "spi_flash_internal.h" |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 19 | #include <timer.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 20 | |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 21 | static struct spi_flash *spi_flash_dev = NULL; |
| 22 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 23 | static void spi_flash_addr(u32 addr, u8 *cmd) |
| 24 | { |
| 25 | /* cmd[0] is actual command */ |
| 26 | cmd[1] = addr >> 16; |
| 27 | cmd[2] = addr >> 8; |
| 28 | cmd[3] = addr >> 0; |
| 29 | } |
| 30 | |
David Hendricks | f101bbe | 2014-03-21 19:13:34 -0700 | [diff] [blame] | 31 | /* |
| 32 | * If atomic sequencing is used, the cycle type is known to the SPI |
| 33 | * controller so that it can perform consecutive transfers and arbitrate |
| 34 | * automatically. Otherwise the SPI controller transfers whatever the |
| 35 | * user requests immediately, without regard to sequence. Atomic |
| 36 | * sequencing is commonly used on x86 platforms. |
| 37 | * |
| 38 | * SPI flash commands are simple two-step sequences. The command byte is |
| 39 | * always written first and may be followed by an address. Then data is |
| 40 | * either read or written. For atomic sequencing we'll pass everything into |
| 41 | * spi_xfer() at once and let the controller handle the details. Otherwise |
| 42 | * we will write all output bytes first and then read if necessary. |
| 43 | * |
| 44 | * FIXME: This really should be abstracted better, but that will |
| 45 | * require overhauling the entire SPI infrastructure. |
| 46 | */ |
| 47 | static int do_spi_flash_cmd(struct spi_slave *spi, const void *dout, |
| 48 | unsigned int bytes_out, void *din, unsigned int bytes_in) |
| 49 | { |
| 50 | int ret = 1; |
| 51 | |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 52 | if (spi_claim_bus(spi)) |
| 53 | return ret; |
| 54 | |
David Hendricks | f101bbe | 2014-03-21 19:13:34 -0700 | [diff] [blame] | 55 | #if CONFIG_SPI_ATOMIC_SEQUENCING == 1 |
| 56 | if (spi_xfer(spi, dout, bytes_out, din, bytes_in) < 0) |
| 57 | goto done; |
| 58 | #else |
| 59 | if (dout && bytes_out) { |
| 60 | if (spi_xfer(spi, dout, bytes_out, NULL, 0) < 0) |
| 61 | goto done; |
| 62 | } |
| 63 | |
| 64 | if (din && bytes_in) { |
| 65 | if (spi_xfer(spi, NULL, 0, din, bytes_in) < 0) |
| 66 | goto done; |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | ret = 0; |
| 71 | done: |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 72 | spi_release_bus(spi); |
David Hendricks | f101bbe | 2014-03-21 19:13:34 -0700 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 76 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
| 77 | { |
David Hendricks | f101bbe | 2014-03-21 19:13:34 -0700 | [diff] [blame] | 78 | int ret = do_spi_flash_cmd(spi, &cmd, sizeof(cmd), response, len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 79 | if (ret) |
| 80 | printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret); |
| 81 | |
| 82 | return ret; |
| 83 | } |
| 84 | |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 85 | static int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 86 | size_t cmd_len, void *data, size_t data_len) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 87 | { |
David Hendricks | f101bbe | 2014-03-21 19:13:34 -0700 | [diff] [blame] | 88 | int ret = do_spi_flash_cmd(spi, cmd, cmd_len, data, data_len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 89 | if (ret) { |
| 90 | printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n", |
| 91 | data_len, ret); |
| 92 | } |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, |
| 98 | const void *data, size_t data_len) |
| 99 | { |
| 100 | int ret; |
| 101 | u8 buff[cmd_len + data_len]; |
| 102 | memcpy(buff, cmd, cmd_len); |
| 103 | memcpy(buff + cmd_len, data, data_len); |
| 104 | |
David Hendricks | f101bbe | 2014-03-21 19:13:34 -0700 | [diff] [blame] | 105 | ret = do_spi_flash_cmd(spi, buff, cmd_len + data_len, NULL, 0); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 106 | if (ret) { |
| 107 | printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n", |
| 108 | data_len, ret); |
| 109 | } |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 114 | static int spi_flash_cmd_read_array(struct spi_slave *spi, u8 *cmd, |
| 115 | size_t cmd_len, u32 offset, |
| 116 | size_t len, void *data) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 117 | { |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 118 | while (len) { |
| 119 | size_t transfer_size; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 120 | |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 121 | if (spi->max_transfer_size) |
| 122 | transfer_size = min(len, spi->max_transfer_size); |
| 123 | else |
| 124 | transfer_size = len; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 125 | |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 126 | spi_flash_addr(offset, cmd); |
| 127 | |
| 128 | if (spi_flash_cmd_read(spi, cmd, cmd_len, data, transfer_size)) |
| 129 | break; |
| 130 | |
| 131 | offset += transfer_size; |
| 132 | data = (void *)((uintptr_t)data + transfer_size); |
| 133 | len -= transfer_size; |
| 134 | } |
| 135 | |
| 136 | return len != 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset, |
| 140 | size_t len, void *data) |
| 141 | { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 142 | u8 cmd[5]; |
| 143 | |
| 144 | cmd[0] = CMD_READ_ARRAY_FAST; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 145 | cmd[4] = 0x00; |
| 146 | |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 147 | return spi_flash_cmd_read_array(flash->spi, cmd, sizeof(cmd), |
| 148 | offset, len, data); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset, |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 152 | size_t len, void *data) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 153 | { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 154 | u8 cmd[4]; |
| 155 | |
| 156 | cmd[0] = CMD_READ_ARRAY_SLOW; |
Vadim Bendebury | f9ff353 | 2014-11-29 15:06:26 -0800 | [diff] [blame] | 157 | return spi_flash_cmd_read_array(flash->spi, cmd, sizeof(cmd), |
| 158 | offset, len, data); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout, |
| 162 | u8 cmd, u8 poll_bit) |
| 163 | { |
| 164 | struct spi_slave *spi = flash->spi; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 165 | int ret; |
| 166 | u8 status; |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 167 | struct mono_time current, end; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 168 | |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 169 | timer_monotonic_get(¤t); |
| 170 | end = current; |
| 171 | mono_time_add_msecs(&end, timeout); |
| 172 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 173 | do { |
| 174 | ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1); |
| 175 | if (ret) |
| 176 | return -1; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 177 | if ((status & poll_bit) == 0) |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 178 | return 0; |
| 179 | timer_monotonic_get(¤t); |
| 180 | } while (!mono_time_after(¤t, &end)); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 181 | |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 182 | printk(BIOS_DEBUG, "SF: timeout at %ld msec\n",timeout); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 187 | { |
| 188 | return spi_flash_cmd_poll_bit(flash, timeout, |
| 189 | CMD_READ_STATUS, STATUS_WIP); |
| 190 | } |
| 191 | |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 192 | int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 193 | { |
| 194 | u32 start, end, erase_size; |
| 195 | int ret; |
| 196 | u8 cmd[4]; |
| 197 | |
| 198 | erase_size = flash->sector_size; |
| 199 | if (offset % erase_size || len % erase_size) { |
| 200 | printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n"); |
| 201 | return -1; |
| 202 | } |
| 203 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 204 | flash->spi->rw = SPI_WRITE_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 205 | |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 206 | cmd[0] = flash->erase_cmd; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 207 | start = offset; |
| 208 | end = start + len; |
| 209 | |
| 210 | while (offset < end) { |
| 211 | spi_flash_addr(offset, cmd); |
| 212 | offset += erase_size; |
| 213 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 214 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 215 | printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], |
| 216 | cmd[2], cmd[3], offset); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 217 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 218 | ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0); |
| 219 | if (ret) |
| 220 | goto out; |
| 221 | |
| 222 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0); |
| 223 | if (ret) |
| 224 | goto out; |
| 225 | |
| 226 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); |
| 227 | if (ret) |
| 228 | goto out; |
| 229 | } |
| 230 | |
| 231 | printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start); |
| 232 | |
Patrick Georgi | 20959ba | 2012-05-12 23:30:36 +0200 | [diff] [blame] | 233 | out: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Duncan Laurie | fb03239 | 2015-01-15 15:28:46 -0800 | [diff] [blame] | 237 | int spi_flash_cmd_status(struct spi_flash *flash, u8 *reg) |
| 238 | { |
| 239 | return spi_flash_cmd(flash->spi, flash->status_cmd, reg, sizeof(*reg)); |
| 240 | } |
| 241 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 242 | /* |
| 243 | * The following table holds all device probe functions |
| 244 | * |
| 245 | * shift: number of continuation bytes before the ID |
| 246 | * idcode: the expected IDCODE or 0xff for non JEDEC devices |
| 247 | * probe: the function to call |
| 248 | * |
| 249 | * Non JEDEC devices should be ordered in the table such that |
| 250 | * the probe functions with best detection algorithms come first. |
| 251 | * |
| 252 | * Several matching entries are permitted, they will be tried |
| 253 | * in sequence until a probe function returns non NULL. |
| 254 | * |
| 255 | * IDCODE_CONT_LEN may be redefined if a device needs to declare a |
| 256 | * larger "shift" value. IDCODE_PART_LEN generally shouldn't be |
| 257 | * changed. This is the max number of bytes probe functions may |
| 258 | * examine when looking up part-specific identification info. |
| 259 | * |
| 260 | * Probe functions will be given the idcode buffer starting at their |
| 261 | * manu id byte (the "idcode" in the table below). In other words, |
| 262 | * all of the continuation bytes will be skipped (the "shift" below). |
| 263 | */ |
| 264 | #define IDCODE_CONT_LEN 0 |
| 265 | #define IDCODE_PART_LEN 5 |
Duncan Laurie | 181bbdd | 2012-06-23 16:53:57 -0700 | [diff] [blame] | 266 | static struct { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 267 | const u8 shift; |
| 268 | const u8 idcode; |
| 269 | struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); |
| 270 | } flashes[] = { |
| 271 | /* Keep it sorted by define name */ |
Idwer Vollering | 73a1018 | 2014-02-16 00:32:13 +0000 | [diff] [blame] | 272 | #if CONFIG_SPI_FLASH_AMIC |
| 273 | { 0, 0x37, spi_flash_probe_amic, }, |
| 274 | #endif |
Kyösti Mälkki | 96d9276 | 2014-11-11 15:04:38 +0200 | [diff] [blame] | 275 | #if CONFIG_SPI_FLASH_ATMEL |
| 276 | { 0, 0x1f, spi_flash_probe_atmel, }, |
| 277 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 278 | #if CONFIG_SPI_FLASH_EON |
| 279 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 280 | #endif |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 281 | #if CONFIG_SPI_FLASH_GIGADEVICE |
| 282 | { 0, 0xc8, spi_flash_probe_gigadevice, }, |
| 283 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 284 | #if CONFIG_SPI_FLASH_MACRONIX |
| 285 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 286 | #endif |
| 287 | #if CONFIG_SPI_FLASH_SPANSION |
| 288 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 289 | #endif |
| 290 | #if CONFIG_SPI_FLASH_SST |
| 291 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 292 | #endif |
| 293 | #if CONFIG_SPI_FLASH_STMICRO |
| 294 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 295 | #endif |
| 296 | #if CONFIG_SPI_FLASH_WINBOND |
| 297 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 298 | #endif |
| 299 | /* Keep it sorted by best detection */ |
| 300 | #if CONFIG_SPI_FLASH_STMICRO |
| 301 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 302 | #endif |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 303 | #if CONFIG_SPI_FLASH_ADESTO |
| 304 | { 0, 0x1f, spi_flash_probe_adesto, }, |
| 305 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 306 | }; |
| 307 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 308 | |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 309 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 310 | { |
| 311 | struct spi_slave *spi; |
| 312 | struct spi_flash *flash = NULL; |
| 313 | int ret, i, shift; |
| 314 | u8 idcode[IDCODE_LEN], *idp; |
| 315 | |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 316 | spi = spi_setup_slave(bus, cs); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 317 | if (!spi) { |
| 318 | printk(BIOS_WARNING, "SF: Failed to set up slave\n"); |
| 319 | return NULL; |
| 320 | } |
| 321 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 322 | spi->rw = SPI_READ_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 323 | |
Vladimir Serbinenko | e23bd0e | 2014-01-18 17:45:32 +0100 | [diff] [blame] | 324 | if (spi->force_programmer_specific && spi->programmer_specific_probe) { |
| 325 | flash = spi->programmer_specific_probe (spi); |
| 326 | if (!flash) |
| 327 | goto err_read_id; |
| 328 | goto flash_detected; |
| 329 | } |
| 330 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 331 | /* Read the ID codes */ |
| 332 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
| 333 | if (ret) |
| 334 | goto err_read_id; |
| 335 | |
| 336 | #if CONFIG_DEBUG_SPI_FLASH |
David Hendricks | b598bb3 | 2014-03-21 19:32:09 -0700 | [diff] [blame] | 337 | printk(BIOS_SPEW, "SF: Got idcode: "); |
| 338 | for (i = 0; i < sizeof(idcode); i++) |
| 339 | printk(BIOS_SPEW, "%02x ", idcode[i]); |
| 340 | printk(BIOS_SPEW, "\n"); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 341 | #endif |
| 342 | |
| 343 | /* count the number of continuation bytes */ |
| 344 | for (shift = 0, idp = idcode; |
| 345 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 346 | ++shift, ++idp) |
| 347 | continue; |
| 348 | |
| 349 | /* search the table for matches in shift and id */ |
| 350 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 351 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 352 | #if CONFIG_SMM_TSEG && defined(__SMM__) |
Duncan Laurie | 181bbdd | 2012-06-23 16:53:57 -0700 | [diff] [blame] | 353 | /* Need to relocate this function */ |
| 354 | tseg_relocate((void **)&flashes[i].probe); |
| 355 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 356 | /* we have a match, call probe */ |
| 357 | flash = flashes[i].probe(spi, idp); |
| 358 | if (flash) |
| 359 | break; |
| 360 | } |
| 361 | |
Vladimir Serbinenko | e23bd0e | 2014-01-18 17:45:32 +0100 | [diff] [blame] | 362 | if (!flash && spi->programmer_specific_probe) { |
| 363 | #if CONFIG_SMM_TSEG && defined(__SMM__) |
| 364 | /* Need to relocate this function */ |
| 365 | tseg_relocate((void **)&spi->programmer_specific_probe); |
| 366 | #endif |
| 367 | flash = spi->programmer_specific_probe (spi); |
| 368 | } |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 369 | if (!flash) { |
| 370 | printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp); |
| 371 | goto err_manufacturer_probe; |
| 372 | } |
| 373 | |
Vladimir Serbinenko | e23bd0e | 2014-01-18 17:45:32 +0100 | [diff] [blame] | 374 | flash_detected: |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 375 | #if CONFIG_SMM_TSEG && defined(__SMM__) |
| 376 | /* Ensure flash handlers are valid for TSEG */ |
| 377 | tseg_relocate((void **)&flash->read); |
| 378 | tseg_relocate((void **)&flash->write); |
| 379 | tseg_relocate((void **)&flash->erase); |
Duncan Laurie | fb03239 | 2015-01-15 15:28:46 -0800 | [diff] [blame] | 380 | tseg_relocate((void **)&flash->status); |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 381 | tseg_relocate((void **)&flash->name); |
| 382 | #endif |
| 383 | |
David Imhoff | 8d4377b | 2015-05-03 13:47:49 +0200 | [diff] [blame^] | 384 | printk(BIOS_INFO, "SF: Detected %s with sector size 0x%x, total 0x%x\n", |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 385 | flash->name, flash->sector_size, flash->size); |
| 386 | |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 387 | spi_flash_dev = flash; |
| 388 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 389 | return flash; |
| 390 | |
| 391 | err_manufacturer_probe: |
| 392 | err_read_id: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 393 | return NULL; |
| 394 | } |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 395 | |
| 396 | /* Only the RAM stage will build in the lb_new_record symbol |
| 397 | * so only define this function if we are after that stage */ |
| 398 | #ifdef __RAMSTAGE__ |
| 399 | |
| 400 | void lb_spi_flash(struct lb_header *header) |
| 401 | { |
| 402 | struct lb_spi_flash *flash; |
| 403 | |
| 404 | flash = (struct lb_spi_flash *)lb_new_record(header); |
| 405 | |
| 406 | flash->tag = LB_TAG_SPI_FLASH; |
| 407 | flash->size = sizeof(*flash); |
| 408 | |
| 409 | /* Try to get the flash device if not loaded yet */ |
| 410 | if (!spi_flash_dev) { |
| 411 | struct cbfs_media media; |
| 412 | init_default_cbfs_media(&media); |
| 413 | } |
| 414 | |
| 415 | if (spi_flash_dev) { |
| 416 | flash->flash_size = spi_flash_dev->size; |
| 417 | flash->sector_size = spi_flash_dev->sector_size; |
| 418 | flash->erase_cmd = spi_flash_dev->erase_cmd; |
| 419 | } else { |
| 420 | flash->flash_size = CONFIG_ROM_SIZE; |
| 421 | /* Default 64k erase command should work on most flash. |
| 422 | * Uniform 4k erase only works on certain devices. */ |
| 423 | flash->sector_size = 64 * KiB; |
| 424 | flash->erase_cmd = CMD_BLOCK_ERASE; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #endif |