4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
7 * Licensed under the GPL-2 or later.
13 #include <spi_flash.h>
15 #include "spi_flash_internal.h"
17 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
19 unsigned long flags = SPI_XFER_BEGIN;
23 flags |= SPI_XFER_END;
25 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
27 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
32 ret = spi_xfer(spi, len * 8, NULL, response, SPI_XFER_END);
34 debug("SF: Failed to read response (%zu bytes): %d\n",
41 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
42 size_t cmd_len, void *data, size_t data_len)
44 unsigned long flags = SPI_XFER_BEGIN;
48 flags |= SPI_XFER_END;
50 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
52 debug("SF: Failed to send read command (%zu bytes): %d\n",
54 } else if (data_len != 0) {
55 ret = spi_xfer(spi, data_len * 8, NULL, data, SPI_XFER_END);
57 debug("SF: Failed to read %zu bytes of data: %d\n",
64 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
65 const void *data, size_t data_len)
67 unsigned long flags = SPI_XFER_BEGIN;
71 flags |= SPI_XFER_END;
73 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
75 debug("SF: Failed to send read command (%zu bytes): %d\n",
77 } else if (data_len != 0) {
78 ret = spi_xfer(spi, data_len * 8, data, NULL, SPI_XFER_END);
80 debug("SF: Failed to read %zu bytes of data: %d\n",
88 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
89 size_t cmd_len, void *data, size_t data_len)
91 struct spi_slave *spi = flash->spi;
95 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
102 * The following table holds all device probe functions
104 * shift: number of continuation bytes before the ID
105 * idcode: the expected IDCODE or 0xff for non JEDEC devices
106 * probe: the function to call
108 * Non JEDEC devices should be ordered in the table such that
109 * the probe functions with best detection algorithms come first.
111 * Several matching entries are permitted, they will be tried
112 * in sequence until a probe function returns non NULL.
114 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
115 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
116 * changed. This is the max number of bytes probe functions may
117 * examine when looking up part-specific identification info.
119 * Probe functions will be given the idcode buffer starting at their
120 * manu id byte (the "idcode" in the table below). In other words,
121 * all of the continuation bytes will be skipped (the "shift" below).
123 #define IDCODE_CONT_LEN 0
124 #define IDCODE_PART_LEN 5
125 static const struct {
128 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
130 /* Keep it sorted by define name */
131 #ifdef CONFIG_SPI_FLASH_ATMEL
132 { 0, 0x1f, spi_flash_probe_atmel, },
134 #ifdef CONFIG_SPI_FLASH_MACRONIX
135 { 0, 0xc2, spi_flash_probe_macronix, },
137 #ifdef CONFIG_SPI_FLASH_SPANSION
138 { 0, 0x01, spi_flash_probe_spansion, },
140 #ifdef CONFIG_SPI_FLASH_SST
141 { 0, 0xbf, spi_flash_probe_sst, },
143 #ifdef CONFIG_SPI_FLASH_STMICRO
144 { 0, 0x20, spi_flash_probe_stmicro, },
146 #ifdef CONFIG_SPI_FLASH_WINBOND
147 { 0, 0xef, spi_flash_probe_winbond, },
149 #ifdef CONFIG_SPI_FRAM_RAMTRON
150 { 6, 0xc2, spi_fram_probe_ramtron, },
151 # undef IDCODE_CONT_LEN
152 # define IDCODE_CONT_LEN 6
154 /* Keep it sorted by best detection */
155 #ifdef CONFIG_SPI_FLASH_STMICRO
156 { 0, 0xff, spi_flash_probe_stmicro, },
158 #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
159 { 0, 0xff, spi_fram_probe_ramtron, },
162 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
164 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
165 unsigned int max_hz, unsigned int spi_mode)
167 struct spi_slave *spi;
168 struct spi_flash *flash = NULL;
170 u8 idcode[IDCODE_LEN], *idp;
172 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
174 printf("SF: Failed to set up slave\n");
178 ret = spi_claim_bus(spi);
180 debug("SF: Failed to claim SPI bus: %d\n", ret);
184 /* Read the ID codes */
185 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
190 printf("SF: Got idcodes\n");
191 print_buffer(0, idcode, 1, sizeof(idcode), 0);
194 /* count the number of continuation bytes */
195 for (shift = 0, idp = idcode;
196 shift < IDCODE_CONT_LEN && *idp == 0x7f;
200 /* search the table for matches in shift and id */
201 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
202 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
203 /* we have a match, call probe */
204 flash = flashes[i].probe(spi, idp);
210 printf("SF: Unsupported manufacturer %02x\n", *idp);
211 goto err_manufacturer_probe;
214 spi_release_bus(spi);
218 err_manufacturer_probe:
220 spi_release_bus(spi);
226 void spi_flash_free(struct spi_flash *flash)
228 spi_free_slave(flash->spi);