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 |   48 ++++++++++
 drivers/spi/Makefile           |    1 +
 drivers/spi/sandbox_spi.c      |  198 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 247 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/spi.h b/arch/sandbox/include/asm/spi.h
new file mode 100644
index 0000000..8de19c4
--- /dev/null
+++ b/arch/sandbox/include/asm/spi.h
@@ -0,0 +1,48 @@
+/*
+ * 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>
+
+/*
+ * 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 {
+       /* 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 sb_spi_tristate(u8 *buf, uint len)
+{
+       /* XXX: make this into a user config option ? */
+       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..d0cba76
--- /dev/null
+++ b/drivers/spi/sandbox_spi.c
@@ -0,0 +1,198 @@
+/*
+ * 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];
+       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);
+
+       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;
+
+       if (!spi_cs_is_valid(bus, cs))
+               return NULL;
+
+       sss = malloc(sizeof(*sss));
+       if (!sss)
+               return NULL;
+
+       sss->spec = sb_lookup_arg(bus, cs);
+       if (sb_parse_type(sss)) {
+               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;
+
+int spi_claim_bus(struct spi_slave *slave)
+{
+       if (spi_bus_claim_cnt++)
+               printf("sb_spi: error: bus already claimed!\n");
+       return 0;
+}
+
+void spi_release_bus(struct spi_slave *slave)
+{
+       if (--spi_bus_claim_cnt)
+               printf("sb_spi: error: bus freed too often!\n");
+}
+
+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);
+       }
+       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.4

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to