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 */
24 #define SST_SR_WIP (1 << 0) /* Write-in-Progress */
25 #define SST_SR_WEL (1 << 1) /* Write enable */
26 #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */
27 #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */
28 #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */
29 #define SST_SR_AAI (1 << 6) /* Addressing mode */
30 #define SST_SR_BPL (1 << 7) /* BP bits lock */
32 #define SST_FEAT_WP (1 << 0) /* Supports AAI word program */
33 #define SST_FEAT_MBP (1 << 1) /* Supports multibyte program */
35 struct sst_spi_flash_params {
42 struct sst_spi_flash {
43 struct spi_flash flash;
44 const struct sst_spi_flash_params *params;
47 static const struct sst_spi_flash_params sst_spi_flash_table[] = {
52 .name = "SST25VF040B",
57 .name = "SST25VF080B",
62 .name = "SST25VF016B",
67 .name = "SST25VF032B",
70 .flags = SST_FEAT_MBP,
72 .name = "SST25VF064C",
97 sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
107 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
108 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
110 ret = spi_flash_cmd_write_enable(flash);
114 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
118 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
122 sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
124 size_t actual, cmd_len;
128 ret = spi_claim_bus(flash->spi);
130 debug("SF: Unable to claim SPI bus\n");
134 /* If the data is not word aligned, write out leading single byte */
137 ret = sst_byte_write(flash, offset, buf);
143 ret = spi_flash_cmd_write_enable(flash);
148 cmd[0] = CMD_SST_AAI_WP;
149 cmd[1] = offset >> 16;
150 cmd[2] = offset >> 8;
153 for (; actual < len - 1; actual += 2) {
154 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
155 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual, cmd[0],
158 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
161 debug("SF: sst word program failed\n");
165 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
174 ret = spi_flash_cmd_write_disable(flash);
176 /* If there is a single trailing byte, write it out */
177 if (!ret && actual != len)
178 ret = sst_byte_write(flash, offset, buf + actual);
181 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
182 ret ? "failure" : "success", len, offset - actual);
184 spi_release_bus(flash->spi);
189 spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
191 const struct sst_spi_flash_params *params;
192 struct sst_spi_flash *stm;
195 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
196 params = &sst_spi_flash_table[i];
197 if (params->idcode1 == idcode[2])
201 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
202 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
206 stm = spi_flash_alloc(struct sst_spi_flash, spi, params->name);
208 debug("SF: Failed to allocate memory\n");
212 stm->params = params;
214 if (stm->params->flags & SST_FEAT_WP)
215 stm->flash.write = sst_write_wp;
216 stm->flash.page_size = 256;
217 stm->flash.sector_size = 4096;
218 stm->flash.size = stm->flash.sector_size * params->nr_sectors;
220 /* Flash powers up read-only, so clear BP# bits */
221 spi_flash_cmd_write_status(&stm->flash, 0);