Skip to contentSkip to conversation

support spi gpio driver by control gpio bitbang

PATCH New Donghwa Lee 2 participants 2 messages Sep 1, 2010 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 support spi gpio driver by control gpio bitbang

This patch adds basic support for spi mode 0~3 by control gpio bitbang. It uses several gpio pin and emulates spi chipselect signal, clock signal and sda signal as if spi controller generate spi signal.

---
I resend this patch because format of sended patch was wrong.

 drivers/spi/spi_gpio.c |  153 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/spi.h          |   27 +++++++++
 2 files changed, 180 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/spi_gpio.c
drivers/spi/spi_gpio.c new file +152 -0
new file mode 100644
index 0000000..cce809e
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2010 Samsung Electronics
+ * spi_gpio.c - SPI master driver using generic bitbanged GPIO
+ *
+ * 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 <common.h>
+#include <asm/arch/gpio.h>
+#include <spi.h>
+
+static void spi_gpio_set_sck(struct spi_platform_data *spi, int is_on)
+{
+	gpio_set_value(spi->clk_bank, spi->clk_num, is_on);
+}
+
+static void spi_gpio_set_mosi(struct spi_platform_data *spi, int is_on)
+{
+	gpio_set_value(spi->si_bank, spi->si_num, is_on);
+}
+
+static void spi_gpio_chipselect(struct spi_platform_data *spi,
+	int cpol)
+{
+	gpio_set_value(spi->cs_bank, spi->cs_num,
+		!spi->cs_active);
+
+	/* set initial clock polarity */
+	if (cpol)
+		spi_gpio_set_sck(spi, spi->mode & SPI_CPOL);
+
+	/* SPI is normally active-low */
+	gpio_set_value(spi->cs_bank, spi->cs_num,
+		spi->cs_active);
+}
+
+static void
+spi_gpio_tx_cpha0(struct spi_platform_data *spi,
+		unsigned int nsecs, unsigned int cpol,
+		unsigned int word, unsigned int bits)
+{
+	int i;
+	unsigned int data;
+
+	data = (word << spi->word_len) + bits;
+
+	spi_gpio_chipselect(spi, cpol);
+
+	/* clock starts at inactive polarity */
+	for (i = spi->word_len; i >= 0; i--) {
+
+		/* data high or low */
+		if ((data >> i) & 0x1)
+			spi_gpio_set_mosi(spi, 1);
+		else
+			spi_gpio_set_mosi(spi, 0);
+
+		udelay(nsecs);
+
+		spi_gpio_set_sck(spi, !cpol);
+		udelay(nsecs);
+
+		spi_gpio_set_sck(spi, cpol);
+	}
+}
+
+static void
+spi_gpio_tx_cpha1(struct spi_platform_data *spi,
+		unsigned int nsecs, unsigned int cpol,
+		unsigned int word, unsigned int bits)
+{
+	int i;
+	unsigned int data;
+
+	data = (word << spi->word_len) + bits;
+
+	spi_gpio_chipselect(spi, cpol);
+
+	/* clock starts at inactive polarity */
+	for (i = spi->word_len; i >= 0; i--) {
+
+		/* setup MSB (to slave) on leading edge */
+		spi_gpio_set_sck(spi, !cpol);
+
+		/* data high or low */
+		if ((data >> i) & 0x1)
+			spi_gpio_set_mosi(spi, 1);
+		else
+			spi_gpio_set_mosi(spi, 0);
+
+		udelay(nsecs);
+
+		spi_gpio_set_sck(spi, cpol);
+		udelay(nsecs);
+	}
+}
+
+static void spi_gpio_tx_word_mode0(struct spi_platform_data *spi,
+		unsigned int nsecs, unsigned int word, unsigned int bits)
+{
+	return spi_gpio_tx_cpha0(spi, nsecs, 0, word, bits);
+}
+
+static void spi_gpio_tx_word_mode1(struct spi_platform_data *spi,
+		unsigned int nsecs, unsigned int word, unsigned int bits)
+{
+	return spi_gpio_tx_cpha1(spi, nsecs, 0, word, bits);
+}
+
+static void spi_gpio_tx_word_mode2(struct spi_platform_data *spi,
+		unsigned int nsecs, unsigned int word, unsigned int bits)
+{
+	return spi_gpio_tx_cpha0(spi, nsecs, 1, word, bits);
+}
+
+static void spi_gpio_tx_word_mode3(struct spi_platform_data *spi,
+		unsigned int nsecs, unsigned int word, unsigned int bits)
+{
+	return spi_gpio_tx_cpha1(spi, nsecs, 1, word, bits);
+}
+
+void spi_gpio_write(struct spi_platform_data *spi,
+	unsigned int address, unsigned int command)
+{
+	switch (spi->mode) {
+	case SPI_MODE_0:
+		spi_gpio_tx_word_mode0(spi,
+			1, address, command);
+	case SPI_MODE_1:
+		spi_gpio_tx_word_mode1(spi,
+			1, address, command);
+	case SPI_MODE_2:
+		spi_gpio_tx_word_mode2(spi,
+			1, address, command);
+	case SPI_MODE_3:
+		spi_gpio_tx_word_mode3(spi,
+			1, address, command);
+	}
+}
include/spi.h +24 -0
index 320e50e..ef3f94d 100644
@@ -62,6 +62,33 @@ struct spi_slave {
 	unsigned int	cs;
 };
 
+#define COMMAND_ONLY		0xFE
+#define DATA_ONLY			0xFF
+#define ACTIVE_LOW			0
+#define ACTIVE_HIGH		1
+
+struct spi_platform_data {
+	struct s5p_gpio_bank *cs_bank;
+	struct s5p_gpio_bank *clk_bank;
+	struct s5p_gpio_bank *si_bank;
+	struct s5p_gpio_bank *so_bank;
+
+	unsigned int cs_num;
+	unsigned int clk_num;
+	unsigned int si_num;
+	unsigned int so_num;
+
+	unsigned int mode;
+	unsigned int cs_active;
+	unsigned int word_len;
+};
+
+void spi_gpio_write(struct spi_platform_data *spi,
+				unsigned int address, unsigned int command);
+
 /*-----------------------------------------------------------------------
  * Initialization, must be called once on start up.
  *
Signature · 1 line
1.6.0.4
#1 lore raw

PATCH 1/1 support spi gpio driver by control gpio bitbang

Quoted 3 lines · On Wednesday, September 01, 2010 03:28:28 Donghwa Lee wrote:

This patch adds basic support for spi mode 0~3 by control gpio bitbang. It uses several gpio pin and emulates spi chipselect signal, clock signal and sda signal as if spi controller generate spi signal.

if you're going to make a spi bus driver that uses gpios to bitbang, then you really should be using the generic GPIO framework. it's the same in u-boot as in linux.

please fix your patch summary. it should probably look like:
spi_gpio: new gpio bitbang driver

further, drivers/spi/ is for the u-boot unified spi layer. your driver does not conform to that API, so that needs fixing too.

Quoted 1 line

your e-mail is still broken. it should be "@", not " at ".

Quoted 1 line

+#include <asm/arch/gpio.h>

asm/gpio.h is the generic GPIO layer

include/spi.h struct spi_slave { · line 74

Quoted 5 lines
include/spi.h +5 -0
+struct spi_platform_data {
+	struct s5p_gpio_bank *cs_bank;
+	struct s5p_gpio_bank *clk_bank;
+	struct s5p_gpio_bank *si_bank;
+	struct s5p_gpio_bank *so_bank;

these structs are arch-specific, so this is going to break pretty much everyone else. although, once you fix the implementation to follow the unified SPI interface, this struct wont exist anymore.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20100901/faec9895/attachment.pgp