4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
8 * Licensed under the GPL-2 or later.
15 #include <spi_flash.h>
17 #include "spi_flash_internal.h"
19 DECLARE_GLOBAL_DATA_PTR;
21 #ifdef CONFIG_SPI_FLASH_BAR
22 int spi_flash_bank_config(struct spi_flash *flash, u8 idcode0)
27 /* discover bank cmds */
29 case SPI_FLASH_SPANSION_IDCODE0:
30 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
31 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
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;
39 printf("SF: Unsupported bank commands %02x\n", idcode0);
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");
50 flash->bank_curr = curr_bank;
52 flash->bank_curr = curr_bank;
59 #ifdef CONFIG_OF_CONTROL
60 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
66 /* If there is no node, do nothing */
67 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
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__);
77 if (flash->size != size) {
78 debug("%s: Memory map must cover entire device\n", __func__);
81 flash->memory_map = (void *)addr;
85 #endif /* CONFIG_OF_CONTROL */
88 * The following table holds all device probe functions
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
94 * Non JEDEC devices should be ordered in the table such that
95 * the probe functions with best detection algorithms come first.
97 * Several matching entries are permitted, they will be tried
98 * in sequence until a probe function returns non NULL.
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.
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).
109 #define IDCODE_CONT_LEN 0
110 #define IDCODE_PART_LEN 5
111 static const struct {
114 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
116 /* Keep it sorted by define name */
117 #ifdef CONFIG_SPI_FLASH_ATMEL
118 { 0, 0x1f, spi_flash_probe_atmel, },
120 #ifdef CONFIG_SPI_FLASH_EON
121 { 0, 0x1c, spi_flash_probe_eon, },
123 #ifdef CONFIG_SPI_FLASH_GIGADEVICE
124 { 0, 0xc8, spi_flash_probe_gigadevice, },
126 #ifdef CONFIG_SPI_FLASH_MACRONIX
127 { 0, 0xc2, spi_flash_probe_macronix, },
129 #ifdef CONFIG_SPI_FLASH_SPANSION
130 { 0, 0x01, spi_flash_probe_spansion, },
132 #ifdef CONFIG_SPI_FLASH_SST
133 { 0, 0xbf, spi_flash_probe_sst, },
135 #ifdef CONFIG_SPI_FLASH_STMICRO
136 { 0, 0x20, spi_flash_probe_stmicro, },
138 #ifdef CONFIG_SPI_FLASH_WINBOND
139 { 0, 0xef, spi_flash_probe_winbond, },
141 #ifdef CONFIG_SPI_FRAM_RAMTRON
142 { 6, 0xc2, spi_fram_probe_ramtron, },
143 # undef IDCODE_CONT_LEN
144 # define IDCODE_CONT_LEN 6
146 /* Keep it sorted by best detection */
147 #ifdef CONFIG_SPI_FLASH_STMICRO
148 { 0, 0xff, spi_flash_probe_stmicro, },
150 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
151 { 0, 0xff, spi_fram_probe_ramtron, },
154 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
156 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
157 unsigned int max_hz, unsigned int spi_mode)
159 struct spi_slave *spi;
160 struct spi_flash *flash = NULL;
162 u8 idcode[IDCODE_LEN], *idp;
164 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
166 printf("SF: Failed to set up slave\n");
170 ret = spi_claim_bus(spi);
172 debug("SF: Failed to claim SPI bus: %d\n", ret);
176 /* Read the ID codes */
177 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
182 printf("SF: Got idcodes\n");
183 print_buffer(0, idcode, 1, sizeof(idcode), 0);
186 /* count the number of continuation bytes */
187 for (shift = 0, idp = idcode;
188 shift < IDCODE_CONT_LEN && *idp == 0x7f;
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);
202 printf("SF: Unsupported manufacturer %02x\n", *idp);
203 goto err_manufacturer_probe;
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);
210 goto err_manufacturer_probe;
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;
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);
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");
234 spi_release_bus(spi);
238 err_manufacturer_probe:
240 spi_release_bus(spi);
246 void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi,
249 struct spi_flash *flash;
254 debug("SF: Failed to allocate memory\n");
257 memset(ptr, '\0', size);
258 flash = (struct spi_flash *)(ptr + offset);
260 /* Set up some basic fields - caller will sort out sizes */
263 flash->poll_cmd = CMD_READ_STATUS;
265 flash->read = spi_flash_cmd_read_fast;
266 flash->write = spi_flash_cmd_write_multi;
267 flash->erase = spi_flash_cmd_erase;
272 void spi_flash_free(struct spi_flash *flash)
274 spi_free_slave(flash->spi);