blob: a464adc8767ff7808e894003d5966d8f52f0c9bc [file] [log] [blame]
Tung Nguyen189431e2020-12-16 08:11:51 +00001#!/bin/bash
2#
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +00003# Copyright (c) 2021 Ampere Computing LLC
Tung Nguyen189431e2020-12-16 08:11:51 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Ampere Computing LLC: UART MUX/DEMUX for CPU0 UART0,1,4 and CPU1 UART1
18# Usage: ampere_uartmux_ctrl.sh <CPU UART port number> <UARTx_MODE>
19# <UARTx_MODE> of 1 sets CPU To HDR_CONN
20# <UARTx_MODE> of 2 sets BMC to CPU (eg dropbear ssh server on port 2200)
21
Chau Ly4255eea2023-04-18 07:11:32 +000022# shellcheck source=meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/gpio-lib.sh
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000023source /usr/sbin/gpio-lib.sh
24
Tung Nguyen189431e2020-12-16 08:11:51 +000025if [ $# -lt 2 ]; then
26 exit 1
27fi
28
Tung Nguyen189431e2020-12-16 08:11:51 +000029case "$1" in
30 1) GPIO_UARTx_MODE0=56
31 # CPU0 UART0 connects to BMC UART1
32 CONSOLE_PORT=0
33 ;;
34 2) GPIO_UARTx_MODE0=57
35 # CPU0 UART1 connects to BMC UART2
36 CONSOLE_PORT=1
37 ;;
38 3) GPIO_UARTx_MODE0=58
39 # CPU0 UART4 connects to BMC UART3
40 CONSOLE_PORT=2
41 ;;
42 4) GPIO_UARTx_MODE0=59
43 # CPU1 UART1 connects to BMC UART4
44 CONSOLE_PORT=3
45 ;;
46 *) echo "Invalid UART port selection"
47 exit 1
48 ;;
49esac
50
51# Only switch the MUX when there is no active connection. This means we only
52# switch the MUX before the first session starts and after the last session
53# closes. We do this by querying number of connected sessions to the socket
54# of requested console port.
55# Example format: Accepted: 1; Connected: 1;
56CONNECTED=$(systemctl --no-pager status obmc-console-ttyS${CONSOLE_PORT}-ssh.socket | grep -w Connected | cut -d ':' -f 3 | tr -d ' ;')
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000057if [ ! "$CONNECTED" -le 1 ]; then
Tung Nguyen189431e2020-12-16 08:11:51 +000058 exit 0
59fi
60
61echo "Ampere UART MUX CTRL UART port $1 to mode $2"
62
63case "$2" in
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000064 1) gpio_configure_output "${GPIO_UARTx_MODE0}" 0
Tung Nguyen189431e2020-12-16 08:11:51 +000065 exit 0
66 ;;
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000067 2) gpio_configure_output "${GPIO_UARTx_MODE0}" 1
Tung Nguyen189431e2020-12-16 08:11:51 +000068 exit 0
69 ;;
70 *) echo "Invalid UART mode selection"
71 exit 1
72 ;;
73esac