ft2232_spi.c: Add support for kt-link jtag interface

Change tested writing, reading and erasing spi flashes
pinout:
  jtag - spi
 1 vcc - vcc, wp#, hold#
 4 gnd - gnd
 5 tdi - si
 7 tms - cs#
 9 tck - sck
13 tdo - so
Connect pins 9 and 12 in EXT connector for 3.3V power.

Signed-off-by: Jacek Naglak <jnaglak@tlen.pl>
Change-Id: Id58c675bc410ec3ef6d58603d13efc9ca53bb87c
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/64440
Original-Reviewed-by: Nico Huber <nico.h@gmx.de>
Original-Reviewed-by: Felix Singer <felixsinger@posteo.net>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/71469
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/flashrom.8.tmpl b/flashrom.8.tmpl
index 62c6ce7..155645d 100644
--- a/flashrom.8.tmpl
+++ b/flashrom.8.tmpl
@@ -792,8 +792,8 @@
 This module supports various programmers based on FTDI FT2232/FT4232H/FT232H chips including the DLP Design
 DLP-USB1232H, openbiosprog-spi, Amontec JTAGkey/JTAGkey-tiny/JTAGkey-2, Dangerous Prototypes Bus Blaster,
 Olimex ARM-USB-TINY/-H, Olimex ARM-USB-OCD/-H, OpenMoko Neo1973 Debug board (V2+), TIAO/DIYGADGET USB
-Multi-Protocol Adapter (TUMPA), TUMPA Lite, GOEPEL PicoTAP, Google Servo v1/v2 and Tin Can Tools
-Flyswatter/Flyswatter 2.
+Multi-Protocol Adapter (TUMPA), TUMPA Lite, GOEPEL PicoTAP, Google Servo v1/v2, Tin Can Tools
+Flyswatter/Flyswatter 2 and Kristech KT-LINK.
 .sp
 An optional parameter specifies the controller
 type, channel/interface/port it should support. For that you have to use the
@@ -806,7 +806,7 @@
 .BR 2232H ", " 4232H ", " 232H ", " jtagkey ", " busblaster ", " openmoko ", " \
 arm-usb-tiny ", " arm-usb-tiny-h ", " arm-usb-ocd ", " arm-usb-ocd-h \
 ", " tumpa ", " tumpalite ", " picotap ", " google-servo ", " google-servo-v2 \
-" or " google-servo-v2-legacy
+", " google-servo-v2-legacy " or " kt-link
 .B interface
 can be
 .BR A ", " B ", " C ", or " D .
diff --git a/ft2232_spi.c b/ft2232_spi.c
index d5d31d5..083bc55 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -41,6 +41,7 @@
 #define FTDI_FT4233H_PID	0x6041
 #define TIAO_TUMPA_PID		0x8a98
 #define TIAO_TUMPA_LITE_PID	0x8a99
+#define KT_LINK_PID		0xbbe2
 #define AMONTEC_JTAGKEY_PID	0xCFF8
 
 #define GOEPEL_VID		0x096C
@@ -67,6 +68,7 @@
 	{FTDI_VID, FTDI_FT4233H_PID, OK, "FTDI", "FT4233H"},
 	{FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
 	{FTDI_VID, TIAO_TUMPA_LITE_PID, OK, "TIAO", "USB Multi-Protocol Adapter Lite"},
+	{FTDI_VID, KT_LINK_PID, OK, "Kristech", "KT-LINK"},
 	{FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
 	{GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
 	{GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
@@ -327,6 +329,8 @@
 	uint8_t cs_bits = 0x08;
 	uint8_t aux_bits = 0x00;
 	uint8_t pindir = 0x0b;
+	uint8_t aux_bits_high = 0x00;
+	uint8_t pindir_high = 0x00;
 
 	struct ft2232_data *const spi_data = calloc(1, sizeof(*spi_data));
 	if (!spi_data) {
@@ -419,6 +423,17 @@
 			/* Flyswatter and Flyswatter-2 require GPIO bits 0x80
 			 * and 0x40 to be driven low to enable output buffers */
 			pindir = 0xcb;
+		} else if (!strcasecmp(arg, "kt-link")) {
+			ft2232_type = KT_LINK_PID;
+			/* port B is used as uart */
+			channel_count = 1;
+			/* Set GPIOL1 output high - route TMS and TDO through multiplexers */
+			aux_bits = 0x20;
+			pindir = 0x2b;
+			/* Set GPIOH4 output low - enable TMS output buffer */
+			/* Set GPIOH5 output low - enable TDI output buffer */
+			/* Set GPIOH6 output low - enable TCK output buffer */
+			pindir_high = 0x70;
 		} else {
 			msg_perr("Error: Invalid device type specified.\n");
 			free(arg);
@@ -664,6 +679,17 @@
 		goto ftdi_err;
 	}
 
+	if (pindir_high) {
+		msg_pdbg("Set data bits HighByte\n");
+		buf[0] = SET_BITS_HIGH;
+		buf[1] = aux_bits_high;
+		buf[2] = pindir_high;
+		if (send_buf(ftdic, buf, 3)) {
+			ret = -8;
+			goto ftdi_err;
+		}
+	}
+
 	spi_data->cs_bits = cs_bits;
 	spi_data->aux_bits = aux_bits;
 	spi_data->pindir = pindir;
diff --git a/util/z60_flashrom.rules b/util/z60_flashrom.rules
index 59de68b..9fd3330 100644
--- a/util/z60_flashrom.rules
+++ b/util/z60_flashrom.rules
@@ -62,6 +62,10 @@
 ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="5002", MODE="664", GROUP="plugdev"
 ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="5003", MODE="664", GROUP="plugdev"
 
+# Kristech KT-LINK
+# https://kristech.pl/files/KT-LINK-UM-ENG.pdf
+ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bbe2", MODE="664", GROUP="plugdev"
+
 # Olimex ARM-USB-OCD
 # http://olimex.com/dev/arm-usb-ocd.html
 ATTRS{idVendor}=="15ba", ATTRS{idProduct}=="0003", MODE="664", GROUP="plugdev"