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