4 * Copyright (C) 2008 Atmel Corporation
10 #include <spi_flash.h>
12 #include "spi_flash_internal.h"
14 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
16 unsigned long flags = SPI_XFER_BEGIN;
20 flags |= SPI_XFER_END;
22 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
24 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
29 ret = spi_xfer(spi, len * 8, NULL, response, SPI_XFER_END);
31 debug("SF: Failed to read response (%zu bytes): %d\n",
38 int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
39 size_t cmd_len, void *data, size_t data_len)
41 unsigned long flags = SPI_XFER_BEGIN;
45 flags |= SPI_XFER_END;
47 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
49 debug("SF: Failed to send read command (%zu bytes): %d\n",
51 } else if (data_len != 0) {
52 ret = spi_xfer(spi, data_len * 8, NULL, data, SPI_XFER_END);
54 debug("SF: Failed to read %zu bytes of data: %d\n",
61 int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
62 const void *data, size_t data_len)
64 unsigned long flags = SPI_XFER_BEGIN;
68 flags |= SPI_XFER_END;
70 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
72 debug("SF: Failed to send read command (%zu bytes): %d\n",
74 } else if (data_len != 0) {
75 ret = spi_xfer(spi, data_len * 8, data, NULL, SPI_XFER_END);
77 debug("SF: Failed to read %zu bytes of data: %d\n",
85 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
86 size_t cmd_len, void *data, size_t data_len)
88 struct spi_slave *spi = flash->spi;
92 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
98 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
99 unsigned int max_hz, unsigned int spi_mode)
101 struct spi_slave *spi;
102 struct spi_flash *flash;
106 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
108 debug("SF: Failed to set up slave\n");
112 ret = spi_claim_bus(spi);
114 debug("SF: Failed to claim SPI bus: %d\n", ret);
118 /* Read the ID codes */
119 ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode));
123 debug("SF: Got idcode %02x %02x %02x\n", idcode[0],
124 idcode[1], idcode[2]);
127 #ifdef CONFIG_SPI_FLASH_SPANSION
129 flash = spi_flash_probe_spansion(spi, idcode);
132 #ifdef CONFIG_SPI_FLASH_ATMEL
134 flash = spi_flash_probe_atmel(spi, idcode);
137 #ifdef CONFIG_SPI_FLASH_STMICRO
139 flash = spi_flash_probe_stmicro(spi, idcode);
143 debug("SF: Unsupported manufacturer %02X\n", idcode[0]);
149 goto err_manufacturer_probe;
151 spi_release_bus(spi);
155 err_manufacturer_probe:
157 spi_release_bus(spi);
163 void spi_flash_free(struct spi_flash *flash)
165 spi_free_slave(flash->spi);