blob: 0d67c43385d2403871b99677e453c87bf47d0975 [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
49#if CONFIG_SPI_ATOMIC_SEQUENCING == 1
50 if (spi_xfer(spi, dout, bytes_out, din, bytes_in) < 0)
51 goto done;
52#else
53 if (dout && bytes_out) {
54 if (spi_xfer(spi, dout, bytes_out, NULL, 0) < 0)
55 goto done;
56 }
57
58 if (din && bytes_in) {
59 if (spi_xfer(spi, NULL, 0, din, bytes_in) < 0)
60 goto done;
61 }
62#endif
63
64 ret = 0;
65done:
66 return ret;
67}
68
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070069int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
70{
David Hendricksf101bbe2014-03-21 19:13:34 -070071 int ret = do_spi_flash_cmd(spi, &cmd, sizeof(cmd), response, len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070072 if (ret)
73 printk(BIOS_WARNING, "SF: Failed to send command %02x: %d\n", cmd, ret);
74
75 return ret;
76}
77
78int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
79 size_t cmd_len, void *data, size_t data_len)
80{
David Hendricksf101bbe2014-03-21 19:13:34 -070081 int ret = do_spi_flash_cmd(spi, cmd, cmd_len, data, data_len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070082 if (ret) {
83 printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n",
84 data_len, ret);
85 }
86
87 return ret;
88}
89
90int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
91 const void *data, size_t data_len)
92{
93 int ret;
94 u8 buff[cmd_len + data_len];
95 memcpy(buff, cmd, cmd_len);
96 memcpy(buff + cmd_len, data, data_len);
97
David Hendricksf101bbe2014-03-21 19:13:34 -070098 ret = do_spi_flash_cmd(spi, buff, cmd_len + data_len, NULL, 0);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070099 if (ret) {
100 printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n",
101 data_len, ret);
102 }
103
104 return ret;
105}
106
107int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
108 size_t cmd_len, void *data, size_t data_len)
109{
110 struct spi_slave *spi = flash->spi;
111 int ret;
112
Martin Roth3316cf22012-12-05 16:22:54 -0700113 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700114 spi_claim_bus(spi);
115 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
116 spi_release_bus(spi);
117
118 return ret;
119}
120
121int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
122 size_t len, void *data)
123{
124 struct spi_slave *spi = flash->spi;
125 u8 cmd[5];
126
127 cmd[0] = CMD_READ_ARRAY_FAST;
128 spi_flash_addr(offset, cmd);
129 cmd[4] = 0x00;
130
131 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
132}
133
134int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset,
135 size_t len, void *data)
136{
137 struct spi_slave *spi = flash->spi;
138 u8 cmd[4];
139
140 cmd[0] = CMD_READ_ARRAY_SLOW;
141 spi_flash_addr(offset, cmd);
142
143 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
144}
145
146int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
147 u8 cmd, u8 poll_bit)
148{
149 struct spi_slave *spi = flash->spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700150 int ret;
151 u8 status;
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600152 struct mono_time current, end;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700153
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600154 timer_monotonic_get(&current);
155 end = current;
156 mono_time_add_msecs(&end, timeout);
157
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700158 do {
159 ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1);
160 if (ret)
161 return -1;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700162 if ((status & poll_bit) == 0)
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600163 return 0;
164 timer_monotonic_get(&current);
165 } while (!mono_time_after(&current, &end));
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700166
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600167 printk(BIOS_DEBUG, "SF: timeout at %ld msec\n",timeout);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700168 return -1;
169}
170
171int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
172{
173 return spi_flash_cmd_poll_bit(flash, timeout,
174 CMD_READ_STATUS, STATUS_WIP);
175}
176
177int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
178 u32 offset, size_t len)
179{
180 u32 start, end, erase_size;
181 int ret;
182 u8 cmd[4];
183
184 erase_size = flash->sector_size;
185 if (offset % erase_size || len % erase_size) {
186 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
187 return -1;
188 }
189
Martin Roth3316cf22012-12-05 16:22:54 -0700190 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700191 ret = spi_claim_bus(flash->spi);
192 if (ret) {
193 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
194 return ret;
195 }
196
197 cmd[0] = erase_cmd;
198 start = offset;
199 end = start + len;
200
201 while (offset < end) {
202 spi_flash_addr(offset, cmd);
203 offset += erase_size;
204
Marc Jones747127d2012-12-03 22:16:29 -0700205#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700206 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
207 cmd[2], cmd[3], offset);
Marc Jones747127d2012-12-03 22:16:29 -0700208#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700209 ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
210 if (ret)
211 goto out;
212
213 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
214 if (ret)
215 goto out;
216
217 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
218 if (ret)
219 goto out;
220 }
221
222 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
223
Patrick Georgi20959ba2012-05-12 23:30:36 +0200224out:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700225 spi_release_bus(flash->spi);
226 return ret;
227}
228
229/*
230 * The following table holds all device probe functions
231 *
232 * shift: number of continuation bytes before the ID
233 * idcode: the expected IDCODE or 0xff for non JEDEC devices
234 * probe: the function to call
235 *
236 * Non JEDEC devices should be ordered in the table such that
237 * the probe functions with best detection algorithms come first.
238 *
239 * Several matching entries are permitted, they will be tried
240 * in sequence until a probe function returns non NULL.
241 *
242 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
243 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
244 * changed. This is the max number of bytes probe functions may
245 * examine when looking up part-specific identification info.
246 *
247 * Probe functions will be given the idcode buffer starting at their
248 * manu id byte (the "idcode" in the table below). In other words,
249 * all of the continuation bytes will be skipped (the "shift" below).
250 */
251#define IDCODE_CONT_LEN 0
252#define IDCODE_PART_LEN 5
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700253static struct {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700254 const u8 shift;
255 const u8 idcode;
256 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
257} flashes[] = {
258 /* Keep it sorted by define name */
Idwer Vollering73a10182014-02-16 00:32:13 +0000259#if CONFIG_SPI_FLASH_AMIC
260 { 0, 0x37, spi_flash_probe_amic, },
261#endif
Kyösti Mälkki96d92762014-11-11 15:04:38 +0200262#if CONFIG_SPI_FLASH_ATMEL
263 { 0, 0x1f, spi_flash_probe_atmel, },
264#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700265#if CONFIG_SPI_FLASH_EON
266 { 0, 0x1c, spi_flash_probe_eon, },
267#endif
Martin Rothbceaf7f2012-09-07 15:02:35 -0600268#if CONFIG_SPI_FLASH_GIGADEVICE
269 { 0, 0xc8, spi_flash_probe_gigadevice, },
270#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700271#if CONFIG_SPI_FLASH_MACRONIX
272 { 0, 0xc2, spi_flash_probe_macronix, },
273#endif
274#if CONFIG_SPI_FLASH_SPANSION
275 { 0, 0x01, spi_flash_probe_spansion, },
276#endif
277#if CONFIG_SPI_FLASH_SST
278 { 0, 0xbf, spi_flash_probe_sst, },
279#endif
280#if CONFIG_SPI_FLASH_STMICRO
281 { 0, 0x20, spi_flash_probe_stmicro, },
282#endif
283#if CONFIG_SPI_FLASH_WINBOND
284 { 0, 0xef, spi_flash_probe_winbond, },
285#endif
286 /* Keep it sorted by best detection */
287#if CONFIG_SPI_FLASH_STMICRO
288 { 0, 0xff, spi_flash_probe_stmicro, },
289#endif
Chris Douglassb34739b2014-02-14 13:51:26 -0500290#if CONFIG_SPI_FLASH_ADESTO
291 { 0, 0x1f, spi_flash_probe_adesto, },
292#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700293};
294#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
295
Gabe Black1e187352014-03-27 20:37:03 -0700296struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700297{
298 struct spi_slave *spi;
299 struct spi_flash *flash = NULL;
300 int ret, i, shift;
301 u8 idcode[IDCODE_LEN], *idp;
302
Gabe Black1e187352014-03-27 20:37:03 -0700303 spi = spi_setup_slave(bus, cs);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700304 if (!spi) {
305 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
306 return NULL;
307 }
308
Martin Roth3316cf22012-12-05 16:22:54 -0700309 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700310 ret = spi_claim_bus(spi);
311 if (ret) {
312 printk(BIOS_WARNING, "SF: Failed to claim SPI bus: %d\n", ret);
313 goto err_claim_bus;
314 }
315
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100316 if (spi->force_programmer_specific && spi->programmer_specific_probe) {
317 flash = spi->programmer_specific_probe (spi);
318 if (!flash)
319 goto err_read_id;
320 goto flash_detected;
321 }
322
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700323 /* Read the ID codes */
324 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
325 if (ret)
326 goto err_read_id;
327
328#if CONFIG_DEBUG_SPI_FLASH
David Hendricksb598bb32014-03-21 19:32:09 -0700329 printk(BIOS_SPEW, "SF: Got idcode: ");
330 for (i = 0; i < sizeof(idcode); i++)
331 printk(BIOS_SPEW, "%02x ", idcode[i]);
332 printk(BIOS_SPEW, "\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700333#endif
334
335 /* count the number of continuation bytes */
336 for (shift = 0, idp = idcode;
337 shift < IDCODE_CONT_LEN && *idp == 0x7f;
338 ++shift, ++idp)
339 continue;
340
341 /* search the table for matches in shift and id */
342 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
343 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
Duncan Laurie23b00532012-10-10 14:21:23 -0700344#if CONFIG_SMM_TSEG && defined(__SMM__)
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700345 /* Need to relocate this function */
346 tseg_relocate((void **)&flashes[i].probe);
347#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700348 /* we have a match, call probe */
349 flash = flashes[i].probe(spi, idp);
350 if (flash)
351 break;
352 }
353
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100354 if (!flash && spi->programmer_specific_probe) {
355#if CONFIG_SMM_TSEG && defined(__SMM__)
356 /* Need to relocate this function */
357 tseg_relocate((void **)&spi->programmer_specific_probe);
358#endif
359 flash = spi->programmer_specific_probe (spi);
360 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700361 if (!flash) {
362 printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp);
363 goto err_manufacturer_probe;
364 }
365
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100366flash_detected:
Duncan Laurie23b00532012-10-10 14:21:23 -0700367#if CONFIG_SMM_TSEG && defined(__SMM__)
368 /* Ensure flash handlers are valid for TSEG */
369 tseg_relocate((void **)&flash->read);
370 tseg_relocate((void **)&flash->write);
371 tseg_relocate((void **)&flash->erase);
372 tseg_relocate((void **)&flash->name);
373#endif
374
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700375 printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n",
376 flash->name, flash->sector_size, flash->size);
377
378 spi_release_bus(spi);
379
380 return flash;
381
382err_manufacturer_probe:
383err_read_id:
384 spi_release_bus(spi);
385err_claim_bus:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700386 return NULL;
387}