blob: 2b7f22c5990e8b5c61ff971561fc6fbf2f063354 [file] [log] [blame]
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02001/*
2 * SPI flash interface
3 *
4 * Copyright (C) 2008 Atmel Corporation
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +02005 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 *
Mike Frysinger4166ee52009-10-09 17:12:44 -04007 * Licensed under the GPL-2 or later.
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +02008 */
Mike Frysingerf773a1b2009-03-23 23:03:58 -04009
Haavard Skinnemoend25ce7d2008-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 Frysinger000044d2011-01-10 02:20:11 -050017static int spi_flash_read_write(struct spi_slave *spi,
18 const u8 *cmd, size_t cmd_len,
19 const u8 *data_out, u8 *data_in,
20 size_t data_len)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020021{
22 unsigned long flags = SPI_XFER_BEGIN;
23 int ret;
24
Mike Frysinger000044d2011-01-10 02:20:11 -050025 if (data_len == 0)
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020026 flags |= SPI_XFER_END;
27
Mike Frysinger000044d2011-01-10 02:20:11 -050028 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020029 if (ret) {
Mike Frysinger000044d2011-01-10 02:20:11 -050030 debug("SF: Failed to send command (%zu bytes): %d\n",
31 cmd_len, ret);
32 } else if (data_len != 0) {
33 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020034 if (ret)
Mike Frysinger000044d2011-01-10 02:20:11 -050035 debug("SF: Failed to transfer %zu bytes of data: %d\n",
36 data_len, ret);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020037 }
38
39 return ret;
40}
41
Mike Frysinger000044d2011-01-10 02:20:11 -050042int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
43{
44 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
45}
46
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020047int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
48 size_t cmd_len, void *data, size_t data_len)
49{
Mike Frysinger000044d2011-01-10 02:20:11 -050050 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020051}
52
53int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
54 const void *data, size_t data_len)
55{
Mike Frysinger000044d2011-01-10 02:20:11 -050056 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020057}
58
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +020059int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
60 size_t cmd_len, void *data, size_t data_len)
61{
62 struct spi_slave *spi = flash->spi;
63 int ret;
64
65 spi_claim_bus(spi);
66 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
67 spi_release_bus(spi);
68
69 return ret;
70}
71
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +020072/*
73 * The following table holds all device probe functions
74 *
75 * shift: number of continuation bytes before the ID
76 * idcode: the expected IDCODE or 0xff for non JEDEC devices
77 * probe: the function to call
78 *
79 * Non JEDEC devices should be ordered in the table such that
80 * the probe functions with best detection algorithms come first.
81 *
82 * Several matching entries are permitted, they will be tried
83 * in sequence until a probe function returns non NULL.
84 *
85 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
86 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
87 * changed. This is the max number of bytes probe functions may
88 * examine when looking up part-specific identification info.
89 *
90 * Probe functions will be given the idcode buffer starting at their
91 * manu id byte (the "idcode" in the table below). In other words,
92 * all of the continuation bytes will be skipped (the "shift" below).
93 */
94#define IDCODE_CONT_LEN 0
95#define IDCODE_PART_LEN 5
96static const struct {
97 const u8 shift;
98 const u8 idcode;
99 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
100} flashes[] = {
101 /* Keep it sorted by define name */
102#ifdef CONFIG_SPI_FLASH_ATMEL
103 { 0, 0x1f, spi_flash_probe_atmel, },
104#endif
Chong Huangd1d906562010-11-30 03:33:25 -0500105#ifdef CONFIG_SPI_FLASH_EON
106 { 0, 0x1c, spi_flash_probe_eon, },
107#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200108#ifdef CONFIG_SPI_FLASH_MACRONIX
109 { 0, 0xc2, spi_flash_probe_macronix, },
110#endif
111#ifdef CONFIG_SPI_FLASH_SPANSION
112 { 0, 0x01, spi_flash_probe_spansion, },
113#endif
114#ifdef CONFIG_SPI_FLASH_SST
115 { 0, 0xbf, spi_flash_probe_sst, },
116#endif
117#ifdef CONFIG_SPI_FLASH_STMICRO
118 { 0, 0x20, spi_flash_probe_stmicro, },
119#endif
120#ifdef CONFIG_SPI_FLASH_WINBOND
121 { 0, 0xef, spi_flash_probe_winbond, },
122#endif
Reinhard Meyere0987e22010-10-05 16:56:40 +0200123#ifdef CONFIG_SPI_FRAM_RAMTRON
124 { 6, 0xc2, spi_fram_probe_ramtron, },
125# undef IDCODE_CONT_LEN
126# define IDCODE_CONT_LEN 6
127#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200128 /* Keep it sorted by best detection */
129#ifdef CONFIG_SPI_FLASH_STMICRO
130 { 0, 0xff, spi_flash_probe_stmicro, },
131#endif
Reinhard Meyere0987e22010-10-05 16:56:40 +0200132#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
133 { 0, 0xff, spi_fram_probe_ramtron, },
134#endif
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200135};
136#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
137
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200138struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
139 unsigned int max_hz, unsigned int spi_mode)
140{
141 struct spi_slave *spi;
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200142 struct spi_flash *flash = NULL;
143 int ret, i, shift;
144 u8 idcode[IDCODE_LEN], *idp;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200145
146 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
147 if (!spi) {
Mike Frysingerb376bbb2010-04-29 00:35:12 -0400148 printf("SF: Failed to set up slave\n");
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200149 return NULL;
150 }
151
152 ret = spi_claim_bus(spi);
153 if (ret) {
154 debug("SF: Failed to claim SPI bus: %d\n", ret);
155 goto err_claim_bus;
156 }
157
158 /* Read the ID codes */
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200159 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200160 if (ret)
161 goto err_read_id;
162
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200163#ifdef DEBUG
164 printf("SF: Got idcodes\n");
165 print_buffer(0, idcode, 1, sizeof(idcode), 0);
166#endif
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200167
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200168 /* count the number of continuation bytes */
169 for (shift = 0, idp = idcode;
170 shift < IDCODE_CONT_LEN && *idp == 0x7f;
171 ++shift, ++idp)
172 continue;
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200173
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200174 /* search the table for matches in shift and id */
175 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
176 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
177 /* we have a match, call probe */
178 flash = flashes[i].probe(spi, idp);
179 if (flash)
180 break;
181 }
182
183 if (!flash) {
184 printf("SF: Unsupported manufacturer %02x\n", *idp);
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200185 goto err_manufacturer_probe;
Reinhard Meyer0d3fe2b2010-10-05 16:56:39 +0200186 }
Haavard Skinnemoend25ce7d2008-05-16 11:10:33 +0200187
188 spi_release_bus(spi);
189
190 return flash;
191
192err_manufacturer_probe:
193err_read_id:
194 spi_release_bus(spi);
195err_claim_bus:
196 spi_free_slave(spi);
197 return NULL;
198}
199
200void spi_flash_free(struct spi_flash *flash)
201{
202 spi_free_slave(flash->spi);
203 free(flash);
204}