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
56 #define SPANSION_SR_WIP (1 << 0) /* Write-in-Progress */
58 struct spansion_spi_flash_params {
67 struct spansion_spi_flash {
68 struct spi_flash flash;
69 const struct spansion_spi_flash_params *params;
72 static inline struct spansion_spi_flash *to_spansion_spi_flash(struct spi_flash
75 return container_of(flash, struct spansion_spi_flash, flash);
78 static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
80 .idcode1 = SPSN_ID_S25FL008A,
83 .pages_per_sector = 256,
88 .idcode1 = SPSN_ID_S25FL016A,
91 .pages_per_sector = 256,
96 .idcode1 = SPSN_ID_S25FL032A,
99 .pages_per_sector = 256,
104 .idcode1 = SPSN_ID_S25FL064A,
107 .pages_per_sector = 256,
112 .idcode1 = SPSN_ID_S25FL128P,
113 .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
115 .pages_per_sector = 256,
117 .name = "S25FL128P_64K",
120 .idcode1 = SPSN_ID_S25FL128P,
121 .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
123 .pages_per_sector = 1024,
125 .name = "S25FL128P_256K",
129 static int spansion_wait_ready(struct spi_flash *flash, unsigned long timeout)
131 struct spi_slave *spi = flash->spi;
132 unsigned long timebase;
136 timebase = get_timer(0);
138 ret = spi_flash_cmd(spi, CMD_S25FLXX_RDSR, &status, sizeof(status));
142 if ((status & SPANSION_SR_WIP) == 0)
145 } while (get_timer(timebase) < timeout);
148 if ((status & SPANSION_SR_WIP) == 0)
155 static int spansion_read_fast(struct spi_flash *flash,
156 u32 offset, size_t len, void *buf)
158 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
159 unsigned long page_addr;
160 unsigned long page_size;
163 page_size = spsn->params->page_size;
164 page_addr = offset / page_size;
166 cmd[0] = CMD_READ_ARRAY_FAST;
167 cmd[1] = page_addr >> 8;
169 cmd[3] = offset % page_size;
173 ("READ: 0x%x => cmd = { 0x%02x 0x%02x%02x%02x%02x } len = 0x%x\n",
174 offset, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], len);
176 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
179 static int spansion_write(struct spi_flash *flash,
180 u32 offset, size_t len, const void *buf)
182 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
183 unsigned long page_addr;
184 unsigned long byte_addr;
185 unsigned long page_size;
191 page_size = spsn->params->page_size;
192 page_addr = offset / page_size;
193 byte_addr = offset % page_size;
195 ret = spi_claim_bus(flash->spi);
197 debug("SF: Unable to claim SPI bus\n");
202 for (actual = 0; actual < len; actual += chunk_len) {
203 chunk_len = min(len - actual, page_size - byte_addr);
205 cmd[0] = CMD_S25FLXX_PP;
206 cmd[1] = page_addr >> 8;
211 ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
212 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
214 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
216 debug("SF: Enabling Write failed\n");
220 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
221 buf + actual, chunk_len);
223 debug("SF: SPANSION Page Program failed\n");
227 ret = spansion_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
229 debug("SF: SPANSION page programming timed out\n");
237 debug("SF: SPANSION: Successfully programmed %u bytes @ 0x%x\n",
240 spi_release_bus(flash->spi);
244 int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
246 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
247 unsigned long sector_size;
253 * This function currently uses sector erase only.
254 * probably speed things up by using bulk erase
258 sector_size = spsn->params->page_size * spsn->params->pages_per_sector;
260 if (offset % sector_size || len % sector_size) {
261 debug("SF: Erase offset/length not multiple of sector size\n");
266 cmd[0] = CMD_S25FLXX_SE;
270 ret = spi_claim_bus(flash->spi);
272 debug("SF: Unable to claim SPI bus\n");
277 for (actual = 0; actual < len; actual++) {
278 cmd[1] = (offset / sector_size) + actual;
280 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
282 debug("SF: Enabling Write failed\n");
286 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
288 debug("SF: SPANSION page erase failed\n");
292 /* Up to 2 seconds */
293 ret = spansion_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
295 debug("SF: SPANSION page erase timed out\n");
300 debug("SF: SPANSION: Successfully erased %u bytes @ 0x%x\n",
301 len * sector_size, offset);
303 spi_release_bus(flash->spi);
307 struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
309 const struct spansion_spi_flash_params *params;
310 struct spansion_spi_flash *spsn;
312 unsigned short jedec, ext_jedec;
314 jedec = idcode[1] << 8 | idcode[2];
315 ext_jedec = idcode[3] << 8 | idcode[4];
317 for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
318 params = &spansion_spi_flash_table[i];
319 if (params->idcode1 == jedec) {
320 if (params->idcode2 == ext_jedec)
325 if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
326 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
330 spsn = malloc(sizeof(struct spansion_spi_flash));
332 debug("SF: Failed to allocate memory\n");
336 spsn->params = params;
337 spsn->flash.spi = spi;
338 spsn->flash.name = params->name;
340 spsn->flash.write = spansion_write;
341 spsn->flash.erase = spansion_erase;
342 spsn->flash.read = spansion_read_fast;
343 spsn->flash.size = params->page_size * params->pages_per_sector
344 * params->nr_sectors;
346 debug("SF: Detected %s with page size %u, total %u bytes\n",
347 params->name, params->page_size, spsn->flash.size);