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.

---
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 +153 -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 +0 -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;
+
+ unsigned int set_rev;
+};
+
+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

Dear Donghwa Lee,

2010/9/1 Donghwa Lee <[email protected]>:

Quoted 6 lines

?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.

Please fix your email address.

Quoted 7 lines

---
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

Your patch is line wrapped and the tabs are stripped out. Please resubmit.
And then, people would review your patch.

drivers/spi/spi_gpio.c line 2

Quoted 9 lines
drivers/spi/spi_gpio.c new file +2 -0
new file mode 100644
index 0000000..cce809e
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2010 Samsung Electronics

Please insert the name of author

Quoted 9 lines
drivers/spi/spi_gpio.c new file +9 -0
+ * 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

drivers/spi/spi_gpio.c line 21

Quoted 10 lines
drivers/spi/spi_gpio.c new file +10 -0
+ * 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
+ */
+
+

please remove this space

Quoted 64 lines
drivers/spi/spi_gpio.c new file +64 -0
+#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)
+{

drivers/spi/spi_gpio.c line 95

Quoted 10 lines
drivers/spi/spi_gpio.c new file +10 -0
+ 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--) {
+

please remove this space

Quoted 63 lines
drivers/spi/spi_gpio.c new file +58 -0
+ /* 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 +0 -0
index 320e50e..ef3f94d 100644

include/spi.h struct spi_slave { · line 69

Quoted 8 lines
include/spi.h +5 -0
@@ -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
+

please remove this space

include/spi.h struct spi_slave { · line 75

Quoted 6 lines
include/spi.h +6 -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;

Is it S5P SoC specific?
It will be occur compiler errors at other SoCs.

Quoted 23 lines
include/spi.h +16 -1
+
+ 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;
+
+ unsigned int set_rev;
+};
+
+void spi_gpio_write(struct spi_platform_data *spi,
+ unsigned int address, unsigned int command);
+
 /*-----------------------------------------------------------------------
 * Initialization, must be called once on start up.
 *
--

1.6.0.4

Quoted 6 lines

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Thanks
Minkyu Kang

Signature · 2 lines
from. prom.
www.promsoft.net