blob: 607fb214a80f7f43dc096b9302edd5053e357a22 [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
Edward O'Callaghanc4561e22014-06-26 15:02:40 +100010#include <cpu/x86/smm.h>
11#include <delay.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070012#include <stdlib.h>
13#include <string.h>
Zheng Bao600784e2013-02-07 17:30:23 +080014#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070015#include <spi_flash.h>
Edward O'Callaghanc4561e22014-06-26 15:02:40 +100016
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070017#include "spi_flash_internal.h"
Dave Frodinc50c0ab2014-06-11 12:53:47 -060018#include <timer.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070019
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
David Hendricksf101bbe2014-03-21 19:13:34 -070028/*
29 * If atomic sequencing is used, the cycle type is known to the SPI
30 * controller so that it can perform consecutive transfers and arbitrate
31 * automatically. Otherwise the SPI controller transfers whatever the
32 * user requests immediately, without regard to sequence. Atomic
33 * sequencing is commonly used on x86 platforms.
34 *
35 * SPI flash commands are simple two-step sequences. The command byte is
36 * always written first and may be followed by an address. Then data is
37 * either read or written. For atomic sequencing we'll pass everything into
38 * spi_xfer() at once and let the controller handle the details. Otherwise
39 * we will write all output bytes first and then read if necessary.
40 *
41 * FIXME: This really should be abstracted better, but that will
42 * require overhauling the entire SPI infrastructure.
43 */
44static int do_spi_flash_cmd(struct spi_slave *spi, const void *dout,
45 unsigned int bytes_out, void *din, unsigned int bytes_in)
46{
47 int ret = 1;
48
David Hendricks032c8432014-04-11 19:48:55 -070049 if (spi_claim_bus(spi))
50 return ret;
51
David Hendricksf101bbe2014-03-21 19:13:34 -070052#if CONFIG_SPI_ATOMIC_SEQUENCING == 1
53 if (spi_xfer(spi, dout, bytes_out, din, bytes_in) < 0)
54 goto done;
55#else
56 if (dout && bytes_out) {
57 if (spi_xfer(spi, dout, bytes_out, NULL, 0) < 0)
58 goto done;
59 }
60
61 if (din && bytes_in) {
62 if (spi_xfer(spi, NULL, 0, din, bytes_in) < 0)
63 goto done;
64 }
65#endif
66
67 ret = 0;
68done:
David Hendricks032c8432014-04-11 19:48:55 -070069 spi_release_bus(spi);
David Hendricksf101bbe2014-03-21 19:13:34 -070070 return ret;
71}
72
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070073int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
74{
David Hendricksf101bbe2014-03-21 19:13:34 -070075 int ret = do_spi_flash_cmd(spi, &cmd, sizeof(cmd), response, len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070076 if (ret)
77 printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret);
78
79 return ret;
80}
81
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -080082static int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
83 size_t cmd_len, void *data, size_t data_len)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070084{
David Hendricksf101bbe2014-03-21 19:13:34 -070085 int ret = do_spi_flash_cmd(spi, cmd, cmd_len, data, data_len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070086 if (ret) {
87 printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n",
88 data_len, ret);
89 }
90
91 return ret;
92}
93
94int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
95 const void *data, size_t data_len)
96{
97 int ret;
98 u8 buff[cmd_len + data_len];
99 memcpy(buff, cmd, cmd_len);
100 memcpy(buff + cmd_len, data, data_len);
101
David Hendricksf101bbe2014-03-21 19:13:34 -0700102 ret = do_spi_flash_cmd(spi, buff, cmd_len + data_len, NULL, 0);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700103 if (ret) {
104 printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n",
105 data_len, ret);
106 }
107
108 return ret;
109}
110
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800111static int spi_flash_cmd_read_array(struct spi_slave *spi, u8 *cmd,
112 size_t cmd_len, u32 offset,
113 size_t len, void *data)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700114{
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800115 while (len) {
116 size_t transfer_size;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700117
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800118 if (spi->max_transfer_size)
119 transfer_size = min(len, spi->max_transfer_size);
120 else
121 transfer_size = len;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700122
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800123 spi_flash_addr(offset, cmd);
124
125 if (spi_flash_cmd_read(spi, cmd, cmd_len, data, transfer_size))
126 break;
127
128 offset += transfer_size;
129 data = (void *)((uintptr_t)data + transfer_size);
130 len -= transfer_size;
131 }
132
133 return len != 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700134}
135
136int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
137 size_t len, void *data)
138{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700139 u8 cmd[5];
140
141 cmd[0] = CMD_READ_ARRAY_FAST;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700142 cmd[4] = 0x00;
143
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800144 return spi_flash_cmd_read_array(flash->spi, cmd, sizeof(cmd),
145 offset, len, data);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700146}
147
148int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset,
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800149 size_t len, void *data)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700150{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700151 u8 cmd[4];
152
153 cmd[0] = CMD_READ_ARRAY_SLOW;
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800154 return spi_flash_cmd_read_array(flash->spi, cmd, sizeof(cmd),
155 offset, len, data);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700156}
157
158int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
159 u8 cmd, u8 poll_bit)
160{
161 struct spi_slave *spi = flash->spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700162 int ret;
163 u8 status;
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600164 struct mono_time current, end;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700165
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600166 timer_monotonic_get(&current);
167 end = current;
168 mono_time_add_msecs(&end, timeout);
169
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700170 do {
171 ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1);
172 if (ret)
173 return -1;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700174 if ((status & poll_bit) == 0)
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600175 return 0;
176 timer_monotonic_get(&current);
177 } while (!mono_time_after(&current, &end));
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700178
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600179 printk(BIOS_DEBUG, "SF: timeout at %ld msec\n",timeout);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700180 return -1;
181}
182
183int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
184{
185 return spi_flash_cmd_poll_bit(flash, timeout,
186 CMD_READ_STATUS, STATUS_WIP);
187}
188
189int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
190 u32 offset, size_t len)
191{
192 u32 start, end, erase_size;
193 int ret;
194 u8 cmd[4];
195
196 erase_size = flash->sector_size;
197 if (offset % erase_size || len % erase_size) {
198 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
199 return -1;
200 }
201
Martin Roth3316cf22012-12-05 16:22:54 -0700202 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700203
204 cmd[0] = erase_cmd;
205 start = offset;
206 end = start + len;
207
208 while (offset < end) {
209 spi_flash_addr(offset, cmd);
210 offset += erase_size;
211
Marc Jones747127d2012-12-03 22:16:29 -0700212#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700213 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
214 cmd[2], cmd[3], offset);
Marc Jones747127d2012-12-03 22:16:29 -0700215#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700216 ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
217 if (ret)
218 goto out;
219
220 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
221 if (ret)
222 goto out;
223
224 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
225 if (ret)
226 goto out;
227 }
228
229 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
230
Patrick Georgi20959ba2012-05-12 23:30:36 +0200231out:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700232 return ret;
233}
234
235/*
236 * The following table holds all device probe functions
237 *
238 * shift: number of continuation bytes before the ID
239 * idcode: the expected IDCODE or 0xff for non JEDEC devices
240 * probe: the function to call
241 *
242 * Non JEDEC devices should be ordered in the table such that
243 * the probe functions with best detection algorithms come first.
244 *
245 * Several matching entries are permitted, they will be tried
246 * in sequence until a probe function returns non NULL.
247 *
248 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
249 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
250 * changed. This is the max number of bytes probe functions may
251 * examine when looking up part-specific identification info.
252 *
253 * Probe functions will be given the idcode buffer starting at their
254 * manu id byte (the "idcode" in the table below). In other words,
255 * all of the continuation bytes will be skipped (the "shift" below).
256 */
257#define IDCODE_CONT_LEN 0
258#define IDCODE_PART_LEN 5
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700259static struct {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700260 const u8 shift;
261 const u8 idcode;
262 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
263} flashes[] = {
264 /* Keep it sorted by define name */
Idwer Vollering73a10182014-02-16 00:32:13 +0000265#if CONFIG_SPI_FLASH_AMIC
266 { 0, 0x37, spi_flash_probe_amic, },
267#endif
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200268#if CONFIG_SPI_FLASH_ATMEL
269 { 0, 0x1f, spi_flash_probe_atmel, },
270#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700271#if CONFIG_SPI_FLASH_EON
272 { 0, 0x1c, spi_flash_probe_eon, },
273#endif
Martin Rothbceaf7f2012-09-07 15:02:35 -0600274#if CONFIG_SPI_FLASH_GIGADEVICE
275 { 0, 0xc8, spi_flash_probe_gigadevice, },
276#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700277#if CONFIG_SPI_FLASH_MACRONIX
278 { 0, 0xc2, spi_flash_probe_macronix, },
279#endif
280#if CONFIG_SPI_FLASH_SPANSION
281 { 0, 0x01, spi_flash_probe_spansion, },
282#endif
283#if CONFIG_SPI_FLASH_SST
284 { 0, 0xbf, spi_flash_probe_sst, },
285#endif
286#if CONFIG_SPI_FLASH_STMICRO
287 { 0, 0x20, spi_flash_probe_stmicro, },
288#endif
289#if CONFIG_SPI_FLASH_WINBOND
290 { 0, 0xef, spi_flash_probe_winbond, },
291#endif
292 /* Keep it sorted by best detection */
293#if CONFIG_SPI_FLASH_STMICRO
294 { 0, 0xff, spi_flash_probe_stmicro, },
295#endif
Chris Douglassb34739b2014-02-14 13:51:26 -0500296#if CONFIG_SPI_FLASH_ADESTO
297 { 0, 0x1f, spi_flash_probe_adesto, },
298#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700299};
300#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
301
Gabe Black1e187352014-03-27 20:37:03 -0700302struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700303{
304 struct spi_slave *spi;
305 struct spi_flash *flash = NULL;
306 int ret, i, shift;
307 u8 idcode[IDCODE_LEN], *idp;
308
Gabe Black1e187352014-03-27 20:37:03 -0700309 spi = spi_setup_slave(bus, cs);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700310 if (!spi) {
311 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
312 return NULL;
313 }
314
Martin Roth3316cf22012-12-05 16:22:54 -0700315 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700316
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100317 if (spi->force_programmer_specific && spi->programmer_specific_probe) {
318 flash = spi->programmer_specific_probe (spi);
319 if (!flash)
320 goto err_read_id;
321 goto flash_detected;
322 }
323
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700324 /* Read the ID codes */
325 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
326 if (ret)
327 goto err_read_id;
328
329#if CONFIG_DEBUG_SPI_FLASH
David Hendricksb598bb32014-03-21 19:32:09 -0700330 printk(BIOS_SPEW, "SF: Got idcode: ");
331 for (i = 0; i < sizeof(idcode); i++)
332 printk(BIOS_SPEW, "%02x ", idcode[i]);
333 printk(BIOS_SPEW, "\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700334#endif
335
336 /* count the number of continuation bytes */
337 for (shift = 0, idp = idcode;
338 shift < IDCODE_CONT_LEN && *idp == 0x7f;
339 ++shift, ++idp)
340 continue;
341
342 /* search the table for matches in shift and id */
343 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
344 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
Duncan Laurie23b00532012-10-10 14:21:23 -0700345#if CONFIG_SMM_TSEG && defined(__SMM__)
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700346 /* Need to relocate this function */
347 tseg_relocate((void **)&flashes[i].probe);
348#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700349 /* we have a match, call probe */
350 flash = flashes[i].probe(spi, idp);
351 if (flash)
352 break;
353 }
354
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100355 if (!flash && spi->programmer_specific_probe) {
356#if CONFIG_SMM_TSEG && defined(__SMM__)
357 /* Need to relocate this function */
358 tseg_relocate((void **)&spi->programmer_specific_probe);
359#endif
360 flash = spi->programmer_specific_probe (spi);
361 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700362 if (!flash) {
363 printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp);
364 goto err_manufacturer_probe;
365 }
366
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100367flash_detected:
Duncan Laurie23b00532012-10-10 14:21:23 -0700368#if CONFIG_SMM_TSEG && defined(__SMM__)
369 /* Ensure flash handlers are valid for TSEG */
370 tseg_relocate((void **)&flash->read);
371 tseg_relocate((void **)&flash->write);
372 tseg_relocate((void **)&flash->erase);
373 tseg_relocate((void **)&flash->name);
374#endif
375
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700376 printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n",
377 flash->name, flash->sector_size, flash->size);
378
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700379 return flash;
380
381err_manufacturer_probe:
382err_read_id:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700383 return NULL;
384}