2 * Copyright (C) 2009 Freescale Semiconductor, Inc.
4 * Author: Mingkai Hu (Mingkai.hu@freescale.com)
5 * Based on stmicro.c by Wolfgang Denk (wd@denx.de),
6 * TsiChung Liew (Tsi-Chung.Liew@freescale.com),
7 * and Jason McMullan (mcmullan@netapp.com)
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <spi_flash.h>
32 #include "spi_flash_internal.h"
34 /* S25FLxx-specific commands */
35 #define CMD_S25FLXX_READ 0x03 /* Read Data Bytes */
36 #define CMD_S25FLXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
37 #define CMD_S25FLXX_READID 0x90 /* Read Manufacture ID and Device ID */
38 #define CMD_S25FLXX_WREN 0x06 /* Write Enable */
39 #define CMD_S25FLXX_WRDI 0x04 /* Write Disable */
40 #define CMD_S25FLXX_RDSR 0x05 /* Read Status Register */
41 #define CMD_S25FLXX_WRSR 0x01 /* Write Status Register */
42 #define CMD_S25FLXX_PP 0x02 /* Page Program */
43 #define CMD_S25FLXX_SE 0xd8 /* Sector Erase */
44 #define CMD_S25FLXX_BE 0xc7 /* Bulk Erase */
45 #define CMD_S25FLXX_DP 0xb9 /* Deep Power-down */
46 #define CMD_S25FLXX_RES 0xab /* Release from DP, and Read Signature */
48 #define SPSN_ID_S25FL008A 0x0213
49 #define SPSN_ID_S25FL016A 0x0214
50 #define SPSN_ID_S25FL032A 0x0215
51 #define SPSN_ID_S25FL064A 0x0216
52 #define SPSN_ID_S25FL128P 0x2018
53 #define SPSN_EXT_ID_S25FL128P_256KB 0x0300
54 #define SPSN_EXT_ID_S25FL128P_64KB 0x0301
55 #define SPSN_EXT_ID_S25FL032P 0x4d00
57 struct spansion_spi_flash_params {
66 struct spansion_spi_flash {
67 struct spi_flash flash;
68 const struct spansion_spi_flash_params *params;
71 static inline struct spansion_spi_flash *to_spansion_spi_flash(struct spi_flash
74 return container_of(flash, struct spansion_spi_flash, flash);
77 static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
79 .idcode1 = SPSN_ID_S25FL008A,
82 .pages_per_sector = 256,
87 .idcode1 = SPSN_ID_S25FL016A,
90 .pages_per_sector = 256,
95 .idcode1 = SPSN_ID_S25FL032A,
98 .pages_per_sector = 256,
103 .idcode1 = SPSN_ID_S25FL064A,
106 .pages_per_sector = 256,
111 .idcode1 = SPSN_ID_S25FL128P,
112 .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
114 .pages_per_sector = 256,
116 .name = "S25FL128P_64K",
119 .idcode1 = SPSN_ID_S25FL128P,
120 .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
122 .pages_per_sector = 1024,
124 .name = "S25FL128P_256K",
127 .idcode1 = SPSN_ID_S25FL032A,
128 .idcode2 = SPSN_EXT_ID_S25FL032P,
130 .pages_per_sector = 256,
136 static int spansion_write(struct spi_flash *flash,
137 u32 offset, size_t len, const void *buf)
139 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
140 unsigned long page_addr;
141 unsigned long byte_addr;
142 unsigned long page_size;
148 page_size = spsn->params->page_size;
149 page_addr = offset / page_size;
150 byte_addr = offset % page_size;
152 ret = spi_claim_bus(flash->spi);
154 debug("SF: Unable to claim SPI bus\n");
159 for (actual = 0; actual < len; actual += chunk_len) {
160 chunk_len = min(len - actual, page_size - byte_addr);
162 cmd[0] = CMD_S25FLXX_PP;
163 cmd[1] = page_addr >> 8;
168 ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
169 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
171 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
173 debug("SF: Enabling Write failed\n");
177 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
178 buf + actual, chunk_len);
180 debug("SF: SPANSION Page Program failed\n");
184 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
192 debug("SF: SPANSION: Successfully programmed %u bytes @ 0x%x\n",
195 spi_release_bus(flash->spi);
199 static int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
201 return spi_flash_cmd_erase(flash, CMD_S25FLXX_SE, offset, len);
204 struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
206 const struct spansion_spi_flash_params *params;
207 struct spansion_spi_flash *spsn;
209 unsigned short jedec, ext_jedec;
211 jedec = idcode[1] << 8 | idcode[2];
212 ext_jedec = idcode[3] << 8 | idcode[4];
214 for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
215 params = &spansion_spi_flash_table[i];
216 if (params->idcode1 == jedec) {
217 if (params->idcode2 == ext_jedec)
222 if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
223 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
227 spsn = malloc(sizeof(struct spansion_spi_flash));
229 debug("SF: Failed to allocate memory\n");
233 spsn->params = params;
234 spsn->flash.spi = spi;
235 spsn->flash.name = params->name;
237 spsn->flash.write = spansion_write;
238 spsn->flash.erase = spansion_erase;
239 spsn->flash.read = spi_flash_cmd_read_fast;
240 spsn->flash.sector_size = params->page_size * params->pages_per_sector;
241 spsn->flash.size = spsn->flash.sector_size * params->nr_sectors;