Patchwork [U-Boot] Sandbox: Add spi driver to sandbox

login
register
mail settings
Submitter viktor.krivak@gmail.com
Date Oct. 4, 2012, 10:14 a.m.
Message ID <1349345664-30236-1-git-send-email-viktor.krivak@gmail.com>
Download mbox | patch
Permalink /patch/189084/
State Changes Requested
Delegated to: Tom Rini
Headers show

Comments

viktor.krivak@gmail.com - Oct. 4, 2012, 10:14 a.m.
From: Viktor Krivak <viktor.krivak@gmail.com>

Signed-off-by: Viktor Krivak <viktor.krivak@gmail.com>
---
 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
Tom Rini - Oct. 4, 2012, 10:23 p.m.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 10/04/12 03:14, viktor.krivak@gmail.com wrote:
> From: Viktor Krivak <viktor.krivak@gmail.com>
> 
> Signed-off-by: Viktor Krivak <viktor.krivak@gmail.com>
[snip]
> +#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

Please just define these in the config file and fail to build when unset.

[snip]
> +/* + * 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. + */

Almost kernel-doc style, but that needs /**, please update globally.

- -- 
Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQIcBAEBAgAGBQJQbgxVAAoJENk4IS6UOR1Wo18P/jrHcuWPckQ+gLsxbT1lZXaV
3iNeozB6rZOvrrMhbt8HKn83GclUrA8Od6NCMtW9U0DRBPn1mEJL1HlUe4TwYlPO
rtRQhvTQdaly1M9TFKCs4KKbedrJuypiQf9ToLW6qkHDC4ocDPg81Rsp2PtBdqB7
joFwd2xlpSAyaBxJpRk0+K98Qt6MxwU/C1c6pdZCgWCyaFAspg5VSEtX+Y6y3oG/
x1z9E8y/dVJqCyuMdXWch3fLsMFLFB/iFCyfgg649cNC40xuHc7rRO2rAdNDU9u7
wZ3aS/35Nm0iFL7JRL1sgcswUrPu0eqK24b+Mq+anfp+4z8vk0oh5pXYN0qi9uEW
QCB0jqNzhJQS0ZzZI6hY40DGzGXHdYHNB56BUlR/TW19QYFjeLNJAizbTFsTxC3v
u1U5u+vmZqoXo3VaX9G5WoxDpHYSLGMOon8B8aeU7BuBKLMZL+BE10Sfd2ubesto
AHG9nXPSkDfnyOmZXsG3RrdNNo0x9klGnKrUKC1tROaDKzqvcVH6M8WrHRBU7nX4
5khTph/tZZ+ZShBAMa1EGA5E9htqswDi2P6S4CUIIHD4DrAInY/S0/3Y/Z7Bv2IJ
r3okMqawaHOpwTklFHxJVFtlZIBnCHHlVD8BiUfAFR0IyOz5gMYuekPkrAJ8Mo4E
AGH8N0JH/AmaFFpxvmF4
=bGe/
-----END PGP SIGNATURE-----
Simon Glass - Oct. 9, 2012, 12:14 p.m.
Hi,

On Thu, Oct 4, 2012 at 3:14 AM,  <viktor.krivak@gmail.com> wrote:
> From: Viktor Krivak <viktor.krivak@gmail.com>
>
> Signed-off-by: Viktor Krivak <viktor.krivak@gmail.com>
> ---
>  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
>

Please can you take a look at Mike's work here also? He seems to have
done a similar thing.

http://patchwork.ozlabs.org/patch/146127/
http://patchwork.ozlabs.org/patch/146128/
http://patchwork.ozlabs.org/patch/146129/

Regards,
SImon

Patch

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 <viktor.krivak@gmail.com>
+ *
+ * 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 <linux/types.h>
+#include <asm/errno.h>
+#include <spi.h>
+#include <malloc.h>
+
+#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.
  */