]> git.sur5r.net Git - u-boot/blob - drivers/mtd/spi/spi_flash_probe.c
sf: probe: Add support for EN25Q* flash parts
[u-boot] / drivers / mtd / spi / spi_flash_probe.c
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 /*
22  * struct spi_flash_params - SPI/QSPI flash device params structure
23  *
24  * @name:               Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO])
25  * @jedec:              Device jedec ID (0x[1byte_manuf_id][2byte_dev_id])
26  * @ext_jedec:          Device ext_jedec ID
27  * @sector_size:        Sector size of this device
28  * @nr_sectors: No.of sectors on this device
29  */
30 struct spi_flash_params {
31         const char *name;
32         u32 jedec;
33         u16 ext_jedec;
34         u32 sector_size;
35         u32 nr_sectors;
36 };
37
38 static const struct spi_flash_params spi_flash_params_table[] = {
39 #ifdef CONFIG_SPI_FLASH_EON             /* EON */
40         {"EN25Q32B",            0x1c3016, 0x0,     64 * 1024,     64},
41         {"EN25Q128B",           0x1c3018, 0x0,     64 * 1024,    256},
42 #endif
43 #ifdef CONFIG_SPI_FLASH_STMICRO         /* STMICRO */
44         {"M25P10",              0x202011, 0x0,     32 * 1024,      4},
45         {"M25P20",              0x202012, 0x0,     64 * 1024,      4},
46         {"M25P40",              0x202013, 0x0,     64 * 1024,      8},
47         {"M25P80",              0x202014, 0x0,     64 * 1024,     16},
48         {"M25P16",              0x202015, 0x0,     64 * 1024,     32},
49         {"M25P32",              0x202016, 0x0,     64 * 1024,     64},
50         {"M25P64",              0x202017, 0x0,     64 * 1024,    128},
51         {"M25P128",             0x202018, 0x0,    256 * 1024,     64},
52         {"N25Q32",              0x20ba16, 0x0,     64 * 1024,     64},
53         {"N25Q32A",             0x20bb16, 0x0,     64 * 1024,     64},
54         {"N25Q64",              0x20ba17, 0x0,     64 * 1024,    128},
55         {"N25Q64A",             0x20bb17, 0x0,     64 * 1024,    128},
56         {"N25Q128",             0x20ba18, 0x0,     64 * 1024,    256},
57         {"N25Q128A",            0x20bb18, 0x0,     64 * 1024,    256},
58         {"N25Q256",             0x20ba19, 0x0,     64 * 1024,    512},
59         {"N25Q256A",            0x20bb19, 0x0,     64 * 1024,    512},
60         {"N25Q512",             0x20ba20, 0x0,     64 * 1024,   1024},
61         {"N25Q512A",            0x20bb20, 0x0,     64 * 1024,   1024},
62         {"N25Q1024",            0x20ba21, 0x0,     64 * 1024,   2048},
63         {"N25Q1024A",           0x20bb21, 0x0,     64 * 1024,   2048},
64 #endif
65         /*
66          * TODO:
67          * ATMEL
68          * GIGADEVICE
69          * MACRONIX
70          * RAMTRON
71          * SPANSION
72          * SST
73          * WINBOND
74          */
75 };
76
77 struct spi_flash *spi_flash_validate_ids(struct spi_slave *spi, u8 *idcode)
78 {
79         const struct spi_flash_params *params;
80         struct spi_flash *flash;
81         int i;
82         u16 jedec = idcode[1] << 8 | idcode[2];
83
84         /* Get the flash id (jedec = manuf_id + dev_id) */
85         for (i = 0; i < ARRAY_SIZE(spi_flash_params_table); i++) {
86                 params = &spi_flash_params_table[i];
87                 if ((params->jedec >> 16) == idcode[0]) {
88                         if ((params->jedec & 0xFFFF) == jedec)
89                                 break;
90                 }
91         }
92
93         if (i == ARRAY_SIZE(spi_flash_params_table)) {
94                 printf("SF: Unsupported flash ID: manuf %02x, jedec %04x\n",
95                        idcode[0], jedec);
96                 return NULL;
97         }
98
99         flash = malloc(sizeof(*flash));
100         if (!flash) {
101                 debug("SF: Failed to allocate spi_flash\n");
102                 return NULL;
103         }
104         memset(flash, '\0', sizeof(*flash));
105
106         flash->spi = spi;
107         flash->name = params->name;
108         flash->poll_cmd = CMD_READ_STATUS;
109
110         /* Assign spi_flash ops */
111         flash->write = spi_flash_cmd_write_multi;
112         flash->erase = spi_flash_cmd_erase;
113         flash->read = spi_flash_cmd_read_fast;
114
115         /* Compute the flash size */
116         flash->page_size = 256;
117         flash->sector_size = params->sector_size;
118         flash->size = flash->sector_size * params->nr_sectors;
119
120         return flash;
121 }
122
123 #ifdef CONFIG_SPI_FLASH_BAR
124 int spi_flash_bank_config(struct spi_flash *flash, u8 idcode0)
125 {
126         u8 cmd;
127         u8 curr_bank = 0;
128
129         /* discover bank cmds */
130         switch (idcode0) {
131         case SPI_FLASH_SPANSION_IDCODE0:
132                 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
133                 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
134                 break;
135         case SPI_FLASH_STMICRO_IDCODE0:
136         case SPI_FLASH_WINBOND_IDCODE0:
137                 flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
138                 flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
139                 break;
140         default:
141                 printf("SF: Unsupported bank commands %02x\n", idcode0);
142                 return -1;
143         }
144
145         /* read the bank reg - on which bank the flash is in currently */
146         cmd = flash->bank_read_cmd;
147         if (flash->size > SPI_FLASH_16MB_BOUN) {
148                 if (spi_flash_read_common(flash, &cmd, 1, &curr_bank, 1)) {
149                         debug("SF: fail to read bank addr register\n");
150                         return -1;
151                 }
152                 flash->bank_curr = curr_bank;
153         } else {
154                 flash->bank_curr = curr_bank;
155         }
156
157         return 0;
158 }
159 #endif
160
161 #ifdef CONFIG_OF_CONTROL
162 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
163 {
164         fdt_addr_t addr;
165         fdt_size_t size;
166         int node;
167
168         /* If there is no node, do nothing */
169         node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
170         if (node < 0)
171                 return 0;
172
173         addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
174         if (addr == FDT_ADDR_T_NONE) {
175                 debug("%s: Cannot decode address\n", __func__);
176                 return 0;
177         }
178
179         if (flash->size != size) {
180                 debug("%s: Memory map must cover entire device\n", __func__);
181                 return -1;
182         }
183         flash->memory_map = (void *)addr;
184
185         return 0;
186 }
187 #endif /* CONFIG_OF_CONTROL */
188
189 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
190                 unsigned int max_hz, unsigned int spi_mode)
191 {
192         struct spi_slave *spi;
193         struct spi_flash *flash = NULL;
194         u8 idcode[5], *idp;
195         int ret;
196
197         /* Setup spi_slave */
198         spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
199         if (!spi) {
200                 printf("SF: Failed to set up slave\n");
201                 return NULL;
202         }
203
204         /* Claim spi bus */
205         ret = spi_claim_bus(spi);
206         if (ret) {
207                 debug("SF: Failed to claim SPI bus: %d\n", ret);
208                 goto err_claim_bus;
209         }
210
211         /* Read the ID codes */
212         ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
213         if (ret) {
214                 printf("SF: Failed to get idcodes\n");
215                 goto err_read_id;
216         }
217
218 #ifdef DEBUG
219         printf("SF: Got idcodes\n");
220         print_buffer(0, idcode, 1, sizeof(idcode), 0);
221 #endif
222
223         /* Validate ID's from flash dev table */
224         idp = idcode;
225         flash = spi_flash_validate_ids(spi, idp);
226         if (!flash)
227                 goto err_read_id;
228
229 #ifdef CONFIG_SPI_FLASH_BAR
230         /* Configure the BAR - discover bank cmds and read current bank  */
231         ret = spi_flash_bank_config(flash, *idp);
232         if (ret < 0)
233                 goto err_read_id;
234 #endif
235
236 #ifdef CONFIG_OF_CONTROL
237         if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
238                 debug("SF: FDT decode error\n");
239                 goto err_read_id;
240         }
241 #endif
242 #ifndef CONFIG_SPL_BUILD
243         printf("SF: Detected %s with page size ", flash->name);
244         print_size(flash->sector_size, ", total ");
245         print_size(flash->size, "");
246         if (flash->memory_map)
247                 printf(", mapped at %p", flash->memory_map);
248         puts("\n");
249 #endif
250 #ifndef CONFIG_SPI_FLASH_BAR
251         if (flash->size > SPI_FLASH_16MB_BOUN) {
252                 puts("SF: Warning - Only lower 16MiB accessible,");
253                 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
254         }
255 #endif
256
257         /* Release spi bus */
258         spi_release_bus(spi);
259
260         return flash;
261
262 err_read_id:
263         spi_release_bus(spi);
264 err_claim_bus:
265         spi_free_slave(spi);
266         return NULL;
267 }
268
269 void spi_flash_free(struct spi_flash *flash)
270 {
271         spi_free_slave(flash->spi);
272         free(flash);
273 }