meta-bletchley: add usbmux utility

There is a issue caused by switching USBMUX GPIO pin slowly.
For example, when user set USBMUX GPIO pin to sled1, the USBMUX will
first switch to sled3 and then switch to sled1 if there is a long
interval between each gpioset command.

1. USBMUX is set to off initially
    USB2_SEL0_A = 1
    USB2_SEL1_A = 1
    USB2_SEL0_B = 1
    USB2_SEL1_B = 1

2. Set USBMUX to sled1
    USB2_SEL0_A = 0
    USB2_SEL1_A = 0
    USB2_SEL0_B = 1
    USB2_SEL1_B = 1

```
root@bletchley:~# gpioset $(gpiofind USB2_SEL0_A)=0  --> USBMUX set to sled3 first.
root@bletchley:~# gpioset $(gpiofind USB2_SEL1_A)=0  --> then USBMUX set to sled1.
```

Add bletchley-usbmux-util to set USBMUX GPIO pins to selected sled, and
avoid USB issues with slow mux switching.

Change-Id: Id2a40b5908c1286929648cc433cb426a9c493e3a
Signed-off-by: Potin Lai <potin.lai@quantatw.com>
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
index 081d740..b28711e 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
@@ -10,12 +10,16 @@
     file://bletchley-system-state-init \
     file://bletchley-system-state-init@.service \
     file://bletchley-switch-diag \
+    file://bletchley-usbmux-util \
     "
 
 do_install() {
     install -d ${D}${libexecdir}
     install -m 0755 ${WORKDIR}/bletchley-system-state-init ${D}${libexecdir}
     install -m 0755 ${WORKDIR}/bletchley-switch-diag ${D}${libexecdir}
+
+    install -d ${D}${bindir}
+    install -m 0755 ${WORKDIR}/bletchley-usbmux-util ${D}${bindir}
 }
 
 
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-usbmux-util b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-usbmux-util
new file mode 100644
index 0000000..5731d4f
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-usbmux-util
@@ -0,0 +1,122 @@
+#!/bin/bash
+
+CMD=$1
+SLED_INDEX=$2
+
+CHIP_NUM=0
+USB2_SEL0_A_PIN_OFFSET=0
+USB2_SEL1_A_PIN_OFFSET=0
+USB2_SEL0_B_PIN_OFFSET=0
+USB2_SEL1_B_PIN_OFFSET=0
+
+init_gpio_pin_info()
+{
+    local pin_info
+
+    read -r -a pin_info < <(gpiofind USB2_SEL0_A)
+    CHIP_NUM="${pin_info[0]}"
+    USB2_SEL0_A_PIN_OFFSET="${pin_info[1]}"
+    read -r -a pin_info < <(gpiofind USB2_SEL1_A)
+    USB2_SEL1_A_PIN_OFFSET="${pin_info[1]}"
+    read -r -a pin_info < <(gpiofind USB2_SEL0_B)
+    USB2_SEL0_B_PIN_OFFSET="${pin_info[1]}"
+    read -r -a pin_info < <(gpiofind USB2_SEL1_B)
+    USB2_SEL1_B_PIN_OFFSET="${pin_info[1]}"
+}
+
+set_usbmux_gpio()
+{
+    gpioset "$CHIP_NUM" \
+    "$USB2_SEL0_A_PIN_OFFSET"="$1" \
+    "$USB2_SEL1_A_PIN_OFFSET"="$2" \
+    "$USB2_SEL0_B_PIN_OFFSET"="$3" \
+    "$USB2_SEL1_B_PIN_OFFSET"="$4"
+}
+
+print_help()
+{
+    echo "Usage:"
+    echo "    bletchley-usbmux-util off"
+    echo "    bletchley-usbmux-util on <SLED_INDEX>"
+    echo ""
+    echo "SLED_INDEX: 1 - 6"
+    echo ""
+}
+
+usb_mux_off()
+{
+    set_usbmux_gpio 1 1 1 1
+}
+
+usb_mux_sled1()
+{
+    usb_mux_off
+    sleep 2
+    set_usbmux_gpio 0 0 1 1
+}
+
+usb_mux_sled2()
+{
+    usb_mux_off
+    sleep 2
+    set_usbmux_gpio 1 0 1 1
+}
+
+usb_mux_sled3()
+{
+    usb_mux_off
+    sleep 2
+    set_usbmux_gpio 0 1 1 1
+}
+
+usb_mux_sled4()
+{
+    usb_mux_off
+    sleep 2
+    set_usbmux_gpio 1 1 0 0
+}
+
+usb_mux_sled5()
+{
+    usb_mux_off
+    sleep 2
+    set_usbmux_gpio 1 1 1 0
+}
+
+usb_mux_sled6()
+{
+    usb_mux_off
+    sleep 2
+    set_usbmux_gpio 1 1 0 1
+}
+
+init_gpio_pin_info
+
+if [ "$CMD" == "off" ]; then
+    usb_mux_off
+elif [ "$CMD" == "on" ]; then
+    if [ "$SLED_INDEX" -eq 1 ]; then
+        usb_mux_sled1
+    elif [ "$SLED_INDEX" -eq 2 ]; then
+        usb_mux_sled2
+    elif [ "$SLED_INDEX" -eq 3 ]; then
+        usb_mux_sled3
+    elif [ "$SLED_INDEX" -eq 4 ]; then
+        usb_mux_sled4
+    elif [ "$SLED_INDEX" -eq 5 ]; then
+        usb_mux_sled5
+    elif [ "$SLED_INDEX" -eq 6 ]; then
+        usb_mux_sled6
+    else
+        echo "Invalid SLED index: $SLED_INDEX"
+        print_help
+        exit 1
+    fi
+    sleep 1
+else
+    echo "Invalid command: $CMD"
+    print_help
+    exit 1
+fi
+
+exit 0