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