blob: c5bec12b66ea31764c09d6e0dd5c07e1d2c53f69 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
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 <stdlib.h>
16#include <spi_flash.h>
17#include "spi_flash_internal.h"
18
19#define CMD_SST_WREN 0x06 /* Write Enable */
20#define CMD_SST_WRDI 0x04 /* Write Disable */
21#define CMD_SST_RDSR 0x05 /* Read Status Register */
22#define CMD_SST_WRSR 0x01 /* Write Status Register */
23#define CMD_SST_READ 0x03 /* Read Data Bytes */
24#define CMD_SST_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
25#define CMD_SST_BP 0x02 /* Byte Program */
26#define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */
27#define CMD_SST_SE 0x20 /* Sector Erase */
28
29#define SST_SR_WIP (1 << 0) /* Write-in-Progress */
30#define SST_SR_WEL (1 << 1) /* Write enable */
31#define SST_SR_BP0 (1 << 2) /* Block Protection 0 */
32#define SST_SR_BP1 (1 << 3) /* Block Protection 1 */
33#define SST_SR_BP2 (1 << 4) /* Block Protection 2 */
34#define SST_SR_AAI (1 << 6) /* Addressing mode */
35#define SST_SR_BPL (1 << 7) /* BP bits lock */
36
37struct sst_spi_flash_params {
38 u8 idcode1;
39 u16 nr_sectors;
40 const char *name;
41};
42
43struct sst_spi_flash {
44 struct spi_flash flash;
45 const struct sst_spi_flash_params *params;
46};
47
48static inline struct sst_spi_flash *to_sst_spi_flash(struct spi_flash *flash)
49{
50 return container_of(flash, struct sst_spi_flash, flash);
51}
52
53#define SST_SECTOR_SIZE (4 * 1024)
54static const struct sst_spi_flash_params sst_spi_flash_table[] = {
55 {
56 .idcode1 = 0x8d,
57 .nr_sectors = 128,
58 .name = "SST25VF040B",
59 },{
60 .idcode1 = 0x8e,
61 .nr_sectors = 256,
62 .name = "SST25VF080B",
63 },{
64 .idcode1 = 0x41,
65 .nr_sectors = 512,
66 .name = "SST25VF016B",
67 },{
68 .idcode1 = 0x4a,
69 .nr_sectors = 1024,
70 .name = "SST25VF032B",
71 },{
72 .idcode1 = 0x4b,
73 .nr_sectors = 2048,
74 .name = "SST25VF064C",
75 },{
76 .idcode1 = 0x01,
77 .nr_sectors = 16,
78 .name = "SST25WF512",
79 },{
80 .idcode1 = 0x02,
81 .nr_sectors = 32,
82 .name = "SST25WF010",
83 },{
84 .idcode1 = 0x03,
85 .nr_sectors = 64,
86 .name = "SST25WF020",
87 },{
88 .idcode1 = 0x04,
89 .nr_sectors = 128,
90 .name = "SST25WF040",
91 },
92};
93
94static int
95sst_enable_writing(struct spi_flash *flash)
96{
97 int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
98 if (ret)
99 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
100 return ret;
101}
102
103static int
104sst_disable_writing(struct spi_flash *flash)
105{
106 int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
107 if (ret)
108 printk(BIOS_WARNING, "SF: Disabling Write failed\n");
109 return ret;
110}
111
112static int
113sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
114{
115 int ret;
116 u8 cmd[4] = {
117 CMD_SST_BP,
118 offset >> 16,
119 offset >> 8,
120 offset,
121 };
122
123 printk(BIOS_SPEW, "BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
124 spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
125
126 ret = sst_enable_writing(flash);
127 if (ret)
128 return ret;
129
130 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
131 if (ret)
132 return ret;
133
134 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
135}
136
137static int
138sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
139{
140 size_t actual, cmd_len;
141 int ret;
142 u8 cmd[4];
143
144 ret = spi_claim_bus(flash->spi);
145 if (ret) {
146 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
147 return ret;
148 }
149
150 /* If the data is not word aligned, write out leading single byte */
151 actual = offset % 2;
152 if (actual) {
153 ret = sst_byte_write(flash, offset, buf);
154 if (ret)
155 goto done;
156 }
157 offset += actual;
158
159 ret = sst_enable_writing(flash);
160 if (ret)
161 goto done;
162
163 cmd_len = 4;
164 cmd[0] = CMD_SST_AAI_WP;
165 cmd[1] = offset >> 16;
166 cmd[2] = offset >> 8;
167 cmd[3] = offset;
168
169 for (; actual < len - 1; actual += 2) {
170 printk(BIOS_SPEW, "WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
171 spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0],
172 offset);
173
174 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
175 buf + actual, 2);
176 if (ret) {
177 printk(BIOS_WARNING, "SF: SST word program failed\n");
178 break;
179 }
180
181 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
182 if (ret)
183 break;
184
185 cmd_len = 1;
186 offset += 2;
187 }
188
189 if (!ret)
190 ret = sst_disable_writing(flash);
191
192 /* If there is a single trailing byte, write it out */
193 if (!ret && actual != len)
194 ret = sst_byte_write(flash, offset, buf + actual);
195
Patrick Georgi20959ba2012-05-12 23:30:36 +0200196done:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700197 printk(BIOS_INFO, "SF: SST: program %s %zu bytes @ 0x%lx\n",
198 ret ? "failure" : "success", len, offset - actual);
199
200 spi_release_bus(flash->spi);
201 return ret;
202}
203
204static int sst_erase(struct spi_flash *flash, u32 offset, size_t len)
205{
206 return spi_flash_cmd_erase(flash, CMD_SST_SE, offset, len);
207}
208
209static int
210sst_unlock(struct spi_flash *flash)
211{
212 int ret;
213 u8 cmd, status;
214
215 ret = sst_enable_writing(flash);
216 if (ret)
217 return ret;
218
219 cmd = CMD_SST_WRSR;
220 status = 0;
221 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
222 if (ret)
223 printk(BIOS_WARNING, "SF: Unable to set status byte\n");
224
225 printk(BIOS_INFO, "SF: SST: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
226
227 return ret;
228}
229
230struct spi_flash *
231spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
232{
233 const struct sst_spi_flash_params *params;
234 struct sst_spi_flash *stm;
235 size_t i;
236
237 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
238 params = &sst_spi_flash_table[i];
239 if (params->idcode1 == idcode[2])
240 break;
241 }
242
243 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
244 printk(BIOS_WARNING, "SF: Unsupported SST ID %02x\n", idcode[1]);
245 return NULL;
246 }
247
248 stm = malloc(sizeof(*stm));
249 if (!stm) {
250 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
251 return NULL;
252 }
253
254 stm->params = params;
255 stm->flash.spi = spi;
256 stm->flash.name = params->name;
257
258 stm->flash.write = sst_write;
259 stm->flash.erase = sst_erase;
260 stm->flash.read = spi_flash_cmd_read_fast;
261 stm->flash.sector_size = SST_SECTOR_SIZE;
262 stm->flash.size = stm->flash.sector_size * params->nr_sectors;
263
264 /* Flash powers up read-only, so clear BP# bits */
265 sst_unlock(&stm->flash);
266
267 return &stm->flash;
268}