#!/bin/sh
# Copyright 2014-present Facebook. All Rights Reserved.

#
# Functions to enable/disable isolation buffer:
#
# An Isolation Buffer is a 8-bit bus switch (TI SN74CBT3245APWR):
#  - when OE (controlled by ISO_BUF_EN GPIO) is low, 8 input ports are
#    connected to the 8 output ports.
#  - when OE (controlled by ISO_BUF_EN GPIO) is high, input and output
#    prots are disconnected.
#
# For more details, please refer to:
#  - wedge schematics, "BMC SIGNAL ISOLATION BUFFERS" in page 61.
#  - https://www.ti.com/store/ti/en/p/product/?p=SN74CBT3245APWR
#
wedge_iso_buf_enable() {
    gpio_set_value ISO_BUF_EN 0
}

wedge_iso_buf_disable() {
    gpio_set_value ISO_BUF_EN 1
}

wedge_is_us_on() {
    local val n retries prog
    if [ $# -gt 0 ]; then
        retries="$1"
    else
        retries=1
    fi
    if [ $# -gt 1 ]; then
        prog="$2"
    else
        prog=""
    fi
    if [ $# -gt 2 ]; then
        default=$3              # value 0 means defaul is 'ON'
    else
        default=1
    fi
    n=1
    while true; do
        val=$(cat "${SYSFS_I2C_DEVICES}/4-0040/gpio_inputs" 2>/dev/null)
        if [ -n "$val" ]; then
            break
        fi
        n=$((n+1))
        if [ $n -gt $retries ]; then
            echo -n " failed to read GPIO. "
            return $default
            break
        fi
        echo -n "$prog"
        sleep 1
    done
    if [ "$((val & (0x1 << 14)))" != "0" ]; then
        # powered on already
        return 0
    else
        return 1
    fi
}

# Return the board type, 'LC', 'FC-LEFT', 'FC-RIGHT', or, 'WEDGE'
wedge_board_type() {
    local pn
    pn=$(/usr/bin/weutil 2> /dev/null | grep -i '^Location on Fabric:')
    case "$pn" in
        *LEFT*)
            echo 'FC-LEFT'
            ;;
        *RIGHT*)
            echo 'FC-RIGHT'
            ;;
        *LC*)
            echo 'LC'
            ;;
        *)
            echo 'WEDGE'
            ;;
    esac
}

# On FC,
#   board rev < 2:
#     FAB_SLOT_ID (GPIOU0), low == FC0; high == FC1
#   else:
#     FAB_SLOT_ID (GPIOU6), low == FC0; high == FC1
## On LC, Wedge,
#   board rev < 3:
#     GPIOU0(ID0), GPIOU1(ID1), GPIOU2(ID2), GPIOU3(ID3)
#   else:
#     GPIOU6(ID0), GPIOU7(ID1), GPIOV0(ID2), GPIOV1(ID3)
#
# ID[2:0] ID3 Slot#
# 000      0   1
# 000      1   2
# 001      0   3
# 001      1   4
# 010      0   5
# 010      1   6
# 011      0   7
# 011      1   8

wedge_slot_id() {
    local type slot id3 id2 id1 id0 FC_CARD_BASE board_rev
    FC_CARD_BASE=100
    # need to check the board rev
    board_rev=$(wedge_board_rev)
    case "$1" in
        FC-LEFT|FC-RIGHT)
            # On FC
            slot=$(gpio_get_value FAB_SLOT_ID)
            if [ "$1" = "FC-LEFT" ]; then
                # fabric card left
                slot=$((FC_CARD_BASE * (slot + 1) + 1))
            else
                # fabric card right
                slot=$((FC_CARD_BASE * (slot + 1) + 2))
            fi
            ;;
        *)
            # either edge or LC
            id0=$(gpio_get_value BP_SLOT_ID0)
            id1=$(gpio_get_value BP_SLOT_ID1)
            id2=$(gpio_get_value BP_SLOT_ID2)
            id3=$(gpio_get_value BP_SLOT_ID3)
            slot=$(((id2 * 4 + id1 * 2 + id0) * 2 + id3 + 1))
    esac
    echo "$slot"
}

# wedge_board_rev() is only valid after GPIO Y0, Y1, and Y2 are enabled
wedge_board_rev() {
    local val0 val1 val2
    val0=$(gpio_get_value BOARD_REV_ID0)
    val1=$(gpio_get_value BOARD_REV_ID1)
    val2=$(gpio_get_value BOARD_REV_ID2)
    echo $((val0 | (val1 << 1) | (val2 << 2)))
}

# Should we enable OOB interface or not
wedge_should_enable_oob() {
    board_rev=$(wedge_board_rev)
    board_type=$(wedge_board_type)
    case "$board_type" in
        FC-LEFT|FC-RIGHT)
            if [ $board_rev -lt 2 ]; then
                return 0
            fi
            ;;
        *)
            if [ $board_rev -lt 3 ]; then
                return 0
            fi
            ;;
    esac
    return 1
}


#!/bin/sh
#
# Copyright 2004-present Facebook. All rights reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Displays values onto the debug header LEDs."
    echo "Hex and decimal accepted."
    echo "Usage: $0 <value>"
}

. /usr/local/bin/openbmc-utils.sh

# Function to set the less significant hex digit
display_lower() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    # Now set the GPIOs to the right binary values
    gpio_set_value LED_POSTCODE_0 "$bit0"
    gpio_set_value LED_POSTCODE_1 "$bit1"
    gpio_set_value LED_POSTCODE_2 "$bit2"
    gpio_set_value LED_POSTCODE_3 "$bit3"
}

# Function to set the more significant hex digit
display_upper() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    gpio_set_value LED_POSTCODE_4 "$bit0"
    gpio_set_value LED_POSTCODE_5 "$bit1"
    gpio_set_value LED_POSTCODE_6 "$bit2"
    gpio_set_value LED_POSTCODE_7 "$bit3"
}

# Check number of parameters
if [ $# -ne 1 ]
then
    usage
    exit 1
fi

# Make sure input is actually numeric
DEC_VALUE=$(printf "%d" $1 2>/dev/null)
if [ $? -eq 1 ]
then
    echo "Unable to parse input as numeric value."
    exit 1
fi

# Make sure input is within proper range
if [ $DEC_VALUE -lt 0 ] || [ $DEC_VALUE -gt 255 ]
then
    echo "Value $DEC_VALUE is outside of displayable range 0 - 0xff (255)."
    exit 1
fi

# Get upper/lower decimal values
LOWER_DEC_VALUE=$(expr $DEC_VALUE % 16)
UPPER_DEC_VALUE=$(expr $DEC_VALUE / 16)

# Display the results
display_lower $LOWER_DEC_VALUE
display_upper $UPPER_DEC_VALUE


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO
. /usr/local/bin/openbmc-utils.sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

echo -n "Checking microserver power status ... "
if wedge_is_us_on 10 "."; then
    echo "on"
    on=1
else
    echo "off"
    on=0
fi

if [ $on -eq 0 ]; then
    # Reset USB hub
    /usr/local/bin/reset_usb.sh
    # Set up ROV
    /usr/local/bin/setup_rov.sh
    # Configure the management switch on LEFT side of FC.
    # Must do so after setup_rov.sh, as setup_rov.sh might reset
    # T2, which also resets the switch.
    if [ "$(wedge_board_type)" = "FC-LEFT" ]; then
        echo -n "Configure management switch ... "
        if [ $(wedge_board_rev) -gt 1 ]; then
            echo "skip. Need new switch setup script!!!"
        else
            # configure MDIO as GPIO output
            gpio_set_value BMC_MAC2_MDC 1
            gpio_set_value BMC_MAC2_MDIO 1
            # set the switch to be configured by CPU
            gpio_set_value SWITCH_EEPROM1_WRT 1
            # set the switch to be configured by CPU, then reset the switch,
            # which also cause T2 reset in the current board
            gpio_set_value SWITCH_EEPROM1_WRT 1
            gpio_set_value T2_RESET_N 0
            sleep 1
            gpio_set_value T2_RESET_N 1
            # configure the switch
            setup_switch.py
            echo "done"
        fi
    fi
    # Power on now
    wedge_power.sh on -f
fi


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Usage: $1 <on | off>"
    exit 1
}

. /usr/local/bin/openbmc-utils.sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

set -e

if [ $# != 1 ]; then
    usage $0
fi

if [ $1 = "on" ]; then
    val=1
elif [ $1 = "off" ]; then
    val=0
else
    usage $0
fi

gpio_set_value LED_PWR_BLUE $val


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

# Make sure RESET_SEQ0 and RESET_SEQ1 are 0
gpio_set_value RESET_SEQ0 0
gpio_set_value RESET_SEQ1 0

# Now toggle T2_RESET_N
gpio_set_value T2_RESET_N 0
sleep 1
gpio_set_value T2_RESET_N 1

logger "Reset T2 with both SEQ0 and SEQ1 low"


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

source /usr/local/bin/openbmc-utils.sh

echo -n "Reset CP2112 Switch ... "

gpio_set_value USB_BRDG_RST 0
usleep 50000
gpio_set_value USB_BRDG_RST 1

echo "Done"


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

echo -n "Reset USB Switch ... "

gpio_set_value BMC_USB_RESET_N 1
sleep 1
gpio_set_value BMC_USB_RESET_N 0
sleep 1
gpio_set_value BMC_USB_RESET_N 1

echo "Done"


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          gpio-setup
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description:  Set up GPIO pins as appropriate
### END INIT INFO

# This file contains definitions for the GPIO pins that were not otherwise
# defined in other files.  We should probably move some more of the
# definitions to this file at some point.

# The commented-out sections are generally already defined elsewhere,
# and defining them twice generates errors.

# The exception to this is the definition of the GPIO H0, H1, and H2
# pins, which seem to adversely affect the rebooting of the system.
# When defined, the system doesn't reboot cleanly.  We're still
# investigating this.

. /usr/local/bin/openbmc-utils.sh

if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
    LEGACY_KERNEL="legacy-4.1"
fi

devmem_cond_set() {
    local addr=$1
    local bit=$2

    if [ -n "${LEGACY_KERNEL}" ]; then
        devmem_set_bit "${addr}" "${bit}"
    fi
}

devmem_cond_clear() {
    local addr=$1
    local bit=$2

    if [ -n "${LEGACY_KERNEL}" ]; then
        devmem_clear_bit "${addr}" "${bit}"
    fi
}

setup_gpio_fc_evt() {
    local board_type=$1

    # FAB_SLOT_ID is GPIOU0
    # PEER_FAB_PRSNT is GPIOU1
    devmem_cond_set "$(scu_addr a0)" 8
    devmem_cond_set "$(scu_addr a0)" 9
    gpio_export_by_name "${ASPEED_GPIO}" GPIOU0 FAB_SLOT_ID
    gpio_export_by_name "${ASPEED_GPIO}" GPIOU1 PEER_FAB_PRSNT

    # T2_POWER_UP is GPIOT6
    devmem_cond_set "$(scu_addr a0)" 6
    gpio_export_by_name "${ASPEED_GPIO}" GPIOT6 T2_POWER_UP

    # HS_FAULT_N is GPIOT7
    devmem_cond_set "$(scu_addr a0)" 7
    gpio_export_by_name "${ASPEED_GPIO}" GPIOT7 HS_FAULT_N

    if [ "$board_type" = "FC-LEFT" ]; then
        # GPIOE2 is CPU_EEPROM_SEL, on FC-LEFT
        devmem_cond_clear "$(scu_addr 80)" 18
        devmem_cond_clear "$(scu_addr 8c)" 13
        devmem_cond_clear "$(scu_addr 70)" 22
        gpio_export_by_name "${ASPEED_GPIO}" GPIOE2 CPU_EEPROM_SEL

        # GPIOA6 and GPIOA7 are MAC2 MDIO pins, we use them as
        # GPIO for bitbang driver
        devmem_cond_clear "$(scu_addr 90)" 2
        devmem_cond_clear "$(scu_addr 80)" 6
        devmem_cond_clear "$(scu_addr 80)" 7
        gpio_export_by_name "${ASPEED_GPIO}" GPIOA6 BMC_MAC2_MDC
        gpio_export_by_name "${ASPEED_GPIO}" GPIOA7 BMC_MAC2_MDIO
    fi
}

setup_gpio_fc_dvt() {
    local board_type=$1

    if [ "$board_type" = "FC-LEFT" ]; then # Left FC
        # BMC_SW_RST is GPIOL0, 16p switch
        # SCU84[16] must be 0
        devmem_cond_clear "$(scu_addr 84)" 16
        gpio_export_by_name "${ASPEED_GPIO}" GPIOL0 BMC_SW_RST
        gpio_set_value BMC_SW_RST 1

        # MDC|MDIO_CONT are GPIOR6 and GPIOR7, 16p switch
        # SCU88[30:31] must be 0
        devmem_cond_clear "$(scu_addr 88)" 30
        devmem_cond_clear "$(scu_addr 88)" 31
        gpio_export_by_name "${ASPEED_GPIO}" GPIOR6 BMC_MAC1_MDC
        gpio_export_by_name "${ASPEED_GPIO}" GPIOR7 BMC_MAC1_MDIO
        gpio_set_value BMC_MAC1_MDC 1
        gpio_set_value BMC_MAC1_MDIO 1

        # SWITCH_EEPROM1_WRT is GPIOE2, 16p switch EEPROM (U61)
        # SCU80[18], SCU8C[13], and SCU70[22] must be 0
        devmem_cond_clear "$(scu_addr 80)" 18
        devmem_cond_clear "$(scu_addr 8C)" 13
        devmem_cond_clear "$(scu_addr 70)" 22
        gpio_export_by_name "${ASPEED_GPIO}" GPIOE2 SWITCH_EEPROM1_WRT

        # SPI bus to 16p switch EEPROM
        # GPIOI4 <--> BMC_EEPROM1_SPI_SS
        # GPIOI5 <--> BMC_EEPROM1_SPI_SCK
        # GPIOI6 <--> BMC_EEPROM1_SPI_MOSI
        # GPIOI7 <--> BMC_EEPROM1_SPI_MISO
        # The EEPROM SPI clk does not match with the BMC SPI master.
        # Have to configure these pins as GPIO to use with
        # SPI bitbang driver.
        # SCU70[13:12,5] must be 0
        devmem_cond_clear "$(scu_addr 70)" 5
        devmem_cond_clear "$(scu_addr 70)" 12
        devmem_cond_clear "$(scu_addr 70)" 13
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI4 BMC_EEPROM1_SPI_SS
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI5 BMC_EEPROM1_SPI_SCK
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI6 BMC_EEPROM1_SPI_MOSI
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI7 BMC_EEPROM1_SPI_MISO

        # BMC_PHY_RST is GPIOT0, Front Panel Port PHY on the 16p switch
        # SCUA0[0] must be 1
        devmem_cond_set "$(scu_addr a0)" 0
        gpio_export_by_name "${ASPEED_GPIO}" GPIOT0 BMC_PHY_RST
        gpio_set_value BMC_PHY_RST 1

        # BMC_5PORTSW_RST is GPIOT1, 5p switch
        # SCUA0[1] must be 1
        devmem_cond_set "$(scu_addr a0)" 1
        gpio_export_by_name "${ASPEED_GPIO}" GPIOT1 BMC_5PORTSW_RST
        gpio_set_value BMC_5PORTSW_RST 1

        # ISO_SWITCH1_MDC|MDIO are GPIOT4 and GPIOT5, 5p switch
        # SCUA0[4:5] must be 1
        devmem_cond_set "$(scu_addr a0)" 4
        devmem_cond_set "$(scu_addr a0)" 5
        gpio_export_by_name "${ASPEED_GPIO}" GPIOT4 ISO_SWITCH1_MDC
        gpio_export_by_name "${ASPEED_GPIO}" GPIOT5 ISO_SWITCH1_MDIO
        gpio_set_value ISO_SWITCH1_MDC 1
        gpio_set_value ISO_SWITCH1_MDIO 1

        # ISO_SWITCH_EEPROM2_WRT is GPIOV0, 5p switch EEPROM (U114)
        # SCUA0[16] must be 1
        devmem_cond_set "$(scu_addr a0)" 16
        gpio_export_by_name "${ASPEED_GPIO}" GPIOV0 ISO_SWITCH_EEPROM2_WRT

        # SPI bus to 5p switch EEPROM (U114)
        # GPIOI0 <--> ISO_BMC_EEPROM2_SPI_SS
        # GPIOI1 <--> ISO_BMC_EEPROM2_SPI_SCK
        # GPIOI2 <--> ISO_BMC_EEPROM2_SPI_MOSI
        # GPIOI3 <--> ISO_BMC_EEPROM2_SPI_MISO
        # The EEPROM SPI clk does not match with the BMC SPI master.
        # Have to configure these pins as GPIO to use with
        # SPI bitbang driver.
        # SCU70[13] must be 0, has already been set when
        # preparing for GPIOI4-GPIOI7
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI0 ISO_BMC_EEPROM2_SPI_SS
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI1 ISO_BMC_EEPROM2_SPI_SCK
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI2 ISO_BMC_EEPROM2_SPI_MOSI
        gpio_export_by_name "${ASPEED_GPIO}" GPIOI3 ISO_BMC_EEPROM2_SPI_MISO

        # BMC_PHYL_RST is GPIOF0, Left BMC PHY
        # SCU80[24] must be 0
        devmem_cond_clear "$(scu_addr 80)" 24
        gpio_export_by_name "${ASPEED_GPIO}" GPIOF0 BMC_PHYL_RST
        gpio_set_value BMC_PHYL_RST 1
    else # Right FC
        # BMC_PHYR_RST is GPIOL1, Right BMC PHY
        # SCU84[17] must be 0
        devmem_cond_clear "$(scu_addr 84)" 17
        gpio_export_by_name "${ASPEED_GPIO}" GPIOL1 BMC_PHYR_RST
        gpio_set_value BMC_PHYR_RST 1
    fi

    # T2_POWER_UP is GPIOU4
    # SCUA0[12] must be 1
    devmem_cond_set "$(scu_addr a0)" 12
    gpio_export_by_name "${ASPEED_GPIO}" GPIOU4 T2_POWER_UP

    # HS_FAULT_N is GPIOU5
    # SCUA0[13] must be 1
    devmem_cond_set "$(scu_addr a0)" 13
    gpio_export_by_name "${ASPEED_GPIO}" GPIOU5 HS_FAULT_N

    # FAB_SLOT_ID is GPIOU6
    # SCUA0[14] must be 1
    devmem_cond_set "$(scu_addr a0)" 14
    gpio_export_by_name "${ASPEED_GPIO}" GPIOU6 FAB_SLOT_ID

    # PEER_FAB_PRSNT is GPIOU7
    # SCUA0[15] must be 1
    devmem_cond_set "$(scu_addr a0)" 15
    gpio_export_by_name "${ASPEED_GPIO}" GPIOU7 PEER_FAB_PRSNT
}

setup_gpio_lc_wedge() {
    local board_rev=$1

    # Set up to watch for FC presence, and switch between interfaces.
    # GPIOC0..C7, interested in C4, C5
    devmem_cond_clear "$(scu_addr 90)" 0
    devmem_cond_clear "$(scu_addr 90)" 25

    if [ "$board_rev" -lt 3 ]; then
        # Prior to DVTC
        # BP_SLOT_ID GPIO pins are U0, U1, U2, U3
        devmem_cond_set "$(scu_addr a0)" 8
        devmem_cond_set "$(scu_addr a0)" 9
        devmem_cond_set "$(scu_addr a0)" 10
        devmem_cond_set "$(scu_addr a0)" 11
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU0 BP_SLOT_ID0
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU1 BP_SLOT_ID1
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU2 BP_SLOT_ID2
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU3 BP_SLOT_ID3

        # T2_POWER_UP is GPIOT6
        devmem_cond_set "$(scu_addr a0)" 6
        gpio_export_by_name "${ASPEED_GPIO}" GPIOT6 T2_POWER_UP

        # HS_FAULT_N is GPIOT7
        devmem_cond_set "$(scu_addr a0)" 7
        gpio_export_by_name "${ASPEED_GPIO}" GPIOT7 HS_FAULT_N
    else
        # Starting from DVTC
        # BP_SLOT_ID GPIO pins are U6, U7, V0, V1
        devmem_cond_set "$(scu_addr 70)" 6
        devmem_cond_set "$(scu_addr a0)" 14
        devmem_cond_set "$(scu_addr a0)" 15
        devmem_cond_set "$(scu_addr a0)" 16
        devmem_cond_set "$(scu_addr a0)" 17
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU6 BP_SLOT_ID0
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU7 BP_SLOT_ID1
        gpio_export_by_name "${ASPEED_GPIO}" GPIOV0 BP_SLOT_ID2
        gpio_export_by_name "${ASPEED_GPIO}" GPIOV1 BP_SLOT_ID3

        # T2_POWER_UP is GPIOU4
        devmem_cond_set "$(scu_addr a0)" 12
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU4 T2_POWER_UP

        # HS_FAULT_N is GPIOU5
        devmem_cond_set "$(scu_addr a0)" 13
        gpio_export_by_name "${ASPEED_GPIO}" GPIOU5 HS_FAULT_N
    fi
}

# Set up to read the board revision pins, Y0, Y1, Y2
devmem_cond_set "$(scu_addr 70)" 19
devmem_cond_clear "$(scu_addr a4)" 8
devmem_cond_clear "$(scu_addr a4)" 9
devmem_cond_clear "$(scu_addr a4)" 10
gpio_export_by_name "${ASPEED_GPIO}" GPIOY0 BOARD_REV_ID0
gpio_export_by_name "${ASPEED_GPIO}" GPIOY1 BOARD_REV_ID1
gpio_export_by_name "${ASPEED_GPIO}" GPIOY2 BOARD_REV_ID2

# enabled Y0, Y1, Y2, we can use wedge_board_rev() now
board_rev=$(wedge_board_rev)

# Set up ISO_SVR_ID[0-3], GPION[2-5]
# On wedge, these 4 GPIOs are not connected. And the corresponding
# 4 pins from uS are strapped to low.
# On fabic, these 4 pins are connected to uS SVR_ID pins,
# which are used to set the uS FPGA i2c address.
# Force all pins to low to have the same uS FPGA i2c address on wedge
# and fabric
# To use GPION[2-5], SCU90[4:5] must be 0, and SCU88[2-5] must be 0 also
devmem_cond_clear "$(scu_addr 90)" 4
devmem_cond_clear "$(scu_addr 90)" 5
devmem_cond_clear "$(scu_addr 88)" 2
devmem_cond_clear "$(scu_addr 88)" 3
devmem_cond_clear "$(scu_addr 88)" 4
devmem_cond_clear "$(scu_addr 88)" 5
gpio_export_by_name "${ASPEED_GPIO}" GPION2 ISO_SVR_ID0
gpio_export_by_name "${ASPEED_GPIO}" GPION3 ISO_SVR_ID1
gpio_export_by_name "${ASPEED_GPIO}" GPION4 ISO_SVR_ID2
gpio_export_by_name "${ASPEED_GPIO}" GPION5 ISO_SVR_ID3
for i in 0 1 2 3; do
    gpio_set_value ISO_SVR_ID${i} 0
done

## CARD_EN, GPIO C3
#devmem_cond_clear "$(scu_addr 90)" 0
#devmem_cond_clear "$(scu_addr 90)" 24
#echo 18 > /sys/class/gpio/export

# T2_RESET_N, RESET_SEQ0, RESET_SEQ1, on GPIO C0, F3, and F2
devmem_cond_clear "$(scu_addr 90)" 0
devmem_cond_clear "$(scu_addr 90)" 23
devmem_cond_clear "$(scu_addr 80)" 26
devmem_cond_clear "$(scu_addr a4)" 13
devmem_cond_clear "$(scu_addr 80)" 27
devmem_cond_clear "$(scu_addr a4)" 14
devmem_cond_set "$(scu_addr 70)" 19
gpio_export_by_name "${ASPEED_GPIO}" GPIOC0 T2_RESET_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOF2 RESET_SEQ1
gpio_export_by_name "${ASPEED_GPIO}" GPIOF3 RESET_SEQ0

# PANTHER_PRSNT_N, uServer presence, on GPIO E4
devmem_cond_clear "$(scu_addr 80)" 20
devmem_cond_clear "$(scu_addr 8c)" 14
devmem_cond_clear "$(scu_addr 70)" 22
gpio_export_by_name "${ASPEED_GPIO}" GPIOE4 PANTHER_PRSNT_N

# MRSRVR_SYS_RST, reset the uServer, on GPIO C1
devmem_cond_clear "$(scu_addr 90)" 0
devmem_cond_clear "$(scu_addr 90)" 23
gpio_export_by_name "${ASPEED_GPIO}" GPIOC1 MRSRVR_SYS_RST
# output

# BMC_PWR_BTN_IN_N, uServer power button in, on GPIO D0
# BMC_PWR_BTN_OUT_N, uServer power button out, on GPIO D1
devmem_cond_clear "$(scu_addr 90)" 1
devmem_cond_clear "$(scu_addr 8c)" 8
devmem_cond_clear "$(scu_addr 70)" 21
gpio_export_by_name "${ASPEED_GPIO}" GPIOD0 BMC_PWR_BTN_IN_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOD1 BMC_PWR_BTN_OUT_N
# we have to ensure that BMC_PWR_BTN_OUT_N is high so that
# when we enable the isolation buffer, uS will not be powered down
gpio_set_value BMC_PWR_BTN_OUT_N 1

## BMC_READY_IN, BMC signal that it's up, on GPIO P7
# To use GPIOP7 (127), SCU88[23] must be 0
devmem_cond_clear "$(scu_addr 88)" 23
# Put GPIOP7 (127) to low so that we can control uS power now
# This must be after 'gpio_set 25 1'
gpio_export_by_name "${ASPEED_GPIO}" GPIOP7 BMC_READY_IN
gpio_set_value BMC_READY_IN 0

# PANTHER_I2C_ALERT_N, alert for uServer I2C, GPIO B0
devmem_cond_clear "$(scu_addr 80)" 8
gpio_export_by_name "${ASPEED_GPIO}" GPIOB0 PANTHER_I2C_ALERT_N

# MNSERV_NIC_SMBUS_ALRT, alert for uServer NIC, GPIO B1
devmem_cond_clear "$(scu_addr 80)" 9
gpio_export_by_name "${ASPEED_GPIO}" GPIOB1 MNSERV_NIC_SMBUS_ALRT

# LED_PWR_BLUE, blue power light, GPIO E5
devmem_cond_clear "$(scu_addr 80)" 21
devmem_cond_clear "$(scu_addr 8c)" 14
devmem_cond_clear "$(scu_addr 70)" 22
gpio_export_by_name "${ASPEED_GPIO}" GPIOE5 LED_PWR_BLUE
# output

# BMC_HEARTBEAT_N, heartbeat LED, GPIO Q7
devmem_cond_clear "$(scu_addr 90)" 28
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ7 BMC_HEARTBEAT_N
# output

# XXX:  setting those causes the system to lock up on reboot
## T2 ROV1, ROV2, ROV3 -- voltage reading, GPIOs H0, H1, and H2
#devmem_cond_clear "$(scu_addr 90)" 6
#devmem_cond_clear "$(scu_addr 90)" 7
#devmem_cond_clear "$(scu_addr 70)" 4
## Do I need to set 70:1 and 70:0 to 1?
gpio_export_by_name "${ASPEED_GPIO}" GPIOH0 T2_ROV1
gpio_export_by_name "${ASPEED_GPIO}" GPIOH1 T2_ROV2
gpio_export_by_name "${ASPEED_GPIO}" GPIOH2 T2_ROV3

# HOTSWAP_PG, hotswap issues, GPIO L3
devmem_cond_clear "$(scu_addr 90)" 5
devmem_cond_clear "$(scu_addr 90)" 4
devmem_cond_clear "$(scu_addr 84)" 19
gpio_export_by_name "${ASPEED_GPIO}" GPIOL3 HOTSWAP_PG

# XXX:  These interfere with i2c bus 11 (on Linux, it's 12 on the hardware)
# which we need to talk to the power supplies on certain hardware.
## Hardware presence pins C4 and C5
#devmem_cond_clear "$(scu_addr 90)" 0
#devmem_cond_clear "$(scu_addr 90)" 24
#echo 20 > /sys/class/gpio/export
#echo 21 > /sys/class/gpio/export

# FAB_GE_SEL, uServer GE connection, GPIO A0
devmem_cond_clear "$(scu_addr 80)" 0
gpio_export_by_name "${ASPEED_GPIO}" GPIOA0 FAB_GE_SEL
# output

# USB_OCS_N1, resettable fuse tripped, GPIO Q6
devmem_cond_clear "$(scu_addr 90)" 28
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ6 USB_OCS_N1

# RX loss signal?

# System SPI
# Strap 12 must be 0 and Strape 13 must be 1
devmem_cond_clear "$(scu_addr 70)" 12
devmem_cond_set "$(scu_addr 70)" 13
# GPIOQ4 is ISO_FLASH_WP, must be 1 to avoid write protection
# GPIOQ5 is ISO_FLASH_HOLD, must be 1 to be out of reset
# To use GPIOQ4 and GPIOQ5, SCU90[27] must be 0
devmem_cond_clear "$(scu_addr 90)" 27
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ4 ISO_FLASH_WP
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ5 ISO_FLASH_HOLD
gpio_set_value ISO_FLASH_WP 1
gpio_set_value ISO_FLASH_HOLD 1
# GPIOD6 is ISO_FL_PRG_SEL, set it to 0 so that BMC does not have control
# on the EEPROM by default.
# To use GPIOD6, SCU90[1] must be 0, SCU8C[21] must be 0, and Strap[21] must be 0
devmem_cond_clear "$(scu_addr 90)" 1
devmem_cond_clear "$(scu_addr 8c)" 8
devmem_cond_clear "$(scu_addr 70)" 21
gpio_export_by_name "${ASPEED_GPIO}" GPIOD6 ISO_FL_PRG_SEL
gpio_set_value ISO_FL_PRG_SEL 0

# DEBUG_RST_BTN_N, Debug Reset button on front panel, GPIO R2
devmem_cond_clear "$(scu_addr 88)" 26
gpio_export_by_name "${ASPEED_GPIO}" GPIOR2 DEBUG_RST_BTN_N

# DEBUG_PORT_UART_SEL_N, Debug Select button, GPIO B2
devmem_cond_clear "$(scu_addr 80)" 10
gpio_export_by_name "${ASPEED_GPIO}" GPIOB2 DEBUG_PORT_UART_SEL_N

# DEBUG_UART_SEL_0, select uServer UART to the debug header, GPIO E0
devmem_cond_clear "$(scu_addr 80)" 16
devmem_cond_clear "$(scu_addr 8c)" 12
devmem_cond_clear "$(scu_addr 70)" 22
gpio_export_by_name "${ASPEED_GPIO}" GPIOE0 DEBUG_UART_SEL_0
# output

# USB_BRDG_RST , GPIO D4
devmem_cond_clear "$(scu_addr 90)" 1
devmem_cond_clear "$(scu_addr 8c)" 10
devmem_cond_clear "$(scu_addr 70)" 21
gpio_export_by_name "${ASPEED_GPIO}" GPIOD4 USB_BRDG_RST

# Bloodhound GPIOs, P0-6, G4, J1-3, Y3
# Make sure GPIOP0,1,2,3,6 are enabled.
for i in {16..19} 22; do
  devmem_cond_clear "$(scu_addr 88)" $i
done
# Enable GPIOY3
devmem_cond_clear "$(scu_addr a4)" 11
# GPIOG4
devmem_cond_clear "$(scu_addr 2c)" 1
# GPIOJ1
devmem_cond_clear "$(scu_addr 84)" 9
# GPIOJ2
devmem_cond_clear "$(scu_addr 84)" 10
# GPIOJ11
devmem_cond_clear "$(scu_addr 84)" 11

# Export all the GPIOs
for i in {0..6}; do
    gpio_export_by_name "${ASPEED_GPIO}" GPIOP${i} BLOODHOUND_GPIOP${i}
done
for i in {1..3}; do
    gpio_export_by_name "${ASPEED_GPIO}" GPIOJ${i} BLOODHOUND_GPIOJ${i}
done
gpio_export_by_name "${ASPEED_GPIO}" GPIOG4 BLOODHOUND_GPIOG4
gpio_export_by_name "${ASPEED_GPIO}" GPIOY3 BLOODHOUND_GPIOY3

# ISO_BUF_EN, GPIOC2, SCU90[0] and SCU90[24] must be 0
devmem_cond_clear "$(scu_addr 90)" 0
devmem_cond_clear "$(scu_addr 90)" 24
gpio_export_by_name "${ASPEED_GPIO}" GPIOC2 ISO_BUF_EN

# Enable the isolation buffer
wedge_iso_buf_enable

# load the EEPROM
i2c_device_add 6 0x50 24c64

# Get the board type by parsing the EEPROM.
# This must be after enabling isolation buffer as the i2c bus
# is isolated by the buffer
board_type=$(wedge_board_type)

case "$board_type" in
    FC-LEFT|FC-RIGHT)
        # On FC
        if [ "$board_rev" -lt 2 ]; then
            # EVT board
            setup_gpio_fc_evt "$board_type"
        else
            # DVT board
            setup_gpio_fc_dvt "$board_type"
        fi
        ;;
    *)
        setup_gpio_lc_wedge "$board_rev"
        ;;
esac

# Make it possible to turn off T2 if fand sees overheating via GPIOF1
# Do not change the GPIO direction here as the default value of this GPIO
# is low, which causes a Non-maskable interrupt to the uS.
devmem_cond_clear "$(scu_addr 80)" 25
devmem_cond_clear "$(scu_addr a4)" 12
gpio_export_by_name "${ASPEED_GPIO}" GPIOF1 BMC_GPIOF1

# Allow us to set the fan LEDs boards.
# This is GPIO G5, G6, G7, and J0

devmem_cond_clear "$(scu_addr 70)" 23
devmem_cond_clear "$(scu_addr 84)" 5
devmem_cond_clear "$(scu_addr 84)" 6
devmem_cond_clear "$(scu_addr 84)" 7
devmem_cond_clear "$(scu_addr 84)" 8
gpio_export_by_name "${ASPEED_GPIO}" GPIOG5 FAN_LED_GPIOG5
gpio_export_by_name "${ASPEED_GPIO}" GPIOG6 FAN_LED_GPIOG6
gpio_export_by_name "${ASPEED_GPIO}" GPIOG7 FAN_LED_GPIOG7
gpio_export_by_name "${ASPEED_GPIO}" GPIOJ0 FAN_LED_GPIOJ0

gpio_set_direction FAN_LED_GPIOG5 out
gpio_set_direction FAN_LED_GPIOG6 out
gpio_set_direction FAN_LED_GPIOG7 out
gpio_set_direction FAN_LED_GPIOJ0 out

# Once we set "out", output values will be random unless we set them
# to something
gpio_set_value FAN_LED_GPIOG5 0
gpio_set_value FAN_LED_GPIOG6 0
gpio_set_value FAN_LED_GPIOG7 0
gpio_set_value FAN_LED_GPIOJ0 0

#
# Setup LED_POSTCODE_[0..3], connected to GPIOG[0..3]:
#   - GPIOG[0..3]: SCU84[0..3] = 0
#
devmem_cond_clear $(scu_addr 84) 0
devmem_cond_clear $(scu_addr 84) 1
devmem_cond_clear $(scu_addr 84) 2
devmem_cond_clear $(scu_addr 84) 3
gpio_export_by_name "${ASPEED_GPIO}" GPIOG0 LED_POSTCODE_0
gpio_export_by_name "${ASPEED_GPIO}" GPIOG1 LED_POSTCODE_1
gpio_export_by_name "${ASPEED_GPIO}" GPIOG2 LED_POSTCODE_2
gpio_export_by_name "${ASPEED_GPIO}" GPIOG3 LED_POSTCODE_3

#
# Setup LED_POSTCODE_[4..7], connected to GPIOB[4..7]:
#   - GPIOB4: SCU80[12] = 0 and Strap[14] = 0
#   - GPIOB[5..7]: SCU80[13..15] = 0
#
devmem_cond_clear $(scu_addr 70) 14
devmem_cond_clear $(scu_addr 80) 12
devmem_cond_clear $(scu_addr 80) 13
devmem_cond_clear $(scu_addr 80) 14
devmem_cond_clear $(scu_addr 80) 15
gpio_export_by_name "${ASPEED_GPIO}" GPIOB4 LED_POSTCODE_4
gpio_export_by_name "${ASPEED_GPIO}" GPIOB5 LED_POSTCODE_5
gpio_export_by_name "${ASPEED_GPIO}" GPIOB6 LED_POSTCODE_6
gpio_export_by_name "${ASPEED_GPIO}" GPIOB7 LED_POSTCODE_7

#
# Setup BMC_USB_RESET_N, connected to GPIOD7
#   - GPIOD7: SCU90[1], SCU8C[11], and SCU70[21] must be 0
#
devmem_cond_clear $(scu_addr 8C) 11
devmem_cond_clear $(scu_addr 70) 21
devmem_cond_clear $(scu_addr 90) 1
gpio_export_by_name "${ASPEED_GPIO}" GPIOD7 BMC_USB_RESET_N


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

. /usr/local/bin/openbmc-utils.sh

# Bus 1
i2c_device_add 1 0x60 ncp4200

# Bus 2
i2c_device_add 2 0x60 ncp4200

# Bus 3
i2c_device_add 3 0x48 tmp75
i2c_device_add 3 0x49 tmp75
i2c_device_add 3 0x4a tmp75

# Bus 4
i2c_device_add 4 0x40 fb_panther_plus
i2c_device_add 4 0x51 24c128

# Bus 6, trouble
i2c_device_add 6 0x28 max127
i2c_device_add 6 0x3f pcf8574

# Bus 7
i2c_device_add 7 0x50 24c64         # 6pack power supply EEPROM
i2c_device_add 7 0x51 24c64         # wedge power supply EEPROM
i2c_device_add 7 0x52 24c64         # wedge power supply EEPROM
i2c_device_add 7 0x59 pfe1100       # wedge power supply
i2c_device_add 7 0x5a pfe1100       # wedge power supply

# Bus 8
i2c_device_add 8 0x60 ncp4200

# Bus 12
i2c_device_add 12 0x10 adm1278

i2c_check_driver_binding


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

# The T2 chip can prefer different input voltages, depending, presumably
# of manufacturing variations.  We need to determine whether it wants
# 0.95V or 1.025V, reset the T2 to reduce total power usage, set the
# outgoing voltage on the first buck converter, and bring T2 up out of
# reset.

. /usr/local/bin/openbmc-utils.sh

PMBUS_DRIVER_DIR="${SYSFS_I2C_DRIVERS}/pmbus"

# read the T2 ROV after the GPIOs are enabled
t2_rov() {
    local val0 val1 val2
    # Note that the values are *not* read in order.
    val0=$(gpio_get_value T2_ROV3)
    val1=$(gpio_get_value T2_ROV1)
    val2=$(gpio_get_value T2_ROV2)
    echo $((val0 | (val1 << 1) | (val2 << 2)))
}

rov=$(t2_rov)

# target_volts come from the data sheet and 18mV of loss and
# some fudging based on actual measurements to get either 1.025V
# or 0.95V at T2
if [ $rov -eq 1 ]; then
    target_volts=0x5a
elif [ $rov -eq 2 ]; then
    target_volts=0x65
else
    echo "Unrecognized T2 ROV value $rov, setting failed."
    exit 1
fi
target_volts=$((target_volts * 1))  # normalize to decimal

#
# Unbind pmbus driver from 1-0060, otherwise, following i2cget could
# fail with EBUSY.
#
echo 1-0060 > "${PMBUS_DRIVER_DIR}/unbind"

# Get current voltage value.
# The device here is NCP4200. It turns out the first i2c transaction to this
# device always fails. The spec does not mention anything about NCP4200 i2c
# engine start delay. However, each time, when the isolation buffer between the
# BMC i2c controller and NCP4200 is disabled and re-enabled, the first i2c
# transaction to NCP4200 always fails.
# To workaround this issue, we are doing the first read twice.
cur_volts=$(i2cget -y 1 0x60 0x8b w 2> /dev/null)
cur_volts=$(i2cget -y 1 0x60 0x8b w)
cur_volts=$((cur_volts * 1))  # normalize to decimal

# Only bounce the T2 if we actually need to modify the voltage
if [ $cur_volts -ne $target_volts ]; then
    # Set values before turning out output;  we're using "PCIE, then MCS"
    gpio_set_value RESET_SEQ1 1
    gpio_set_value RESET_SEQ0 1
    gpio_set_direction RESET_SEQ1 out
    gpio_set_direction RESET_SEQ0 out
    gpio_set_value T2_RESET_N 0
    gpio_set_direction T2_RESET_N out
    # T2 is in reset;  note that this may cause NMI messages on the uServer,
    # which shouldn't be up anyway when this is first run.

    # Set the requested value to the current value to avoid rapid shifts
    i2cset -y 1 0x60 0x21 $cur_volts w
    # Enable the requested voltage
    i2cset -y 1 0x60 0xd2 0x5a
    i2cset -y 1 0x60 0xd3 0x5a
    sleep 1

    # Set the target voltage
    i2cset -y 1 0x60 0x21 $target_volts w

    sleep 1

    # Let T2 come out of reset
    gpio_set_value T2_RESET_N 1
    echo "T2 ROV value set based on $rov."
    sleep 2
    gpio_set_value RESET_SEQ1 0
    gpio_set_value RESET_SEQ0 0
else
    echo "T2 ROV already correctly set."
fi

#
# Bind pmbus driver to 1-0060.
#
echo 1-0060 > "${PMBUS_DRIVER_DIR}/bind"


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
CONSOLE_SH=/usr/local/bin/us_console.sh
FILE=/etc/us_pseudo_tty
TTY=/dev/ttyS1

mTerm_server_running() {
  pid=$(ps | grep -v grep | grep '/usr/local/bin/mTerm_server' -m 1 |
      awk '{print $1}')
  if [ $pid ] ; then
    return 0
  fi
  return 1
}

start_sol_session() {
  if [ -a $FILE ]
    then
      read -r TTY<$FILE
  fi
  $CONSOLE_SH connect

  echo "You are in SOL session."
  echo "Use ctrl-x to quit."
  echo "-----------------------"
  echo

  trap '"$CONSOLE_SH" disconnect' INT TERM QUIT EXIT

  /usr/bin/microcom -s 57600 $TTY

  echo
  echo
  echo "-----------------------"
  echo "Exit from SOL session."
}

# if mTerm server is running use mTerm_client to connect to userver
# otherwise fallback to the old method

if mTerm_server_running; then
  exec /usr/local/bin/mTerm_client wedge
else
  start_sol_session
fi


#! /bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          us-monitor
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Start the microserver reset monitoring script
#
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME="us_monitor"
DESC="monitoring microserver reset"
DAEMON="us_monitor.sh"

# source function library
. /etc/init.d/functions

. /usr/local/bin/openbmc-utils.sh

STOPPER=
ACTION="$1"

case "$ACTION" in
  start)
    echo -n "Starting $DESC: "
    /usr/local/bin/${DAEMON} > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    killall ${DAEMON}
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    killall ${DAEMON}
    /usr/local/bin/${DAEMON} > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  status)
    status ${DAEMON}
    exit $?
    ;;
  *)
    N=${0##*/}
    N=${N#[SK]??}
    echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "$0 <connect | disconnect>"
}

. /usr/local/bin/openbmc-utils.sh

if [ $# -ne 1 ]; then
    usage
    exit 1
fi

if [ "$1" == "connect" ]; then
    VALUE=1
elif [ "$1" == "disconnect" ]; then
    VALUE=0
else
    usage
    exit 1
fi

# GPIOE0 (DEBUG_UART_SEL_0) controls if uS console connects to UART1 or not.
gpio_set_value DEBUG_UART_SEL_0 $VALUE


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

. /usr/local/bin/openbmc-utils.sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# Because the voltage leak from uS COM pins could cause uS to struck when
# transitting from S5 to S0, we will need to explicitely pull down uS COM
# pins before powering off/reset and restoring COM pins after

_logger() {
    logger "USMON: " $*
}

pull_down_us_com() {
    if [ $connected -eq 0 || $down -eq 0 ]; then
        _logger "Connect console pins with pull down"
        connected=1
        down=1
    fi

    #
    # XXX do we need to configure GPIOL6/GPIOL7 (BMC_UART1_TX/RX) as GPIO
    # and set them to low?
    #

    # now, connect uart from BMC to the uS
    gpio_set_value DEBUG_UART_SEL_0 1
}

restore_us_com() {
    # if sol.sh or terminal server is running, keep uServer console UART
    # connected with BMC
    if pidof -x sol.sh > /dev/null 2>&1 || pidof -x mTerm_server > /dev/null 2>&1; then
        if [ $connected -eq 0 ]; then
            _logger "Restore console. Keep connected"
            connected=1
        fi
        gpio_set_value DEBUG_UART_SEL_0 1
    else
        if [ $connected -eq 1 ]; then
            _logger "Restore console. Keep disconnected"
            connected=0
        fi
        gpio_set_value DEBUG_UART_SEL_0 0
    fi
    down=0
}

connected=0
down=1
while true; do
    if ! wedge_is_us_on 1 '' 0 > /dev/null 2>&1; then
        pull_down_us_com
    else
        restore_us_com
    fi
    usleep 400000               # 400ms
done


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

prog="$0"

usage() {
    echo "Usage: $prog <command> [command options]"
    echo
    echo "Commands:"
    echo "  status: Get the current microserver power status"
    echo
    echo "  on: Power on microserver if not powered on already"
    echo "    options:"
    echo "      -f: Re-do power on sequence no matter if microserver has "
    echo "          been powered on or not."
    echo
    echo "  off: Power off microserver ungracefully"
    echo
    echo "  reset: Power reset microserver ungracefully"
    echo "    options:"
    echo "      -s: Power reset whole wedge system ungracefully"
    echo
}

do_status() {
    echo -n "Microserver power is "
    if wedge_is_us_on; then
        echo "on"
    else
        echo "off"
    fi
    return 0
}

do_on() {
    local force opt pulse_us n retries
    force=0
    while getopts "f" opt; do
        case $opt in
            f)
                force=1
                ;;
            *)
                usage
                exit 1
                ;;

        esac
    done
    echo -n "Power on microserver ..."
    if [ $force -eq 0 ]; then
        # need to check if uS is on or not
        if wedge_is_us_on 10 "."; then
            echo " Already on. Skip!"
            return 1
        fi
    fi
    # start with resetting T2
    reset_brcm.sh
    # first make sure, GPIOD1 (25) is high
    gpio_set_value BMC_PWR_BTN_OUT_N 1
    sleep 1
    # then, put GPIOP7 (127) to low
    gpio_set_value BMC_READY_IN 0
    pulse_us=500000             # 500ms
    retries=3
    n=1
    while true; do
        # first make sure, GPIOD1 (25) is high
        gpio_set_value BMC_PWR_BTN_OUT_N 1
        usleep $pulse_us
        # generate the power on pulse
        gpio_set_value BMC_PWR_BTN_OUT_N 0
        usleep $pulse_us
        gpio_set_value BMC_PWR_BTN_OUT_N 1
        sleep 3
        if wedge_is_us_on 1 '' 1; then
            break
        fi
        n=$((n+1))
        if [ $n -gt $retries ]; then
            echo " Failed"
            logger "Failed to power on micro-server"
            return 1
        fi
        echo -n "..."
    done
    # Turn on the power LED (GPIOE5)
    /usr/local/bin/power_led.sh on
    echo " Done"
    logger "Successfully power on micro-server"
    return 0
}

do_off() {
    echo -n "Power off microserver ..."
    # first make sure, GPIOD1 (25) is high
    gpio_set_value BMC_PWR_BTN_OUT_N 1
    # then, put GPIOP7 (127) to low
    gpio_set_value BMC_READY_IN 0
    gpio_set_value BMC_PWR_BTN_OUT_N 0
    sleep 5
    gpio_set_value BMC_PWR_BTN_OUT_N 1
    # Turn off the power LED (GPIOE5)
    /usr/local/bin/power_led.sh off
    echo " Done"
    logger "Successfully power off micro-server"
    return 0
}

do_reset() {
    local system opt
    system=0
    while getopts "s" opt; do
        case $opt in
            s)
                system=1
                ;;
            *)
                usage
                exit 1
                ;;
        esac
    done
    if [ $system -eq 1 ]; then
        logger "Power reset the whole system ..."
        echo -n "Power reset the whole system ..."
        sleep 1             # give some time for the message to go out
        i2cset -f -y 12 0x10 0xd9 c
    else
        if ! wedge_is_us_on; then
            echo "Power resetting microserver that is powered off has no effect."
            echo "Use '$prog on' to power the microserver on"
            return -1
        fi
        # reset T2 first
        reset_brcm.sh
        echo -n "Power reset microserver ..."
        # then, put GPIOP7 (127) to low
        gpio_set_value BMC_READY_IN 0
        gpio_set_value MRSRVR_SYS_RST 0
        sleep 1
        gpio_set_value MRSRVR_SYS_RST 1
        sleep 1
        logger "Successfully power reset micro-server"
    fi
    echo " Done"
    return 0
}

if [ $# -lt 1 ]; then
    usage
    exit 1
fi

command="$1"
shift

case "$command" in
    status)
        do_status $@
        ;;
    on)
        do_on $@
        ;;
    off)
        do_off $@
        ;;
    reset)
        do_reset $@
        ;;
    *)
        usage
        exit 1
        ;;
esac

exit $?


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

mac=$(i2cdump -y 0x0 0x49 s 0xd4 | grep '^00: d4'| awk '{ print $3":"$4":"$5":"$6":"$7":"$8 }') 2>/dev/null

if [ -n "$mac" ]; then
    echo $mac
else
    echo "Cannot find out the microserver MAC" 1>&2
fi


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          bmc-log
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Collect micro-server kernel logs through serial port
### END INIT INFO

. /etc/default/bmc-log
. /etc/init.d/functions

DAEMON=/usr/sbin/bmc-log
NAME=bmc-log
DESC="Micro-Server log collection"

TTY=${US_TTY:-/dev/ttyS1}
IP=${LOG_SERVER_IP_VERSION:-4}
LOG_SERVER=${LOG_SERVER_NAME:-}
PORT=${LOG_SERVER_PORT:-}
BAUD_RATE=${TTY_BAUD_RATE:-}

if [ -z "$LOG_SERVER" ] || [ -z "$PORT" ]
then
	echo "Error: Server and/or port not set"
	exit 0
fi

ACTION="$1"

case "$ACTION" in
  start)
  	echo -e "Starting $DESC"
	$DAEMON $TTY $IP $LOG_SERVER $PORT $BAUD_RATE
    ;;
  stop)
    echo -e "Stopping $DESC: "
    start-stop-daemon --stop --quiet --exec $DAEMON
    ;;
   restart|force-reload)
    echo -e "Restarting $DESC: "
    start-stop-daemon --stop --quiet --exec $DAEMON
    sleep 1
    $DAEMON $TTY $IP $LOG_SERVER $PORT $BAUD_RATE
    ;;
  status)
    stat $DAEMON
    exit $?
    ;;
  *)
    N=${0##*/}
    N=${N#[SK]??}
    echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
    exit 1
    ;;
esac


#!/bin/sh
#
# Copyright 2004-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
set -e

. "/usr/local/bin/pwm_common.sh"

if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
    PWM_SYSFS_DIR="/sys/devices/platform/ast_pwm_tacho.0"

    #
    # Refer to "init_pwm.sh" for more details about fan-tacho mapping.
    #
    FAN_TACHO_MAP="0:3,7 1:2,6 2:0,4 3:1,5"
else
    PWM_SYSFS_DIR=$(hwmon_lookup_by_name "aspeed_pwm_tacho")
    if [ -z "$PWM_SYSFS_DIR" ]; then
        echo "unable to find hwmon directory for aspeed_pwm_tacho. Exiting!"
        exit 1
    fi
    FAN_TACHO_MAP="0:4,8 1:3,7 2:1,5 3:2,6"
fi

usage() {
    echo "Usage: $0 [Fan Unit (0..3)]" >&2
}

get_fan_speed_41() {
    fan_id="$1"
    front_tacho="$2"
    rear_tacho="$3"

    front_rpm=$(cat "${PWM_SYSFS_DIR}/tacho${front_tacho}_rpm")
    rear_rpm=$(cat "${PWM_SYSFS_DIR}/tacho${rear_tacho}_rpm")
    echo "Fan $fan_id RPMs: $front_rpm, $rear_rpm"
}

get_fan_speed_5x() {
    fan_id="$1"
    front_tacho="$2"
    rear_tacho="$3"

    front_rpm=$(cat "${PWM_SYSFS_DIR}/fan${front_tacho}_input")
    rear_rpm=$(cat "${PWM_SYSFS_DIR}/fan${rear_tacho}_input")
    echo "Fan $fan_id RPMs: $front_rpm, $rear_rpm"
}

get_fan_speed() {
    fan_id="$1"
    tacho_list="$2"

    front_tacho=${tacho_list%%,*}
    rear_tacho=${tacho_list##*,}
    if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
        get_fan_speed_41 "$fan_id" "$front_tacho" "$rear_tacho"
    else
        get_fan_speed_5x "$fan_id" "$front_tacho" "$rear_tacho"
    fi
}

# refer to the comments in init_pwn.sh regarding
# the fan unit and tacho mapping
if [ "$#" -eq 0 ]; then
    FAN_TACHOS="$FAN_TACHO_MAP"
elif [ "$#" -eq 1 ]; then
    case "$1" in
    "0")
        FAN_TACHOS=$(echo "$FAN_TACHO_MAP" | cut -d' ' -f1)
        ;;
    "1")
        FAN_TACHOS=$(echo "$FAN_TACHO_MAP" | cut -d' ' -f2)
        ;;
    "2")
        FAN_TACHOS=$(echo "$FAN_TACHO_MAP" | cut -d' ' -f3)
        ;;
    "3")
        FAN_TACHOS=$(echo "$FAN_TACHO_MAP" | cut -d' ' -f4)
        ;;
    *)
        usage
        exit 1
        ;;
    esac
else
    usage
    exit 1
fi

for fan_tacho in $FAN_TACHOS; do
    fan_id=${fan_tacho%%:*}
    tacho_list=${fan_tacho##*:}

    get_fan_speed "$fan_id" "$tacho_list"
done


#!/bin/sh
#
# Copyright 2004-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
. /usr/local/bin/openbmc-utils.sh

PWM_DIR=/sys/devices/platform/ast_pwm_tacho.0

set -e

#
# Note: the script is only needed for kernel 4.1, as pwm/tacho is already
# initialized in device tree in kernel 5.x.
#
if ! uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
    exit 0
fi

# Enable PWM 0, 1, 6, 7
devmem_set_bit $(scu_addr 88) 0
devmem_set_bit $(scu_addr 88) 1
devmem_set_bit $(scu_addr 88) 6
devmem_set_bit $(scu_addr 88) 7

# Eable tacho 0-7
devmem_clear_bit $(scu_addr 88) 8
devmem_clear_bit $(scu_addr 88) 9
devmem_clear_bit $(scu_addr 88) 10
devmem_clear_bit $(scu_addr 88) 11
devmem_clear_bit $(scu_addr 88) 12
devmem_clear_bit $(scu_addr 88) 13
devmem_clear_bit $(scu_addr 88) 14
devmem_clear_bit $(scu_addr 88) 15

# The PWM frequency is

#   clk_source / ((2 ^ division_) * (2 * division_l) * (unit + 1))
#
# Our clk_source is 24Mhz.  4-pin fans are generally supposed to be driven with
# a 25Khz PWM control signal.  Therefore we want the divisor to equal 960.
#
# We also want the unit to be as large as possible, since this controls the
# granularity with which we can modulate the PWM signal.  The following
# settings allow us to set the fan from 0 to 100% in increments of 1/96th.
#
# The AST chip supports 3 different PWM clock configurations, but we only use
# type M for now.
echo 0 > $PWM_DIR/pwm_type_m_division_h
echo 5 > $PWM_DIR/pwm_type_m_division_l
echo 95 > $PWM_DIR/pwm_type_m_unit

# On wedge, there are 4 fans connected.
# Each fan has one PWM input and 2 tacho outputs.
# Here is the mapping between the fan and PWN/Tacho,
# staring from the one from the edge
# Fan 0: PWM 7, Tacho3, Tacho7
# Fan 1: PWM 6, Tacho2, Tacho6
# Fan 2: PWM 0, Tacho0, Tacho4
# Fan 3: PWM 1, Tacho1, Tacho5

# For each fan, setting the type, and 100% initially
for pwm in 0 1 6 7; do
    echo 0 > $PWM_DIR/pwm${pwm}_type
    echo 0 > $PWM_DIR/pwm${pwm}_rising
    echo 0 > $PWM_DIR/pwm${pwm}_falling
    echo 1 > $PWM_DIR/pwm${pwm}_en
done

# Enable Tach 0..7
echo 0 > $PWM_DIR/tacho0_source
echo 0 > $PWM_DIR/tacho4_source
echo 1 > $PWM_DIR/tacho1_source
echo 1 > $PWM_DIR/tacho5_source
echo 6 > $PWM_DIR/tacho2_source
echo 6 > $PWM_DIR/tacho6_source
echo 7 > $PWM_DIR/tacho3_source
echo 7 > $PWM_DIR/tacho7_source
t=0
while [ $t -le 7 ]; do
    echo 1 > $PWM_DIR/tacho${t}_en
    t=$((t+1))
done


#!/bin/sh
#
# Copyright 2020-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
set -e

HWMON_SYSFS_ROOT="/sys/class/hwmon"

#
# Lookup pwm sysfs directory, used in kernel 5.x or higher versions.
#
hwmon_lookup_by_name() {
    hwmon_name="$1"
    pwm_dir=""

    for entry in "${HWMON_SYSFS_ROOT}"/*; do
        if echo "$entry" | grep "/hwmon/hwmon" > /dev/null 2>&1; then
            if grep "$hwmon_name" "${entry}/name" > /dev/null 2>&1; then
                pwm_dir="$entry"
                break
            fi
        fi
    done

    echo "$pwm_dir"
}


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-fan
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set fan speed
### END INIT INFO

. /usr/local/bin/openbmc-utils.sh

# Enable the isolation buffer
wedge_iso_buf_enable

echo -n "Setup fan speed... "
if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
    /usr/local/bin/init_pwm.sh
fi
/usr/local/bin/set_fan_speed.sh 50
/usr/local/bin/fand
echo "done."


#!/bin/sh
#
# Copyright 2004-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
set -e

. "/usr/local/bin/pwm_common.sh"

if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
    PWM_SYSFS_DIR="/sys/devices/platform/ast_pwm_tacho.0"

    #
    # Refer to "init_pwm.sh" for more details about pwm configuration as
    # well as fan-pwm mapping.
    #
    PWM_UNIT_MAX=96
    FAN_PWM_MAP="0:7 1:6 2:0 3:1"
else
    # kernel 5.x and higher versions
    PWM_SYSFS_DIR=$(hwmon_lookup_by_name "aspeed_pwm_tacho")
    if [ -z "$PWM_SYSFS_DIR" ]; then
        echo "unable to find hwmon directory for aspeed_pwm_tacho. Exiting!"
        exit 1
    fi
    PWM_UNIT_MAX=256
    FAN_PWM_MAP="0:8 1:7 2:1 3:2"
fi

usage() {
    echo "Usage: $0 <PERCENT (0..100)> <Fan Unit (0..3)> " >&2
}

set_fan_pwm_41() {
    pwm_id="$1"
    unit="$2"

    if [ "$unit" -eq 0 ]; then
        # For 0%, turn off the PWM entirely
        echo 0 > "$PWM_SYSFS_DIR/pwm${pwm_id}_en"
    else
        if [ "$unit" -eq $PWM_UNIT_MAX ]; then
            # For 100%, set falling and rising to the same value
            unit=0
        fi

        # always use type M. refer to the comments in init_pwm.sh
        echo 0 > "$PWM_SYSFS_DIR/pwm${pwm_id}_type"
        echo 0 > "$PWM_SYSFS_DIR/pwm${pwm_id}_rising"
        echo "$unit" > "$PWM_SYSFS_DIR/pwm${pwm_id}_falling"
        echo 1 > "$PWM_SYSFS_DIR/pwm${pwm_id}_en"
    fi
}

set_fan_pwm_5x() {
    pwm_id="$1"
    unit="$2"

    echo "$unit" > "${PWM_SYSFS_DIR}/pwm${pwm_id}"
}

set_fan_speed() {
    pwm_id="$1"
    percent="$2"
    unit=$((percent * PWM_UNIT_MAX / 100))

    if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
        set_fan_pwm_41 "$pwm_id" "$unit"
    else
        set_fan_pwm_5x "$pwm_id" "$unit"
    fi
}

if [ "$#" -ne 2 ] && [ "$#" -ne 1 ]; then
    usage
    exit 1
fi
PERCENT="$1"
FAN_ID="$2"

# refer to the comments in init_pwn.sh regarding
# the fan unit and PWM mapping
if [ "$#" -eq 1 ]; then
    FAN_PWMS="${FAN_PWM_MAP}"
else
    case "$FAN_ID" in
    "0")
        FAN_PWMS=$(echo "$FAN_PWM_MAP" | cut -d' ' -f1)
        ;;
    "1")
        FAN_PWMS=$(echo "$FAN_PWM_MAP" | cut -d' ' -f2)
        ;;
    "2")
        FAN_PWMS=$(echo "$FAN_PWM_MAP" | cut -d' ' -f3)
        ;;
    "3")
        FAN_PWMS=$(echo "$FAN_PWM_MAP" | cut -d' ' -f4)
        ;;
    *)
        usage
        exit 1
        ;;
    esac
fi

for fan_pwm in $FAN_PWMS; do
    fan_id=${fan_pwm%%:*}
    pwm_id=${fan_pwm##*:}

    set_fan_speed "$pwm_id" "$PERCENT"
    echo "Successfully set fan ${fan_id} (PWM: $pwm_id) speed to $percent%"
done


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-ipmid
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set IPMI Message handler
### END INIT INFO

echo -n "Setup IPMI message handler... "
/usr/local/bin/ipmid
echo "done."


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          sensor-setup
# Required-Start:    power-on
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO

. /usr/local/bin/openbmc-utils.sh

if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
    # Eventually, this will be used to configure the various (mostly
    # i2c-based) sensors, once we have a kernel version that supports
    # doing this more dynamically.
    #
    # For now, we're using it to install the lm75 and pmbus module so that it
    # can detect the fourth temperature sensor, which is located
    # on the uServer, which doesn't get power until power-on executes.
    #
    # Similarly, the pmbus sensor seems to have an easier time of
    # detecting the NCP4200 buck converters after poweron.  This has not
    # been carefully explored.

    modprobe lm75
    modprobe pmbus

    modprobe ast_adc

    # channel 5: r1:  1.0K; r2:  1.0K; v2: 1000mv
    # channel 6: r1:  1.0K; r2:  1.0K; v2: 1000mv
    # channel 7: r1:  1.0K; r2:  1.0K; v2: 0mv
    # channel 8: r1:  1.0K; r2:  1.0K; v2: 0mv
    # channel 9: r1:  1.0K; r2:  1.0K; v2: 0mv
    ast_config_adc 5  1 1 1000
    ast_config_adc 6  1 1 1000
    ast_config_adc 7  1 1 0
    ast_config_adc 8  1 1 0
    ast_config_adc 9  1 1 0

    # Enable the ADC inputs;  adc5 - adc9 should be connected to
    # 1V, 1.03V, 5V, 3.3V, and 2.5V.
    echo 1 > /sys/devices/platform/ast_adc.0/adc5_en
    echo 1 > /sys/devices/platform/ast_adc.0/adc6_en
    echo 1 > /sys/devices/platform/ast_adc.0/adc7_en
    echo 1 > /sys/devices/platform/ast_adc.0/adc8_en
    echo 1 > /sys/devices/platform/ast_adc.0/adc9_en
else
    #
    # Increase max127 (6-0028) input channel 6 (+5V voltage) max range
    # from default 5V to 10V (1000 mv).
    #
    max127_hwmon_dir="${SYSFS_I2C_DEVICES}/6-0028/hwmon"
    hwmon_idx=$(ls "$max127_hwmon_dir")
    in6_max_path="${max127_hwmon_dir}/${hwmon_idx}/in6_max"
    if [ -e "$in6_max_path" ]; then
        echo 10000 > "$in6_max_path"
    fi
fi


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-sms-kcs
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set SMS KCS handler
### END INIT INFO

echo -n "Setup SMS KCS message handler... "
/usr/local/bin/sms-kcsd
echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

usage() {
    echo "Usage: $0 [Fan Unit (1..5)]" >&2
}

FAN_DIR=$(i2c_device_sysfs_abspath 8-0033)

show_pwm()
{
    pwm="${FAN_DIR}/fantray${1}_pwm"
    val=$(cat $pwm | head -n 1)
    echo "$((val * 100 / 31))%"
}

show_rpm()
{
    front_rpm="${FAN_DIR}/fan$((($1 * 2 - 1)))_input"
    rear_rpm="${FAN_DIR}/fan$((($1 * 2)))_input"
    echo "$(cat $front_rpm), $(cat $rear_rpm)"
}

set -e

# refer to the comments in init_pwn.sh regarding
# the fan unit and tacho mapping
if [ "$#" -eq 0 ]; then
    FANS="1 2 3 4 5"
elif [ "$#" -eq 1 ]; then
    if [ $1 -gt 5 ]; then
        usage
        exit 1
    fi
    FANS="$1"
else
    usage
    exit 1
fi

for fan in $FANS; do
    echo "Fan $fan RPMs: $(show_rpm $fan), ($(show_pwm $fan))"
done


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-fan
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set fan speed
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup fan speed... "
/usr/local/bin/set_fan_speed.sh 50
runsv /etc/sv/fscd > /dev/null 2>&1 &
echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

usage() {
    echo "Usage: $0 <PERCENT (0..100)> <Fan Unit (1..5)> " >&2
}

FAN_DIR=$(i2c_device_sysfs_abspath 8-0033)

set -e

if [ "$#" -ne 2 ] && [ "$#" -ne 1 ]; then
    usage
    exit 1
fi

if [ "$#" -eq 1 ]; then
    FANS="1 2 3 4 5"
else
    FANS="$2"
fi

# Convert the percentage to our 1/32th unit (0-31).
unit=$(( ( $1 * 31 ) / 100 ))

for fan in $FANS; do
    pwm="${FAN_DIR}/fantray${fan}_pwm"
    echo "$unit" > $pwm
    echo "Successfully set fan ${fan} speed to $1%"
done


# Copyright 2015-present Facebook. All Rights Reserved.

SYSCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 12-0031)
FANCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 8-0033)
COME_SYSFS_DIR=$(i2c_device_sysfs_abspath 4-0033)

PWR_MAIN_SYSFS="${SYSCPLD_SYSFS_DIR}/pwr_main_n"
PWR_USRV_SYSFS="${SYSCPLD_SYSFS_DIR}/pwr_usrv_en"
PWR_USRV_BTN_SYSFS="${SYSCPLD_SYSFS_DIR}/pwr_usrv_btn_en"
SLOTID_SYSFS="${SYSCPLD_SYSFS_DIR}/slotid"

wedge_iso_buf_enable() {
    # TODO, no isolation buffer
    return 0
}

wedge_iso_buf_disable() {
    # TODO, no isolation buffer
    return 0
}

wedge_is_us_on() {
    local val n retries prog
    if [ $# -gt 0 ]; then
        retries="$1"
    else
        retries=1
    fi
    if [ $# -gt 1 ]; then
        prog="$2"
    else
        prog=""
    fi
    if [ $# -gt 2 ]; then
        default=$3              # value 0 means defaul is 'ON'
    else
        default=1
    fi
    val=$(cat $PWR_USRV_SYSFS 2> /dev/null | head -n 1)
    if [ -z "$val" ]; then
        return $default
    elif [ "$val" == "0x1" ]; then
        return 0            # powered on
    else
        return 1
    fi
}

wedge_board_type() {
    echo 'WEDGE100'
}

wedge_slot_id() {
    printf "%d\n" $(cat $SLOTID_SYSFS)
}

wedge_board_rev() {
    local val0 val1 val2
    val0=$(gpio_get_value BOARD_REV_ID0)
    val1=$(gpio_get_value BOARD_REV_ID1)
    val2=$(gpio_get_value BOARD_REV_ID2)
    echo $((val0 | (val1 << 1) | (val2 << 2)))
}

# Should we enable OOB interface or not
wedge_should_enable_oob() {
    # wedge100 uses BMC MAC since beginning
    return -1
}

wedge_power_on_board() {
    local val
    # power on main power, uServer power, and enable power button
    val=$(cat $PWR_MAIN_SYSFS | head -n 1)
    if [ "$val" != "0x1" ]; then
        echo 1 > $PWR_MAIN_SYSFS
        sleep 2
    fi
    val=$(cat $PWR_USRV_BTN_SYSFS | head -n 1)
    if [ "$val" != "0x1" ]; then
        echo 1 > $PWR_USRV_BTN_SYSFS
        sleep 1
    fi
}

#
# Helper function to check if AC PSU (#1 or #2) is present/plugged by
# reading syscpld "psu1|2_present" sysfs node.
#
# $1 - PSU ID/Index (1 or 2)
#
wedge_psu_is_present() {
    psu_id="$1"
    pathname="${SYSCPLD_SYSFS_DIR}/psu${psu_id}_present"

    # Return false if the according syscpld register is not accessible
    if ! val=$(head -n 1 "$pathname"); then
        return 1
    fi

    # Note: 0 means the PSU is present.
    if [ "$val" = "0x0" ]; then
        return 0
    fi

    return 1
}


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

CMD="${SYSCPLD_SYSFS_DIR}/i2c_flush_en"

echo -n "I2c flush to CP2112..."
echo 1 > $CMD
usleep 50000
echo 0 > $CMD
echo "Done"


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
set -e
set -o pipefail

. /usr/local/bin/openbmc-utils.sh

BASEDIR=${SYSCPLD_SYSFS_DIR}
if [[ $# -gt 0 && "$1" != "--fan" ]]; then
    echo "Prints SYS CPLD version information when called with no arguments" >&2
    echo "Prints FAN CPLD version if called with --fan" >&2
    echo "Usage: $0 [--fan]" >&2
    exit 2
fi

if [[ "$1" == "--fan" ]]; then
    BASEDIR=${FANCPLD_SYSFS_DIR}
fi
rev=$(head -n 1 ${BASEDIR}/cpld_rev)
sub_rev=$(head -n 1 ${BASEDIR}/cpld_sub_rev)

echo $(($rev)).$(($sub_rev))


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

set -x
set -e
set -u
set -o pipefail

upgrade_syscpld() {
  echo "Started SYSCPLD upgrade .."
  echo out > /tmp/gpionames/CPLD_JTAG_SEL/direction
  echo 1 > /tmp/gpionames/CPLD_JTAG_SEL/value
  sys_usercode=$(jbi -r -aREAD_USERCODE -gc102 -gi101 -go103 -gs100 "$1" |\
                 grep -i "USERCODE code is" | awk '{ print $6 }')
  new_usercode=$(jbi -i "$1" | grep -i "\"USERCODE"\" | awk -F '"' '{ print $4 }')
  if [[ $sys_usercode != "$new_usercode" ]];
  then
      #program syscpld
      jbi -r -aPROGRAM -dDO_REAL_TIME_ISP=1 -gc102 -gi101 -go103 -gs100 "$1"
      echo "Finished SYSCPLD upgrade: Pass"
  else
    echo "Finished SYSCPLD upgrade: No Action (Same usercode)"
  fi
}

upgrade_fancpld() {
  echo "Started FANCPLD upgrade .."
  echo out > /tmp/gpionames/CPLD_UPD_EN/direction
  echo 0 > /tmp/gpionames/CPLD_UPD_EN/value
  #get usercode from system
  sys_usercode=$(jbi -r -aREAD_USERCODE -gc77 -gi78 -go79 -gs76 "$1" |\
                 grep -i "USERCODE code is" | awk '{ print $6 }')
  #get usercode from file
  new_usercode=$(jbi -i "$1" | grep -i "\"USERCODE"\" | awk -F '"' '{ print $4 }')
  if [[ $sys_usercode != "$new_usercode" ]];
  then
      #program fancpld
      jbi -aPROGRAM -dDO_REAL_TIME_ISP=1 -gc77 -gi78 -go79 -gs76 "$1"
      echo "Finished FANCPLD upgrade: Pass"
  else
    echo "Finished FANCPLD upgrade: No Action (Same usercode)"
  fi
}

# Check the number of arguments provided, should get atleast 1 for file path
if [ $# -lt 1 ]; then
  echo "Provide FAN or SYSCPLD path"
  exit 1
fi

# Check the file path provided is a valid one.
jbcfile="$1"
if [[ ! -f $jbcfile ]]; then
    echo "$jbcfile does not exist"
    exit 1
fi

# Check the file path extension is .jbc
filename="$(basename "$jbcfile")"
if [[ ${filename: -4} != ".jbc" ]]; then
    echo "Must pass in a .jbc file"
    exit 1
fi

# Check the file name and upgrade accordingly
case $filename in
  *FAN*)
    upgrade_fancpld "$jbcfile"
    ;;
  *SYSCPLD*)
    upgrade_syscpld "$jbcfile"
    ;;
  *)
    echo "Provide FAN or SYSCPLD path"
    exit 1
    ;;
esac


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.Â  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

version=$(cat "${COME_SYSFS_DIR}/version")
released_date=$(cat "${COME_SYSFS_DIR}/date")

if [ "${version:(-4)}" == "0x80" ]; then
  echo "EC Test Version"
  echo "EC Released Date: $released_date"
else
  r=$((16#${version:15:2}))
  e=$((16#${version:22:2}))
  echo "EC Released Version: R$(printf '%02d' $r).E$(printf '%02d' $e)"
  echo "EC Released Date: $released_date"
fi


#!/bin/sh

. /usr/local/bin/openbmc-utils.sh

# select WDTRST
devmem_clear_bit $(scu_addr 2C) 1
devmem_set_bit $(scu_addr 84) 4


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
set -e
set -o pipefail
exit_code=2

. /usr/local/bin/openbmc-utils.sh

EEPROM_FILE="/tmp/.tmp_oob_eeprom.bin"

cleanup () {
  rm -f "$EEPROM_FILE"
  exit $exit_code
}
trap cleanup EXIT ERR INT TERM

if ! out=$(/usr/local/bin/spi_util.sh read spi2 OOB_SWITCH_EEPROM "$EEPROM_FILE" > /dev/null 2>&1); then
    echo "Error: failed to read the switch eeprom!"
    echo "$out"
    exit 1
fi

sha256sum < "$EEPROM_FILE" | head -c 64
exit_code=$?


#!/bin/sh
#
# Copyright 2004-present Facebook. All rights reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Displays values onto the debug header LEDs."
    echo "Hex and decimal accepted."
    echo "Usage: $0 <value>"
}

. /usr/local/bin/openbmc-utils.sh

display_value() {
    num=0
    input=$1
    while [ $num -lt 8 ]; do  {
        gpio_set_value "LED_POSTCODE_$num" $((input & 1))
        input=$((input >> 1))
        num=$((num + 1))
    }
    done
}

# Check number of parameters
if [ $# -ne 1 ]
then
    usage
    exit 1
fi

# Make sure input is actually numeric
DEC_VALUE=$(printf "%d" $1 2>/dev/null)
if [ $? -eq 1 ]
then
    echo "Unable to parse input as numeric value."
    exit 1
fi

# Make sure input is within proper range
if [ $DEC_VALUE -lt 0 ] || [ $DEC_VALUE -gt 255 ]
then
    echo "Value $DEC_VALUE is outside of displayable range 0 - 0xff (255)."
    exit 1
fi

display_value $DEC_VALUE


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO
. /usr/local/bin/openbmc-utils.sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# make power button high to prepare for power on sequence
gpio_set_value BMC_PWR_BTN_OUT_N 1

# First power on TH, and if Panther+ is used,
# provide standby power to Panther+.
wedge_power_on_board

echo -n "Checking microserver power status ... "
if wedge_is_us_on 10 "."; then
    echo "on"
    on=1
else
    echo "off"
    on=0
fi

if [ $on -eq 0 ]; then
    # Power on now
    wedge_power.sh on -f
fi


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

PWR_TH_RST_SYSFS="${SYSCPLD_SYSFS_DIR}/th_sys_rst_n"

echo 0 > $PWR_TH_RST_SYSFS
sleep 1
echo 1 > $PWR_TH_RST_SYSFS

logger -p user.crit "Reset Tomahawk"


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

CMD="${SYSCPLD_SYSFS_DIR}/usb2cp2112_rst_n"

echo -n "Reset CP2112 Switch ... "
echo 0 > $CMD
usleep 50000
echo 1 > $CMD

echo "Done"


#!/bin/bash
#
# Copyright 2020-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
set -e

. /usr/local/bin/openbmc-utils.sh

#
# We prefer writing to sysfs nodes (generated by syscpld driver) rather
# than "i2cset", because i2cset could potentially conflict with syscpld
# driver (who may be talking to 12-0031 at the same time).
#
FANCPLD_RST_N="${SYSCPLD_SYSFS_DIR}/fan_rackmon_rst_n"

echo "Reseting fancpld.."
if [ -f "$FANCPLD_RST_N" ]; then
    echo 0 > "$FANCPLD_RST_N"  # write 0 to trigger FAN_RACKMON card reset
    echo 1 > "$FANCPLD_RST_N"  # write 1 for normal operation
else
    echo "Warning: unable to locate $FANCPLD_RST_N: use i2cset instead.."
    i2cset -f -y 12 0x31 0x39 0xfe
    i2cset -f -y 12 0x31 0x39 0xff
fi

exit 0


#!/bin/bash
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

#
# This script resets the OOB switch BCM5387, and it's designed to reload
# the new settings from OOB switch EEPROM without power cycling the whole
# chassis.
#
# WARNING:
#   1. Both uServer and OpenBMC OOB connection would be down for a few
#      seconds when the command is executed.
#   2. DO NOT run the script from ssh terminal because of #1: run the
#      command from OpenBMC console instead.
#

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

SYSCPLD_OOB_RST="${SYSCPLD_SYSFS_DIR}/oob_bcm5387_rst_n"

echo "This command disrupts uServer and OpenBMC OOB connection!"
read -r -t 8 -p "Do you want to continue? [y/n] " user_input
if [ "$user_input" != "y" ] && [ "$user_input" != "Y" ] ; then
    exit 0
fi

if [ ! -L "${SHADOW_GPIO}/SWITCH_EEPROM1_WRT" ]; then
    gpio_export_by_name "$ASPEED_GPIO" GPIOE2 SWITCH_EEPROM1_WRT
fi
gpio_set_direction SWITCH_EEPROM1_WRT "in"

# The OOB switch enters reset state after below command!!!
logger -p user.crit "Reset OOB switch BCM5387.."
echo "Reset OOB switch BCM5387.."
echo 0 > "$SYSCPLD_OOB_RST"

# Now let's bring the OOB switch out of reset after small delay.
sleep 2
echo "Bring BCM5387 out of reset.."
echo 1 > "$SYSCPLD_OOB_RST"

gpio_unexport SWITCH_EEPROM1_WRT
exit 0


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

function reset_to_mux
{
  local num=0 val=$1
  while [ $num -lt 4 ]; do
    CMD="${SYSCPLD_SYSFS_DIR}/i2c_mux${num}_rst_n"
    echo $val > $CMD
    (( num++ ))
  done
}

echo -n "Reset QSFP i2c-mux ... "
reset_to_mux 0                  # write 0 to put mux into reset
usleep 50000
reset_to_mux 1                  # write 1 to bring mux out of reset
echo "Done"


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/bin/openbmc-utils.sh

echo -n "Reset USB Switch ... "

file="${SYSCPLD_SYSFS_DIR}/usb_hub_rst_n"

echo 0 > $file                  # write 0 to put hub into reset
sleep 1
echo 1 > $file                  # write 1 to bring hub out of reset

echo "Done"


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          setup_board.sh
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup the board
### END INIT INFO

. /usr/local/bin/openbmc-utils.sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

board_rev=$(wedge_board_rev)

if [ $board_rev -ge 2 ]; then
    # DVT or later
    # Enable the isolation buffer between BMC and COMe i2c bus
    echo 1 > ${SYSCPLD_SYSFS_DIR}/com-e_i2c_isobuf_en
    # Enable the COM6_BUF_EN to open the isolation buffer between COMe BIOS
    # EEPROM with COMe
    gpio_set_value COM6_BUF_EN 0
    # Make the BIOS EEPROM connect to COMe instead of BMC
    gpio_set_value COM_SPI_SEL 0
fi


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

#
# Create i2c switches at the beginning so all the channels are properly
# initialized when the child devices are created: the step is only needed
# for kernel 4.1 as i2c switches are handled in device tree for newer
# kernel versions.
#
# 7-0070 is the i2c-switch for ltc power supply in DC deployment. It has
# 8 child channels which are assigned i2c bus 14-21. 2 PFE1100 power
# supplies are connected to channel #0 (14-005a) and #1 (15-0059).
#
PSU_MUX_DEV="7-0070"
PSU_MUX_PATH=$(i2c_device_sysfs_abspath "$PSU_MUX_DEV")
if [ ! -d "$PSU_MUX_PATH" ]; then
    i2c_mux_add_sync 7 0x70 pca9548 8
fi

# Bus 2
i2c_device_add 2 0x3a pwr1014a

# Bus 3
i2c_device_add 3 0x48 tmp75
i2c_device_add 3 0x49 tmp75
i2c_device_add 3 0x4a tmp75
i2c_device_add 3 0x4b tmp75
i2c_device_add 3 0x4c tmp75

# Bus 4
i2c_device_add 4 0x33 com_e_driver

# Bus 6
i2c_device_add 6 0x2f pcf8574
i2c_device_add 6 0x51 24c64

# Bus 7
i2c_device_add 7 0x50 24c02         # BMC PHY EEPROM

# Bus 8
i2c_device_add 8 0x33 fancpld
i2c_device_add 8 0x48 tmp75
i2c_device_add 8 0x49 tmp75

# Bus 9   TPM driver for TCSD service
i2c_device_add 9 0x20 tpm_i2c_infineon

# Bus 12
i2c_device_add 12 0x31 syscpld

i2c_check_driver_binding