blob: dcc0bbe4b136ad19a5893123989f26fd2ebf52bb [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
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040021#define CMD_SST_BP 0x02 /* Byte Program */
22#define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040023
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 */
31
Mike Frysingerb75d9902011-04-25 06:59:54 +000032#define SST_FEAT_WP (1 << 0) /* Supports AAI word program */
33#define SST_FEAT_MBP (1 << 1) /* Supports multibyte program */
34
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040035struct sst_spi_flash_params {
36 u8 idcode1;
Mike Frysingerb75d9902011-04-25 06:59:54 +000037 u8 flags;
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040038 u16 nr_sectors;
39 const char *name;
40};
41
42struct sst_spi_flash {
43 struct spi_flash flash;
44 const struct sst_spi_flash_params *params;
45};
46
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040047static const struct sst_spi_flash_params sst_spi_flash_table[] = {
48 {
Mike Frysingerca3e83b2009-06-19 03:27:28 -040049 .idcode1 = 0x8d,
Mike Frysingerb75d9902011-04-25 06:59:54 +000050 .flags = SST_FEAT_WP,
Mike Frysingerca3e83b2009-06-19 03:27:28 -040051 .nr_sectors = 128,
52 .name = "SST25VF040B",
53 },{
54 .idcode1 = 0x8e,
Mike Frysingerb75d9902011-04-25 06:59:54 +000055 .flags = SST_FEAT_WP,
Mike Frysingerca3e83b2009-06-19 03:27:28 -040056 .nr_sectors = 256,
57 .name = "SST25VF080B",
58 },{
59 .idcode1 = 0x41,
Mike Frysingerb75d9902011-04-25 06:59:54 +000060 .flags = SST_FEAT_WP,
Mike Frysingerca3e83b2009-06-19 03:27:28 -040061 .nr_sectors = 512,
62 .name = "SST25VF016B",
63 },{
64 .idcode1 = 0x4a,
Mike Frysingerb75d9902011-04-25 06:59:54 +000065 .flags = SST_FEAT_WP,
Mike Frysingerca3e83b2009-06-19 03:27:28 -040066 .nr_sectors = 1024,
67 .name = "SST25VF032B",
68 },{
James Kosin23ec2c22011-04-13 15:12:18 -040069 .idcode1 = 0x4b,
Mike Frysingerb75d9902011-04-25 06:59:54 +000070 .flags = SST_FEAT_MBP,
James Kosin23ec2c22011-04-13 15:12:18 -040071 .nr_sectors = 2048,
72 .name = "SST25VF064C",
73 },{
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040074 .idcode1 = 0x01,
Mike Frysingerb75d9902011-04-25 06:59:54 +000075 .flags = SST_FEAT_WP,
Mike Frysingerad4897e2009-06-19 03:20:06 -040076 .nr_sectors = 16,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040077 .name = "SST25WF512",
78 },{
79 .idcode1 = 0x02,
Mike Frysingerb75d9902011-04-25 06:59:54 +000080 .flags = SST_FEAT_WP,
Mike Frysingerad4897e2009-06-19 03:20:06 -040081 .nr_sectors = 32,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040082 .name = "SST25WF010",
83 },{
84 .idcode1 = 0x03,
Mike Frysingerb75d9902011-04-25 06:59:54 +000085 .flags = SST_FEAT_WP,
Mike Frysingerad4897e2009-06-19 03:20:06 -040086 .nr_sectors = 64,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040087 .name = "SST25WF020",
88 },{
89 .idcode1 = 0x04,
Mike Frysingerb75d9902011-04-25 06:59:54 +000090 .flags = SST_FEAT_WP,
Mike Frysingerad4897e2009-06-19 03:20:06 -040091 .nr_sectors = 128,
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040092 .name = "SST25WF040",
93 },
94};
95
96static int
Mike Frysingerf1cb7c82009-03-27 19:27:58 -040097sst_enable_writing(struct spi_flash *flash)
98{
Mike Frysinger8ec7f4c2011-04-23 23:05:55 +000099 int ret = spi_flash_cmd_write_enable(flash);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400100 if (ret)
101 debug("SF: Enabling Write failed\n");
102 return ret;
103}
104
105static int
106sst_disable_writing(struct spi_flash *flash)
107{
Mike Frysinger79112112011-04-25 06:59:53 +0000108 int ret = spi_flash_cmd_write_disable(flash);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400109 if (ret)
110 debug("SF: Disabling Write failed\n");
111 return ret;
112}
113
114static int
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400115sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
116{
117 int ret;
118 u8 cmd[4] = {
119 CMD_SST_BP,
120 offset >> 16,
121 offset >> 8,
122 offset,
123 };
124
125 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Mike Frysinger1302bec2012-01-28 16:26:03 -0800126 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400127
128 ret = sst_enable_writing(flash);
129 if (ret)
130 return ret;
131
132 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
133 if (ret)
134 return ret;
135
Mike Frysinger37e13bc2011-01-10 02:20:12 -0500136 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400137}
138
139static int
Mike Frysingerb75d9902011-04-25 06:59:54 +0000140sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400141{
142 size_t actual, cmd_len;
143 int ret;
144 u8 cmd[4];
145
146 ret = spi_claim_bus(flash->spi);
147 if (ret) {
148 debug("SF: Unable to claim SPI bus\n");
149 return ret;
150 }
151
152 /* If the data is not word aligned, write out leading single byte */
153 actual = offset % 2;
154 if (actual) {
155 ret = sst_byte_write(flash, offset, buf);
156 if (ret)
157 goto done;
158 }
159 offset += actual;
160
161 ret = sst_enable_writing(flash);
162 if (ret)
163 goto done;
164
165 cmd_len = 4;
166 cmd[0] = CMD_SST_AAI_WP;
167 cmd[1] = offset >> 16;
168 cmd[2] = offset >> 8;
169 cmd[3] = offset;
170
171 for (; actual < len - 1; actual += 2) {
172 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Mike Frysinger1302bec2012-01-28 16:26:03 -0800173 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual, cmd[0],
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400174 offset);
175
176 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
177 buf + actual, 2);
178 if (ret) {
179 debug("SF: sst word program failed\n");
180 break;
181 }
182
Mike Frysinger37e13bc2011-01-10 02:20:12 -0500183 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400184 if (ret)
185 break;
186
187 cmd_len = 1;
188 offset += 2;
189 }
190
191 if (!ret)
192 ret = sst_disable_writing(flash);
193
194 /* If there is a single trailing byte, write it out */
195 if (!ret && actual != len)
196 ret = sst_byte_write(flash, offset, buf + actual);
197
198 done:
199 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
200 ret ? "failure" : "success", len, offset - actual);
201
202 spi_release_bus(flash->spi);
203 return ret;
204}
205
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400206static int
207sst_unlock(struct spi_flash *flash)
208{
209 int ret;
210 u8 cmd, status;
211
212 ret = sst_enable_writing(flash);
213 if (ret)
214 return ret;
215
Mike Frysinger1302bec2012-01-28 16:26:03 -0800216 cmd = CMD_WRITE_STATUS;
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400217 status = 0;
218 ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
219 if (ret)
220 debug("SF: Unable to set status byte\n");
221
Mike Frysinger1302bec2012-01-28 16:26:03 -0800222 debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_READ_STATUS));
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400223
224 return ret;
225}
226
227struct spi_flash *
228spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
229{
230 const struct sst_spi_flash_params *params;
231 struct sst_spi_flash *stm;
232 size_t i;
233
234 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
235 params = &sst_spi_flash_table[i];
236 if (params->idcode1 == idcode[2])
237 break;
238 }
239
240 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
241 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
242 return NULL;
243 }
244
245 stm = malloc(sizeof(*stm));
246 if (!stm) {
247 debug("SF: Failed to allocate memory\n");
248 return NULL;
249 }
250
251 stm->params = params;
252 stm->flash.spi = spi;
253 stm->flash.name = params->name;
254
Mike Frysingerb75d9902011-04-25 06:59:54 +0000255 if (stm->params->flags & SST_FEAT_WP)
256 stm->flash.write = sst_write_wp;
257 else
258 stm->flash.write = spi_flash_cmd_write_multi;
Mike Frysinger41477982012-03-04 22:35:50 -0500259 stm->flash.erase = spi_flash_cmd_erase;
Mike Frysinger816790c2011-04-12 01:34:55 -0400260 stm->flash.read = spi_flash_cmd_read_fast;
Mike Frysinger41477982012-03-04 22:35:50 -0500261 stm->flash.page_size = 256;
262 stm->flash.sector_size = 4096;
Richard Retanubunb0148dc2011-02-16 16:37:22 -0500263 stm->flash.size = stm->flash.sector_size * params->nr_sectors;
Mike Frysingerf1cb7c82009-03-27 19:27:58 -0400264
265 /* Flash powers up read-only, so clear BP# bits */
266 sst_unlock(&stm->flash);
267
268 return &stm->flash;
269}