]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/spi/sst.c
sf: Fix code cleanups
[karo-tx-uboot.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_BP              0x02    /* Byte Program */
22 #define CMD_SST_AAI_WP          0xAD    /* Auto Address Incr Word Program */
23
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
32 #define SST_FEAT_WP             (1 << 0)        /* Supports AAI word program */
33 #define SST_FEAT_MBP            (1 << 1)        /* Supports multibyte program */
34
35 struct sst_spi_flash_params {
36         u8 idcode1;
37         u8 flags;
38         u16 nr_sectors;
39         const char *name;
40 };
41
42 struct sst_spi_flash {
43         struct spi_flash flash;
44         const struct sst_spi_flash_params *params;
45 };
46
47 static const struct sst_spi_flash_params sst_spi_flash_table[] = {
48         {
49                 .idcode1 = 0x8d,
50                 .flags = SST_FEAT_WP,
51                 .nr_sectors = 128,
52                 .name = "SST25VF040B",
53         },
54         {
55                 .idcode1 = 0x8e,
56                 .flags = SST_FEAT_WP,
57                 .nr_sectors = 256,
58                 .name = "SST25VF080B",
59         },
60         {
61                 .idcode1 = 0x41,
62                 .flags = SST_FEAT_WP,
63                 .nr_sectors = 512,
64                 .name = "SST25VF016B",
65         },
66         {
67                 .idcode1 = 0x4a,
68                 .flags = SST_FEAT_WP,
69                 .nr_sectors = 1024,
70                 .name = "SST25VF032B",
71         },
72         {
73                 .idcode1 = 0x4b,
74                 .flags = SST_FEAT_MBP,
75                 .nr_sectors = 2048,
76                 .name = "SST25VF064C",
77         },
78         {
79                 .idcode1 = 0x01,
80                 .flags = SST_FEAT_WP,
81                 .nr_sectors = 16,
82                 .name = "SST25WF512",
83         },
84         {
85                 .idcode1 = 0x02,
86                 .flags = SST_FEAT_WP,
87                 .nr_sectors = 32,
88                 .name = "SST25WF010",
89         },
90         {
91                 .idcode1 = 0x03,
92                 .flags = SST_FEAT_WP,
93                 .nr_sectors = 64,
94                 .name = "SST25WF020",
95         },
96         {
97                 .idcode1 = 0x04,
98                 .flags = SST_FEAT_WP,
99                 .nr_sectors = 128,
100                 .name = "SST25WF040",
101         },
102 };
103
104 static int
105 sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
106 {
107         int ret;
108         u8 cmd[4] = {
109                 CMD_SST_BP,
110                 offset >> 16,
111                 offset >> 8,
112                 offset,
113         };
114
115         debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
116               spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
117
118         ret = spi_flash_cmd_write_enable(flash);
119         if (ret)
120                 return ret;
121
122         ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
123         if (ret)
124                 return ret;
125
126         return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
127 }
128
129 static int
130 sst_write_wp(struct spi_flash *flash, u32 offset, size_t len, const void *buf)
131 {
132         size_t actual, cmd_len;
133         int ret;
134         u8 cmd[4];
135
136         ret = spi_claim_bus(flash->spi);
137         if (ret) {
138                 debug("SF: Unable to claim SPI bus\n");
139                 return ret;
140         }
141
142         /* If the data is not word aligned, write out leading single byte */
143         actual = offset % 2;
144         if (actual) {
145                 ret = sst_byte_write(flash, offset, buf);
146                 if (ret)
147                         goto done;
148         }
149         offset += actual;
150
151         ret = spi_flash_cmd_write_enable(flash);
152         if (ret)
153                 goto done;
154
155         cmd_len = 4;
156         cmd[0] = CMD_SST_AAI_WP;
157         cmd[1] = offset >> 16;
158         cmd[2] = offset >> 8;
159         cmd[3] = offset;
160
161         for (; actual < len - 1; actual += 2) {
162                 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
163                       spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
164                       cmd[0], offset);
165
166                 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
167                                         buf + actual, 2);
168                 if (ret) {
169                         debug("SF: sst word program failed\n");
170                         break;
171                 }
172
173                 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
174                 if (ret)
175                         break;
176
177                 cmd_len = 1;
178                 offset += 2;
179         }
180
181         if (!ret)
182                 ret = spi_flash_cmd_write_disable(flash);
183
184         /* If there is a single trailing byte, write it out */
185         if (!ret && actual != len)
186                 ret = sst_byte_write(flash, offset, buf + actual);
187
188  done:
189         debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
190               ret ? "failure" : "success", len, offset - actual);
191
192         spi_release_bus(flash->spi);
193         return ret;
194 }
195
196 struct spi_flash *
197 spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode)
198 {
199         const struct sst_spi_flash_params *params;
200         struct sst_spi_flash *stm;
201         size_t i;
202
203         for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) {
204                 params = &sst_spi_flash_table[i];
205                 if (params->idcode1 == idcode[2])
206                         break;
207         }
208
209         if (i == ARRAY_SIZE(sst_spi_flash_table)) {
210                 debug("SF: Unsupported SST ID %02x\n", idcode[1]);
211                 return NULL;
212         }
213
214         stm = spi_flash_alloc(struct sst_spi_flash, spi, params->name);
215         if (!stm) {
216                 debug("SF: Failed to allocate memory\n");
217                 return NULL;
218         }
219
220         stm->params = params;
221
222         if (stm->params->flags & SST_FEAT_WP)
223                 stm->flash.write = sst_write_wp;
224         stm->flash.page_size = 256;
225         stm->flash.sector_size = 4096;
226         stm->flash.size = stm->flash.sector_size * params->nr_sectors;
227
228         /* Flash powers up read-only, so clear BP# bits */
229         spi_flash_cmd_write_status(&stm->flash, 0);
230
231         return &stm->flash;
232 }