blob: 9b676ae61f493f87a8a2ef40c7dcb36fefc38073 [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>
Zheng Bao600784e2013-02-07 17:30:23 +080012#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070013#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"
Dave Frodinc50c0ab2014-06-11 12:53:47 -060019#include <timer.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070020
21static void spi_flash_addr(u32 addr, u8 *cmd)
22{
23 /* cmd[0] is actual command */
24 cmd[1] = addr >> 16;
25 cmd[2] = addr >> 8;
26 cmd[3] = addr >> 0;
27}
28
29int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
30{
31 int ret = spi_xfer(spi, &cmd, 8, response, len * 8);
32 if (ret)
33 printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret);
34
35 return ret;
36}
37
38int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
39 size_t cmd_len, void *data, size_t data_len)
40{
41 int ret = spi_xfer(spi, cmd, cmd_len * 8, data, data_len * 8);
42 if (ret) {
43 printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n",
44 data_len, ret);
45 }
46
47 return ret;
48}
49
50int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
51 const void *data, size_t data_len)
52{
53 int ret;
54 u8 buff[cmd_len + data_len];
55 memcpy(buff, cmd, cmd_len);
56 memcpy(buff + cmd_len, data, data_len);
57
58 ret = spi_xfer(spi, buff, (cmd_len + data_len) * 8, NULL, 0);
59 if (ret) {
60 printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n",
61 data_len, ret);
62 }
63
64 return ret;
65}
66
67int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
68 size_t cmd_len, void *data, size_t data_len)
69{
70 struct spi_slave *spi = flash->spi;
71 int ret;
72
Martin Roth3316cf22012-12-05 16:22:54 -070073 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070074 spi_claim_bus(spi);
75 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
76 spi_release_bus(spi);
77
78 return ret;
79}
80
81int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
82 size_t len, void *data)
83{
84 struct spi_slave *spi = flash->spi;
85 u8 cmd[5];
86
87 cmd[0] = CMD_READ_ARRAY_FAST;
88 spi_flash_addr(offset, cmd);
89 cmd[4] = 0x00;
90
91 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
92}
93
94int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset,
95 size_t len, void *data)
96{
97 struct spi_slave *spi = flash->spi;
98 u8 cmd[4];
99
100 cmd[0] = CMD_READ_ARRAY_SLOW;
101 spi_flash_addr(offset, cmd);
102
103 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
104}
105
106int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
107 u8 cmd, u8 poll_bit)
108{
109 struct spi_slave *spi = flash->spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700110 int ret;
111 u8 status;
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600112 struct mono_time current, end;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700113
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600114 timer_monotonic_get(&current);
115 end = current;
116 mono_time_add_msecs(&end, timeout);
117
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700118 do {
119 ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1);
120 if (ret)
121 return -1;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700122 if ((status & poll_bit) == 0)
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600123 return 0;
124 timer_monotonic_get(&current);
125 } while (!mono_time_after(&current, &end));
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700126
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600127 printk(BIOS_DEBUG, "SF: timeout at %ld msec\n",timeout);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700128 return -1;
129}
130
131int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
132{
133 return spi_flash_cmd_poll_bit(flash, timeout,
134 CMD_READ_STATUS, STATUS_WIP);
135}
136
137int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
138 u32 offset, size_t len)
139{
140 u32 start, end, erase_size;
141 int ret;
142 u8 cmd[4];
143
144 erase_size = flash->sector_size;
145 if (offset % erase_size || len % erase_size) {
146 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
147 return -1;
148 }
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 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 */
Idwer Vollering73a10182014-02-16 00:32:13 +0000219#if CONFIG_SPI_FLASH_AMIC
220 { 0, 0x37, spi_flash_probe_amic, },
221#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700222#if CONFIG_SPI_FLASH_EON
223 { 0, 0x1c, spi_flash_probe_eon, },
224#endif
Martin Rothbceaf7f2012-09-07 15:02:35 -0600225#if CONFIG_SPI_FLASH_GIGADEVICE
226 { 0, 0xc8, spi_flash_probe_gigadevice, },
227#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700228#if CONFIG_SPI_FLASH_MACRONIX
229 { 0, 0xc2, spi_flash_probe_macronix, },
230#endif
231#if CONFIG_SPI_FLASH_SPANSION
232 { 0, 0x01, spi_flash_probe_spansion, },
233#endif
234#if CONFIG_SPI_FLASH_SST
235 { 0, 0xbf, spi_flash_probe_sst, },
236#endif
237#if CONFIG_SPI_FLASH_STMICRO
238 { 0, 0x20, spi_flash_probe_stmicro, },
239#endif
240#if CONFIG_SPI_FLASH_WINBOND
241 { 0, 0xef, spi_flash_probe_winbond, },
242#endif
243 /* Keep it sorted by best detection */
244#if CONFIG_SPI_FLASH_STMICRO
245 { 0, 0xff, spi_flash_probe_stmicro, },
246#endif
Chris Douglassb34739b2014-02-14 13:51:26 -0500247#if CONFIG_SPI_FLASH_ADESTO
248 { 0, 0x1f, spi_flash_probe_adesto, },
249#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700250};
251#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
252
Gabe Black1e187352014-03-27 20:37:03 -0700253struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700254{
255 struct spi_slave *spi;
256 struct spi_flash *flash = NULL;
257 int ret, i, shift;
258 u8 idcode[IDCODE_LEN], *idp;
259
Gabe Black1e187352014-03-27 20:37:03 -0700260 spi = spi_setup_slave(bus, cs);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700261 if (!spi) {
262 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
263 return NULL;
264 }
265
Martin Roth3316cf22012-12-05 16:22:54 -0700266 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700267 ret = spi_claim_bus(spi);
268 if (ret) {
269 printk(BIOS_WARNING, "SF: Failed to claim SPI bus: %d\n", ret);
270 goto err_claim_bus;
271 }
272
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100273 if (spi->force_programmer_specific && spi->programmer_specific_probe) {
274 flash = spi->programmer_specific_probe (spi);
275 if (!flash)
276 goto err_read_id;
277 goto flash_detected;
278 }
279
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700280 /* Read the ID codes */
281 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
282 if (ret)
283 goto err_read_id;
284
285#if CONFIG_DEBUG_SPI_FLASH
286 printk(BIOS_SPEW, "SF: Got idcodes\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700287#endif
288
289 /* count the number of continuation bytes */
290 for (shift = 0, idp = idcode;
291 shift < IDCODE_CONT_LEN && *idp == 0x7f;
292 ++shift, ++idp)
293 continue;
294
295 /* search the table for matches in shift and id */
296 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
297 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
Duncan Laurie23b00532012-10-10 14:21:23 -0700298#if CONFIG_SMM_TSEG && defined(__SMM__)
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700299 /* Need to relocate this function */
300 tseg_relocate((void **)&flashes[i].probe);
301#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700302 /* we have a match, call probe */
303 flash = flashes[i].probe(spi, idp);
304 if (flash)
305 break;
306 }
307
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100308 if (!flash && spi->programmer_specific_probe) {
309#if CONFIG_SMM_TSEG && defined(__SMM__)
310 /* Need to relocate this function */
311 tseg_relocate((void **)&spi->programmer_specific_probe);
312#endif
313 flash = spi->programmer_specific_probe (spi);
314 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700315 if (!flash) {
316 printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp);
317 goto err_manufacturer_probe;
318 }
319
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100320flash_detected:
Duncan Laurie23b00532012-10-10 14:21:23 -0700321#if CONFIG_SMM_TSEG && defined(__SMM__)
322 /* Ensure flash handlers are valid for TSEG */
323 tseg_relocate((void **)&flash->read);
324 tseg_relocate((void **)&flash->write);
325 tseg_relocate((void **)&flash->erase);
326 tseg_relocate((void **)&flash->name);
327#endif
328
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700329 printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n",
330 flash->name, flash->sector_size, flash->size);
331
332 spi_release_bus(spi);
333
334 return flash;
335
336err_manufacturer_probe:
337err_read_id:
338 spi_release_bus(spi);
339err_claim_bus:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700340 return NULL;
341}