From patchwork Mon Jan 23 06:30:25 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Frysinger X-Patchwork-Id: 137263 X-Patchwork-Delegate: vapier@gentoo.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 48C8FB6FA2 for ; Mon, 23 Jan 2012 17:30:35 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 17F5128181; Mon, 23 Jan 2012 07:30:30 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ppvIRSoyDEwf; Mon, 23 Jan 2012 07:30:29 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 2764128182; Mon, 23 Jan 2012 07:30:20 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 943332816F for ; Mon, 23 Jan 2012 07:30:14 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pL5tbp9yZ7B0 for ; Mon, 23 Jan 2012 07:30:14 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by theia.denx.de (Postfix) with ESMTPS id BC09D28170 for ; Mon, 23 Jan 2012 07:30:11 +0100 (CET) Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 2A7C61B4029; Mon, 23 Jan 2012 06:30:09 +0000 (UTC) From: Mike Frysinger To: u-boot@lists.denx.de Date: Mon, 23 Jan 2012 01:30:25 -0500 Message-Id: <1327300228-26386-4-git-send-email-vapier@gentoo.org> X-Mailer: git-send-email 1.7.8.3 In-Reply-To: <1327300228-26386-1-git-send-email-vapier@gentoo.org> References: <1327300228-26386-1-git-send-email-vapier@gentoo.org> Subject: [U-Boot] [PATCH 3/6] sandbox: SPI emulation bus X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de This adds a SPI framework for people to hook up simulated SPI clients. Signed-off-by: Mike Frysinger --- 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 + +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 +#include +#include +#include + +#include + +#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; +}