blob: d40f72a852f773162a6bcfc2fc5adb5279ccbe35 [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
Dan Ehrenberga5aac762015-01-08 10:29:19 -080010#include <cbfs.h>
Edward O'Callaghanc4561e22014-06-26 15:02:40 +100011#include <cpu/x86/smm.h>
12#include <delay.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070013#include <stdlib.h>
14#include <string.h>
Zheng Bao600784e2013-02-07 17:30:23 +080015#include <spi-generic.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070016#include <spi_flash.h>
Edward O'Callaghanc4561e22014-06-26 15:02:40 +100017
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
Dan Ehrenberga5aac762015-01-08 10:29:19 -080021static struct spi_flash *spi_flash_dev = NULL;
22
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070023static void spi_flash_addr(u32 addr, u8 *cmd)
24{
25 /* cmd[0] is actual command */
26 cmd[1] = addr >> 16;
27 cmd[2] = addr >> 8;
28 cmd[3] = addr >> 0;
29}
30
David Hendricksf101bbe2014-03-21 19:13:34 -070031/*
32 * If atomic sequencing is used, the cycle type is known to the SPI
33 * controller so that it can perform consecutive transfers and arbitrate
34 * automatically. Otherwise the SPI controller transfers whatever the
35 * user requests immediately, without regard to sequence. Atomic
36 * sequencing is commonly used on x86 platforms.
37 *
38 * SPI flash commands are simple two-step sequences. The command byte is
39 * always written first and may be followed by an address. Then data is
40 * either read or written. For atomic sequencing we'll pass everything into
41 * spi_xfer() at once and let the controller handle the details. Otherwise
42 * we will write all output bytes first and then read if necessary.
43 *
44 * FIXME: This really should be abstracted better, but that will
45 * require overhauling the entire SPI infrastructure.
46 */
47static int do_spi_flash_cmd(struct spi_slave *spi, const void *dout,
48 unsigned int bytes_out, void *din, unsigned int bytes_in)
49{
50 int ret = 1;
51
David Hendricks032c8432014-04-11 19:48:55 -070052 if (spi_claim_bus(spi))
53 return ret;
54
David Hendricksf101bbe2014-03-21 19:13:34 -070055#if CONFIG_SPI_ATOMIC_SEQUENCING == 1
56 if (spi_xfer(spi, dout, bytes_out, din, bytes_in) < 0)
57 goto done;
58#else
59 if (dout && bytes_out) {
60 if (spi_xfer(spi, dout, bytes_out, NULL, 0) < 0)
61 goto done;
62 }
63
64 if (din && bytes_in) {
65 if (spi_xfer(spi, NULL, 0, din, bytes_in) < 0)
66 goto done;
67 }
68#endif
69
70 ret = 0;
71done:
David Hendricks032c8432014-04-11 19:48:55 -070072 spi_release_bus(spi);
David Hendricksf101bbe2014-03-21 19:13:34 -070073 return ret;
74}
75
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070076int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
77{
David Hendricksf101bbe2014-03-21 19:13:34 -070078 int ret = do_spi_flash_cmd(spi, &cmd, sizeof(cmd), response, len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070079 if (ret)
80 printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret);
81
82 return ret;
83}
84
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -080085static int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
86 size_t cmd_len, void *data, size_t data_len)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070087{
David Hendricksf101bbe2014-03-21 19:13:34 -070088 int ret = do_spi_flash_cmd(spi, cmd, cmd_len, data, data_len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070089 if (ret) {
90 printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n",
91 data_len, ret);
92 }
93
94 return ret;
95}
96
97int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
98 const void *data, size_t data_len)
99{
100 int ret;
101 u8 buff[cmd_len + data_len];
102 memcpy(buff, cmd, cmd_len);
103 memcpy(buff + cmd_len, data, data_len);
104
David Hendricksf101bbe2014-03-21 19:13:34 -0700105 ret = do_spi_flash_cmd(spi, buff, cmd_len + data_len, NULL, 0);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700106 if (ret) {
107 printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n",
108 data_len, ret);
109 }
110
111 return ret;
112}
113
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800114static int spi_flash_cmd_read_array(struct spi_slave *spi, u8 *cmd,
115 size_t cmd_len, u32 offset,
116 size_t len, void *data)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700117{
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800118 while (len) {
119 size_t transfer_size;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700120
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800121 if (spi->max_transfer_size)
122 transfer_size = min(len, spi->max_transfer_size);
123 else
124 transfer_size = len;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700125
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800126 spi_flash_addr(offset, cmd);
127
128 if (spi_flash_cmd_read(spi, cmd, cmd_len, data, transfer_size))
129 break;
130
131 offset += transfer_size;
132 data = (void *)((uintptr_t)data + transfer_size);
133 len -= transfer_size;
134 }
135
136 return len != 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700137}
138
139int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
140 size_t len, void *data)
141{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700142 u8 cmd[5];
143
144 cmd[0] = CMD_READ_ARRAY_FAST;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700145 cmd[4] = 0x00;
146
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800147 return spi_flash_cmd_read_array(flash->spi, cmd, sizeof(cmd),
148 offset, len, data);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700149}
150
151int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset,
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800152 size_t len, void *data)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700153{
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700154 u8 cmd[4];
155
156 cmd[0] = CMD_READ_ARRAY_SLOW;
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -0800157 return spi_flash_cmd_read_array(flash->spi, cmd, sizeof(cmd),
158 offset, len, data);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700159}
160
161int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
162 u8 cmd, u8 poll_bit)
163{
164 struct spi_slave *spi = flash->spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700165 int ret;
166 u8 status;
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600167 struct mono_time current, end;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700168
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600169 timer_monotonic_get(&current);
170 end = current;
171 mono_time_add_msecs(&end, timeout);
172
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700173 do {
174 ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1);
175 if (ret)
176 return -1;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700177 if ((status & poll_bit) == 0)
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600178 return 0;
179 timer_monotonic_get(&current);
180 } while (!mono_time_after(&current, &end));
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700181
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600182 printk(BIOS_DEBUG, "SF: timeout at %ld msec\n",timeout);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700183 return -1;
184}
185
186int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
187{
188 return spi_flash_cmd_poll_bit(flash, timeout,
189 CMD_READ_STATUS, STATUS_WIP);
190}
191
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800192int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700193{
194 u32 start, end, erase_size;
195 int ret;
196 u8 cmd[4];
197
198 erase_size = flash->sector_size;
199 if (offset % erase_size || len % erase_size) {
200 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
201 return -1;
202 }
203
Martin Roth3316cf22012-12-05 16:22:54 -0700204 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700205
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800206 cmd[0] = flash->erase_cmd;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700207 start = offset;
208 end = start + len;
209
210 while (offset < end) {
211 spi_flash_addr(offset, cmd);
212 offset += erase_size;
213
Marc Jones747127d2012-12-03 22:16:29 -0700214#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700215 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
216 cmd[2], cmd[3], offset);
Marc Jones747127d2012-12-03 22:16:29 -0700217#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700218 ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
219 if (ret)
220 goto out;
221
222 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
223 if (ret)
224 goto out;
225
226 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
227 if (ret)
228 goto out;
229 }
230
231 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
232
Patrick Georgi20959ba2012-05-12 23:30:36 +0200233out:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700234 return ret;
235}
236
Duncan Lauriefb032392015-01-15 15:28:46 -0800237int spi_flash_cmd_status(struct spi_flash *flash, u8 *reg)
238{
239 return spi_flash_cmd(flash->spi, flash->status_cmd, reg, sizeof(*reg));
240}
241
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700242/*
243 * The following table holds all device probe functions
244 *
245 * shift: number of continuation bytes before the ID
246 * idcode: the expected IDCODE or 0xff for non JEDEC devices
247 * probe: the function to call
248 *
249 * Non JEDEC devices should be ordered in the table such that
250 * the probe functions with best detection algorithms come first.
251 *
252 * Several matching entries are permitted, they will be tried
253 * in sequence until a probe function returns non NULL.
254 *
255 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
256 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
257 * changed. This is the max number of bytes probe functions may
258 * examine when looking up part-specific identification info.
259 *
260 * Probe functions will be given the idcode buffer starting at their
261 * manu id byte (the "idcode" in the table below). In other words,
262 * all of the continuation bytes will be skipped (the "shift" below).
263 */
264#define IDCODE_CONT_LEN 0
265#define IDCODE_PART_LEN 5
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700266static struct {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700267 const u8 shift;
268 const u8 idcode;
269 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
270} flashes[] = {
271 /* Keep it sorted by define name */
Idwer Vollering73a10182014-02-16 00:32:13 +0000272#if CONFIG_SPI_FLASH_AMIC
273 { 0, 0x37, spi_flash_probe_amic, },
274#endif
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200275#if CONFIG_SPI_FLASH_ATMEL
276 { 0, 0x1f, spi_flash_probe_atmel, },
277#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700278#if CONFIG_SPI_FLASH_EON
279 { 0, 0x1c, spi_flash_probe_eon, },
280#endif
Martin Rothbceaf7f2012-09-07 15:02:35 -0600281#if CONFIG_SPI_FLASH_GIGADEVICE
282 { 0, 0xc8, spi_flash_probe_gigadevice, },
283#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700284#if CONFIG_SPI_FLASH_MACRONIX
285 { 0, 0xc2, spi_flash_probe_macronix, },
286#endif
287#if CONFIG_SPI_FLASH_SPANSION
288 { 0, 0x01, spi_flash_probe_spansion, },
289#endif
290#if CONFIG_SPI_FLASH_SST
291 { 0, 0xbf, spi_flash_probe_sst, },
292#endif
293#if CONFIG_SPI_FLASH_STMICRO
294 { 0, 0x20, spi_flash_probe_stmicro, },
295#endif
296#if CONFIG_SPI_FLASH_WINBOND
297 { 0, 0xef, spi_flash_probe_winbond, },
298#endif
299 /* Keep it sorted by best detection */
300#if CONFIG_SPI_FLASH_STMICRO
301 { 0, 0xff, spi_flash_probe_stmicro, },
302#endif
Chris Douglassb34739b2014-02-14 13:51:26 -0500303#if CONFIG_SPI_FLASH_ADESTO
304 { 0, 0x1f, spi_flash_probe_adesto, },
305#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700306};
307#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
308
Gabe Black1e187352014-03-27 20:37:03 -0700309struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700310{
311 struct spi_slave *spi;
312 struct spi_flash *flash = NULL;
313 int ret, i, shift;
314 u8 idcode[IDCODE_LEN], *idp;
315
Gabe Black1e187352014-03-27 20:37:03 -0700316 spi = spi_setup_slave(bus, cs);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700317 if (!spi) {
318 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
319 return NULL;
320 }
321
Martin Roth3316cf22012-12-05 16:22:54 -0700322 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700323
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100324 if (spi->force_programmer_specific && spi->programmer_specific_probe) {
325 flash = spi->programmer_specific_probe (spi);
326 if (!flash)
327 goto err_read_id;
328 goto flash_detected;
329 }
330
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700331 /* Read the ID codes */
332 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
333 if (ret)
334 goto err_read_id;
335
336#if CONFIG_DEBUG_SPI_FLASH
David Hendricksb598bb32014-03-21 19:32:09 -0700337 printk(BIOS_SPEW, "SF: Got idcode: ");
338 for (i = 0; i < sizeof(idcode); i++)
339 printk(BIOS_SPEW, "%02x ", idcode[i]);
340 printk(BIOS_SPEW, "\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700341#endif
342
343 /* count the number of continuation bytes */
344 for (shift = 0, idp = idcode;
345 shift < IDCODE_CONT_LEN && *idp == 0x7f;
346 ++shift, ++idp)
347 continue;
348
349 /* search the table for matches in shift and id */
350 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
351 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
Duncan Laurie23b00532012-10-10 14:21:23 -0700352#if CONFIG_SMM_TSEG && defined(__SMM__)
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700353 /* Need to relocate this function */
354 tseg_relocate((void **)&flashes[i].probe);
355#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700356 /* we have a match, call probe */
357 flash = flashes[i].probe(spi, idp);
358 if (flash)
359 break;
360 }
361
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100362 if (!flash && spi->programmer_specific_probe) {
363#if CONFIG_SMM_TSEG && defined(__SMM__)
364 /* Need to relocate this function */
365 tseg_relocate((void **)&spi->programmer_specific_probe);
366#endif
367 flash = spi->programmer_specific_probe (spi);
368 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700369 if (!flash) {
370 printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp);
371 goto err_manufacturer_probe;
372 }
373
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100374flash_detected:
Duncan Laurie23b00532012-10-10 14:21:23 -0700375#if CONFIG_SMM_TSEG && defined(__SMM__)
376 /* Ensure flash handlers are valid for TSEG */
377 tseg_relocate((void **)&flash->read);
378 tseg_relocate((void **)&flash->write);
379 tseg_relocate((void **)&flash->erase);
Duncan Lauriefb032392015-01-15 15:28:46 -0800380 tseg_relocate((void **)&flash->status);
Duncan Laurie23b00532012-10-10 14:21:23 -0700381 tseg_relocate((void **)&flash->name);
382#endif
383
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700384 printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n",
385 flash->name, flash->sector_size, flash->size);
386
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800387 spi_flash_dev = flash;
388
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700389 return flash;
390
391err_manufacturer_probe:
392err_read_id:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700393 return NULL;
394}
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800395
396/* Only the RAM stage will build in the lb_new_record symbol
397 * so only define this function if we are after that stage */
398#ifdef __RAMSTAGE__
399
400void lb_spi_flash(struct lb_header *header)
401{
402 struct lb_spi_flash *flash;
403
404 flash = (struct lb_spi_flash *)lb_new_record(header);
405
406 flash->tag = LB_TAG_SPI_FLASH;
407 flash->size = sizeof(*flash);
408
409 /* Try to get the flash device if not loaded yet */
410 if (!spi_flash_dev) {
411 struct cbfs_media media;
412 init_default_cbfs_media(&media);
413 }
414
415 if (spi_flash_dev) {
416 flash->flash_size = spi_flash_dev->size;
417 flash->sector_size = spi_flash_dev->sector_size;
418 flash->erase_cmd = spi_flash_dev->erase_cmd;
419 } else {
420 flash->flash_size = CONFIG_ROM_SIZE;
421 /* Default 64k erase command should work on most flash.
422 * Uniform 4k erase only works on certain devices. */
423 flash->sector_size = 64 * KiB;
424 flash->erase_cmd = CMD_BLOCK_ERASE;
425 }
426}
427
428#endif