4 * Copyright (C) 2008 Atmel Corporation
5 * Licensed under the GPL-2 or later.
11 #include <spi_flash.h>
13 #include "spi_flash_internal.h"
15 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
17 unsigned long flags = SPI_XFER_BEGIN;
21 flags |= SPI_XFER_END;
23 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
25 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
30 ret = spi_xfer(spi, len * 8, NULL, response, SPI_XFER_END);
32 debug("SF: Failed to read response (%zu bytes): %d\n",
39 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
40 size_t cmd_len, void *data, size_t data_len)
42 unsigned long flags = SPI_XFER_BEGIN;
46 flags |= SPI_XFER_END;
48 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
50 debug("SF: Failed to send read command (%zu bytes): %d\n",
52 } else if (data_len != 0) {
53 ret = spi_xfer(spi, data_len * 8, NULL, data, SPI_XFER_END);
55 debug("SF: Failed to read %zu bytes of data: %d\n",
62 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
63 const void *data, size_t data_len)
65 unsigned long flags = SPI_XFER_BEGIN;
69 flags |= SPI_XFER_END;
71 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
73 debug("SF: Failed to send read command (%zu bytes): %d\n",
75 } else if (data_len != 0) {
76 ret = spi_xfer(spi, data_len * 8, data, NULL, SPI_XFER_END);
78 debug("SF: Failed to read %zu bytes of data: %d\n",
86 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
87 size_t cmd_len, void *data, size_t data_len)
89 struct spi_slave *spi = flash->spi;
93 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
99 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
100 unsigned int max_hz, unsigned int spi_mode)
102 struct spi_slave *spi;
103 struct spi_flash *flash;
107 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
109 debug("SF: Failed to set up slave\n");
113 ret = spi_claim_bus(spi);
115 debug("SF: Failed to claim SPI bus: %d\n", ret);
119 /* Read the ID codes */
120 ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode));
124 debug("SF: Got idcode %02x %02x %02x %02x %02x\n", idcode[0],
125 idcode[1], idcode[2], idcode[3], idcode[4]);
128 #ifdef CONFIG_SPI_FLASH_SPANSION
130 flash = spi_flash_probe_spansion(spi, idcode);
133 #ifdef CONFIG_SPI_FLASH_ATMEL
135 flash = spi_flash_probe_atmel(spi, idcode);
138 #ifdef CONFIG_SPI_FLASH_MACRONIX
140 flash = spi_flash_probe_macronix(spi, idcode);
143 #ifdef CONFIG_SPI_FLASH_WINBOND
145 flash = spi_flash_probe_winbond(spi, idcode);
148 #ifdef CONFIG_SPI_FLASH_STMICRO
150 flash = spi_flash_probe_stmicro(spi, idcode);
153 #ifdef CONFIG_SPI_FLASH_SST
155 flash = spi_flash_probe_sst(spi, idcode);
159 debug("SF: Unsupported manufacturer %02X\n", idcode[0]);
165 goto err_manufacturer_probe;
167 spi_release_bus(spi);
171 err_manufacturer_probe:
173 spi_release_bus(spi);
179 void spi_flash_free(struct spi_flash *flash)
181 spi_free_slave(flash->spi);