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 | |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <spi.h> |
| 13 | #include <spi_flash.h> |
| 14 | #include <delay.h> |
Duncan Laurie | 181bbdd | 2012-06-23 16:53:57 -0700 | [diff] [blame^] | 15 | #ifdef __SMM__ |
| 16 | #include <cpu/x86/smm.h> |
| 17 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 18 | #include "spi_flash_internal.h" |
| 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 | { |
| 30 | int ret = spi_xfer(spi, &cmd, 8, response, len * 8); |
| 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 | { |
| 40 | int ret = spi_xfer(spi, cmd, cmd_len * 8, data, data_len * 8); |
| 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 | |
| 57 | ret = spi_xfer(spi, buff, (cmd_len + data_len) * 8, NULL, 0); |
| 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 | |
| 72 | spi_claim_bus(spi); |
| 73 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 74 | spi_release_bus(spi); |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset, |
| 80 | size_t len, void *data) |
| 81 | { |
| 82 | struct spi_slave *spi = flash->spi; |
| 83 | u8 cmd[5]; |
| 84 | |
| 85 | cmd[0] = CMD_READ_ARRAY_FAST; |
| 86 | spi_flash_addr(offset, cmd); |
| 87 | cmd[4] = 0x00; |
| 88 | |
| 89 | return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len); |
| 90 | } |
| 91 | |
| 92 | int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset, |
| 93 | size_t len, void *data) |
| 94 | { |
| 95 | struct spi_slave *spi = flash->spi; |
| 96 | u8 cmd[4]; |
| 97 | |
| 98 | cmd[0] = CMD_READ_ARRAY_SLOW; |
| 99 | spi_flash_addr(offset, cmd); |
| 100 | |
| 101 | return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len); |
| 102 | } |
| 103 | |
| 104 | int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout, |
| 105 | u8 cmd, u8 poll_bit) |
| 106 | { |
| 107 | struct spi_slave *spi = flash->spi; |
| 108 | unsigned long timebase; |
| 109 | int ret; |
| 110 | u8 status; |
| 111 | |
| 112 | timebase = timeout; |
| 113 | do { |
| 114 | ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1); |
| 115 | if (ret) |
| 116 | return -1; |
| 117 | |
| 118 | if ((status & poll_bit) == 0) |
| 119 | break; |
| 120 | |
Duncan Laurie | 181bbdd | 2012-06-23 16:53:57 -0700 | [diff] [blame^] | 121 | udelay(500); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 122 | } while (timebase--); |
| 123 | |
| 124 | if ((status & poll_bit) == 0) |
| 125 | return 0; |
| 126 | |
| 127 | /* Timed out */ |
| 128 | printk(BIOS_DEBUG, "SF: time out!\n"); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 133 | { |
| 134 | return spi_flash_cmd_poll_bit(flash, timeout, |
| 135 | CMD_READ_STATUS, STATUS_WIP); |
| 136 | } |
| 137 | |
| 138 | int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd, |
| 139 | u32 offset, size_t len) |
| 140 | { |
| 141 | u32 start, end, erase_size; |
| 142 | int ret; |
| 143 | u8 cmd[4]; |
| 144 | |
| 145 | erase_size = flash->sector_size; |
| 146 | if (offset % erase_size || len % erase_size) { |
| 147 | printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n"); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | ret = spi_claim_bus(flash->spi); |
| 152 | if (ret) { |
| 153 | printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n"); |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | cmd[0] = erase_cmd; |
| 158 | start = offset; |
| 159 | end = start + len; |
| 160 | |
| 161 | while (offset < end) { |
| 162 | spi_flash_addr(offset, cmd); |
| 163 | offset += erase_size; |
| 164 | |
| 165 | printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], |
| 166 | cmd[2], cmd[3], offset); |
| 167 | |
| 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 */ |
| 218 | #if CONFIG_SPI_FLASH_EON |
| 219 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 220 | #endif |
| 221 | #if CONFIG_SPI_FLASH_MACRONIX |
| 222 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 223 | #endif |
| 224 | #if CONFIG_SPI_FLASH_SPANSION |
| 225 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 226 | #endif |
| 227 | #if CONFIG_SPI_FLASH_SST |
| 228 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 229 | #endif |
| 230 | #if CONFIG_SPI_FLASH_STMICRO |
| 231 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 232 | #endif |
| 233 | #if CONFIG_SPI_FLASH_WINBOND |
| 234 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 235 | #endif |
| 236 | /* Keep it sorted by best detection */ |
| 237 | #if CONFIG_SPI_FLASH_STMICRO |
| 238 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 239 | #endif |
| 240 | }; |
| 241 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 242 | |
| 243 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 244 | unsigned int max_hz, unsigned int spi_mode) |
| 245 | { |
| 246 | struct spi_slave *spi; |
| 247 | struct spi_flash *flash = NULL; |
| 248 | int ret, i, shift; |
| 249 | u8 idcode[IDCODE_LEN], *idp; |
| 250 | |
| 251 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 252 | if (!spi) { |
| 253 | printk(BIOS_WARNING, "SF: Failed to set up slave\n"); |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | ret = spi_claim_bus(spi); |
| 258 | if (ret) { |
| 259 | printk(BIOS_WARNING, "SF: Failed to claim SPI bus: %d\n", ret); |
| 260 | goto err_claim_bus; |
| 261 | } |
| 262 | |
| 263 | /* Read the ID codes */ |
| 264 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
| 265 | if (ret) |
| 266 | goto err_read_id; |
| 267 | |
| 268 | #if CONFIG_DEBUG_SPI_FLASH |
| 269 | printk(BIOS_SPEW, "SF: Got idcodes\n"); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 270 | #endif |
| 271 | |
| 272 | /* count the number of continuation bytes */ |
| 273 | for (shift = 0, idp = idcode; |
| 274 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 275 | ++shift, ++idp) |
| 276 | continue; |
| 277 | |
| 278 | /* search the table for matches in shift and id */ |
| 279 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 280 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
Duncan Laurie | 181bbdd | 2012-06-23 16:53:57 -0700 | [diff] [blame^] | 281 | #ifdef __SMM__ |
| 282 | /* Need to relocate this function */ |
| 283 | tseg_relocate((void **)&flashes[i].probe); |
| 284 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 285 | /* we have a match, call probe */ |
| 286 | flash = flashes[i].probe(spi, idp); |
| 287 | if (flash) |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | if (!flash) { |
| 292 | printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp); |
| 293 | goto err_manufacturer_probe; |
| 294 | } |
| 295 | |
| 296 | printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n", |
| 297 | flash->name, flash->sector_size, flash->size); |
| 298 | |
| 299 | spi_release_bus(spi); |
| 300 | |
| 301 | return flash; |
| 302 | |
| 303 | err_manufacturer_probe: |
| 304 | err_read_id: |
| 305 | spi_release_bus(spi); |
| 306 | err_claim_bus: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 307 | return NULL; |
| 308 | } |