meta-bletchley: add usb-controller-update tool

Add usb-controller-update script for firmware updating.

Tested results:

root@bletchley:~# systemctl start flash-usb-controller@3.service
root@bletchley:~# systemctl status flash-usb-controller@3.service
○ flash-usb-controller@3.service - Flash usb controller image on SLED 3
     Loaded: loaded (/lib/systemd/system/flash-usb-controller@.service; static)
     Active: inactive (dead)

Apr 22 02:52:52 bletchley systemd[1]: Starting Flash usb controller image on SLED 3...
Apr 22 02:52:54 bletchley usb-controller-update[3915]: Bind mtd node success, path: /dev/mtd7
Apr 22 02:53:01 bletchley usb-controller-update[3937]: [293B blob data]
Apr 22 02:53:01 bletchley usb-controller-update[3915]: Erase flash block done
Apr 22 02:53:08 bletchley usb-controller-update[3940]: [438B blob data]
Apr 22 02:53:10 bletchley usb-controller-update[3940]: [293B blob data]
Apr 22 02:53:11 bletchley usb-controller-update[3940]: [293B blob data]
Apr 22 02:53:11 bletchley usb-controller-update[3915]: Write firmware image done
Apr 22 02:53:12 bletchley systemd[1]: flash-usb-controller@3.service: Deactivated successfully.
Apr 22 02:53:12 bletchley systemd[1]: Finished Flash usb controller image on SLED 3.

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: Icd26fb3c7585461152c3b929a46dc16675d6c937
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update/flash-usb-controller@.service b/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update/flash-usb-controller@.service
new file mode 100644
index 0000000..88f1628
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update/flash-usb-controller@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Flash usb controller image on SLED %i
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart=/usr/sbin/usb-controller-update %i
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update/usb-controller-update b/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update/usb-controller-update
new file mode 100644
index 0000000..1a18497
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update/usb-controller-update
@@ -0,0 +1,121 @@
+#!/bin/bash
+
+set_gpio()
+{
+    local pin_info
+    read -r -a pin_info < <(gpiofind "$1")
+    gpioset "${pin_info[0]}" "${pin_info[1]}"="$2"
+}
+
+bind_flash()
+{
+    local sled_index=$1
+
+    if [ "$sled_index" -eq 1 ]; then
+        set_gpio SEL_SPI2_MUX 0
+        set_gpio SPI2_MUX1 0
+        set_gpio SPI2_MUX2 1
+        set_gpio SPI2_MUX3 1
+    elif [ "$sled_index" -eq 2 ]; then
+        set_gpio SEL_SPI2_MUX 1
+        set_gpio SPI2_MUX1 0
+        set_gpio SPI2_MUX2 1
+        set_gpio SPI2_MUX3 1
+    elif [ "$sled_index" -eq 3 ]; then
+        set_gpio SEL_SPI2_MUX 0
+        set_gpio SPI2_MUX1 1
+        set_gpio SPI2_MUX2 0
+        set_gpio SPI2_MUX3 1
+    elif [ "$sled_index" -eq 4 ]; then
+        set_gpio SEL_SPI2_MUX 1
+        set_gpio SPI2_MUX1 1
+        set_gpio SPI2_MUX2 0
+        set_gpio SPI2_MUX3 1
+    elif [ "$sled_index" -eq 5 ]; then
+        set_gpio SEL_SPI2_MUX 0
+        set_gpio SPI2_MUX1 1
+        set_gpio SPI2_MUX2 1
+        set_gpio SPI2_MUX3 0
+    elif [ "$sled_index" -eq 6 ]; then
+        set_gpio SEL_SPI2_MUX 1
+        set_gpio SPI2_MUX1 1
+        set_gpio SPI2_MUX2 1
+        set_gpio SPI2_MUX3 0
+    else
+        echo "Error: Invalid sled index $sled_index"
+        return 1
+    fi
+
+    if ! echo -n 1e631000.spi > /sys/bus/platform/drivers/aspeed-smc/bind; then
+        echo "Error: flash bind failed"
+        return 1
+    fi
+    return 0
+}
+
+unbind_flash()
+{
+    echo -n 1e631000.spi > /sys/bus/platform/drivers/aspeed-smc/unbind
+    set_gpio SEL_SPI2_MUX 1
+    set_gpio SPI2_MUX1 1
+    set_gpio SPI2_MUX2 1
+    set_gpio SPI2_MUX3 1
+    return 0
+}
+
+findmtd()
+{
+    m=$(grep -xl "$1" /sys/class/mtd/*/name)
+    m=${m%/name}
+    m=${m##*/}
+    echo "$m"
+}
+
+error_cleanup()
+{
+    unbind_flash
+    exit 1
+}
+
+
+SLED_INDEX=$1
+IMAGE_FILE="/run/initramfs/image-usb"
+MTD_NAME="pnor"
+FLASH_CP_CMD="/usr/sbin/flashcp"
+FLASH_ERASEALL_CMD="/usr/sbin/flash_eraseall"
+
+
+if ! bind_flash "$SLED_INDEX"; then
+    error_cleanup
+fi
+
+sleep 1
+
+pnormtd="$(findmtd $MTD_NAME)"
+if test -z "$pnormtd"; then
+    echo "Error: can not find pnor mtd device"
+    error_cleanup
+fi
+
+sleep 1
+
+MTD_NODE_PATH="/dev/$pnormtd"
+if [ ! -c  "$MTD_NODE_PATH" ]; then
+    echo "Error: /dev/$pnormtd device node not exist"
+    error_cleanup
+fi
+
+echo "Bind mtd node success, path: $MTD_NODE_PATH"
+sleep 1
+
+$FLASH_ERASEALL_CMD "$MTD_NODE_PATH" || error_cleanup
+echo "Erase flash block done"
+sleep 1
+
+$FLASH_CP_CMD -v $IMAGE_FILE "$MTD_NODE_PATH" || error_cleanup
+echo "Write firmware image done"
+sync
+sleep 1
+
+unbind_flash
+exit 0
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update_0.1.bb
new file mode 100644
index 0000000..91fdc88
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/fw-update/fw-update_0.1.bb
@@ -0,0 +1,23 @@
+SUMMARY = "Firmware update tools"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit obmc-phosphor-systemd
+
+RDEPENDS:${PN} += "bash"
+RDEPENDS:${PN} += "libgpiod-tools"
+
+S = "${WORKDIR}"
+SRC_URI += " \
+    file://usb-controller-update \
+"
+
+do_install() {
+        install -d ${D}${sbindir}
+        install -m 0755 ${WORKDIR}/usb-controller-update ${D}${sbindir}
+}
+
+FLASH_USB_CONTROLLER_INSTFMT="flash-usb-controller@.service:flash-usb-controller@{0}.service"
+SYSTEMD_SERVICE:${PN} += "flash-usb-controller@.service"
+SYSTEMD_LINK:${PN} += "${@compose_list(d, 'FLASH_USB_CONTROLLER_INSTFMT', 'OBMC_HOST_INSTANCES')}"
diff --git a/meta-facebook/meta-bletchley/recipes-phosphor/images/fb-bletchley-phosphor-image.inc b/meta-facebook/meta-bletchley/recipes-phosphor/images/fb-bletchley-phosphor-image.inc
index 721861e..7929a0a 100644
--- a/meta-facebook/meta-bletchley/recipes-phosphor/images/fb-bletchley-phosphor-image.inc
+++ b/meta-facebook/meta-bletchley/recipes-phosphor/images/fb-bletchley-phosphor-image.inc
@@ -5,4 +5,5 @@
     phosphor-virtual-sensor \
     usbutils \
     idevicerestore \
+    fw-update \
 "