]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mtd/spi/spansion.c
b99bd13e7e493a8e94fb381828041915b41a770e
[people/ms/u-boot.git] / drivers / mtd / spi / spansion.c
1 /*
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_SE 0xd8 /* Sector Erase */
36 #define CMD_S25FLXX_BE 0xc7 /* Bulk Erase */
37
38 #define SPSN_ID_S25FL008A 0x0213
39 #define SPSN_ID_S25FL016A 0x0214
40 #define SPSN_ID_S25FL032A 0x0215
41 #define SPSN_ID_S25FL064A 0x0216
42 #define SPSN_ID_S25FL128P 0x2018
43 #define SPSN_EXT_ID_S25FL128P_256KB 0x0300
44 #define SPSN_EXT_ID_S25FL128P_64KB 0x0301
45 #define SPSN_EXT_ID_S25FL032P 0x4d00
46 #define SPSN_EXT_ID_S25FL129P 0x4d01
47
48 struct spansion_spi_flash_params {
49 u16 idcode1;
50 u16 idcode2;
51 u16 page_size;
52 u16 pages_per_sector;
53 u16 nr_sectors;
54 const char *name;
55 };
56
57 static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
58 {
59 .idcode1 = SPSN_ID_S25FL008A,
60 .idcode2 = 0,
61 .page_size = 256,
62 .pages_per_sector = 256,
63 .nr_sectors = 16,
64 .name = "S25FL008A",
65 },
66 {
67 .idcode1 = SPSN_ID_S25FL016A,
68 .idcode2 = 0,
69 .page_size = 256,
70 .pages_per_sector = 256,
71 .nr_sectors = 32,
72 .name = "S25FL016A",
73 },
74 {
75 .idcode1 = SPSN_ID_S25FL032A,
76 .idcode2 = 0,
77 .page_size = 256,
78 .pages_per_sector = 256,
79 .nr_sectors = 64,
80 .name = "S25FL032A",
81 },
82 {
83 .idcode1 = SPSN_ID_S25FL064A,
84 .idcode2 = 0,
85 .page_size = 256,
86 .pages_per_sector = 256,
87 .nr_sectors = 128,
88 .name = "S25FL064A",
89 },
90 {
91 .idcode1 = SPSN_ID_S25FL128P,
92 .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
93 .page_size = 256,
94 .pages_per_sector = 256,
95 .nr_sectors = 256,
96 .name = "S25FL128P_64K",
97 },
98 {
99 .idcode1 = SPSN_ID_S25FL128P,
100 .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
101 .page_size = 256,
102 .pages_per_sector = 1024,
103 .nr_sectors = 64,
104 .name = "S25FL128P_256K",
105 },
106 {
107 .idcode1 = SPSN_ID_S25FL032A,
108 .idcode2 = SPSN_EXT_ID_S25FL032P,
109 .page_size = 256,
110 .pages_per_sector = 256,
111 .nr_sectors = 64,
112 .name = "S25FL032P",
113 },
114 {
115 .idcode1 = SPSN_ID_S25FL128P,
116 .idcode2 = SPSN_EXT_ID_S25FL129P,
117 .page_size = 256,
118 .pages_per_sector = 256,
119 .nr_sectors = 256,
120 .name = "S25FL129P_64K",
121 },
122 };
123
124 static int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
125 {
126 return spi_flash_cmd_erase(flash, CMD_S25FLXX_SE, offset, len);
127 }
128
129 struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
130 {
131 const struct spansion_spi_flash_params *params;
132 struct spi_flash *flash;
133 unsigned int i;
134 unsigned short jedec, ext_jedec;
135
136 jedec = idcode[1] << 8 | idcode[2];
137 ext_jedec = idcode[3] << 8 | idcode[4];
138
139 for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
140 params = &spansion_spi_flash_table[i];
141 if (params->idcode1 == jedec) {
142 if (params->idcode2 == ext_jedec)
143 break;
144 }
145 }
146
147 if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
148 debug("SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
149 return NULL;
150 }
151
152 flash = malloc(sizeof(*flash));
153 if (!flash) {
154 debug("SF: Failed to allocate memory\n");
155 return NULL;
156 }
157
158 flash->spi = spi;
159 flash->name = params->name;
160
161 flash->write = spi_flash_cmd_write_multi;
162 flash->erase = spansion_erase;
163 flash->read = spi_flash_cmd_read_fast;
164 flash->page_size = params->page_size;
165 flash->sector_size = params->page_size * params->pages_per_sector;
166 flash->size = flash->sector_size * params->nr_sectors;
167
168 return flash;
169 }