This patch adds support for a new SPI programmer, based on the FT2232H/4232H chip from FTDI

FTDI support is autodetected during compilation.

Paul writes:
There are certainly possible improvements: The code has hard-coded
values for which interface of the ftdi chip to use (interface B was
chosen because libftdi seems to have trouble with A right now), what
clock rate use for the SPI interface (I've been running at 30Mhz, but
the patch sets it to 10Mhz), and possibly others. I think this means
that per-programmer options might be a good idea at some point.

Carl-Daniel writes:
There is one additional FIXME comment in the code, but AFAICS that
problem is not solvable with current libftdi.

Corresponding to flashrom svn r598.

Signed-off-by: Paul Fox <pgf@laptop.org>
Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Uwe Hermann <uwe@hermann-uwe.de>
diff --git a/Makefile b/Makefile
index 00a3d93..39b5577 100644
--- a/Makefile
+++ b/Makefile
@@ -22,6 +22,7 @@
 CC      ?= gcc
 STRIP   = strip
 INSTALL = install
+DIFF    = diff
 PREFIX  ?= /usr/local
 MANDIR  ?= $(PREFIX)/share/man
 CFLAGS  ?= -Os -Wall -Werror
@@ -48,9 +49,9 @@
 	sst49lfxxxc.o sst_fwhub.o layout.o cbtable.o flashchips.o physmap.o \
 	flashrom.o w39v080fa.o sharplhf00l04.o w29ee011.o spi.o it87spi.o \
 	ichspi.o w39v040c.o sb600spi.o wbsio_spi.o m29f002.o internal.o \
-	dummyflasher.o pcidev.o nic3com.o satasii.o
+	dummyflasher.o pcidev.o nic3com.o satasii.o ft2232_spi.o
 
-all: pciutils dep $(PROGRAM)
+all: pciutils .features dep $(PROGRAM)
 
 # Set the flashrom version string from the highest revision number
 # of the checked out flashrom files.
@@ -63,16 +64,19 @@
 SVNDEF := -D'FLASHROM_VERSION="$(VERSION)"'
 
 $(PROGRAM): $(OBJS)
-	$(CC) $(LDFLAGS) -o $(PROGRAM) $(OBJS) $(LIBS)
+	$(CC) $(LDFLAGS) -o $(PROGRAM) $(OBJS) $(LIBS) $(FEATURE_LIBS)
 
 flashrom.o: flashrom.c
-	$(CC) $(CFLAGS) $(CPPFLAGS)  -c -o $@ $< $(SVNDEF)
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(FEATURE_CFLAGS) -c -o $@ $< $(SVNDEF)
+
+%.o: %.c .features
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(FEATURE_CFLAGS) -c $< -o $@
 
 clean:
 	rm -f $(PROGRAM) *.o
 
 distclean: clean
-	rm -f .dependencies
+	rm -f .dependencies .features
 
 dep:
 	@$(CC) $(CPPFLAGS) $(SVNDEF) -MM *.c > .dependencies
@@ -81,7 +85,7 @@
 	$(STRIP) $(STRIP_ARGS) $(PROGRAM)
 
 compiler:
-	@echo; printf "Checking for a C compiler... "
+	@printf "Checking for a C compiler... "
 	@$(shell ( echo "int main(int argc, char **argv)"; \
 		   echo "{ return 0; }"; ) > .test.c )
 	@$(CC) $(CFLAGS) $(LDFLAGS) .test.c -o .test >/dev/null &&	\
@@ -89,8 +93,10 @@
 		rm -f .test.c .test; exit 1)
 	@rm -f .test.c .test
 
-pciutils: compiler
-	@echo; printf "Checking for pciutils and zlib... "
+# We don't specify compiler as requirement because the compiler is already
+# checked during makefile remake through .features
+pciutils:
+	@printf "Checking for pciutils and zlib... "
 	@$(shell ( echo "#include <pci/pci.h>";		   \
 		   echo "struct pci_access *pacc;";	   \
 		   echo "int main(int argc, char **argv)"; \
@@ -102,6 +108,19 @@
 		rm -f .test.c .test; exit 1)
 	@rm -f .test.c .test
 
+.features: compiler
+	@printf "Checking for FTDI support... "
+	@$(shell ( echo "#include <ftdi.h>";		   \
+		   echo "struct ftdi_context *ftdic = NULL;";	   \
+		   echo "int main(int argc, char **argv)"; \
+		   echo "{ return ftdi_init(ftdic); }"; ) > .featuretest.c )
+	@$(CC) $(CFLAGS) $(LDFLAGS) .featuretest.c -o .featuretest $(LIBS) -lftdi >/dev/null 2>&1 &&	\
+		( echo "found."; echo FEATURE_CFLAGS := -D'FT2232_SPI_SUPPORT=1' > .features.tmp;	\
+		  echo FEATURE_LIBS := -lftdi >> .features.tmp) ||	\
+		( echo "not found."; echo "" > .features.tmp )
+	@$(DIFF) -q .features.tmp .features >/dev/null 2>&1 && rm .features.tmp || mv .features.tmp .features
+	@rm -f .featuretest.c .featuretest
+
 install: $(PROGRAM)
 	mkdir -p $(DESTDIR)$(PREFIX)/sbin
 	mkdir -p $(DESTDIR)$(MANDIR)/man8
@@ -122,3 +141,4 @@
 .PHONY: all clean distclean dep compiler pciutils export tarball
 
 -include .dependencies
+-include .features