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_WREN 0x06 /* Write Enable */
22 #define CMD_SST_WRDI 0x04 /* Write Disable */
23 #define CMD_SST_RDSR 0x05 /* Read Status Register */
24 #define CMD_SST_WRSR 0x01 /* Write Status Register */
25 #define CMD_SST_READ 0x03 /* Read Data Bytes */
26 #define CMD_SST_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
27 #define CMD_SST_BP 0x02 /* Byte Program */
28 #define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */
29 #define CMD_SST_SE 0x20 /* Sector Erase */
31 #define SST_SR_WIP (1 << 0) /* Write-in-Progress */
32 #define SST_SR_WEL (1 << 1) /* Write enable */
33 #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */
34 #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */
35 #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */
36 #define SST_SR_AAI (1 << 6) /* Addressing mode */
37 #define SST_SR_BPL (1 << 7) /* BP bits lock */
39 struct sst_spi_flash_params {
45 struct sst_spi_flash {
46 struct spi_flash flash;
47 const struct sst_spi_flash_params *params;
50 static inline struct sst_spi_flash *to_sst_spi_flash(struct spi_flash *flash)
52 return container_of(flash, struct sst_spi_flash, flash);
55 #define SST_SECTOR_SIZE (4 * 1024)
56 static const struct sst_spi_flash_params sst_spi_flash_table[] = {
60 .name = "SST25VF040B",
64 .name = "SST25VF080B",
68 .name = "SST25VF016B",
72 .name = "SST25VF032B",
93 sst_enable_writing(struct spi_flash *flash)
95 int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
97 debug("SF: Enabling Write failed\n");
102 sst_disable_writing(struct spi_flash *flash)
104 int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
106 debug("SF: Disabling Write failed\n");
111 sst_read_fast(struct spi_flash *flash, u32 offset, size_t len, void *buf)
120 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
124 sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
134 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
135 spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
137 ret = sst_enable_writing(flash);
141 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
145 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
149 sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
151 size_t actual, cmd_len;
155 ret = spi_claim_bus(flash->spi);
157 debug("SF: Unable to claim SPI bus\n");
161 /* If the data is not word aligned, write out leading single byte */
164 ret = sst_byte_write(flash, offset, buf);
170 ret = sst_enable_writing(flash);
175 cmd[0] = CMD_SST_AAI_WP;
176 cmd[1] = offset >> 16;
177 cmd[2] = offset >> 8;
180 for (; actual < len - 1; actual += 2) {
181 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
182 spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0],
185 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
188 debug("SF: sst word program failed\n");
192 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
201 ret = sst_disable_writing(flash);
203 /* If there is a single trailing byte, write it out */
204 if (!ret && actual != len)
205 ret = sst_byte_write(flash, offset, buf + actual);
208 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
209 ret ? "failure" : "success", len, offset - actual);
211 spi_release_bus(flash->spi);
216 sst_erase(struct spi_flash *flash, u32 offset, size_t len)
218 unsigned long sector_size;
224 * This function currently uses sector erase only.
225 * Probably speed things up by using bulk erase
229 sector_size = SST_SECTOR_SIZE;
231 if (offset % sector_size) {
232 debug("SF: Erase offset not multiple of sector size\n");
236 ret = spi_claim_bus(flash->spi);
238 debug("SF: Unable to claim SPI bus\n");
248 while (offset < end) {
249 cmd[1] = offset >> 16;
250 cmd[2] = offset >> 8;
251 offset += sector_size;
253 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
254 cmd[2], cmd[3], offset);
256 ret = sst_enable_writing(flash);
260 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
262 debug("SF: sst page erase failed\n");
266 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
271 debug("SF: sst: Successfully erased %lu bytes @ 0x%x\n",
272 len * sector_size, start);
274 spi_release_bus(flash->spi);
279 sst_unlock(struct spi_flash *flash)
284 ret = sst_enable_writing(flash);
290 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
292 debug("SF: Unable to set status byte\n");
294 debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
300 spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
302 const struct sst_spi_flash_params *params;
303 struct sst_spi_flash *stm;
306 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
307 params = &sst_spi_flash_table[i];
308 if (params->idcode1 == idcode[2])
312 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
313 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
317 stm = malloc(sizeof(*stm));
319 debug("SF: Failed to allocate memory\n");
323 stm->params = params;
324 stm->flash.spi = spi;
325 stm->flash.name = params->name;
327 stm->flash.write = sst_write;
328 stm->flash.erase = sst_erase;
329 stm->flash.read = sst_read_fast;
330 stm->flash.size = SST_SECTOR_SIZE * params->nr_sectors;
332 printf("SF: Detected %s with page size %u, total ",
333 params->name, SST_SECTOR_SIZE);
334 print_size(stm->flash.size, "\n");
336 /* Flash powers up read-only, so clear BP# bits */
337 sst_unlock(&stm->flash);