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> |
| 14 | |
| 15 | #include "spi_flash_internal.h" |
| 16 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 17 | static int spi_flash_read_write(struct spi_slave *spi, |
| 18 | const u8 *cmd, size_t cmd_len, |
| 19 | const u8 *data_out, u8 *data_in, |
| 20 | size_t data_len) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 21 | { |
| 22 | unsigned long flags = SPI_XFER_BEGIN; |
| 23 | int ret; |
| 24 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 25 | if (data_len == 0) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 26 | flags |= SPI_XFER_END; |
| 27 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 28 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 29 | if (ret) { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 30 | debug("SF: Failed to send command (%zu bytes): %d\n", |
| 31 | cmd_len, ret); |
| 32 | } else if (data_len != 0) { |
| 33 | 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] | 34 | if (ret) |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 35 | debug("SF: Failed to transfer %zu bytes of data: %d\n", |
| 36 | data_len, ret); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 42 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
| 43 | { |
| 44 | return spi_flash_cmd_read(spi, &cmd, 1, response, len); |
| 45 | } |
| 46 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 47 | int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 48 | size_t cmd_len, void *data, size_t data_len) |
| 49 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 50 | 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] | 51 | } |
| 52 | |
| 53 | int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, |
| 54 | const void *data, size_t data_len) |
| 55 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 56 | 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] | 57 | } |
| 58 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 59 | int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, |
| 60 | size_t cmd_len, void *data, size_t data_len) |
| 61 | { |
| 62 | struct spi_slave *spi = flash->spi; |
| 63 | int ret; |
| 64 | |
| 65 | spi_claim_bus(spi); |
| 66 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 67 | spi_release_bus(spi); |
| 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | |
Mike Frysinger | 6163045 | 2011-01-10 02:20:12 -0500 | [diff] [blame^] | 72 | int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout, |
| 73 | u8 cmd, u8 poll_bit) |
| 74 | { |
| 75 | struct spi_slave *spi = flash->spi; |
| 76 | unsigned long timebase; |
| 77 | int ret; |
| 78 | u8 status; |
| 79 | |
| 80 | ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN); |
| 81 | if (ret) { |
| 82 | debug("SF: Failed to send command %02x: %d\n", cmd, ret); |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | timebase = get_timer(0); |
| 87 | do { |
| 88 | ret = spi_xfer(spi, 8, NULL, &status, 0); |
| 89 | if (ret) |
| 90 | return -1; |
| 91 | |
| 92 | if ((status & poll_bit) == 0) |
| 93 | break; |
| 94 | |
| 95 | } while (get_timer(timebase) < timeout); |
| 96 | |
| 97 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); |
| 98 | |
| 99 | if ((status & poll_bit) == 0) |
| 100 | return 0; |
| 101 | |
| 102 | /* Timed out */ |
| 103 | debug("SF: time out!\n"); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 108 | { |
| 109 | return spi_flash_cmd_poll_bit(flash, timeout, |
| 110 | CMD_READ_STATUS, STATUS_WIP); |
| 111 | } |
| 112 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 113 | /* |
| 114 | * The following table holds all device probe functions |
| 115 | * |
| 116 | * shift: number of continuation bytes before the ID |
| 117 | * idcode: the expected IDCODE or 0xff for non JEDEC devices |
| 118 | * probe: the function to call |
| 119 | * |
| 120 | * Non JEDEC devices should be ordered in the table such that |
| 121 | * the probe functions with best detection algorithms come first. |
| 122 | * |
| 123 | * Several matching entries are permitted, they will be tried |
| 124 | * in sequence until a probe function returns non NULL. |
| 125 | * |
| 126 | * IDCODE_CONT_LEN may be redefined if a device needs to declare a |
| 127 | * larger "shift" value. IDCODE_PART_LEN generally shouldn't be |
| 128 | * changed. This is the max number of bytes probe functions may |
| 129 | * examine when looking up part-specific identification info. |
| 130 | * |
| 131 | * Probe functions will be given the idcode buffer starting at their |
| 132 | * manu id byte (the "idcode" in the table below). In other words, |
| 133 | * all of the continuation bytes will be skipped (the "shift" below). |
| 134 | */ |
| 135 | #define IDCODE_CONT_LEN 0 |
| 136 | #define IDCODE_PART_LEN 5 |
| 137 | static const struct { |
| 138 | const u8 shift; |
| 139 | const u8 idcode; |
| 140 | struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); |
| 141 | } flashes[] = { |
| 142 | /* Keep it sorted by define name */ |
| 143 | #ifdef CONFIG_SPI_FLASH_ATMEL |
| 144 | { 0, 0x1f, spi_flash_probe_atmel, }, |
| 145 | #endif |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 146 | #ifdef CONFIG_SPI_FLASH_EON |
| 147 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 148 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 149 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 150 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 151 | #endif |
| 152 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 153 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 154 | #endif |
| 155 | #ifdef CONFIG_SPI_FLASH_SST |
| 156 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 157 | #endif |
| 158 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 159 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 160 | #endif |
| 161 | #ifdef CONFIG_SPI_FLASH_WINBOND |
| 162 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 163 | #endif |
Reinhard Meyer | e0987e2 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 164 | #ifdef CONFIG_SPI_FRAM_RAMTRON |
| 165 | { 6, 0xc2, spi_fram_probe_ramtron, }, |
| 166 | # undef IDCODE_CONT_LEN |
| 167 | # define IDCODE_CONT_LEN 6 |
| 168 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 169 | /* Keep it sorted by best detection */ |
| 170 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 171 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 172 | #endif |
Reinhard Meyer | e0987e2 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 173 | #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC |
| 174 | { 0, 0xff, spi_fram_probe_ramtron, }, |
| 175 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 176 | }; |
| 177 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 178 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 179 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 180 | unsigned int max_hz, unsigned int spi_mode) |
| 181 | { |
| 182 | struct spi_slave *spi; |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 183 | struct spi_flash *flash = NULL; |
| 184 | int ret, i, shift; |
| 185 | u8 idcode[IDCODE_LEN], *idp; |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 186 | |
| 187 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 188 | if (!spi) { |
Mike Frysinger | b376bbb | 2010-04-29 00:35:12 -0400 | [diff] [blame] | 189 | printf("SF: Failed to set up slave\n"); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | ret = spi_claim_bus(spi); |
| 194 | if (ret) { |
| 195 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 196 | goto err_claim_bus; |
| 197 | } |
| 198 | |
| 199 | /* Read the ID codes */ |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 200 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 201 | if (ret) |
| 202 | goto err_read_id; |
| 203 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 204 | #ifdef DEBUG |
| 205 | printf("SF: Got idcodes\n"); |
| 206 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 207 | #endif |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 208 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 209 | /* count the number of continuation bytes */ |
| 210 | for (shift = 0, idp = idcode; |
| 211 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 212 | ++shift, ++idp) |
| 213 | continue; |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 214 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 215 | /* search the table for matches in shift and id */ |
| 216 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 217 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
| 218 | /* we have a match, call probe */ |
| 219 | flash = flashes[i].probe(spi, idp); |
| 220 | if (flash) |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | if (!flash) { |
| 225 | printf("SF: Unsupported manufacturer %02x\n", *idp); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 226 | goto err_manufacturer_probe; |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 227 | } |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 228 | |
| 229 | spi_release_bus(spi); |
| 230 | |
| 231 | return flash; |
| 232 | |
| 233 | err_manufacturer_probe: |
| 234 | err_read_id: |
| 235 | spi_release_bus(spi); |
| 236 | err_claim_bus: |
| 237 | spi_free_slave(spi); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | void spi_flash_free(struct spi_flash *flash) |
| 242 | { |
| 243 | spi_free_slave(flash->spi); |
| 244 | free(flash); |
| 245 | } |