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 <common.h> 11 #include <malloc.h> 12 #include <spi.h> 13 #include <spi_flash.h> 14 #include <watchdog.h> 15 16 #include "spi_flash_internal.h" 17 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 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) 30 { 31 unsigned long flags = SPI_XFER_BEGIN; 32 int ret; 33 34 if (data_len == 0) 35 flags |= SPI_XFER_END; 36 37 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); 38 if (ret) { 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); 43 if (ret) 44 debug("SF: Failed to transfer %zu bytes of data: %d\n", 45 data_len, ret); 46 } 47 48 return ret; 49 } 50 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 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 { 59 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len); 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 { 65 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len); 66 } 67 68 int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset, 69 size_t len, const void *buf) 70 { 71 unsigned long page_addr, byte_addr, page_size; 72 size_t chunk_len, actual; 73 int ret; 74 u8 cmd[4]; 75 76 page_size = flash->page_size; 77 page_addr = offset / page_size; 78 byte_addr = offset % page_size; 79 80 ret = spi_claim_bus(flash->spi); 81 if (ret) { 82 debug("SF: unable to claim SPI bus\n"); 83 return ret; 84 } 85 86 cmd[0] = CMD_PAGE_PROGRAM; 87 for (actual = 0; actual < len; actual += chunk_len) { 88 chunk_len = min(len - actual, page_size - byte_addr); 89 90 cmd[1] = page_addr >> 8; 91 cmd[2] = page_addr; 92 cmd[3] = byte_addr; 93 94 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n", 95 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); 96 97 ret = spi_flash_cmd_write_enable(flash); 98 if (ret < 0) { 99 debug("SF: enabling write failed\n"); 100 break; 101 } 102 103 ret = spi_flash_cmd_write(flash->spi, cmd, 4, 104 buf + actual, chunk_len); 105 if (ret < 0) { 106 debug("SF: write failed\n"); 107 break; 108 } 109 110 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); 111 if (ret) 112 break; 113 114 page_addr++; 115 byte_addr = 0; 116 } 117 118 debug("SF: program %s %zu bytes @ %#x\n", 119 ret ? "failure" : "success", len, offset); 120 121 spi_release_bus(flash->spi); 122 return ret; 123 } 124 125 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, 126 size_t cmd_len, void *data, size_t data_len) 127 { 128 struct spi_slave *spi = flash->spi; 129 int ret; 130 131 spi_claim_bus(spi); 132 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); 133 spi_release_bus(spi); 134 135 return ret; 136 } 137 138 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset, 139 size_t len, void *data) 140 { 141 u8 cmd[5]; 142 143 cmd[0] = CMD_READ_ARRAY_FAST; 144 spi_flash_addr(offset, cmd); 145 cmd[4] = 0x00; 146 147 return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len); 148 } 149 150 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout, 151 u8 cmd, u8 poll_bit) 152 { 153 struct spi_slave *spi = flash->spi; 154 unsigned long timebase; 155 int ret; 156 u8 status; 157 158 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN); 159 if (ret) { 160 debug("SF: Failed to send command %02x: %d\n", cmd, ret); 161 return ret; 162 } 163 164 timebase = get_timer(0); 165 do { 166 WATCHDOG_RESET(); 167 168 ret = spi_xfer(spi, 8, NULL, &status, 0); 169 if (ret) 170 return -1; 171 172 if ((status & poll_bit) == 0) 173 break; 174 175 } while (get_timer(timebase) < timeout); 176 177 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); 178 179 if ((status & poll_bit) == 0) 180 return 0; 181 182 /* Timed out */ 183 debug("SF: time out!\n"); 184 return -1; 185 } 186 187 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) 188 { 189 return spi_flash_cmd_poll_bit(flash, timeout, 190 CMD_READ_STATUS, STATUS_WIP); 191 } 192 193 int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd, 194 u32 offset, size_t len) 195 { 196 u32 start, end, erase_size; 197 int ret; 198 u8 cmd[4]; 199 200 erase_size = flash->sector_size; 201 if (offset % erase_size || len % erase_size) { 202 debug("SF: Erase offset/length not multiple of erase size\n"); 203 return -1; 204 } 205 206 ret = spi_claim_bus(flash->spi); 207 if (ret) { 208 debug("SF: Unable to claim SPI bus\n"); 209 return ret; 210 } 211 212 cmd[0] = erase_cmd; 213 start = offset; 214 end = start + len; 215 216 while (offset < end) { 217 spi_flash_addr(offset, cmd); 218 offset += erase_size; 219 220 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], 221 cmd[2], cmd[3], offset); 222 223 ret = spi_flash_cmd_write_enable(flash); 224 if (ret) 225 goto out; 226 227 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0); 228 if (ret) 229 goto out; 230 231 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); 232 if (ret) 233 goto out; 234 } 235 236 debug("SF: Successfully erased %lu bytes @ %#x\n", 237 len * erase_size, start); 238 239 out: 240 spi_release_bus(flash->spi); 241 return ret; 242 } 243 244 /* 245 * The following table holds all device probe functions 246 * 247 * shift: number of continuation bytes before the ID 248 * idcode: the expected IDCODE or 0xff for non JEDEC devices 249 * probe: the function to call 250 * 251 * Non JEDEC devices should be ordered in the table such that 252 * the probe functions with best detection algorithms come first. 253 * 254 * Several matching entries are permitted, they will be tried 255 * in sequence until a probe function returns non NULL. 256 * 257 * IDCODE_CONT_LEN may be redefined if a device needs to declare a 258 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be 259 * changed. This is the max number of bytes probe functions may 260 * examine when looking up part-specific identification info. 261 * 262 * Probe functions will be given the idcode buffer starting at their 263 * manu id byte (the "idcode" in the table below). In other words, 264 * all of the continuation bytes will be skipped (the "shift" below). 265 */ 266 #define IDCODE_CONT_LEN 0 267 #define IDCODE_PART_LEN 5 268 static const struct { 269 const u8 shift; 270 const u8 idcode; 271 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); 272 } flashes[] = { 273 /* Keep it sorted by define name */ 274 #ifdef CONFIG_SPI_FLASH_ATMEL 275 { 0, 0x1f, spi_flash_probe_atmel, }, 276 #endif 277 #ifdef CONFIG_SPI_FLASH_EON 278 { 0, 0x1c, spi_flash_probe_eon, }, 279 #endif 280 #ifdef CONFIG_SPI_FLASH_MACRONIX 281 { 0, 0xc2, spi_flash_probe_macronix, }, 282 #endif 283 #ifdef CONFIG_SPI_FLASH_SPANSION 284 { 0, 0x01, spi_flash_probe_spansion, }, 285 #endif 286 #ifdef CONFIG_SPI_FLASH_SST 287 { 0, 0xbf, spi_flash_probe_sst, }, 288 #endif 289 #ifdef CONFIG_SPI_FLASH_STMICRO 290 { 0, 0x20, spi_flash_probe_stmicro, }, 291 #endif 292 #ifdef CONFIG_SPI_FLASH_WINBOND 293 { 0, 0xef, spi_flash_probe_winbond, }, 294 #endif 295 #ifdef CONFIG_SPI_FRAM_RAMTRON 296 { 6, 0xc2, spi_fram_probe_ramtron, }, 297 # undef IDCODE_CONT_LEN 298 # define IDCODE_CONT_LEN 6 299 #endif 300 /* Keep it sorted by best detection */ 301 #ifdef CONFIG_SPI_FLASH_STMICRO 302 { 0, 0xff, spi_flash_probe_stmicro, }, 303 #endif 304 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC 305 { 0, 0xff, spi_fram_probe_ramtron, }, 306 #endif 307 }; 308 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) 309 310 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, 311 unsigned int max_hz, unsigned int spi_mode) 312 { 313 struct spi_slave *spi; 314 struct spi_flash *flash = NULL; 315 int ret, i, shift; 316 u8 idcode[IDCODE_LEN], *idp; 317 318 spi = spi_setup_slave(bus, cs, max_hz, spi_mode); 319 if (!spi) { 320 printf("SF: Failed to set up slave\n"); 321 return NULL; 322 } 323 324 ret = spi_claim_bus(spi); 325 if (ret) { 326 debug("SF: Failed to claim SPI bus: %d\n", ret); 327 goto err_claim_bus; 328 } 329 330 /* Read the ID codes */ 331 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); 332 if (ret) 333 goto err_read_id; 334 335 #ifdef DEBUG 336 printf("SF: Got idcodes\n"); 337 print_buffer(0, idcode, 1, sizeof(idcode), 0); 338 #endif 339 340 /* count the number of continuation bytes */ 341 for (shift = 0, idp = idcode; 342 shift < IDCODE_CONT_LEN && *idp == 0x7f; 343 ++shift, ++idp) 344 continue; 345 346 /* search the table for matches in shift and id */ 347 for (i = 0; i < ARRAY_SIZE(flashes); ++i) 348 if (flashes[i].shift == shift && flashes[i].idcode == *idp) { 349 /* we have a match, call probe */ 350 flash = flashes[i].probe(spi, idp); 351 if (flash) 352 break; 353 } 354 355 if (!flash) { 356 printf("SF: Unsupported manufacturer %02x\n", *idp); 357 goto err_manufacturer_probe; 358 } 359 360 printf("SF: Detected %s with page size ", flash->name); 361 print_size(flash->sector_size, ", total "); 362 print_size(flash->size, "\n"); 363 364 spi_release_bus(spi); 365 366 return flash; 367 368 err_manufacturer_probe: 369 err_read_id: 370 spi_release_bus(spi); 371 err_claim_bus: 372 spi_free_slave(spi); 373 return NULL; 374 } 375 376 void spi_flash_free(struct spi_flash *flash) 377 { 378 spi_free_slave(flash->spi); 379 free(flash); 380 } 381