2 * Driver for SST serial flashes
4 * (C) Copyright 2000-2002
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Copyright 2008, Network Appliance Inc.
7 * Jason McMullan <mcmullan@netapp.com>
8 * Copyright (C) 2004-2007 Freescale Semiconductor, Inc.
9 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
10 * Copyright (c) 2008-2009 Analog Devices Inc.
12 * Licensed under the GPL-2 or later.
17 #include <spi_flash.h>
19 #include "spi_flash_internal.h"
21 #define CMD_SST_BP 0x02 /* Byte Program */
22 #define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */
23 #define CMD_SST_SE 0x20 /* Sector Erase */
25 #define SST_SR_WIP (1 << 0) /* Write-in-Progress */
26 #define SST_SR_WEL (1 << 1) /* Write enable */
27 #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */
28 #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */
29 #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */
30 #define SST_SR_AAI (1 << 6) /* Addressing mode */
31 #define SST_SR_BPL (1 << 7) /* BP bits lock */
33 #define SST_FEAT_WP (1 << 0) /* Supports AAI word program */
34 #define SST_FEAT_MBP (1 << 1) /* Supports multibyte program */
36 struct sst_spi_flash_params {
43 struct sst_spi_flash {
44 struct spi_flash flash;
45 const struct sst_spi_flash_params *params;
48 static inline struct sst_spi_flash *to_sst_spi_flash(struct spi_flash *flash)
50 return container_of(flash, struct sst_spi_flash, flash);
53 #define SST_SECTOR_SIZE (4 * 1024)
54 #define SST_PAGE_SIZE 256
55 static const struct sst_spi_flash_params sst_spi_flash_table[] = {
60 .name = "SST25VF040B",
65 .name = "SST25VF080B",
70 .name = "SST25VF016B",
75 .name = "SST25VF032B",
78 .flags = SST_FEAT_MBP,
80 .name = "SST25VF064C",
100 .name = "SST25WF040",
105 sst_enable_writing(struct spi_flash *flash)
107 int ret = spi_flash_cmd_write_enable(flash);
109 debug("SF: Enabling Write failed\n");
114 sst_disable_writing(struct spi_flash *flash)
116 int ret = spi_flash_cmd_write_disable(flash);
118 debug("SF: Disabling Write failed\n");
123 sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
133 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
134 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
136 ret = sst_enable_writing(flash);
140 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
144 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
148 sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
150 size_t actual, cmd_len;
154 ret = spi_claim_bus(flash->spi);
156 debug("SF: Unable to claim SPI bus\n");
160 /* If the data is not word aligned, write out leading single byte */
163 ret = sst_byte_write(flash, offset, buf);
169 ret = sst_enable_writing(flash);
174 cmd[0] = CMD_SST_AAI_WP;
175 cmd[1] = offset >> 16;
176 cmd[2] = offset >> 8;
179 for (; actual < len - 1; actual += 2) {
180 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
181 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual, cmd[0],
184 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
187 debug("SF: sst word program failed\n");
191 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
200 ret = sst_disable_writing(flash);
202 /* If there is a single trailing byte, write it out */
203 if (!ret && actual != len)
204 ret = sst_byte_write(flash, offset, buf + actual);
207 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
208 ret ? "failure" : "success", len, offset - actual);
210 spi_release_bus(flash->spi);
214 static int sst_erase(struct spi_flash *flash, u32 offset, size_t len)
216 return spi_flash_cmd_erase(flash, CMD_SST_SE, offset, len);
220 sst_unlock(struct spi_flash *flash)
225 ret = sst_enable_writing(flash);
229 cmd = CMD_WRITE_STATUS;
231 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
233 debug("SF: Unable to set status byte\n");
235 debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_READ_STATUS));
241 spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
243 const struct sst_spi_flash_params *params;
244 struct sst_spi_flash *stm;
247 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
248 params = &sst_spi_flash_table[i];
249 if (params->idcode1 == idcode[2])
253 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
254 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
258 stm = malloc(sizeof(*stm));
260 debug("SF: Failed to allocate memory\n");
264 stm->params = params;
265 stm->flash.spi = spi;
266 stm->flash.name = params->name;
268 if (stm->params->flags & SST_FEAT_WP)
269 stm->flash.write = sst_write_wp;
271 stm->flash.write = spi_flash_cmd_write_multi;
272 stm->flash.erase = sst_erase;
273 stm->flash.read = spi_flash_cmd_read_fast;
274 stm->flash.page_size = SST_PAGE_SIZE;
275 stm->flash.sector_size = SST_SECTOR_SIZE;
276 stm->flash.size = stm->flash.sector_size * params->nr_sectors;
278 /* Flash powers up read-only, so clear BP# bits */
279 sst_unlock(&stm->flash);