blob: ccb7e314085066879e4842ec4b1ad2c5da003783 [file] [log] [blame]
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02001/*
2 * SPI flash interface
3 *
4 * Copyright (C) 2008 Atmel Corporation
Reinhard Meyercfe1c672010-10-05 16:56:39 +02005 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 *
Mike Frysinger22f8b682009-10-09 17:12:44 -04007 * Licensed under the GPL-2 or later.
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +02008 */
Mike Frysingerc5431ac2009-03-23 23:03:58 -04009
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020010#include <common.h>
11#include <malloc.h>
12#include <spi.h>
13#include <spi_flash.h>
14
15#include "spi_flash_internal.h"
16
Mike Frysinger53421bb2011-01-10 02:20:13 -050017static void spi_flash_addr(u32 addr, u8 *cmd)
18{
19 /* cmd[0] is actual command */
20 cmd[1] = addr >> 16;
21 cmd[2] = addr >> 8;
22 cmd[3] = addr >> 0;
23}
24
Mike Frysinger5efdb042011-01-10 02:20:11 -050025static int spi_flash_read_write(struct spi_slave *spi,
26 const u8 *cmd, size_t cmd_len,
27 const u8 *data_out, u8 *data_in,
28 size_t data_len)
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020029{
30 unsigned long flags = SPI_XFER_BEGIN;
31 int ret;
32
33 if (data_len == 0)
34 flags |= SPI_XFER_END;
35
36 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
37 if (ret) {
Mike Frysinger5efdb042011-01-10 02:20:11 -050038 debug("SF: Failed to send command (%zu bytes): %d\n",
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020039 cmd_len, ret);
40 } else if (data_len != 0) {
Mike Frysinger5efdb042011-01-10 02:20:11 -050041 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020042 if (ret)
Mike Frysinger5efdb042011-01-10 02:20:11 -050043 debug("SF: Failed to transfer %zu bytes of data: %d\n",
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020044 data_len, ret);
45 }
46
47 return ret;
48}
49
Mike Frysinger5efdb042011-01-10 02:20:11 -050050int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020051{
Mike Frysinger5efdb042011-01-10 02:20:11 -050052 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
53}
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020054
Mike Frysinger5efdb042011-01-10 02:20:11 -050055int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
56 size_t cmd_len, void *data, size_t data_len)
57{
58 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020059}
60
Mike Frysinger5efdb042011-01-10 02:20:11 -050061int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
62 const void *data, size_t data_len)
63{
64 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
65}
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +020066
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
73 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
Mike Frysinger373e7d62011-01-10 02:20:14 -050080int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
81 size_t len, void *data)
82{
83 u8 cmd[5];
84
85 cmd[0] = CMD_READ_ARRAY_FAST;
86 spi_flash_addr(offset, cmd);
87 cmd[4] = 0x00;
88
89 return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
90}
91
Mike Frysinger37e13bc2011-01-10 02:20:12 -050092int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
93 u8 cmd, u8 poll_bit)
94{
95 struct spi_slave *spi = flash->spi;
96 unsigned long timebase;
97 int ret;
98 u8 status;
99
100 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
101 if (ret) {
102 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
103 return ret;
104 }
105
106 timebase = get_timer(0);
107 do {
108 ret = spi_xfer(spi, 8, NULL, &status, 0);
109 if (ret)
110 return -1;
111
112 if ((status & poll_bit) == 0)
113 break;
114
115 } while (get_timer(timebase) < timeout);
116
117 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
118
119 if ((status & poll_bit) == 0)
120 return 0;
121
122 /* Timed out */
123 debug("SF: time out!\n");
124 return -1;
125}
126
127int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
128{
129 return spi_flash_cmd_poll_bit(flash, timeout,
130 CMD_READ_STATUS, STATUS_WIP);
131}
132
Mike Frysinger53421bb2011-01-10 02:20:13 -0500133int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
Richard Retanubunb0148dc2011-02-16 16:37:22 -0500134 u32 offset, size_t len)
Mike Frysinger53421bb2011-01-10 02:20:13 -0500135{
Richard Retanubunb0148dc2011-02-16 16:37:22 -0500136 u32 start, end, erase_size;
Mike Frysinger53421bb2011-01-10 02:20:13 -0500137 int ret;
138 u8 cmd[4];
139
Richard Retanubunb0148dc2011-02-16 16:37:22 -0500140 erase_size = flash->sector_size;
Mike Frysinger53421bb2011-01-10 02:20:13 -0500141 if (offset % erase_size || len % erase_size) {
142 debug("SF: Erase offset/length not multiple of erase size\n");
143 return -1;
144 }
145
146 ret = spi_claim_bus(flash->spi);
147 if (ret) {
148 debug("SF: Unable to claim SPI bus\n");
149 return ret;
150 }
151
152 cmd[0] = erase_cmd;
153 start = offset;
154 end = start + len;
155
156 while (offset < end) {
157 spi_flash_addr(offset, cmd);
158 offset += erase_size;
159
160 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
161 cmd[2], cmd[3], offset);
162
163 ret = spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
164 if (ret)
165 goto out;
166
167 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
168 if (ret)
169 goto out;
170
171 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
172 if (ret)
173 goto out;
174 }
175
176 debug("SF: Successfully erased %lu bytes @ %#x\n",
177 len * erase_size, start);
178
179 out:
180 spi_release_bus(flash->spi);
181 return ret;
182}
183
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200184/*
185 * The following table holds all device probe functions
186 *
187 * shift: number of continuation bytes before the ID
188 * idcode: the expected IDCODE or 0xff for non JEDEC devices
189 * probe: the function to call
190 *
191 * Non JEDEC devices should be ordered in the table such that
192 * the probe functions with best detection algorithms come first.
193 *
194 * Several matching entries are permitted, they will be tried
195 * in sequence until a probe function returns non NULL.
196 *
197 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
198 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
199 * changed. This is the max number of bytes probe functions may
200 * examine when looking up part-specific identification info.
201 *
202 * Probe functions will be given the idcode buffer starting at their
203 * manu id byte (the "idcode" in the table below). In other words,
204 * all of the continuation bytes will be skipped (the "shift" below).
205 */
206#define IDCODE_CONT_LEN 0
207#define IDCODE_PART_LEN 5
208static const struct {
209 const u8 shift;
210 const u8 idcode;
211 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
212} flashes[] = {
213 /* Keep it sorted by define name */
214#ifdef CONFIG_SPI_FLASH_ATMEL
215 { 0, 0x1f, spi_flash_probe_atmel, },
216#endif
Chong Huangc71e6dc2010-11-30 03:33:25 -0500217#ifdef CONFIG_SPI_FLASH_EON
218 { 0, 0x1c, spi_flash_probe_eon, },
219#endif
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200220#ifdef CONFIG_SPI_FLASH_MACRONIX
221 { 0, 0xc2, spi_flash_probe_macronix, },
222#endif
223#ifdef CONFIG_SPI_FLASH_SPANSION
224 { 0, 0x01, spi_flash_probe_spansion, },
225#endif
226#ifdef CONFIG_SPI_FLASH_SST
227 { 0, 0xbf, spi_flash_probe_sst, },
228#endif
229#ifdef CONFIG_SPI_FLASH_STMICRO
230 { 0, 0x20, spi_flash_probe_stmicro, },
231#endif
232#ifdef CONFIG_SPI_FLASH_WINBOND
233 { 0, 0xef, spi_flash_probe_winbond, },
234#endif
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200235#ifdef CONFIG_SPI_FRAM_RAMTRON
236 { 6, 0xc2, spi_fram_probe_ramtron, },
237# undef IDCODE_CONT_LEN
238# define IDCODE_CONT_LEN 6
239#endif
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200240 /* Keep it sorted by best detection */
241#ifdef CONFIG_SPI_FLASH_STMICRO
242 { 0, 0xff, spi_flash_probe_stmicro, },
243#endif
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200244#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
245 { 0, 0xff, spi_fram_probe_ramtron, },
246#endif
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200247};
248#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
249
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200250struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
251 unsigned int max_hz, unsigned int spi_mode)
252{
253 struct spi_slave *spi;
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200254 struct spi_flash *flash = NULL;
255 int ret, i, shift;
256 u8 idcode[IDCODE_LEN], *idp;
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200257
258 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
259 if (!spi) {
Mike Frysinger02110b52010-04-29 00:35:12 -0400260 printf("SF: Failed to set up slave\n");
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200261 return NULL;
262 }
263
264 ret = spi_claim_bus(spi);
265 if (ret) {
266 debug("SF: Failed to claim SPI bus: %d\n", ret);
267 goto err_claim_bus;
268 }
269
270 /* Read the ID codes */
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200271 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200272 if (ret)
273 goto err_read_id;
274
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200275#ifdef DEBUG
276 printf("SF: Got idcodes\n");
277 print_buffer(0, idcode, 1, sizeof(idcode), 0);
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400278#endif
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200279
280 /* count the number of continuation bytes */
281 for (shift = 0, idp = idcode;
282 shift < IDCODE_CONT_LEN && *idp == 0x7f;
283 ++shift, ++idp)
284 continue;
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200285
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200286 /* search the table for matches in shift and id */
287 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
288 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
289 /* we have a match, call probe */
290 flash = flashes[i].probe(spi, idp);
291 if (flash)
292 break;
293 }
294
295 if (!flash) {
296 printf("SF: Unsupported manufacturer %02x\n", *idp);
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200297 goto err_manufacturer_probe;
Reinhard Meyercfe1c672010-10-05 16:56:39 +0200298 }
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200299
Richard Retanubunb0148dc2011-02-16 16:37:22 -0500300 printf("SF: Detected %s with page size %u, total ",
301 flash->name, flash->sector_size);
302 print_size(flash->size, "\n");
303
Haavard Skinnemoen2f5bfb72008-05-16 11:10:33 +0200304 spi_release_bus(spi);
305
306 return flash;
307
308err_manufacturer_probe:
309err_read_id:
310 spi_release_bus(spi);
311err_claim_bus:
312 spi_free_slave(spi);
313 return NULL;
314}
315
316void spi_flash_free(struct spi_flash *flash)
317{
318 spi_free_slave(flash->spi);
319 free(flash);
320}