Tung Nguyen | 189431e | 2020-12-16 08:11:51 +0000 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (c) 2020 Ampere Computing LLC |
| 4 | # |
| 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 | |
| 22 | if [ $# -lt 2 ]; then |
| 23 | exit 1 |
| 24 | fi |
| 25 | |
| 26 | function set_gpio_active_low() { |
| 27 | if [ $# -ne 2 ]; then |
| 28 | echo "set_gpio_active_low: need both GPIO# and initial level"; |
| 29 | return; |
| 30 | fi |
| 31 | |
| 32 | if [ ! -d /sys/class/gpio/gpio$1 ]; then |
| 33 | echo $1 > /sys/class/gpio/export |
| 34 | fi |
| 35 | echo $2 > /sys/class/gpio/gpio$1/direction |
| 36 | } |
| 37 | |
| 38 | GPIO_BASE=$(cat /sys/class/gpio/gpio*/base) |
| 39 | |
| 40 | case "$1" in |
| 41 | 1) GPIO_UARTx_MODE0=56 |
| 42 | # CPU0 UART0 connects to BMC UART1 |
| 43 | CONSOLE_PORT=0 |
| 44 | ;; |
| 45 | 2) GPIO_UARTx_MODE0=57 |
| 46 | # CPU0 UART1 connects to BMC UART2 |
| 47 | CONSOLE_PORT=1 |
| 48 | ;; |
| 49 | 3) GPIO_UARTx_MODE0=58 |
| 50 | # CPU0 UART4 connects to BMC UART3 |
| 51 | CONSOLE_PORT=2 |
| 52 | ;; |
| 53 | 4) GPIO_UARTx_MODE0=59 |
| 54 | # CPU1 UART1 connects to BMC UART4 |
| 55 | CONSOLE_PORT=3 |
| 56 | ;; |
| 57 | *) echo "Invalid UART port selection" |
| 58 | exit 1 |
| 59 | ;; |
| 60 | esac |
| 61 | |
| 62 | # Only switch the MUX when there is no active connection. This means we only |
| 63 | # switch the MUX before the first session starts and after the last session |
| 64 | # closes. We do this by querying number of connected sessions to the socket |
| 65 | # of requested console port. |
| 66 | # Example format: Accepted: 1; Connected: 1; |
| 67 | CONNECTED=$(systemctl --no-pager status obmc-console-ttyS${CONSOLE_PORT}-ssh.socket | grep -w Connected | cut -d ':' -f 3 | tr -d ' ;') |
| 68 | if [ ! $CONNECTED -le 1 ]; then |
| 69 | exit 0 |
| 70 | fi |
| 71 | |
| 72 | echo "Ampere UART MUX CTRL UART port $1 to mode $2" |
| 73 | |
| 74 | case "$2" in |
| 75 | 1) set_gpio_active_low $((${GPIO_BASE} + ${GPIO_UARTx_MODE0})) low |
| 76 | exit 0 |
| 77 | ;; |
| 78 | 2) set_gpio_active_low $((${GPIO_BASE} + ${GPIO_UARTx_MODE0})) high |
| 79 | exit 0 |
| 80 | ;; |
| 81 | *) echo "Invalid UART mode selection" |
| 82 | exit 1 |
| 83 | ;; |
| 84 | esac |