From patchwork Thu Oct 4 10:14:24 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: viktor.krivak@gmail.com X-Patchwork-Id: 189084 X-Patchwork-Delegate: trini@ti.com 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 678B72C00A8 for ; Thu, 4 Oct 2012 20:14:50 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 3DD0C28125; Thu, 4 Oct 2012 12:14:47 +0200 (CEST) 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 s69sEDWgh9pc; Thu, 4 Oct 2012 12:14:46 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 13E8B28087; Thu, 4 Oct 2012 12:14:45 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1A4A228087 for ; Thu, 4 Oct 2012 12:14:43 +0200 (CEST) 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 388WdSUA8S5i for ; Thu, 4 Oct 2012 12:14:42 +0200 (CEST) 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 mail-bk0-f44.google.com (mail-bk0-f44.google.com [209.85.214.44]) by theia.denx.de (Postfix) with ESMTPS id 5C0FB28086 for ; Thu, 4 Oct 2012 12:14:40 +0200 (CEST) Received: by mail-bk0-f44.google.com with SMTP id jc3so201641bkc.3 for ; Thu, 04 Oct 2012 03:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=88IUvGZAQn6hvLxEVCiOvYHvQS2mSqCG0a1b4FOVkuU=; b=PhfolqoUqBDfVIzqJhgJL9w8o6IAbqo7xqUtUIj7xTpD8wLyRUlRNOZo17h0a433MU wpG/S8/wA6ysDWzfuXRn4gdxFnqdS8/om4/cmbxnY3b1twHTGiXNpB+8CrndSlcP15r5 XJzGwNu2Lpnpm9KKT5PQU2omfICE11q8P4cpzb3eU5zGDtu64FRI+pwWw3bgBxFLECeX JvAjfmVxgBl4u3adimQL2Cl0E/uF3wcEQMmlGt3Cb+JWiCbz40gt/d05Zpj2GFFGWwvJ rqQu7klK7sLqrqWOLE7u2UuLj0MJyMYMNuK7FrCN/FrQRUOX+x+cMQh5U1sSshAa55xE xe+A== Received: by 10.204.0.74 with SMTP id 10mr247444bka.83.1349345680503; Thu, 04 Oct 2012 03:14:40 -0700 (PDT) Received: from viktor.kolej.mff.cuni.cz (pafw.wpb.cz. [62.77.71.46]) by mx.google.com with ESMTPS id z22sm5171966bkw.2.2012.10.04.03.14.39 (version=SSLv3 cipher=OTHER); Thu, 04 Oct 2012 03:14:39 -0700 (PDT) From: viktor.krivak@gmail.com To: u-boot@lists.denx.de Date: Thu, 4 Oct 2012 12:14:24 +0200 Message-Id: <1349345664-30236-1-git-send-email-viktor.krivak@gmail.com> X-Mailer: git-send-email 1.7.9.5 Cc: trini@ti.com Subject: [U-Boot] [PATCH] Sandbox: Add spi driver to sandbox 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 From: Viktor Krivak Signed-off-by: Viktor Krivak --- Simple spi driver which only cache input data and send them back on next call. Usefull for high level testing in sandbox. drivers/spi/Makefile | 1 + drivers/spi/sandbox.c | 162 +++++++++++++++++++++++++++++++++++++++++++++ include/configs/sandbox.h | 3 + 3 files changed, 166 insertions(+) create mode 100644 drivers/spi/sandbox.c diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index f0b82c6..a3b44fd 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -46,6 +46,7 @@ COBJS-$(CONFIG_SH_SPI) += sh_spi.o COBJS-$(CONFIG_FSL_ESPI) += fsl_espi.o COBJS-$(CONFIG_TEGRA_SPI) += tegra_spi.o COBJS-$(CONFIG_XILINX_SPI) += xilinx_spi.o +COBJS-$(CONFIG_SANDBOX_SPI) += sandbox.o COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/drivers/spi/sandbox.c b/drivers/spi/sandbox.c new file mode 100644 index 0000000..d76e38cf --- /dev/null +++ b/drivers/spi/sandbox.c @@ -0,0 +1,162 @@ +/* + * (C) Copyright 2012 + * Viktor Krivak + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include +#include +#include +#include + +#ifndef CONFIG_SANDBOX_SPI_BUS_NUM + #define CONFIG_SANDBOX_SPI_BUS_NUM 1 +#endif + +#ifndef CONFIG_SANDBOX_SPI_CS_NUM + #define CONFIG_SANDBOX_SPI_CS_NUM 1 +#endif + +#ifndef CONFIG_SANDBOX_SPI_BUFFER_LEN + #define CONFIG_SANDBOX_SPI_BUFFER_LEN 32 +#endif + +static unsigned char spi_bus_claimed[CONFIG_SANDBOX_SPI_BUS_NUM]; +static unsigned char *buffer[CONFIG_SANDBOX_SPI_BUFFER_LEN]; + +/* + * spi_init() - Init spi + * + * Empty function. No init in sandbox. + */ +void spi_init(void) +{ + /* Do nothing */ +} + +/* + * spi_cs_is_valid() - Check if bus and cs are valid number + * + * @bus: Bus id + * @cs: Chip select id + * + * Compare bus and cs against defined constant. Return 0 if everything is ok + * and other value on error. + */ +int spi_cs_is_valid(unsigned int bus, unsigned int cs) +{ + if (!(bus < CONFIG_SANDBOX_SPI_BUS_NUM || + cs < CONFIG_SANDBOX_SPI_CS_NUM)) + return -EINVAL; + else + return 0; +} + +/* + * spi_setup() - Setup spi + * + * @bus: Bus id + * @cs: Chip select id + * @max_hz: Maximum SCK rate in Hz (ignored) + * @mode: Clock polarity, clock phase and other parameters(ignored) + * + * Check if bus and cs are valid and allocate memory for slave structure. + * Return pointer to slave stucture of NULL if something fail. + */ +struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, + unsigned int max_hz, unsigned int mode) +{ + struct spi_slave *slave; + if (spi_cs_is_valid(bus, cs)) + return NULL; + slave = malloc(sizeof(slave)); + if (!slave) + return NULL; + slave->bus = bus; + slave->cs = cs; + return slave; +} + +/* + * spi_claim_bus() - Claim bus + * + * @slave: Pointer to slave struct + * + * Check if slave struct contain valid data and claim bus for it. If bus + * already claimed return error. If everything ok return 0. + */ +int spi_claim_bus(struct spi_slave *slave) +{ + if (spi_cs_is_valid(slave->bus, slave->cs)) + return -EINVAL; + if (!spi_bus_claimed[slave->bus]) { + spi_bus_claimed[slave->bus] = 1; + return 0; + } + return -EBUSY; +} + +/* + * spi_release_bus() - Release claim on bus + * + * @slave: Pointer to slave struct + * + * Release claim on bus defined in slave struct + */ +void spi_release_bus(struct spi_slave *slave) +{ + if (spi_cs_is_valid(slave->bus, slave->cs)) + return; + spi_bus_claimed[slave->bus] = 0; +} + +/* + * spi_xfer() - Send data throw SPI + * + * @slave: Pointer to slave struct + * @bitlen: Number of send or received bits + * @dout: Pointer to send buffer + * @din: Pointer to receive buffer + * @flags: Flags for SPI(ignored) + * + * Return data that was send in prevois call. Copy din to internal buffer and + * copy internal buffer to dout. Use temp variable for case when din = dout. + */ +int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, + void *din, unsigned long flags) +{ + unsigned char *buffer_temp[CONFIG_SANDBOX_SPI_BUFFER_LEN]; + memcpy(buffer_temp, dout, bitlen); + memcpy(din, buffer, bitlen); + memcpy(buffer, buffer_temp, bitlen); + return 0; +} + +/* + * spi_free_slave() - Destroy slave struct + * + * @slave: Pointer to slave struct + * + * Call free on slave struct + */ +void spi_free_slave(struct spi_slave *slave) +{ + free(slave); +} diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h index 9c431bf..0ea4438 100644 --- a/include/configs/sandbox.h +++ b/include/configs/sandbox.h @@ -38,6 +38,9 @@ #define CONFIG_SANDBOX_GPIO #define CONFIG_SANDBOX_GPIO_COUNT 20 +#define CONFIG_CMD_SPI +#define CONFIG_SANDBOX_SPI + /* * Size of malloc() pool, although we don't actually use this yet. */