Atmel LCD driver GUARDTIME fix
[platform/kernel/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 = 0x01,
59                 .nr_sectors = 128,
60                 .name = "SST25WF512",
61         },{
62                 .idcode1 = 0x02,
63                 .nr_sectors = 256,
64                 .name = "SST25WF010",
65         },{
66                 .idcode1 = 0x03,
67                 .nr_sectors = 512,
68                 .name = "SST25WF020",
69         },{
70                 .idcode1 = 0x04,
71                 .nr_sectors = 1024,
72                 .name = "SST25WF040",
73         },
74 };
75
76 static int
77 sst_wait_ready(struct spi_flash *flash, unsigned long timeout)
78 {
79         struct spi_slave *spi = flash->spi;
80         unsigned long timebase;
81         int ret;
82         u8 byte = CMD_SST_RDSR;
83
84         ret = spi_xfer(spi, sizeof(byte) * 8, &byte, NULL, SPI_XFER_BEGIN);
85         if (ret) {
86                 debug("SF: Failed to send command %02x: %d\n", byte, ret);
87                 return ret;
88         }
89
90         timebase = get_timer(0);
91         do {
92                 ret = spi_xfer(spi, sizeof(byte) * 8, NULL, &byte, 0);
93                 if (ret)
94                         break;
95
96                 if ((byte & SST_SR_WIP) == 0)
97                         break;
98
99         } while (get_timer(timebase) < timeout);
100
101         spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
102
103         if (!ret && (byte & SST_SR_WIP) != 0)
104                 ret = -1;
105
106         if (ret)
107                 debug("SF: sst wait for ready timed out\n");
108         return ret;
109 }
110
111 static int
112 sst_enable_writing(struct spi_flash *flash)
113 {
114         int ret = spi_flash_cmd(flash->spi, CMD_SST_WREN, NULL, 0);
115         if (ret)
116                 debug("SF: Enabling Write failed\n");
117         return ret;
118 }
119
120 static int
121 sst_disable_writing(struct spi_flash *flash)
122 {
123         int ret = spi_flash_cmd(flash->spi, CMD_SST_WRDI, NULL, 0);
124         if (ret)
125                 debug("SF: Disabling Write failed\n");
126         return ret;
127 }
128
129 static int
130 sst_read_fast(struct spi_flash *flash, u32 offset, size_t len, void *buf)
131 {
132         u8 cmd[5] = {
133                 CMD_READ_ARRAY_FAST,
134                 offset >> 16,
135                 offset >> 8,
136                 offset,
137                 0x00,
138         };
139         return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
140 }
141
142 static int
143 sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
144 {
145         int ret;
146         u8 cmd[4] = {
147                 CMD_SST_BP,
148                 offset >> 16,
149                 offset >> 8,
150                 offset,
151         };
152
153         debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
154                 spi_w8r8(flash->spi, CMD_SST_RDSR), buf, cmd[0], offset);
155
156         ret = sst_enable_writing(flash);
157         if (ret)
158                 return ret;
159
160         ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
161         if (ret)
162                 return ret;
163
164         return sst_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
165 }
166
167 static int
168 sst_write(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
169 {
170         size_t actual, cmd_len;
171         int ret;
172         u8 cmd[4];
173
174         ret = spi_claim_bus(flash->spi);
175         if (ret) {
176                 debug("SF: Unable to claim SPI bus\n");
177                 return ret;
178         }
179
180         /* If the data is not word aligned, write out leading single byte */
181         actual = offset % 2;
182         if (actual) {
183                 ret = sst_byte_write(flash, offset, buf);
184                 if (ret)
185                         goto done;
186         }
187         offset += actual;
188
189         ret = sst_enable_writing(flash);
190         if (ret)
191                 goto done;
192
193         cmd_len = 4;
194         cmd[0] = CMD_SST_AAI_WP;
195         cmd[1] = offset >> 16;
196         cmd[2] = offset >> 8;
197         cmd[3] = offset;
198
199         for (; actual < len - 1; actual += 2) {
200                 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
201                      spi_w8r8(flash->spi, CMD_SST_RDSR), buf + actual, cmd[0],
202                      offset);
203
204                 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
205                                           buf + actual, 2);
206                 if (ret) {
207                         debug("SF: sst word program failed\n");
208                         break;
209                 }
210
211                 ret = sst_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
212                 if (ret)
213                         break;
214
215                 cmd_len = 1;
216                 offset += 2;
217         }
218
219         if (!ret)
220                 ret = sst_disable_writing(flash);
221
222         /* If there is a single trailing byte, write it out */
223         if (!ret && actual != len)
224                 ret = sst_byte_write(flash, offset, buf + actual);
225
226  done:
227         debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
228               ret ? "failure" : "success", len, offset - actual);
229
230         spi_release_bus(flash->spi);
231         return ret;
232 }
233
234 int
235 sst_erase(struct spi_flash *flash, u32 offset, size_t len)
236 {
237         unsigned long sector_size;
238         u32 start, end;
239         int ret;
240         u8 cmd[4];
241
242         /*
243          * This function currently uses sector erase only.
244          * Probably speed things up by using bulk erase
245          * when possible.
246          */
247
248         sector_size = SST_SECTOR_SIZE;
249
250         if (offset % sector_size) {
251                 debug("SF: Erase offset not multiple of sector size\n");
252                 return -1;
253         }
254
255         ret = spi_claim_bus(flash->spi);
256         if (ret) {
257                 debug("SF: Unable to claim SPI bus\n");
258                 return ret;
259         }
260
261         cmd[0] = CMD_SST_SE;
262         cmd[3] = 0;
263         start = offset;
264         end = start + len;
265
266         ret = 0;
267         while (offset < end) {
268                 cmd[1] = offset >> 16;
269                 cmd[2] = offset >> 8;
270                 offset += sector_size;
271
272                 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
273                       cmd[2], cmd[3], offset);
274
275                 ret = sst_enable_writing(flash);
276                 if (ret)
277                         break;
278
279                 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
280                 if (ret) {
281                         debug("SF: sst page erase failed\n");
282                         break;
283                 }
284
285                 ret = sst_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
286                 if (ret)
287                         break;
288         }
289
290         debug("SF: sst: Successfully erased %lu bytes @ 0x%x\n",
291               len * sector_size, start);
292
293         spi_release_bus(flash->spi);
294         return ret;
295 }
296
297 static int
298 sst_unlock(struct spi_flash *flash)
299 {
300         int ret;
301         u8 cmd, status;
302
303         ret = sst_enable_writing(flash);
304         if (ret)
305                 return ret;
306
307         cmd = CMD_SST_WRSR;
308         status = 0;
309         ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &status, 1);
310         if (ret)
311                 debug("SF: Unable to set status byte\n");
312
313         debug("SF: sst: status = %x\n", spi_w8r8(flash->spi, CMD_SST_RDSR));
314
315         return ret;
316 }
317
318 struct spi_flash *
319 spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
320 {
321         const struct sst_spi_flash_params *params;
322         struct sst_spi_flash *stm;
323         size_t i;
324
325         for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
326                 params = &sst_spi_flash_table[i];
327                 if (params->idcode1 == idcode[2])
328                         break;
329         }
330
331         if (i == ARRAY_SIZE(sst_spi_flash_table)) {
332                 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
333                 return NULL;
334         }
335
336         stm = malloc(sizeof(*stm));
337         if (!stm) {
338                 debug("SF: Failed to allocate memory\n");
339                 return NULL;
340         }
341
342         stm->params = params;
343         stm->flash.spi = spi;
344         stm->flash.name = params->name;
345
346         stm->flash.write = sst_write;
347         stm->flash.erase = sst_erase;
348         stm->flash.read = sst_read_fast;
349         stm->flash.size = SST_SECTOR_SIZE * params->nr_sectors;
350
351         debug("SF: Detected %s with page size %u, total %u bytes\n",
352               params->name, SST_SECTOR_SIZE, stm->flash.size);
353
354         /* Flash powers up read-only, so clear BP# bits */
355         sst_unlock(&stm->flash);
356
357         return &stm->flash;
358 }