[U-Boot] [PATCH 1/3 v3] sandbox: SPI emulation bus

Simon Glass sjg at chromium.org
Thu Mar 15 05:03:28 CET 2012


Hi Mike,

On Mon, Mar 12, 2012 at 8:22 AM, Mike Frysinger <vapier at gentoo.org> wrote:
> This adds a SPI framework for people to hook up simulated SPI clients.
>
> Signed-off-by: Mike Frysinger <vapier at gentoo.org>
> ---
> v3
>        - rearchitected on top of state/getopt support
>
>  arch/sandbox/include/asm/config.h |    8 ++
>  arch/sandbox/include/asm/spi.h    |   58 ++++++++++++
>  arch/sandbox/include/asm/state.h  |    1 +
>  drivers/spi/Makefile              |    1 +
>  drivers/spi/sandbox_spi.c         |  186 +++++++++++++++++++++++++++++++++++++
>  5 files changed, 254 insertions(+), 0 deletions(-)
>  create mode 100644 arch/sandbox/include/asm/spi.h
>  create mode 100644 drivers/spi/sandbox_spi.c
>
> diff --git a/arch/sandbox/include/asm/config.h b/arch/sandbox/include/asm/config.h
> index 2ef0564..a9590bc 100644
> --- a/arch/sandbox/include/asm/config.h
> +++ b/arch/sandbox/include/asm/config.h
> @@ -23,4 +23,12 @@
>
>  #define CONFIG_SANDBOX_ARCH
>
> +/* Used by drivers/spi/sandbox_spi.c */
> +#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

Why not put this at the top of that driver? Also shouldn't these be
added to the README?

> +
>  #endif
> diff --git a/arch/sandbox/include/asm/spi.h b/arch/sandbox/include/asm/spi.h
> new file mode 100644
> index 0000000..f96a926
> --- /dev/null
> +++ b/arch/sandbox/include/asm/spi.h
> @@ -0,0 +1,58 @@
> +/*
> + * Simulate a SPI port and clients

It would be nice if you could add a few more details about what is
simulated and how to use it.

> + *
> + * 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>
> +
> +/*
> + * 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 sb_spi_emu_ops {

I would really like to have sandbox_ written out instead of sb_. I
thought you were ok with that for exported functions / structures at
least?

> +       /* 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);

Can we have full docs on each of these functions? They look similar to
spi.h but not the same?

> +};
> +
> +/*
> + * 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 sb_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 *sb_spi_parse_spec(const char *arg, unsigned long *bus,
> +                              unsigned long *cs);
> +
> +#endif
> diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
> index 2b62b46..ea5fbe0 100644
> --- a/arch/sandbox/include/asm/state.h
> +++ b/arch/sandbox/include/asm/state.h
> @@ -38,6 +38,7 @@ struct sandbox_state {
>        const char *parse_err;          /* Error to report from parsing */
>        int argc;                       /* Program arguments */
>        char **argv;
> +       const void *spi[CONFIG_SANDBOX_SPI_MAX_BUS][CONFIG_SANDBOX_SPI_MAX_CS][2];

OK now I see why you need it in config.h.

>  };
>
>  /**
> 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..bb89c7a
> --- /dev/null
> +++ b/drivers/spi/sandbox_spi.c
> @@ -0,0 +1,186 @@
> +/*
> + * 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>
> +#include <asm/state.h>
> +
> +#ifndef CONFIG_SPI_IDLE_VAL
> +# define CONFIG_SPI_IDLE_VAL 0xFF
> +#endif

Add to README?

> +
> +struct sb_spi_slave {
> +       struct spi_slave slave;
> +       const struct sb_spi_emu_ops *ops;
> +       void *priv;
> +};
> +
> +#define to_sb_spi_slave(s) container_of(s, struct sb_spi_slave, slave)
> +
> +const char *sb_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 sb_spi_slave *sss = to_sb_spi_slave(slave);
> +
> +       debug("sb_spi: activating CS\n");
> +       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);
> +
> +       debug("sb_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 sb_spi_slave *sss;
> +       struct sandbox_state *state = state_get_current();
> +       const char *spec;
> +
> +       if (!spi_cs_is_valid(bus, cs))
> +               return NULL;
> +
> +       sss = malloc(sizeof(*sss));
> +       if (!sss)
> +               return NULL;

Do we want debug() for those two returns?

> +
> +       spec = state->spi[bus][cs][1];
> +       sss->ops = state->spi[bus][cs][0];
> +       if (!spec || !sss->ops || sss->ops->setup(&sss->priv, spec)) {
> +               free(sss);
> +               printf("sb_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);
> +
> +       debug("sb_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("sb_spi: error: bus already claimed!\n");
> +       return 0;
> +}
> +
> +void spi_release_bus(struct spi_slave *slave)
> +{
> +       if (--spi_bus_claim_cnt[slave->bus])
> +               printf("sb_spi: error: bus freed too often!\n");

Can we assert() here also?

> +}
> +
> +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) {
> +               printf("sb_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("sb_spi: xfer: auto-allocating tx scratch buffer\n");
> +               tx = malloc(bytes);
> +               if (tx)
> +                       memset(tx, CONFIG_SPI_IDLE_VAL, bytes);
> +       }
> +       if (!rx) {
> +               debug("sb_spi: xfer: auto-allocating rx scratch buffer\n");
> +               rx = malloc(bytes);

assert()?

I'm not sure what is supposed to happen if rx / tx allow fails. It
seems that things are then broken, yet we continue regardless.
> +       }
> +       if (tx && rx) {
> +               debug("sb_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("sb_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;
> +}
> --
> 1.7.8.5
>

Regards,
Simon


More information about the U-Boot mailing list