blob: 75a5c216197aa8443bec4daea915255a920e3f4d [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
72 spi_claim_bus(spi);
73 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
74 spi_release_bus(spi);
75
76 return ret;
77}
78
79int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
80 size_t len, void *data)
81{
82 struct spi_slave *spi = flash->spi;
83 u8 cmd[5];
84
85 cmd[0] = CMD_READ_ARRAY_FAST;
86 spi_flash_addr(offset, cmd);
87 cmd[4] = 0x00;
88
89 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
90}
91
92int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset,
93 size_t len, void *data)
94{
95 struct spi_slave *spi = flash->spi;
96 u8 cmd[4];
97
98 cmd[0] = CMD_READ_ARRAY_SLOW;
99 spi_flash_addr(offset, cmd);
100
101 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
102}
103
104int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
105 u8 cmd, u8 poll_bit)
106{
107 struct spi_slave *spi = flash->spi;
108 unsigned long timebase;
109 int ret;
110 u8 status;
111
112 timebase = timeout;
113 do {
114 ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1);
115 if (ret)
116 return -1;
117
118 if ((status & poll_bit) == 0)
119 break;
120
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700121 udelay(500);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700122 } while (timebase--);
123
124 if ((status & poll_bit) == 0)
125 return 0;
126
127 /* Timed out */
128 printk(BIOS_DEBUG, "SF: time out!\n");
129 return -1;
130}
131
132int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
133{
134 return spi_flash_cmd_poll_bit(flash, timeout,
135 CMD_READ_STATUS, STATUS_WIP);
136}
137
138int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
139 u32 offset, size_t len)
140{
141 u32 start, end, erase_size;
142 int ret;
143 u8 cmd[4];
144
145 erase_size = flash->sector_size;
146 if (offset % erase_size || len % erase_size) {
147 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
148 return -1;
149 }
150
151 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 cmd[0] = erase_cmd;
158 start = offset;
159 end = start + len;
160
161 while (offset < end) {
162 spi_flash_addr(offset, cmd);
163 offset += erase_size;
164
Marc Jones747127d2012-12-03 22:16:29 -0700165#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700166 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
167 cmd[2], cmd[3], offset);
Marc Jones747127d2012-12-03 22:16:29 -0700168#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700169 ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
170 if (ret)
171 goto out;
172
173 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
174 if (ret)
175 goto out;
176
177 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
178 if (ret)
179 goto out;
180 }
181
182 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
183
Patrick Georgi20959ba2012-05-12 23:30:36 +0200184out:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700185 spi_release_bus(flash->spi);
186 return ret;
187}
188
189/*
190 * The following table holds all device probe functions
191 *
192 * shift: number of continuation bytes before the ID
193 * idcode: the expected IDCODE or 0xff for non JEDEC devices
194 * probe: the function to call
195 *
196 * Non JEDEC devices should be ordered in the table such that
197 * the probe functions with best detection algorithms come first.
198 *
199 * Several matching entries are permitted, they will be tried
200 * in sequence until a probe function returns non NULL.
201 *
202 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
203 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
204 * changed. This is the max number of bytes probe functions may
205 * examine when looking up part-specific identification info.
206 *
207 * Probe functions will be given the idcode buffer starting at their
208 * manu id byte (the "idcode" in the table below). In other words,
209 * all of the continuation bytes will be skipped (the "shift" below).
210 */
211#define IDCODE_CONT_LEN 0
212#define IDCODE_PART_LEN 5
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700213static struct {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700214 const u8 shift;
215 const u8 idcode;
216 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
217} flashes[] = {
218 /* Keep it sorted by define name */
219#if CONFIG_SPI_FLASH_EON
220 { 0, 0x1c, spi_flash_probe_eon, },
221#endif
Martin Rothbceaf7f2012-09-07 15:02:35 -0600222#if CONFIG_SPI_FLASH_GIGADEVICE
223 { 0, 0xc8, spi_flash_probe_gigadevice, },
224#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700225#if CONFIG_SPI_FLASH_MACRONIX
226 { 0, 0xc2, spi_flash_probe_macronix, },
227#endif
228#if CONFIG_SPI_FLASH_SPANSION
229 { 0, 0x01, spi_flash_probe_spansion, },
230#endif
231#if CONFIG_SPI_FLASH_SST
232 { 0, 0xbf, spi_flash_probe_sst, },
233#endif
234#if CONFIG_SPI_FLASH_STMICRO
235 { 0, 0x20, spi_flash_probe_stmicro, },
236#endif
237#if CONFIG_SPI_FLASH_WINBOND
238 { 0, 0xef, spi_flash_probe_winbond, },
239#endif
240 /* Keep it sorted by best detection */
241#if CONFIG_SPI_FLASH_STMICRO
242 { 0, 0xff, spi_flash_probe_stmicro, },
243#endif
244};
245#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
246
247struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
248 unsigned int max_hz, unsigned int spi_mode)
249{
250 struct spi_slave *spi;
251 struct spi_flash *flash = NULL;
252 int ret, i, shift;
253 u8 idcode[IDCODE_LEN], *idp;
254
255 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
256 if (!spi) {
257 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
258 return NULL;
259 }
260
261 ret = spi_claim_bus(spi);
262 if (ret) {
263 printk(BIOS_WARNING, "SF: Failed to claim SPI bus: %d\n", ret);
264 goto err_claim_bus;
265 }
266
267 /* Read the ID codes */
268 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
269 if (ret)
270 goto err_read_id;
271
272#if CONFIG_DEBUG_SPI_FLASH
273 printk(BIOS_SPEW, "SF: Got idcodes\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700274#endif
275
276 /* count the number of continuation bytes */
277 for (shift = 0, idp = idcode;
278 shift < IDCODE_CONT_LEN && *idp == 0x7f;
279 ++shift, ++idp)
280 continue;
281
282 /* search the table for matches in shift and id */
283 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
284 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
Duncan Laurie23b00532012-10-10 14:21:23 -0700285#if CONFIG_SMM_TSEG && defined(__SMM__)
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700286 /* Need to relocate this function */
287 tseg_relocate((void **)&flashes[i].probe);
288#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700289 /* we have a match, call probe */
290 flash = flashes[i].probe(spi, idp);
291 if (flash)
292 break;
293 }
294
295 if (!flash) {
296 printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp);
297 goto err_manufacturer_probe;
298 }
299
Duncan Laurie23b00532012-10-10 14:21:23 -0700300#if CONFIG_SMM_TSEG && defined(__SMM__)
301 /* Ensure flash handlers are valid for TSEG */
302 tseg_relocate((void **)&flash->read);
303 tseg_relocate((void **)&flash->write);
304 tseg_relocate((void **)&flash->erase);
305 tseg_relocate((void **)&flash->name);
306#endif
307
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700308 printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n",
309 flash->name, flash->sector_size, flash->size);
310
311 spi_release_bus(spi);
312
313 return flash;
314
315err_manufacturer_probe:
316err_read_id:
317 spi_release_bus(spi);
318err_claim_bus:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700319 return NULL;
320}