blob: 2c0f702e703a27d1f2ffd9eca81d787d5fc03f60 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * SPI flash interface
3 *
4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 *
7 * Licensed under the GPL-2 or later.
8 */
9
10#include <stdlib.h>
11#include <string.h>
12#include <spi.h>
13#include <spi_flash.h>
14#include <delay.h>
15#include "spi_flash_internal.h"
16
17static void spi_flash_addr(u32 addr, u8 *cmd)
18{
19 /* cmd[0] is actual command */
20 cmd[1] = addr >> 16;
21 cmd[2] = addr >> 8;
22 cmd[3] = addr >> 0;
23}
24
25int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
26{
27 int ret = spi_xfer(spi, &cmd, 8, response, len * 8);
28 if (ret)
29 printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret);
30
31 return ret;
32}
33
34int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
35 size_t cmd_len, void *data, size_t data_len)
36{
37 int ret = spi_xfer(spi, cmd, cmd_len * 8, data, data_len * 8);
38 if (ret) {
39 printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n",
40 data_len, ret);
41 }
42
43 return ret;
44}
45
46int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
47 const void *data, size_t data_len)
48{
49 int ret;
50 u8 buff[cmd_len + data_len];
51 memcpy(buff, cmd, cmd_len);
52 memcpy(buff + cmd_len, data, data_len);
53
54 ret = spi_xfer(spi, buff, (cmd_len + data_len) * 8, NULL, 0);
55 if (ret) {
56 printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n",
57 data_len, ret);
58 }
59
60 return ret;
61}
62
63int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
64 size_t cmd_len, void *data, size_t data_len)
65{
66 struct spi_slave *spi = flash->spi;
67 int ret;
68
69 spi_claim_bus(spi);
70 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
71 spi_release_bus(spi);
72
73 return ret;
74}
75
76int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
77 size_t len, void *data)
78{
79 struct spi_slave *spi = flash->spi;
80 u8 cmd[5];
81
82 cmd[0] = CMD_READ_ARRAY_FAST;
83 spi_flash_addr(offset, cmd);
84 cmd[4] = 0x00;
85
86 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
87}
88
89int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset,
90 size_t len, void *data)
91{
92 struct spi_slave *spi = flash->spi;
93 u8 cmd[4];
94
95 cmd[0] = CMD_READ_ARRAY_SLOW;
96 spi_flash_addr(offset, cmd);
97
98 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
99}
100
101int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
102 u8 cmd, u8 poll_bit)
103{
104 struct spi_slave *spi = flash->spi;
105 unsigned long timebase;
106 int ret;
107 u8 status;
108
109 timebase = timeout;
110 do {
111 ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1);
112 if (ret)
113 return -1;
114
115 if ((status & poll_bit) == 0)
116 break;
117
118 mdelay(1);
119 } while (timebase--);
120
121 if ((status & poll_bit) == 0)
122 return 0;
123
124 /* Timed out */
125 printk(BIOS_DEBUG, "SF: time out!\n");
126 return -1;
127}
128
129int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
130{
131 return spi_flash_cmd_poll_bit(flash, timeout,
132 CMD_READ_STATUS, STATUS_WIP);
133}
134
135int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
136 u32 offset, size_t len)
137{
138 u32 start, end, erase_size;
139 int ret;
140 u8 cmd[4];
141
142 erase_size = flash->sector_size;
143 if (offset % erase_size || len % erase_size) {
144 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
145 return -1;
146 }
147
148 ret = spi_claim_bus(flash->spi);
149 if (ret) {
150 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
151 return ret;
152 }
153
154 cmd[0] = erase_cmd;
155 start = offset;
156 end = start + len;
157
158 while (offset < end) {
159 spi_flash_addr(offset, cmd);
160 offset += erase_size;
161
162 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
163 cmd[2], cmd[3], offset);
164
165 ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
166 if (ret)
167 goto out;
168
169 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
170 if (ret)
171 goto out;
172
173 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
174 if (ret)
175 goto out;
176 }
177
178 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
179
Patrick Georgi20959ba2012-05-12 23:30:36 +0200180out:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700181 spi_release_bus(flash->spi);
182 return ret;
183}
184
185/*
186 * The following table holds all device probe functions
187 *
188 * shift: number of continuation bytes before the ID
189 * idcode: the expected IDCODE or 0xff for non JEDEC devices
190 * probe: the function to call
191 *
192 * Non JEDEC devices should be ordered in the table such that
193 * the probe functions with best detection algorithms come first.
194 *
195 * Several matching entries are permitted, they will be tried
196 * in sequence until a probe function returns non NULL.
197 *
198 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
199 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
200 * changed. This is the max number of bytes probe functions may
201 * examine when looking up part-specific identification info.
202 *
203 * Probe functions will be given the idcode buffer starting at their
204 * manu id byte (the "idcode" in the table below). In other words,
205 * all of the continuation bytes will be skipped (the "shift" below).
206 */
207#define IDCODE_CONT_LEN 0
208#define IDCODE_PART_LEN 5
209static const struct {
210 const u8 shift;
211 const u8 idcode;
212 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
213} flashes[] = {
214 /* Keep it sorted by define name */
215#if CONFIG_SPI_FLASH_EON
216 { 0, 0x1c, spi_flash_probe_eon, },
217#endif
218#if CONFIG_SPI_FLASH_MACRONIX
219 { 0, 0xc2, spi_flash_probe_macronix, },
220#endif
221#if CONFIG_SPI_FLASH_SPANSION
222 { 0, 0x01, spi_flash_probe_spansion, },
223#endif
224#if CONFIG_SPI_FLASH_SST
225 { 0, 0xbf, spi_flash_probe_sst, },
226#endif
227#if CONFIG_SPI_FLASH_STMICRO
228 { 0, 0x20, spi_flash_probe_stmicro, },
229#endif
230#if CONFIG_SPI_FLASH_WINBOND
231 { 0, 0xef, spi_flash_probe_winbond, },
232#endif
233 /* Keep it sorted by best detection */
234#if CONFIG_SPI_FLASH_STMICRO
235 { 0, 0xff, spi_flash_probe_stmicro, },
236#endif
237};
238#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
239
240struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
241 unsigned int max_hz, unsigned int spi_mode)
242{
243 struct spi_slave *spi;
244 struct spi_flash *flash = NULL;
245 int ret, i, shift;
246 u8 idcode[IDCODE_LEN], *idp;
247
248 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
249 if (!spi) {
250 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
251 return NULL;
252 }
253
254 ret = spi_claim_bus(spi);
255 if (ret) {
256 printk(BIOS_WARNING, "SF: Failed to claim SPI bus: %d\n", ret);
257 goto err_claim_bus;
258 }
259
260 /* Read the ID codes */
261 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
262 if (ret)
263 goto err_read_id;
264
265#if CONFIG_DEBUG_SPI_FLASH
266 printk(BIOS_SPEW, "SF: Got idcodes\n");
267 print_buffer(0, idcode, 1, sizeof(idcode), 0);
268#endif
269
270 /* count the number of continuation bytes */
271 for (shift = 0, idp = idcode;
272 shift < IDCODE_CONT_LEN && *idp == 0x7f;
273 ++shift, ++idp)
274 continue;
275
276 /* search the table for matches in shift and id */
277 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
278 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
279 /* we have a match, call probe */
280 flash = flashes[i].probe(spi, idp);
281 if (flash)
282 break;
283 }
284
285 if (!flash) {
286 printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp);
287 goto err_manufacturer_probe;
288 }
289
290 printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n",
291 flash->name, flash->sector_size, flash->size);
292
293 spi_release_bus(spi);
294
295 return flash;
296
297err_manufacturer_probe:
298err_read_id:
299 spi_release_bus(spi);
300err_claim_bus:
301 spi_free_slave(spi);
302 return NULL;
303}
304
305void spi_flash_free(struct spi_flash *flash)
306{
307 spi_free_slave(flash->spi);
308 free(flash);
309}