linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] spi: add OpenCores tiny SPI driver
       [not found] ` <1294645208-6322-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
@ 2011-01-12  2:41   ` Thomas Chou
       [not found]     ` <1294800109-31603-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
  0 siblings, 1 reply; 13+ messages in thread
From: Thomas Chou @ 2011-01-12  2:41 UTC (permalink / raw)
  To: David Brownell, Grant Likely
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, Mike Frysinger,
	devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

This patch adds support of OpenCores tiny SPI master driver.

http://opencores.org/project,tiny_spi

Signed-off-by: Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
---
v2 add devicetree support

 drivers/spi/Kconfig             |    6 +
 drivers/spi/Makefile            |    1 +
 drivers/spi/oc_tiny_spi.c       |  428 +++++++++++++++++++++++++++++++++++++++
 include/linux/spi/oc_tiny_spi.h |   16 ++
 4 files changed, 451 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/oc_tiny_spi.c
 create mode 100644 include/linux/spi/oc_tiny_spi.h

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index c58d81b..46a2ddc 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -227,6 +227,12 @@ config SPI_FSL_ESPI
 	  From MPC8536, 85xx platform uses the controller, and all P10xx,
 	  P20xx, P30xx,P40xx, P50xx uses this controller.
 
+config SPI_OC_TINY_SPI
+	tristate "OpenCores tiny SPI"
+	select SPI_BITBANG
+	help
+	  This is the driver for OpenCores tiny SPI master controller.
+
 config SPI_OMAP_UWIRE
 	tristate "OMAP1 MicroWire"
 	depends on ARCH_OMAP1
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 10e516a..6a2ae28 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_SPI_GPIO)			+= spi_gpio.o
 obj-$(CONFIG_SPI_IMX)			+= spi_imx.o
 obj-$(CONFIG_SPI_LM70_LLP)		+= spi_lm70llp.o
 obj-$(CONFIG_SPI_PXA2XX)		+= pxa2xx_spi.o
+obj-$(CONFIG_SPI_OC_TINY_SPI)		+= oc_tiny_spi.o
 obj-$(CONFIG_SPI_OMAP_UWIRE)		+= omap_uwire.o
 obj-$(CONFIG_SPI_OMAP24XX)		+= omap2_mcspi.o
 obj-$(CONFIG_SPI_OMAP_100K)		+= omap_spi_100k.o
diff --git a/drivers/spi/oc_tiny_spi.c b/drivers/spi/oc_tiny_spi.c
new file mode 100644
index 0000000..c575dba
--- /dev/null
+++ b/drivers/spi/oc_tiny_spi.c
@@ -0,0 +1,428 @@
+/*
+ * OpenCores tiny SPI master driver
+ *
+ * http://opencores.org/project,tiny_spi
+ *
+ * Copyright (C) 2011 Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
+ *
+ * Based on spi_s3c24xx.c, which is:
+ * Copyright (c) 2006 Ben Dooks
+ * Copyright (c) 2006 Simtec Electronics
+ *	Ben Dooks <ben-Y5A6D6n0/KfQXOPxS62xeg@public.gmane.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/clk.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
+#include <linux/spi/oc_tiny_spi.h>
+#include <linux/io.h>
+#include <linux/gpio.h>
+#include <linux/of.h>
+
+#define DRV_NAME "oc_tiny_spi"
+
+#define TINY_SPI_RXDATA 0
+#define TINY_SPI_TXDATA 4
+#define TINY_SPI_STATUS 8
+#define TINY_SPI_CONTROL 12
+#define TINY_SPI_BAUD 16
+
+#define TINY_SPI_STATUS_TXE 0x1
+#define TINY_SPI_STATUS_TXR 0x2
+
+struct tiny_spi {
+	/* bitbang has to be first */
+	struct spi_bitbang bitbang;
+	struct completion done;
+
+	void __iomem *base;
+	int irq;
+	unsigned int freq;
+	unsigned int baudwidth;
+	int interrupt; /* use interrupt driven data transfer, slow */
+	unsigned int baud;
+	unsigned int speed_hz;
+	unsigned int mode;
+	unsigned int len;
+	unsigned int txc, rxc;
+	const u8 *txp;
+	u8 *rxp;
+};
+
+static inline struct tiny_spi *to_hw(struct spi_device *sdev)
+{
+	return spi_master_get_devdata(sdev->master);
+}
+
+static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
+{
+	struct tiny_spi *hw = to_hw(spi);
+
+	return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
+}
+
+static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
+{
+	gpio_set_value(spi->chip_select,
+		       (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
+}
+
+static int tiny_spi_setup_transfer(struct spi_device *spi,
+				   struct spi_transfer *t)
+{
+	struct tiny_spi *hw = to_hw(spi);
+	unsigned int baud = hw->baud;
+
+	if (t) {
+		if (t->speed_hz && t->speed_hz != hw->speed_hz)
+			baud = tiny_spi_baud(spi, t->speed_hz);
+	}
+	writel(baud, hw->base + TINY_SPI_BAUD);
+	writel(hw->mode, hw->base + TINY_SPI_CONTROL);
+	return 0;
+}
+
+static int tiny_spi_setup(struct spi_device *spi)
+{
+	struct tiny_spi *hw = to_hw(spi);
+
+	if (spi->max_speed_hz != hw->speed_hz) {
+		hw->speed_hz = spi->max_speed_hz;
+		hw->baud = tiny_spi_baud(spi, hw->speed_hz);
+	}
+	hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
+	return 0;
+}
+
+#ifndef CONFIG_TINY_SPI_IDLE_VAL
+# define CONFIG_TINY_SPI_IDLE_VAL 0x00
+#endif
+
+static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
+{
+	struct tiny_spi *hw = to_hw(spi);
+	const u8 *txp = t->tx_buf;
+	u8 *rxp = t->rx_buf;
+	unsigned int i;
+
+	if (hw->irq >= 0 && hw->interrupt) {
+		/* use intrrupt driven data transfer */
+		hw->len = t->len;
+		hw->txp = t->tx_buf;
+		hw->rxp = t->rx_buf;
+		hw->txc = 0;
+		hw->rxc = 0;
+		init_completion(&hw->done);
+
+		/* send the first byte */
+		if (t->len > 1) {
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
+		}
+
+		wait_for_completion(&hw->done);
+	} else if (txp && rxp) {
+		/* we need to tighten the transfer loop */
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx, tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (rxp) {
+		writeb(CONFIG_TINY_SPI_IDLE_VAL, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(CONFIG_TINY_SPI_IDLE_VAL,
+				       hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (txp) {
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	} else {
+		writeb(CONFIG_TINY_SPI_IDLE_VAL, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(CONFIG_TINY_SPI_IDLE_VAL,
+				       hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	}
+	return t->len;
+}
+
+static irqreturn_t tiny_spi_irq(int irq, void *dev)
+{
+	struct tiny_spi *hw = dev;
+
+	writeb(0, hw->base + TINY_SPI_STATUS);
+	if (hw->rxc + 1 == hw->len) {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+		hw->rxc++;
+		complete(&hw->done);
+	} else {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		hw->rxc++;
+		if (hw->txc < hw->len) {
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR,
+			       hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(TINY_SPI_STATUS_TXE,
+			       hw->base + TINY_SPI_STATUS);
+		}
+	}
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_OF
+static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
+				       struct tiny_spi *hw)
+{
+	const __be32 *val;
+
+	hw->bitbang.master->dev.of_node = pdev->dev.of_node;
+	val = of_get_property(pdev->dev.of_node, "baud-width", NULL);
+	if (val)
+		hw->baudwidth = be32_to_cpup(val);
+	val = of_get_property(pdev->dev.of_node, "clock-frequency", NULL);
+	if (val)
+		hw->freq = be32_to_cpup(val);
+	val = of_get_property(pdev->dev.of_node, "interrupt-driven", NULL);
+	if (val)
+		hw->interrupt = be32_to_cpup(val);
+	return 0;
+}
+#else
+static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
+				       struct tiny_spi *hw)
+{
+	return 0;
+}
+#endif
+
+static int __devinit tiny_spi_probe(struct platform_device *pdev)
+{
+	struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
+	struct tiny_spi *hw;
+	struct spi_master *master;
+	struct resource *res;
+	int err = 0;
+
+	master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
+	if (master == NULL) {
+		dev_err(&pdev->dev, "No memory for spi_master\n");
+		err = -ENOMEM;
+		goto err_no_mem;
+	}
+
+	/* setup the master state. */
+	master->bus_num = pdev->id;
+	master->num_chipselect = 255;
+	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
+	master->setup = tiny_spi_setup;
+
+	hw = spi_master_get_devdata(master);
+	platform_set_drvdata(pdev, hw);
+
+	/* setup the state for the bitbang driver */
+	hw->bitbang.master = spi_master_get(master);
+	if (hw->bitbang.master == NULL) {
+		dev_err(&pdev->dev, "Cannot get device\n");
+		err = -ENODEV;
+		goto err_no_dev;
+	}
+	hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
+	hw->bitbang.chipselect = tiny_spi_chipselect;
+	hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
+
+	/* find and map our resources */
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (res == NULL) {
+		dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
+		err = -ENOENT;
+		goto err_no_iores;
+	}
+	hw->base = ioremap(res->start, (res->end - res->start) + 1);
+	if (hw->base == 0) {
+		dev_err(&pdev->dev, "Cannot map IO\n");
+		err = -ENXIO;
+		goto err_no_iomap;
+	}
+	/* irq is optional */
+	hw->irq = platform_get_irq(pdev, 0);
+	if (hw->irq >= 0) {
+		init_completion(&hw->done);
+		err = request_irq(hw->irq, tiny_spi_irq, 0, pdev->name, hw);
+		if (err) {
+			dev_err(&pdev->dev, "Cannot claim IRQ\n");
+			goto err_no_irq;
+		}
+	}
+	/* find platform data */
+	if (platp) {
+		hw->freq = platp->freq;
+		hw->baudwidth = platp->baudwidth;
+		hw->interrupt = platp->interrupt;
+	} else {
+		err = tiny_spi_of_probe(pdev, hw);
+		if (err)
+			goto err_no_of;
+	}
+
+	/* register our spi controller */
+	err = spi_bitbang_start(&hw->bitbang);
+	if (err) {
+		dev_err(&pdev->dev, "Failed to register SPI master\n");
+		goto err_register;
+	}
+	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
+
+	return 0;
+
+err_register:
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+err_no_irq:
+	iounmap((void *)hw->base);
+err_no_iomap:
+err_no_iores:
+	spi_master_put(master);
+err_no_mem:
+err_no_of:
+err_no_dev:
+	return err;
+}
+
+static int __devexit tiny_spi_remove(struct platform_device *dev)
+{
+	struct tiny_spi *hw = platform_get_drvdata(dev);
+	struct spi_master *master = hw->bitbang.master;
+
+	spi_bitbang_stop(&hw->bitbang);
+
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+	iounmap((void *)hw->base);
+
+	platform_set_drvdata(dev, NULL);
+	spi_master_put(master);
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static struct of_device_id oc_tiny_spi_match[] = {
+	{
+		.compatible = "opencores,oc_tiny_spi",
+	},
+	{},
+}
+MODULE_DEVICE_TABLE(of, oc_tiny_spi_match);
+#endif
+
+static struct platform_driver tiny_spidrv = {
+	.remove = __devexit_p(tiny_spi_remove),
+	.driver = {
+		.name = DRV_NAME,
+		.owner = THIS_MODULE,
+		.pm = NULL,
+#ifdef CONFIG_OF
+		.of_match_table = oc_tiny_spi_match,
+#endif
+	},
+};
+
+static int __init tiny_spi_init(void)
+{
+	return platform_driver_probe(&tiny_spidrv, tiny_spi_probe);
+}
+
+static void __exit tiny_spi_exit(void)
+{
+	platform_driver_unregister(&tiny_spidrv);
+}
+
+module_init(tiny_spi_init);
+module_exit(tiny_spi_exit);
+
+MODULE_DESCRIPTION("OpenCores tiny SPI driver");
+MODULE_AUTHOR("Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
diff --git a/include/linux/spi/oc_tiny_spi.h b/include/linux/spi/oc_tiny_spi.h
new file mode 100644
index 0000000..9dedd59
--- /dev/null
+++ b/include/linux/spi/oc_tiny_spi.h
@@ -0,0 +1,16 @@
+#ifndef _LINUX_SPI_OC_TINY_SPI_H
+#define _LINUX_SPI_OC_TINY_SPI_H
+
+/**
+ * struct tiny_spi_platform_data - platform data of the OpenCores tiny SPI
+ * @freq:	input clock freq to the core.
+ * @baudwidth:	baud rate divider width of the core.
+ * @interrupt:	use intrrupt driven data transfer.
+ */
+struct tiny_spi_platform_data {
+	uint freq;
+	uint baudwidth;
+	int interrupt;
+};
+
+#endif /* _LINUX_SPI_OC_TINY_SPI_H */
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2] spi: add OpenCores tiny SPI driver
       [not found]     ` <1294800109-31603-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
@ 2011-01-17  7:32       ` Thomas Chou
       [not found]         ` <1295249542-26743-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
  0 siblings, 1 reply; 13+ messages in thread
From: Thomas Chou @ 2011-01-17  7:32 UTC (permalink / raw)
  To: David Brownell, Grant Likely
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, Mike Frysinger,
	devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

This patch adds support of OpenCores tiny SPI driver.

http://opencores.org/project,tiny_spi

Signed-off-by: Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
---
v2 minor cleanup, same as Grant suggest for spi_altera.

 drivers/spi/Kconfig             |    6 +
 drivers/spi/Makefile            |    1 +
 drivers/spi/oc_tiny_spi.c       |  422 +++++++++++++++++++++++++++++++++++++++
 include/linux/spi/oc_tiny_spi.h |   14 ++
 4 files changed, 443 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/oc_tiny_spi.c
 create mode 100644 include/linux/spi/oc_tiny_spi.h

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index c58d81b..46a2ddc 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -227,6 +227,12 @@ config SPI_FSL_ESPI
 	  From MPC8536, 85xx platform uses the controller, and all P10xx,
 	  P20xx, P30xx,P40xx, P50xx uses this controller.
 
+config SPI_OC_TINY_SPI
+	tristate "OpenCores tiny SPI"
+	select SPI_BITBANG
+	help
+	  This is the driver for OpenCores tiny SPI master controller.
+
 config SPI_OMAP_UWIRE
 	tristate "OMAP1 MicroWire"
 	depends on ARCH_OMAP1
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 10e516a..6a2ae28 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_SPI_GPIO)			+= spi_gpio.o
 obj-$(CONFIG_SPI_IMX)			+= spi_imx.o
 obj-$(CONFIG_SPI_LM70_LLP)		+= spi_lm70llp.o
 obj-$(CONFIG_SPI_PXA2XX)		+= pxa2xx_spi.o
+obj-$(CONFIG_SPI_OC_TINY_SPI)		+= oc_tiny_spi.o
 obj-$(CONFIG_SPI_OMAP_UWIRE)		+= omap_uwire.o
 obj-$(CONFIG_SPI_OMAP24XX)		+= omap2_mcspi.o
 obj-$(CONFIG_SPI_OMAP_100K)		+= omap_spi_100k.o
diff --git a/drivers/spi/oc_tiny_spi.c b/drivers/spi/oc_tiny_spi.c
new file mode 100644
index 0000000..ead7a09
--- /dev/null
+++ b/drivers/spi/oc_tiny_spi.c
@@ -0,0 +1,422 @@
+/*
+ * OpenCores tiny SPI master driver
+ *
+ * http://opencores.org/project,tiny_spi
+ *
+ * Copyright (C) 2011 Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
+ *
+ * Based on spi_s3c24xx.c, which is:
+ * Copyright (c) 2006 Ben Dooks
+ * Copyright (c) 2006 Simtec Electronics
+ *	Ben Dooks <ben-Y5A6D6n0/KfQXOPxS62xeg@public.gmane.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/clk.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
+#include <linux/spi/oc_tiny_spi.h>
+#include <linux/io.h>
+#include <linux/gpio.h>
+#include <linux/of.h>
+
+#define DRV_NAME "oc_tiny_spi"
+
+#define TINY_SPI_RXDATA 0
+#define TINY_SPI_TXDATA 4
+#define TINY_SPI_STATUS 8
+#define TINY_SPI_CONTROL 12
+#define TINY_SPI_BAUD 16
+
+#define TINY_SPI_STATUS_TXE 0x1
+#define TINY_SPI_STATUS_TXR 0x2
+
+struct tiny_spi {
+	/* bitbang has to be first */
+	struct spi_bitbang bitbang;
+	struct completion done;
+
+	void __iomem *base;
+	int irq;
+	unsigned int freq;
+	unsigned int baudwidth;
+	unsigned int baud;
+	unsigned int speed_hz;
+	unsigned int mode;
+	unsigned int len;
+	unsigned int txc, rxc;
+	const u8 *txp;
+	u8 *rxp;
+};
+
+static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
+{
+	return spi_master_get_devdata(sdev->master);
+}
+
+static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+
+	return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
+}
+
+static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
+{
+	gpio_set_value(spi->chip_select,
+		       (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
+}
+
+static int tiny_spi_setup_transfer(struct spi_device *spi,
+				   struct spi_transfer *t)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+	unsigned int baud = hw->baud;
+
+	if (t) {
+		if (t->speed_hz && t->speed_hz != hw->speed_hz)
+			baud = tiny_spi_baud(spi, t->speed_hz);
+	}
+	writel(baud, hw->base + TINY_SPI_BAUD);
+	writel(hw->mode, hw->base + TINY_SPI_CONTROL);
+	return 0;
+}
+
+static int tiny_spi_setup(struct spi_device *spi)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+
+	if (spi->max_speed_hz != hw->speed_hz) {
+		hw->speed_hz = spi->max_speed_hz;
+		hw->baud = tiny_spi_baud(spi, hw->speed_hz);
+	}
+	hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
+	return 0;
+}
+
+#ifndef CONFIG_TINY_SPI_IDLE_VAL
+# define CONFIG_TINY_SPI_IDLE_VAL 0x00
+#endif
+
+static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+	const u8 *txp = t->tx_buf;
+	u8 *rxp = t->rx_buf;
+	unsigned int i;
+
+	if (hw->irq >= 0) {
+		/* use intrrupt driven data transfer */
+		hw->len = t->len;
+		hw->txp = t->tx_buf;
+		hw->rxp = t->rx_buf;
+		hw->txc = 0;
+		hw->rxc = 0;
+		init_completion(&hw->done);
+
+		/* send the first byte */
+		if (t->len > 1) {
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
+		}
+
+		wait_for_completion(&hw->done);
+	} else if (txp && rxp) {
+		/* we need to tighten the transfer loop */
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx, tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (rxp) {
+		writeb(CONFIG_TINY_SPI_IDLE_VAL, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(CONFIG_TINY_SPI_IDLE_VAL,
+				       hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (txp) {
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	} else {
+		writeb(CONFIG_TINY_SPI_IDLE_VAL, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(CONFIG_TINY_SPI_IDLE_VAL,
+				       hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	}
+	return t->len;
+}
+
+static irqreturn_t tiny_spi_irq(int irq, void *dev)
+{
+	struct tiny_spi *hw = dev;
+
+	writeb(0, hw->base + TINY_SPI_STATUS);
+	if (hw->rxc + 1 == hw->len) {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+		hw->rxc++;
+		complete(&hw->done);
+	} else {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		hw->rxc++;
+		if (hw->txc < hw->len) {
+			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR,
+			       hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(TINY_SPI_STATUS_TXE,
+			       hw->base + TINY_SPI_STATUS);
+		}
+	}
+	return IRQ_HANDLED;
+}
+
+#ifdef CONFIG_OF
+static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
+				       struct tiny_spi *hw)
+{
+	const __be32 *val;
+
+	hw->bitbang.master->dev.of_node = pdev->dev.of_node;
+	val = of_get_property(pdev->dev.of_node, "baud-width", NULL);
+	if (val)
+		hw->baudwidth = be32_to_cpup(val);
+	val = of_get_property(pdev->dev.of_node, "clock-frequency", NULL);
+	if (val)
+		hw->freq = be32_to_cpup(val);
+	return 0;
+}
+#else
+static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
+				       struct tiny_spi *hw)
+{
+	return 0;
+}
+#endif
+
+static int __devinit tiny_spi_probe(struct platform_device *pdev)
+{
+	struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
+	struct tiny_spi *hw;
+	struct spi_master *master;
+	struct resource *res;
+	int err = 0;
+
+	master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
+	if (master == NULL) {
+		dev_err(&pdev->dev, "No memory for spi_master\n");
+		err = -ENOMEM;
+		goto err_no_mem;
+	}
+
+	/* setup the master state. */
+	master->bus_num = pdev->id;
+	master->num_chipselect = 255;
+	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
+	master->setup = tiny_spi_setup;
+
+	hw = spi_master_get_devdata(master);
+	platform_set_drvdata(pdev, hw);
+
+	/* setup the state for the bitbang driver */
+	hw->bitbang.master = spi_master_get(master);
+	if (hw->bitbang.master == NULL) {
+		dev_err(&pdev->dev, "Cannot get device\n");
+		err = -ENODEV;
+		goto err_no_dev;
+	}
+	hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
+	hw->bitbang.chipselect = tiny_spi_chipselect;
+	hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
+
+	/* find and map our resources */
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (res == NULL) {
+		dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
+		err = -ENOENT;
+		goto err_no_iores;
+	}
+	hw->base = ioremap(res->start, (res->end - res->start) + 1);
+	if (hw->base == 0) {
+		dev_err(&pdev->dev, "Cannot map IO\n");
+		err = -ENXIO;
+		goto err_no_iomap;
+	}
+	/* irq is optional */
+	hw->irq = platform_get_irq(pdev, 0);
+	if (hw->irq >= 0) {
+		init_completion(&hw->done);
+		err = request_irq(hw->irq, tiny_spi_irq, 0, pdev->name, hw);
+		if (err) {
+			dev_err(&pdev->dev, "Cannot claim IRQ\n");
+			goto err_no_irq;
+		}
+	}
+	/* find platform data */
+	if (platp) {
+		hw->freq = platp->freq;
+		hw->baudwidth = platp->baudwidth;
+	} else {
+		err = tiny_spi_of_probe(pdev, hw);
+		if (err)
+			goto err_no_of;
+	}
+
+	/* register our spi controller */
+	err = spi_bitbang_start(&hw->bitbang);
+	if (err) {
+		dev_err(&pdev->dev, "Failed to register SPI master\n");
+		goto err_register;
+	}
+	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
+
+	return 0;
+
+err_register:
+err_no_of:
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+err_no_irq:
+	iounmap(hw->base);
+err_no_iomap:
+err_no_iores:
+	spi_master_put(master);
+err_no_mem:
+err_no_dev:
+	return err;
+}
+
+static int __devexit tiny_spi_remove(struct platform_device *dev)
+{
+	struct tiny_spi *hw = platform_get_drvdata(dev);
+	struct spi_master *master = hw->bitbang.master;
+
+	spi_bitbang_stop(&hw->bitbang);
+
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+	iounmap(hw->base);
+
+	platform_set_drvdata(dev, NULL);
+	spi_master_put(master);
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static struct of_device_id oc_tiny_spi_match[] = {
+	{
+		.compatible = "opencores,oc_tiny_spi",
+	},
+	{},
+}
+MODULE_DEVICE_TABLE(of, oc_tiny_spi_match);
+#endif
+
+static struct platform_driver tiny_spidrv = {
+	.remove = __devexit_p(tiny_spi_remove),
+	.driver = {
+		.name = DRV_NAME,
+		.owner = THIS_MODULE,
+		.pm = NULL,
+#ifdef CONFIG_OF
+		.of_match_table = oc_tiny_spi_match,
+#endif
+	},
+};
+
+static int __init tiny_spi_init(void)
+{
+	return platform_driver_probe(&tiny_spidrv, tiny_spi_probe);
+}
+module_init(tiny_spi_init);
+
+static void __exit tiny_spi_exit(void)
+{
+	platform_driver_unregister(&tiny_spidrv);
+}
+module_exit(tiny_spi_exit);
+
+MODULE_DESCRIPTION("OpenCores tiny SPI driver");
+MODULE_AUTHOR("Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
diff --git a/include/linux/spi/oc_tiny_spi.h b/include/linux/spi/oc_tiny_spi.h
new file mode 100644
index 0000000..83de49d
--- /dev/null
+++ b/include/linux/spi/oc_tiny_spi.h
@@ -0,0 +1,14 @@
+#ifndef _LINUX_SPI_OC_TINY_SPI_H
+#define _LINUX_SPI_OC_TINY_SPI_H
+
+/**
+ * struct tiny_spi_platform_data - platform data of the OpenCores tiny SPI
+ * @freq:	input clock freq to the core.
+ * @baudwidth:	baud rate divider width of the core.
+ */
+struct tiny_spi_platform_data {
+	uint freq;
+	uint baudwidth;
+};
+
+#endif /* _LINUX_SPI_OC_TINY_SPI_H */
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] spi: add OpenCores tiny SPI driver
       [not found]         ` <1295249542-26743-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
@ 2011-01-20 16:54           ` Grant Likely
  2011-01-20 21:36             ` Thomas Chou
  0 siblings, 1 reply; 13+ messages in thread
From: Grant Likely @ 2011-01-20 16:54 UTC (permalink / raw)
  To: Thomas Chou
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, David Brownell,
	Mike Frysinger, devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

On Mon, Jan 17, 2011 at 03:32:22PM +0800, Thomas Chou wrote:
> This patch adds support of OpenCores tiny SPI driver.
> 
> http://opencores.org/project,tiny_spi
> 
> Signed-off-by: Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
> ---
> v2 minor cleanup, same as Grant suggest for spi_altera.
> 
>  drivers/spi/Kconfig             |    6 +
>  drivers/spi/Makefile            |    1 +
>  drivers/spi/oc_tiny_spi.c       |  422 +++++++++++++++++++++++++++++++++++++++

Rename files to spi_oc_tiny.c

>  include/linux/spi/oc_tiny_spi.h |   14 ++
>  4 files changed, 443 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/spi/oc_tiny_spi.c
>  create mode 100644 include/linux/spi/oc_tiny_spi.h
> 
> diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
> index c58d81b..46a2ddc 100644
> --- a/drivers/spi/Kconfig
> +++ b/drivers/spi/Kconfig
> @@ -227,6 +227,12 @@ config SPI_FSL_ESPI
>  	  From MPC8536, 85xx platform uses the controller, and all P10xx,
>  	  P20xx, P30xx,P40xx, P50xx uses this controller.
>  
> +config SPI_OC_TINY_SPI
> +	tristate "OpenCores tiny SPI"
> +	select SPI_BITBANG
> +	help
> +	  This is the driver for OpenCores tiny SPI master controller.
> +
>  config SPI_OMAP_UWIRE
>  	tristate "OMAP1 MicroWire"
>  	depends on ARCH_OMAP1
> diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
> index 10e516a..6a2ae28 100644
> --- a/drivers/spi/Makefile
> +++ b/drivers/spi/Makefile
> @@ -25,6 +25,7 @@ obj-$(CONFIG_SPI_GPIO)			+= spi_gpio.o
>  obj-$(CONFIG_SPI_IMX)			+= spi_imx.o
>  obj-$(CONFIG_SPI_LM70_LLP)		+= spi_lm70llp.o
>  obj-$(CONFIG_SPI_PXA2XX)		+= pxa2xx_spi.o
> +obj-$(CONFIG_SPI_OC_TINY_SPI)		+= oc_tiny_spi.o
>  obj-$(CONFIG_SPI_OMAP_UWIRE)		+= omap_uwire.o
>  obj-$(CONFIG_SPI_OMAP24XX)		+= omap2_mcspi.o
>  obj-$(CONFIG_SPI_OMAP_100K)		+= omap_spi_100k.o
> diff --git a/drivers/spi/oc_tiny_spi.c b/drivers/spi/oc_tiny_spi.c
> new file mode 100644
> index 0000000..ead7a09
> --- /dev/null
> +++ b/drivers/spi/oc_tiny_spi.c
> @@ -0,0 +1,422 @@
> +/*
> + * OpenCores tiny SPI master driver
> + *
> + * http://opencores.org/project,tiny_spi
> + *
> + * Copyright (C) 2011 Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
> + *
> + * Based on spi_s3c24xx.c, which is:
> + * Copyright (c) 2006 Ben Dooks
> + * Copyright (c) 2006 Simtec Electronics
> + *	Ben Dooks <ben-Y5A6D6n0/KfQXOPxS62xeg@public.gmane.org>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + */
> +
> +#include <linux/init.h>
> +#include <linux/spinlock.h>
> +#include <linux/interrupt.h>
> +#include <linux/delay.h>
> +#include <linux/errno.h>
> +#include <linux/err.h>
> +#include <linux/clk.h>
> +#include <linux/platform_device.h>
> +#include <linux/spi/spi.h>
> +#include <linux/spi/spi_bitbang.h>
> +#include <linux/spi/oc_tiny_spi.h>
> +#include <linux/io.h>
> +#include <linux/gpio.h>
> +#include <linux/of.h>
> +
> +#define DRV_NAME "oc_tiny_spi"
> +
> +#define TINY_SPI_RXDATA 0
> +#define TINY_SPI_TXDATA 4
> +#define TINY_SPI_STATUS 8
> +#define TINY_SPI_CONTROL 12
> +#define TINY_SPI_BAUD 16
> +
> +#define TINY_SPI_STATUS_TXE 0x1
> +#define TINY_SPI_STATUS_TXR 0x2
> +
> +struct tiny_spi {
> +	/* bitbang has to be first */
> +	struct spi_bitbang bitbang;
> +	struct completion done;
> +
> +	void __iomem *base;
> +	int irq;
> +	unsigned int freq;
> +	unsigned int baudwidth;
> +	unsigned int baud;
> +	unsigned int speed_hz;
> +	unsigned int mode;
> +	unsigned int len;
> +	unsigned int txc, rxc;
> +	const u8 *txp;
> +	u8 *rxp;
> +};
> +
> +static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
> +{
> +	return spi_master_get_devdata(sdev->master);
> +}
> +
> +static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
> +{
> +	struct tiny_spi *hw = tiny_spi_to_hw(spi);
> +
> +	return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
> +}
> +
> +static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
> +{
> +	gpio_set_value(spi->chip_select,
> +		       (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
> +}
> +
> +static int tiny_spi_setup_transfer(struct spi_device *spi,
> +				   struct spi_transfer *t)
> +{
> +	struct tiny_spi *hw = tiny_spi_to_hw(spi);
> +	unsigned int baud = hw->baud;
> +
> +	if (t) {
> +		if (t->speed_hz && t->speed_hz != hw->speed_hz)
> +			baud = tiny_spi_baud(spi, t->speed_hz);
> +	}
> +	writel(baud, hw->base + TINY_SPI_BAUD);
> +	writel(hw->mode, hw->base + TINY_SPI_CONTROL);
> +	return 0;
> +}
> +
> +static int tiny_spi_setup(struct spi_device *spi)
> +{
> +	struct tiny_spi *hw = tiny_spi_to_hw(spi);
> +
> +	if (spi->max_speed_hz != hw->speed_hz) {
> +		hw->speed_hz = spi->max_speed_hz;
> +		hw->baud = tiny_spi_baud(spi, hw->speed_hz);
> +	}
> +	hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
> +	return 0;
> +}
> +
> +#ifndef CONFIG_TINY_SPI_IDLE_VAL
> +# define CONFIG_TINY_SPI_IDLE_VAL 0x00
> +#endif

Do you really want this as a Kconfig symbol?  Looks like something
that should be configured in pdata or the device tree.

> +
> +static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
> +{
> +	struct tiny_spi *hw = tiny_spi_to_hw(spi);
> +	const u8 *txp = t->tx_buf;
> +	u8 *rxp = t->rx_buf;
> +	unsigned int i;
> +
> +	if (hw->irq >= 0) {
> +		/* use intrrupt driven data transfer */
> +		hw->len = t->len;
> +		hw->txp = t->tx_buf;
> +		hw->rxp = t->rx_buf;
> +		hw->txc = 0;
> +		hw->rxc = 0;
> +		init_completion(&hw->done);
> +
> +		/* send the first byte */
> +		if (t->len > 1) {
> +			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
> +		} else {
> +			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
> +		}
> +
> +		wait_for_completion(&hw->done);
> +	} else if (txp && rxp) {
> +		/* we need to tighten the transfer loop */
> +		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +		if (t->len > 1) {
> +			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i < t->len; i++) {
> +				u8 rx, tx = *txp++;
> +				while (!(readb(hw->base + TINY_SPI_STATUS) &
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				rx = readb(hw->base + TINY_SPI_TXDATA);
> +				writeb(tx, hw->base + TINY_SPI_TXDATA);
> +				*rxp++ = rx;
> +			}
> +			while (!(readb(hw->base + TINY_SPI_STATUS) &
> +				 TINY_SPI_STATUS_TXR))
> +				cpu_relax();
> +			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS) &
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
> +	} else if (rxp) {
> +		writeb(CONFIG_TINY_SPI_IDLE_VAL, hw->base + TINY_SPI_TXDATA);
> +		if (t->len > 1) {
> +			writeb(CONFIG_TINY_SPI_IDLE_VAL,
> +			       hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i < t->len; i++) {
> +				u8 rx;
> +				while (!(readb(hw->base + TINY_SPI_STATUS) &
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				rx = readb(hw->base + TINY_SPI_TXDATA);
> +				writeb(CONFIG_TINY_SPI_IDLE_VAL,
> +				       hw->base + TINY_SPI_TXDATA);
> +				*rxp++ = rx;
> +			}
> +			while (!(readb(hw->base + TINY_SPI_STATUS) &
> +				 TINY_SPI_STATUS_TXR))
> +				cpu_relax();
> +			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS) &
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
> +	} else if (txp) {
> +		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +		if (t->len > 1) {
> +			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i < t->len; i++) {
> +				u8 tx = *txp++;
> +				while (!(readb(hw->base + TINY_SPI_STATUS) &
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				writeb(tx, hw->base + TINY_SPI_TXDATA);
> +			}
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS) &
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +	} else {
> +		writeb(CONFIG_TINY_SPI_IDLE_VAL, hw->base + TINY_SPI_TXDATA);
> +		if (t->len > 1) {
> +			writeb(CONFIG_TINY_SPI_IDLE_VAL,
> +			       hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i < t->len; i++) {
> +				while (!(readb(hw->base + TINY_SPI_STATUS) &
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				writeb(CONFIG_TINY_SPI_IDLE_VAL,
> +				       hw->base + TINY_SPI_TXDATA);
> +			}
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS) &
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +	}
> +	return t->len;
> +}

I see a bunch of while loops in this function with no exit condition
if something goes badly with the hardware.  Also, this driver is going
to chew up *a lot* of cpu cycles when not using irqs.  Consider
putting the polled work into a tasklet instead of blindly spinning.
It will actually make the driver simpler because the irq and non-irq
cases become very similar.

> +
> +static irqreturn_t tiny_spi_irq(int irq, void *dev)
> +{
> +	struct tiny_spi *hw = dev;
> +
> +	writeb(0, hw->base + TINY_SPI_STATUS);
> +	if (hw->rxc + 1 == hw->len) {
> +		if (hw->rxp)
> +			*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
> +		hw->rxc++;
> +		complete(&hw->done);
> +	} else {
> +		if (hw->rxp)
> +			*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
> +		hw->rxc++;
> +		if (hw->txc < hw->len) {
> +			writeb(hw->txp ? *hw->txp++ : CONFIG_TINY_SPI_IDLE_VAL,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(TINY_SPI_STATUS_TXR,
> +			       hw->base + TINY_SPI_STATUS);
> +		} else {
> +			writeb(TINY_SPI_STATUS_TXE,
> +			       hw->base + TINY_SPI_STATUS);
> +		}
> +	}
> +	return IRQ_HANDLED;
> +}
> +
> +#ifdef CONFIG_OF
> +static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
> +				       struct tiny_spi *hw)
> +{
> +	const __be32 *val;
> +
> +	hw->bitbang.master->dev.of_node = pdev->dev.of_node;
> +	val = of_get_property(pdev->dev.of_node, "baud-width", NULL);

'baud-width'?

These properties need to be documented.  See below.

> +	if (val)
> +		hw->baudwidth = be32_to_cpup(val);
> +	val = of_get_property(pdev->dev.of_node, "clock-frequency", NULL);
> +	if (val)
> +		hw->freq = be32_to_cpup(val);
> +	return 0;
> +}
> +#else
> +static int __devinit tiny_spi_of_probe(struct platform_device *pdev,
> +				       struct tiny_spi *hw)
> +{
> +	return 0;
> +}
> +#endif
> +
> +static int __devinit tiny_spi_probe(struct platform_device *pdev)
> +{
> +	struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
> +	struct tiny_spi *hw;
> +	struct spi_master *master;
> +	struct resource *res;
> +	int err = 0;
> +
> +	master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
> +	if (master == NULL) {
> +		dev_err(&pdev->dev, "No memory for spi_master\n");
> +		err = -ENOMEM;
> +		goto err_no_mem;
> +	}
> +
> +	/* setup the master state. */
> +	master->bus_num = pdev->id;
> +	master->num_chipselect = 255;
> +	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
> +	master->setup = tiny_spi_setup;
> +
> +	hw = spi_master_get_devdata(master);
> +	platform_set_drvdata(pdev, hw);
> +
> +	/* setup the state for the bitbang driver */
> +	hw->bitbang.master = spi_master_get(master);
> +	if (hw->bitbang.master == NULL) {
> +		dev_err(&pdev->dev, "Cannot get device\n");
> +		err = -ENODEV;
> +		goto err_no_dev;
> +	}
> +	hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
> +	hw->bitbang.chipselect = tiny_spi_chipselect;
> +	hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
> +
> +	/* find and map our resources */
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	if (res == NULL) {
> +		dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
> +		err = -ENOENT;
> +		goto err_no_iores;
> +	}
> +	hw->base = ioremap(res->start, (res->end - res->start) + 1);
> +	if (hw->base == 0) {
> +		dev_err(&pdev->dev, "Cannot map IO\n");
> +		err = -ENXIO;
> +		goto err_no_iomap;
> +	}
> +	/* irq is optional */
> +	hw->irq = platform_get_irq(pdev, 0);
> +	if (hw->irq >= 0) {
> +		init_completion(&hw->done);
> +		err = request_irq(hw->irq, tiny_spi_irq, 0, pdev->name, hw);
> +		if (err) {
> +			dev_err(&pdev->dev, "Cannot claim IRQ\n");
> +			goto err_no_irq;
> +		}
> +	}
> +	/* find platform data */
> +	if (platp) {
> +		hw->freq = platp->freq;
> +		hw->baudwidth = platp->baudwidth;
> +	} else {
> +		err = tiny_spi_of_probe(pdev, hw);
> +		if (err)
> +			goto err_no_of;
> +	}
> +
> +	/* register our spi controller */
> +	err = spi_bitbang_start(&hw->bitbang);
> +	if (err) {
> +		dev_err(&pdev->dev, "Failed to register SPI master\n");
> +		goto err_register;
> +	}
> +	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
> +
> +	return 0;
> +
> +err_register:
> +err_no_of:
> +	if (hw->irq >= 0)
> +		free_irq(hw->irq, hw);
> +err_no_irq:
> +	iounmap(hw->base);
> +err_no_iomap:
> +err_no_iores:
> +	spi_master_put(master);
> +err_no_mem:
> +err_no_dev:
> +	return err;
> +}
> +
> +static int __devexit tiny_spi_remove(struct platform_device *dev)
> +{
> +	struct tiny_spi *hw = platform_get_drvdata(dev);
> +	struct spi_master *master = hw->bitbang.master;
> +
> +	spi_bitbang_stop(&hw->bitbang);
> +
> +	if (hw->irq >= 0)
> +		free_irq(hw->irq, hw);
> +	iounmap(hw->base);
> +
> +	platform_set_drvdata(dev, NULL);
> +	spi_master_put(master);
> +	return 0;
> +}
> +
> +#ifdef CONFIG_OF
> +static struct of_device_id oc_tiny_spi_match[] = {
> +	{
> +		.compatible = "opencores,oc_tiny_spi",

If this is a soft core, then there should be a version number of some
sort on the compatible value.  Also, please use dash '-' instead of
underscore '_' in compatible values.  Also, for all of these new
bindings, they need to be documented.  Please add documentation to
Documentation/powerpc/dts-bindings (yes, I know, this is not
for powerpc, but that is the established directory.  I'll move it to a
better location soon).

Finally, one nitpick.  For conciseness, most of_device_id tables are
written in the form:

static struct of_device_id oc_tiny_spi_match[] = {
	{ .compatible = "opencores,oc_tiny_spi", }, },
	{},
}

> +MODULE_DEVICE_TABLE(of, oc_tiny_spi_match);
> +#endif
> +
> +static struct platform_driver tiny_spidrv = {
> +	.remove = __devexit_p(tiny_spi_remove),
> +	.driver = {
> +		.name = DRV_NAME,
> +		.owner = THIS_MODULE,
> +		.pm = NULL,
> +#ifdef CONFIG_OF
> +		.of_match_table = oc_tiny_spi_match,
> +#endif
> +	},
> +};
> +
> +static int __init tiny_spi_init(void)
> +{
> +	return platform_driver_probe(&tiny_spidrv, tiny_spi_probe);
> +}
> +module_init(tiny_spi_init);

Please use platform_driver_register, and put your probe function into
the platform_driver structure.

> +
> +static void __exit tiny_spi_exit(void)
> +{
> +	platform_driver_unregister(&tiny_spidrv);
> +}
> +module_exit(tiny_spi_exit);
> +
> +MODULE_DESCRIPTION("OpenCores tiny SPI driver");
> +MODULE_AUTHOR("Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>");
> +MODULE_LICENSE("GPL");
> +MODULE_ALIAS("platform:" DRV_NAME);
> diff --git a/include/linux/spi/oc_tiny_spi.h b/include/linux/spi/oc_tiny_spi.h
> new file mode 100644
> index 0000000..83de49d
> --- /dev/null
> +++ b/include/linux/spi/oc_tiny_spi.h
> @@ -0,0 +1,14 @@
> +#ifndef _LINUX_SPI_OC_TINY_SPI_H
> +#define _LINUX_SPI_OC_TINY_SPI_H
> +
> +/**
> + * struct tiny_spi_platform_data - platform data of the OpenCores tiny SPI
> + * @freq:	input clock freq to the core.
> + * @baudwidth:	baud rate divider width of the core.
> + */
> +struct tiny_spi_platform_data {
> +	uint freq;
> +	uint baudwidth;
> +};
> +
> +#endif /* _LINUX_SPI_OC_TINY_SPI_H */
> -- 
> 1.7.3.4
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] spi: add OpenCores tiny SPI driver
  2011-01-20 16:54           ` Grant Likely
@ 2011-01-20 21:36             ` Thomas Chou
       [not found]               ` <4D38AAEB.7020308-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
  0 siblings, 1 reply; 13+ messages in thread
From: Thomas Chou @ 2011-01-20 21:36 UTC (permalink / raw)
  To: Grant Likely
  Cc: David Brownell, linux-kernel, nios2-dev, devicetree-discuss,
	spi-devel-general, Mike Frysinger

Hi Grant,

Thanks a lot for the review.

On 01/21/2011 12:54 AM, Grant Likely wrote:
>>   drivers/spi/oc_tiny_spi.c       |  422 +++++++++++++++++++++++++++++++++++++++
>
> Rename files to spi_oc_tiny.c

OK.

>> +#ifndef CONFIG_TINY_SPI_IDLE_VAL
>> +# define CONFIG_TINY_SPI_IDLE_VAL 0x00
>> +#endif
>
> Do you really want this as a Kconfig symbol?  Looks like something
> that should be configured in pdata or the device tree.

I will drop this def, as linux use 00 as default.

>> +		if (t->len>  1) {
>> +			writeb(CONFIG_TINY_SPI_IDLE_VAL,
>> +			       hw->base + TINY_SPI_TXDATA);
>> +			for (i = 2; i<  t->len; i++) {
>> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
>> +					 TINY_SPI_STATUS_TXR))
>> +					cpu_relax();
>> +				writeb(CONFIG_TINY_SPI_IDLE_VAL,
>> +				       hw->base + TINY_SPI_TXDATA);
>> +			}
>> +		}
>> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
>> +			 TINY_SPI_STATUS_TXE))
>> +			cpu_relax();
>
> I see a bunch of while loops in this function with no exit condition
> if something goes badly with the hardware.  Also, this driver is going
> to chew up *a lot* of cpu cycles when not using irqs.  Consider
> putting the polled work into a tasklet instead of blindly spinning.
> It will actually make the driver simpler because the irq and non-irq
> cases become very similar.

But bitbang driver already runs as a workqueue, do we still need to 
create a tasklet?

	bitbang->workqueue = create_singlethread_workqueue(
			dev_name(bitbang->master->dev.parent));

>> +	hw->bitbang.master->dev.of_node = pdev->dev.of_node;
>> +	val = of_get_property(pdev->dev.of_node, "baud-width", NULL);
>
> 'baud-width'?
>
> These properties need to be documented.  See below.

This is the width of baud rate divider. I will document it. Do you have 
suggestion on the naming?

>> +#ifdef CONFIG_OF
>> +static struct of_device_id oc_tiny_spi_match[] = {
>> +	{
>> +		.compatible = "opencores,oc_tiny_spi",
>
> If this is a soft core, then there should be a version number of some
> sort on the compatible value.  Also, please use dash '-' instead of
> underscore '_' in compatible values.  Also, for all of these new
> bindings, they need to be documented.  Please add documentation to
> Documentation/powerpc/dts-bindings (yes, I know, this is not
> for powerpc, but that is the established directory.  I'll move it to a
> better location soon).
>
> Finally, one nitpick.  For conciseness, most of_device_id tables are
> written in the form:
>
> static struct of_device_id oc_tiny_spi_match[] = {
> 	{ .compatible = "opencores,oc_tiny_spi", }, },
> 	{},
> }
>

Nice. I am working with Walter on the dts generator for our cores. We 
will add them to the listing.

>> +static int __init tiny_spi_init(void)
>> +{
>> +	return platform_driver_probe(&tiny_spidrv, tiny_spi_probe);
>> +}
>> +module_init(tiny_spi_init);
>
> Please use platform_driver_register, and put your probe function into
> the platform_driver structure.
>

OK. I will do the same for spi_altera.

Best regards,
Thomas

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] spi: add OpenCores tiny SPI driver
       [not found]               ` <4D38AAEB.7020308-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
@ 2011-01-21 12:27                 ` Jonas Bonn
  2011-01-24  1:19                   ` Thomas Chou
  0 siblings, 1 reply; 13+ messages in thread
From: Jonas Bonn @ 2011-01-21 12:27 UTC (permalink / raw)
  To: Thomas Chou
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, David Brownell,
	Mike Frysinger, devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

Hi,

> 
> >> +#ifdef CONFIG_OF
> >> +static struct of_device_id oc_tiny_spi_match[] = {
> >> +	{
> >> +		.compatible = "opencores,oc_tiny_spi",
> >
> > If this is a soft core, then there should be a version number of some
> > sort on the compatible value.  Also, please use dash '-' instead of
> > underscore '_' in compatible values.  Also, for all of these new
> > bindings, they need to be documented.  Please add documentation to
> > Documentation/powerpc/dts-bindings (yes, I know, this is not
> > for powerpc, but that is the established directory.  I'll move it to a
> > better location soon).
> >

It would be nice to use the same name for the OpenCores project and the
device tree identifier, and since "opencores" is already in the name,
the "oc_" bit is superfluous anyway.  I'd suggest "opencores,tiny-spi"
in order to match your OpenCores project name.

Versioning of OpenCores cores is not sorted yet.  In order to avoid
clashing with the versioning/naming scheme that's decided on, please
just use a neutral version number for now (especially as your core is so
new).  e.g. "opencores,tiny-spi-0"

/Jonas

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] spi: add OpenCores tiny SPI driver
  2011-01-21 12:27                 ` Jonas Bonn
@ 2011-01-24  1:19                   ` Thomas Chou
       [not found]                     ` <4D3CD38D.4080808-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
  0 siblings, 1 reply; 13+ messages in thread
From: Thomas Chou @ 2011-01-24  1:19 UTC (permalink / raw)
  To: Jonas Bonn
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, David Brownell,
	Mike Frysinger, devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

Hi Jonas,


On 01/21/2011 08:27 PM, Jonas Bonn wrote:
> It would be nice to use the same name for the OpenCores project and the
> device tree identifier, and since "opencores" is already in the name,
> the "oc_" bit is superfluous anyway.  I'd suggest "opencores,tiny-spi"
> in order to match your OpenCores project name.
>
> Versioning of OpenCores cores is not sorted yet.  In order to avoid
> clashing with the versioning/naming scheme that's decided on, please
> just use a neutral version number for now (especially as your core is so
> new).  e.g. "opencores,tiny-spi-0"
>

Thanks. I will update it to "opencores,tiny-spi-1.0".

- Thomas

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] spi: add OpenCores tiny SPI driver
       [not found]                     ` <4D3CD38D.4080808-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
@ 2011-01-24 10:59                       ` Jonas Bonn
  2011-01-24 14:50                         ` Thomas Chou
  2011-01-25  3:17                         ` [PATCH v3] " Thomas Chou
  0 siblings, 2 replies; 13+ messages in thread
From: Jonas Bonn @ 2011-01-24 10:59 UTC (permalink / raw)
  To: Thomas Chou
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, David Brownell,
	Mike Frysinger, devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

Hi Thomas,

> > Versioning of OpenCores cores is not sorted yet.  In order to avoid
> > clashing with the versioning/naming scheme that's decided on, please
> > just use a neutral version number for now (especially as your core is so
> > new).  e.g. "opencores,tiny-spi-0"
> >
> 
> Thanks. I will update it to "opencores,tiny-spi-1.0".
> 

We've discussed this a bit internally now and decided that the best way
to version OpenCores cores is to use the SVN commit ID of the rtl/
directory in the project's repository.  So, for your tiny-spi project,
the last SVN commit to update the rtl/ directory was commit number 2;
hence, I'd recommend you use the following device tree identifier:

opencores,tiny-spi-rtlsvn2

That's:  opencores,<project>-rtl<VCS><commit ID>

As projects are currently in SVN, VCS=svn.

Linux drivers for other OpenCores cores will be updated accordingly.

/Jonas


------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] spi: add OpenCores tiny SPI driver
  2011-01-24 10:59                       ` Jonas Bonn
@ 2011-01-24 14:50                         ` Thomas Chou
       [not found]                           ` <4D3D91D1.8000701-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
  2011-01-25  3:17                         ` [PATCH v3] " Thomas Chou
  1 sibling, 1 reply; 13+ messages in thread
From: Thomas Chou @ 2011-01-24 14:50 UTC (permalink / raw)
  To: Jonas Bonn
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, David Brownell,
	Mike Frysinger, devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

Hi Jonas,

On 01/24/2011 06:59 PM, Jonas Bonn wrote:
> We've discussed this a bit internally now and decided that the best way
> to version OpenCores cores is to use the SVN commit ID of the rtl/
> directory in the project's repository.  So, for your tiny-spi project,
> the last SVN commit to update the rtl/ directory was commit number 2;
> hence, I'd recommend you use the following device tree identifier:
>
> opencores,tiny-spi-rtlsvn2
>
> That's:  opencores,<project>-rtl<VCS><commit ID>
>
> As projects are currently in SVN, VCS=svn.
>
> Linux drivers for other OpenCores cores will be updated accordingly.

I will follow your suggestion exactly. Thank you very much.

Best regards,
Thomas

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v2] spi: add OpenCores tiny SPI driver
       [not found]                           ` <4D3D91D1.8000701-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
@ 2011-01-24 15:50                             ` Grant Likely
  0 siblings, 0 replies; 13+ messages in thread
From: Grant Likely @ 2011-01-24 15:50 UTC (permalink / raw)
  To: Thomas Chou
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH, David Brownell,
	Mike Frysinger, devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

On Mon, Jan 24, 2011 at 10:50:57PM +0800, Thomas Chou wrote:
> Hi Jonas,
> 
> On 01/24/2011 06:59 PM, Jonas Bonn wrote:
> >We've discussed this a bit internally now and decided that the best way
> >to version OpenCores cores is to use the SVN commit ID of the rtl/
> >directory in the project's repository.  So, for your tiny-spi project,
> >the last SVN commit to update the rtl/ directory was commit number 2;
> >hence, I'd recommend you use the following device tree identifier:
> >
> >opencores,tiny-spi-rtlsvn2
> >
> >That's:  opencores,<project>-rtl<VCS><commit ID>
> >
> >As projects are currently in SVN, VCS=svn.
> >
> >Linux drivers for other OpenCores cores will be updated accordingly.
> 
> I will follow your suggestion exactly. Thank you very much.

Make sure you also document the binding in Documentation/powerpc/dts-bindings

g.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3] spi: add OpenCores tiny SPI driver
  2011-01-24 10:59                       ` Jonas Bonn
  2011-01-24 14:50                         ` Thomas Chou
@ 2011-01-25  3:17                         ` Thomas Chou
       [not found]                           ` <1295925450-28550-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
  1 sibling, 1 reply; 13+ messages in thread
From: Thomas Chou @ 2011-01-25  3:17 UTC (permalink / raw)
  To: David Brownell, Grant Likely
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH,
	devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

This patch adds support of OpenCores tiny SPI driver.

http://opencores.org/project,tiny_spi

Signed-off-by: Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
---
v2 minor cleanup, same as Grant suggest for spi_altera.
v3 rename driver to spi_oc_tiny as Grant suggested.
   set version to tiny-spi-rtlsvn2 as Jonas suggested.

 drivers/spi/Kconfig             |    6 +
 drivers/spi/Makefile            |    1 +
 drivers/spi/spi_oc_tiny.c       |  398 +++++++++++++++++++++++++++++++++++++++
 include/linux/spi/spi_oc_tiny.h |   14 ++
 4 files changed, 419 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/spi_oc_tiny.c
 create mode 100644 include/linux/spi/spi_oc_tiny.h

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index bb233a9..fbb82ee 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -231,6 +231,12 @@ config SPI_FSL_ESPI
 	  From MPC8536, 85xx platform uses the controller, and all P10xx,
 	  P20xx, P30xx,P40xx, P50xx uses this controller.
 
+config SPI_OC_TINY
+	tristate "OpenCores tiny SPI"
+	select SPI_BITBANG
+	help
+	  This is the driver for OpenCores tiny SPI master controller.
+
 config SPI_OMAP_UWIRE
 	tristate "OMAP1 MicroWire"
 	depends on ARCH_OMAP1
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 86d1b5f..c7e4c23 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -27,6 +27,7 @@ obj-$(CONFIG_SPI_IMX)			+= spi_imx.o
 obj-$(CONFIG_SPI_LM70_LLP)		+= spi_lm70llp.o
 obj-$(CONFIG_SPI_PXA2XX)		+= pxa2xx_spi.o
 obj-$(CONFIG_SPI_PXA2XX_PCI)		+= pxa2xx_spi_pci.o
+obj-$(CONFIG_SPI_OC_TINY)		+= spi_oc_tiny.o
 obj-$(CONFIG_SPI_OMAP_UWIRE)		+= omap_uwire.o
 obj-$(CONFIG_SPI_OMAP24XX)		+= omap2_mcspi.o
 obj-$(CONFIG_SPI_OMAP_100K)		+= omap_spi_100k.o
diff --git a/drivers/spi/spi_oc_tiny.c b/drivers/spi/spi_oc_tiny.c
new file mode 100644
index 0000000..6515b25
--- /dev/null
+++ b/drivers/spi/spi_oc_tiny.c
@@ -0,0 +1,398 @@
+/*
+ * OpenCores tiny SPI master driver
+ *
+ * http://opencores.org/project,tiny_spi
+ *
+ * Copyright (C) 2011 Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
+ *
+ * Based on spi_s3c24xx.c, which is:
+ * Copyright (c) 2006 Ben Dooks
+ * Copyright (c) 2006 Simtec Electronics
+ *	Ben Dooks <ben-Y5A6D6n0/KfQXOPxS62xeg@public.gmane.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/clk.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
+#include <linux/spi/spi_oc_tiny.h>
+#include <linux/io.h>
+#include <linux/gpio.h>
+#include <linux/of.h>
+
+#define DRV_NAME "spi_oc_tiny"
+
+#define TINY_SPI_RXDATA 0
+#define TINY_SPI_TXDATA 4
+#define TINY_SPI_STATUS 8
+#define TINY_SPI_CONTROL 12
+#define TINY_SPI_BAUD 16
+
+#define TINY_SPI_STATUS_TXE 0x1
+#define TINY_SPI_STATUS_TXR 0x2
+
+struct tiny_spi {
+	/* bitbang has to be first */
+	struct spi_bitbang bitbang;
+	struct completion done;
+
+	void __iomem *base;
+	int irq;
+	unsigned int freq;
+	unsigned int baudwidth;
+	unsigned int baud;
+	unsigned int speed_hz;
+	unsigned int mode;
+	unsigned int len;
+	unsigned int txc, rxc;
+	const u8 *txp;
+	u8 *rxp;
+};
+
+static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
+{
+	return spi_master_get_devdata(sdev->master);
+}
+
+static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+
+	return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
+}
+
+static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
+{
+	gpio_set_value(spi->chip_select,
+		       (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
+}
+
+static int tiny_spi_setup_transfer(struct spi_device *spi,
+				   struct spi_transfer *t)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+	unsigned int baud = hw->baud;
+
+	if (t) {
+		if (t->speed_hz && t->speed_hz != hw->speed_hz)
+			baud = tiny_spi_baud(spi, t->speed_hz);
+	}
+	writel(baud, hw->base + TINY_SPI_BAUD);
+	writel(hw->mode, hw->base + TINY_SPI_CONTROL);
+	return 0;
+}
+
+static int tiny_spi_setup(struct spi_device *spi)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+
+	if (spi->max_speed_hz != hw->speed_hz) {
+		hw->speed_hz = spi->max_speed_hz;
+		hw->baud = tiny_spi_baud(spi, hw->speed_hz);
+	}
+	hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
+	return 0;
+}
+
+static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+	const u8 *txp = t->tx_buf;
+	u8 *rxp = t->rx_buf;
+	unsigned int i;
+
+	if (hw->irq >= 0) {
+		/* use intrrupt driven data transfer */
+		hw->len = t->len;
+		hw->txp = t->tx_buf;
+		hw->rxp = t->rx_buf;
+		hw->txc = 0;
+		hw->rxc = 0;
+
+		/* send the first byte */
+		if (t->len > 1) {
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
+		}
+
+		wait_for_completion(&hw->done);
+	} else if (txp && rxp) {
+		/* we need to tighten the transfer loop */
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx, tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (rxp) {
+		writeb(0, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(0,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(0,
+				       hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (txp) {
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	} else {
+		writeb(0, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(0,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(0,
+				       hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	}
+	return t->len;
+}
+
+static irqreturn_t tiny_spi_irq(int irq, void *dev)
+{
+	struct tiny_spi *hw = dev;
+
+	writeb(0, hw->base + TINY_SPI_STATUS);
+	if (hw->rxc + 1 == hw->len) {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+		hw->rxc++;
+		complete(&hw->done);
+	} else {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		hw->rxc++;
+		if (hw->txc < hw->len) {
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR,
+			       hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(TINY_SPI_STATUS_TXE,
+			       hw->base + TINY_SPI_STATUS);
+		}
+	}
+	return IRQ_HANDLED;
+}
+
+static int __devinit tiny_spi_probe(struct platform_device *pdev)
+{
+	struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
+	struct tiny_spi *hw;
+	struct spi_master *master;
+	struct resource *res;
+	int err = 0;
+
+	master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
+	if (master == NULL) {
+		dev_err(&pdev->dev, "No memory for spi_master\n");
+		err = -ENOMEM;
+		goto err_no_mem;
+	}
+
+	/* setup the master state. */
+	master->bus_num = pdev->id;
+	master->num_chipselect = 255;
+	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
+	master->setup = tiny_spi_setup;
+
+	hw = spi_master_get_devdata(master);
+	platform_set_drvdata(pdev, hw);
+
+	/* setup the state for the bitbang driver */
+	hw->bitbang.master = spi_master_get(master);
+	if (hw->bitbang.master == NULL) {
+		dev_err(&pdev->dev, "Cannot get device\n");
+		err = -ENODEV;
+		goto err_no_dev;
+	}
+	hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
+	hw->bitbang.chipselect = tiny_spi_chipselect;
+	hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
+
+	/* find and map our resources */
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (res == NULL) {
+		dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
+		err = -ENOENT;
+		goto err_no_iores;
+	}
+	hw->base = ioremap(res->start, (res->end - res->start) + 1);
+	if (hw->base == 0) {
+		dev_err(&pdev->dev, "Cannot map IO\n");
+		err = -ENXIO;
+		goto err_no_iomap;
+	}
+	/* irq is optional */
+	hw->irq = platform_get_irq(pdev, 0);
+	if (hw->irq >= 0) {
+		init_completion(&hw->done);
+		err = request_irq(hw->irq, tiny_spi_irq, 0, pdev->name, hw);
+		if (err) {
+			dev_err(&pdev->dev, "Cannot claim IRQ\n");
+			goto err_no_irq;
+		}
+	}
+	/* find platform data */
+	if (platp) {
+		hw->freq = platp->freq;
+		hw->baudwidth = platp->baudwidth;
+	} else {
+#ifdef CONFIG_OF
+		const __be32 *val;
+
+		hw->bitbang.master->dev.of_node = pdev->dev.of_node;
+		val = of_get_property(pdev->dev.of_node,
+				      "clock-frequency", NULL);
+		if (val)
+			hw->freq = be32_to_cpup(val);
+		val = of_get_property(pdev->dev.of_node, "baud-width", NULL);
+		if (val)
+			hw->baudwidth = be32_to_cpup(val);
+#endif
+	}
+
+	/* register our spi controller */
+	err = spi_bitbang_start(&hw->bitbang);
+	if (err) {
+		dev_err(&pdev->dev, "Failed to register SPI master\n");
+		goto err_register;
+	}
+	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
+
+	return 0;
+
+err_register:
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+err_no_irq:
+	iounmap(hw->base);
+err_no_iomap:
+err_no_iores:
+	spi_master_put(master);
+err_no_mem:
+err_no_dev:
+	return err;
+}
+
+static int __devexit tiny_spi_remove(struct platform_device *dev)
+{
+	struct tiny_spi *hw = platform_get_drvdata(dev);
+	struct spi_master *master = hw->bitbang.master;
+
+	spi_bitbang_stop(&hw->bitbang);
+
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+	iounmap(hw->base);
+
+	platform_set_drvdata(dev, NULL);
+	spi_master_put(master);
+	return 0;
+}
+
+static const struct of_device_id tiny_spi_match[] = {
+	{ .compatible = "opencores,tiny-spi-rtlsvn2", },
+	{},
+}
+MODULE_DEVICE_TABLE(of, tiny_spi_match);
+
+static struct platform_driver tiny_spidrv = {
+	.remove = __devexit_p(tiny_spi_remove),
+	.driver = {
+		.name = DRV_NAME,
+		.owner = THIS_MODULE,
+		.pm = NULL,
+#ifdef CONFIG_OF
+		.of_match_table = tiny_spi_match,
+#endif
+	},
+};
+
+static int __init tiny_spi_init(void)
+{
+	return platform_driver_probe(&tiny_spidrv, tiny_spi_probe);
+}
+module_init(tiny_spi_init);
+
+static void __exit tiny_spi_exit(void)
+{
+	platform_driver_unregister(&tiny_spidrv);
+}
+module_exit(tiny_spi_exit);
+
+MODULE_DESCRIPTION("OpenCores tiny SPI driver");
+MODULE_AUTHOR("Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
diff --git a/include/linux/spi/spi_oc_tiny.h b/include/linux/spi/spi_oc_tiny.h
new file mode 100644
index 0000000..5080e1c
--- /dev/null
+++ b/include/linux/spi/spi_oc_tiny.h
@@ -0,0 +1,14 @@
+#ifndef _LINUX_SPI_SPI_OC_TINY_H
+#define _LINUX_SPI_SPI_OC_TINY_H
+
+/*
+ * struct tiny_spi_platform_data - platform data of the OpenCores tiny SPI
+ * @freq:	input clock freq to the core.
+ * @baudwidth:	baud rate divider width of the core.
+ */
+struct tiny_spi_platform_data {
+	uint freq;
+	uint baudwidth;
+};
+
+#endif /* _LINUX_SPI_SPI_OC_TINY_H */
-- 
1.7.3.5

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v4] spi: add OpenCores tiny SPI driver
       [not found]                           ` <1295925450-28550-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
@ 2011-02-03 10:37                             ` Thomas Chou
  2011-02-03 11:03                               ` Wolfram Sang
  2011-02-03 15:20                               ` Dirk Brandewie
  0 siblings, 2 replies; 13+ messages in thread
From: Thomas Chou @ 2011-02-03 10:37 UTC (permalink / raw)
  To: David Brownell, Grant Likely
  Cc: nios2-dev-1eJk0qcHJCcaeqlQEoCUNoJY59XmG8rH,
	devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

This patch adds support of OpenCores tiny SPI driver.

http://opencores.org/project,tiny_spi

Signed-off-by: Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
---
v2 minor cleanup, same as Grant suggest for spi_altera.
v3 rename driver to spi_oc_tiny as Grant suggested.
   set version to tiny-spi-rtlsvn2 as Jonas suggested.
v4 add dts binding doc.

 .../devicetree/bindings/spi/spi_oc_tiny.txt        |   11 +
 drivers/spi/Kconfig                                |    6 +
 drivers/spi/Makefile                               |    1 +
 drivers/spi/spi_oc_tiny.c                          |  396 ++++++++++++++++++++
 include/linux/spi/spi_oc_tiny.h                    |   14 +
 5 files changed, 428 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/spi/spi_oc_tiny.txt
 create mode 100644 drivers/spi/spi_oc_tiny.c
 create mode 100644 include/linux/spi/spi_oc_tiny.h

diff --git a/Documentation/devicetree/bindings/spi/spi_oc_tiny.txt b/Documentation/devicetree/bindings/spi/spi_oc_tiny.txt
new file mode 100644
index 0000000..fba0f30
--- /dev/null
+++ b/Documentation/devicetree/bindings/spi/spi_oc_tiny.txt
@@ -0,0 +1,11 @@
+OpenCores tiny SPI
+
+Required properties:
+- compatible : should be "opencores,tiny-spi-rtlsvn2".
+Optional properties:
+- clock-frequency : input clock frequency to the core.
+- baud-width: width, in bits, of the programmable divider used to scale
+	the input clock to SCLK.
+
+The clock-frequency and baud-width properties are needed only if the divider
+is programmable. They are not needed if the divider is fixed.
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index bb233a9..fbb82ee 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -231,6 +231,12 @@ config SPI_FSL_ESPI
 	  From MPC8536, 85xx platform uses the controller, and all P10xx,
 	  P20xx, P30xx,P40xx, P50xx uses this controller.
 
+config SPI_OC_TINY
+	tristate "OpenCores tiny SPI"
+	select SPI_BITBANG
+	help
+	  This is the driver for OpenCores tiny SPI master controller.
+
 config SPI_OMAP_UWIRE
 	tristate "OMAP1 MicroWire"
 	depends on ARCH_OMAP1
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 86d1b5f..c7e4c23 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -27,6 +27,7 @@ obj-$(CONFIG_SPI_IMX)			+= spi_imx.o
 obj-$(CONFIG_SPI_LM70_LLP)		+= spi_lm70llp.o
 obj-$(CONFIG_SPI_PXA2XX)		+= pxa2xx_spi.o
 obj-$(CONFIG_SPI_PXA2XX_PCI)		+= pxa2xx_spi_pci.o
+obj-$(CONFIG_SPI_OC_TINY)		+= spi_oc_tiny.o
 obj-$(CONFIG_SPI_OMAP_UWIRE)		+= omap_uwire.o
 obj-$(CONFIG_SPI_OMAP24XX)		+= omap2_mcspi.o
 obj-$(CONFIG_SPI_OMAP_100K)		+= omap_spi_100k.o
diff --git a/drivers/spi/spi_oc_tiny.c b/drivers/spi/spi_oc_tiny.c
new file mode 100644
index 0000000..1ea3223
--- /dev/null
+++ b/drivers/spi/spi_oc_tiny.c
@@ -0,0 +1,396 @@
+/*
+ * OpenCores tiny SPI master driver
+ *
+ * http://opencores.org/project,tiny_spi
+ *
+ * Copyright (C) 2011 Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
+ *
+ * Based on spi_s3c24xx.c, which is:
+ * Copyright (c) 2006 Ben Dooks
+ * Copyright (c) 2006 Simtec Electronics
+ *	Ben Dooks <ben-Y5A6D6n0/KfQXOPxS62xeg@public.gmane.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/clk.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
+#include <linux/spi/spi_oc_tiny.h>
+#include <linux/io.h>
+#include <linux/gpio.h>
+#include <linux/of.h>
+
+#define DRV_NAME "spi_oc_tiny"
+
+#define TINY_SPI_RXDATA 0
+#define TINY_SPI_TXDATA 4
+#define TINY_SPI_STATUS 8
+#define TINY_SPI_CONTROL 12
+#define TINY_SPI_BAUD 16
+
+#define TINY_SPI_STATUS_TXE 0x1
+#define TINY_SPI_STATUS_TXR 0x2
+
+struct tiny_spi {
+	/* bitbang has to be first */
+	struct spi_bitbang bitbang;
+	struct completion done;
+
+	void __iomem *base;
+	int irq;
+	unsigned int freq;
+	unsigned int baudwidth;
+	unsigned int baud;
+	unsigned int speed_hz;
+	unsigned int mode;
+	unsigned int len;
+	unsigned int txc, rxc;
+	const u8 *txp;
+	u8 *rxp;
+};
+
+static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
+{
+	return spi_master_get_devdata(sdev->master);
+}
+
+static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+
+	return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
+}
+
+static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
+{
+	gpio_set_value(spi->chip_select,
+		       (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
+}
+
+static int tiny_spi_setup_transfer(struct spi_device *spi,
+				   struct spi_transfer *t)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+	unsigned int baud = hw->baud;
+
+	if (t) {
+		if (t->speed_hz && t->speed_hz != hw->speed_hz)
+			baud = tiny_spi_baud(spi, t->speed_hz);
+	}
+	writel(baud, hw->base + TINY_SPI_BAUD);
+	writel(hw->mode, hw->base + TINY_SPI_CONTROL);
+	return 0;
+}
+
+static int tiny_spi_setup(struct spi_device *spi)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+
+	if (spi->max_speed_hz != hw->speed_hz) {
+		hw->speed_hz = spi->max_speed_hz;
+		hw->baud = tiny_spi_baud(spi, hw->speed_hz);
+	}
+	hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
+	return 0;
+}
+
+static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
+{
+	struct tiny_spi *hw = tiny_spi_to_hw(spi);
+	const u8 *txp = t->tx_buf;
+	u8 *rxp = t->rx_buf;
+	unsigned int i;
+
+	if (hw->irq >= 0) {
+		/* use intrrupt driven data transfer */
+		hw->len = t->len;
+		hw->txp = t->tx_buf;
+		hw->rxp = t->rx_buf;
+		hw->txc = 0;
+		hw->rxc = 0;
+
+		/* send the first byte */
+		if (t->len > 1) {
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
+		}
+
+		wait_for_completion(&hw->done);
+	} else if (txp && rxp) {
+		/* we need to tighten the transfer loop */
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx, tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (rxp) {
+		writeb(0, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(0,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 rx;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				rx = readb(hw->base + TINY_SPI_TXDATA);
+				writeb(0,
+				       hw->base + TINY_SPI_TXDATA);
+				*rxp++ = rx;
+			}
+			while (!(readb(hw->base + TINY_SPI_STATUS) &
+				 TINY_SPI_STATUS_TXR))
+				cpu_relax();
+			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+	} else if (txp) {
+		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				u8 tx = *txp++;
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(tx, hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	} else {
+		writeb(0, hw->base + TINY_SPI_TXDATA);
+		if (t->len > 1) {
+			writeb(0,
+			       hw->base + TINY_SPI_TXDATA);
+			for (i = 2; i < t->len; i++) {
+				while (!(readb(hw->base + TINY_SPI_STATUS) &
+					 TINY_SPI_STATUS_TXR))
+					cpu_relax();
+				writeb(0,
+				       hw->base + TINY_SPI_TXDATA);
+			}
+		}
+		while (!(readb(hw->base + TINY_SPI_STATUS) &
+			 TINY_SPI_STATUS_TXE))
+			cpu_relax();
+	}
+	return t->len;
+}
+
+static irqreturn_t tiny_spi_irq(int irq, void *dev)
+{
+	struct tiny_spi *hw = dev;
+
+	writeb(0, hw->base + TINY_SPI_STATUS);
+	if (hw->rxc + 1 == hw->len) {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
+		hw->rxc++;
+		complete(&hw->done);
+	} else {
+		if (hw->rxp)
+			*hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
+		hw->rxc++;
+		if (hw->txc < hw->len) {
+			writeb(hw->txp ? *hw->txp++ : 0,
+			       hw->base + TINY_SPI_TXDATA);
+			hw->txc++;
+			writeb(TINY_SPI_STATUS_TXR,
+			       hw->base + TINY_SPI_STATUS);
+		} else {
+			writeb(TINY_SPI_STATUS_TXE,
+			       hw->base + TINY_SPI_STATUS);
+		}
+	}
+	return IRQ_HANDLED;
+}
+
+static int __devinit tiny_spi_probe(struct platform_device *pdev)
+{
+	struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
+	struct tiny_spi *hw;
+	struct spi_master *master;
+	struct resource *res;
+	int err = 0;
+
+	master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
+	if (master == NULL) {
+		dev_err(&pdev->dev, "No memory for spi_master\n");
+		err = -ENOMEM;
+		goto err_no_mem;
+	}
+
+	/* setup the master state. */
+	master->bus_num = pdev->id;
+	master->num_chipselect = 255;
+	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
+	master->setup = tiny_spi_setup;
+
+	hw = spi_master_get_devdata(master);
+	platform_set_drvdata(pdev, hw);
+
+	/* setup the state for the bitbang driver */
+	hw->bitbang.master = spi_master_get(master);
+	if (hw->bitbang.master == NULL) {
+		dev_err(&pdev->dev, "Cannot get device\n");
+		err = -ENODEV;
+		goto err_no_dev;
+	}
+	hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
+	hw->bitbang.chipselect = tiny_spi_chipselect;
+	hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
+
+	/* find and map our resources */
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (res == NULL) {
+		dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
+		err = -ENOENT;
+		goto err_no_iores;
+	}
+	hw->base = ioremap(res->start, (res->end - res->start) + 1);
+	if (hw->base == 0) {
+		dev_err(&pdev->dev, "Cannot map IO\n");
+		err = -ENXIO;
+		goto err_no_iomap;
+	}
+	/* irq is optional */
+	hw->irq = platform_get_irq(pdev, 0);
+	if (hw->irq >= 0) {
+		init_completion(&hw->done);
+		err = request_irq(hw->irq, tiny_spi_irq, 0, pdev->name, hw);
+		if (err) {
+			dev_err(&pdev->dev, "Cannot claim IRQ\n");
+			goto err_no_irq;
+		}
+	}
+	/* find platform data */
+	if (platp) {
+		hw->freq = platp->freq;
+		hw->baudwidth = platp->baudwidth;
+	} else {
+#ifdef CONFIG_OF
+		const __be32 *val;
+
+		hw->bitbang.master->dev.of_node = pdev->dev.of_node;
+		val = of_get_property(pdev->dev.of_node,
+				      "clock-frequency", NULL);
+		if (val)
+			hw->freq = be32_to_cpup(val);
+		val = of_get_property(pdev->dev.of_node, "baud-width", NULL);
+		if (val)
+			hw->baudwidth = be32_to_cpup(val);
+#endif
+	}
+
+	/* register our spi controller */
+	err = spi_bitbang_start(&hw->bitbang);
+	if (err) {
+		dev_err(&pdev->dev, "Failed to register SPI master\n");
+		goto err_register;
+	}
+	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
+
+	return 0;
+
+err_register:
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+err_no_irq:
+	iounmap(hw->base);
+err_no_iomap:
+err_no_iores:
+	spi_master_put(master);
+err_no_mem:
+err_no_dev:
+	return err;
+}
+
+static int __devexit tiny_spi_remove(struct platform_device *dev)
+{
+	struct tiny_spi *hw = platform_get_drvdata(dev);
+	struct spi_master *master = hw->bitbang.master;
+
+	spi_bitbang_stop(&hw->bitbang);
+
+	if (hw->irq >= 0)
+		free_irq(hw->irq, hw);
+	iounmap(hw->base);
+
+	platform_set_drvdata(dev, NULL);
+	spi_master_put(master);
+	return 0;
+}
+
+static const struct of_device_id tiny_spi_match[] = {
+	{ .compatible = "opencores,tiny-spi-rtlsvn2", },
+	{},
+}
+MODULE_DEVICE_TABLE(of, tiny_spi_match);
+
+static struct platform_driver tiny_spidrv = {
+	.remove = __devexit_p(tiny_spi_remove),
+	.driver = {
+		.name = DRV_NAME,
+		.owner = THIS_MODULE,
+		.pm = NULL,
+		.of_match_table = tiny_spi_match,
+	},
+};
+
+static int __init tiny_spi_init(void)
+{
+	return platform_driver_probe(&tiny_spidrv, tiny_spi_probe);
+}
+module_init(tiny_spi_init);
+
+static void __exit tiny_spi_exit(void)
+{
+	platform_driver_unregister(&tiny_spidrv);
+}
+module_exit(tiny_spi_exit);
+
+MODULE_DESCRIPTION("OpenCores tiny SPI driver");
+MODULE_AUTHOR("Thomas Chou <thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" DRV_NAME);
diff --git a/include/linux/spi/spi_oc_tiny.h b/include/linux/spi/spi_oc_tiny.h
new file mode 100644
index 0000000..5080e1c
--- /dev/null
+++ b/include/linux/spi/spi_oc_tiny.h
@@ -0,0 +1,14 @@
+#ifndef _LINUX_SPI_SPI_OC_TINY_H
+#define _LINUX_SPI_SPI_OC_TINY_H
+
+/*
+ * struct tiny_spi_platform_data - platform data of the OpenCores tiny SPI
+ * @freq:	input clock freq to the core.
+ * @baudwidth:	baud rate divider width of the core.
+ */
+struct tiny_spi_platform_data {
+	uint freq;
+	uint baudwidth;
+};
+
+#endif /* _LINUX_SPI_SPI_OC_TINY_H */
-- 
1.7.3.5

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v4] spi: add OpenCores tiny SPI driver
  2011-02-03 10:37                             ` [PATCH v4] " Thomas Chou
@ 2011-02-03 11:03                               ` Wolfram Sang
  2011-02-03 15:20                               ` Dirk Brandewie
  1 sibling, 0 replies; 13+ messages in thread
From: Wolfram Sang @ 2011-02-03 11:03 UTC (permalink / raw)
  To: Thomas Chou
  Cc: David Brownell, Grant Likely, nios2-dev, devicetree-discuss,
	linux-kernel, spi-devel-general

[-- Attachment #1: Type: text/plain, Size: 1041 bytes --]

On Thu, Feb 03, 2011 at 06:37:46PM +0800, Thomas Chou wrote:

> This patch adds support of OpenCores tiny SPI driver.

A few comments to probe().

> +	/* find and map our resources */
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	if (res == NULL) {
> +		dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
> +		err = -ENOENT;
> +		goto err_no_iores;
> +	}
> +	hw->base = ioremap(res->start, (res->end - res->start) + 1);

No request_mem_region?
Maybe ioremap_nocache?
Please use the resource_size()-macro.

> +err_register:
> +	if (hw->irq >= 0)
> +		free_irq(hw->irq, hw);
> +err_no_irq:
> +	iounmap(hw->base);
> +err_no_iomap:
> +err_no_iores:
> +	spi_master_put(master);
> +err_no_mem:
> +err_no_dev:
> +	return err;

It may pay off to use managed devices (devm_*), so this part will become
very simple.

Regards,

   Wolfram

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v4] spi: add OpenCores tiny SPI driver
  2011-02-03 10:37                             ` [PATCH v4] " Thomas Chou
  2011-02-03 11:03                               ` Wolfram Sang
@ 2011-02-03 15:20                               ` Dirk Brandewie
  1 sibling, 0 replies; 13+ messages in thread
From: Dirk Brandewie @ 2011-02-03 15:20 UTC (permalink / raw)
  To: Thomas Chou
  Cc: David Brownell, Grant Likely, nios2-dev, devicetree-discuss,
	linux-kernel, spi-devel-general

On 02/03/2011 02:37 AM, Thomas Chou wrote:
> This patch adds support of OpenCores tiny SPI driver.
>
> http://opencores.org/project,tiny_spi
>

> +static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
> +{
> +	struct tiny_spi *hw = tiny_spi_to_hw(spi);
> +	const u8 *txp = t->tx_buf;
> +	u8 *rxp = t->rx_buf;
> +	unsigned int i;
> +
> +	if (hw->irq>= 0) {
> +		/* use intrrupt driven data transfer */
> +		hw->len = t->len;
> +		hw->txp = t->tx_buf;
> +		hw->rxp = t->rx_buf;
> +		hw->txc = 0;
> +		hw->rxc = 0;
> +
> +		/* send the first byte */
> +		if (t->len>  1) {
> +			writeb(hw->txp ? *hw->txp++ : 0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(hw->txp ? *hw->txp++ : 0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
> +		} else {
> +			writeb(hw->txp ? *hw->txp++ : 0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			hw->txc++;
> +			writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
> +		}
> +
> +		wait_for_completion(&hw->done);
> +	} else if (txp&&  rxp) {
> +		/* we need to tighten the transfer loop */
> +		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				u8 rx, tx = *txp++;
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();

Putting the read status, cpu releax in an inline function would make this 
function a lot easier to read.

> +				rx = readb(hw->base + TINY_SPI_TXDATA);
> +				writeb(tx, hw->base + TINY_SPI_TXDATA);
> +				*rxp++ = rx;
> +			}
> +			while (!(readb(hw->base + TINY_SPI_STATUS)&
> +				 TINY_SPI_STATUS_TXR))
> +				cpu_relax();
> +			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
> +	} else if (rxp) {
> +		writeb(0, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				u8 rx;
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				rx = readb(hw->base + TINY_SPI_TXDATA);
> +				writeb(0,
> +				       hw->base + TINY_SPI_TXDATA);
> +				*rxp++ = rx;
> +			}
> +			while (!(readb(hw->base + TINY_SPI_STATUS)&
> +				 TINY_SPI_STATUS_TXR))
> +				cpu_relax();
> +			*rxp++ = readb(hw->base + TINY_SPI_TXDATA);
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +		*rxp++ = readb(hw->base + TINY_SPI_RXDATA);
> +	} else if (txp) {
> +		writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(*txp++, hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				u8 tx = *txp++;
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				writeb(tx, hw->base + TINY_SPI_TXDATA);
> +			}
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +	} else {
> +		writeb(0, hw->base + TINY_SPI_TXDATA);
> +		if (t->len>  1) {
> +			writeb(0,
> +			       hw->base + TINY_SPI_TXDATA);
> +			for (i = 2; i<  t->len; i++) {
> +				while (!(readb(hw->base + TINY_SPI_STATUS)&
> +					 TINY_SPI_STATUS_TXR))
> +					cpu_relax();
> +				writeb(0,
> +				       hw->base + TINY_SPI_TXDATA);
> +			}
> +		}
> +		while (!(readb(hw->base + TINY_SPI_STATUS)&
> +			 TINY_SPI_STATUS_TXE))
> +			cpu_relax();
> +	}
> +	return t->len;
> +}

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2011-02-03 15:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1294645208-6322-1-git-send-email-thomas@wytron.com.tw>
     [not found] ` <1294645208-6322-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-12  2:41   ` [PATCH v2] spi: add OpenCores tiny SPI driver Thomas Chou
     [not found]     ` <1294800109-31603-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-17  7:32       ` Thomas Chou
     [not found]         ` <1295249542-26743-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-20 16:54           ` Grant Likely
2011-01-20 21:36             ` Thomas Chou
     [not found]               ` <4D38AAEB.7020308-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-21 12:27                 ` Jonas Bonn
2011-01-24  1:19                   ` Thomas Chou
     [not found]                     ` <4D3CD38D.4080808-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-24 10:59                       ` Jonas Bonn
2011-01-24 14:50                         ` Thomas Chou
     [not found]                           ` <4D3D91D1.8000701-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-01-24 15:50                             ` Grant Likely
2011-01-25  3:17                         ` [PATCH v3] " Thomas Chou
     [not found]                           ` <1295925450-28550-1-git-send-email-thomas-SDxUXYEhEBiCuPEqFHbRBg@public.gmane.org>
2011-02-03 10:37                             ` [PATCH v4] " Thomas Chou
2011-02-03 11:03                               ` Wolfram Sang
2011-02-03 15:20                               ` Dirk Brandewie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).