meta-ampere: mtjade: Refactor obmc-console setup

This commit refactors obmc-console setup for mtjade

Tested:
1. Successfully connects to host consoles via ssh sol
   $ ssh root@${bmc} -p 220*
2. Successfully connects host consoles via ipmi sol
   $ ipmitool -I lanplus -H ${bmc} -U root -P 0penBmc \
     -C 17 sol activate
3. All the host console logs are in /var/log/

Signed-off-by: Chau Ly <chaul@amperecomputing.com>
Change-Id: Ie01361139644f5836646d1d98934cda52828ec70
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init.bb b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init.bb
index aaeb33f..c209295 100644
--- a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init.bb
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init.bb
@@ -14,6 +14,8 @@
 
 SRC_URI = " \
     file://ampere_platform_init.sh \
+    file://ampere_uart_console_setup.sh \
+    file://ampere_uartmux_ctrl.sh \
     file://ampere-platform-init.service \
     "
 
@@ -23,6 +25,8 @@
 do_install () {
     install -d ${D}${sbindir}
     install -m 0755 ${WORKDIR}/ampere_platform_init.sh ${D}${sbindir}/
+    install -m 0755 ${WORKDIR}/ampere_uart_console_setup.sh ${D}${sbindir}/
+    install -m 0755 ${WORKDIR}/ampere_uartmux_ctrl.sh ${D}/${sbindir}/
     install -d ${D}${systemd_unitdir}/system/
     install -m 0644 ${WORKDIR}/ampere-platform-init.service ${D}${systemd_unitdir}/system
 }
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_platform_init.sh b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_platform_init.sh
index 861b105..0a14259 100644
--- a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_platform_init.sh
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_platform_init.sh
@@ -4,6 +4,7 @@
 source /usr/sbin/gpio-lib.sh
 # shellcheck source=meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/gpio-defs.sh
 source /usr/sbin/gpio-defs.sh
+source /usr/sbin/ampere_uart_console_setup.sh
 
 # Configure to boot from MAIN SPI-HOST
 gpio_configure_output "$SPI0_BACKUP_SEL" 0
@@ -45,3 +46,6 @@
 fi
 
 gpio_configure_output "$BMC_READY" 1
+# =======================================================
+# Setting uart muxes to BMC as default
+uart_console_setup
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_uart_console_setup.sh b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_uart_console_setup.sh
new file mode 100644
index 0000000..fe8d203
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_uart_console_setup.sh
@@ -0,0 +1,57 @@
+#!/bin/sh -e
+#
+# Copyright (c) 2020 Ampere Computing LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#	http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# shellcheck disable=SC2039
+# shellcheck disable=SC2112
+# shellcheck disable=SC3010
+# shellcheck disable=SC3030
+# shellcheck disable=SC3054
+
+export obmc_console_tty=("ttyS0" "ttyS1" "ttyS2" "ttyS3")
+
+function get_uart_port()
+{
+   tty=$1
+   case "${tty}" in
+   "ttyS0") uart=1
+   ;;
+   "ttyS1") uart=2
+   ;;
+   "ttyS2") uart=3
+   ;;
+   "ttyS3") uart=4
+   ;;
+   *) echo "Invalid tty passed to $0. Exiting!"
+      exit 1;
+   ;;
+   esac
+   echo $uart
+}
+
+function uart_console_setup()
+{
+   # Default the host routing through the mux to use the BMC (2)
+   # This allows the SoL console in webui, and the ssh port 2200, to work
+   # upon startup. If UART transcievers are installed on the header and required,
+   # this value should be set to 1
+   for tty in "${obmc_console_tty[@]}"; do
+      uart=$(get_uart_port "$tty")
+      if [ "${uart}" -ne 0 ]
+      then
+         /usr/sbin/ampere_uartmux_ctrl.sh "${uart}" 2
+      fi
+   done
+}
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_uartmux_ctrl.sh b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_uartmux_ctrl.sh
new file mode 100755
index 0000000..f285898
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-platform-init/ampere_uartmux_ctrl.sh
@@ -0,0 +1,73 @@
+#!/bin/bash
+#
+# Copyright (c) 2021 Ampere Computing LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#	http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Ampere Computing LLC: UART MUX/DEMUX for CPU0 UART0,1,4 and CPU1 UART1
+# Usage: ampere_uartmux_ctrl.sh <CPU UART port number> <UARTx_MODE>
+#        <UARTx_MODE> of 1 sets CPU To HDR_CONN
+#        <UARTx_MODE> of 2 sets BMC to CPU (eg dropbear ssh server on port 2200)
+
+# shellcheck source=/dev/null
+source /usr/sbin/gpio-lib.sh
+
+if [ $# -lt 2 ]; then
+	exit 1
+fi
+
+case "$1" in
+	1) GPIO_UARTx_MODE0=56
+	   # CPU0 UART0 connects to BMC UART1
+	   CONSOLE_PORT=0
+	;;
+	2) GPIO_UARTx_MODE0=57
+	   # CPU0 UART1 connects to BMC UART2
+	   CONSOLE_PORT=1
+	;;
+	3) GPIO_UARTx_MODE0=58
+	   # CPU0 UART4 connects to BMC UART3
+	   CONSOLE_PORT=2
+	;;
+	4) GPIO_UARTx_MODE0=59
+	   # CPU1 UART1 connects to BMC UART4
+	   CONSOLE_PORT=3
+	;;
+	*) echo "Invalid UART port selection"
+	   exit 1
+	;;
+esac
+
+# Only switch the MUX when there is no active connection. This means we only
+# switch the MUX before the first session starts and after the last session
+# closes. We do this by querying number of connected sessions to the socket
+# of requested console port.
+# Example format:  Accepted: 1; Connected: 1;
+CONNECTED=$(systemctl --no-pager status obmc-console-ttyS${CONSOLE_PORT}-ssh.socket | grep -w Connected | cut -d ':' -f 3 | tr -d ' ;')
+if [ ! "$CONNECTED" -le 1 ]; then
+	exit 0
+fi
+
+echo "Ampere UART MUX CTRL UART port $1 to mode $2"
+
+case "$2" in
+	1) gpio_configure_output "${GPIO_UARTx_MODE0}" 0
+	   exit 0
+	;;
+	2) gpio_configure_output "${GPIO_UARTx_MODE0}" 1
+	   exit 0
+	;;
+	*) echo "Invalid UART mode selection"
+	   exit 1
+	;;
+esac