]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/spi/sf_probe.c
sf: Separate the flash params table
[karo-tx-uboot.git] / drivers / mtd / spi / sf_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  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 #include <common.h>
12 #include <fdtdec.h>
13 #include <malloc.h>
14 #include <spi.h>
15 #include <spi_flash.h>
16 #include <asm/io.h>
17
18 #include "sf_internal.h"
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 /* Read commands array */
23 static u8 spi_read_cmds_array[] = {
24         CMD_READ_ARRAY_SLOW,
25         CMD_READ_DUAL_OUTPUT_FAST,
26         CMD_READ_DUAL_IO_FAST,
27         CMD_READ_QUAD_OUTPUT_FAST,
28 };
29
30 static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
31 {
32         switch (idcode0) {
33 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
34         case SPI_FLASH_CFI_MFR_SPANSION:
35         case SPI_FLASH_CFI_MFR_WINBOND:
36                 return spi_flash_set_qeb_winspan(flash);
37 #endif
38 #ifdef CONFIG_SPI_FLASH_STMICRO
39         case SPI_FLASH_CFI_MFR_STMICRO:
40                 debug("SF: QEB is volatile for %02x flash\n", idcode0);
41                 return 0;
42 #endif
43         default:
44                 printf("SF: Need set QEB func for %02x flash\n", idcode0);
45                 return -1;
46         }
47 }
48
49 static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
50                 u8 *idcode)
51 {
52         const struct spi_flash_params *params;
53         struct spi_flash *flash;
54         u8 cmd;
55         u16 jedec = idcode[1] << 8 | idcode[2];
56         u16 ext_jedec = idcode[3] << 8 | idcode[4];
57
58         params = spi_flash_params_table;
59         for (; params->name != NULL; params++) {
60                 if ((params->jedec >> 16) == idcode[0]) {
61                         if ((params->jedec & 0xFFFF) == jedec) {
62                                 if (params->ext_jedec == 0)
63                                         break;
64                                 else if (params->ext_jedec == ext_jedec)
65                                         break;
66                         }
67                 }
68         }
69
70         if (!params->name) {
71                 printf("SF: Unsupported flash IDs: ");
72                 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
73                        idcode[0], jedec, ext_jedec);
74                 return NULL;
75         }
76
77         flash = malloc(sizeof(*flash));
78         if (!flash) {
79                 debug("SF: Failed to allocate spi_flash\n");
80                 return NULL;
81         }
82         memset(flash, '\0', sizeof(*flash));
83
84         /* Assign spi data */
85         flash->spi = spi;
86         flash->name = params->name;
87         flash->memory_map = spi->memory_map;
88
89         /* Assign spi_flash ops */
90         flash->write = spi_flash_cmd_write_ops;
91 #ifdef CONFIG_SPI_FLASH_SST
92         if (params->flags & SST_WP)
93                 flash->write = sst_write_wp;
94 #endif
95         flash->erase = spi_flash_cmd_erase_ops;
96         flash->read = spi_flash_cmd_read_ops;
97
98         /* Compute the flash size */
99         flash->page_size = (ext_jedec == 0x4d00) ? 512 : 256;
100         flash->sector_size = params->sector_size;
101         flash->size = flash->sector_size * params->nr_sectors;
102
103         /* Compute erase sector and command */
104         if (params->flags & SECT_4K) {
105                 flash->erase_cmd = CMD_ERASE_4K;
106                 flash->erase_size = 4096;
107         } else if (params->flags & SECT_32K) {
108                 flash->erase_cmd = CMD_ERASE_32K;
109                 flash->erase_size = 32768;
110         } else {
111                 flash->erase_cmd = CMD_ERASE_64K;
112                 flash->erase_size = flash->sector_size;
113         }
114
115         /* Look for the fastest read cmd */
116         cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
117         if (cmd) {
118                 cmd = spi_read_cmds_array[cmd - 1];
119                 flash->read_cmd = cmd;
120         } else {
121                 /* Go for for default supported read cmd */
122                 flash->read_cmd = CMD_READ_ARRAY_FAST;
123         }
124
125         /* Not require to look for fastest only two write cmds yet */
126         if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
127                 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
128         else
129                 /* Go for default supported write cmd */
130                 flash->write_cmd = CMD_PAGE_PROGRAM;
131
132         /* Set the quad enable bit - only for quad commands */
133         if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
134             (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
135                 if (spi_flash_set_qeb(flash, idcode[0])) {
136                         debug("SF: Fail to set QEB for %02x\n", idcode[0]);
137                         return NULL;
138                 }
139         }
140
141         /* Poll cmd seclection */
142         flash->poll_cmd = CMD_READ_STATUS;
143 #ifdef CONFIG_SPI_FLASH_STMICRO
144         if (params->flags & E_FSR)
145                 flash->poll_cmd = CMD_FLAG_STATUS;
146 #endif
147
148         /* Configure the BAR - discover bank cmds and read current bank */
149 #ifdef CONFIG_SPI_FLASH_BAR
150         u8 curr_bank = 0;
151         if (flash->size > SPI_FLASH_16MB_BOUN) {
152                 flash->bank_read_cmd = (idcode[0] == 0x01) ?
153                                         CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
154                 flash->bank_write_cmd = (idcode[0] == 0x01) ?
155                                         CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
156
157                 if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
158                                           &curr_bank, 1)) {
159                         debug("SF: fail to read bank addr register\n");
160                         return NULL;
161                 }
162                 flash->bank_curr = curr_bank;
163         } else {
164                 flash->bank_curr = curr_bank;
165         }
166 #endif
167
168         /* Flash powers up read-only, so clear BP# bits */
169 #if defined(CONFIG_SPI_FLASH_ATMEL) || \
170         defined(CONFIG_SPI_FLASH_MACRONIX) || \
171         defined(CONFIG_SPI_FLASH_SST)
172                 spi_flash_cmd_write_status(flash, 0);
173 #endif
174
175         return flash;
176 }
177
178 #ifdef CONFIG_OF_CONTROL
179 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
180 {
181         fdt_addr_t addr;
182         fdt_size_t size;
183         int node;
184
185         /* If there is no node, do nothing */
186         node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
187         if (node < 0)
188                 return 0;
189
190         addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
191         if (addr == FDT_ADDR_T_NONE) {
192                 debug("%s: Cannot decode address\n", __func__);
193                 return 0;
194         }
195
196         if (flash->size != size) {
197                 debug("%s: Memory map must cover entire device\n", __func__);
198                 return -1;
199         }
200         flash->memory_map = map_sysmem(addr, size);
201
202         return 0;
203 }
204 #endif /* CONFIG_OF_CONTROL */
205
206 static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
207 {
208         struct spi_flash *flash = NULL;
209         u8 idcode[5];
210         int ret;
211
212         /* Setup spi_slave */
213         if (!spi) {
214                 printf("SF: Failed to set up slave\n");
215                 return NULL;
216         }
217
218         /* Claim spi bus */
219         ret = spi_claim_bus(spi);
220         if (ret) {
221                 debug("SF: Failed to claim SPI bus: %d\n", ret);
222                 goto err_claim_bus;
223         }
224
225         /* Read the ID codes */
226         ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
227         if (ret) {
228                 printf("SF: Failed to get idcodes\n");
229                 goto err_read_id;
230         }
231
232 #ifdef DEBUG
233         printf("SF: Got idcodes\n");
234         print_buffer(0, idcode, 1, sizeof(idcode), 0);
235 #endif
236
237         /* Validate params from spi_flash_params table */
238         flash = spi_flash_validate_params(spi, idcode);
239         if (!flash)
240                 goto err_read_id;
241
242 #ifdef CONFIG_OF_CONTROL
243         if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
244                 debug("SF: FDT decode error\n");
245                 goto err_read_id;
246         }
247 #endif
248 #ifndef CONFIG_SPL_BUILD
249         printf("SF: Detected %s with page size ", flash->name);
250         print_size(flash->page_size, ", erase size ");
251         print_size(flash->erase_size, ", total ");
252         print_size(flash->size, "");
253         if (flash->memory_map)
254                 printf(", mapped at %p", flash->memory_map);
255         puts("\n");
256 #endif
257 #ifndef CONFIG_SPI_FLASH_BAR
258         if (flash->size > SPI_FLASH_16MB_BOUN) {
259                 puts("SF: Warning - Only lower 16MiB accessible,");
260                 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
261         }
262 #endif
263
264         /* Release spi bus */
265         spi_release_bus(spi);
266
267         return flash;
268
269 err_read_id:
270         spi_release_bus(spi);
271 err_claim_bus:
272         spi_free_slave(spi);
273         return NULL;
274 }
275
276 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
277                 unsigned int max_hz, unsigned int spi_mode)
278 {
279         struct spi_slave *spi;
280
281         spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
282         return spi_flash_probe_slave(spi);
283 }
284
285 #ifdef CONFIG_OF_SPI_FLASH
286 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
287                                       int spi_node)
288 {
289         struct spi_slave *spi;
290
291         spi = spi_setup_slave_fdt(blob, slave_node, spi_node);
292         return spi_flash_probe_slave(spi);
293 }
294 #endif
295
296 void spi_flash_free(struct spi_flash *flash)
297 {
298         spi_free_slave(flash->spi);
299         free(flash);
300 }