Hi Mike, On Sun, Jan 22, 2012 at 10:30 PM, Mike Frysinger <vap...@gentoo.org> wrote: > This adds a SPI framework for people to hook up simulated SPI clients. > > Signed-off-by: Mike Frysinger <vap...@gentoo.org> > --- > arch/sandbox/include/asm/spi.h | 33 +++++++ > drivers/spi/Makefile | 1 + > drivers/spi/sandbox_spi.c | 183 > ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 217 insertions(+), 0 deletions(-) > create mode 100644 arch/sandbox/include/asm/spi.h > create mode 100644 drivers/spi/sandbox_spi.c
I don't know the SPI interface as well as use - might be useful to have a few comments and debug() things when things go wrong I think. Please see below for some ideas. > > diff --git a/arch/sandbox/include/asm/spi.h b/arch/sandbox/include/asm/spi.h > new file mode 100644 > index 0000000..082df37 > --- /dev/null > +++ b/arch/sandbox/include/asm/spi.h > @@ -0,0 +1,33 @@ > +/* > + * Simulate a SPI port and clients > + * > + * Copyright (c) 2011-2012 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> > + > +struct sb_spi_emu_ops { > + int (*setup)(void **priv, const char *spec); > + void (*free)(void *priv); > + void (*cs_activate)(void *priv); > + void (*cs_deactivate)(void *priv); > + int (*xfer)(void *priv, const u8 *tx, u8 *rx, uint bytes); > +}; I think this could do with some comments. > + > +static inline void sb_spi_tristate(u8 *buf, uint len) What does this do - comments? > +{ > + /* > + * XXX: make this into a user config option ? let them pick > + * whether this is pulled low, or high, or tristates. > + */ > + memset(buf, 0xff, len); > +} > + > +#endif > diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile > index c967d87..d80ff8b 100644 > --- a/drivers/spi/Makefile > +++ b/drivers/spi/Makefile > @@ -40,6 +40,7 @@ COBJS-$(CONFIG_MXC_SPI) += mxc_spi.o > COBJS-$(CONFIG_MXS_SPI) += mxs_spi.o > COBJS-$(CONFIG_OC_TINY_SPI) += oc_tiny_spi.o > COBJS-$(CONFIG_OMAP3_SPI) += omap3_spi.o > +COBJS-$(CONFIG_SANDBOX_SPI) += sandbox_spi.o > COBJS-$(CONFIG_SOFT_SPI) += soft_spi.o > COBJS-$(CONFIG_SH_SPI) += sh_spi.o > COBJS-$(CONFIG_FSL_ESPI) += fsl_espi.o > diff --git a/drivers/spi/sandbox_spi.c b/drivers/spi/sandbox_spi.c > new file mode 100644 > index 0000000..eda0f3e > --- /dev/null > +++ b/drivers/spi/sandbox_spi.c > @@ -0,0 +1,183 @@ > +/* > + * Simulate a SPI port > + * > + * Copyright (c) 2011-2012 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/spi.h> > + > +#ifndef CONFIG_SPI_IDLE_VAL > +# define CONFIG_SPI_IDLE_VAL 0xFF > +#endif > + > +struct sb_spi_slave { > + struct spi_slave slave; > + const char *spec; > + const struct sb_spi_emu_ops *ops; > + void *priv; > +}; > + > +#define to_sb_spi_slave(s) container_of(s, struct sb_spi_slave, slave) > + > +static const char *sb_lookup_arg(unsigned int bus, unsigned int cs) > +{ > + char sf_arg[20]; blank line? > + sprintf(sf_arg, "--spi-%u-%u", bus, cs); > + return os_getopt(sf_arg, 1); > +} > + > +static const struct { > + const char *spec; > + const struct sb_spi_emu_ops *ops; > +} sb_emu_map[] = { > +}; > + > +static int sb_parse_type(struct sb_spi_slave *sss) > +{ > + size_t i; > + > + for (i = 0; i < ARRAY_SIZE(sb_emu_map); ++i) { > + size_t len = strlen(sb_emu_map[i].spec); > + const char *sub_spec = sss->spec + len; > + > + sss->ops = sb_emu_map[i].ops; > + if (!memcmp(sss->spec, sb_emu_map[i].spec, len) && > + sss->spec[len] == ':') > + return sss->ops->setup(&sss->priv, sub_spec + 1); > + } > + > + return 1; > +} > + > +int spi_cs_is_valid(unsigned int bus, unsigned int cs) > +{ > + return sb_lookup_arg(bus, cs) ? 1 : 0; > +} > + > +void spi_cs_activate(struct spi_slave *slave) > +{ > + struct sb_spi_slave *sss = to_sb_spi_slave(slave); > + > + if (sss->ops->cs_activate) > + sss->ops->cs_activate(sss->priv); > +} > + > +void spi_cs_deactivate(struct spi_slave *slave) > +{ > + struct sb_spi_slave *sss = to_sb_spi_slave(slave); > + > + 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) > +{ Should this store the value somewhere? > +} > + > +struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, > + unsigned int max_hz, unsigned int mode) > +{ > + struct sb_spi_slave *sss; > + > + if (!spi_cs_is_valid(bus, cs)) > + return NULL; > + > + sss = malloc(sizeof(*sss)); > + if (!sss) > + return NULL; Do we want a debug() message for this? > + > + sss->spec = sb_lookup_arg(bus, cs); > + if (sb_parse_type(sss)) { > + 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 sb_spi_slave *sss = to_sb_spi_slave(slave); > + > + if (sss->ops->free) > + sss->ops->free(sss->priv); > + > + free(sss); > +} > + > +int spi_claim_bus(struct spi_slave *slave) > +{ Should we track claim/release for debugging purposes? > + return 0; > +} > + > +void spi_release_bus(struct spi_slave *slave) > +{ > +} > + > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, > + void *din, unsigned long flags) > +{ > + struct sb_spi_slave *sss = to_sb_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) { > + flags |= SPI_XFER_END; > + goto done; > + } debug() on these perhaps? > + > + if (flags & SPI_XFER_BEGIN) > + spi_cs_activate(slave); > + > + /* make sure rx/tx buffers are full so clients can assume */ > + if (!tx) { > + tx = malloc(bytes); > + if (tx) > + memset(tx, CONFIG_SPI_IDLE_VAL, bytes); > + } > + if (!rx) > + rx = malloc(bytes); > + if (tx && rx) { > + debug("sb_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("sb_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"); > + } debug() message if rx / tx is NULL? > + if (tx != dout) > + free(tx); > + if (rx != din) > + free(rx); > + > + done: > + if (flags & SPI_XFER_END) > + spi_cs_deactivate(slave); > + > + return ret; > +} > -- > 1.7.8.3 > _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot