summaryrefslogtreecommitdiff
path: root/common/cmd_spi.c
diff options
context:
space:
mode:
authorTom Rini <trini@ti.com>2014-09-26 20:10:48 -0400
committerTom Rini <trini@ti.com>2014-09-26 20:10:48 -0400
commitbe9f643ae6aa9044c60fe80e3a2c10be8371c692 (patch)
treeb0b2226a1bf2e382257c56165d4c1269b057980b /common/cmd_spi.c
parent2c2277f15cbaa9533ef8aead7328c3f605251147 (diff)
parent248a0488bfbb2eb16dee408a976d5f4b5546bb51 (diff)
downloadu-boot-imx-be9f643ae6aa9044c60fe80e3a2c10be8371c692.zip
u-boot-imx-be9f643ae6aa9044c60fe80e3a2c10be8371c692.tar.gz
u-boot-imx-be9f643ae6aa9044c60fe80e3a2c10be8371c692.tar.bz2
Merge branch 'for-tom' of git://git.denx.de/u-boot-dm
Diffstat (limited to 'common/cmd_spi.c')
-rw-r--r--common/cmd_spi.c53
1 files changed, 32 insertions, 21 deletions
diff --git a/common/cmd_spi.c b/common/cmd_spi.c
index 3c8e913..be5709c 100644
--- a/common/cmd_spi.c
+++ b/common/cmd_spi.c
@@ -11,6 +11,7 @@
#include <common.h>
#include <command.h>
+#include <errno.h>
#include <spi.h>
/*-----------------------------------------------------------------------
@@ -38,6 +39,35 @@ static int bitlen;
static uchar dout[MAX_SPI_BYTES];
static uchar din[MAX_SPI_BYTES];
+static int do_spi_xfer(int bus, int cs)
+{
+ struct spi_slave *slave;
+ int rcode = 0;
+
+ slave = spi_setup_slave(bus, cs, 1000000, mode);
+ if (!slave) {
+ printf("Invalid device %d:%d\n", bus, cs);
+ return -EINVAL;
+ }
+
+ spi_claim_bus(slave);
+ if (spi_xfer(slave, bitlen, dout, din,
+ SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
+ printf("Error during SPI transaction\n");
+ rcode = -EIO;
+ } else {
+ int j;
+
+ for (j = 0; j < ((bitlen + 7) / 8); j++)
+ printf("%02X", din[j]);
+ printf("\n");
+ }
+ spi_release_bus(slave);
+ spi_free_slave(slave);
+
+ return rcode;
+}
+
/*
* SPI read/write
*
@@ -51,11 +81,9 @@ static uchar din[MAX_SPI_BYTES];
int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
- struct spi_slave *slave;
char *cp = 0;
uchar tmp;
int j;
- int rcode = 0;
/*
* We use the last specified parameters, unless new ones are
@@ -103,27 +131,10 @@ int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
return 1;
}
- slave = spi_setup_slave(bus, cs, 1000000, mode);
- if (!slave) {
- printf("Invalid device %d:%d\n", bus, cs);
+ if (do_spi_xfer(bus, cs))
return 1;
- }
-
- spi_claim_bus(slave);
- if(spi_xfer(slave, bitlen, dout, din,
- SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
- printf("Error during SPI transaction\n");
- rcode = 1;
- } else {
- for(j = 0; j < ((bitlen + 7) / 8); j++) {
- printf("%02X", din[j]);
- }
- printf("\n");
- }
- spi_release_bus(slave);
- spi_free_slave(slave);
- return rcode;
+ return 0;
}
/***************************************************/