[U-Boot-Users] [RFC 2/2] atmel_spi: Driver for the Atmel SPI controller

Haavard Skinnemoen hskinnemoen at atmel.com
Fri Nov 30 13:44:15 CET 2007


From: Hans-Christian Egtvedt <hcegtvedt at atmel.com>

This adds a driver for the SPI controller found on most AT91 and AVR32
chips.

Currently, AT91RM9200 seems to have a SPI framework on its own. It
probably makes sense to merge the two drivers and make AT91RM9200 use
the framework everyone else uses at some point...

[hskinnemoen at atmel.com: moved to drivers/spi and simplified]
Signed-off-by: Haavard Skinnemoen <hskinnemoen at atmel.com>
---
 Makefile                |    2 +
 drivers/spi/Makefile    |   46 +++++++++++++++++++
 drivers/spi/atmel_spi.c |  111 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/spi/atmel_spi.h |   84 +++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/Makefile
 create mode 100644 drivers/spi/atmel_spi.c
 create mode 100644 drivers/spi/atmel_spi.h

diff --git a/Makefile b/Makefile
index bfd4132..7293a11 100644
--- a/Makefile
+++ b/Makefile
@@ -227,6 +227,7 @@ LIBS += drivers/qe/qe.a
 endif
 LIBS += drivers/rtc/librtc.a
 LIBS += drivers/serial/libserial.a
+LIBS += drivers/spi/libspi.a
 LIBS += drivers/usb/libusb.a
 LIBS += drivers/video/libvideo.a
 LIBS += post/libpost.a post/drivers/libpostdrivers.a
@@ -359,6 +360,7 @@ TAG_SUBDIRS += drivers/pcmcia
 TAG_SUBDIRS += drivers/qe
 TAG_SUBDIRS += drivers/rtc
 TAG_SUBDIRS += drivers/serial
+TAG_SUBDIRS += drivers/spi
 TAG_SUBDIRS += drivers/usb
 TAG_SUBDIRS += drivers/video
 
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
new file mode 100644
index 0000000..61d33a1
--- /dev/null
+++ b/drivers/spi/Makefile
@@ -0,0 +1,46 @@
+#
+# (C) Copyright 2000-2007
+# Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+#
+# See file CREDITS for list of people who contributed to this
+# project.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+# MA 02111-1307 USA
+#
+
+include $(TOPDIR)/config.mk
+
+LIB 				:= $(obj)libspi.a
+
+COBJS-$(CONFIG_ATMEL_SPI)	+= atmel_spi.o
+
+COBJS	:= $(COBJS-y)
+SRCS 	:= $(COBJS:.o=.c)
+OBJS 	:= $(addprefix $(obj),$(COBJS))
+
+all:	$(LIB)
+
+$(LIB):	$(obj).depend $(OBJS)
+	$(AR) $(ARFLAGS) $@ $(OBJS)
+
+#########################################################################
+
+# defines $(obj).depend target
+include $(SRCTREE)/rules.mk
+
+sinclude $(obj).depend
+
+#########################################################################
diff --git a/drivers/spi/atmel_spi.c b/drivers/spi/atmel_spi.c
new file mode 100644
index 0000000..11b2a59
--- /dev/null
+++ b/drivers/spi/atmel_spi.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2007 Atmel Corporation
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+#include <common.h>
+#include <spi.h>
+
+#include <asm/io.h>
+
+#include <asm/arch/clk.h>
+#include <asm/arch/memory-map.h>
+
+#define ATMEL_SPI_MASTER	0
+#define ATMEL_SPI_BASE		SPI0_BASE
+
+#include "atmel_spi.h"
+
+void spi_init()
+{
+	/* We currently don't support external chip select decoding */
+	spi_writel(CR, ATMEL_SPI_CR_SWRST);
+	spi_writel(CR, ATMEL_SPI_CR_SPIEN);
+	spi_writel(MR, ATMEL_SPI_MR_MSTR | ATMEL_SPI_MR_PCS(15));
+}
+
+int spi_setup(int cs, unsigned int max_hz, unsigned int mode)
+{
+	unsigned long	scbr;
+	u32		csrx;
+
+	if (cs >= 4 || !spi_cs_is_valid(cs))
+		return -1;
+
+	scbr = (get_spi_clk_rate(ATMEL_SPI_MASTER) + max_hz - 1) / max_hz;
+	if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
+		/* Too low max SCK rate */
+		return -1;
+	if (scbr < 1)
+		scbr = 1;
+
+	csrx = ATMEL_SPI_CSRx_SCBR(scbr);
+	csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
+	if (!(mode & SPI_CPHA))
+		csrx |= ATMEL_SPI_CSRx_NCPHA;
+	if (mode & SPI_CPOL)
+		csrx |= ATMEL_SPI_CSRx_CPOL;
+
+	spi_writel(CSR(cs), csrx);
+
+	return 0;
+}
+
+int spi_xfer(int cs, int bitlen, uchar *dout, uchar *din)
+{
+	unsigned int	len_tx;
+	unsigned int	len_rx;
+	unsigned int	len;
+	u32		status;
+
+	if (bitlen == 0)
+		return 0;
+	if (bitlen < 0 || bitlen % 8)
+		return -1;
+
+	len = bitlen / 8;
+
+	spi_writel(MR, ATMEL_SPI_MR_MSTR | ATMEL_SPI_MR_PCS(~(1 << cs)));
+
+	for (len_tx = 0, len_rx = 0; len_rx < len; ) {
+		status = spi_readl(SR);
+
+		if (status & (ATMEL_SPI_SR_MODF | ATMEL_SPI_SR_OVRES))
+			return -1;
+
+		if (len_tx < len && (status & ATMEL_SPI_SR_TDRE)) {
+			spi_writel(TDR, *dout++);
+			len_tx++;
+		}
+		if (status & ATMEL_SPI_SR_RDRF) {
+			*din++ = spi_readl(RDR);
+			len_rx++;
+		}
+	}
+
+	/*
+	 * Wait until the transfer is completely done so that we can
+	 * be sure that CS is deactivated.
+	 */
+	do {
+		status = spi_readl(SR);
+	} while (!(status & ATMEL_SPI_SR_TXEMPTY));
+
+	return 0;
+}
diff --git a/drivers/spi/atmel_spi.h b/drivers/spi/atmel_spi.h
new file mode 100644
index 0000000..4df8cbe
--- /dev/null
+++ b/drivers/spi/atmel_spi.h
@@ -0,0 +1,84 @@
+/*
+ * Register definitions for the Atmel AT32/AT91 SPI Controller
+ */
+
+/* Register offsets */
+#define ATMEL_SPI_CR			0x0000
+#define ATMEL_SPI_MR			0x0004
+#define ATMEL_SPI_RDR			0x0008
+#define ATMEL_SPI_TDR			0x000c
+#define ATMEL_SPI_SR			0x0010
+#define ATMEL_SPI_IER			0x0014
+#define ATMEL_SPI_IDR			0x0018
+#define ATMEL_SPI_IMR			0x001c
+#define ATMEL_SPI_CSR(x)		(0x0030 + 4 * (x))
+#define ATMEL_SPI_VERSION		0x00fc
+
+/* Bits in CR */
+#define ATMEL_SPI_CR_SPIEN		(1 << 0)
+#define ATMEL_SPI_CR_SPIDIS		(1 << 1)
+#define ATMEL_SPI_CR_SWRST		(1 << 7)
+#define ATMEL_SPI_CR_LASTXFER		(1 << 24)
+
+/* Bits in MR */
+#define ATMEL_SPI_MR_MSTR		(1 << 0)
+#define ATMEL_SPI_MR_PS			(1 << 1)
+#define ATMEL_SPI_MR_PCSDEC		(1 << 2)
+#define ATMEL_SPI_MR_FDIV		(1 << 3)
+#define ATMEL_SPI_MR_MODFDIS		(1 << 4)
+#define ATMEL_SPI_MR_LLB		(1 << 7)
+#define ATMEL_SPI_MR_PCS(x)		(((x) & 15) << 16)
+#define ATMEL_SPI_MR_DLYBCS(x)		((x) << 24)
+
+/* Bits in RDR */
+#define ATMEL_SPI_RDR_RD(x)		(x)
+#define ATMEL_SPI_RDR_PCS(x)		((x) << 16)
+
+/* Bits in TDR */
+#define ATMEL_SPI_TDR_TD(x)		(x)
+#define ATMEL_SPI_TDR_PCS(x)		((x) << 16)
+#define ATMEL_SPI_TDR_LASTXFER		(1 << 24)
+
+/* Bits in SR/IER/IDR/IMR */
+#define ATMEL_SPI_SR_RDRF		(1 << 0)
+#define ATMEL_SPI_SR_TDRE		(1 << 1)
+#define ATMEL_SPI_SR_MODF		(1 << 2)
+#define ATMEL_SPI_SR_OVRES		(1 << 3)
+#define ATMEL_SPI_SR_ENDRX		(1 << 4)
+#define ATMEL_SPI_SR_ENDTX		(1 << 5)
+#define ATMEL_SPI_SR_RXBUFF		(1 << 6)
+#define ATMEL_SPI_SR_TXBUFE		(1 << 7)
+#define ATMEL_SPI_SR_NSSR		(1 << 8)
+#define ATMEL_SPI_SR_TXEMPTY		(1 << 9)
+#define ATMEL_SPI_SR_SPIENS		(1 << 16)
+
+/* Bits in CSRx */
+#define ATMEL_SPI_CSRx_CPOL		(1 << 0)
+#define ATMEL_SPI_CSRx_NCPHA		(1 << 1)
+#define ATMEL_SPI_CSRx_CSAAT		(1 << 3)
+#define ATMEL_SPI_CSRx_BITS(x)		((x) << 4)
+#define ATMEL_SPI_CSRx_SCBR(x)		((x) << 8)
+#define ATMEL_SPI_CSRx_SCBR_MAX		0xff
+#define ATMEL_SPI_CSRx_DLYBS(x)		((x) << 16)
+#define ATMEL_SPI_CSRx_DLYBCT(x)	((x) << 24)
+
+/* Bits in VERSION */
+#define ATMEL_SPI_VERSION_REV(x)	((x) << 0)
+#define ATMEL_SPI_VERSION_MFN(x)	((x) << 16)
+
+/* Constants for CSRx:BITS */
+#define ATMEL_SPI_BITS_8		0
+#define ATMEL_SPI_BITS_9		1
+#define ATMEL_SPI_BITS_10		2
+#define ATMEL_SPI_BITS_11		3
+#define ATMEL_SPI_BITS_12		4
+#define ATMEL_SPI_BITS_13		5
+#define ATMEL_SPI_BITS_14		6
+#define ATMEL_SPI_BITS_15		7
+#define ATMEL_SPI_BITS_16		8
+
+/* Register access macros */
+#define spi_readl(reg)						\
+	readl((void *)ATMEL_SPI_BASE + ATMEL_SPI_##reg)
+#define spi_writel(reg, value)					\
+	writel(value, (void *)ATMEL_SPI_BASE + ATMEL_SPI_##reg)
-- 
1.5.3.4





More information about the U-Boot mailing list