diff mbox series

Do not abuse cs_bit

Message ID 20181122234705.30290-1-vicencb@gmail.com
State New
Headers show
Series Do not abuse cs_bit | expand

Commit Message

Vicente Bergas Nov. 22, 2018, 11:47 p.m. UTC
"cs_bit[s]" variable stores the bit location for Chip Select, as implied by the name.
A new variable "cfg" is added for static configuration bits, like Output Enables.
If outputs are disabled during operation, then the result will depend on the
target having proper pull-up resistors, which is not always guaranteed.

Signed-off-by: Vicente Bergas <vicencb@gmail.com>
---
 ft2232_spi.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/ft2232_spi.c b/ft2232_spi.c
index 95584aa..fe7fcf8 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -84,18 +84,19 @@  const struct dev_entry devs_ft2232spi[] = {
 #define BITMODE_BITBANG_NORMAL	1
 #define BITMODE_BITBANG_SPI	2
 
-/* The variables cs_bits and pindir store the values for the "set data bits low byte" MPSSE command that
+/* The variables cs_bit, cfg and pindir store the values for the "set data bits low byte" MPSSE command that
  * sets the initial state and the direction of the I/O pins. The pin offsets are as follows:
  * SCK is bit 0.
  * DO  is bit 1.
  * DI  is bit 2.
- * CS  is bit 3.
+ * CS  is at cs_bit. Default is bit 3.
  *
  * The default values (set below) are used for most devices:
  *  value: 0x08  CS=high, DI=low, DO=low, SK=low
  *    dir: 0x0b  CS=output, DI=input, DO=output, SK=output
  */
-static uint8_t cs_bits = 0x08;
+static uint8_t cs_bit = 0x08;
+static uint8_t cfg = 0x00;
 static uint8_t pindir = 0x0b;
 static struct ftdi_context ftdic_context;
 
@@ -209,10 +210,9 @@  int ft2232_spi_init(void)
 			ft2232_type = AMONTEC_JTAGKEY_PID;
 			channel_count = 2;
 			/* JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
-			*  value: 0x18  OE=high, CS=high, DI=low, DO=low, SK=low
-			*    dir: 0x1b  OE=output, CS=output, DI=input, DO=output, SK=output */
-			cs_bits = 0x18;
-			pindir = 0x1b;
+			*  value: 0x08  #OE=low, CS=high, DI=low, DO=low, SK=low
+			*    dir: 0x1b  #OE=output, CS=output, DI=input, DO=output, SK=output */
+			pindir |= 0x10;
 		} else if (!strcasecmp(arg, "picotap")) {
 			ft2232_vid = GOEPEL_VID;
 			ft2232_type = GOEPEL_PICOTAP_PID;
@@ -229,8 +229,7 @@  int ft2232_spi_init(void)
 			/* In its default configuration it is a jtagkey clone */
 			ft2232_type = FTDI_FT2232H_PID;
 			channel_count = 2;
-			cs_bits = 0x18;
-			pindir = 0x1b;
+			pindir |= 0x10;
 		} else if (!strcasecmp(arg, "openmoko")) {
 			ft2232_vid = FIC_VID;
 			ft2232_type = OPENMOKO_DBGBOARD_PID;
@@ -242,8 +241,7 @@  int ft2232_spi_init(void)
 			/* arm-usb-ocd(-h) has an output buffer that needs to be enabled by pulling ADBUS4 low.
 			*  value: 0x08  #OE=low, CS=high, DI=low, DO=low, SK=low
 			*    dir: 0x1b  #OE=output, CS=output, DI=input, DO=output, SK=output */
-			cs_bits = 0x08;
-			pindir = 0x1b;
+			pindir |= 0x10;
 		} else if (!strcasecmp(arg, "arm-usb-tiny")) {
 			ft2232_vid = OLIMEX_VID;
 			ft2232_type = OLIMEX_ARM_TINY_PID;
@@ -253,8 +251,7 @@  int ft2232_spi_init(void)
 			ft2232_type = OLIMEX_ARM_OCD_H_PID;
 			channel_count = 2;
 			/* See arm-usb-ocd */
-			cs_bits = 0x08;
-			pindir = 0x1b;
+			pindir |= 0x10;
 		} else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
 			ft2232_vid = OLIMEX_VID;
 			ft2232_type = OLIMEX_ARM_TINY_H_PID;
@@ -338,8 +335,8 @@  int ft2232_spi_init(void)
 			return -2;
 		} else {
 			unsigned int pin = temp + 4;
-			cs_bits |= 1 << pin;
-			pindir |= 1 << pin;
+			cs_bit = 1 << pin;
+			pindir = (pindir & ~0x08) | cs_bit;
 		}
 	}
 	free(arg);
@@ -425,7 +422,7 @@  int ft2232_spi_init(void)
 
 	msg_pdbg("Set data bits\n");
 	buf[0] = SET_BITS_LOW;
-	buf[1] = cs_bits;
+	buf[1] = cfg | cs_bit;
 	buf[2] = pindir;
 	if (send_buf(ftdic, buf, 3)) {
 		ret = -8;
@@ -480,7 +477,7 @@  static int ft2232_spi_send_command(struct flashctx *flash,
 	 */
 	msg_pspew("Assert CS#\n");
 	buf[i++] = SET_BITS_LOW;
-	buf[i++] = 0 & ~cs_bits; /* assertive */
+	buf[i++] = cfg & ~cs_bit;
 	buf[i++] = pindir;
 
 	if (writecnt) {
@@ -521,7 +518,7 @@  static int ft2232_spi_send_command(struct flashctx *flash,
 
 	msg_pspew("De-assert CS#\n");
 	buf[i++] = SET_BITS_LOW;
-	buf[i++] = cs_bits;
+	buf[i++] = cfg | cs_bit;
 	buf[i++] = pindir;
 	ret = send_buf(ftdic, buf, i);
 	failed |= ret;