Merge branch 'sf' of git://git.denx.de/u-boot-blackfin
[oweals/u-boot.git] / drivers / mtd / spi / sst.c
1 /*
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
39 struct sst_spi_flash_params {
40         u8 idcode1;
41         u16 nr_sectors;
42         const char *name;
43 };
44
45 struct sst_spi_flash {
46         struct spi_flash flash;
47         const struct sst_spi_flash_params *params;
48 };
49
50 static 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)
56 static const struct sst_spi_flash_params sst_spi_flash_table[] = {
57         {
58                 .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         },{
74                 .idcode1 = 0x4b,
75                 .nr_sectors = 2048,
76                 .name = "SST25VF064C",
77         },{
78                 .idcode1 = 0x01,
79                 .nr_sectors = 16,
80                 .name = "SST25WF512",
81         },{
82                 .idcode1 = 0x02,
83                 .nr_sectors = 32,
84                 .name = "SST25WF010",
85         },{
86                 .idcode1 = 0x03,
87                 .nr_sectors = 64,
88                 .name = "SST25WF020",
89         },{
90                 .idcode1 = 0x04,
91                 .nr_sectors = 128,
92                 .name = "SST25WF040",
93         },
94 };
95
96 static int
97 sst_enable_writing(struct spi_flash *flash)
98 {
99         int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
100         if (ret)
101                 debug("SF: Enabling Write failed\n");
102         return ret;
103 }
104
105 static int
106 sst_disable_writing(struct spi_flash *flash)
107 {
108         int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
109         if (ret)
110                 debug("SF: Disabling Write failed\n");
111         return ret;
112 }
113
114 static int
115 sst_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",
126                 spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
127
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
136         return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
137 }
138
139 static int
140 sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
141 {
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",
173                      spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0],
174                      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
183                 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
184                 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
206 static int sst_erase(struct spi_flash *flash, u32 offset, size_t len)
207 {
208         return spi_flash_cmd_erase(flash, CMD_SST_SE, offset, len);
209 }
210
211 static int
212 sst_unlock(struct spi_flash *flash)
213 {
214         int ret;
215         u8 cmd, status;
216
217         ret = sst_enable_writing(flash);
218         if (ret)
219                 return ret;
220
221         cmd = CMD_SST_WRSR;
222         status = 0;
223         ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
224         if (ret)
225                 debug("SF: Unable to set status byte\n");
226
227         debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
228
229         return ret;
230 }
231
232 struct spi_flash *
233 spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
234 {
235         const struct sst_spi_flash_params *params;
236         struct sst_spi_flash *stm;
237         size_t i;
238
239         for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
240                 params = &sst_spi_flash_table[i];
241                 if (params->idcode1 == idcode[2])
242                         break;
243         }
244
245         if (i == ARRAY_SIZE(sst_spi_flash_table)) {
246                 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
247                 return NULL;
248         }
249
250         stm = malloc(sizeof(*stm));
251         if (!stm) {
252                 debug("SF: Failed to allocate memory\n");
253                 return NULL;
254         }
255
256         stm->params = params;
257         stm->flash.spi = spi;
258         stm->flash.name = params->name;
259
260         stm->flash.write = sst_write;
261         stm->flash.erase = sst_erase;
262         stm->flash.read = spi_flash_cmd_read_fast;
263         stm->flash.sector_size = SST_SECTOR_SIZE;
264         stm->flash.size = stm->flash.sector_size * params->nr_sectors;
265
266         /* Flash powers up read-only, so clear BP# bits */
267         sst_unlock(&stm->flash);
268
269         return &stm->flash;
270 }