blob: 626d5069b01cca1916714f870e84a94e56d05c45 [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
28int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
29{
Gabe Black93d9f922014-03-27 21:52:43 -070030 int ret = spi_xfer(spi, &cmd, sizeof(cmd), response, len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070031 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{
Gabe Black93d9f922014-03-27 21:52:43 -070040 int ret = spi_xfer(spi, cmd, cmd_len, data, data_len);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070041 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
Gabe Black93d9f922014-03-27 21:52:43 -070057 ret = spi_xfer(spi, buff, cmd_len + data_len, NULL, 0);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070058 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
Martin Roth3316cf22012-12-05 16:22:54 -070072 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070073 spi_claim_bus(spi);
74 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
75 spi_release_bus(spi);
76
77 return ret;
78}
79
80int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
81 size_t len, void *data)
82{
83 struct spi_slave *spi = flash->spi;
84 u8 cmd[5];
85
86 cmd[0] = CMD_READ_ARRAY_FAST;
87 spi_flash_addr(offset, cmd);
88 cmd[4] = 0x00;
89
90 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
91}
92
93int spi_flash_cmd_read_slow(struct spi_flash *flash, u32 offset,
94 size_t len, void *data)
95{
96 struct spi_slave *spi = flash->spi;
97 u8 cmd[4];
98
99 cmd[0] = CMD_READ_ARRAY_SLOW;
100 spi_flash_addr(offset, cmd);
101
102 return spi_flash_cmd_read(spi, cmd, sizeof(cmd), data, len);
103}
104
105int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
106 u8 cmd, u8 poll_bit)
107{
108 struct spi_slave *spi = flash->spi;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700109 int ret;
110 u8 status;
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600111 struct mono_time current, end;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700112
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600113 timer_monotonic_get(&current);
114 end = current;
115 mono_time_add_msecs(&end, timeout);
116
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700117 do {
118 ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1);
119 if (ret)
120 return -1;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700121 if ((status & poll_bit) == 0)
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600122 return 0;
123 timer_monotonic_get(&current);
124 } while (!mono_time_after(&current, &end));
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700125
Dave Frodinc50c0ab2014-06-11 12:53:47 -0600126 printk(BIOS_DEBUG, "SF: timeout at %ld msec\n",timeout);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700127 return -1;
128}
129
130int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
131{
132 return spi_flash_cmd_poll_bit(flash, timeout,
133 CMD_READ_STATUS, STATUS_WIP);
134}
135
136int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
137 u32 offset, size_t len)
138{
139 u32 start, end, erase_size;
140 int ret;
141 u8 cmd[4];
142
143 erase_size = flash->sector_size;
144 if (offset % erase_size || len % erase_size) {
145 printk(BIOS_WARNING, "SF: Erase offset/length not multiple of erase size\n");
146 return -1;
147 }
148
Martin Roth3316cf22012-12-05 16:22:54 -0700149 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700150 ret = spi_claim_bus(flash->spi);
151 if (ret) {
152 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
153 return ret;
154 }
155
156 cmd[0] = erase_cmd;
157 start = offset;
158 end = start + len;
159
160 while (offset < end) {
161 spi_flash_addr(offset, cmd);
162 offset += erase_size;
163
Marc Jones747127d2012-12-03 22:16:29 -0700164#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700165 printk(BIOS_SPEW, "SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
166 cmd[2], cmd[3], offset);
Marc Jones747127d2012-12-03 22:16:29 -0700167#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700168 ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
169 if (ret)
170 goto out;
171
172 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
173 if (ret)
174 goto out;
175
176 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
177 if (ret)
178 goto out;
179 }
180
181 printk(BIOS_DEBUG, "SF: Successfully erased %zu bytes @ %#x\n", len, start);
182
Patrick Georgi20959ba2012-05-12 23:30:36 +0200183out:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700184 spi_release_bus(flash->spi);
185 return ret;
186}
187
188/*
189 * The following table holds all device probe functions
190 *
191 * shift: number of continuation bytes before the ID
192 * idcode: the expected IDCODE or 0xff for non JEDEC devices
193 * probe: the function to call
194 *
195 * Non JEDEC devices should be ordered in the table such that
196 * the probe functions with best detection algorithms come first.
197 *
198 * Several matching entries are permitted, they will be tried
199 * in sequence until a probe function returns non NULL.
200 *
201 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
202 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
203 * changed. This is the max number of bytes probe functions may
204 * examine when looking up part-specific identification info.
205 *
206 * Probe functions will be given the idcode buffer starting at their
207 * manu id byte (the "idcode" in the table below). In other words,
208 * all of the continuation bytes will be skipped (the "shift" below).
209 */
210#define IDCODE_CONT_LEN 0
211#define IDCODE_PART_LEN 5
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700212static struct {
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700213 const u8 shift;
214 const u8 idcode;
215 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
216} flashes[] = {
217 /* Keep it sorted by define name */
Idwer Vollering73a10182014-02-16 00:32:13 +0000218#if CONFIG_SPI_FLASH_AMIC
219 { 0, 0x37, spi_flash_probe_amic, },
220#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700221#if CONFIG_SPI_FLASH_EON
222 { 0, 0x1c, spi_flash_probe_eon, },
223#endif
Martin Rothbceaf7f2012-09-07 15:02:35 -0600224#if CONFIG_SPI_FLASH_GIGADEVICE
225 { 0, 0xc8, spi_flash_probe_gigadevice, },
226#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700227#if CONFIG_SPI_FLASH_MACRONIX
228 { 0, 0xc2, spi_flash_probe_macronix, },
229#endif
230#if CONFIG_SPI_FLASH_SPANSION
231 { 0, 0x01, spi_flash_probe_spansion, },
232#endif
233#if CONFIG_SPI_FLASH_SST
234 { 0, 0xbf, spi_flash_probe_sst, },
235#endif
236#if CONFIG_SPI_FLASH_STMICRO
237 { 0, 0x20, spi_flash_probe_stmicro, },
238#endif
239#if CONFIG_SPI_FLASH_WINBOND
240 { 0, 0xef, spi_flash_probe_winbond, },
241#endif
242 /* Keep it sorted by best detection */
243#if CONFIG_SPI_FLASH_STMICRO
244 { 0, 0xff, spi_flash_probe_stmicro, },
245#endif
Chris Douglassb34739b2014-02-14 13:51:26 -0500246#if CONFIG_SPI_FLASH_ADESTO
247 { 0, 0x1f, spi_flash_probe_adesto, },
248#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700249};
250#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
251
Gabe Black1e187352014-03-27 20:37:03 -0700252struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700253{
254 struct spi_slave *spi;
255 struct spi_flash *flash = NULL;
256 int ret, i, shift;
257 u8 idcode[IDCODE_LEN], *idp;
258
Gabe Black1e187352014-03-27 20:37:03 -0700259 spi = spi_setup_slave(bus, cs);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700260 if (!spi) {
261 printk(BIOS_WARNING, "SF: Failed to set up slave\n");
262 return NULL;
263 }
264
Martin Roth3316cf22012-12-05 16:22:54 -0700265 spi->rw = SPI_READ_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700266 ret = spi_claim_bus(spi);
267 if (ret) {
268 printk(BIOS_WARNING, "SF: Failed to claim SPI bus: %d\n", ret);
269 goto err_claim_bus;
270 }
271
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100272 if (spi->force_programmer_specific && spi->programmer_specific_probe) {
273 flash = spi->programmer_specific_probe (spi);
274 if (!flash)
275 goto err_read_id;
276 goto flash_detected;
277 }
278
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700279 /* Read the ID codes */
280 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
281 if (ret)
282 goto err_read_id;
283
284#if CONFIG_DEBUG_SPI_FLASH
285 printk(BIOS_SPEW, "SF: Got idcodes\n");
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700286#endif
287
288 /* count the number of continuation bytes */
289 for (shift = 0, idp = idcode;
290 shift < IDCODE_CONT_LEN && *idp == 0x7f;
291 ++shift, ++idp)
292 continue;
293
294 /* search the table for matches in shift and id */
295 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
296 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
Duncan Laurie23b00532012-10-10 14:21:23 -0700297#if CONFIG_SMM_TSEG && defined(__SMM__)
Duncan Laurie181bbdd2012-06-23 16:53:57 -0700298 /* Need to relocate this function */
299 tseg_relocate((void **)&flashes[i].probe);
300#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700301 /* we have a match, call probe */
302 flash = flashes[i].probe(spi, idp);
303 if (flash)
304 break;
305 }
306
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100307 if (!flash && spi->programmer_specific_probe) {
308#if CONFIG_SMM_TSEG && defined(__SMM__)
309 /* Need to relocate this function */
310 tseg_relocate((void **)&spi->programmer_specific_probe);
311#endif
312 flash = spi->programmer_specific_probe (spi);
313 }
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700314 if (!flash) {
315 printk(BIOS_WARNING, "SF: Unsupported manufacturer %02x\n", *idp);
316 goto err_manufacturer_probe;
317 }
318
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +0100319flash_detected:
Duncan Laurie23b00532012-10-10 14:21:23 -0700320#if CONFIG_SMM_TSEG && defined(__SMM__)
321 /* Ensure flash handlers are valid for TSEG */
322 tseg_relocate((void **)&flash->read);
323 tseg_relocate((void **)&flash->write);
324 tseg_relocate((void **)&flash->erase);
325 tseg_relocate((void **)&flash->name);
326#endif
327
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700328 printk(BIOS_INFO, "SF: Detected %s with page size %x, total %x\n",
329 flash->name, flash->sector_size, flash->size);
330
331 spi_release_bus(spi);
332
333 return flash;
334
335err_manufacturer_probe:
336err_read_id:
337 spi_release_bus(spi);
338err_claim_bus:
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700339 return NULL;
340}