blob: 95f5490c350ac0f85287b518f1b2427286c30ee3 [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
Mike Frysinger1c587432009-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 Frysinger1c587432009-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 Frysinger8875bdb2011-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 Frysinger1c587432009-03-27 19:27:58 -040035struct sst_spi_flash_params {
36 u8 idcode1;
Mike Frysinger8875bdb2011-04-25 06:59:54 +000037 u8 flags;
Mike Frysinger1c587432009-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 Frysinger1c587432009-03-27 19:27:58 -040047static const struct sst_spi_flash_params sst_spi_flash_table[] = {
48 {
Mike Frysingerdd541262009-06-19 03:27:28 -040049 .idcode1 = 0x8d,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000050 .flags = SST_FEAT_WP,
Mike Frysingerdd541262009-06-19 03:27:28 -040051 .nr_sectors = 128,
52 .name = "SST25VF040B",
53 },{
54 .idcode1 = 0x8e,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000055 .flags = SST_FEAT_WP,
Mike Frysingerdd541262009-06-19 03:27:28 -040056 .nr_sectors = 256,
57 .name = "SST25VF080B",
58 },{
59 .idcode1 = 0x41,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000060 .flags = SST_FEAT_WP,
Mike Frysingerdd541262009-06-19 03:27:28 -040061 .nr_sectors = 512,
62 .name = "SST25VF016B",
63 },{
64 .idcode1 = 0x4a,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000065 .flags = SST_FEAT_WP,
Mike Frysingerdd541262009-06-19 03:27:28 -040066 .nr_sectors = 1024,
67 .name = "SST25VF032B",
68 },{
James Kosin1c091f52011-04-13 15:12:18 -040069 .idcode1 = 0x4b,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000070 .flags = SST_FEAT_MBP,
James Kosin1c091f52011-04-13 15:12:18 -040071 .nr_sectors = 2048,
72 .name = "SST25VF064C",
73 },{
Mike Frysinger1c587432009-03-27 19:27:58 -040074 .idcode1 = 0x01,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000075 .flags = SST_FEAT_WP,
Mike Frysinger7d907f02009-06-19 03:20:06 -040076 .nr_sectors = 16,
Mike Frysinger1c587432009-03-27 19:27:58 -040077 .name = "SST25WF512",
78 },{
79 .idcode1 = 0x02,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000080 .flags = SST_FEAT_WP,
Mike Frysinger7d907f02009-06-19 03:20:06 -040081 .nr_sectors = 32,
Mike Frysinger1c587432009-03-27 19:27:58 -040082 .name = "SST25WF010",
83 },{
84 .idcode1 = 0x03,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000085 .flags = SST_FEAT_WP,
Mike Frysinger7d907f02009-06-19 03:20:06 -040086 .nr_sectors = 64,
Mike Frysinger1c587432009-03-27 19:27:58 -040087 .name = "SST25WF020",
88 },{
89 .idcode1 = 0x04,
Mike Frysinger8875bdb2011-04-25 06:59:54 +000090 .flags = SST_FEAT_WP,
Mike Frysinger7d907f02009-06-19 03:20:06 -040091 .nr_sectors = 128,
Mike Frysinger1c587432009-03-27 19:27:58 -040092 .name = "SST25WF040",
93 },
94};
95
96static int
Mike Frysinger1c587432009-03-27 19:27:58 -040097sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
98{
99 int ret;
100 u8 cmd[4] = {
101 CMD_SST_BP,
102 offset >> 16,
103 offset >> 8,
104 offset,
105 };
106
107 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Mike Frysingerb4c87d62012-01-28 16:26:03 -0800108 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
Mike Frysinger1c587432009-03-27 19:27:58 -0400109
Mike Frysinger7a6d2a72012-03-04 23:12:23 -0500110 ret = spi_flash_cmd_write_enable(flash);
Mike Frysinger1c587432009-03-27 19:27:58 -0400111 if (ret)
112 return ret;
113
114 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
115 if (ret)
116 return ret;
117
Mike Frysinger61630452011-01-10 02:20:12 -0500118 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Mike Frysinger1c587432009-03-27 19:27:58 -0400119}
120
121static int
Mike Frysinger8875bdb2011-04-25 06:59:54 +0000122sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
Mike Frysinger1c587432009-03-27 19:27:58 -0400123{
124 size_t actual, cmd_len;
125 int ret;
126 u8 cmd[4];
127
128 ret = spi_claim_bus(flash->spi);
129 if (ret) {
130 debug("SF: Unable to claim SPI bus\n");
131 return ret;
132 }
133
134 /* If the data is not word aligned, write out leading single byte */
135 actual = offset % 2;
136 if (actual) {
137 ret = sst_byte_write(flash, offset, buf);
138 if (ret)
139 goto done;
140 }
141 offset += actual;
142
Mike Frysinger7a6d2a72012-03-04 23:12:23 -0500143 ret = spi_flash_cmd_write_enable(flash);
Mike Frysinger1c587432009-03-27 19:27:58 -0400144 if (ret)
145 goto done;
146
147 cmd_len = 4;
148 cmd[0] = CMD_SST_AAI_WP;
149 cmd[1] = offset >> 16;
150 cmd[2] = offset >> 8;
151 cmd[3] = offset;
152
153 for (; actual < len - 1; actual += 2) {
154 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Mike Frysingerb4c87d62012-01-28 16:26:03 -0800155 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual, cmd[0],
Mike Frysinger1c587432009-03-27 19:27:58 -0400156 offset);
157
158 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
159 buf + actual, 2);
160 if (ret) {
161 debug("SF: sst word program failed\n");
162 break;
163 }
164
Mike Frysinger61630452011-01-10 02:20:12 -0500165 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Mike Frysinger1c587432009-03-27 19:27:58 -0400166 if (ret)
167 break;
168
169 cmd_len = 1;
170 offset += 2;
171 }
172
173 if (!ret)
Mike Frysinger7a6d2a72012-03-04 23:12:23 -0500174 ret = spi_flash_cmd_write_disable(flash);
Mike Frysinger1c587432009-03-27 19:27:58 -0400175
176 /* If there is a single trailing byte, write it out */
177 if (!ret && actual != len)
178 ret = sst_byte_write(flash, offset, buf + actual);
179
180 done:
181 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
182 ret ? "failure" : "success", len, offset - actual);
183
184 spi_release_bus(flash->spi);
185 return ret;
186}
187
Mike Frysinger1c587432009-03-27 19:27:58 -0400188struct spi_flash *
189spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
190{
191 const struct sst_spi_flash_params *params;
192 struct sst_spi_flash *stm;
193 size_t i;
194
195 for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
196 params = &sst_spi_flash_table[i];
197 if (params->idcode1 == idcode[2])
198 break;
199 }
200
201 if (i == ARRAY_SIZE(sst_spi_flash_table)) {
202 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
203 return NULL;
204 }
205
Simon Glassc0f87dd2013-03-11 06:08:03 +0000206 stm = spi_flash_alloc(struct sst_spi_flash, spi, params->name);
Mike Frysinger1c587432009-03-27 19:27:58 -0400207 if (!stm) {
208 debug("SF: Failed to allocate memory\n");
209 return NULL;
210 }
211
212 stm->params = params;
Mike Frysinger1c587432009-03-27 19:27:58 -0400213
Mike Frysinger8875bdb2011-04-25 06:59:54 +0000214 if (stm->params->flags & SST_FEAT_WP)
215 stm->flash.write = sst_write_wp;
Mike Frysingerc4e932c2012-03-04 22:35:50 -0500216 stm->flash.page_size = 256;
217 stm->flash.sector_size = 4096;
Richard Retanubun4e6a5152011-02-16 16:37:22 -0500218 stm->flash.size = stm->flash.sector_size * params->nr_sectors;
Mike Frysinger1c587432009-03-27 19:27:58 -0400219
220 /* Flash powers up read-only, so clear BP# bits */
Mike Frysinger41e17132012-03-04 23:18:17 -0500221 spi_flash_cmd_write_status(&stm->flash, 0);
Mike Frysinger1c587432009-03-27 19:27:58 -0400222
223 return &stm->flash;
224}