blob: d0f03ffafa2f9b2aa28ef9e69bdbaa4a9258865c [file] [log] [blame]
Mingkai Hu2ba690b2009-03-31 14:09:41 +08001/*
2 * Copyright (C) 2009 Freescale Semiconductor, Inc.
3 *
4 * Author: Mingkai Hu (Mingkai.hu@freescale.com)
5 * Based on stmicro.c by Wolfgang Denk (wd@denx.de),
6 * TsiChung Liew (Tsi-Chung.Liew@freescale.com),
7 * and Jason McMullan (mcmullan@netapp.com)
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <malloc.h>
30#include <spi_flash.h>
31
32#include "spi_flash_internal.h"
33
34/* S25FLxx-specific commands */
35#define CMD_S25FLXX_READ 0x03 /* Read Data Bytes */
36#define CMD_S25FLXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
37#define CMD_S25FLXX_READID 0x90 /* Read Manufacture ID and Device ID */
38#define CMD_S25FLXX_WREN 0x06 /* Write Enable */
39#define CMD_S25FLXX_WRDI 0x04 /* Write Disable */
40#define CMD_S25FLXX_RDSR 0x05 /* Read Status Register */
41#define CMD_S25FLXX_WRSR 0x01 /* Write Status Register */
42#define CMD_S25FLXX_PP 0x02 /* Page Program */
43#define CMD_S25FLXX_SE 0xd8 /* Sector Erase */
44#define CMD_S25FLXX_BE 0xc7 /* Bulk Erase */
45#define CMD_S25FLXX_DP 0xb9 /* Deep Power-down */
46#define CMD_S25FLXX_RES 0xab /* Release from DP, and Read Signature */
47
48#define SPSN_ID_S25FL008A 0x0213
49#define SPSN_ID_S25FL016A 0x0214
50#define SPSN_ID_S25FL032A 0x0215
51#define SPSN_ID_S25FL064A 0x0216
52#define SPSN_ID_S25FL128P 0x2018
53#define SPSN_EXT_ID_S25FL128P_256KB 0x0300
54#define SPSN_EXT_ID_S25FL128P_64KB 0x0301
David Janderd7cc4682010-08-23 15:12:16 +020055#define SPSN_EXT_ID_S25FL032P 0x4d00
Mingkai Hu2ba690b2009-03-31 14:09:41 +080056
Mingkai Hu2ba690b2009-03-31 14:09:41 +080057struct spansion_spi_flash_params {
58 u16 idcode1;
59 u16 idcode2;
60 u16 page_size;
61 u16 pages_per_sector;
62 u16 nr_sectors;
63 const char *name;
64};
65
66struct spansion_spi_flash {
67 struct spi_flash flash;
68 const struct spansion_spi_flash_params *params;
69};
70
71static inline struct spansion_spi_flash *to_spansion_spi_flash(struct spi_flash
72 *flash)
73{
74 return container_of(flash, struct spansion_spi_flash, flash);
75}
76
77static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
78 {
79 .idcode1 = SPSN_ID_S25FL008A,
80 .idcode2 = 0,
81 .page_size = 256,
82 .pages_per_sector = 256,
83 .nr_sectors = 16,
84 .name = "S25FL008A",
85 },
86 {
87 .idcode1 = SPSN_ID_S25FL016A,
88 .idcode2 = 0,
89 .page_size = 256,
90 .pages_per_sector = 256,
91 .nr_sectors = 32,
92 .name = "S25FL016A",
93 },
94 {
95 .idcode1 = SPSN_ID_S25FL032A,
96 .idcode2 = 0,
97 .page_size = 256,
98 .pages_per_sector = 256,
99 .nr_sectors = 64,
100 .name = "S25FL032A",
101 },
102 {
103 .idcode1 = SPSN_ID_S25FL064A,
104 .idcode2 = 0,
105 .page_size = 256,
106 .pages_per_sector = 256,
107 .nr_sectors = 128,
108 .name = "S25FL064A",
109 },
110 {
111 .idcode1 = SPSN_ID_S25FL128P,
112 .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
113 .page_size = 256,
114 .pages_per_sector = 256,
115 .nr_sectors = 256,
116 .name = "S25FL128P_64K",
117 },
118 {
119 .idcode1 = SPSN_ID_S25FL128P,
120 .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
121 .page_size = 256,
122 .pages_per_sector = 1024,
123 .nr_sectors = 64,
124 .name = "S25FL128P_256K",
125 },
David Janderd7cc4682010-08-23 15:12:16 +0200126 {
127 .idcode1 = SPSN_ID_S25FL032A,
128 .idcode2 = SPSN_EXT_ID_S25FL032P,
129 .page_size = 256,
130 .pages_per_sector = 256,
131 .nr_sectors = 64,
132 .name = "S25FL032P",
133 },
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800134};
135
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800136static int spansion_read_fast(struct spi_flash *flash,
137 u32 offset, size_t len, void *buf)
138{
139 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
140 unsigned long page_addr;
141 unsigned long page_size;
142 u8 cmd[5];
143
144 page_size = spsn->params->page_size;
145 page_addr = offset / page_size;
146
147 cmd[0] = CMD_READ_ARRAY_FAST;
148 cmd[1] = page_addr >> 8;
149 cmd[2] = page_addr;
150 cmd[3] = offset % page_size;
151 cmd[4] = 0x00;
152
153 debug
154 ("READ: 0x%x => cmd = { 0x%02x 0x%02x%02x%02x%02x } len = 0x%x\n",
155 offset, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], len);
156
157 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
158}
159
160static int spansion_write(struct spi_flash *flash,
161 u32 offset, size_t len, const void *buf)
162{
163 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
164 unsigned long page_addr;
165 unsigned long byte_addr;
166 unsigned long page_size;
167 size_t chunk_len;
168 size_t actual;
169 int ret;
170 u8 cmd[4];
171
172 page_size = spsn->params->page_size;
173 page_addr = offset / page_size;
174 byte_addr = offset % page_size;
175
176 ret = spi_claim_bus(flash->spi);
177 if (ret) {
178 debug("SF: Unable to claim SPI bus\n");
179 return ret;
180 }
181
182 ret = 0;
183 for (actual = 0; actual < len; actual += chunk_len) {
184 chunk_len = min(len - actual, page_size - byte_addr);
185
186 cmd[0] = CMD_S25FLXX_PP;
187 cmd[1] = page_addr >> 8;
188 cmd[2] = page_addr;
189 cmd[3] = byte_addr;
190
191 debug
192 ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
193 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
194
195 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
196 if (ret < 0) {
197 debug("SF: Enabling Write failed\n");
198 break;
199 }
200
201 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
202 buf + actual, chunk_len);
203 if (ret < 0) {
204 debug("SF: SPANSION Page Program failed\n");
205 break;
206 }
207
Mike Frysinger37e13bc2011-01-10 02:20:12 -0500208 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
209 if (ret)
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800210 break;
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800211
212 page_addr++;
213 byte_addr = 0;
214 }
215
216 debug("SF: SPANSION: Successfully programmed %u bytes @ 0x%x\n",
217 len, offset);
218
219 spi_release_bus(flash->spi);
220 return ret;
221}
222
223int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
224{
225 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
226 unsigned long sector_size;
227 size_t actual;
228 int ret;
229 u8 cmd[4];
230
231 /*
232 * This function currently uses sector erase only.
233 * probably speed things up by using bulk erase
234 * when possible.
235 */
236
237 sector_size = spsn->params->page_size * spsn->params->pages_per_sector;
238
239 if (offset % sector_size || len % sector_size) {
240 debug("SF: Erase offset/length not multiple of sector size\n");
241 return -1;
242 }
243
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800244 cmd[0] = CMD_S25FLXX_SE;
245 cmd[2] = 0x00;
246 cmd[3] = 0x00;
247
248 ret = spi_claim_bus(flash->spi);
249 if (ret) {
250 debug("SF: Unable to claim SPI bus\n");
251 return ret;
252 }
253
254 ret = 0;
Marc-André Hébert8f464922010-08-10 09:02:09 -0400255 for (actual = 0; actual < len; actual += sector_size) {
256 cmd[1] = (offset + actual) >> 16;
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800257
258 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
259 if (ret < 0) {
260 debug("SF: Enabling Write failed\n");
261 break;
262 }
263
264 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
265 if (ret < 0) {
266 debug("SF: SPANSION page erase failed\n");
267 break;
268 }
269
Mike Frysinger37e13bc2011-01-10 02:20:12 -0500270 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
271 if (ret)
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800272 break;
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800273 }
274
275 debug("SF: SPANSION: Successfully erased %u bytes @ 0x%x\n",
Marc-André Hébert8f464922010-08-10 09:02:09 -0400276 len, offset);
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800277
278 spi_release_bus(flash->spi);
279 return ret;
280}
281
282struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
283{
284 const struct spansion_spi_flash_params *params;
285 struct spansion_spi_flash *spsn;
286 unsigned int i;
287 unsigned short jedec, ext_jedec;
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800288
Mike Frysingerf145ce52009-03-28 06:41:09 -0400289 jedec = idcode[1] << 8 | idcode[2];
290 ext_jedec = idcode[3] << 8 | idcode[4];
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800291
292 for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
293 params = &spansion_spi_flash_table[i];
294 if (params->idcode1 == jedec) {
295 if (params->idcode2 == ext_jedec)
296 break;
297 }
298 }
299
300 if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
301 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
302 return NULL;
303 }
304
305 spsn = malloc(sizeof(struct spansion_spi_flash));
306 if (!spsn) {
307 debug("SF: Failed to allocate memory\n");
308 return NULL;
309 }
310
311 spsn->params = params;
312 spsn->flash.spi = spi;
313 spsn->flash.name = params->name;
314
315 spsn->flash.write = spansion_write;
316 spsn->flash.erase = spansion_erase;
317 spsn->flash.read = spansion_read_fast;
318 spsn->flash.size = params->page_size * params->pages_per_sector
319 * params->nr_sectors;
320
Mike Frysinger02110b52010-04-29 00:35:12 -0400321 printf("SF: Detected %s with page size %u, total ",
322 params->name, params->page_size);
323 print_size(spsn->flash.size, "\n");
Mingkai Hu2ba690b2009-03-31 14:09:41 +0800324
325 return &spsn->flash;
326}