#!/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
#

# Get slot type
sh /usr/local/bin/check_slot_type.sh > /dev/NULL
SLOT_TYPE=$?
echo $SLOT_TYPE > /tmp/slot.bin

# do init power
echo "[ minilakeTB ] : Start setup PCA9555"
do_init_power=0;

ret=$(i2cget -y 12 0x20  0x02 w)
if [ "$ret" != "0xd7fd" ]; then
    do_init_power=1;
fi

ret=$(i2cget -y 12 0x20  0x06 w)
if [ "$ret" != "0xd18d" ]; then
    do_init_power=1;
fi

if [ "$do_init_power" -eq "1" ]; then
    # set 0x02 output register, 0xFD port0, 0xD7 port1
    i2cset -y 12 0x20  0x02 0xFD 0xD7 i
    # set 0x06 dir register, 0xFD port0, 0xD3 port1
    i2cset -y 12 0x20  0x06 0xFD 0xD3 i
    usleep 10000;

    # set P0V9 as high
    i2cset -y 12 0x20  0x06 0xDD 0xD3 i
    usleep 10000;

    # set P1V8 as high
    i2cset -y 12 0x20  0x06 0x9D 0xD3 i
    usleep 10000;

    # set OSC_OE as high
    i2cset -y 12 0x20  0x06 0x9D 0xD1 i
    usleep 10000;

    # set RST_PLT_MEZZ_N as high
    i2cset -y 12 0x20  0x06 0x8D 0xD1 i
    usleep 10000;

    echo "[ minilakeTB ] : setup PCA9555 finish !!!"
else
    echo "[ minilakeTB ] : no need to setup PCA9555 !!!"
fi

# get MAC from EEPROM and set to BMC.
SetMAC() {
    rmmod at24
    i2cset -y 8 0x51 0x18 0x10 || return 1;

    MAC=$(i2cget -y 8 0x51)|| return 1;
    MAC=$MAC':'$(i2cget -y 8 0x51)|| return 1;
    MAC=$MAC':'$(i2cget -y 8 0x51)|| return 1;
    MAC=$MAC':'$(i2cget -y 8 0x51)|| return 1;
    MAC=$MAC':'$(i2cget -y 8 0x51)|| return 1;
    MAC=$MAC':'$(i2cget -y 8 0x51)|| return 1;

    MAC=$(echo $MAC | sed 's/0x//g');
    #ifdown eth0
    ifconfig eth0 hw ether $MAC
    #ifup eth0
    modprobe at24
    return 0
}

retry=5;
while [[ $retry -gt 0 ]]; do
    SetMAC
    if [ $? -eq 0 ]; then
        echo setup MAC finish !
        exit 0
    else
        retry=$(($retry-1))
    fi
done
# Finish MAC setting


#!/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
#

# Get server type
sh /usr/local/bin/check_server_type.sh > /dev/NULL
SERVER_TYPE=$?
echo $SERVER_TYPE > /tmp/server_type.bin


# This file contains definitions for the different system configurations.
# We should probably move some more of the definitions to this file at some point.

echo -n "Setup System Configuration .."

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

SPB_REV=$(($(($(($(gpio_get_val Y2) + $(gpio_get_val Y1))) * 2)) + $(gpio_get_val Y0)))
echo $SPB_REV > /tmp/spb_rev

if [ $(is_server_prsnt 2) == "1" ] ; then
   if [ $(get_slot_type 2) != "0" ] ; then
      gpio_set O5 0
      logger -p user.crit "Invalid configuration on SLOT2"
      if [ $(is_server_prsnt 1) == "1" ] && [ $(get_slot_type 1) != "0" ] ; then
         gpio_set O4 0
      fi
   fi
else
   if [ $(is_server_prsnt 1) == "1" ] && [ $(get_slot_type 1) != "0" ] ; then
      gpio_set O4 0
   fi
fi

if [ $(is_server_prsnt 4) == "1" ] ; then
   if [ $(get_slot_type 4) != "0" ] ; then
      gpio_set O7 0
      logger -p user.crit "Invalid configuration on SLOT4"
      if [ $(is_server_prsnt 3) == "1" ] && [ $(get_slot_type 3) != "0" ] ; then
         gpio_set O6 0
      fi
   fi
else
   if [ $(is_server_prsnt 3) == "1" ] && [ $(get_slot_type 3) != "0" ] ; then
      gpio_set O6 0
   fi
fi

# Enable buffer to pass through signal by system configuration
if [ $(is_server_prsnt 1) == "1" ] && [ $(is_server_prsnt 2) == "1" ] ; then
   if [ $(get_slot_type 1) != "0" ] && [ $(get_slot_type 2) == "0" ] ; then
      gpio_set J0 0
      gpio_set B4 0
      gpio_set B5 0
   else
      gpio_set B4 1
      gpio_set B5 1
   fi
else
   gpio_set B4 1
   gpio_set B5 1
fi

if [ $(is_server_prsnt 3) == "1" ] && [ $(is_server_prsnt 4) == "1" ] ; then
   if [ $(get_slot_type 3) != "0" ] && [ $(get_slot_type 4) == "0" ] ; then
      gpio_set J1 0
      gpio_set B6 0
      gpio_set B7 0
   else
      gpio_set B6 1
      gpio_set B7 1
   fi
else
   gpio_set B6 1
   gpio_set B7 1
fi

# create devices on I2C bus 1 and bus 5
# Bus1,  Glacier Point
if [[ $(is_server_prsnt 1) == "1" && $(get_slot_type 1) != "0" ]]; then

   devmem 0x1e78a084 w 0xFFF77304

   if [ $(get_slot_type 1) == "2" ]; then
      # I2C mux, 0xE2
      echo pca9551 0x71 > /sys/class/i2c-dev/i2c-1/device/new_device
      # I2C GPIO, 0x40
      echo pca9551 0x20 > /sys/class/i2c-dev/i2c-1/device/new_device
   fi

   # EEPROM, 0xA2
   echo 24c128 0x51 > /sys/class/i2c-dev/i2c-1/device/new_device
   # Inlet temp sensor, 0x9A
   echo tmp75 0x4d > /sys/class/i2c-dev/i2c-1/device/new_device
   # outlet temp sensor, 0x9C
   echo tmp75 0x4e > /sys/class/i2c-dev/i2c-1/device/new_device
   # Voltage sensor, 0x80
   echo ina230 0x40 > /sys/class/i2c-dev/i2c-1/device/new_device
else
   devmem 0x1e78a084 w 0xFFF5E700
fi

# Bus5,  Glacier Point
if [[ $(is_server_prsnt 3) == "1" && $(get_slot_type 3) != "0" ]]; then

   devmem 0x1e78a184 w 0xFFF77304

   if [ $(get_slot_type 3) == "2" ]; then
      # I2C mux, 0xE2
      echo pca9551 0x71 > /sys/class/i2c-dev/i2c-5/device/new_device
      # I2C GPIO, 0x40
      echo pca9551 0x20 > /sys/class/i2c-dev/i2c-5/device/new_device
   fi

   # EEPROM, 0xA2
   echo 24c128 0x51 > /sys/class/i2c-dev/i2c-5/device/new_device
   # Inlet temp sensor, 0x9A
   echo tmp75 0x4d > /sys/class/i2c-dev/i2c-5/device/new_device
   # outlet temp sensor, 0x9C
   echo tmp75 0x4e > /sys/class/i2c-dev/i2c-5/device/new_device
   # Voltage sensor, 0x80
   echo ina230 0x40 > /sys/class/i2c-dev/i2c-5/device/new_device
else
   devmem 0x1e78a184 w 0xFFF5E700
fi

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
#

# 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/fbpackages/utils/ast-functions

# 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=$(cat /sys/class/gpio/gpio58/value 2>/dev/null)
    val1=$(cat /sys/class/gpio/gpio56/value 2>/dev/null)
    val2=$(cat /sys/class/gpio/gpio57/value 2>/dev/null)
    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

# We shouldn't have to rmmod pmbus, because it hasn't been loaded yet,
# but if the script is rerun after the system is up, it may be necessary.
rmmod pmbus
reload=$?

# Get current voltage value
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"
    echo 1 > /sys/class/gpio/gpio42/value
    echo 1 > /sys/class/gpio/gpio43/value
    echo out > /sys/class/gpio/gpio42/direction
    echo out > /sys/class/gpio/gpio43/direction
    echo 0 > /sys/class/gpio/gpio16/value
    echo out > /sys/class/gpio/gpio16/direction
    # 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
    echo 1 > /sys/class/gpio/gpio16/value
    echo "T2 ROV value set based on $rov."
    sleep 2
    echo 0 > /sys/class/gpio/gpio42/value
    echo 0 > /sys/class/gpio/gpio43/value
else
    echo "T2 ROV already correctly set."
fi
# Bring back pmbus if necessary
if [ $reload -eq 0 ]; then
    modprobe pmbus
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
#

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

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

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

gpio_set 32 $VALUE


#!/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/fbpackages/utils/ast-functions

MAC1LINK=0    # GPIOA0
FAB0_PRES=20  # GPIOC4
FAB1_PRES=21  # GPIOC5
while true; do
  # fabN_pres: active low.
  if [ $(gpio_get $FAB0_PRES) = 0 ]; then
    gpio_set $MAC1LINK 0
  elif [ $(gpio_get $FAB1_PRES) = 0 ]; then
    gpio_set $MAC1LINK 1
  fi
  sleep 1
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
#

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
. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup sensor monitoring for minilaketb... "

# Check for the slots present and run sensord for those slots only.
SLOTS=
  if [ $(is_server_prsnt 1) == "1" ]; then
    if [ $(is_slot_12v_on 1) == "1" ]; then
      if [[ $(get_slot_type 1) != "0" || $(is_bic_ready 1) == "1" ]]; then
        SLOTS="$SLOTS slot1"
      fi
    else
      SLOTS="$SLOTS slot1"
    fi
  fi

SLOTS="$SLOTS spb"
exec /usr/local/bin/sensord $SLOTS


#!/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
#

### BEGIN INIT INFO
# Provides:          setup-sensord
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

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

echo -n "Setup sensor monitoring for minilaketb... "
runsv /etc/sv/sensord > /dev/null 2>&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
#

### 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

# 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

# Enable the ADC inputs;  adc0 - adc15 are connected to various voltage sensors
# adc12 - adc15 are for SLOT_TYPE

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

# setup ADC channels

ADC_PATH="/sys/devices/platform/ast_adc.0"
# For MINILAKETB tyep V:
# channel 0:  r1:  5.36K; r2:  2.0K; v2: 0mv
# channel 1:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 2:  r1:  2.87K; r2:  2.0K; v2: 0mv
# channel 3:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 4:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 5:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 6:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 7:  r1:  2.87K; r2:  2.0K; v2: 0mv
# channel 8:  r1:     1K; r2:  0.0K; v2: 0mv
# channel 9:  r1:     1K; r2:  0.0K; v2: 0mv
# channel 10: r1:  1.69K; r2:  2.0K; v2: 0mv
# channel 11: r1:    665; r2:  2.0K; v2: 0mv
# channel 12: r1:     1K; r2:    0K; v2: 0mv
# channel 13: r1:     1K; r2:    0K; v2: 0mv
# channel 14: r1:     1K; r2:    0K; v2: 0mv
# channel 15: r1:     1K; r2:    0K; v2: 0mv

config_adc() {
    channel=$1
    r1=$2
    r2=$3
    v2=$4
    echo $r1 > ${ADC_PATH}/adc${channel}_r1
    echo $r2 > ${ADC_PATH}/adc${channel}_r2
    echo $v2 > ${ADC_PATH}/adc${channel}_v2
    echo 1 > ${ADC_PATH}/adc${channel}_en
}

config_adc 0  5360  2000 0
config_adc 1 15800  2000 0
config_adc 2  2870  2000 0
config_adc 3 15800  2000 0
config_adc 4 15800  2000 0
config_adc 5 15800  2000 0
config_adc 6 15800  2000 0
config_adc 7  2870  2000 0
config_adc 8  	 0  1000 0
config_adc 9  	 0  1000 0
config_adc 10 1690  2000 0
config_adc 11  665  2000 0
config_adc 12    0  1000 0
config_adc 13    0  1000 0
config_adc 14    0  1000 0
config_adc 15    0  1000 0


#!/bin/bash

SLOT_NAME=$1

case $SLOT_NAME in
    scm)
      SLOT_NUM=1
      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      echo "Usage: $N {scm}"
      exit 1
      ;;
esac

# File format autodump<slot_id>.pid (See pal_is_crashdump_ongoing()
# function definition)
PID_FILE="/var/run/autodump$SLOT_NUM.pid"

# check if auto crashdump is already running
if [ -f $PID_FILE ]; then
  echo "Another auto crashdump for $SLOT_NAME is runnung"
  exit 1
else
  touch $PID_FILE
fi

# Set crashdump timestamp
sys_runtime=$(awk '{print $1}' /proc/uptime)
sys_runtime=$(printf "%0.f" $sys_runtime)
echo $((sys_runtime+630)) > /tmp/cache_store/fru${SLOT_NUM}_crashdump

DUMP_SCRIPT="/usr/local/bin/dump.sh"
CRASHDUMP_FILE="/mnt/data/crashdump_$SLOT_NAME"
CRASHDUMP_LOG_ARCHIVE="/mnt/data/crashdump_$SLOT_NAME.tar.gz"

echo "Auto Dump for $SLOT_NAME Started"

#HEADER LINE for the dump
$DUMP_SCRIPT "time" > $CRASHDUMP_FILE
#COREID dump
$DUMP_SCRIPT $SLOT_NAME "coreid" >> $CRASHDUMP_FILE
#MSR dump
$DUMP_SCRIPT $SLOT_NAME "msr" >> $CRASHDUMP_FILE
# Sensors & sensor thresholds
echo "Sensor history at dump:" >> $CRASHDUMP_FILE 2>&1
$DUMP_SCRIPT $SLOT_NAME "sensors" >> $CRASHDUMP_FILE
echo "Sensor threshold at dump:" >> $CRASHDUMP_FILE 2>&1
$DUMP_SCRIPT $SLOT_NAME "threshold" >> $CRASHDUMP_FILE

tar zcf $CRASHDUMP_LOG_ARCHIVE -C `dirname $CRASHDUMP_FILE` `basename $CRASHDUMP_FILE` && \
rm -rf $CRASHDUMP_FILE && \
logger -t "ipmid" -p daemon.crit "Crashdump for $SLOT_NAME is generated at $CRASHDUMP_LOG_ARCHIVE"

echo "Auto Dump for $SLOT_NAME Completed"

rm $PID_FILE
exit 0


#!/bin/bash

BIC_UTIL="/usr/bin/bic-util"
SENSOR_HISTORY=180

if [ "$#" -eq 1 ] && [ $1 = "time" ]; then
  now=$(date)
  echo "Crash Dump generated at $now"
  exit 0
fi

if [ "$#" -ne 2 ]; then
  echo "$0 <slot#> coreid    ==> for CPU CoreID"
  echo "$0 <slot#> msr       ==> for CPU MSR"
  echo "$0 <slot#> sensors   ==> for sensor history"
  echo "$0 <slot#> threshold ==> for sensor threshold"
  exit 1
fi

echo ""

if [ "$2" = "coreid" ]; then
  [ -r /usr/local/fbpackages/crashdump/crashdump_coreid ] && $BIC_UTIL $1 --me_file /usr/local/fbpackages/crashdump/crashdump_coreid
elif [ "$2" = "msr" ]; then
  [ -r /usr/local/fbpackages/crashdump/crashdump_msr ] && $BIC_UTIL $1 --me_file /usr/local/fbpackages/crashdump/crashdump_msr
elif [ "$2" = "sensors" ]; then
  /usr/local/bin/sensor-util all --history $SENSOR_HISTORY
elif [ "$2" = "threshold" ]; then
  /usr/local/bin/sensor-util all --threshold
fi

exit 0


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
### BEGIN INIT INFO
# Provides:          setup-front-paneld
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Start front panel control daemon
### END INIT INFO

echo -n "Setup Front Panel Daemon.."
runsv /etc/sv/front-paneld > /dev/null 2>&1 &
echo "done."


#! /bin/sh
#
# Copyright 2018-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:          ipmbd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Provides ipmb message tx/rx service
#
### END INIT INFO

echo "Starting IPMB Rx/Tx Daemon"

ulimit -q 1024000
runsv /etc/sv/ipmbd_0 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_4 > /dev/null 2>&1 &


#!/bin/sh
. /usr/local/fbpackages/utils/ast-functions

ARGS="scm smb pim1 pim2 pim3 pim4 pim5 pim6 pim7 pim8 psu1 psu2 psu3 psu4"
exec /usr/local/bin/sensord $ARGS


#!/bin/sh
#
# Copyright 2018-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-sensord
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

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

echo -n "Setup sensor monitoring for MINIPACK... "

runsv /etc/sv/sensord > /dev/null 2>&1 &

echo "done."


#!/bin/sh
#
# Copyright 2018-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

# 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


#!/bin/sh

### BEGIN INIT INFO
# Provides:		tcsd trousers
# Required-Start:	$local_fs $remote_fs $network
# Required-Stop:	$local_fs $remote_fs $network
# Should-Start:
# Should-Stop:
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	starts tcsd
# Description:		tcsd belongs to the TrouSerS TCG Software Stack
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/tcsd
NAME=tcsd
DESC="Trusted Computing daemon"
USER="root"

test -x "${DAEMON}" || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

case "${1}" in
	start)
		echo "Starting $DESC: "

    # If TPM chip has not been discovered, probe the chip here
		if [ ! -e /dev/tpm* ]
		then
      # Finally, register the device and install the driver
      . /usr/local/bin/openbmc-utils.sh
      i2c_device_add 7 0x20 tpm_i2c_infineon
    fi

    # Additionally, Generate symlink to tpm0 or tpm1, so that tcsd can use it
    rm -f /dev/tpm
		if [ -e /dev/tpm0 ]
    then
      ln -s /dev/tpm0 /dev/tpm
    else
      ln -s /dev/tpm1 /dev/tpm
    fi

		start-stop-daemon --start --quiet --oknodo --pidfile /var/run/${NAME}.pid --user ${USER} --chuid ${USER} --exec ${DAEMON} -- ${DAEMON_OPTS}
		RETVAL="$?"
		echo "$NAME."
		[ "$RETVAL" = 0 ] && pidof $DAEMON > /var/run/${NAME}.pid
		exit $RETVAL
		;;

	stop)
		echo "Stopping $DESC: "

		start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/${NAME}.pid --user ${USER} --exec ${DAEMON}
		RETVAL="$?"
                echo  "$NAME."
		rm -f /var/run/${NAME}.pid
		exit $RETVAL
		;;

	restart|force-reload)
		"${0}" stop
		sleep 1
		"${0}" start
		exit $?
		;;
	*)
		echo "Usage: ${NAME} {start|stop|restart|force-reload|status}" >&2
		exit 3
		;;
esac

exit 0


#!/bin/sh
#
# Copyright 2018-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

fcm_b_ver=`head -n1 ${BOTTOM_FCMCPLD_SYSFS_DIR}/cpld_ver 2> /dev/null`
fcm_t_ver=`head -n1 ${TOP_FCMCPLD_SYSFS_DIR}/cpld_ver 2> /dev/null`

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

# Top FCM    (BUS=64) PWM 1 ~ 4 control Fantray 1 3 5 7
# Bottom FCM (BUS=72) PWM 1 ~ 4 control Fantray 2 4 6 8
show_pwm()
{
    pwm="$(i2c_device_sysfs_abspath $1-0033)/fantray$2_pwm"
    val=$(cat $pwm | head -n 1)

    if [ "$fcm_b_ver" == "0x0" ] || [ "$fcm_t_ver" == "0x0" ]; then
        echo "$((val * 100 / 31))%"
    else
        echo "$((val * 100 / 63))%"
    fi
}

show_rpm()
{
    front_rpm="$(i2c_device_sysfs_abspath $1-0033)/fan$((($2 * 2 - 1)))_input"
    rear_rpm="$(i2c_device_sysfs_abspath $1-0033)/fan$((($2 * 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 6 7 8"
elif [ "$#" -eq 1 ]; then
    if [ "$1" -le 0 ] || [ "$1" -ge 9 ]; then
        echo "Fan $1: not a valid Fan Unit"
        exit 1
    fi
    FANS="$1"
else
    usage
    exit 1
fi

for fan in $FANS; do
    FAN="${fan}"

    if [ $(($fan % 2)) -eq 0 ]; then
      BUS="72"
      UNIT=$((${FAN} / 2 ))
    else
      BUS="64"
      FAN=$((${FAN} / 2 ))
      UNIT=$((${FAN} + 1))
    fi

    echo "Fan $fan RPMs: $(show_rpm $BUS $UNIT), ($(show_pwm $BUS $UNIT))"
done


#!/bin/sh
#
# Copyright 2018-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
. /usr/local/fbpackages/utils/ast-functions

default_fsc_config="/etc/fsc-config.json"
fcm_b_ver=`head -n1 ${BOTTOM_FCMCPLD_SYSFS_DIR}/cpld_ver \
           2> /dev/null`
fcm_b_sub_ver=`head -n1 ${BOTTOM_FCMCPLD_SYSFS_DIR}/cpld_sub_ver \
           2> /dev/null`
fcm_t_ver=`head -n1 ${TOP_FCMCPLD_SYSFS_DIR}/cpld_ver \
           2> /dev/null`
fcm_t_sub_ver=`head -n1 ${TOP_FCMCPLD_SYSFS_DIR}/cpld_sub_ver \
           2> /dev/null`
fcm_b_maj=`echo $fcm_b_ver | awk '{printf "%d", $1;}'`
fcm_b_min=`echo $fcm_b_sub_ver | awk '{printf "%d", $1;}'`
fcm_t_maj=`echo $fcm_t_ver | awk '{printf "%d", $1;}'`
fcm_t_min=`echo $fcm_t_sub_ver | awk '{printf "%d", $1;}'`
echo -n "Setup fan speed... "

if [ "$fcm_b_ver" == "0x0" ] || [ "$fcm_t_ver" == "0x0" ]; then
    echo "Run FSC PWM 32 Levels Config"
    cp /etc/FSC-PWM-32-config.json ${default_fsc_config}
else
    echo "Run FSC PWM 64 Levels Config"
    cp /etc/FSC-PWM-64-config.json ${default_fsc_config}
fi
echo "Setting fan speed to 50%..."
/usr/local/bin/set_fan_speed.sh 50
echo "Done setting fan speed"
# Currently, fcmcpld version 0.xx will cause BMC to
# run into crash loop, due to fscd's own logic and
# its failure to disarm bootup watchdog.
# (this problem is only for fcmcpld 0.xx. Fcmcpld 1.9 and
# above works well without any problem.)
# Until we have fix, we will run fscd only if fcmcpld is 1.9 or higher.
fcm_b_compatible=0
fcm_t_compatible=0
if [ $fcm_b_maj -eq 1 ] && [ $fcm_b_min -ge 9 ]; then
   fcm_b_compatible=1
fi
if [ $fcm_b_maj -ge 2 ]; then
   fcm_b_compatible=1
fi
if [ $fcm_t_maj -eq 1 ] && [ $fcm_t_min -ge 9 ]; then
   fcm_t_compatible=1
fi
if [ $fcm_b_maj -ge 2 ]; then
   fcm_t_compatible=1
fi
echo fcm_b version : $fcm_b_maj $fcm_b_min compatible: $fcm_b_compatible
echo fcm_t version : $fcm_t_maj $fcm_t_min compatible: $fcm_t_compatible
if [ $fcm_b_compatible -eq 0 ] || [ $fcm_t_compatible -eq 0 ]; then
    echo "Old FCM CPLD detected. Running fan at the fixed speed."
    /usr/local/bin/wdtcli stop
else
    echo "Starting fscd..."
    runsv /etc/sv/fscd > /dev/null 2>&1 &
fi
echo "done."


#!/bin/sh
#
# Copyright 2018-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

fcm_b_ver=`head -n1 ${BOTTOM_FCMCPLD_SYSFS_DIR}/cpld_ver 2> /dev/null`
fcm_t_ver=`head -n1 ${TOP_FCMCPLD_SYSFS_DIR}/cpld_ver 2> /dev/null`

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

set -e

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

# Top FCM    (BUS=64) PWM 1 ~ 4 control Fantray 1 3 5 7
# Bottom FCM (BUS=72) PWM 1 ~ 4 control Fantray 2 4 6 8
BUS="64 72"
if [ "$#" -eq 1 ]; then
    FAN_UNIT="1 2 3 4"
else
    if [ "$2" -le 0 ] || [ "$2" -ge 9 ]; then
        echo "Fan $2: not a valid Fan Unit"
        exit 1
    fi

    FAN="$2"

    if [ $(($FAN % 2)) -eq 0 ]; then
      BUS="72"
      FAN_UNIT=$((${FAN} / 2 ))
    else
      BUS="64"
      FAN=$((${FAN} / 2 ))
      FAN_UNIT=$((${FAN} + 1))
    fi
fi

if [ "$fcm_b_ver" == "0x0" ] || [ "$fcm_t_ver" == "0x0" ]; then
    # Convert the percentage to our 1/32th level (0-31).
    step=$((($1 * 31) / 100))
else
    # Convert the percentage to our 1/64th level (0-63).
    step=$((($1 * 63) / 100))
fi
cnt=1

for bus in ${BUS}; do
    for unit in ${FAN_UNIT}; do
        pwm="$(i2c_device_sysfs_abspath ${bus}-0033)/fantray${unit}_pwm"
        echo "$step" > "${pwm}"

    if [ "$#" -eq 1 ]; then
        echo "Successfully set fan $cnt speed to $1%"
        cnt=$(($cnt+1))
    else
        echo "Successfully set fan $2 speed to $1%"
    fi
    done
done


# Copyright 2018-present Facebook. All Rights Reserved.

#
# I2C paths of CPLDs
#
SCMCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 2-0035)
SMBCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 12-003e)
TOP_FCMCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 64-0033)
BOTTOM_FCMCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 72-0033)
LEFT_PDBCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 55-0060)
RIGHT_PDBCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 63-0060)

#
# I2C paths of FPGAs
#
IOBFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 13-0035)
PIM1_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 80-0060)
PIM2_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 88-0060)
PIM3_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 96-0060)
PIM4_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 104-0060)
PIM5_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 112-0060)
PIM6_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 120-0060)
PIM7_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 128-0060)
PIM8_DOMFPGA_SYSFS_DIR=$(i2c_device_sysfs_abspath 136-0060)

PWR_USRV_SYSFS="${SCMCPLD_SYSFS_DIR}/com_exp_pwr_enable"
PWR_TH_RST_SYSFS="${SMBCPLD_SYSFS_DIR}/cpld_mac_reset_n"

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

    val=$(cat $PWR_USRV_SYSFS 2> /dev/null | head -n 1)
    if [ "$val" == "0x1" ]; then
        return 0            # powered on
    else
        return 1
    fi

    return 0
}

wedge_board_type() {
    echo 'MINIPACK'
}

wedge_slot_id() {
    printf "1\n"
}

wedge_board_rev() {
    local val0 val1 val2
    val0=$(gpio_get_value BMC_CPLD_BOARD_REV_ID0)
    val1=$(gpio_get_value BMC_CPLD_BOARD_REV_ID1)
    val2=$(gpio_get_value BMC_CPLD_BOARD_REV_ID2)
    echo $((val0 | (val1 << 1) | (val2 << 2)))
}

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

wedge_power_on_board() {
    # minipack SCM power is controlled by SCMCPLD
    return 0
}


#!/bin/bash
#
# Copyright 2018-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
exitCode=0

dump_cpld_version() {
    local cpld_dir=$1
    local cpld_name=$2

    cpld_ver=`head -n 1 ${cpld_dir}/cpld_ver 2> /dev/null`
    if [ $? -ne 0 ]; then
        echo "${cpld_name} is not detected"
        exitCode=1
    fi
    cpld_sub_ver=`head -n 1 ${cpld_dir}/cpld_sub_ver 2> /dev/null`
    if [ $? -ne 0 ]; then
        echo "${cpld_name} is not detected"
        exitCode=1
    fi

    if [ "$exitCode" -eq 0 ]; then
      echo "${cpld_name}: $(($cpld_ver)).$(($cpld_sub_ver))"
    fi
}

dump_cpld_version ${SMBCPLD_SYSFS_DIR} "SMBCPLD"
usleep 50000

dump_cpld_version ${SCMCPLD_SYSFS_DIR} "SCMCPLD"
usleep 50000

dump_cpld_version ${TOP_FCMCPLD_SYSFS_DIR} "Top FCMCPLD"
usleep 50000

dump_cpld_version ${BOTTOM_FCMCPLD_SYSFS_DIR} "Bottom FCMCPLD"
usleep 50000

board_rev=$(wedge_board_rev)
if [ $board_rev -ne 4 ]; then
    dump_cpld_version ${LEFT_PDBCPLD_SYSFS_DIR} "Left PDBCPLD"
    usleep 50000

    dump_cpld_version ${RIGHT_PDBCPLD_SYSFS_DIR} "Right PDBCPLD"
fi

if [ "$exitCode" -ne 0 ]; then
  echo "since all the CPLDs didn't succeed with the  name listed above... so exiting"
  exit 1
fi


#!/bin/bash
#
# Copyright 2018-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

for index in 1 2 3 4 5 6 7 8
do
    pim_path=${SMBCPLD_SYSFS_DIR}/pim_fpga_cpld_${index}_prsnt_n_status
    pim_prsnt=$(cat $pim_path 2> /dev/null | head -n 1)
    cache_file="/tmp/pim${index}_serial.txt"

    # Refresh pimserial cache file when
    #   1) the pim is present, and
    #   2) the cache file doesn't exist or it doesn't contain valid strings
    if [ ! -s "$cache_file" ] || [ -z "$(cat $cache_file)" ]; then
        if [ "${pim_prsnt}" == 0x0 ]; then
            /usr/local/bin/peutil $index |grep Product|grep Serial|cut -d ' ' -f 4 > "$cache_file"
        fi
    fi
    serial="$(cat $cache_file)"
    echo PIM${index} : ${serial}
done


#!/bin/bash
#
# Copyright 2018-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

SMBCPLD=/sys/bus/i2c/devices/i2c-12/12-003e

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

img="$1"

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

top_fan_cpld="${SMBCPLD}/smb_cpld_fcm_b_cpld_sel"
bottom_fan_cpld="${SMBCPLD}/smb_cpld_fcm_b_cpld_sel"

trap 'gpio_set_value BMC_FCM_T_MUX_SEL 0; \
      gpio_set_value BMC_FCM_B_MUX_SEL 0; \
      echo 0 > "${top_fan_cpld}"; \
      echo 0 > "${bottom_fan_cpld}"; \
      rm -rf /tmp/fcmcpld_update' INT TERM QUIT EXIT

# change BMC_FCM_T_MUX_SEL and smb_cpld_fcm_t_cpld_sel to 1
# to connect BMC to Top FANCPLD pins
gpio_set_value BMC_FCM_T_MUX_SEL 1
echo 1 > "${top_fan_cpld}"

# change BMC_FCM_B_MUX_SEL and smb_cpld_fcm_t_cpld_sel to 1
# to connect BMC to Bottom FANCPLD pins
gpio_set_value BMC_FCM_B_MUX_SEL 1
echo 1 > "${bottom_fan_cpld}"

echo 1 > /tmp/fcmcpld_update

ispvm dll /usr/lib/libcpldupdate_dll_gpio.so "${img}" \
    --tms BMC_FCM_CPLD_TMS \
    --tdo BMC_FCM_CPLD_TDI \
    --tdi BMC_FCM_CPLD_TDO \
    --tck BMC_FCM_CPLD_TCK
result=$?
# 1 is returned upon upgrade success
if [ $result -eq 1 ]; then
    echo "Upgrade successful."
    exit 0
else
    echo "Upgrade failure. Return code from utility : $result"
    exit 1
fi


#!/bin/bash
#
# Copyright 2018-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
exitCode=0

dump_fpga_version() {
    local fpga_dir=$1
    local fpga_name=$2

    fpga_ver=`head -n 1 ${fpga_dir}/fpga_ver 2> /dev/null`
    if [ $? -ne 0 ]; then
        echo "${fpga_name} is not detected"
        exit 1

    fi
    fpga_sub_ver=`head -n 1 ${fpga_dir}/fpga_sub_ver 2> /dev/null`
    if [ $? -ne 0 ]; then
        echo "${fpga_name} is not detected"
        exit 1
    fi

    echo "${fpga_name}: $(($fpga_ver)).$(($fpga_sub_ver))"
}

echo "------IOBFPGA------"
dump_fpga_version ${IOBFPGA_SYSFS_DIR} "IOBFPGA"

echo "------DOMFPGA------"
for num in $(seq 8); do
    pim_var_name="PIM${num}_DOMFPGA_SYSFS_DIR"
    pim_fpga_dir=${!pim_var_name}

    echo "PIM $num:"

    pim_type=`head -n1 ${pim_fpga_dir}/board_ver 2> /dev/null`
    if [ "${pim_type}" == "0x0" ]; then
        dump_fpga_version ${pim_fpga_dir} "16Q DOMFPGA"
    elif [ "$((pim_type & 0xf0))" == "$((0xf0))" ]; then
        dump_fpga_version ${pim_fpga_dir} "16O DOMFPGA"
    elif [ "${pim_type}" == "0x10" ]; then
        dump_fpga_version ${pim_fpga_dir} "4DD DOMFPGA"
    else
        echo "DOMFPGA is not detected or PIM $num is not inserted"
        exitCode=1
    fi
    usleep 50000
done

if [ "$exitCode" -ne 0 ]; then
    echo "Not all DOMFPGA or PIM were detected/inserted. Please review the logs above.... exiting"
    exit 1
fi


#!/bin/bash
#
# Copyright 2018-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

board_rev=$(wedge_board_rev)

usage(){
    program=`basename "$0"`
    echo "Usage:"
    echo "     $program jtag <Location> <*.vme>"
    echo "     $program i2c <Location> <*.hex>"
    echo "     <Location>  : left or right"
}

if [ $board_rev -eq 4 ]; then
    echo "EVTA hardware doesn't have PDBCPLD"
    exit -1
fi

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

if [ "$2" = "left" ]; then
    IO_BUS=54
    CPLD_BUS=53
elif [ "$2" = "right" ]; then
    IO_BUS=62
    CPLD_BUS=61
else
    echo "$2: not a valid PDB location"
    exit -1
fi

interface="$1"
img="$3"

CPLD_PIN_OFFSETS=(0 1 2 3)
CPLD_SHADOWS=(BMC_PDB_CPLD_TDO BMC_PDB_CPLD_TDI BMC_PDB_CPLD_TMS BMC_PDB_CPLD_TCK)
HITLESS_OFFSET=7
HITLESS_SHADOW=BMC_PDB_HITLESS
PCA9534=`i2cdetect -y ${IO_BUS} 0x21 0x21 | grep "\-\-"` > /dev/null

pca9534_gpio_add()
{
    if [ "${PCA9534}" != "" ]; then
        echo "PDB $1 not insert or PCA9534 fail!"
        exit -1
    fi

    i2c_device_add ${IO_BUS} 0x21 pca9534
    usleep 100000

    gpio_export_by_offset ${IO_BUS}-0021 ${HITLESS_OFFSET} ${HITLESS_SHADOW}
    gpio_set_value ${HITLESS_SHADOW} 0

    if [ "$interface" = "jtag" ]; then
        for i in {0..3}; do
            gpio_export_by_offset ${IO_BUS}-0021 ${CPLD_PIN_OFFSETS[i]} ${CPLD_SHADOWS[i]}
        done
    fi
}

pca9534_gpio_delete()
{
    if [ "${PCA9534}" != "" ]; then
        exit -1
    fi

    gpio_set_value ${HITLESS_SHADOW} 1
    gpio_unexport ${HITLESS_SHADOW}

    if [ "$interface" = "jtag" ]; then
        for i in {0..3}; do
            gpio_unexport ${CPLD_SHADOWS[i]}
        done
    fi

    i2c_device_delete ${IO_BUS} 0x21
    rm -rf /tmp/pdbcpld_update
}

trap pca9534_gpio_delete INT TERM QUIT EXIT

echo 1 > /tmp/pdbcpld_update

# export pca9534 GPIO to connect BMC to PDB CPLD pins
pca9534_gpio_add $2

if [ "$interface" = "jtag" ]; then
    ispvm dll /usr/lib/libcpldupdate_dll_gpio.so "${img}" \
              --tms ${CPLD_SHADOWS[2]} --tdo ${CPLD_SHADOWS[0]} \
              --tdi ${CPLD_SHADOWS[1]} --tck ${CPLD_SHADOWS[3]}
    result=$?
    sleep 1
    # 1 is returned upon upgrade success
    if [ $result -eq 1 ]; then
        echo "Upgrade successful."
        exit 0
    else
        echo "Upgrade failure. Return code from utility : $result"
        exit 1
    fi
elif [ "$interface" = "i2c" ]; then
    cpldupdate-i2c ${CPLD_BUS} 0x40 "${img}"
    sleep 1
else
    echo "$1 is not a valid PDB interface"
    exit -1
fi


#!/bin/bash
#
# Copyright 2018-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

usage(){
    program=`basename "$0"`
    echo "Usage:"
    echo "     $program <PIM> <File Path>"
    echo "     <PIM>: 1 ~ 8"
}

if [ $# -ne 2 ]; then
    usage
    exit -1
fi
UNIT=$1
IMAGE="$2"

if [ "${UNIT}" -le 0 ] || [ "${UNIT}" -ge 9 ]; then
    echo "PIM #${UNIT}: not a valid PIM Unit"
    exit -1
fi

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

PIN_OFFSETS=(3 4 5 6)
SHADOWS=(BMC_PIM${UNIT}_CPLD_TCK BMC_PIM${UNIT}_CPLD_TMS \
         BMC_PIM${UNIT}_CPLD_TDO BMC_PIM${UNIT}_CPLD_TDI)
BUS=$(((${UNIT} - 1) * 8 + 85 ))
PCA9534=`i2cdetect -y ${BUS} 0x26 0x26 | grep "\-\-"` > /dev/null

# PIM1 -> BUS 85 ; PIM2 -> BUS 93 ; PIM3 -> BUS 101; PIM4 -> BUS 109
# PIM5 -> BUS 117; PIM6 -> BUS 125; PIM7 -> BUS 133; PIM8 -> BUS 141
pca9534_gpio_add()
{
    if [ "${PCA9534}" != "" ]; then
        echo "PIM #${UNIT} not insert or PCA9534 fail!"
        exit -1
    fi

    i2c_device_add ${BUS} 0x26 pca9534
    usleep 100000

    for i in {0..3}; do
        gpio_export_by_offset ${BUS}-0026 ${PIN_OFFSETS[i]} ${SHADOWS[i]}
    done
}

pca9534_gpio_delete()
{
    if [ "${PCA9534}" != "" ]; then
        exit -1
    fi

    for i in {0..3}; do
        gpio_unexport ${SHADOWS[i]}
    done

    i2c_device_delete ${BUS} 0x26
    rm -rf /tmp/pimcpld_update
}

trap pca9534_gpio_delete INT TERM QUIT EXIT

# export pca9534 GPIO to connect BMC to PIM CPLD pins
pca9534_gpio_add ${UNIT}

echo 1 > /tmp/pimcpld_update

ispvm dll /usr/lib/libcpldupdate_dll_gpio.so "${IMAGE}" \
    --tms ${SHADOWS[1]} \
    --tdo ${SHADOWS[2]} \
    --tdi ${SHADOWS[3]} \
    --tck ${SHADOWS[0]}
result=$?
# 1 is returned upon upgrade success
if [ $result -eq 1 ]; then
    echo "Upgrade successful."
    exit 0
else
    echo "Upgrade failure. Return code from utility : $result"
    exit 1
fi


#!/bin/sh
#
# Copyright 2018-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

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 2018-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

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

SCM_CMD=$SMBCPLD_SYSFS_DIR'/scm_presnt_status'
PIM_CMD=$SMBCPLD_SYSFS_DIR'/*prsnt_n_status'

function usage
{
  program=`basename "$0"`
  echo "Usage:"
  echo "     $program "
  echo "     $program <TYPE>"
  echo "     <TYPE>: scm/pim/fan/psu"
}

# Will return <key> : <val>
# eg - scm : 1
function get_presence
{
    local file=$1 key=$2
    val=$(head -n1 $file | awk '{print substr($0,0,3) }' | sed 's/.x//')
    if [ $val -eq 0 ]; then
        val=1
    else
        val=0
    fi
    echo $key ":" $val
}

# SCM
function get_scm_presence
{
    key='scm'
    get_presence $SCM_CMD $key
}

# PIMs
function get_pim_presence
{
    local count=1
    for file in $PIM_CMD; do
        key='pim'$count
        get_presence $file $key
        (( count++ ))
    done
}

# FANs
function get_fan_presence
{
    local num=1
    while [ $num -le 4 ]; do
        count=$(((${num} * 2 ) - 1))
        key='fan'$count
        file=$TOP_FCMCPLD_SYSFS_DIR'/fantray'$num'_present'
        get_presence $file $key
        count=$((${num} * 2 ))
        key='fan'$count
        file=$BOTTOM_FCMCPLD_SYSFS_DIR'/fantray'$num'_present'
        get_presence $file $key
        (( num++ ))
        (( count++ ))
    done
}

# Power supplies
function get_psu_presence
{
    for i in {2..1}; do
        count=$((3 - ${i}))
        key='psu'$count
        file=$SMBCPLD_SYSFS_DIR'/psu_L'${i}'_present_L'
        get_presence $file $key
    done
    for i in {2..1}; do
        count=$((5 - ${i}))
        key='psu'$count
        file=$SMBCPLD_SYSFS_DIR'/psu_R'${i}'_present_L'
        get_presence $file $key
    done
}

# get all info
if [[ $1 = "" ]]; then
  echo "CARDS"
  get_scm_presence
  get_pim_presence

  echo "FANS"
  get_fan_presence

  echo "POWER SUPPLIES"
  get_psu_presence

else
  if [[ $1 = "scm" ]]; then
    get_scm_presence
  elif [[ $1 = "pim" ]]; then
    get_pim_presence
  elif [[ $1 = "fan" ]]; then
    get_fan_presence
  elif [[ $1 = "psu" ]]; then
    get_psu_presence
  else
    usage
  fi
fi


#!/bin/bash
#
# Copyright 2018-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

IO0=$(i2cget -f -y 50 0x20 0x2)
IO1=$(i2cget -f -y 50 0x20 0x3)

usage(){
  program=$(basename "$0")
    echo "Usage:"
    echo "     $program <brd_rev>"
    echo ""
    echo "     <brd_rev>: borad revision"
}

print_color_name(){
  case $2 in
      6)
          color="red"
      ;;
      3)
          color="blue"
      ;;
      4)
          color="yellow"
      ;;
      7)
          color="off"
      ;;
      *)
      echo "Read value that is not a valid color $1:$2"
      exit 1
      ;;
  esac
  echo "$1:$color"
}

if [ "$1" = "0" ] || [ "$1" = "4" ]; then

    PSU_VAL=$(((IO0 & 0x38) >> 3))
    SMB_VAL=$(((IO1 & 0x38) >> 3))
    FAN_VAL=$((IO0 & 0x7))
    SYS_VAL=$((IO1 & 0x7))

else

    FAN_VAL=$(((IO0 & 0x38) >> 3))
    SMB_VAL=$(((IO1 & 0x38) >> 3))
    SYS_VAL=$((IO0 & 0x7))
    PSU_VAL=$((IO1 & 0x7))

fi

print_color_name "sys" $SYS_VAL
print_color_name "smb" $SMB_VAL
print_color_name "psu" $PSU_VAL
print_color_name "fan" $FAN_VAL


#!/bin/bash
#
# Copyright 2018-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

# There are two reset pins to ASIC. System reset and PCI reset.
#
# When PWR_TH_RST_SYSFS is set to 0, CPLD will drive both reset pins to trigger
# reset.
#
# When PWR_TH_RST_SYSFS is set to 1, CPLD will drive system reset, followed by
# PCI reset following the reset sequeence.
#
# There is another sysfs file, 'cpld_mac_pcie_perst_l', which can be used
# to control the PCI reset pin separately. However, we don't use it for now
# as we depend on the CPLD for the correct reset sequence.
echo 0 > $PWR_TH_RST_SYSFS
sleep 1
echo 1 > $PWR_TH_RST_SYSFS

logger "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="${SMBCPLD_SYSFS_DIR}/cpld_usb2112a_brdg_rst"

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

echo "Done"


#!/bin/bash
#
# Copyright 2018-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

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

img="$1"

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

trap 'gpio_set_value BMC_SCM_CPLD_EN 1; \
      rm -rf /tmp/scmcpld_update' INT TERM QUIT EXIT

# change BMC_SCM_CPLD_EN to 0 to connect BMC to SCM CPLD pins
gpio_set_value BMC_SCM_CPLD_EN 0

echo 1 > /tmp/scmcpld_update

ispvm dll /usr/lib/libcpldupdate_dll_gpio.so "${img}" \
    --tms BMC_SCM_CPLD_TMS \
    --tdo BMC_SCM_CPLD_TDO \
    --tdi BMC_SCM_CPLD_TDI \
    --tck BMC_SCM_CPLD_TCK
result=$?
# 1 is returned upon upgrade success
if [ $result -eq 1 ]; then
    echo "Upgrade successful."
    exit 0
else
    echo "Upgrade failure. Return code from utility : $result"
    exit 1
fi


#!/bin/sh
#
# Copyright 2018-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)

# Enable the isolation buffer between BMC and COMe i2c bus
echo 1 > ${SCMCPLD_SYSFS_DIR}/i2c_bus_en
echo 0 > ${SCMCPLD_SYSFS_DIR}/iso_com_early_en

# Make the backup BIOS flash connect to COMe instead of BMC
echo 0 > ${SCMCPLD_SYSFS_DIR}/com_spi_oe_n
echo 0 > ${SCMCPLD_SYSFS_DIR}/com_spi_sel

# Setup management port LED
/usr/local/bin/setup_mgmt.sh led &

# Setup TH3 PCI-e repeater
/usr/local/bin/setup_pcie_repeater.sh th3 write

# Init server_por_cfg
if [ ! -f "/mnt/data/kv_store/server_por_cfg" ]; then
    /usr/bin/kv set server_por_cfg on persistent
fi

# Setup hotswap power monitor averaging for FCM-T and FCM-B
i2cset -f -y 67 0x10 0xd4 0x3f1e w
i2cset -f -y 75 0x10 0xd4 0x3f1e w

/usr/bin/kv set smb_board_rev $board_rev


#!/bin/sh
#
# Copyright 2018-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

board_rev=$(wedge_board_rev)

#
# instantiate all the i2c-muxes.
# Note:
#   - the function may be skipped if i2c switches were already created
#     in kernel space.
#   - please do not modify the order of i2c-mux creation unless you've
#     decided to fix bus-number of leaf i2c-devices.
#
bulk_create_i2c_mux() {
    i2c_mux_name="pca9548"
    mux_channels=8

    # The first-level i2c-muxes which are directly connected to aspeed
    # i2c adapters are described in device tree, and the bus number of
    # these i2c-muxes' channels are hardcoded (as below) to avoid
    # breaking the existing applications.
    #    i2c-mux 2-0070: child bus 16-23
    #    i2c-mux 8-0070: child bus 24-31
    #    i2c-mux 9-0070: child bus 32-39
    #    i2c-mux 11-0070: child bus 40-47

    # Create second-level i2c-muxes which are connected to first level
    # mux "8-0070". "8-0070" has 8 channels and the first 4 channels
    # are connected with 1 extra level of i2c-mux. So total 32 child
    # buses (48-79) will be registered.
    i2c_mux_add_sync 24 0x71 "$i2c_mux_name" "$mux_channels"

    i2c_mux_add_sync 25 0x72 "$i2c_mux_name" "$mux_channels"

    i2c_mux_add_sync 26 0x76 "$i2c_mux_name" "$mux_channels"

    i2c_mux_add_sync 27 0x76 "$i2c_mux_name" "$mux_channels"

    # Create second-level i2c-muxes which are connected to first level
    # mux "11-0070". "11-0070" has 8 channels and each channel is
    # connected with 1 extra level of i2c-mux (@ address 0x73), thus
    # totally 64 i2c buses (80-143) will be registered.
    parent_buses="40 41 42 43 44 45 46 47"
    for bus in ${parent_buses}; do
        i2c_mux_add_sync "$bus" 0x73 "$i2c_mux_name" "$mux_channels"
    done
}

#
# Let's check a few i2c switches to determine if these switches were
# already created in kernel space.
#
I2C_MUX_PDB_LEFT="${SYSFS_I2C_DEVICES}/24-0071"
I2C_MUX_FCM_TOP="${SYSFS_I2C_DEVICES}/26-0076"
I2C_MUX_PIM1="${SYSFS_I2C_DEVICES}/40-0073"
I2C_MUX_PIM6="${SYSFS_I2C_DEVICES}/45-0073"
if [ ! -e "${I2C_MUX_PDB_LEFT}" ] && [ ! -e "${I2C_MUX_FCM_TOP}" ] && \
   [ ! -e "${I2C_MUX_PIM1}" ] && [ ! -e "${I2C_MUX_PIM6}" ]; then
    bulk_create_i2c_mux
fi

KERNEL_VERSION=`uname -r`
if [[ ${KERNEL_VERSION} != 4.1.* ]]; then
    # Create i2c slave backend on bus 0 and 4.
    i2c_mslave_add 0 0x10
    i2c_mslave_add 4 0x10
fi

# # Bus 2
i2c_device_add 2 0x35 scmcpld          # SCMCPLD

# # Bus 12
i2c_device_add 12 0x3e smbcpld         # SMBCPLD

# # Bus 13
i2c_device_add 13 0x35 iobfpga         # IOBFPGA

# # Bus 1
i2c_device_add 1 0x3a powr1220         # SMB power sequencer
i2c_device_add 1 0x12 ir3595           # TH3 core voltage/current monitor
i2c_device_add 1 0x60 isl68127         # TH3 serdes voltage/current monitor

# # Bus 3
i2c_device_add 3 0x48 tmp75            # SMB temp. sensor
i2c_device_add 3 0x49 tmp75            # SMB temp. sensor
i2c_device_add 3 0x4a tmp75            # SMB temp. sensor
i2c_device_add 3 0x4b tmp75            # SMB temp. sensor

# # Bus 30
i2c_device_add 30 0x4c tmp422          # TH3 temp. sensor

# # Bus 16
i2c_device_add 16 0x10 adm1278         # SCM Hotswap

# # Bus 17
i2c_device_add 17 0x4c tmp421          # SCM temp. sensor
i2c_device_add 17 0x4d tmp421          # SCM temp. sensor

if [ $board_rev -ne 4 ]; then
    # # Bus 55
    i2c_device_add 55 0x60 pdbcpld         # Left PDBCPLD

    # # Bus 63
    i2c_device_add 63 0x60 pdbcpld         # Right PDBCPLD
fi

# # Bus 49
i2c_device_add 49 0x59 psu_driver      # PDB-L PSU1 Driver

# # Bus 48
i2c_device_add 48 0x58 psu_driver      # PDB-L PSU2 Driver

# # Bus 57
i2c_device_add 57 0x59 psu_driver      # PDB-R PSU3 Driver

# # Bus 56
i2c_device_add 56 0x58 psu_driver      # PDB-R PSU4 Driver

# # Bus 51
i2c_device_add 51 0x48 tmp75           # PDB-L temp. sensor

# # Bus 52
i2c_device_add 52 0x49 tmp75           # PDB-L temp. sensor

# # Bus 59
i2c_device_add 59 0x48 tmp75           # PDB-R temp. sensor

# # Bus 60
i2c_device_add 60 0x49 tmp75           # PDB-R temp. sensor

# # Bus 64
i2c_device_add 64 0x33 fcmcpld         # Top FCMCPLD

# # Bus 72
i2c_device_add 72 0x33 fcmcpld         # Bottom FCMCPLD

# # Bus 66
i2c_device_add 66 0x48 tmp75           # FCM-T temp. sensor
i2c_device_add 66 0x49 tmp75           # FCM-T temp. sensor

# # Bus 67
i2c_device_add 67 0x10 adm1278         # FCM-T Hotswap

# # Bus 74
i2c_device_add 74 0x48 tmp75           # FCM-B temp. sensor
i2c_device_add 74 0x49 tmp75           # FCM-B temp. sensor

# # Bus 67
i2c_device_add 75 0x10 adm1278         # FCM-T Hotswap

# # Bus 6
i2c_device_add 6 0x51 24c64            # Board fru EEPROM

# # Bus 18
i2c_device_add 18 0x52 24c64           # SCM fru eeprom

# # Bus 65
i2c_device_add 65 0x51 24c02           # FCM-T fru eeprom

# # Bus 73
i2c_device_add 73 0x51 24c02           # FCM-B fru eeprom

# # Bus 68 ~ 71                        # Fantray 7 5 3 1  fru eeprom
# # Bus 76 ~ 79                        # Fantray 8 6 4 2  fru eeprom
BUS="68 69 70 71 76 77 78 79"
for bus in ${BUS}; do
    i2c_device_add $bus 0x52 24c64
done

# # Bus 81 89 97 105 113 121 129 137   # PIM1 ~ PIM8 fru eeprom
BUS="81 89 97 105 113 121 129 137"
for bus in ${BUS}; do
    i2c_device_add $bus 0x53 24c64     # 4DD
    i2c_device_add $bus 0x56 24c64     # 16Q
done

# # Bus 80 88 96 104 112 120 128 136  # PIM1 ~ PIM8 DOMFPGA
BUS="80 88 96 104 112 120 128 136"
for bus in ${BUS}; do
    i2c_device_add $bus 0x60 domfpga
done


#!/bin/sh
#
# Copyright 2018-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

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

SFP_AUTO_DETECT="${SCMCPLD_SYSFS_DIR}/sfp_auto_detect"
COM_E_PWRGD=`head -n 1 ${SCMCPLD_SYSFS_DIR}/pwrgd_pch_pwrok`
TX_DISABLE="${SCMCPLD_SYSFS_DIR}/tx_disable"

usage(){
    program=`basename "$0"`
    echo "Usage:"
    echo "     $program led/combo/all"
}

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

# When COM-e power is not ready, it doesn't work to set PHY register.
# Therefore, waiting for COM-e power ready, then start to set PHY register.
while [ "$COM_E_PWRGD" == "0x0" ];
do
    COM_E_PWRGD=`head -n 1 ${SCMCPLD_SYSFS_DIR}/pwrgd_pch_pwrok`
    sleep 5
done

devmem_set_bit $(scu_addr 88) 30
devmem_set_bit $(scu_addr 88) 31

if [ "$1" == "led" ]; then
    echo "Wait a few seconds to setup management port LED..."
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x01d
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x3453
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x012
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0xa03
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x019
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x2418
    echo "Done!"
elif [ "$1" == "combo" ]; then
    echo "Wait a few minutes to setup management combo port..."
    ast-mdio.py --mac 1 --phy 0x9 write 0x17 0x0f7e
    ast-mdio.py --mac 1 --phy 0x9 write 0x15 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x083c
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x2f
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7167
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x21
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7c05
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x1140
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x21
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7c04
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x09
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x0200
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x04
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x01e1
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x1340
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x234
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0xd18f
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0xb00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x1140
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x23e
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7ae2
    echo 1 > $SFP_AUTO_DETECT
    echo 0 > $TX_DISABLE
    echo "Done!"
elif [ "$1" == "all" ]; then
    echo "Wait a few minutes to setup management port..."
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x01d
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x3453
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x012
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0xa03
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x019
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x2418
    ast-mdio.py --mac 1 --phy 0x9 write 0x17 0x0f7e
    ast-mdio.py --mac 1 --phy 0x9 write 0x15 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x083c
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x2f
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7167
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x21
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7c05
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x1140
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x21
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7c04
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x09
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x0200
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x04
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x01e1
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x1340
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x234
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0xd18f
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0xb00
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x1140
    ast-mdio.py --mac 1 --phy 0x9 write 0x1e 0x23e
    ast-mdio.py --mac 1 --phy 0x9 write 0x1f 0x7ae2
    echo 1 > $SFP_AUTO_DETECT
    echo 0 > $TX_DISABLE
else
    usage
    exit -1
fi


#!/bin/bash
#
# Copyright 2018-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

usage(){
    program=`basename "$0"`
    echo "Usage:"
    echo "     $program <Device> <Operation>"
    echo "     <Device>:    th3"
    echo "     <Operation>: read / write"
}

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

REG=(0x00 0x01 0x02 0x06 0x07 0x08 0x0A 0x28 \
     0x51 0x0E 0x0F 0x10 0x11 0x12 0x15 0x16 \
     0x17 0x18 0x19 0x1C 0x1D 0x1E 0x1F 0x20 \
     0x23 0x24 0x25 0x26 0x27 0x2B 0x2C 0x2D \
     0x2E 0x2F 0x32 0x33 0x34 0x35 0x36 0x39 \
     0x3A 0x3B 0x3C 0x3D 0x40 0x41 0x42 0x43 \
     0x44 0x03 0x04 0x05 0x09 0x0B 0x0C 0x0D \
     0x13 0x14 0x1A 0x1B 0x21 0x22 0x29 0x2A \
     0x30 0x31 0x37 0x38 0x3E 0x3F 0x45 0x46 \
     0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E \
     0x4F 0x50 0x52 0x53 0x54 0x55 0x56 0x57 \
     0x58 0x59 0x5A 0x5B 0x5C 0x5D 0x5E 0x5F \
     0x60 0x61 \
     )

if [ "$1" == "th3" ]; then
    VAL=(0x08 0x00 0x00 0x18 0x01 0x00 0xFF 0x0C \
         0x44 0x00 0x00 0xAD 0x01 0x00 0x00 0x00 \
         0xAD 0x01 0x00 0x00 0x00 0xAD 0x01 0x00 \
         0x00 0x00 0xAD 0x01 0x00 0x00 0x00 0xAD \
         0x00 0x00 0x00 0x00 0xAD 0x00 0x00 0x00 \
         0x00 0xAD 0x00 0x00 0x00 0x00 0xAD 0x00 \
         0x00 0x00 0x00 0x00 0x00 0x70 0x00 0x00 \
         0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 \
         0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x38 \
         0x00 0x05 0x00 0x00 0x00 0x00 0x00 0x00 \
         0x00 0x00 0x00 0x00 0x00 0x00 0x10 0x44 \
         0x21 0x00 0x54 0x54 0x00 0x00 0x00 0x00 \
         0x00 0x00 \
         )
    # TH3 PCI-e repeater slave mode
    echo 0 > ${SCMCPLD_SYSFS_DIR}/iso_th3rp_en
    echo 1 > ${SCMCPLD_SYSFS_DIR}/a_ds80_ensmb
    th3_repeater=`i2cdetect -y 21 0x58 0x58 | grep "\-\-" 2> /dev/null`

    if [ "${th3_repeater}" != "" ]; then
        echo "TH3 PCI-e repeater is not detected"
    else
        if [ "$2" == "write" ]; then
            echo -n "Setup TH3 PCI-e repeater... "
            for i in {0..97}; do
                i2cset -f -y 21 0x58 ${REG[i]} ${VAL[i]} 2> /dev/null
                usleep 11000
            done
            echo "Done"
        elif [ "$2" == "read" ]; then
            echo "Bus:21 Addr:0x58"
            for i in {0..97}; do
                echo -n "Reg:${REG[i]} Val:"
                i2cget -f -y 21 0x58 ${REG[i]} 2> /dev/null
                if [ $? -ne 0 ]; then
                    echo ""
                fi
                usleep 11000
            done
        fi
    fi
fi


#!/bin/sh
#
# Copyright 2018-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

usage(){
    program=`basename "$0"`
    echo "Usage:"
    echo "     $program reset <PIM>"
    echo "     $program data  <PIM> <Operation>"
    echo "     <PIM>: all, 1 ~ 8"
    echo "     <Operation>: enable / disable"
}

if [ $# -le 1 ] || [ $# -ge 4 ]; then
    usage
    exit -1
fi

# Bus 80 88 96 104 112 120 128 136   # PIM1 ~ PIM8 FPGA
if [ "$2" == "all" ]; then
    FPGA_BUS="80 88 96 104 112 120 128 136"
elif [ "$2" -ge 1 ] || [ "$2" -le 8 ]; then
    FPGA_BUS=$((($2 - 1) * 8 + 80 ))
else
    usage
    exit -1
fi

if [ "$1" == "reset" ]; then
    echo -n "Reset QSFP ... "
    for bus in ${FPGA_BUS}; do
        i2c_path=$(i2c_device_sysfs_abspath ${bus}-0060)
        reset="${i2c_path}/qsfp_reset"
        echo 0xffff > "${reset}"
        usleep 1000
        echo 0x0 > "${reset}"
    done
    echo "Done"
elif [ "$1" == "data" ]; then
    echo -n "DOM data collection "$3" ... "
    for bus in ${FPGA_BUS}; do
        i2c_path=$(i2c_device_sysfs_abspath ${bus}-0060)
        config="${i2c_path}/dom_ctrl_config"
        if [ "$3" == "enable" ]; then
            echo 0x5000001 > "${config}"
        elif [ "$3" == "disable" ]; then
            echo 0xf00 > "${config}"
        else
            usage
            exit -1
        fi
    done
    echo "Done"
else
    usage
    exit -1
fi


#!/bin/bash
#
# Copyright 2019-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_sensors_conf.sh
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup lm-sensors config
### END INIT INFO

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

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

KERNEL_VERSION=$(uname -r)
SENSORS_CONF_PATH="/etc/sensors.d"
FCM_T_BOARD_REV=$(head -n1 "$TOP_FCMCPLD_SYSFS_DIR"/board_ver)
FCM_B_BOARD_REV=$(head -n1 "$BOTTOM_FCMCPLD_SYSFS_DIR"/board_ver)

# In kernel version 4.1.x -
# pmbus_core.c use milliwatt for direct format power output,
# so we need to multiply power output by 1000.
# In higher kernel version -
# pmbus_core.c use microwatt for direct format power output,
# so we need to multiply power output by 1.
if [[ "$KERNEL_VERSION" != 4.1.* ]]; then
    div=1
else
    div=1000
fi

sed -i "s/power_factor/$div/g" "$SENSORS_CONF_PATH"/custom/*.conf
cp "$SENSORS_CONF_PATH"/custom/adm1278-SCM.conf "$SENSORS_CONF_PATH"/scm.conf

# Config FCM Rsense value at different hardware version.
# Kernel driver use Rsense equal to 1 milliohm. We need to correct Rsense
# value, and all value are from hardware team.
if [ "$FCM_T_BOARD_REV" = "0x4" ] || [ "$FCM_T_BOARD_REV" = "0x5" ]; then
    # For R0D or R01 FCM
    fcm_t_rsense=2.651
elif [ "$FCM_T_BOARD_REV" = "0x7" ] || [ "$FCM_T_BOARD_REV" = "0x2" ]; then
    # For R0A, R0B or R0C FCM
    fcm_t_rsense=3.03
else
    # Default, keep Rsense to 1, if we have new FCM version, we can use default
    # Rsense value as a base to correct real value.
    fcm_t_rsense=1
fi

# Config FCM Rsense value at different hardware version.
# Kernel driver use Rsense equal to 1 milliohm. We need to correct Rsense
# value, and all value are from hardware team.
if [ "$FCM_B_BOARD_REV" = "0x4" ] || [ "$FCM_B_BOARD_REV" = "0x5" ]; then
    # For R0D or R01 FCM
    fcm_b_rsense=2.659
elif [ "$FCM_B_BOARD_REV" = "0x7" ] || [ "$FCM_B_BOARD_REV" = "0x2" ]; then
    # For R0A, R0B or R0C FCM
    fcm_b_rsense=3.03
else
    # Default, keep Rsense to 1, if we have new FCM version, we can use default
    # Rsense value as a base to correct real value.
    fcm_b_rsense=1
fi

sed "s/rsense/$fcm_t_rsense/g" "$SENSORS_CONF_PATH"/custom/adm1278-FCM-T.conf \
> "$SENSORS_CONF_PATH"/fcm-t.conf

sed "s/rsense/$fcm_b_rsense/g" "$SENSORS_CONF_PATH"/custom/adm1278-FCM-B.conf \
> "$SENSORS_CONF_PATH"/fcm-b.conf


#!/bin/sh
#
# Copyright 2018-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

usage(){
    program=`basename "$0"`
    echo "Usage:"
    echo "     $program all <Operation>"
    echo "     $program <PIM> <Operation>"
    echo "     <PIM>: 1 ~ 8"
    echo "     <Operation>: load / unload"
}

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

count=0

# Bus 80 88 96 104 112 120 128 136   # PIM1 ~ PIM8 FPGA
# Bus 82 90 98 106 114 122 130 138   # PIM1 ~ PIM8 temp. sensor
# Bus 83 91 99 107 115 123 131 139   # PIM1 ~ PIM8 temp. sensor
# Bus 84 92 100 108 116 124 132 140  # PIM1 ~ PIM8 Hotswap
# Bus 86 94 102 110 118 126 134 142  # PIM1 ~ PIM8 MAX34461
if [ "$1" == "all" ]; then
    FPGA_BUS="80 88 96 104 112 120 128 136"
else
    if [ "$1" -le 0 ] || [ "$1" -ge 9 ]; then
        echo "PIM$1: not a valid PIM Unit"
        exit -1
    fi
    FPGA_BUS=$((($1 - 1) * 8 + 80 ))
fi

if [ "$2" == "unload" ]; then
    for bus in ${FPGA_BUS}; do
        i2c_device_delete $(($bus + 4)) 0x10 2> /dev/null
        i2c_device_delete $(($bus + 6)) 0x74 2> /dev/null
        i2c_device_delete $(($bus + 2)) 0x48 2> /dev/null
        i2c_device_delete $(($bus + 3)) 0x4b 2> /dev/null
    done
elif [ "$2" == "load" ]; then
    #Sleep to avoid mount max34461 fail.
    #It needs 3~4 seconds to initialize.
    #Thus, we sleep 5 seconds to be on the save side.
    sleep 5
    for bus in ${FPGA_BUS}; do
        i2c_device_add $(($bus + 4)) 0x10 adm1278 2> /dev/null
        i2c_device_add $(($bus + 2)) 0x48 tmp75 2> /dev/null
        i2c_device_add $(($bus + 3)) 0x4b tmp75 2> /dev/null

        dev_sysfs_dir=$(i2c_device_sysfs_abspath $bus-0060)
        pim_type=$(head -n1 "${dev_sysfs_dir}"/board_ver 2> /dev/null)
        if [ "${pim_type}" == "0x0" ]; then   #PIM16Q
            i2c_device_add $(($bus + 6)) 0x74 max34461 2> /dev/null
        elif [ "${pim_type}" == "0xf0" ]; then #PIM16O
            i2c_device_add $(($bus + 6)) 0x74 max34460 2> /dev/null
        elif [ "${pim_type}" == "0x10" ]; then #PIM4DD
            i2c_device_add $(($bus + 6)) 0x74 max34461 2> /dev/null
        else
            echo "DOMFPGA is not detected or PIM $num is not inserted"
        fi
    done
else
    echo "Wrong operation"
    usage
    exit -1
fi


#!/bin/sh
#
# Copyright 2018-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

# Set all IO direction to output
i2cset -f -y 50 0x20 0x06 0x00
i2cset -f -y 50 0x20 0x07 0x00

IO0=`i2cget -f -y 50 0x20 0x2`
IO1=`i2cget -f -y 50 0x20 0x3`

color=$2

usage(){
    program=`basename "$0"`
    echo "Usage:"
    echo "     $program <dev> <color>"
    echo ""
    echo "     <dev>: sys/smb/psu/fan"
    echo "     <color>: off/red/blue/yellow"
}

case $color in
    "red")
        val=0x6
    ;;
    "blue")
        val=0x3
    ;;
    "yellow")
        val=0x4
    ;;
    "off")
        val=0x7
    ;;
    *)
    usage
    exit -1
    ;;
esac

if [ "$3" == "0" ] || [ "$3" == "4" ]; then

  if [ "$1" == "smb" ] || [ "$1" == "psu" ]; then
    val=$(($val << 3))
    IO0_VAL=$((($IO0 & 0x7) | $val))
    IO1_VAL=$((($IO1 & 0x7) | $val))
  elif [ "$1" == "sys" ] || [ "$1" == "fan" ]; then
    IO0_VAL=$((($IO0 & 0x38) | $val))
    IO1_VAL=$((($IO1 & 0x38) | $val))
  else
    usage
    exit -1
  fi

  if [ "$1" == "psu" ] || [ "$1" == "fan" ]; then
    i2cset -f -y 50 0x20 0x2 $IO0_VAL
  else
    i2cset -f -y 50 0x20 0x3 $IO1_VAL
  fi

else

  if [ "$1" == "fan" ] || [ "$1" == "smb" ]; then
    val=$(($val << 3))
    IO0_VAL=$((($IO0 & 0x7) | $val))
    IO1_VAL=$((($IO1 & 0x7) | $val))
  elif [ "$1" == "sys" ] || [ "$1" == "psu" ]; then
    IO0_VAL=$((($IO0 & 0x38) | $val))
    IO1_VAL=$((($IO1 & 0x38) | $val))
  else
    usage
    exit -1
  fi

  if [ "$1" == "sys" ] || [ "$1" == "fan" ]; then
    i2cset -f -y 50 0x20 0x2 $IO0_VAL
  else
    i2cset -f -y 50 0x20 0x3 $IO1_VAL
  fi

fi


#!/bin/bash
#
# Copyright 2018-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

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

img="$1"

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

trap 'gpio_set_value BMC_SYSCPLD_JTAG_MUX_SEL 0; \
      rm -rf /tmp/smbcpld_update' INT TERM QUIT EXIT

# change BMC_SYSCPLD_JTAG_MUX_SEL to 1 to connect BMC to SMB CPLD pins
gpio_set_value BMC_SYSCPLD_JTAG_MUX_SEL 1

echo 1 > /tmp/smbcpld_update

ispvm dll /usr/lib/libcpldupdate_dll_gpio.so "${img}" \
    --tms BMC_CPLD_TMS \
    --tdo BMC_CPLD_TDO \
    --tdi BMC_CPLD_TDI \
    --tck BMC_CPLD_TCK
result=$?
# 1 is returned upon upgrade success
if [ $result -eq 1 ]; then
    echo "Upgrade successful."
    exit 0
else
    echo "Upgrade failure. Return code from utility : $result"
    exit 1
fi


#!/bin/sh
#
# Copyright 2018-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/ttyS0

mTerm_server_running() {
  pid=$(ps w | 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