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 | |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 10 | #include <cpu/x86/smm.h> |
| 11 | #include <delay.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 14 | #include <spi-generic.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 15 | #include <spi_flash.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 16 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 17 | #include "spi_flash_internal.h" |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 18 | #include <timer.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 19 | |
| 20 | static void spi_flash_addr(u32 addr, u8 *cmd) |
| 21 | { |
| 22 | /* cmd[0] is actual command */ |
| 23 | cmd[1] = addr >> 16; |
| 24 | cmd[2] = addr >> 8; |
| 25 | cmd[3] = addr >> 0; |
| 26 | } |
| 27 | |
| 28 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
| 29 | { |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 30 | int ret = spi_xfer(spi, &cmd, sizeof(cmd), response, len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 31 | if (ret) |
| 32 | printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret); |
| 33 | |
| 34 | return ret; |
| 35 | } |
| 36 | |
| 37 | int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 38 | size_t cmd_len, void *data, size_t data_len) |
| 39 | { |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 40 | int ret = spi_xfer(spi, cmd, cmd_len, data, data_len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 41 | if (ret) { |
| 42 | printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n", |
| 43 | data_len, ret); |
| 44 | } |
| 45 | |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, |
| 50 | const void *data, size_t data_len) |
| 51 | { |
| 52 | int ret; |
| 53 | u8 buff[cmd_len + data_len]; |
| 54 | memcpy(buff, cmd, cmd_len); |
| 55 | memcpy(buff + cmd_len, data, data_len); |
| 56 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 57 | ret = spi_xfer(spi, buff, cmd_len + data_len, NULL, 0); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 58 | if (ret) { |
| 59 | printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n", |
| 60 | data_len, ret); |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, |
| 67 | size_t cmd_len, void *data, size_t data_len) |
| 68 | { |
| 69 | struct spi_slave *spi = flash->spi; |
| 70 | int ret; |
| 71 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 72 | spi->rw = SPI_READ_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 73 | spi_claim_bus(spi); |
| 74 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 75 | spi_release_bus(spi); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset, |
| 81 | size_t len, void *data) |
| 82 | { |
| 83 | struct spi_slave *spi = flash->spi; |
| 84 | u8 cmd[5]; |
| 85 | |
| 86 | cmd[0] = CMD_READ_ARRAY_FAST; |
| 87 | spi_flash_addr(offset, cmd); |
| 88 | cmd[4] = 0x00; |
| 89 | |
| 90 | return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len); |
| 91 | } |
| 92 | |
| 93 | int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset, |
| 94 | size_t len, void *data) |
| 95 | { |
| 96 | struct spi_slave *spi = flash->spi; |
| 97 | u8 cmd[4]; |
| 98 | |
| 99 | cmd[0] = CMD_READ_ARRAY_SLOW; |
| 100 | spi_flash_addr(offset, cmd); |
| 101 | |
| 102 | return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len); |
| 103 | } |
| 104 | |
| 105 | int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout, |
| 106 | u8 cmd, u8 poll_bit) |
| 107 | { |
| 108 | struct spi_slave *spi = flash->spi; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 109 | int ret; |
| 110 | u8 status; |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 111 | struct mono_time current, end; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 112 | |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 113 | timer_monotonic_get(¤t); |
| 114 | end = current; |
| 115 | mono_time_add_msecs(&end, timeout); |
| 116 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 117 | do { |
| 118 | ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1); |
| 119 | if (ret) |
| 120 | return -1; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 121 | if ((status & poll_bit) == 0) |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 122 | return 0; |
| 123 | timer_monotonic_get(¤t); |
| 124 | } while (!mono_time_after(¤t, &end)); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 125 | |
Dave Frodin | c50c0ab | 2014-06-11 12:53:47 -0600 | [diff] [blame] | 126 | printk(BIOS_DEBUG, "SF: timeout at %ld msec\n",timeout); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 131 | { |
| 132 | return spi_flash_cmd_poll_bit(flash, timeout, |
| 133 | CMD_READ_STATUS, STATUS_WIP); |
| 134 | } |
| 135 | |
| 136 | int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd, |
| 137 | u32 offset, size_t len) |
| 138 | { |
| 139 | u32 start, end, erase_size; |
| 140 | int ret; |
| 141 | u8 cmd[4]; |
| 142 | |
| 143 | erase_size = flash->sector_size; |
| 144 | if (offset % erase_size || len % erase_size) { |
| 145 | printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 149 | flash->spi->rw = SPI_WRITE_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 150 | ret = spi_claim_bus(flash->spi); |
| 151 | if (ret) { |
| 152 | printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n"); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | cmd[0] = erase_cmd; |
| 157 | start = offset; |
| 158 | end = start + len; |
| 159 | |
| 160 | while (offset < end) { |
| 161 | spi_flash_addr(offset, cmd); |
| 162 | offset += erase_size; |
| 163 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 164 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 165 | printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], |
| 166 | cmd[2], cmd[3], offset); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 167 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 168 | ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0); |
| 169 | if (ret) |
| 170 | goto out; |
| 171 | |
| 172 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0); |
| 173 | if (ret) |
| 174 | goto out; |
| 175 | |
| 176 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); |
| 177 | if (ret) |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start); |
| 182 | |
Patrick Georgi | 20959ba | 2012-05-12 23:30:36 +0200 | [diff] [blame] | 183 | out: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 184 | spi_release_bus(flash->spi); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * The following table holds all device probe functions |
| 190 | * |
| 191 | * shift: number of continuation bytes before the ID |
| 192 | * idcode: the expected IDCODE or 0xff for non JEDEC devices |
| 193 | * probe: the function to call |
| 194 | * |
| 195 | * Non JEDEC devices should be ordered in the table such that |
| 196 | * the probe functions with best detection algorithms come first. |
| 197 | * |
| 198 | * Several matching entries are permitted, they will be tried |
| 199 | * in sequence until a probe function returns non NULL. |
| 200 | * |
| 201 | * IDCODE_CONT_LEN may be redefined if a device needs to declare a |
| 202 | * larger "shift" value. IDCODE_PART_LEN generally shouldn't be |
| 203 | * changed. This is the max number of bytes probe functions may |
| 204 | * examine when looking up part-specific identification info. |
| 205 | * |
| 206 | * Probe functions will be given the idcode buffer starting at their |
| 207 | * manu id byte (the "idcode" in the table below). In other words, |
| 208 | * all of the continuation bytes will be skipped (the "shift" below). |
| 209 | */ |
| 210 | #define IDCODE_CONT_LEN 0 |
| 211 | #define IDCODE_PART_LEN 5 |
Duncan Laurie | 181bbdd | 2012-06-23 16:53:57 -0700 | [diff] [blame] | 212 | static struct { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 213 | const u8 shift; |
| 214 | const u8 idcode; |
| 215 | struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); |
| 216 | } flashes[] = { |
| 217 | /* Keep it sorted by define name */ |
Idwer Vollering | 73a1018 | 2014-02-16 00:32:13 +0000 | [diff] [blame] | 218 | #if CONFIG_SPI_FLASH_AMIC |
| 219 | { 0, 0x37, spi_flash_probe_amic, }, |
| 220 | #endif |
Kyösti Mälkki | 96d9276 | 2014-11-11 15:04:38 +0200 | [diff] [blame] | 221 | #if CONFIG_SPI_FLASH_ATMEL |
| 222 | { 0, 0x1f, spi_flash_probe_atmel, }, |
| 223 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 224 | #if CONFIG_SPI_FLASH_EON |
| 225 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 226 | #endif |
Martin Roth | bceaf7f | 2012-09-07 15:02:35 -0600 | [diff] [blame] | 227 | #if CONFIG_SPI_FLASH_GIGADEVICE |
| 228 | { 0, 0xc8, spi_flash_probe_gigadevice, }, |
| 229 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 230 | #if CONFIG_SPI_FLASH_MACRONIX |
| 231 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 232 | #endif |
| 233 | #if CONFIG_SPI_FLASH_SPANSION |
| 234 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 235 | #endif |
| 236 | #if CONFIG_SPI_FLASH_SST |
| 237 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 238 | #endif |
| 239 | #if CONFIG_SPI_FLASH_STMICRO |
| 240 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 241 | #endif |
| 242 | #if CONFIG_SPI_FLASH_WINBOND |
| 243 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 244 | #endif |
| 245 | /* Keep it sorted by best detection */ |
| 246 | #if CONFIG_SPI_FLASH_STMICRO |
| 247 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 248 | #endif |
Chris Douglass | b34739b | 2014-02-14 13:51:26 -0500 | [diff] [blame] | 249 | #if CONFIG_SPI_FLASH_ADESTO |
| 250 | { 0, 0x1f, spi_flash_probe_adesto, }, |
| 251 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 252 | }; |
| 253 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 254 | |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 255 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 256 | { |
| 257 | struct spi_slave *spi; |
| 258 | struct spi_flash *flash = NULL; |
| 259 | int ret, i, shift; |
| 260 | u8 idcode[IDCODE_LEN], *idp; |
| 261 | |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 262 | spi = spi_setup_slave(bus, cs); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 263 | if (!spi) { |
| 264 | printk(BIOS_WARNING, "SF: Failed to set up slave\n"); |
| 265 | return NULL; |
| 266 | } |
| 267 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 268 | spi->rw = SPI_READ_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 269 | ret = spi_claim_bus(spi); |
| 270 | if (ret) { |
| 271 | printk(BIOS_WARNING, "SF: Failed to claim SPI bus: %d\n", ret); |
| 272 | goto err_claim_bus; |
| 273 | } |
| 274 | |
Vladimir Serbinenko | e23bd0e | 2014-01-18 17:45:32 +0100 | [diff] [blame] | 275 | if (spi->force_programmer_specific && spi->programmer_specific_probe) { |
| 276 | flash = spi->programmer_specific_probe (spi); |
| 277 | if (!flash) |
| 278 | goto err_read_id; |
| 279 | goto flash_detected; |
| 280 | } |
| 281 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 282 | /* Read the ID codes */ |
| 283 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
| 284 | if (ret) |
| 285 | goto err_read_id; |
| 286 | |
| 287 | #if CONFIG_DEBUG_SPI_FLASH |
David Hendricks | b598bb3 | 2014-03-21 19:32:09 -0700 | [diff] [blame^] | 288 | printk(BIOS_SPEW, "SF: Got idcode: "); |
| 289 | for (i = 0; i < sizeof(idcode); i++) |
| 290 | printk(BIOS_SPEW, "%02x ", idcode[i]); |
| 291 | printk(BIOS_SPEW, "\n"); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 292 | #endif |
| 293 | |
| 294 | /* count the number of continuation bytes */ |
| 295 | for (shift = 0, idp = idcode; |
| 296 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 297 | ++shift, ++idp) |
| 298 | continue; |
| 299 | |
| 300 | /* search the table for matches in shift and id */ |
| 301 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 302 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 303 | #if CONFIG_SMM_TSEG && defined(__SMM__) |
Duncan Laurie | 181bbdd | 2012-06-23 16:53:57 -0700 | [diff] [blame] | 304 | /* Need to relocate this function */ |
| 305 | tseg_relocate((void **)&flashes[i].probe); |
| 306 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 307 | /* we have a match, call probe */ |
| 308 | flash = flashes[i].probe(spi, idp); |
| 309 | if (flash) |
| 310 | break; |
| 311 | } |
| 312 | |
Vladimir Serbinenko | e23bd0e | 2014-01-18 17:45:32 +0100 | [diff] [blame] | 313 | if (!flash && spi->programmer_specific_probe) { |
| 314 | #if CONFIG_SMM_TSEG && defined(__SMM__) |
| 315 | /* Need to relocate this function */ |
| 316 | tseg_relocate((void **)&spi->programmer_specific_probe); |
| 317 | #endif |
| 318 | flash = spi->programmer_specific_probe (spi); |
| 319 | } |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 320 | if (!flash) { |
| 321 | printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp); |
| 322 | goto err_manufacturer_probe; |
| 323 | } |
| 324 | |
Vladimir Serbinenko | e23bd0e | 2014-01-18 17:45:32 +0100 | [diff] [blame] | 325 | flash_detected: |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 326 | #if CONFIG_SMM_TSEG && defined(__SMM__) |
| 327 | /* Ensure flash handlers are valid for TSEG */ |
| 328 | tseg_relocate((void **)&flash->read); |
| 329 | tseg_relocate((void **)&flash->write); |
| 330 | tseg_relocate((void **)&flash->erase); |
| 331 | tseg_relocate((void **)&flash->name); |
| 332 | #endif |
| 333 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 334 | printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n", |
| 335 | flash->name, flash->sector_size, flash->size); |
| 336 | |
| 337 | spi_release_bus(spi); |
| 338 | |
| 339 | return flash; |
| 340 | |
| 341 | err_manufacturer_probe: |
| 342 | err_read_id: |
| 343 | spi_release_bus(spi); |
| 344 | err_claim_bus: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 345 | return NULL; |
| 346 | } |