mirror of
https://github.com/brain-hackers/u-boot-brain
synced 2024-09-30 08:30:50 +09:00
sandbox: spi: Add SPI emulation bus
This adds a SPI framework for people to hook up simulated SPI clients. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
parent
afb6134f30
commit
6122813fa2
@ -9,4 +9,12 @@
|
|||||||
|
|
||||||
#define CONFIG_SANDBOX_ARCH
|
#define CONFIG_SANDBOX_ARCH
|
||||||
|
|
||||||
|
/* Used by drivers/spi/sandbox_spi.c and arch/sandbox/include/asm/state.h */
|
||||||
|
#ifndef CONFIG_SANDBOX_SPI_MAX_BUS
|
||||||
|
#define CONFIG_SANDBOX_SPI_MAX_BUS 1
|
||||||
|
#endif
|
||||||
|
#ifndef CONFIG_SANDBOX_SPI_MAX_CS
|
||||||
|
#define CONFIG_SANDBOX_SPI_MAX_CS 10
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
58
arch/sandbox/include/asm/spi.h
Normal file
58
arch/sandbox/include/asm/spi.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Simulate a SPI port and clients (see README.sandbox for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2011-2013 The Chromium OS Authors.
|
||||||
|
* See file CREDITS for list of people who contributed to this
|
||||||
|
* project.
|
||||||
|
*
|
||||||
|
* Licensed under the GPL-2 or later.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __ASM_SPI_H__
|
||||||
|
#define __ASM_SPI_H__
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The interface between the SPI bus and the SPI client. The bus will
|
||||||
|
* instantiate a client, and that then call into it via these entry
|
||||||
|
* points. These should be enough for the client to emulate the SPI
|
||||||
|
* device just like the real hardware.
|
||||||
|
*/
|
||||||
|
struct sandbox_spi_emu_ops {
|
||||||
|
/* The bus wants to instantiate a new client, so setup everything */
|
||||||
|
int (*setup)(void **priv, const char *spec);
|
||||||
|
/* The bus is done with us, so break things down */
|
||||||
|
void (*free)(void *priv);
|
||||||
|
/* The CS has been "activated" -- we won't worry about low/high */
|
||||||
|
void (*cs_activate)(void *priv);
|
||||||
|
/* The CS has been "deactivated" -- we won't worry about low/high */
|
||||||
|
void (*cs_deactivate)(void *priv);
|
||||||
|
/* The client is rx-ing bytes from the bus, so it should tx some */
|
||||||
|
int (*xfer)(void *priv, const u8 *rx, u8 *tx, uint bytes);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* There are times when the data lines are allowed to tristate. What
|
||||||
|
* is actually sensed on the line depends on the hardware. It could
|
||||||
|
* always be 0xFF/0x00 (if there are pull ups/downs), or things could
|
||||||
|
* float and so we'd get garbage back. This func encapsulates that
|
||||||
|
* scenario so we can worry about the details here.
|
||||||
|
*/
|
||||||
|
static inline void sandbox_spi_tristate(u8 *buf, uint len)
|
||||||
|
{
|
||||||
|
/* XXX: make this into a user config option ? */
|
||||||
|
memset(buf, 0xff, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Extract the bus/cs from the spi spec and return the start of the spi
|
||||||
|
* client spec. If the bus/cs are invalid for the current config, then
|
||||||
|
* it returns NULL.
|
||||||
|
*
|
||||||
|
* Example: arg="0:1:foo" will set bus to 0, cs to 1, and return "foo"
|
||||||
|
*/
|
||||||
|
const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
|
||||||
|
unsigned long *cs);
|
||||||
|
|
||||||
|
#endif
|
@ -15,6 +15,11 @@ enum exit_type_id {
|
|||||||
STATE_EXIT_POWER_OFF,
|
STATE_EXIT_POWER_OFF,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct sandbox_spi_info {
|
||||||
|
const char *spec;
|
||||||
|
const struct sandbox_spi_emu_ops *ops;
|
||||||
|
};
|
||||||
|
|
||||||
/* The complete state of the test system */
|
/* The complete state of the test system */
|
||||||
struct sandbox_state {
|
struct sandbox_state {
|
||||||
const char *cmd; /* Command to execute */
|
const char *cmd; /* Command to execute */
|
||||||
@ -23,6 +28,10 @@ struct sandbox_state {
|
|||||||
const char *parse_err; /* Error to report from parsing */
|
const char *parse_err; /* Error to report from parsing */
|
||||||
int argc; /* Program arguments */
|
int argc; /* Program arguments */
|
||||||
char **argv;
|
char **argv;
|
||||||
|
|
||||||
|
/* Pointer to information for each SPI bus/cs */
|
||||||
|
struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS]
|
||||||
|
[CONFIG_SANDBOX_SPI_MAX_CS];
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,6 +27,7 @@ obj-$(CONFIG_MXC_SPI) += mxc_spi.o
|
|||||||
obj-$(CONFIG_MXS_SPI) += mxs_spi.o
|
obj-$(CONFIG_MXS_SPI) += mxs_spi.o
|
||||||
obj-$(CONFIG_OC_TINY_SPI) += oc_tiny_spi.o
|
obj-$(CONFIG_OC_TINY_SPI) += oc_tiny_spi.o
|
||||||
obj-$(CONFIG_OMAP3_SPI) += omap3_spi.o
|
obj-$(CONFIG_OMAP3_SPI) += omap3_spi.o
|
||||||
|
obj-$(CONFIG_SANDBOX_SPI) += sandbox_spi.o
|
||||||
obj-$(CONFIG_SOFT_SPI) += soft_spi.o
|
obj-$(CONFIG_SOFT_SPI) += soft_spi.o
|
||||||
obj-$(CONFIG_SH_SPI) += sh_spi.o
|
obj-$(CONFIG_SH_SPI) += sh_spi.o
|
||||||
obj-$(CONFIG_FSL_ESPI) += fsl_espi.o
|
obj-$(CONFIG_FSL_ESPI) += fsl_espi.o
|
||||||
|
204
drivers/spi/sandbox_spi.c
Normal file
204
drivers/spi/sandbox_spi.c
Normal file
@ -0,0 +1,204 @@
|
|||||||
|
/*
|
||||||
|
* Simulate a SPI port
|
||||||
|
*
|
||||||
|
* Copyright (c) 2011-2013 The Chromium OS Authors.
|
||||||
|
* See file CREDITS for list of people who contributed to this
|
||||||
|
* project.
|
||||||
|
*
|
||||||
|
* Licensed under the GPL-2 or later.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <common.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <spi.h>
|
||||||
|
#include <os.h>
|
||||||
|
|
||||||
|
#include <asm/errno.h>
|
||||||
|
#include <asm/spi.h>
|
||||||
|
#include <asm/state.h>
|
||||||
|
|
||||||
|
#ifndef CONFIG_SPI_IDLE_VAL
|
||||||
|
# define CONFIG_SPI_IDLE_VAL 0xFF
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct sandbox_spi_slave {
|
||||||
|
struct spi_slave slave;
|
||||||
|
const struct sandbox_spi_emu_ops *ops;
|
||||||
|
void *priv;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define to_sandbox_spi_slave(s) container_of(s, struct sandbox_spi_slave, slave)
|
||||||
|
|
||||||
|
const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
|
||||||
|
unsigned long *cs)
|
||||||
|
{
|
||||||
|
char *endp;
|
||||||
|
|
||||||
|
*bus = simple_strtoul(arg, &endp, 0);
|
||||||
|
if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
*cs = simple_strtoul(endp + 1, &endp, 0);
|
||||||
|
if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return endp + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int spi_cs_is_valid(unsigned int bus, unsigned int cs)
|
||||||
|
{
|
||||||
|
return bus < CONFIG_SANDBOX_SPI_MAX_BUS &&
|
||||||
|
cs < CONFIG_SANDBOX_SPI_MAX_CS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void spi_cs_activate(struct spi_slave *slave)
|
||||||
|
{
|
||||||
|
struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
|
||||||
|
|
||||||
|
debug("sandbox_spi: activating CS\n");
|
||||||
|
if (sss->ops->cs_activate)
|
||||||
|
sss->ops->cs_activate(sss->priv);
|
||||||
|
}
|
||||||
|
|
||||||
|
void spi_cs_deactivate(struct spi_slave *slave)
|
||||||
|
{
|
||||||
|
struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
|
||||||
|
|
||||||
|
debug("sandbox_spi: deactivating CS\n");
|
||||||
|
if (sss->ops->cs_deactivate)
|
||||||
|
sss->ops->cs_deactivate(sss->priv);
|
||||||
|
}
|
||||||
|
|
||||||
|
void spi_init(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void spi_set_speed(struct spi_slave *slave, uint hz)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
|
||||||
|
unsigned int max_hz, unsigned int mode)
|
||||||
|
{
|
||||||
|
struct sandbox_spi_slave *sss;
|
||||||
|
struct sandbox_state *state = state_get_current();
|
||||||
|
const char *spec;
|
||||||
|
|
||||||
|
if (!spi_cs_is_valid(bus, cs)) {
|
||||||
|
debug("sandbox_spi: Invalid SPI bus/cs\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
sss = spi_alloc_slave(struct sandbox_spi_slave, bus, cs);
|
||||||
|
if (!sss) {
|
||||||
|
debug("sandbox_spi: Out of memory\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
spec = state->spi[bus][cs].spec;
|
||||||
|
sss->ops = state->spi[bus][cs].ops;
|
||||||
|
if (!spec || !sss->ops || sss->ops->setup(&sss->priv, spec)) {
|
||||||
|
free(sss);
|
||||||
|
printf("sandbox_spi: unable to locate a slave client\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &sss->slave;
|
||||||
|
}
|
||||||
|
|
||||||
|
void spi_free_slave(struct spi_slave *slave)
|
||||||
|
{
|
||||||
|
struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
|
||||||
|
|
||||||
|
debug("sandbox_spi: releasing slave\n");
|
||||||
|
|
||||||
|
if (sss->ops->free)
|
||||||
|
sss->ops->free(sss->priv);
|
||||||
|
|
||||||
|
free(sss);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int spi_bus_claim_cnt[CONFIG_SANDBOX_SPI_MAX_BUS];
|
||||||
|
|
||||||
|
int spi_claim_bus(struct spi_slave *slave)
|
||||||
|
{
|
||||||
|
if (spi_bus_claim_cnt[slave->bus]++) {
|
||||||
|
printf("sandbox_spi: error: bus already claimed: %d!\n",
|
||||||
|
spi_bus_claim_cnt[slave->bus]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void spi_release_bus(struct spi_slave *slave)
|
||||||
|
{
|
||||||
|
if (--spi_bus_claim_cnt[slave->bus]) {
|
||||||
|
printf("sandbox_spi: error: bus freed too often: %d!\n",
|
||||||
|
spi_bus_claim_cnt[slave->bus]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
|
||||||
|
void *din, unsigned long flags)
|
||||||
|
{
|
||||||
|
struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
|
||||||
|
uint bytes = bitlen / 8, i;
|
||||||
|
int ret = 0;
|
||||||
|
u8 *tx = (void *)dout, *rx = din;
|
||||||
|
|
||||||
|
if (bitlen == 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
/* we can only do 8 bit transfers */
|
||||||
|
if (bitlen % 8) {
|
||||||
|
printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
|
||||||
|
bitlen);
|
||||||
|
flags |= SPI_XFER_END;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & SPI_XFER_BEGIN)
|
||||||
|
spi_cs_activate(slave);
|
||||||
|
|
||||||
|
/* make sure rx/tx buffers are full so clients can assume */
|
||||||
|
if (!tx) {
|
||||||
|
debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
|
||||||
|
tx = malloc(bytes);
|
||||||
|
if (!tx) {
|
||||||
|
debug("sandbox_spi: Out of memory\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!rx) {
|
||||||
|
debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
|
||||||
|
rx = malloc(bytes);
|
||||||
|
if (!rx) {
|
||||||
|
debug("sandbox_spi: Out of memory\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug("sandbox_spi: xfer: bytes = %u\n tx:", bytes);
|
||||||
|
for (i = 0; i < bytes; ++i)
|
||||||
|
debug(" %u:%02x", i, tx[i]);
|
||||||
|
debug("\n");
|
||||||
|
|
||||||
|
ret = sss->ops->xfer(sss->priv, tx, rx, bytes);
|
||||||
|
|
||||||
|
debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
|
||||||
|
ret, ret ? "bad" : "good");
|
||||||
|
for (i = 0; i < bytes; ++i)
|
||||||
|
debug(" %u:%02x", i, rx[i]);
|
||||||
|
debug("\n");
|
||||||
|
|
||||||
|
if (tx != dout)
|
||||||
|
free(tx);
|
||||||
|
if (rx != din)
|
||||||
|
free(rx);
|
||||||
|
|
||||||
|
done:
|
||||||
|
if (flags & SPI_XFER_END)
|
||||||
|
spi_cs_deactivate(slave);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user