blob: d55055035cdb4c7120d1bb379aad7e679cbd3e68 [file] [log] [blame]
Mike Frysingerf1cb7c82009-03-27 19:27:58 -04001/*
2 * Driver for SST serial flashes
3 *
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.
11 *
12 * Licensed under the GPL-2 or later.
13 */
14
15#include <common.h>
16#include <malloc.h>
17#include <spi_flash.h>
18
19#include "spi_flash_internal.h"
20
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 */
30
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 */
38
39struct sst_spi_flash_params {
40 u8 idcode1;
41 u16 nr_sectors;
42 const char *name;
43};
44
45struct sst_spi_flash {
46 struct spi_flash flash;
47 const struct sst_spi_flash_params *params;
48};
49
50static inline struct sst_spi_flash *to_sst_spi_flash(struct spi_flash *flash)
51{
52 return container_of(flash, struct sst_spi_flash, flash);
53}
54
55#define SST_SECTOR_SIZE (4 * 1024)
56static const struct sst_spi_flash_params sst_spi_flash_table[] = {
57 {
Mike Frysingerca3e83b2009-06-19 03:27:28 -040058 .idcode1 = 0x8d,
59 .nr_sectors = 128,
60 .name = "SST25VF040B",
61 },{
62 .idcode1 = 0x8e,
63 .nr_sectors = 256,
64 .name = "SST25VF080B",
65 },{
66 .idcode1 = 0x41,
67 .nr_sectors = 512,
68 .name = "SST25VF016B",
69 },{
70 .idcode1 = 0x4a,
71 .nr_sectors = 1024,
72 .name = "SST25VF032B",
73 },{
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040074 .idcode1 = 0x01,
Mike Frysingerad4897e2009-06-19 03:20:06 -040075 .nr_sectors = 16,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040076 .name = "SST25WF512",
77 },{
78 .idcode1 = 0x02,
Mike Frysingerad4897e2009-06-19 03:20:06 -040079 .nr_sectors = 32,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040080 .name = "SST25WF010",
81 },{
82 .idcode1 = 0x03,
Mike Frysingerad4897e2009-06-19 03:20:06 -040083 .nr_sectors = 64,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040084 .name = "SST25WF020",
85 },{
86 .idcode1 = 0x04,
Mike Frysingerad4897e2009-06-19 03:20:06 -040087 .nr_sectors = 128,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040088 .name = "SST25WF040",
89 },
90};
91
92static int
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040093sst_enable_writing(struct spi_flash *flash)
94{
95 int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
96 if (ret)
97 debug("SF: Enabling Write failed\n");
98 return ret;
99}
100
101static int
102sst_disable_writing(struct spi_flash *flash)
103{
104 int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
105 if (ret)
106 debug("SF: Disabling Write failed\n");
107 return ret;
108}
109
110static int
111sst_read_fast(struct spi_flash *flash, u32 offset, size_t len, void *buf)
112{
113 u8 cmd[5] = {
114 CMD_READ_ARRAY_FAST,
115 offset >> 16,
116 offset >> 8,
117 offset,
118 0x00,
119 };
120 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
121}
122
123static int
124sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
125{
126 int ret;
127 u8 cmd[4] = {
128 CMD_SST_BP,
129 offset >> 16,
130 offset >> 8,
131 offset,
132 };
133
134 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
135 spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
136
137 ret = sst_enable_writing(flash);
138 if (ret)
139 return ret;
140
141 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
142 if (ret)
143 return ret;
144
Mike Frysinger37e13bc2011-01-10 02:20:12 -0500145 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400146}
147
148static int
149sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
150{
151 size_t actual, cmd_len;
152 int ret;
153 u8 cmd[4];
154
155 ret = spi_claim_bus(flash->spi);
156 if (ret) {
157 debug("SF: Unable to claim SPI bus\n");
158 return ret;
159 }
160
161 /* If the data is not word aligned, write out leading single byte */
162 actual = offset % 2;
163 if (actual) {
164 ret = sst_byte_write(flash, offset, buf);
165 if (ret)
166 goto done;
167 }
168 offset += actual;
169
170 ret = sst_enable_writing(flash);
171 if (ret)
172 goto done;
173
174 cmd_len = 4;
175 cmd[0] = CMD_SST_AAI_WP;
176 cmd[1] = offset >> 16;
177 cmd[2] = offset >> 8;
178 cmd[3] = offset;
179
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],
183 offset);
184
185 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
186 buf + actual, 2);
187 if (ret) {
188 debug("SF: sst word program failed\n");
189 break;
190 }
191
Mike Frysinger37e13bc2011-01-10 02:20:12 -0500192 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400193 if (ret)
194 break;
195
196 cmd_len = 1;
197 offset += 2;
198 }
199
200 if (!ret)
201 ret = sst_disable_writing(flash);
202
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);
206
207 done:
208 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
209 ret ? "failure" : "success", len, offset - actual);
210
211 spi_release_bus(flash->spi);
212 return ret;
213}
214
Mike Frysinger53421bb2011-01-10 02:20:13 -0500215int sst_erase(struct spi_flash *flash, u32 offset, size_t len)
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400216{
Mike Frysinger53421bb2011-01-10 02:20:13 -0500217 return spi_flash_cmd_erase(flash, CMD_SST_SE, SST_SECTOR_SIZE,
218 offset, len);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400219}
220
221static int
222sst_unlock(struct spi_flash *flash)
223{
224 int ret;
225 u8 cmd, status;
226
227 ret = sst_enable_writing(flash);
228 if (ret)
229 return ret;
230
231 cmd = CMD_SST_WRSR;
232 status = 0;
233 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
234 if (ret)
235 debug("SF: Unable to set status byte\n");
236
237 debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
238
239 return ret;
240}
241
242struct spi_flash *
243spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
244{
245 const struct sst_spi_flash_params *params;
246 struct sst_spi_flash *stm;
247 size_t i;
248
249 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
250 params = &sst_spi_flash_table[i];
251 if (params->idcode1 == idcode[2])
252 break;
253 }
254
255 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
256 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
257 return NULL;
258 }
259
260 stm = malloc(sizeof(*stm));
261 if (!stm) {
262 debug("SF: Failed to allocate memory\n");
263 return NULL;
264 }
265
266 stm->params = params;
267 stm->flash.spi = spi;
268 stm->flash.name = params->name;
269
270 stm->flash.write = sst_write;
271 stm->flash.erase = sst_erase;
272 stm->flash.read = sst_read_fast;
273 stm->flash.size = SST_SECTOR_SIZE * params->nr_sectors;
274
Mike Frysinger02110b52010-04-29 00:35:12 -0400275 printf("SF: Detected %s with page size %u, total ",
276 params->name, SST_SECTOR_SIZE);
277 print_size(stm->flash.size, "\n");
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400278
279 /* Flash powers up read-only, so clear BP# bits */
280 sst_unlock(&stm->flash);
281
282 return &stm->flash;
283}