Jagannadha Sutradharudu Teki | bb30220 | 2013-09-24 16:01:23 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPI flash probing |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
| 5 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
| 6 | * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc. |
| 7 | * |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <fdtdec.h> |
| 13 | #include <malloc.h> |
| 14 | #include <spi.h> |
| 15 | #include <spi_flash.h> |
| 16 | |
| 17 | #include "spi_flash_internal.h" |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | #ifdef CONFIG_SPI_FLASH_BAR |
| 22 | int spi_flash_bank_config(struct spi_flash *flash, u8 idcode0) |
| 23 | { |
| 24 | u8 cmd; |
| 25 | u8 curr_bank = 0; |
| 26 | |
| 27 | /* discover bank cmds */ |
| 28 | switch (idcode0) { |
| 29 | case SPI_FLASH_SPANSION_IDCODE0: |
| 30 | flash->bank_read_cmd = CMD_BANKADDR_BRRD; |
| 31 | flash->bank_write_cmd = CMD_BANKADDR_BRWR; |
| 32 | break; |
| 33 | case SPI_FLASH_STMICRO_IDCODE0: |
| 34 | case SPI_FLASH_WINBOND_IDCODE0: |
| 35 | flash->bank_read_cmd = CMD_EXTNADDR_RDEAR; |
| 36 | flash->bank_write_cmd = CMD_EXTNADDR_WREAR; |
| 37 | break; |
| 38 | default: |
| 39 | printf("SF: Unsupported bank commands %02x\n", idcode0); |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | /* read the bank reg - on which bank the flash is in currently */ |
| 44 | cmd = flash->bank_read_cmd; |
| 45 | if (flash->size > SPI_FLASH_16MB_BOUN) { |
| 46 | if (spi_flash_read_common(flash, &cmd, 1, &curr_bank, 1)) { |
| 47 | debug("SF: fail to read bank addr register\n"); |
| 48 | return -1; |
| 49 | } |
| 50 | flash->bank_curr = curr_bank; |
| 51 | } else { |
| 52 | flash->bank_curr = curr_bank; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | #ifdef CONFIG_OF_CONTROL |
| 60 | int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) |
| 61 | { |
| 62 | fdt_addr_t addr; |
| 63 | fdt_size_t size; |
| 64 | int node; |
| 65 | |
| 66 | /* If there is no node, do nothing */ |
| 67 | node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); |
| 68 | if (node < 0) |
| 69 | return 0; |
| 70 | |
| 71 | addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); |
| 72 | if (addr == FDT_ADDR_T_NONE) { |
| 73 | debug("%s: Cannot decode address\n", __func__); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | if (flash->size != size) { |
| 78 | debug("%s: Memory map must cover entire device\n", __func__); |
| 79 | return -1; |
| 80 | } |
| 81 | flash->memory_map = (void *)addr; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | #endif /* CONFIG_OF_CONTROL */ |
| 86 | |
| 87 | /* |
| 88 | * The following table holds all device probe functions |
| 89 | * |
| 90 | * shift: number of continuation bytes before the ID |
| 91 | * idcode: the expected IDCODE or 0xff for non JEDEC devices |
| 92 | * probe: the function to call |
| 93 | * |
| 94 | * Non JEDEC devices should be ordered in the table such that |
| 95 | * the probe functions with best detection algorithms come first. |
| 96 | * |
| 97 | * Several matching entries are permitted, they will be tried |
| 98 | * in sequence until a probe function returns non NULL. |
| 99 | * |
| 100 | * IDCODE_CONT_LEN may be redefined if a device needs to declare a |
| 101 | * larger "shift" value. IDCODE_PART_LEN generally shouldn't be |
| 102 | * changed. This is the max number of bytes probe functions may |
| 103 | * examine when looking up part-specific identification info. |
| 104 | * |
| 105 | * Probe functions will be given the idcode buffer starting at their |
| 106 | * manu id byte (the "idcode" in the table below). In other words, |
| 107 | * all of the continuation bytes will be skipped (the "shift" below). |
| 108 | */ |
| 109 | #define IDCODE_CONT_LEN 0 |
| 110 | #define IDCODE_PART_LEN 5 |
| 111 | static const struct { |
| 112 | const u8 shift; |
| 113 | const u8 idcode; |
| 114 | struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); |
| 115 | } flashes[] = { |
| 116 | /* Keep it sorted by define name */ |
| 117 | #ifdef CONFIG_SPI_FLASH_ATMEL |
| 118 | { 0, 0x1f, spi_flash_probe_atmel, }, |
| 119 | #endif |
| 120 | #ifdef CONFIG_SPI_FLASH_EON |
| 121 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 122 | #endif |
| 123 | #ifdef CONFIG_SPI_FLASH_GIGADEVICE |
| 124 | { 0, 0xc8, spi_flash_probe_gigadevice, }, |
| 125 | #endif |
| 126 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 127 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 128 | #endif |
| 129 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 130 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 131 | #endif |
| 132 | #ifdef CONFIG_SPI_FLASH_SST |
| 133 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 134 | #endif |
| 135 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 136 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 137 | #endif |
| 138 | #ifdef CONFIG_SPI_FLASH_WINBOND |
| 139 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 140 | #endif |
| 141 | #ifdef CONFIG_SPI_FRAM_RAMTRON |
| 142 | { 6, 0xc2, spi_fram_probe_ramtron, }, |
| 143 | # undef IDCODE_CONT_LEN |
| 144 | # define IDCODE_CONT_LEN 6 |
| 145 | #endif |
| 146 | /* Keep it sorted by best detection */ |
| 147 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 148 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 149 | #endif |
| 150 | #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC |
| 151 | { 0, 0xff, spi_fram_probe_ramtron, }, |
| 152 | #endif |
| 153 | }; |
| 154 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 155 | |
| 156 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 157 | unsigned int max_hz, unsigned int spi_mode) |
| 158 | { |
| 159 | struct spi_slave *spi; |
| 160 | struct spi_flash *flash = NULL; |
| 161 | int ret, i, shift; |
| 162 | u8 idcode[IDCODE_LEN], *idp; |
| 163 | |
| 164 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 165 | if (!spi) { |
| 166 | printf("SF: Failed to set up slave\n"); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | ret = spi_claim_bus(spi); |
| 171 | if (ret) { |
| 172 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 173 | goto err_claim_bus; |
| 174 | } |
| 175 | |
| 176 | /* Read the ID codes */ |
| 177 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
| 178 | if (ret) |
| 179 | goto err_read_id; |
| 180 | |
| 181 | #ifdef DEBUG |
| 182 | printf("SF: Got idcodes\n"); |
| 183 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 184 | #endif |
| 185 | |
| 186 | /* count the number of continuation bytes */ |
| 187 | for (shift = 0, idp = idcode; |
| 188 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 189 | ++shift, ++idp) |
| 190 | continue; |
| 191 | |
| 192 | /* search the table for matches in shift and id */ |
| 193 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 194 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
| 195 | /* we have a match, call probe */ |
| 196 | flash = flashes[i].probe(spi, idp); |
| 197 | if (flash) |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | if (!flash) { |
| 202 | printf("SF: Unsupported manufacturer %02x\n", *idp); |
| 203 | goto err_manufacturer_probe; |
| 204 | } |
| 205 | |
| 206 | #ifdef CONFIG_SPI_FLASH_BAR |
| 207 | /* Configure the BAR - disover bank cmds and read current bank */ |
| 208 | ret = spi_flash_bank_config(flash, *idp); |
| 209 | if (ret < 0) |
| 210 | goto err_manufacturer_probe; |
| 211 | #endif |
| 212 | |
| 213 | #ifdef CONFIG_OF_CONTROL |
| 214 | if (spi_flash_decode_fdt(gd->fdt_blob, flash)) { |
| 215 | debug("SF: FDT decode error\n"); |
| 216 | goto err_manufacturer_probe; |
| 217 | } |
| 218 | #endif |
| 219 | #ifndef CONFIG_SPL_BUILD |
| 220 | printf("SF: Detected %s with page size ", flash->name); |
| 221 | print_size(flash->sector_size, ", total "); |
| 222 | print_size(flash->size, ""); |
| 223 | if (flash->memory_map) |
| 224 | printf(", mapped at %p", flash->memory_map); |
| 225 | puts("\n"); |
| 226 | #endif |
| 227 | #ifndef CONFIG_SPI_FLASH_BAR |
| 228 | if (flash->size > SPI_FLASH_16MB_BOUN) { |
| 229 | puts("SF: Warning - Only lower 16MiB accessible,"); |
| 230 | puts(" Full access #define CONFIG_SPI_FLASH_BAR\n"); |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | spi_release_bus(spi); |
| 235 | |
| 236 | return flash; |
| 237 | |
| 238 | err_manufacturer_probe: |
| 239 | err_read_id: |
| 240 | spi_release_bus(spi); |
| 241 | err_claim_bus: |
| 242 | spi_free_slave(spi); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi, |
| 247 | const char *name) |
| 248 | { |
| 249 | struct spi_flash *flash; |
| 250 | void *ptr; |
| 251 | |
| 252 | ptr = malloc(size); |
| 253 | if (!ptr) { |
| 254 | debug("SF: Failed to allocate memory\n"); |
| 255 | return NULL; |
| 256 | } |
| 257 | memset(ptr, '\0', size); |
| 258 | flash = (struct spi_flash *)(ptr + offset); |
| 259 | |
| 260 | /* Set up some basic fields - caller will sort out sizes */ |
| 261 | flash->spi = spi; |
| 262 | flash->name = name; |
| 263 | flash->poll_cmd = CMD_READ_STATUS; |
| 264 | |
| 265 | flash->read = spi_flash_cmd_read_fast; |
| 266 | flash->write = spi_flash_cmd_write_multi; |
| 267 | flash->erase = spi_flash_cmd_erase; |
| 268 | |
| 269 | return flash; |
| 270 | } |
| 271 | |
| 272 | void spi_flash_free(struct spi_flash *flash) |
| 273 | { |
| 274 | spi_free_slave(flash->spi); |
| 275 | free(flash); |
| 276 | } |