2008-05-16 09:10:33 +00:00
|
|
|
/*
|
|
|
|
* SPI flash interface
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Atmel Corporation
|
2010-10-05 14:56:39 +00:00
|
|
|
* Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
|
|
|
|
*
|
2013-10-10 17:02:55 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2008-05-16 09:10:33 +00:00
|
|
|
*/
|
2009-03-24 03:03:58 +00:00
|
|
|
|
2008-05-16 09:10:33 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <spi.h>
|
2011-01-10 07:20:13 +00:00
|
|
|
|
2011-01-10 07:20:11 +00:00
|
|
|
static int spi_flash_read_write(struct spi_slave *spi,
|
|
|
|
const u8 *cmd, size_t cmd_len,
|
|
|
|
const u8 *data_out, u8 *data_in,
|
|
|
|
size_t data_len)
|
2008-05-16 09:10:33 +00:00
|
|
|
{
|
|
|
|
unsigned long flags = SPI_XFER_BEGIN;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (data_len == 0)
|
|
|
|
flags |= SPI_XFER_END;
|
|
|
|
|
|
|
|
ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
|
|
|
|
if (ret) {
|
2011-01-10 07:20:11 +00:00
|
|
|
debug("SF: Failed to send command (%zu bytes): %d\n",
|
2013-07-29 16:46:41 +00:00
|
|
|
cmd_len, ret);
|
2008-05-16 09:10:33 +00:00
|
|
|
} else if (data_len != 0) {
|
2013-07-29 16:46:41 +00:00
|
|
|
ret = spi_xfer(spi, data_len * 8, data_out, data_in,
|
|
|
|
SPI_XFER_END);
|
2008-05-16 09:10:33 +00:00
|
|
|
if (ret)
|
2011-01-10 07:20:11 +00:00
|
|
|
debug("SF: Failed to transfer %zu bytes of data: %d\n",
|
2013-07-29 16:46:41 +00:00
|
|
|
data_len, ret);
|
2008-05-16 09:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-10 07:20:11 +00:00
|
|
|
int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
|
|
|
|
size_t cmd_len, void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
|
2008-05-16 09:10:33 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 13:31:56 +00:00
|
|
|
int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
|
2013-03-11 06:08:02 +00:00
|
|
|
{
|
2013-08-29 13:31:56 +00:00
|
|
|
return spi_flash_cmd_read(spi, &cmd, 1, response, len);
|
2013-03-11 06:08:02 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 13:31:56 +00:00
|
|
|
int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
|
|
|
|
const void *data, size_t data_len)
|
2008-05-16 09:10:33 +00:00
|
|
|
{
|
2013-08-29 13:31:56 +00:00
|
|
|
return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
|
2008-05-16 09:10:33 +00:00
|
|
|
}
|