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

Mike Frysinger vapier at gentoo.org
Mon Jan 23 07:30:25 CET 2012


This adds a SPI framework for people to hook up simulated SPI clients.

Signed-off-by: Mike Frysinger <vapier at 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

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);
+};
+
+static inline void sb_spi_tristate(u8 *buf, uint len)
+{
+	/*
+	 * 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];
+	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)
+{
+}
+
+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("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)
+{
+	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;
+	}
+
+	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");
+	}
+	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



More information about the U-Boot mailing list