Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPI flash interface |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
| 6 | * |
Mike Frysinger | 4166ee5 | 2009-10-09 17:12:44 -0400 | [diff] [blame] | 7 | * Licensed under the GPL-2 or later. |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 8 | */ |
Mike Frysinger | f773a1b | 2009-03-23 23:03:58 -0400 | [diff] [blame] | 9 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <malloc.h> |
| 12 | #include <spi.h> |
| 13 | #include <spi_flash.h> |
Patrick Sestier | bd0d19c | 2011-04-15 14:25:25 +0000 | [diff] [blame] | 14 | #include <watchdog.h> |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 15 | |
| 16 | #include "spi_flash_internal.h" |
| 17 | |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 18 | static void spi_flash_addr(u32 addr, u8 *cmd) |
| 19 | { |
| 20 | /* cmd[0] is actual command */ |
| 21 | cmd[1] = addr >> 16; |
| 22 | cmd[2] = addr >> 8; |
| 23 | cmd[3] = addr >> 0; |
| 24 | } |
| 25 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 26 | static int spi_flash_read_write(struct spi_slave *spi, |
| 27 | const u8 *cmd, size_t cmd_len, |
| 28 | const u8 *data_out, u8 *data_in, |
| 29 | size_t data_len) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 30 | { |
| 31 | unsigned long flags = SPI_XFER_BEGIN; |
| 32 | int ret; |
| 33 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 34 | if (data_len == 0) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 35 | flags |= SPI_XFER_END; |
| 36 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 37 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 38 | if (ret) { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 39 | debug("SF: Failed to send command (%zu bytes): %d\n", |
| 40 | cmd_len, ret); |
| 41 | } else if (data_len != 0) { |
| 42 | ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 43 | if (ret) |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 44 | debug("SF: Failed to transfer %zu bytes of data: %d\n", |
| 45 | data_len, ret); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 51 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
| 52 | { |
| 53 | return spi_flash_cmd_read(spi, &cmd, 1, response, len); |
| 54 | } |
| 55 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 56 | int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 57 | size_t cmd_len, void *data, size_t data_len) |
| 58 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 59 | return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, |
| 63 | const void *data, size_t data_len) |
| 64 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 65 | return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 68 | int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, |
| 69 | size_t cmd_len, void *data, size_t data_len) |
| 70 | { |
| 71 | struct spi_slave *spi = flash->spi; |
| 72 | int ret; |
| 73 | |
| 74 | spi_claim_bus(spi); |
| 75 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 76 | spi_release_bus(spi); |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
Mike Frysinger | a4c3b40 | 2011-01-10 02:20:14 -0500 | [diff] [blame] | 81 | int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset, |
| 82 | size_t len, void *data) |
| 83 | { |
| 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_read_common(flash, cmd, sizeof(cmd), data, len); |
| 91 | } |
| 92 | |
Mike Frysinger | 6163045 | 2011-01-10 02:20:12 -0500 | [diff] [blame] | 93 | int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout, |
| 94 | u8 cmd, u8 poll_bit) |
| 95 | { |
| 96 | struct spi_slave *spi = flash->spi; |
| 97 | unsigned long timebase; |
| 98 | int ret; |
| 99 | u8 status; |
| 100 | |
| 101 | ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN); |
| 102 | if (ret) { |
| 103 | debug("SF: Failed to send command %02x: %d\n", cmd, ret); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | timebase = get_timer(0); |
| 108 | do { |
Patrick Sestier | bd0d19c | 2011-04-15 14:25:25 +0000 | [diff] [blame] | 109 | WATCHDOG_RESET(); |
| 110 | |
Mike Frysinger | 6163045 | 2011-01-10 02:20:12 -0500 | [diff] [blame] | 111 | ret = spi_xfer(spi, 8, NULL, &status, 0); |
| 112 | if (ret) |
| 113 | return -1; |
| 114 | |
| 115 | if ((status & poll_bit) == 0) |
| 116 | break; |
| 117 | |
| 118 | } while (get_timer(timebase) < timeout); |
| 119 | |
| 120 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); |
| 121 | |
| 122 | if ((status & poll_bit) == 0) |
| 123 | return 0; |
| 124 | |
| 125 | /* Timed out */ |
| 126 | debug("SF: time out!\n"); |
| 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 | |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 136 | int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd, |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 137 | u32 offset, size_t len) |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 138 | { |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 139 | u32 start, end, erase_size; |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 140 | int ret; |
| 141 | u8 cmd[4]; |
| 142 | |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 143 | erase_size = flash->sector_size; |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 144 | if (offset % erase_size || len % erase_size) { |
| 145 | debug("SF: Erase offset/length not multiple of erase size\n"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | ret = spi_claim_bus(flash->spi); |
| 150 | if (ret) { |
| 151 | debug("SF: Unable to claim SPI bus\n"); |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | cmd[0] = erase_cmd; |
| 156 | start = offset; |
| 157 | end = start + len; |
| 158 | |
| 159 | while (offset < end) { |
| 160 | spi_flash_addr(offset, cmd); |
| 161 | offset += erase_size; |
| 162 | |
| 163 | debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], |
| 164 | cmd[2], cmd[3], offset); |
| 165 | |
Mike Frysinger | 2744a4e | 2011-04-23 23:05:55 +0000 | [diff] [blame^] | 166 | ret = spi_flash_cmd_write_enable(flash); |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 167 | if (ret) |
| 168 | goto out; |
| 169 | |
| 170 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0); |
| 171 | if (ret) |
| 172 | goto out; |
| 173 | |
| 174 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); |
| 175 | if (ret) |
| 176 | goto out; |
| 177 | } |
| 178 | |
| 179 | debug("SF: Successfully erased %lu bytes @ %#x\n", |
| 180 | len * erase_size, start); |
| 181 | |
| 182 | out: |
| 183 | spi_release_bus(flash->spi); |
| 184 | return ret; |
| 185 | } |
| 186 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 187 | /* |
| 188 | * The following table holds all device probe functions |
| 189 | * |
| 190 | * shift: number of continuation bytes before the ID |
| 191 | * idcode: the expected IDCODE or 0xff for non JEDEC devices |
| 192 | * probe: the function to call |
| 193 | * |
| 194 | * Non JEDEC devices should be ordered in the table such that |
| 195 | * the probe functions with best detection algorithms come first. |
| 196 | * |
| 197 | * Several matching entries are permitted, they will be tried |
| 198 | * in sequence until a probe function returns non NULL. |
| 199 | * |
| 200 | * IDCODE_CONT_LEN may be redefined if a device needs to declare a |
| 201 | * larger "shift" value. IDCODE_PART_LEN generally shouldn't be |
| 202 | * changed. This is the max number of bytes probe functions may |
| 203 | * examine when looking up part-specific identification info. |
| 204 | * |
| 205 | * Probe functions will be given the idcode buffer starting at their |
| 206 | * manu id byte (the "idcode" in the table below). In other words, |
| 207 | * all of the continuation bytes will be skipped (the "shift" below). |
| 208 | */ |
| 209 | #define IDCODE_CONT_LEN 0 |
| 210 | #define IDCODE_PART_LEN 5 |
| 211 | static const struct { |
| 212 | const u8 shift; |
| 213 | const u8 idcode; |
| 214 | struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); |
| 215 | } flashes[] = { |
| 216 | /* Keep it sorted by define name */ |
| 217 | #ifdef CONFIG_SPI_FLASH_ATMEL |
| 218 | { 0, 0x1f, spi_flash_probe_atmel, }, |
| 219 | #endif |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 220 | #ifdef CONFIG_SPI_FLASH_EON |
| 221 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 222 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 223 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 224 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 225 | #endif |
| 226 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 227 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 228 | #endif |
| 229 | #ifdef CONFIG_SPI_FLASH_SST |
| 230 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 231 | #endif |
| 232 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 233 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 234 | #endif |
| 235 | #ifdef CONFIG_SPI_FLASH_WINBOND |
| 236 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 237 | #endif |
Reinhard Meyer | e0987e2 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 238 | #ifdef CONFIG_SPI_FRAM_RAMTRON |
| 239 | { 6, 0xc2, spi_fram_probe_ramtron, }, |
| 240 | # undef IDCODE_CONT_LEN |
| 241 | # define IDCODE_CONT_LEN 6 |
| 242 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 243 | /* Keep it sorted by best detection */ |
| 244 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 245 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 246 | #endif |
Reinhard Meyer | e0987e2 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 247 | #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC |
| 248 | { 0, 0xff, spi_fram_probe_ramtron, }, |
| 249 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 250 | }; |
| 251 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 252 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 253 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 254 | unsigned int max_hz, unsigned int spi_mode) |
| 255 | { |
| 256 | struct spi_slave *spi; |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 257 | struct spi_flash *flash = NULL; |
| 258 | int ret, i, shift; |
| 259 | u8 idcode[IDCODE_LEN], *idp; |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 260 | |
| 261 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 262 | if (!spi) { |
Mike Frysinger | b376bbb | 2010-04-29 00:35:12 -0400 | [diff] [blame] | 263 | printf("SF: Failed to set up slave\n"); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | ret = spi_claim_bus(spi); |
| 268 | if (ret) { |
| 269 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 270 | goto err_claim_bus; |
| 271 | } |
| 272 | |
| 273 | /* Read the ID codes */ |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 274 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 275 | if (ret) |
| 276 | goto err_read_id; |
| 277 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 278 | #ifdef DEBUG |
| 279 | printf("SF: Got idcodes\n"); |
| 280 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 281 | #endif |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 282 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 283 | /* count the number of continuation bytes */ |
| 284 | for (shift = 0, idp = idcode; |
| 285 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 286 | ++shift, ++idp) |
| 287 | continue; |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 288 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 289 | /* search the table for matches in shift and id */ |
| 290 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 291 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
| 292 | /* we have a match, call probe */ |
| 293 | flash = flashes[i].probe(spi, idp); |
| 294 | if (flash) |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | if (!flash) { |
| 299 | printf("SF: Unsupported manufacturer %02x\n", *idp); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 300 | goto err_manufacturer_probe; |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 301 | } |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 302 | |
Mike Frysinger | 493c360 | 2011-04-12 02:09:28 -0400 | [diff] [blame] | 303 | printf("SF: Detected %s with page size ", flash->name); |
| 304 | print_size(flash->sector_size, ", total "); |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 305 | print_size(flash->size, "\n"); |
| 306 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 307 | spi_release_bus(spi); |
| 308 | |
| 309 | return flash; |
| 310 | |
| 311 | err_manufacturer_probe: |
| 312 | err_read_id: |
| 313 | spi_release_bus(spi); |
| 314 | err_claim_bus: |
| 315 | spi_free_slave(spi); |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | void spi_flash_free(struct spi_flash *flash) |
| 320 | { |
| 321 | spi_free_slave(flash->spi); |
| 322 | free(flash); |
| 323 | } |