blob: c0900f978f25710bf86ecab0e62546b1d4d6fad5 [file] [log] [blame]
Mingkai Hu6805e4b2009-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 Janderff0dc2c2010-08-23 15:12:16 +020055#define SPSN_EXT_ID_S25FL032P 0x4d00
Mingkai Hu6805e4b2009-03-31 14:09:41 +080056
57#define SPANSION_SR_WIP (1 << 0) /* Write-in-Progress */
58
59struct spansion_spi_flash_params {
60 u16 idcode1;
61 u16 idcode2;
62 u16 page_size;
63 u16 pages_per_sector;
64 u16 nr_sectors;
65 const char *name;
66};
67
68struct spansion_spi_flash {
69 struct spi_flash flash;
70 const struct spansion_spi_flash_params *params;
71};
72
73static inline struct spansion_spi_flash *to_spansion_spi_flash(struct spi_flash
74 *flash)
75{
76 return container_of(flash, struct spansion_spi_flash, flash);
77}
78
79static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
80 {
81 .idcode1 = SPSN_ID_S25FL008A,
82 .idcode2 = 0,
83 .page_size = 256,
84 .pages_per_sector = 256,
85 .nr_sectors = 16,
86 .name = "S25FL008A",
87 },
88 {
89 .idcode1 = SPSN_ID_S25FL016A,
90 .idcode2 = 0,
91 .page_size = 256,
92 .pages_per_sector = 256,
93 .nr_sectors = 32,
94 .name = "S25FL016A",
95 },
96 {
97 .idcode1 = SPSN_ID_S25FL032A,
98 .idcode2 = 0,
99 .page_size = 256,
100 .pages_per_sector = 256,
101 .nr_sectors = 64,
102 .name = "S25FL032A",
103 },
104 {
105 .idcode1 = SPSN_ID_S25FL064A,
106 .idcode2 = 0,
107 .page_size = 256,
108 .pages_per_sector = 256,
109 .nr_sectors = 128,
110 .name = "S25FL064A",
111 },
112 {
113 .idcode1 = SPSN_ID_S25FL128P,
114 .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
115 .page_size = 256,
116 .pages_per_sector = 256,
117 .nr_sectors = 256,
118 .name = "S25FL128P_64K",
119 },
120 {
121 .idcode1 = SPSN_ID_S25FL128P,
122 .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
123 .page_size = 256,
124 .pages_per_sector = 1024,
125 .nr_sectors = 64,
126 .name = "S25FL128P_256K",
127 },
David Janderff0dc2c2010-08-23 15:12:16 +0200128 {
129 .idcode1 = SPSN_ID_S25FL032A,
130 .idcode2 = SPSN_EXT_ID_S25FL032P,
131 .page_size = 256,
132 .pages_per_sector = 256,
133 .nr_sectors = 64,
134 .name = "S25FL032P",
135 },
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800136};
137
138static int spansion_wait_ready(struct spi_flash *flash, unsigned long timeout)
139{
140 struct spi_slave *spi = flash->spi;
141 unsigned long timebase;
142 int ret;
143 u8 status;
144
145 timebase = get_timer(0);
146 do {
147 ret = spi_flash_cmd(spi, CMD_S25FLXX_RDSR, &status, sizeof(status));
148 if (ret)
149 return -1;
150
151 if ((status & SPANSION_SR_WIP) == 0)
152 break;
153
154 } while (get_timer(timebase) < timeout);
155
156
157 if ((status & SPANSION_SR_WIP) == 0)
158 return 0;
159
160 /* Timed out */
161 return -1;
162}
163
164static int spansion_read_fast(struct spi_flash *flash,
165 u32 offset, size_t len, void *buf)
166{
167 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
168 unsigned long page_addr;
169 unsigned long page_size;
170 u8 cmd[5];
171
172 page_size = spsn->params->page_size;
173 page_addr = offset / page_size;
174
175 cmd[0] = CMD_READ_ARRAY_FAST;
176 cmd[1] = page_addr >> 8;
177 cmd[2] = page_addr;
178 cmd[3] = offset % page_size;
179 cmd[4] = 0x00;
180
181 debug
182 ("READ: 0x%x => cmd = { 0x%02x 0x%02x%02x%02x%02x } len = 0x%x\n",
183 offset, cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], len);
184
185 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
186}
187
188static int spansion_write(struct spi_flash *flash,
189 u32 offset, size_t len, const void *buf)
190{
191 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
192 unsigned long page_addr;
193 unsigned long byte_addr;
194 unsigned long page_size;
195 size_t chunk_len;
196 size_t actual;
197 int ret;
198 u8 cmd[4];
199
200 page_size = spsn->params->page_size;
201 page_addr = offset / page_size;
202 byte_addr = offset % page_size;
203
204 ret = spi_claim_bus(flash->spi);
205 if (ret) {
206 debug("SF: Unable to claim SPI bus\n");
207 return ret;
208 }
209
210 ret = 0;
211 for (actual = 0; actual < len; actual += chunk_len) {
212 chunk_len = min(len - actual, page_size - byte_addr);
213
214 cmd[0] = CMD_S25FLXX_PP;
215 cmd[1] = page_addr >> 8;
216 cmd[2] = page_addr;
217 cmd[3] = byte_addr;
218
219 debug
220 ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
221 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
222
223 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
224 if (ret < 0) {
225 debug("SF: Enabling Write failed\n");
226 break;
227 }
228
229 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
230 buf + actual, chunk_len);
231 if (ret < 0) {
232 debug("SF: SPANSION Page Program failed\n");
233 break;
234 }
235
236 ret = spansion_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
237 if (ret < 0) {
238 debug("SF: SPANSION page programming timed out\n");
239 break;
240 }
241
242 page_addr++;
243 byte_addr = 0;
244 }
245
246 debug("SF: SPANSION: Successfully programmed %u bytes @ 0x%x\n",
247 len, offset);
248
249 spi_release_bus(flash->spi);
250 return ret;
251}
252
253int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
254{
255 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
256 unsigned long sector_size;
257 size_t actual;
258 int ret;
259 u8 cmd[4];
260
261 /*
262 * This function currently uses sector erase only.
263 * probably speed things up by using bulk erase
264 * when possible.
265 */
266
267 sector_size = spsn->params->page_size * spsn->params->pages_per_sector;
268
269 if (offset % sector_size || len % sector_size) {
270 debug("SF: Erase offset/length not multiple of sector size\n");
271 return -1;
272 }
273
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800274 cmd[0] = CMD_S25FLXX_SE;
275 cmd[2] = 0x00;
276 cmd[3] = 0x00;
277
278 ret = spi_claim_bus(flash->spi);
279 if (ret) {
280 debug("SF: Unable to claim SPI bus\n");
281 return ret;
282 }
283
284 ret = 0;
Marc-André Hébertc3cb0922010-08-10 09:02:09 -0400285 for (actual = 0; actual < len; actual += sector_size) {
286 cmd[1] = (offset + actual) >> 16;
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800287
288 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
289 if (ret < 0) {
290 debug("SF: Enabling Write failed\n");
291 break;
292 }
293
294 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
295 if (ret < 0) {
296 debug("SF: SPANSION page erase failed\n");
297 break;
298 }
299
300 /* Up to 2 seconds */
301 ret = spansion_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
302 if (ret < 0) {
303 debug("SF: SPANSION page erase timed out\n");
304 break;
305 }
306 }
307
308 debug("SF: SPANSION: Successfully erased %u bytes @ 0x%x\n",
Marc-André Hébertc3cb0922010-08-10 09:02:09 -0400309 len, offset);
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800310
311 spi_release_bus(flash->spi);
312 return ret;
313}
314
315struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
316{
317 const struct spansion_spi_flash_params *params;
318 struct spansion_spi_flash *spsn;
319 unsigned int i;
320 unsigned short jedec, ext_jedec;
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800321
Mike Frysinger0dcdbb12009-03-28 06:41:09 -0400322 jedec = idcode[1] << 8 | idcode[2];
323 ext_jedec = idcode[3] << 8 | idcode[4];
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800324
325 for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
326 params = &spansion_spi_flash_table[i];
327 if (params->idcode1 == jedec) {
328 if (params->idcode2 == ext_jedec)
329 break;
330 }
331 }
332
333 if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
334 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
335 return NULL;
336 }
337
338 spsn = malloc(sizeof(struct spansion_spi_flash));
339 if (!spsn) {
340 debug("SF: Failed to allocate memory\n");
341 return NULL;
342 }
343
344 spsn->params = params;
345 spsn->flash.spi = spi;
346 spsn->flash.name = params->name;
347
348 spsn->flash.write = spansion_write;
349 spsn->flash.erase = spansion_erase;
350 spsn->flash.read = spansion_read_fast;
351 spsn->flash.size = params->page_size * params->pages_per_sector
352 * params->nr_sectors;
353
Mike Frysingerb376bbb2010-04-29 00:35:12 -0400354 printf("SF: Detected %s with page size %u, total ",
355 params->name, params->page_size);
356 print_size(spsn->flash.size, "\n");
Mingkai Hu6805e4b2009-03-31 14:09:41 +0800357
358 return &spsn->flash;
359}