blob: b9e14c5a2bb1ac45a2ef3e9b0d4147cbff2a8302 [file] [log] [blame]
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +05301/*
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 *
Jagannadha Sutradharudu Teki0c88a842013-10-10 22:32:55 +05308 * SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +05309 */
10
11#include <common.h>
12#include <fdtdec.h>
13#include <malloc.h>
14#include <spi.h>
15#include <spi_flash.h>
Mike Frysingerffdb20b2013-12-03 16:43:27 -070016#include <asm/io.h>
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053017
Jagannadha Sutradharudu Teki898e76c2013-09-26 16:00:15 +053018#include "sf_internal.h"
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053019
20DECLARE_GLOBAL_DATA_PTR;
21
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053022/* Read commands array */
23static u8 spi_read_cmds_array[] = {
24 CMD_READ_ARRAY_SLOW,
25 CMD_READ_DUAL_OUTPUT_FAST,
26 CMD_READ_DUAL_IO_FAST,
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +053027 CMD_READ_QUAD_OUTPUT_FAST,
Jagannadha Sutradharudu Tekic4ba0d82013-12-24 15:24:31 +053028 CMD_READ_QUAD_IO_FAST,
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +053029};
30
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053031#ifdef CONFIG_SPI_FLASH_MACRONIX
32static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
33{
34 u8 qeb_status;
35 int ret;
36
37 ret = spi_flash_cmd_read_status(flash, &qeb_status);
38 if (ret < 0)
39 return ret;
40
41 if (qeb_status & STATUS_QEB_MXIC) {
42 debug("SF: mxic: QEB is already set\n");
43 } else {
44 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
45 if (ret < 0)
46 return ret;
47 }
48
49 return ret;
50}
51#endif
52
53#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
54static int spi_flash_set_qeb_winspan(struct spi_flash *flash)
55{
56 u8 qeb_status;
57 int ret;
58
59 ret = spi_flash_cmd_read_config(flash, &qeb_status);
60 if (ret < 0)
61 return ret;
62
63 if (qeb_status & STATUS_QEB_WINSPAN) {
64 debug("SF: winspan: QEB is already set\n");
65 } else {
66 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
67 if (ret < 0)
68 return ret;
69 }
70
71 return ret;
72}
73#endif
74
Jagannadha Sutradharudu Tekid08a1ba2013-12-26 13:54:57 +053075static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
76{
77 switch (idcode0) {
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053078#ifdef CONFIG_SPI_FLASH_MACRONIX
79 case SPI_FLASH_CFI_MFR_MACRONIX:
80 return spi_flash_set_qeb_mxic(flash);
81#endif
Jagannadha Sutradharudu Tekid08a1ba2013-12-26 13:54:57 +053082#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
83 case SPI_FLASH_CFI_MFR_SPANSION:
84 case SPI_FLASH_CFI_MFR_WINBOND:
85 return spi_flash_set_qeb_winspan(flash);
86#endif
87#ifdef CONFIG_SPI_FLASH_STMICRO
88 case SPI_FLASH_CFI_MFR_STMICRO:
89 debug("SF: QEB is volatile for %02x flash\n", idcode0);
90 return 0;
91#endif
92 default:
93 printf("SF: Need set QEB func for %02x flash\n", idcode0);
94 return -1;
95 }
96}
97
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053098static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
99 u8 *idcode)
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530100{
101 const struct spi_flash_params *params;
102 struct spi_flash *flash;
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530103 u8 cmd;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530104 u16 jedec = idcode[1] << 8 | idcode[2];
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530105 u16 ext_jedec = idcode[3] << 8 | idcode[4];
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530106
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +0530107 params = spi_flash_params_table;
108 for (; params->name != NULL; params++) {
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530109 if ((params->jedec >> 16) == idcode[0]) {
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530110 if ((params->jedec & 0xFFFF) == jedec) {
111 if (params->ext_jedec == 0)
112 break;
113 else if (params->ext_jedec == ext_jedec)
114 break;
115 }
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530116 }
117 }
118
Jagannadha Sutradharudu Teki33adfb52013-12-23 23:34:42 +0530119 if (!params->name) {
Jagannadha Sutradharudu Teki74bec162013-09-28 16:49:37 +0530120 printf("SF: Unsupported flash IDs: ");
121 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
122 idcode[0], jedec, ext_jedec);
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530123 return NULL;
124 }
125
126 flash = malloc(sizeof(*flash));
127 if (!flash) {
128 debug("SF: Failed to allocate spi_flash\n");
129 return NULL;
130 }
131 memset(flash, '\0', sizeof(*flash));
132
Jagannadha Sutradharudu Teki469146c2013-10-10 22:14:09 +0530133 /* Assign spi data */
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530134 flash->spi = spi;
135 flash->name = params->name;
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530136 flash->memory_map = spi->memory_map;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530137 flash->dual_flash = flash->spi->option;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530138
139 /* Assign spi_flash ops */
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530140 flash->write = spi_flash_cmd_write_ops;
Jagannadha Sutradharudu Teki10ca45d2013-10-02 19:34:53 +0530141#ifdef CONFIG_SPI_FLASH_SST
142 if (params->flags & SST_WP)
143 flash->write = sst_write_wp;
144#endif
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530145 flash->erase = spi_flash_cmd_erase_ops;
146 flash->read = spi_flash_cmd_read_ops;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530147
148 /* Compute the flash size */
Jagannadha Sutradharudu Tekif0be6de2013-08-28 12:17:56 +0530149 flash->page_size = (ext_jedec == 0x4d00) ? 512 : 256;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530150 flash->sector_size = params->sector_size;
151 flash->size = flash->sector_size * params->nr_sectors;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530152 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
153 flash->size <<= 1;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530154
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530155 /* Compute erase sector and command */
156 if (params->flags & SECT_4K) {
157 flash->erase_cmd = CMD_ERASE_4K;
158 flash->erase_size = 4096;
159 } else if (params->flags & SECT_32K) {
160 flash->erase_cmd = CMD_ERASE_32K;
161 flash->erase_size = 32768;
162 } else {
163 flash->erase_cmd = CMD_ERASE_64K;
164 flash->erase_size = flash->sector_size;
165 }
166
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530167 /* Look for the fastest read cmd */
168 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
169 if (cmd) {
170 cmd = spi_read_cmds_array[cmd - 1];
171 flash->read_cmd = cmd;
172 } else {
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +0530173 /* Go for default supported read cmd */
Jagannadha Sutradharudu Teki4e09cc12014-01-11 15:10:28 +0530174 flash->read_cmd = CMD_READ_ARRAY_FAST;
175 }
176
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +0530177 /* Not require to look for fastest only two write cmds yet */
178 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
179 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
180 else
181 /* Go for default supported write cmd */
182 flash->write_cmd = CMD_PAGE_PROGRAM;
183
Jagannadha Sutradharudu Tekid08a1ba2013-12-26 13:54:57 +0530184 /* Set the quad enable bit - only for quad commands */
185 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
Jagannadha Sutradharudu Tekic4ba0d82013-12-24 15:24:31 +0530186 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
Jagannadha Sutradharudu Tekid08a1ba2013-12-26 13:54:57 +0530187 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
188 if (spi_flash_set_qeb(flash, idcode[0])) {
189 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
190 return NULL;
191 }
192 }
193
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530194 /* Read dummy_byte: dummy byte is determined based on the
195 * dummy cycles of a particular command.
196 * Fast commands - dummy_byte = dummy_cycles/8
197 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
198 * For I/O commands except cmd[0] everything goes on no.of lines
199 * based on particular command but incase of fast commands except
200 * data all go on single line irrespective of command.
201 */
202 switch (flash->read_cmd) {
203 case CMD_READ_QUAD_IO_FAST:
204 flash->dummy_byte = 2;
205 break;
206 case CMD_READ_ARRAY_SLOW:
207 flash->dummy_byte = 0;
208 break;
209 default:
210 flash->dummy_byte = 1;
211 }
212
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +0530213 /* Poll cmd selection */
Jagannadha Sutradharudu Teki0f623282013-10-02 19:37:43 +0530214 flash->poll_cmd = CMD_READ_STATUS;
215#ifdef CONFIG_SPI_FLASH_STMICRO
216 if (params->flags & E_FSR)
217 flash->poll_cmd = CMD_FLAG_STATUS;
218#endif
219
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +0530220 /* Configure the BAR - discover bank cmds and read current bank */
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530221#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530222 u8 curr_bank = 0;
223 if (flash->size > SPI_FLASH_16MB_BOUN) {
224 flash->bank_read_cmd = (idcode[0] == 0x01) ?
225 CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
226 flash->bank_write_cmd = (idcode[0] == 0x01) ?
227 CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
228
229 if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
230 &curr_bank, 1)) {
231 debug("SF: fail to read bank addr register\n");
232 return NULL;
233 }
234 flash->bank_curr = curr_bank;
235 } else {
236 flash->bank_curr = curr_bank;
237 }
238#endif
239
Jagannadha Sutradharudu Teki54024c12013-09-26 13:16:45 +0530240 /* Flash powers up read-only, so clear BP# bits */
241#if defined(CONFIG_SPI_FLASH_ATMEL) || \
242 defined(CONFIG_SPI_FLASH_MACRONIX) || \
243 defined(CONFIG_SPI_FLASH_SST)
244 spi_flash_cmd_write_status(flash, 0);
245#endif
246
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530247 return flash;
248}
249
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530250#ifdef CONFIG_OF_CONTROL
251int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
252{
253 fdt_addr_t addr;
254 fdt_size_t size;
255 int node;
256
257 /* If there is no node, do nothing */
258 node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
259 if (node < 0)
260 return 0;
261
262 addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
263 if (addr == FDT_ADDR_T_NONE) {
264 debug("%s: Cannot decode address\n", __func__);
265 return 0;
266 }
267
268 if (flash->size != size) {
269 debug("%s: Memory map must cover entire device\n", __func__);
270 return -1;
271 }
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700272 flash->memory_map = map_sysmem(addr, size);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530273
274 return 0;
275}
276#endif /* CONFIG_OF_CONTROL */
277
Simon Glass0efc0242013-12-03 16:43:24 -0700278static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530279{
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530280 struct spi_flash *flash = NULL;
Jagannadha Sutradharudu Teki32ebd1a2013-09-26 14:24:58 +0530281 u8 idcode[5];
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530282 int ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530283
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530284 /* Setup spi_slave */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530285 if (!spi) {
286 printf("SF: Failed to set up slave\n");
287 return NULL;
288 }
289
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530290 /* Claim spi bus */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530291 ret = spi_claim_bus(spi);
292 if (ret) {
293 debug("SF: Failed to claim SPI bus: %d\n", ret);
294 goto err_claim_bus;
295 }
296
297 /* Read the ID codes */
298 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530299 if (ret) {
300 printf("SF: Failed to get idcodes\n");
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530301 goto err_read_id;
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530302 }
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530303
304#ifdef DEBUG
305 printf("SF: Got idcodes\n");
306 print_buffer(0, idcode, 1, sizeof(idcode), 0);
307#endif
308
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530309 /* Validate params from spi_flash_params table */
310 flash = spi_flash_validate_params(spi, idcode);
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530311 if (!flash)
312 goto err_read_id;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530313
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530314#ifdef CONFIG_OF_CONTROL
315 if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
316 debug("SF: FDT decode error\n");
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530317 goto err_read_id;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530318 }
319#endif
320#ifndef CONFIG_SPL_BUILD
321 printf("SF: Detected %s with page size ", flash->name);
Jagannadha Sutradharudu Teki3ea708f2013-09-15 23:06:12 +0530322 print_size(flash->page_size, ", erase size ");
323 print_size(flash->erase_size, ", total ");
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530324 print_size(flash->size, "");
325 if (flash->memory_map)
326 printf(", mapped at %p", flash->memory_map);
327 puts("\n");
328#endif
329#ifndef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530330 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
331 (flash->size > SPI_FLASH_16MB_BOUN)) ||
332 ((flash->dual_flash > SF_SINGLE_FLASH) &&
333 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530334 puts("SF: Warning - Only lower 16MiB accessible,");
335 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
336 }
337#endif
338
Jagannadha Sutradharudu Teki4d4ec992013-09-24 16:01:23 +0530339 /* Release spi bus */
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530340 spi_release_bus(spi);
341
342 return flash;
343
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530344err_read_id:
345 spi_release_bus(spi);
346err_claim_bus:
347 spi_free_slave(spi);
348 return NULL;
349}
350
Simon Glass0efc0242013-12-03 16:43:24 -0700351struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
352 unsigned int max_hz, unsigned int spi_mode)
353{
354 struct spi_slave *spi;
355
356 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
357 return spi_flash_probe_slave(spi);
358}
359
360#ifdef CONFIG_OF_SPI_FLASH
361struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
362 int spi_node)
363{
364 struct spi_slave *spi;
365
366 spi = spi_setup_slave_fdt(blob, slave_node, spi_node);
367 return spi_flash_probe_slave(spi);
368}
369#endif
370
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530371void spi_flash_free(struct spi_flash *flash)
372{
373 spi_free_slave(flash->spi);
374 free(flash);
375}