[U-Boot] [PATCH 04/11] powerpc/ppc4xx: Add bitbang i2c interface for gdsys boards

Dirk Eibach eibach at gdsys.de
Mon Apr 22 13:16:05 CEST 2013


This adds an i2c bitbang interface that can coexist with processor i2c.

Signed-off-by: Dirk Eibach <eibach at gdsys.de>
---
 board/gdsys/common/Makefile |    2 +-
 board/gdsys/common/bb_i2c.c |  304 +++++++++++++++++++++++++++++++++++++++++++
 board/gdsys/common/bb_i2c.h |   76 +++++++++++
 3 files changed, 381 insertions(+), 1 deletion(-)
 create mode 100644 board/gdsys/common/bb_i2c.c
 create mode 100644 board/gdsys/common/bb_i2c.h

diff --git a/board/gdsys/common/Makefile b/board/gdsys/common/Makefile
index aa15c15..b217d97 100644
--- a/board/gdsys/common/Makefile
+++ b/board/gdsys/common/Makefile
@@ -31,7 +31,7 @@ LIB	= $(obj)lib$(VENDOR).o
 
 COBJS-$(CONFIG_IO) += miiphybb.o
 COBJS-$(CONFIG_IO64) += miiphybb.o
-COBJS-$(CONFIG_IOCON) += osd.o mclink.o cmd_fpga.o
+COBJS-$(CONFIG_IOCON) += osd.o mclink.o cmd_fpga.o bb_i2c.o
 COBJS-$(CONFIG_DLVISION_10G) += osd.o
 
 COBJS   := $(COBJS-y)
diff --git a/board/gdsys/common/bb_i2c.c b/board/gdsys/common/bb_i2c.c
new file mode 100644
index 0000000..a816514
--- /dev/null
+++ b/board/gdsys/common/bb_i2c.c
@@ -0,0 +1,304 @@
+/*
+ * (C) Copyright 2012
+ * Dirk Eibach,  Guntermann & Drunck GmbH, eibach at gdsys.de
+ *
+ * based on soft_i2c.c
+ * (C) Copyright 2001, 2002
+ * 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
+ *
+ * This has been changed substantially by Gerald Van Baren, Custom IDEAS,
+ * vanbaren at cideas.com.  It was heavily influenced by LiMon, written by
+ * Neil Russell.
+ */
+
+#include <common.h>
+
+/*-----------------------------------------------------------------------
+ * Definitions
+ */
+
+#define RETRIES		0
+
+#define BB_I2C_ACK	0		/* PD_SDA level to ack a byte */
+#define BB_I2C_NOACK	1		/* PD_SDA level to noack a byte */
+
+/*-----------------------------------------------------------------------
+ * Local functions
+ */
+static void  send_reset(unsigned int bus);
+static void  send_start(unsigned int bus);
+static void  send_stop(unsigned int bus);
+static void  send_ack(unsigned int bus, int ack);
+static int   write_byte(unsigned int bus, uchar byte);
+static uchar read_byte(unsigned int bus, int ack);
+
+/*-----------------------------------------------------------------------
+ * Send a reset sequence consisting of 9 clocks with the data signal high
+ * to clock any confused device back into an idle state.  Also send a
+ * <stop> at the end of the sequence for belts & suspenders.
+ */
+static void send_reset(unsigned int bus)
+{
+	BB_I2C_SOFT_DECLARATIONS(bus)	/* intentional without ';' */
+	int j;
+
+	BB_I2C_SCL(bus, 1);
+	BB_I2C_SDA(bus, 1);
+	BB_I2C_TRISTATE(bus);
+	for (j = 0; j < 9; j++) {
+		BB_I2C_SCL(bus, 0);
+		BB_I2C_DELAY(bus);
+		BB_I2C_DELAY(bus);
+		BB_I2C_SCL(bus, 1);
+		BB_I2C_DELAY(bus);
+		BB_I2C_DELAY(bus);
+	}
+	send_stop(bus);
+	BB_I2C_TRISTATE(bus);
+}
+
+/*-----------------------------------------------------------------------
+ * START: High -> Low on SDA while SCL is High
+ */
+static void send_start(unsigned int bus)
+{
+	BB_I2C_SOFT_DECLARATIONS(bus)	/* intentional without ';' */
+
+	BB_I2C_DELAY(bus);
+	BB_I2C_SDA(bus, 1);
+	BB_I2C_ACTIVE(bus);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SCL(bus, 1);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SDA(bus, 0);
+	BB_I2C_DELAY(bus);
+}
+
+/*-----------------------------------------------------------------------
+ * STOP: Low -> High on SDA while SCL is High
+ */
+static void send_stop(unsigned int bus)
+{
+	BB_I2C_SOFT_DECLARATIONS(bus)	/* intentional without ';' */
+
+	BB_I2C_SCL(bus, 0);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SDA(bus, 0);
+	BB_I2C_ACTIVE(bus);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SCL(bus, 1);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SDA(bus, 1);
+	BB_I2C_DELAY(bus);
+	BB_I2C_TRISTATE(bus);
+}
+
+/*-----------------------------------------------------------------------
+ * ack should be BB_I2C_ACK or BB_I2C_NOACK
+ */
+static void send_ack(unsigned int bus, int ack)
+{
+	BB_I2C_SOFT_DECLARATIONS(bus)	/* intentional without ';' */
+
+	BB_I2C_SCL(bus, 0);
+	BB_I2C_DELAY(bus);
+	BB_I2C_ACTIVE(bus);
+	BB_I2C_SDA(bus, ack);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SCL(bus, 1);
+	BB_I2C_DELAY(bus);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SCL(bus, 0);
+	BB_I2C_DELAY(bus);
+}
+
+/*-----------------------------------------------------------------------
+ * Send 8 bits and look for an acknowledgement.
+ */
+static int write_byte(unsigned int bus, uchar data)
+{
+	BB_I2C_SOFT_DECLARATIONS(bus)	/* intentional without ';' */
+	int j;
+	int nack;
+
+	BB_I2C_ACTIVE(bus);
+	for (j = 0; j < 8; j++) {
+		BB_I2C_SCL(bus, 0);
+		BB_I2C_DELAY(bus);
+		BB_I2C_SDA(bus, data & 0x80);
+		BB_I2C_DELAY(bus);
+		BB_I2C_SCL(bus, 1);
+		BB_I2C_DELAY(bus);
+		BB_I2C_DELAY(bus);
+
+		data <<= 1;
+	}
+
+	/*
+	 * Look for an <ACK>(negative logic) and return it.
+	 */
+	BB_I2C_SCL(bus, 0);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SDA(bus, 1);
+	BB_I2C_TRISTATE(bus);
+	BB_I2C_DELAY(bus);
+	BB_I2C_SCL(bus, 1);
+	BB_I2C_DELAY(bus);
+	BB_I2C_DELAY(bus);
+	nack = BB_I2C_READ(bus);
+	BB_I2C_SCL(bus, 0);
+	BB_I2C_DELAY(bus);
+	BB_I2C_ACTIVE(bus);
+
+	return nack;	/* not a nack is an ack */
+}
+
+/*-----------------------------------------------------------------------
+ * if ack == BB_I2C_ACK, ACK the byte so can continue reading, else
+ * send BB_I2C_NOACK to end the read.
+ */
+static uchar read_byte(unsigned int bus, int ack)
+{
+	BB_I2C_SOFT_DECLARATIONS(bus)	/* intentional without ';' */
+	int  data;
+	int  j;
+
+	/*
+	 * Read 8 bits, MSB first.
+	 */
+	BB_I2C_TRISTATE(bus);
+	BB_I2C_SDA(bus, 1);
+	data = 0;
+	for (j = 0; j < 8; j++) {
+		BB_I2C_SCL(bus, 0);
+		BB_I2C_DELAY(bus);
+		BB_I2C_SCL(bus, 1);
+		BB_I2C_DELAY(bus);
+		data <<= 1;
+		data |= BB_I2C_READ(bus);
+		BB_I2C_DELAY(bus);
+	}
+	send_ack(bus, ack);
+
+	return data;
+}
+
+/*=====================================================================*/
+/*                         Public Functions                            */
+/*=====================================================================*/
+
+/*-----------------------------------------------------------------------
+ * Initialization
+ */
+void bb_i2c_init(unsigned int bus, int speed, int slaveaddr)
+{
+	send_reset(bus);
+}
+
+/*-----------------------------------------------------------------------
+ * Probe to see if a chip is present.  Also good for checking for the
+ * completion of EEPROM writes since the chip stops responding until
+ * the write completes (typically 10mSec).
+ */
+int bb_i2c_probe(unsigned int bus, uchar addr)
+{
+	int rc;
+
+	/*
+	 * perform 1 byte write transaction with just address byte
+	 * (fake write)
+	 */
+	send_start(bus);
+	rc = write_byte(bus, (addr << 1) | 0);
+	send_stop(bus);
+
+	return rc ? 1 : 0;
+}
+
+/*-----------------------------------------------------------------------
+ * Read bytes
+ */
+int bb_i2c_read(unsigned int bus, uchar chip, uint addr, int alen,
+		uchar *buffer, int len)
+{
+	int shift;
+
+	/*
+	 * Do the addressing portion of a write cycle to set the
+	 * chip's address pointer.  If the address length is zero,
+	 * don't do the normal write cycle to set the address pointer,
+	 * there is no address pointer in this chip.
+	 */
+	send_start(bus);
+	if (alen > 0) {
+		if (write_byte(bus, chip << 1)) {	/* write cycle */
+			send_stop(bus);
+			return 1;
+		}
+		shift = (alen-1) * 8;
+		while (alen-- > 0) {
+			if (write_byte(bus, addr >> shift))
+				return 1;
+			shift -= 8;
+		}
+
+		send_stop(bus);
+		send_start(bus);
+	}
+	/*
+	 * Send the chip address again, this time for a read cycle.
+	 * Then read the data.  On the last byte, we do a NACK instead
+	 * of an ACK(len == 0) to terminate the read.
+	 */
+	write_byte(bus, (chip << 1) | 1);	/* read cycle */
+	while (len-- > 0)
+		*buffer++ = read_byte(bus, len == 0);
+	send_stop(bus);
+	return 0;
+}
+
+/*-----------------------------------------------------------------------
+ * Write bytes
+ */
+int  bb_i2c_write(unsigned int bus, uchar chip, uint addr, int alen,
+		  uchar *buffer, int len)
+{
+	int shift, failures = 0;
+
+	send_start(bus);
+	if (write_byte(bus, chip << 1)) {	/* write cycle */
+		send_stop(bus);
+		return 1;
+	}
+	shift = (alen-1) * 8;
+	while (alen-- > 0) {
+		if (write_byte(bus, addr >> shift))
+			return 1;
+		shift -= 8;
+	}
+
+	while (len-- > 0) {
+		if (write_byte(bus, *buffer++))
+			failures++;
+	}
+	send_stop(bus);
+	return failures;
+}
diff --git a/board/gdsys/common/bb_i2c.h b/board/gdsys/common/bb_i2c.h
new file mode 100644
index 0000000..34f4c0f
--- /dev/null
+++ b/board/gdsys/common/bb_i2c.h
@@ -0,0 +1,76 @@
+/*
+ * (C) Copyright 2012
+ * Dirk Eibach,  Guntermann & Drunck GmbH, eibach at gdsys.de
+ *
+ * based on i2c.h
+ * (C) Copyright 2001
+ * Gerald Van Baren, Custom IDEAS, vanbaren at cideas.com.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ * The original I2C interface was
+ *   (C) 2000 by Paolo Scaffardi (arsenio at tin.it)
+ *   AIRVENT SAM s.p.a - RIMINI(ITALY)
+ * but has been changed substantially.
+ */
+
+#ifndef _BB_I2C_H_
+#define _BB_I2C_H_
+
+/*
+ * Probe the given I2C chip address.  Returns 0 if a chip responded,
+ * not 0 on failure.
+ */
+int bb_i2c_probe(unsigned int bus, uchar chip);
+
+/*
+ * Read/Write interface:
+ *   chip:    I2C chip address, range 0..127
+ *   addr:    Memory (register) address within the chip
+ *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
+ *              memories, 0 for register type devices with only one
+ *              register)
+ *   buffer:  Where to read/write the data
+ *   len:     How many bytes to read/write
+ *
+ *   Returns: 0 on success, not 0 on failure
+ */
+int bb_i2c_read(unsigned int bus, uchar chip, uint addr, int alen,
+		uchar *buffer, int len);
+int bb_i2c_write(unsigned int bus, uchar chip, uint addr, int alen,
+		 uchar *buffer, int len);
+
+/*
+ * Utility routines to read/write registers.
+ */
+static inline u8 bb_i2c_reg_read(unsigned int bus, u8 addr, u8 reg)
+{
+	u8 buf;
+
+	bb_i2c_read(bus, addr, reg, 1, &buf, 1);
+
+	return buf;
+}
+
+static inline void bb_i2c_reg_write(unsigned int bus, u8 addr, u8 reg, u8 val)
+{
+	bb_i2c_write(bus, addr, reg, 1, &val, 1);
+}
+
+#endif	/* _BB_I2C_H_ */
-- 
1.7.10.4




More information about the U-Boot mailing list