blob: 50e929918a824e0bf0a8903daa9b6055681feb18 [file] [log] [blame]
Mike Frysinger1c587432009-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 Frysingerdd541262009-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 Frysinger1c587432009-03-27 19:27:58 -040074 .idcode1 = 0x01,
Mike Frysinger7d907f02009-06-19 03:20:06 -040075 .nr_sectors = 16,
Mike Frysinger1c587432009-03-27 19:27:58 -040076 .name = "SST25WF512",
77 },{
78 .idcode1 = 0x02,
Mike Frysinger7d907f02009-06-19 03:20:06 -040079 .nr_sectors = 32,
Mike Frysinger1c587432009-03-27 19:27:58 -040080 .name = "SST25WF010",
81 },{
82 .idcode1 = 0x03,
Mike Frysinger7d907f02009-06-19 03:20:06 -040083 .nr_sectors = 64,
Mike Frysinger1c587432009-03-27 19:27:58 -040084 .name = "SST25WF020",
85 },{
86 .idcode1 = 0x04,
Mike Frysinger7d907f02009-06-19 03:20:06 -040087 .nr_sectors = 128,
Mike Frysinger1c587432009-03-27 19:27:58 -040088 .name = "SST25WF040",
89 },
90};
91
92static int
93sst_wait_ready(struct spi_flash *flash, unsigned long timeout)
94{
95 struct spi_slave *spi = flash->spi;
96 unsigned long timebase;
97 int ret;
98 u8 byte = CMD_SST_RDSR;
99
100 ret = spi_xfer(spi, sizeof(byte) * 8, &byte, NULL, SPI_XFER_BEGIN);
101 if (ret) {
102 debug("SF: Failed to send command %02x: %d\n", byte, ret);
103 return ret;
104 }
105
106 timebase = get_timer(0);
107 do {
108 ret = spi_xfer(spi, sizeof(byte) * 8, NULL, &byte, 0);
109 if (ret)
110 break;
111
112 if ((byte & SST_SR_WIP) == 0)
113 break;
114
115 } while (get_timer(timebase) < timeout);
116
117 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
118
119 if (!ret && (byte & SST_SR_WIP) != 0)
120 ret = -1;
121
122 if (ret)
123 debug("SF: sst wait for ready timed out\n");
124 return ret;
125}
126
127static int
128sst_enable_writing(struct spi_flash *flash)
129{
130 int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
131 if (ret)
132 debug("SF: Enabling Write failed\n");
133 return ret;
134}
135
136static int
137sst_disable_writing(struct spi_flash *flash)
138{
139 int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
140 if (ret)
141 debug("SF: Disabling Write failed\n");
142 return ret;
143}
144
145static int
146sst_read_fast(struct spi_flash *flash, u32 offset, size_t len, void *buf)
147{
148 u8 cmd[5] = {
149 CMD_READ_ARRAY_FAST,
150 offset >> 16,
151 offset >> 8,
152 offset,
153 0x00,
154 };
155 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
156}
157
158static int
159sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
160{
161 int ret;
162 u8 cmd[4] = {
163 CMD_SST_BP,
164 offset >> 16,
165 offset >> 8,
166 offset,
167 };
168
169 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
170 spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
171
172 ret = sst_enable_writing(flash);
173 if (ret)
174 return ret;
175
176 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
177 if (ret)
178 return ret;
179
180 return sst_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
181}
182
183static int
184sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
185{
186 size_t actual, cmd_len;
187 int ret;
188 u8 cmd[4];
189
190 ret = spi_claim_bus(flash->spi);
191 if (ret) {
192 debug("SF: Unable to claim SPI bus\n");
193 return ret;
194 }
195
196 /* If the data is not word aligned, write out leading single byte */
197 actual = offset % 2;
198 if (actual) {
199 ret = sst_byte_write(flash, offset, buf);
200 if (ret)
201 goto done;
202 }
203 offset += actual;
204
205 ret = sst_enable_writing(flash);
206 if (ret)
207 goto done;
208
209 cmd_len = 4;
210 cmd[0] = CMD_SST_AAI_WP;
211 cmd[1] = offset >> 16;
212 cmd[2] = offset >> 8;
213 cmd[3] = offset;
214
215 for (; actual < len - 1; actual += 2) {
216 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
217 spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0],
218 offset);
219
220 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
221 buf + actual, 2);
222 if (ret) {
223 debug("SF: sst word program failed\n");
224 break;
225 }
226
227 ret = sst_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
228 if (ret)
229 break;
230
231 cmd_len = 1;
232 offset += 2;
233 }
234
235 if (!ret)
236 ret = sst_disable_writing(flash);
237
238 /* If there is a single trailing byte, write it out */
239 if (!ret && actual != len)
240 ret = sst_byte_write(flash, offset, buf + actual);
241
242 done:
243 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
244 ret ? "failure" : "success", len, offset - actual);
245
246 spi_release_bus(flash->spi);
247 return ret;
248}
249
250int
251sst_erase(struct spi_flash *flash, u32 offset, size_t len)
252{
253 unsigned long sector_size;
254 u32 start, end;
255 int ret;
256 u8 cmd[4];
257
258 /*
259 * This function currently uses sector erase only.
260 * Probably speed things up by using bulk erase
261 * when possible.
262 */
263
264 sector_size = SST_SECTOR_SIZE;
265
266 if (offset % sector_size) {
267 debug("SF: Erase offset not multiple of sector size\n");
268 return -1;
269 }
270
271 ret = spi_claim_bus(flash->spi);
272 if (ret) {
273 debug("SF: Unable to claim SPI bus\n");
274 return ret;
275 }
276
277 cmd[0] = CMD_SST_SE;
278 cmd[3] = 0;
279 start = offset;
280 end = start + len;
281
282 ret = 0;
283 while (offset < end) {
284 cmd[1] = offset >> 16;
285 cmd[2] = offset >> 8;
286 offset += sector_size;
287
288 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
289 cmd[2], cmd[3], offset);
290
291 ret = sst_enable_writing(flash);
292 if (ret)
293 break;
294
295 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
296 if (ret) {
297 debug("SF: sst page erase failed\n");
298 break;
299 }
300
301 ret = sst_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
302 if (ret)
303 break;
304 }
305
306 debug("SF: sst: Successfully erased %lu bytes @ 0x%x\n",
307 len * sector_size, start);
308
309 spi_release_bus(flash->spi);
310 return ret;
311}
312
313static int
314sst_unlock(struct spi_flash *flash)
315{
316 int ret;
317 u8 cmd, status;
318
319 ret = sst_enable_writing(flash);
320 if (ret)
321 return ret;
322
323 cmd = CMD_SST_WRSR;
324 status = 0;
325 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
326 if (ret)
327 debug("SF: Unable to set status byte\n");
328
329 debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
330
331 return ret;
332}
333
334struct spi_flash *
335spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
336{
337 const struct sst_spi_flash_params *params;
338 struct sst_spi_flash *stm;
339 size_t i;
340
341 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
342 params = &sst_spi_flash_table[i];
343 if (params->idcode1 == idcode[2])
344 break;
345 }
346
347 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
348 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
349 return NULL;
350 }
351
352 stm = malloc(sizeof(*stm));
353 if (!stm) {
354 debug("SF: Failed to allocate memory\n");
355 return NULL;
356 }
357
358 stm->params = params;
359 stm->flash.spi = spi;
360 stm->flash.name = params->name;
361
362 stm->flash.write = sst_write;
363 stm->flash.erase = sst_erase;
364 stm->flash.read = sst_read_fast;
365 stm->flash.size = SST_SECTOR_SIZE * params->nr_sectors;
366
367 debug("SF: Detected %s with page size %u, total %u bytes\n",
368 params->name, SST_SECTOR_SIZE, stm->flash.size);
369
370 /* Flash powers up read-only, so clear BP# bits */
371 sst_unlock(&stm->flash);
372
373 return &stm->flash;
374}