blob: 6dd6df921a3b5e9da4aaa5a5e800b70825bc461c [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
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
Paul Menzela8ae1c62013-02-20 13:21:20 +010019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070020 * 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 <stdlib.h>
29#include <spi_flash.h>
30#include "spi_flash_internal.h"
31
32/* S25FLxx-specific commands */
33#define CMD_S25FLXX_READ 0x03 /* Read Data Bytes */
34#define CMD_S25FLXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
35#define CMD_S25FLXX_READID 0x90 /* Read Manufacture ID and Device ID */
36#define CMD_S25FLXX_WREN 0x06 /* Write Enable */
37#define CMD_S25FLXX_WRDI 0x04 /* Write Disable */
38#define CMD_S25FLXX_RDSR 0x05 /* Read Status Register */
39#define CMD_S25FLXX_WRSR 0x01 /* Write Status Register */
40#define CMD_S25FLXX_PP 0x02 /* Page Program */
41#define CMD_S25FLXX_SE 0xd8 /* Sector Erase */
42#define CMD_S25FLXX_BE 0xc7 /* Bulk Erase */
43#define CMD_S25FLXX_DP 0xb9 /* Deep Power-down */
44#define CMD_S25FLXX_RES 0xab /* Release from DP, and Read Signature */
45
46#define SPSN_ID_S25FL008A 0x0213
47#define SPSN_ID_S25FL016A 0x0214
48#define SPSN_ID_S25FL032A 0x0215
49#define SPSN_ID_S25FL064A 0x0216
50#define SPSN_ID_S25FL128P 0x2018
51#define SPSN_EXT_ID_S25FL128P_256KB 0x0300
52#define SPSN_EXT_ID_S25FL128P_64KB 0x0301
53#define SPSN_EXT_ID_S25FL032P 0x4d00
54
55struct spansion_spi_flash_params {
56 u16 idcode1;
57 u16 idcode2;
58 u16 page_size;
59 u16 pages_per_sector;
60 u16 nr_sectors;
61 const char *name;
62};
63
64struct spansion_spi_flash {
65 struct spi_flash flash;
66 const struct spansion_spi_flash_params *params;
67};
68
69static inline struct spansion_spi_flash *to_spansion_spi_flash(struct spi_flash
70 *flash)
71{
72 return container_of(flash, struct spansion_spi_flash, flash);
73}
74
75static const struct spansion_spi_flash_params spansion_spi_flash_table[] = {
76 {
77 .idcode1 = SPSN_ID_S25FL008A,
78 .idcode2 = 0,
79 .page_size = 256,
80 .pages_per_sector = 256,
81 .nr_sectors = 16,
82 .name = "S25FL008A",
83 },
84 {
85 .idcode1 = SPSN_ID_S25FL016A,
86 .idcode2 = 0,
87 .page_size = 256,
88 .pages_per_sector = 256,
89 .nr_sectors = 32,
90 .name = "S25FL016A",
91 },
92 {
93 .idcode1 = SPSN_ID_S25FL032A,
94 .idcode2 = 0,
95 .page_size = 256,
96 .pages_per_sector = 256,
97 .nr_sectors = 64,
98 .name = "S25FL032A",
99 },
100 {
101 .idcode1 = SPSN_ID_S25FL064A,
102 .idcode2 = 0,
103 .page_size = 256,
104 .pages_per_sector = 256,
105 .nr_sectors = 128,
106 .name = "S25FL064A",
107 },
108 {
109 .idcode1 = SPSN_ID_S25FL128P,
110 .idcode2 = SPSN_EXT_ID_S25FL128P_64KB,
111 .page_size = 256,
112 .pages_per_sector = 256,
113 .nr_sectors = 256,
114 .name = "S25FL128P_64K",
115 },
116 {
117 .idcode1 = SPSN_ID_S25FL128P,
118 .idcode2 = SPSN_EXT_ID_S25FL128P_256KB,
119 .page_size = 256,
120 .pages_per_sector = 1024,
121 .nr_sectors = 64,
122 .name = "S25FL128P_256K",
123 },
124 {
125 .idcode1 = SPSN_ID_S25FL032A,
126 .idcode2 = SPSN_EXT_ID_S25FL032P,
127 .page_size = 256,
128 .pages_per_sector = 256,
129 .nr_sectors = 64,
130 .name = "S25FL032P",
131 },
132};
133
134static int spansion_write(struct spi_flash *flash,
135 u32 offset, size_t len, const void *buf)
136{
137 struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
138 unsigned long page_addr;
139 unsigned long byte_addr;
140 unsigned long page_size;
141 size_t chunk_len;
142 size_t actual;
143 int ret;
144 u8 cmd[4];
145
146 page_size = spsn->params->page_size;
147 page_addr = offset / page_size;
148 byte_addr = offset % page_size;
149
Martin Roth3316cf22012-12-05 16:22:54 -0700150 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700151 ret = spi_claim_bus(flash->spi);
152 if (ret) {
153 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
154 return ret;
155 }
156
157 ret = 0;
158 for (actual = 0; actual < len; actual += chunk_len) {
159 chunk_len = min(len - actual, page_size - byte_addr);
160
161 cmd[0] = CMD_S25FLXX_PP;
162 cmd[1] = page_addr >> 8;
163 cmd[2] = page_addr;
164 cmd[3] = byte_addr;
165
Stefan Reinauer5649b082012-05-23 11:03:29 -0700166#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer8ea5a342012-05-14 13:52:32 -0700167 printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
168 " chunk_len = %zu\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700169 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
Stefan Reinauer5649b082012-05-23 11:03:29 -0700170#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700171
172 ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0);
173 if (ret < 0) {
174 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
175 break;
176 }
177
178 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
179 buf + actual, chunk_len);
180 if (ret < 0) {
181 printk(BIOS_WARNING, "SF: SPANSION Page Program failed\n");
182 break;
183 }
184
185 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
186 if (ret)
187 break;
188
189 page_addr++;
190 byte_addr = 0;
191 }
192
Marc Jones747127d2012-12-03 22:16:29 -0700193#if CONFIG_DEBUG_SPI_FLASH
194 printk(BIOS_SPEW, "SF: SPANSION: Successfully programmed %zu bytes @ 0x%x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700195 len, offset);
Marc Jones747127d2012-12-03 22:16:29 -0700196#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700197
198 spi_release_bus(flash->spi);
199 return ret;
200}
201
202static int spansion_erase(struct spi_flash *flash, u32 offset, size_t len)
203{
204 return spi_flash_cmd_erase(flash, CMD_S25FLXX_SE, offset, len);
205}
206
207struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode)
208{
209 const struct spansion_spi_flash_params *params;
210 struct spansion_spi_flash *spsn;
211 unsigned int i;
212 unsigned short jedec, ext_jedec;
213
214 jedec = idcode[1] << 8 | idcode[2];
215 ext_jedec = idcode[3] << 8 | idcode[4];
216
217 for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) {
218 params = &spansion_spi_flash_table[i];
219 if (params->idcode1 == jedec) {
220 if (params->idcode2 == ext_jedec)
221 break;
222 }
223 }
224
225 if (i == ARRAY_SIZE(spansion_spi_flash_table)) {
226 printk(BIOS_WARNING, "SF: Unsupported SPANSION ID %04x %04x\n", jedec, ext_jedec);
227 return NULL;
228 }
229
230 spsn = malloc(sizeof(struct spansion_spi_flash));
231 if (!spsn) {
232 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
233 return NULL;
234 }
235
236 spsn->params = params;
237 spsn->flash.spi = spi;
238 spsn->flash.name = params->name;
239
240 spsn->flash.write = spansion_write;
241 spsn->flash.erase = spansion_erase;
242 spsn->flash.read = spi_flash_cmd_read_fast;
243 spsn->flash.sector_size = params->page_size * params->pages_per_sector;
244 spsn->flash.size = spsn->flash.sector_size * params->nr_sectors;
245
246 return &spsn->flash;
247}