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

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

for i in {0..7};
do
  for j in {0..4};
  do
    gpioexp_export 7-002${i} OAM${i}_LINK_CONFIG${j} ${j}
  done
  gpioexp_export 7-002${i} OAM${i}_PE_BIF1 5
  gpioexp_export 7-002${i} OAM${i}_PE_BIF0 6
  gpioexp_export 7-002${i} OAM${i}_MANF_MODE 7
  gpioexp_export 7-002${i} OAM${i}_FW_RECOVERY 8
  gpioexp_export 7-002${i} OAM${i}_TEST_MODE 9
  gpioexp_export 7-002${i} OAM${i}_PLINK_CAP 10
  gpioexp_export 7-002${i} OAM${i}_PWRRDT0 11
  gpioexp_export 7-002${i} OAM${i}_PWRRDT1 12
  gpioexp_export 7-002${i} OAM${i}_MODULE_PWRGD 13
  gpioexp_export 7-002${i} OAM${i}_THERMTRIP 14

  gpio_set OAM${i}_LINK_CONFIG0 0
  gpio_set OAM${i}_LINK_CONFIG2 0
  gpio_set OAM${i}_LINK_CONFIG4 0
done

for i in {0..3};
do
  gpioexp_export 18-0067 FAN${i}_PRESENT ${i}
  gpioexp_export 18-0067 FAN${i}_PWR_GOOD $((i+4))
  gpioexp_export 18-0067 FAN${i}_OK $((i+8))
  gpioexp_export 18-0067 FAN${i}_FAIL $((i+12))
  gpio_set FAN${i}_OK 1
  gpio_set FAN${i}_FAIL 0
done

# To enable GPIOA
#devmem_clear_bit $(scu_addr 80) 0
#devmem_clear_bit $(scu_addr 80) 1
#devmem_clear_bit $(scu_addr 80) 3
#devmem_clear_bit $(scu_addr 80) 6
#devmem_clear_bit $(scu_addr 80) 7
#devmem_clear_bit $(scu_addr 90) 2

gpio_export ASIC_PWR_EN_CTRL GPIOA0

# Reserved
gpio_export BMC_GPIOA1 GPIOA1
gpio_export BMC_GPIOA2 GPIOA2
gpio_export BMC_GPIOA3 GPIOA3

# OAM debug mode enable
gpio_export SCALE_DEBUG_EN_N_ASIC0 GPIOA7
gpio_export SCALE_DEBUG_EN_N_ASIC1 GPIOA6

# To enable GPIOB
#devmem_clear_scu70_bit 23
#devmem_clear_bit $(scu_addr 80) 13
#devmem_clear_bit $(scu_addr 80) 14

# PCIe switch GPIO (preserved)
gpio_export PAX2_SKU_ID0 GPIOB0
gpio_export PAX2_ALERT GPIOB1
gpio_export PAX3_SKU_ID0 GPIOB4
gpio_export PAX3_ALERT GPIOB5
gpio_export PAX0_SKU_ID1 GPIOB6
gpio_set PAX0_SKU_ID1 0
gpio_export PAX1_SKU_ID1 GPIOB7
gpio_set PAX1_SKU_ID1 0

# BMC ready
gpio_export BMC_READY_N GPIOB2

# System power good
gpio_export SYS_PWR_READY GPIOB3

# To enable GPIOD
#devmem_clear_scu70_bit 21
#devmem_clear_bit $(scu_addr 8c) 8
#devmem_clear_bit $(scu_addr 8c) 9
#devmem_clear_bit $(scu_addr 8c) 10
#devmem_clear_bit $(scu_addr 8c) 11
#devmem_clear_bit $(scu_addr 90) 1
gpio_export THERMTRIP_N_ASIC07 GPIOD7

# To enable GPIOE
#devmem_clear_scu70_bit 22
#devmem_clear_bit $(scu_addr 80) 16
#devmem_clear_bit $(scu_addr 80) 17
#devmem_clear_bit $(scu_addr 80) 18
#devmem_clear_bit $(scu_addr 80) 19
#devmem_clear_bit $(scu_addr 80) 20
#devmem_clear_bit $(scu_addr 80) 21
#devmem_clear_bit $(scu_addr 80) 22
#devmem_clear_bit $(scu_addr 80) 23
#devmem_clear_bit $(scu_addr 8c) 12
#devmem_clear_bit $(scu_addr 8c) 13
#devmem_clear_bit $(scu_addr 8c) 14
#devmem_clear_bit $(scu_addr 8c) 15

# BMC power button (input)
gpio_export BMC_PWR_BTN_IN_N GPIOE0

# PDB 12V POWER (output)
gpio_export BMC_IPMI_PWR_ON GPIOE1
gpio_set BMC_IPMI_PWR_ON 1

# ASIC warm reset
gpio_export WARMRST_BMC_N_ASIC0 GPIOE7
gpio_set WARMRST_BMC_N_ASIC0 1
gpio_export WARMRST_BMC_N_ASIC1 GPIOE6
gpio_set WARMRST_BMC_N_ASIC1 1
gpio_export WARMRST_BMC_N_ASIC2 GPIOE5
gpio_set WARMRST_BMC_N_ASIC2 1
gpio_export WARMRST_BMC_N_ASIC3 GPIOE4
gpio_set WARMRST_BMC_N_ASIC3 1
gpio_export WARMRST_BMC_N_ASIC4 GPIOE3
gpio_set WARMRST_BMC_N_ASIC4 1
gpio_export WARMRST_BMC_N_ASIC5 GPIOE2
gpio_set WARMRST_BMC_N_ASIC5 1

# To enable GPIOF
#devmem_clear_bit $(scu_addr 80) 24
#devmem_clear_bit $(scu_addr 80) 25
#devmem_clear_bit $(scu_addr 80) 26
#devmem_clear_bit $(scu_addr 80) 27
#devmem_clear_bit $(scu_addr 80) 28
#devmem_clear_bit $(scu_addr 80) 29
#devmem_clear_bit $(scu_addr 80) 30
#devmem_clear_bit $(scu_addr 80) 31
#devmem_clear_bit $(scu_addr 90) 30

# POST LED
gpio_export LED_POSTCODE_0 GPIOF0
gpio_set LED_POSTCODE_0 0
gpio_export LED_POSTCODE_1 GPIOF1
gpio_set LED_POSTCODE_1 0
gpio_export LED_POSTCODE_2 GPIOF2
gpio_set LED_POSTCODE_2 0
gpio_export LED_POSTCODE_3 GPIOF3
gpio_set LED_POSTCODE_3 0
gpio_export LED_POSTCODE_4 GPIOF4
gpio_set LED_POSTCODE_4 0
gpio_export LED_POSTCODE_5 GPIOF5
gpio_set LED_POSTCODE_5 0
gpio_export LED_POSTCODE_6 GPIOF6
gpio_set LED_POSTCODE_6 0
gpio_export LED_POSTCODE_7 GPIOF7
gpio_set LED_POSTCODE_7 0

# To enable GPIOG
#devmem_clear_bit $(scu_addr 84) 0
#devmem_clear_bit $(scu_addr 84) 1
#devmem_clear_bit $(scu_addr 84) 2
#devmem_clear_bit $(scu_addr 84) 3
#devmem_clear_bit $(scu_addr 84) 4
#devmem_clear_bit $(scu_addr 84) 5
#devmem_clear_bit $(scu_addr 84) 6
#devmem_clear_bit $(scu_addr 84) 7
#devmem_clear_bit $(scu_addr 94) 12

# JTAG mux, 1 = Altera PFR, 0 = Lattice
gpio_export SEL_CPLD GPIOG0
gpio_set SEL_CPLD 0

# TPM presence
gpio_export FM_BMC_TPM_PRES_N GPIOG1

# Enable JTAG mux, 1 = Disbale, 0 = Enable
gpio_export JTAG_ENABLE GPIOG2
gpio_set JTAG_ENABLE 0

# OAM test pin (defined by OAM)
gpio_export BMC_OAM_TEST7 GPIOG3
gpio_export BMC_OAM_TEST8 GPIOG7

# Board revision ID
gpio_export REV_ID0 GPIOG4
gpio_export REV_ID1 GPIOG5
gpio_export REV_ID2 GPIOG6

ID0=$(gpio_get REV_ID0)
ID1=$(gpio_get REV_ID1)
ID2=$(gpio_get REV_ID2)
REV_ID=$(($ID0 + $ID1*2 + $ID2*4))
if [[ $REV_ID < 5 ]]; then
  # FAB6
  ioexp_bus=13
else
  ioexp_bus=4
fi

gpioexp_export ${ioexp_bus}-0020 CONN1_0_PRSNT0_N 8
gpioexp_export ${ioexp_bus}-0020 CONN1_0_PRSNT1_N 9
gpioexp_export ${ioexp_bus}-0020 CONN1_1_PRSNT0_N 12
gpioexp_export ${ioexp_bus}-0020 CONN1_1_PRSNT1_N 13
gpioexp_export ${ioexp_bus}-0020 CONN2_0_PRSNT0_N 0
gpioexp_export ${ioexp_bus}-0020 CONN2_0_PRSNT1_N 1
gpioexp_export ${ioexp_bus}-0020 CONN2_1_PRSNT0_N 4
gpioexp_export ${ioexp_bus}-0020 CONN2_1_PRSNT1_N 5
gpioexp_export ${ioexp_bus}-0020 CONN3_0_PRSNT0_N 2
gpioexp_export ${ioexp_bus}-0020 CONN3_0_PRSNT1_N 3
gpioexp_export ${ioexp_bus}-0020 CONN3_1_PRSNT0_N 6
gpioexp_export ${ioexp_bus}-0020 CONN3_1_PRSNT1_N 7
gpioexp_export ${ioexp_bus}-0020 CONN4_0_PRSNT0_N 10
gpioexp_export ${ioexp_bus}-0020 CONN4_0_PRSNT1_N 11
gpioexp_export ${ioexp_bus}-0020 CONN4_1_PRSNT0_N 14
gpioexp_export ${ioexp_bus}-0020 CONN4_1_PRSNT1_N 15

if [[ "$(gpio_get CONN2_0_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN2_0_PRSNT1_N)" == "0" && \
      "$(gpio_get CONN3_0_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN3_0_PRSNT1_N)" == "0" ]]; then
  MB0_PRSNT=1;
else
  MB0_PRSNT=0;
fi

if [[ "$(gpio_get CONN2_1_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN2_1_PRSNT1_N)" == "0" && \
      "$(gpio_get CONN3_1_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN3_1_PRSNT1_N)" == "0" ]]; then
  MB1_PRSNT=1;
else
  MB1_PRSNT=0;
fi

if [[ "$(gpio_get CONN1_0_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN1_0_PRSNT1_N)" == "0" && \
      "$(gpio_get CONN4_0_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN4_0_PRSNT1_N)" == "0" ]]; then
  MB2_PRSNT=1;
else
  MB2_PRSNT=0;
fi

if [[ "$(gpio_get CONN1_1_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN1_1_PRSNT1_N)" == "0" && \
      "$(gpio_get CONN4_1_PRSNT0_N)" == "0" && \
      "$(gpio_get CONN4_1_PRSNT1_N)" == "0" ]]; then
  MB3_PRSNT=1;
else
  MB3_PRSNT=0;
fi

# To enable GPIOH
#devmem_clear_bit $(scu_addr 90) 7
#devmem_clear_bit $(scu_addr 94) 5
#devmem_clear_bit $(scu_addr 94) 6
#devmem_clear_bit $(scu_addr 94) 7

# ASIC presence
gpio_export PRSNT0_N_ASIC7 GPIOH0
gpio_export PRSNT0_N_ASIC6 GPIOH1
gpio_export PRSNT0_N_ASIC5 GPIOH2
gpio_export PRSNT0_N_ASIC4 GPIOH3
gpio_export PRSNT0_N_ASIC3 GPIOH4
gpio_export PRSNT0_N_ASIC2 GPIOH5
gpio_export PRSNT0_N_ASIC1 GPIOH6
gpio_export PRSNT0_N_ASIC0 GPIOH7

# To enable GPIOI
#devmem_clear_scu70_bit 13

# OAM debug pin (defined by OAM)
gpio_export BMC_OAM_TEST13 GPIOI0
gpio_export BMC_OAM_TEST14 GPIOI1

gpio_export OAM_FAST_BRK_N GPIOI2
gpio_set OAM_FAST_BRK_N 1

gpio_export OAM_FAST_BRK_ON_N GPIOI3
gpio_set OAM_FAST_BRK_ON_N 1

# OAM debug pin (defined by OAM)
gpio_export BMC_OAM_TEST3 GPIOJ0
gpio_export BMC_OAM_TEST4 GPIOJ1
gpio_export BMC_OAM_TEST5 GPIOJ2
gpio_export BMC_OAM_TEST6 GPIOJ3
gpio_export BMC_OAM_TEST0 GPIOJ6
gpio_export BMC_OAM_TEST1 GPIOJ7

# OAM debug mode enable
gpio_export SCALE_DEBUG_EN_N_ASIC5 GPIOJ4
gpio_export SCALE_DEBUG_EN_N_ASIC4 GPIOJ5

# To enable GPIOL
#devmem_clear_bit $(scu_addr 84) 16
#devmem_clear_bit $(scu_addr 84) 17
#devmem_clear_bit $(scu_addr 84) 18
#devmem_clear_bit $(scu_addr 84) 19
#devmem_clear_bit $(scu_addr 84) 20
#devmem_clear_bit $(scu_addr 84) 21
#devmem_clear_bit $(scu_addr 90) 5

# Select clock source from MB
# [S1:S0]
# 0:0 = MB#0
# 1:0 = MB#1
# 0:1 = MB#2
# 1:1 = MB#3
gpio_export SEL0_CLK_MUX GPIOL0
gpio_set SEL0_CLK_MUX 0
gpio_export SEL1_CLK_MUX GPIOL1
gpio_set SEL1_CLK_MUX 0

# Output control for clock source from MB
# 1 = enable, 0 = high-impedance state
gpio_export OEA_CLK_MUX_N GPIOL2
gpio_set OEA_CLK_MUX_N 1
gpio_export OEB_CLK_MUX_N GPIOL3
gpio_set OEB_CLK_MUX_N 0

# ASIC warm reset
gpio_export WARMRST_BMC_N_ASIC7 GPIOL4
gpio_set WARMRST_BMC_N_ASIC7 1
gpio_export WARMRST_BMC_N_ASIC6 GPIOL5
gpio_set WARMRST_BMC_N_ASIC6 1

# To enable GPIOM
#devmem_clear_bit $(scu_addr 84) 24
#devmem_clear_bit $(scu_addr 84) 25
#devmem_clear_bit $(scu_addr 84) 26
#devmem_clear_bit $(scu_addr 84) 27
#devmem_clear_bit $(scu_addr 84) 28
#devmem_clear_bit $(scu_addr 84) 29

# Slave i2c alert (defined by OAM)
gpio_export SMB_ALERT_ASIC01 GPIOM2
gpio_export SMB_ALERT_ASIC23 GPIOM3
gpio_export SMB_ALERT_ASIC45 GPIOM4
gpio_export SMB_ALERT_ASIC67 GPIOM5

# PCIe switch GPIO (preserved)
gpio_export PAX2_SKU_ID1 GPIOM0
gpio_set PAX2_SKU_ID1 0
gpio_export PAX3_SKU_ID1 GPIOM6
gpio_set PAX3_SKU_ID1 0

# Power control for Debug
# 1 = controlled by BMC
# 0 = controlled by CPLD
gpio_export PWR_CTRL GPIOM1

for i in {0..3};
do
  PRSNT=MB${i}_PRSNT
  if [[ "${!PRSNT}" == "1" ]]; then
    echo "Server is detected"
    gpio_set PWR_CTRL 0
    break
  fi
done

# To enable GPION
#devmem_clear_bit $(scu_addr 88) 2
#devmem_clear_bit $(scu_addr 88) 3
#devmem_clear_bit $(scu_addr 88) 4
#devmem_clear_bit $(scu_addr 88) 5
#devmem_clear_bit $(scu_addr 88) 6
#devmem_clear_bit $(scu_addr 88) 7

# OAM debug pin (defined by OAM)
gpio_export BMC_OAM_TEST2 GPION4

# Board ID
gpio_export BOARD_ID0 GPION5
gpio_export BOARD_ID1 GPION6
gpio_export BOARD_ID2 GPION7

# To enable GPIOO
#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

# To enable GPIOP
#devmem_clear_bit $(scu_addr 88) 16
#devmem_clear_bit $(scu_addr 88) 17
#devmem_clear_bit $(scu_addr 88) 18
#devmem_clear_bit $(scu_addr 88) 19
#devmem_clear_bit $(scu_addr 88) 20
#devmem_clear_bit $(scu_addr 88) 21
#devmem_clear_bit $(scu_addr 88) 22
#devmem_clear_bit $(scu_addr 88) 23

# OAM debug pin (defined by OAM)
gpio_export BMC_OAM_TEST9 GPIOP0
gpio_export BMC_OAM_TEST10 GPIOP1
gpio_export BMC_OAM_TEST11 GPIOP2
gpio_export BMC_OAM_TEST12 GPIOP3

# ASIC presence
gpio_export PRSNT1_N_ASIC3 GPIOP4
gpio_export PRSNT1_N_ASIC2 GPIOP5
gpio_export PRSNT1_N_ASIC1 GPIOP6
gpio_export PRSNT1_N_ASIC0 GPIOP7

# To enable GPIOQ
#devmem_clear_bit $(scu_addr 2c) 1
#devmem_clear_bit $(scu_addr 2c) 29

# Battery voltage detect control
gpio_export BATTERY_DETECT GPIOQ6
gpio_set BATTERY_DETECT 0

# Reserved
gpio_export BMC_GPIOQ7 GPIOQ7

# To enable GPIOR
#devmem_clear_bit $(scu_addr 88) 25
#devmem_clear_bit $(scu_addr 88) 26
#devmem_clear_bit $(scu_addr 88) 27
#devmem_clear_bit $(scu_addr 88) 28
#devmem_clear_bit $(scu_addr 88) 29
#devmem_clear_bit $(scu_addr 88) 30
#devmem_clear_bit $(scu_addr 88) 31

# OAM debug mode enable
gpio_export SCALE_DEBUG_EN_N_ASIC3 GPIOR6
gpio_export SCALE_DEBUG_EN_N_ASIC2 GPIOR7

# To enable GPIOS
#devmem_clear_bit $(scu_addr 8C) 0
#devmem_clear_bit $(scu_addr 8C) 1
#devmem_clear_bit $(scu_addr 8C) 2
#devmem_clear_bit $(scu_addr 8C) 3
#devmem_clear_bit $(scu_addr 8C) 4
#devmem_clear_bit $(scu_addr 8C) 5
#devmem_clear_bit $(scu_addr 8C) 6
#devmem_clear_bit $(scu_addr 8C) 7

# HSC throttle
gpio_export HSC1_THROT_N GPIOS0
gpio_export HSC2_THROT_N GPIOS1

# Set target OAM for test
gpio_export CPLD_MUX_ID3 GPIOS2
gpio_set CPLD_MUX_ID3 0

# PMBUS alert from HSC
gpio_export PMBUS_BMC_1_ALERT_N GPIOS3

# To enable GPIOT
#devmem_clear_bit $(scu_addr 48) 29
#devmem_clear_bit $(scu_addr 48) 30
#devmem_set_bit $(scu_addr a0) 0
#devmem_set_bit $(scu_addr a0) 4
#devmem_set_bit $(scu_addr a0) 5
#devmem_set_bit $(scu_addr a0) 6

# OAM debug mode enable
gpio_export SCALE_DEBUG_EN_N_ASIC6 GPIOT0
gpio_export SCALE_DEBUG_EN_N_ASIC7 GPIOT6
gpio_export PD_GPIOT7 GPIOT7

# To enable GPIOU
#devmem_set_bit $(scu_addr a0) 10
#devmem_set_bit $(scu_addr a0) 11
#devmem_set_bit $(scu_addr a0) 13
gpio_export PD_GPIOU1 GPIOU1
gpio_export PD_GPIOU2 GPIOU2
gpio_export PD_GPIOU3 GPIOU3

# PCIe switch firmware recovery pin
gpio_export BMC_BOOT_RCVR_B0_PAX0 GPIOU4
gpio_set BMC_BOOT_RCVR_B0_PAX0 0
gpio_export BMC_BOOT_RCVR_B0_PAX1 GPIOU5
gpio_set BMC_BOOT_RCVR_B0_PAX1 0
gpio_export BMC_BOOT_RCVR_B0_PAX2 GPIOU6
gpio_set BMC_BOOT_RCVR_B0_PAX2 0
gpio_export BMC_BOOT_RCVR_B0_PAX3 GPIOU7
gpio_set BMC_BOOT_RCVR_B0_PAX3 0

# To enable GPIOV
#devmem_set_bit $(scu_addr a0) 19

# Debug connector presence
# 1 = no debug port
gpio_export JTAG_0_SCR_PRESENT GPIOV1

# OAM JTAG SEL
gpio_export BMC2OAM_JTAG_PT_EN GPIOV2
gpio_set BMC2OAM_JTAG_PT_EN 0
gpio_export BMC2OAM_JTAG_PT_SEL GPIOV3
gpio_set BMC2OAM_JTAG_PT_SEL 0

# Reset eMMC on BSM
# 0 = reset, 1 = not reset
gpio_export BMC_EMMC_RST_R0_N GPIOV4
gpio_set BMC_EMMC_RST_R0_N 1

# PMBUS alert from HSC
gpio_export PMBUS_BMC_3_ALERT_N GPIOV5
gpio_export PMBUS_BMC_2_ALERT_N GPIOV6

# i2c alert from CPLD
gpio_export CPLD_SMB_ALERT_N GPIOV7

# To enable GPIOY
#devmem_clear_scu70_bit 19
#devmem_clear_bit $(scu_addr 94) 10
#devmem_clear_bit $(scu_addr 94) 11
#devmem_clear_bit $(scu_addr a4) 8
#devmem_clear_bit $(scu_addr a4) 9
#devmem_clear_bit $(scu_addr a4) 10
#devmem_clear_bit $(scu_addr a4) 11

# ASIC presence
gpio_export PRSNT1_N_ASIC7 GPIOY0
gpio_export PRSNT1_N_ASIC6 GPIOY1
gpio_export PRSNT1_N_ASIC5 GPIOY2
gpio_export PRSNT1_N_ASIC4 GPIOY3

# To enable GPIOZ
#devmem_clear_bit $(scu_addr 90) 31
#devmem_clear_bit $(scu_addr a4) 16
#devmem_clear_bit $(scu_addr a4) 17
#devmem_clear_bit $(scu_addr a4) 18
#devmem_clear_bit $(scu_addr a4) 19
#devmem_clear_bit $(scu_addr a4) 20
#devmem_clear_bit $(scu_addr a4) 21
#devmem_clear_bit $(scu_addr a4) 22
#devmem_clear_bit $(scu_addr a4) 23

# Select USB from MB
# [S1:S0]
# 1:1 = MB#0
# 1:0 = MB#1
# 0:1 = MB#2
# 0:0 = MB#3
# PCH
gpio_export USB2_SEL0_U42 GPIOZ0
gpio_set USB2_SEL0_U42 1
gpio_export USB2_SEL1_U42 GPIOZ1
gpio_set USB2_SEL1_U42 1
# BMC
gpio_export USB2_SEL0_U43 GPIOZ2
gpio_set USB2_SEL0_U43 1
gpio_export USB2_SEL1_U43 GPIOZ3
gpio_set USB2_SEL1_U43 1

# To enable GPIOAA
#devmem_clear_bit $(scu_addr a4) 24
#devmem_clear_bit $(scu_addr a4) 25
#devmem_clear_bit $(scu_addr a4) 26
#devmem_clear_bit $(scu_addr a4) 27
#devmem_clear_bit $(scu_addr a4) 28
#devmem_clear_bit $(scu_addr a4) 29
#devmem_clear_bit $(scu_addr a4) 30
#devmem_clear_bit $(scu_addr a4) 31

# SPI flash select of PCIe switch
# 0: access by PCIe switch
# 1: access by BMC
gpio_export SEL_FLASH_PAX0 GPIOAA0
gpio_set SEL_FLASH_PAX0 0
gpio_export SEL_FLASH_PAX1 GPIOAA1
gpio_set SEL_FLASH_PAX1 0
gpio_export SEL_FLASH_PAX2 GPIOAA5
gpio_set SEL_FLASH_PAX2 0
gpio_export SEL_FLASH_PAX3 GPIOAA6
gpio_set SEL_FLASH_PAX3 0

# System log LED
gpio_export SYSTEM_LOG_LED GPIOAA2
gpio_set SYSTEM_LOG_LED 0

# Select USB from PCH/BMC of MB
# 0: BMC
# 1: PCH
gpio_export SEL_USB_MUX GPIOAA3
gpio_set SEL_USB_MUX 0

# SPI BMC write protection
gpio_export SPI_BMC_BT_WP0_N GPIOAA4
gpio_set SPI_BMC_BT_WP0_N 1

# FRU on BSM write protection
# 0 = writable
gpio_export FRU_WP GPIOAA7
gpio_set FRU_WP 0

# To enable GPIOAB
#devmem_clear_bit $(scu_addr a8) 0
#devmem_clear_bit $(scu_addr a8) 1
#devmem_clear_bit $(scu_addr a8) 3

# Set target OAM for test
gpio_export CPLD_MUX_ID0 GPIOAB1
gpio_set CPLD_MUX_ID0 0
gpio_export CPLD_MUX_ID1 GPIOAB0
gpio_set CPLD_MUX_ID1 0
gpio_export CPLD_MUX_ID2 GPIOAB3
gpio_set CPLD_MUX_ID2 0

# To enable GPIOAC
devmem_clear_bit $(scu_addr ac) 0
devmem_clear_bit $(scu_addr ac) 1
devmem_clear_bit $(scu_addr ac) 2
devmem_clear_bit $(scu_addr ac) 3
devmem_clear_bit $(scu_addr ac) 5
devmem_clear_bit $(scu_addr ac) 6
devmem_clear_bit $(scu_addr ac) 7

# PCIe switch GPIO (preserved)
gpio_export PAX0_SKU_ID0 GPIOAC0
gpio_export PAX0_ALERT GPIOAC1
gpio_export PAX1_SKU_ID0 GPIOAC2
gpio_export PAX1_ALERT GPIOAC3

# Reserved
gpio_export BMC_GPIOAC5 GPIOAC5
gpio_export BMC_GPIOAC6 GPIOAC6
gpio_export BMC_GPIOAC7 GPIOAC7


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

# Keep "power-util mb on" for debug purpose
echo "Waiting for power up"
pwr_ctrl=$(gpio_get PWR_CTRL keepdirection)
pwr_ready=$(gpio_get SYS_PWR_READY keepdirection)
board_id=$(gpio_get BOARD_ID0 keepdirection)
while [ "$pwr_ready" != "1" ]
do
  if [[ "$pwr_ctrl" == "1" ]]; then
    power-util mb on > /dev/null
  fi
  sleep 1
  pwr_ready=$(gpio_get SYS_PWR_READY keepdirection)
done

# Thermal sensors for PCIe switches
i2c_device_add 6 0x4d tmp422
i2c_device_add 6 0x4e tmp422

# Voltage regulators
if [ $board_id -eq 1 ]; then
  i2c_device_add 5 0x60 isl69260
  i2c_device_add 5 0x61 isl69260
  i2c_device_add 5 0x72 isl69260
  i2c_device_add 5 0x75 isl69260
  i2c_device_add 5 0x34 mpq8645p
  i2c_device_add 5 0x35 mpq8645p
  i2c_device_add 5 0x36 mpq8645p
  i2c_device_add 5 0x3b mpq8645p
else
  i2c_device_add 5 0x30 mpq8645p
  i2c_device_add 5 0x31 mpq8645p
  i2c_device_add 5 0x32 mpq8645p
  i2c_device_add 5 0x33 mpq8645p
  i2c_device_add 5 0x34 mpq8645p
  i2c_device_add 5 0x35 mpq8645p
  i2c_device_add 5 0x36 mpq8645p
  i2c_device_add 5 0x3b mpq8645p
fi

# P48V HSCs
i2c_bind_driver adm1275 16-0013
i2c_bind_driver adm1275 17-0010

# i2c mux in front of OAMs
i2c_mux_add_sync 11 0x70 pca9543 2
i2c_mux_add_sync 10 0x70 pca9543 2
i2c_mux_add_sync 9 0x70 pca9543 2
i2c_mux_add_sync 8 0x70 pca9543 2

# Reload lm_sensors config
sv restart sensord > /dev/null
sv restart ipmid > /dev/null
# Mark driver loaded
echo > /tmp/driver_probed


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

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

# Provision Status
for i in {1..3}; do
  pfr=$(/usr/sbin/i2cget -y 4 0x58 0x0a b 2>/dev/null)
  if [ $? -eq 0 ]; then
    pfr=$((pfr & 0x20))
    break
  fi
  pfr=255
  sleep 0.5
done

if [ $pfr -eq 255 ]; then
  exit
fi

for i in {1..3}; do
  c0=$(/usr/sbin/i2cget -y 4 0x58 0xc0 b 2>/dev/null)
  [  $? -eq 0 ] && c0=$((c0 & 0xff))
  c1=$(/usr/sbin/i2cget -y 4 0x58 0xc1 b 2>/dev/null)
  [  $? -eq 0 ] && c1=$((c1 & 0xff))

  offs=$(devmem 0x1e72120c 2>/dev/null)
  start=$((offs & 0xff))
  end=$(((offs >> 8) & 0xff))

  sleep 1
  if [[ "$c0" == "$start" && "$c1" == "$end" ]]; then
    break
  fi
done

# CHKPT_COMPLETE
for i in {1..3}; do
  /usr/sbin/i2cset -y 4 0x58 0x0f 0x09 b > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    break
  fi
  sleep 0.5
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-por
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

store_clear_por


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

### BEGIN INIT INFO
# Provides:          switch-setup
# Required-Start:
# Required-Stop:
# Default-Start:     5
# Default-Stop:
# Short-Description:  Set up strap pins to determine the switch configuration
### END INIT INFO

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

echo -n "Setup PCIe switch configuration "
# 4SEX by default
gpio_set PAX0_SKU_ID0 0
gpio_set PAX0_SKU_ID1 0
gpio_set PAX1_SKU_ID0 0
gpio_set PAX1_SKU_ID1 0
gpio_set PAX2_SKU_ID0 0
gpio_set PAX2_SKU_ID1 0
gpio_set PAX3_SKU_ID0 0
gpio_set PAX3_SKU_ID1 0

server_type=$(kv get server_type persistent)
if [ $? -ne 0 ]; then
  # Get config from MB0's BMC
  for retry in {1..200};
  do
    val=$(/usr/local/bin/ipmb-util 2 0x20 0xE8 0x0)
    val=${val:1:1}
    if [[ "$val" == "0" ]]; then
      /usr/local/bin/cfg-util server_type 8
      server_type=8
      break
    elif [[ "$val" == "1" ]]; then
      /usr/local/bin/cfg-util server_type 4
      server_type=4
      break
    elif [[ "$val" == "2" ]]; then
      /usr/local/bin/cfg-util server_type 2
      server_type=2
      break
    else
      echo -n "."
      sleep 1
    fi
  done
else
  # TODO:
  #   Sync EEPROM with cache for those old systems
  #   We can remove this after all systems got updated
  val=$(ipmitool i2c bus=6 0xA8 0x1 0x4 0x6)
  val=${val:2:1}
  if [[ "$val" != "$server_type" ]]; then
    /usr/local/bin/cfg-util server_type $server_type
  fi
fi

case $server_type in
  8)
    gpio_set PAX0_SKU_ID0 0
    gpio_set PAX0_SKU_ID1 1
    gpio_set PAX1_SKU_ID0 0
    gpio_set PAX1_SKU_ID1 1
    gpio_set PAX2_SKU_ID0 0
    gpio_set PAX2_SKU_ID1 1
    gpio_set PAX3_SKU_ID0 0
    gpio_set PAX3_SKU_ID1 1
    ;;
  4)
    gpio_set PAX0_SKU_ID0 0
    gpio_set PAX0_SKU_ID1 0
    gpio_set PAX1_SKU_ID0 0
    gpio_set PAX1_SKU_ID1 0
    gpio_set PAX2_SKU_ID0 0
    gpio_set PAX2_SKU_ID1 0
    gpio_set PAX3_SKU_ID0 0
    gpio_set PAX3_SKU_ID1 0
    ;;
  2)
    gpio_set PAX0_SKU_ID0 1
    gpio_set PAX0_SKU_ID1 0
    gpio_set PAX1_SKU_ID0 1
    gpio_set PAX1_SKU_ID1 0
    gpio_set PAX2_SKU_ID0 1
    gpio_set PAX2_SKU_ID1 0
    gpio_set PAX3_SKU_ID0 1
    gpio_set PAX3_SKU_ID1 0
    ;;
esac
echo "done"

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

addr=$(/usr/bin/mac-util --get)
if [[ "$addr" == "ff:ff:ff:ff:ff:ff" ]]
then
  addr="02:00:00:00:00:01"
fi
modprobe g_ether host_addr=02:00:00:00:00:02 dev_addr=$addr

ifconfig usb0 up

dhclient -d -pf /var/run/dhclient.usb0.pid usb0 > /dev/null 2>&1 &


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

# Sync RTC with NTP
sync_rtc()
{
  while [ "$(ntpq -c rv | grep leap_none)" == "" ]
  do
    sleep 10
  done
  # if leap code returned normal sync state
  echo Syncing up RTC time with NTP...
  test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
  return
}

sync_rtc


#!/bin/sh

echo "Overwrite the register 0x1 of AUX HSC with 0x2"
i2cset -f -y 18 0x43 0x1 0x2

# Set the OCW of P12V_1/P12V_2 HSC to ~290 Amps then enable it

i2cset -f -y 17 0x40 0xD 0xB9
i2cset -f -y 16 0x53 0xD 0xB9

val=$(i2cget -f -y 17 0x40 0x3)
alert_reg=$(($val|0x20))
i2cset -f -y 17 0x40 0x3 $alert_reg
val=$(i2cget -f -y 16 0x53 0x3)
alert_reg=$(($val|0x20))
i2cset -f -y 16 0x53 0x3 $alert_reg


#!/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 IPMI2 Message handler
### END INIT INFO

echo -n "Setup IPMI2 message handler... "
runsv /etc/sv/ipmid > /dev/null 2>&1 &
echo "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
#
mTerm_server_running() {
  # shellcheck disable=SC2009
  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() {
  echo "You are in SOL session."
  echo "Use ctrl-x to quit."
  echo "-----------------------"
  echo

  /usr/bin/microcom -s @BAUDRATE@ /dev/@TERM@

  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/bash
#
# Copyright 2022-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
#

#
# OVERRIDE_ME: this file needs to be overridden by platform!
#

userver_power_is_on() {
    echo "userver_power_is_on() not implemented. Assuming uServer is off!"
    return 1
}

userver_power_on() {
    echo "userver_power_on() not implemented. Exiting.."
    return 1
}

userver_power_off() {
    echo "userver_power_off() not implemented. Exiting.."
    return 1
}

userver_reset() {
    echo "userver_reset() not implemented. Exiting.."
    return 1
}

chassis_power_cycle() {
    echo "chassis_power_cycle() not implemented. Exiting.."
    return 1
}

bmc_mac_addr() {
    echo ""
}

userver_mac_addr() {
    echo ""
}


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

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

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

echo "Checking x86 (userver) power status... "
if userver_power_is_on; then
    echo "userver is already powered. Skipped."
    exit 0
fi

wedge_power.sh on -f


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

#
# OVERRIDE_ME: this file needs to be overridden by platform!
#
echo "setup-gpio.sh is empty. Exiting."
exit 0


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

#
# OVERRIDE_ME: this file needs to be overridden by platform!
#

echo "setup_board.sh is empty. Exiting."
exit 0


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

#
# OVERRIDE_ME: this file needs to be overridden by platform!
#
echo "setup_i2c.sh is empty. Exiting."
exit 0


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

# shellcheck disable=SC1091
. /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 x86 (userver) power status"
    echo
    echo "  on: Power on x86 (userver) if not powered on already"
    echo "    options:"
    echo "      -f: Re-do power on sequence no matter if x86 (userver) has "
    echo "          been powered on or not."
    echo
    echo "  off: Power off x86 (userver) ungracefully"
    echo
    echo "  reset: Power reset x86 (userver) ungracefully"
    echo "    options:"
    echo "      -s: Power cycle the whole chassis ungracefully"
    echo
    echo
}

do_status() {
    echo -n "Microserver power is "

    if userver_power_is_on; then
        echo "on"
    else
        echo "off"
    fi

    return 0
}

do_on() {
    local opt
    local force=0

    while getopts "f" opt; do
        case $opt in
            f)
                force=1
                ;;
            *)
                usage
                exit 1
                ;;
        esac
    done

    if [ $force -eq 0 ]; then
        if userver_power_is_on; then
            echo " uServer is already on. Skipped."
            return 0
        fi
    fi

    logger -p user.crit "Power on x86 (userver) ..."
    echo -n "Power on x86 (userver) ..."
    if userver_power_on; then
        echo " Done"
        return 0
    fi

    echo " Failed"
    return 1
}

do_off() {
    logger -p user.crit "Power off x86 (userver) ..."
    echo -n "Power off x86 (userver) ..."

    if userver_power_off; then
        echo " Done"
        return 0
    fi

    echo " Failed"
    return 1
}

do_reset() {
    local opt
    local system=0

    while getopts "st:" opt; do
        case $opt in
            s)
                system=1
                ;;
            *)
                usage
                exit 1
                ;;
        esac
    done

    if [ $system -eq 1 ]; then
        logger -p user.crit "Power reset the whole system ..."
        echo  "Power reset the whole system ..."

        sync

        chassis_power_cycle

        sleep 3
        # Control should not reach here!!!
        logger -p user.crit "Failed to reset the system!!!"
        echo "Failed to reset the system!!!"
        exit 1
    else
        if userver_power_is_on; then
            logger -p user.crit "Power reset x86 (userver) ..."
            echo -n "Power reset x86 (userver) ..."

            userver_reset
            echo " Done"
        else
            echo "x86 (userver) is off. Power on x86 now.."
            do_on
        fi
    fi
}

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

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

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

userver_mac_addr


#!/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
# shellcheck source=/dev/null
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

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

        # Generate symlink for tcsd
        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/bash

PID=$$
# File format autodump<fru>.pid (See pal_is_crashdump_ongoing()
# function definition)
PID_FILE='/var/run/autodump1.pid'
DWR_FILE='/tmp/DWR'

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

# check if running auto dump
[ -r $PID_FILE ] && OLDPID=`cat $PID_FILE` || OLDPID=''

# Set current pid
echo $PID > $PID_FILE

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

# kill previous autodump if exist
if [ ! -z "$OLDPID" ] && (grep "autodump" /proc/$OLDPID/cmdline &> /dev/null) ; then
  LOG_FILE='/tmp/autodump.log'
  LOG_ARCHIVE='/mnt/data/autodump_uncompleted.tar.gz'
  echo -n "(uncompleted) Auto Dump End at " >> $LOG_FILE
  date >> $LOG_FILE

  tar zcf $LOG_ARCHIVE -C `dirname $LOG_FILE` `basename $LOG_FILE` && \
  rm -rf $LOG_FILE && \

  echo "kill pid $OLDPID..."
  kill -s 9 $OLDPID
  killall peci-util &> /dev/null
fi
unset OLDPID

DUMP_SCRIPT='/usr/local/bin/dump.sh'
LOG_FILE='/tmp/autodump.log'
LOG_ARCHIVE='/mnt/data/autodump.tar.gz'
LOG_MSG_PREFIX=""

CPU1_SKTOCC_N="FM_CPU1_SKTOCC_LVT3_PLD_N"
DWR=0
SECOND_DUMP=0
DELAY_SEC=30
SENSOR_HISTORY=180

while test $# -gt 0
do
  case "$1" in
  --dwr)
    DWR=1
    ;;
  --now)
    DELAY_SEC=0
    ;;
  --second)
    SECOND_DUMP=1
    ;;
  *)
    echo "unknown argument $1"
    ;;
  esac
  shift
done

if [ "$DWR" == "1" ] || [ "$SECOND_DUMP" == "1" ]; then
  echo "Auto Dump after System Reset or Demoted Warm Reset"
fi

if [ "$DELAY_SEC" != "0" ]; then
  echo "Auto Dump will start after ${DELAY_SEC}s..."

  sleep ${DELAY_SEC}
fi

echo "Auto Dump Started"

# Start to dump and clear LOG_FILE
echo -n "Auto Dump Start at " > $LOG_FILE
date >> $LOG_FILE

# CPU0
$DUMP_SCRIPT 48 coreid >> $LOG_FILE 2>&1
$DUMP_SCRIPT 48 msr >> $LOG_FILE 2>&1

# CPU1
if [ "$(gpio_get_value ${CPU1_SKTOCC_N})" -eq "0" ]; then
  $DUMP_SCRIPT 49 coreid >> $LOG_FILE 2>&1
  $DUMP_SCRIPT 49 msr >> $LOG_FILE 2>&1
else
  echo "<<---------- CPU1 is not present, skip it. ---------->>" >> $LOG_FILE
fi

# PCIe
#$DUMP_SCRIPT pcie >> $LOG_FILE 2>&1

# Sensors
echo "Sensor history of last ${SENSOR_HISTORY}s at dump:" >> $LOG_FILE 2>&1
/usr/local/bin/sensor-util all --history $SENSOR_HISTORY >> $LOG_FILE 2>&1

echo "Sensor threshold at dump: " >> $LOG_FILE 2>&1
/usr/local/bin/sensor-util all --threshold >> $LOG_FILE 2>&1

# only second/dwr autodump need to rename accordingly
if [ "$DWR" == "1" ] || [ "$SECOND_DUMP" == "1" ]; then
# dwr
$DUMP_SCRIPT dwr >> $LOG_FILE 2>&1

# rename the archieve file based on whether dump in DWR mode or not
if [ "$?" == "2" ]; then
  LOG_ARCHIVE='/mnt/data/autodump_dwr.tar.gz'
  LOG_MSG_PREFIX="DWR "
else
  LOG_ARCHIVE='/mnt/data/autodump_second.tar.gz'
  LOG_MSG_PREFIX="SECOND_DUMP "
fi
fi

echo -n "Auto Dump End at " >> $LOG_FILE
date >> $LOG_FILE

tar zcf $LOG_ARCHIVE -C `dirname $LOG_FILE` `basename $LOG_FILE` && \
rm -rf $LOG_FILE && \
logger -t "ipmid" -p daemon.crit "${LOG_MSG_PREFIX}Crashdump for FRU: 1 is generated at $LOG_ARCHIVE"

# Remove current pid file
rm $PID_FILE
rm -r $DWR_FILE >/dev/null 2>&1

echo "${LOG_MSG_PREFIX}Auto Dump Stored in $LOG_ARCHIVE"
exit 0


#!/bin/bash

PROGRAM=$0
PECI_UTIL="/usr/local/bin/peci-util"
CMD_DIR="/etc/peci"

function print_help_msg {
  echo "$PROGRAM 48 coreid  ==> for CPU 1 CoreID"
  echo "$PROGRAM 49 coreid  ==> for CPU 2 CoreID"
  echo "$PROGRAM 48 msr     ==> for CPU 1 MSR"
  echo "$PROGRAM 49 msr     ==> for CPU 2 MSR"
  echo "$PROGRAM pcie       ==> for PCIe"
  echo "$PROGRAM dwr        ==> for DWR check"
}

# read command from stdin
function execute_via_peci {
  # if PECI_UTIL cannot be executed, return
  [ -x $PECI_UTIL ] || \
    return

  cat | \
    $PECI_UTIL -i 10 -r 5 --file /dev/stdin
}

# read command from stdin
function execute_cmd {
  # echo "Use BMC wired PECI interface"
  cat | execute_via_peci
}

# ARG: Bus[8], Device[5], Function[3], Register[12]
function PCI_Config_Addr {
  RESULT=0
  # BUS
  RESULT=$((RESULT | (($1 & 0xFF) << 20) ))
  # DEV
  RESULT=$((RESULT | (($2 & 0x1F) << 15) ))
  # FUN
  RESULT=$((RESULT | (($3 & 0x7) << 12) ))
  # REG
  RESULT=$((RESULT | (($4 & 0xFFF) << 0) ))
  printf "0x%x 0x%x 0x%x 0x%x"\
    $(( RESULT & 0xFF )) \
    $(( (RESULT >> 8) & 0xFF )) \
    $(( (RESULT >> 16) & 0xFF )) \
    $(( (RESULT >> 24) & 0xFF ))
  return 0
}

function pcie_dump {
  # CPU and PCH
  [ -r $CMD_DIR/crashdump_pcie ] && \
    cat $CMD_DIR/crashdump_pcie | execute_cmd

  # Buses
  RES=$(echo "0x30 0x06 0x05 0x61 0x00 0xcc 0x20 0x04 0x00" | execute_cmd)
  echo "Get CPUBUSNO: $RES"
  # Completion Code
  CC=$(echo $RES| awk '{print $1;}')
  # Root port buses
  ROOT_BUSES=$(echo $RES | awk '{printf "0x%s 0x%s 0x%s", $3, $4, $5;}')

  # Success
  if [ "$CC" == "40" ]; then
    for ROOT in $ROOT_BUSES; do
      for DEV in {0..3}; do
        echo "Find Root Port Bus $ROOT Dev $DEV..."
        RES=$(echo "0x30 0x06 0x05 0x61 0x00 $(PCI_Config_Addr $ROOT $DEV 0x00 0x18)" | execute_cmd)
        echo "$RES"
        CC=$(echo $RES| awk '{print $1;}')
        SECONDARY_BUS=0x$(echo $RES| awk '{print $3;}')
        SUBORDINATE_BUS=0x$(echo $RES| awk '{print $4;}')
        if [ "$CC" == "40" ] && [ "$SECONDARY_BUS" != "0x00" ] && [ "$SUBORDINATE_BUS" != "0x00" ]; then
          for (( BUS=$SECONDARY_BUS; BUS<=$SUBORDINATE_BUS; BUS++ )); do
            BUS_MS_NIBBLE=$(printf "%x" $((BUS >> 4)) )
            BUS_LS_NIBBLE=$(printf "%x" $((BUS & 0xF)) )
            [ -r $CMD_DIR/crashdump_pcie_bus ] && \
              sed "s/<BUSM>/$BUS_MS_NIBBLE/g; s/<BUSL>/$BUS_LS_NIBBLE/g" $CMD_DIR/crashdump_pcie_bus | execute_cmd
          done
        fi
      done
    done
  fi
}

function dwr_dump {

  echo
  echo DWR assert check:
  echo =================
  echo

  # Get biosscratchpad7[26]: DWR assert check
  RES=$(echo "0x30 0x06 0x05 0x61 0x00 0xbc 0x20 0x04 0x00" | execute_cmd)

  echo "< Get biosscratchpad7[26]: DWR assert check >"
  echo "$RES"
  echo

  # Completion Code
  CC=$(echo $RES| awk '{print $1;}')
  DWR=0x$(echo $RES| awk '{print $5;}')

  # Success
  if [ "$CC" == "40" ]; then
    if [ $((DWR & 0x04)) == 4 ]; then
      echo "Auto Dump in DWR mode"
      return 2
    else
      echo "Auto Dump in non-DWR mode"
      return 1
    fi
  else
    echo "get DWR mode fail"
    return 0
  fi
}

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

echo ""

if [ "$#" -eq 1 ] && [ $1 = "pcie" ]; then

  pcie_dump

  exit 0
fi

if [ "$#" -eq 1 ] && [ $1 = "dwr" ]; then

  dwr_dump

  exit $?
fi

if [ "$#" -ne 2 ]; then

  print_help_msg

  exit 1

elif [ "$1" -ne 48 ] && [ "$1" -ne 49 ]; then

  print_help_msg

  exit 1

elif [ "$2" = "coreid" ]; then

  if [ "$1" -eq 48 ]; then
    [ -r $CMD_DIR/crashdump_p0_coreid ] && \
      cat $CMD_DIR/crashdump_p0_coreid | execute_cmd
  elif [ "$1" -eq 49 ]; then
    [ -r $CMD_DIR/crashdump_p1_coreid ] && \
      cat $CMD_DIR/crashdump_p1_coreid | execute_cmd
  fi

  exit 0

elif [ "$2" = "msr" ]; then

  if [ "$1" -eq 48 ]; then
    [ -r $CMD_DIR/crashdump_p0_msr ] && \
      cat $CMD_DIR/crashdump_p0_msr | execute_cmd
  elif [ "$1" -eq 49 ]; then
    [ -r $CMD_DIR/crashdump_p1_msr ] && \
      cat $CMD_DIR/crashdump_p1_msr | execute_cmd
  fi

  exit 0

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

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

echo -n "Setup Front Panel Daemon.."
  /usr/local/bin/front-paneld > /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
#

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

echo "Setup fan speed... "
/usr/local/bin/fan-util --set 70
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
#

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

echo -n "Setup gpio monitoring for Sonorapass... "

/usr/local/bin/gpiod &

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
#

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

. /usr/local/fbpackages/utils/ast-functions
echo -n "Starting IPMB Rx/Tx Daemon.."

echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-0/new_device  #USB DBG
echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device  #ME

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

echo "done."


#!/bin/bash

BUS3_CHAN2_EXTEND_BUSNUM=34
BUS3_CHAN3_EXTEND_BUSNUM=35
U11_ADDR=0x60
U13_ADDR=0x6d
U95_ADDR=0x6f

# Disable U13 IDT_9QXL2001BNHGI8 - DIF0~3
U13_byte1=$(/usr/bin/i2craw -w 0 -r 10 $BUS3_CHAN2_EXTEND_BUSNUM $U13_ADDR | sed '$!d' | tr -s ' '| cut -d' ' -f4)
U13_byte1_val=${U13_byte1:2:2}
mod_U13_byte1_val=$[$((16#${U13_byte1_val})) & $((16#F0))]
# Disable U13 IDT_9QXL2001BNHGI8 - DIF13~15
U13_byte2=$(/usr/bin/i2craw -w 0 -r 10 $BUS3_CHAN2_EXTEND_BUSNUM $U13_ADDR | sed '$!d' | tr -s ' '| cut -d' ' -f5)
U13_byte2_val=${U13_byte2:2:2}
mod_U13_byte2_val=$[$((16#${U13_byte2_val})) & $((16#1F))]

/usr/bin/i2craw -w "1 2 $mod_U13_byte1_val $mod_U13_byte2_val" $BUS3_CHAN2_EXTEND_BUSNUM $U13_ADDR

# Disable U95 IDT_9QXL2001BNHGI8 - DIF16~18
U95_byte0=$(/usr/bin/i2craw -w 0 -r 10 $BUS3_CHAN2_EXTEND_BUSNUM $U95_ADDR | sed '$!d' | tr -s ' '| cut -d' ' -f3)
U95_byte0_val=${U95_byte0:2:2}
mod_U95_byte0_val=$[$((16#${U95_byte0_val})) & $((16#C7))]

# Disable U95 IDT_9QXL2001BNHGI8 - DIF3~4
U95_byte1=$(/usr/bin/i2craw -w 0 -r 10 $BUS3_CHAN2_EXTEND_BUSNUM $U95_ADDR | sed '$!d' | tr -s ' '| cut -d' ' -f4)
U95_byte1_val=${U95_byte1:2:2}
mod_U95_byte1_val=$[$((16#${U95_byte1_val})) & $((16#E7))]

# Disable U95 IDT_9QXL2001BNHGI8 - DIF13~15
U95_byte2=$(/usr/bin/i2craw -w 0 -r 10 $BUS3_CHAN2_EXTEND_BUSNUM $U95_ADDR | sed '$!d' | tr -s ' '| cut -d' ' -f5)
U95_byte2_val=${U95_byte2:2:2}
mod_U95_byte2_val=$[$((16#${U95_byte2_val})) & $((16#1F))]

/usr/bin/i2craw -w "0 3 $mod_U95_byte0_val $mod_U95_byte1_val $mod_U95_byte2_val" $BUS3_CHAN2_EXTEND_BUSNUM $U95_ADDR

# Disable U11 IDT_9FGP204BKLFT - RMII0, RMII5, RGMII1 and RGMII0
U11_byte1=$(/usr/bin/i2craw -w 0 -r 10 $BUS3_CHAN3_EXTEND_BUSNUM $U11_ADDR | sed '$!d' | tr -s ' '| cut -d' ' -f4)
U11_byte1_val=${U11_byte1:2:2}
mod_U11_byte1_val=$[$((16#${U11_byte1_val})) & $((16#79))]

/usr/bin/i2craw -w "1 1 $mod_U11_byte1_val" $BUS3_CHAN3_EXTEND_BUSNUM $U11_ADDR


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

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

# FM_BOARD_BMC_REV_ID0
gpio_export FM_BOARD_BMC_REV_ID0 GPIOA0

# FM_BOARD_BMC_REV_ID1 / FM_CPU0_THERMTRIP_LVT3_PLD_N
gpio_export FM_CPU0_THERMTRIP_LVT3_PLD_N GPIOA1
ln -n /tmp/gpionames/FM_CPU0_THERMTRIP_LVT3_PLD_N /tmp/gpionames/FM_BOARD_BMC_REV_ID1

# FM_SPD_DDRCPU_LVLSHFT_EN
gpio_export FM_SPD_DDRCPU_LVLSHFT_EN GPIOA3

# FM_BOARD_BMC_SKU_ID0 / FM_MEM_THERM_EVENT_CPU0_LVT3_N
gpio_export FM_MEM_THERM_EVENT_CPU0_LVT3_N GPIOB0
ln -n /tmp/gpionames/FM_MEM_THERM_EVENT_CPU0_LVT3_N /tmp/gpionames/FM_BOARD_BMC_SKU_ID0

# FM_BOARD_BMC_SKU_ID1 / FM_MEM_THERM_EVENT_CPU1_LVT3_N
gpio_export FM_MEM_THERM_EVENT_CPU1_LVT3_N GPIOB1
ln -n /tmp/gpionames/FM_MEM_THERM_EVENT_CPU1_LVT3_N /tmp/gpionames/FM_BOARD_BMC_SKU_ID1

# FM_BOARD_BMC_SKU_ID2 / FM_CPU0_FIVR_FAULT_LVT3_PLD
gpio_export FM_CPU0_FIVR_FAULT_LVT3_PLD GPIOB2
ln -n /tmp/gpionames/FM_CPU0_FIVR_FAULT_LVT3_PLD /tmp/gpionames/FM_BOARD_BMC_SKU_ID2

# FM_BOARD_BMC_SKU_ID3 / FM_CPU1_FIVR_FAULT_LVT3_PLD
gpio_export FM_CPU1_FIVR_FAULT_LVT3_PLD GPIOB3
ln -n /tmp/gpionames/FM_CPU1_FIVR_FAULT_LVT3_PLD /tmp/gpionames/FM_BOARD_BMC_SKU_ID3

# FM_BOARD_BMC_SKU_ID4
gpio_export FM_BOARD_BMC_SKU_ID4 GPIOB4

# FM_BOARD_BMC_SKU_ID5
gpio_export FM_BOARD_BMC_SKU_ID5 GPIOB5

# FM_CPU0_PROCHOT_LVT3_BMC_N
gpio_export FM_CPU0_PROCHOT_LVT3_BMC_N GPIOB6

# FM_CPU1_PROCHOT_LVT3_BMC_N
gpio_export FM_CPU1_PROCHOT_LVT3_BMC_N GPIOB7

# FM_BOARD_BMC_REV_ID2 / FM_CPU1_THERMTRIP_LVT3_PLD_N
gpio_export FM_CPU1_THERMTRIP_LVT3_PLD_N GPIOD0
ln -n /tmp/gpionames/FM_CPU1_THERMTRIP_LVT3_PLD_N /tmp/gpionames/FM_BOARD_BMC_REV_ID2

# IRQ_TPM_BMC_SPI_N
gpio_export IRQ_TPM_BMC_SPI_N GPIOD1

# OCP_V3_1_NIC_PWR_GOOD
gpio_export OCP_V3_1_NIC_PWR_GOOD GPIOD6

# OCP_V3_2_NIC_PWR_GOOD
gpio_export OCP_V3_2_NIC_PWR_GOOD GPIOD7

# FP_BMC_RST_BTN_N
gpio_export FP_BMC_RST_BTN_N GPIOE0

# RST_BMC_RSTBTN_OUT_R_N, power reset to PCH
gpio_export RST_BMC_RSTBTN_OUT_R_N GPIOE1
gpio_set RST_BMC_RSTBTN_OUT_R_N 1

# FM_BMC_PWR_BTN_R_N
gpio_export FM_BMC_PWR_BTN_R_N GPIOE2

# FM_BMC_PWRBTN_OUT_R_N, power button to CPLD
gpio_export FM_BMC_PWRBTN_OUT_R_N GPIOE3
gpio_set FM_BMC_PWRBTN_OUT_R_N 1

# FP_NMI_BTN_N
gpio_export FP_NMI_BTN_N GPIOE4

# IRQ_BMC_PCH_NMI
gpio_export IRQ_BMC_PCH_NMI GPIOE5

# FP_FAULT_LED_N
gpio_export FP_FAULT_LED_N GPIOE6
gpio_set FP_FAULT_LED_N 1

# ALT_PDB_N
gpio_export ALT_PDB_N GPIOE7

# FM_CPU_ERR0_LVT3_N
gpio_export FM_CPU_ERR0_LVT3_N GPIOF0

# FM_CPU_ERR1_LVT3_N
gpio_export FM_CPU_ERR1_LVT3_N GPIOF1

# FM_CPU_ERR2_LVT3_N
gpio_export FM_CPU_ERR2_LVT3_N GPIOF2

# FM_BMC_DBP_PRESENT_N
gpio_export FM_BMC_DBP_PRESENT_N GPIOF3

# FM_BMC_UV_ADR_TRIGGER_EN
gpio_export FM_BMC_UV_ADR_TRIGGER_EN GPIOF4

# FM_BMC_FORCE_ADR_N
gpio_export FM_BMC_FORCE_ADR_N GPIOF5

# RST_PLTRST_BMC_N
gpio_export RST_PLTRST_BMC_N GPIOF6

# JTAG_BMC_PRDY_N
gpio_export JTAG_BMC_PRDY_N GPIOF7

# FM_CPU1_DISABLE_COD_N
gpio_export FM_CPU1_DISABLE_COD_N GPIOG0

# FM_CPU_MSMI_CATERR_LVT3_N
gpio_export FM_CPU_MSMI_CATERR_LVT3_N GPIOG1

# FM_PCH_BMC_THERMTRIP_N
gpio_export FM_PCH_BMC_THERMTRIP_N GPIOG2

# BMC_READY_N
gpio_export BMC_READY_N GPIOG3
gpio_set BMC_READY_N 1
# To disable Reset Tolerant
devmem_clear_bit 0x1e78003c 19

# IRQ_NMI_EVENT_N
gpio_export IRQ_NMI_EVENT_N GPIOG4

# IRQ_BMC_PCH_SMI_LPC_N
gpio_export IRQ_BMC_PCH_SMI_LPC_N GPIOG5

# IRQ_SMB3_M2_ALERT_N
gpio_export IRQ_SMB3_M2_ALERT_N GPIOG6

# SLIVER_CONNA_BMC_ALERT_N
gpio_export SLIVER_CONNA_BMC_ALERT_N GPIOG7

# LED_POSTCODE_0
gpio_export LED_POSTCODE_0 GPIOH0
gpio_set LED_POSTCODE_0 0

# LED_POSTCODE_1
gpio_export LED_POSTCODE_1 GPIOH1
gpio_set LED_POSTCODE_1 0

# LED_POSTCODE_2
gpio_export LED_POSTCODE_2 GPIOH2
gpio_set LED_POSTCODE_2 0

# LED_POSTCODE_3
gpio_export LED_POSTCODE_3 GPIOH3
gpio_set LED_POSTCODE_3 0

# LED_POSTCODE_4
gpio_export LED_POSTCODE_4 GPIOH4
gpio_set LED_POSTCODE_4 0

# LED_POSTCODE_5
gpio_export LED_POSTCODE_5 GPIOH5
gpio_set LED_POSTCODE_5 0

# LED_POSTCODE_6
gpio_export LED_POSTCODE_6 GPIOH6
gpio_set LED_POSTCODE_6 0

# LED_POSTCODE_7
gpio_export LED_POSTCODE_7 GPIOH7
gpio_set LED_POSTCODE_7 0

devmem_set_bit 0x1e780068 24
devmem_clear_bit 0x1e78006c 24

# PRSNT_2C_PCIE_SLIVER_CABLE_1_N
gpio_export PRSNT_2C_PCIE_SLIVER_CABLE_1_N GPIOI0

# PRSNT_2C_PCIE_SLIVER_CABLE_2_N
gpio_export PRSNT_2C_PCIE_SLIVER_CABLE_2_N GPIOI1

# SLIVER_CONNB_BMC_ALERT_N
gpio_export SLIVER_CONNB_BMC_ALERT_N GPIOI2

# FM_PFR_ACTIVE_N
gpio_export FM_PFR_ACTIVE_N GPIOI3

# FM_UARTSW_LSB_N
gpio_export FM_UARTSW_LSB_N GPIOL0

# FM_UARTSW_MSB_N
gpio_export FM_UARTSW_MSB_N GPIOL1

# IRQ_HSC_FAULT_N
gpio_export IRQ_HSC_FAULT_N GPIOL2

# FM_CPU1_MEMHOT_OUT_N
gpio_export FM_CPU1_MEMHOT_OUT_N GPIOL3

# FM_MASTER_MB_N
gpio_export FM_MASTER_MB_N GPIOL4

# BMC_FORCE_NM_THROTTLE_N
gpio_export BMC_FORCE_NM_THROTTLE_N GPIOL5

# IRQ_OC_DETECT_N
gpio_export IRQ_OC_DETECT_N GPIOM0

# IRQ_UV_DETECT_N
gpio_export IRQ_UV_DETECT_N GPIOM1

# FM_HSC_TIMER_EXP_N
gpio_export FM_HSC_TIMER_EXP_N GPIOM2

# P3V3_STBY_MON_ALERT
gpio_export P3V3_STBY_MON_ALERT GPIOM3

# FM_TPM_BMC_PRES_N
gpio_export FM_TPM_BMC_PRES_N GPIOM4

# SPI_BMC_BT_WP0_N
gpio_export SPI_BMC_BT_WP0_N GPIOM5

# BOARD_ID_MUX_SEL
gpio_export BOARD_ID_MUX_SEL GPION2
gpio_set BOARD_ID_MUX_SEL 0
echo $(($(gpio_get FM_BOARD_BMC_REV_ID2)<<2 |
        $(gpio_get FM_BOARD_BMC_REV_ID1)<<1 |
        $(gpio_get FM_BOARD_BMC_REV_ID0))) > /tmp/mb_rev

echo $(($(gpio_get FM_BOARD_BMC_SKU_ID5)<<5 |
        $(gpio_get FM_BOARD_BMC_SKU_ID4)<<4 |
        $(gpio_get FM_BOARD_BMC_SKU_ID3)<<3 |
        $(gpio_get FM_BOARD_BMC_SKU_ID2)<<2 |
        $(gpio_get FM_BOARD_BMC_SKU_ID1)<<1 |
        $(gpio_get FM_BOARD_BMC_SKU_ID0))) > /tmp/mb_sku

gpio_set BOARD_ID_MUX_SEL 1

# FM_SOL_UART_CH_SEL
gpio_export FM_SOL_UART_CH_SEL GPION3

# IRQ_DIMM_SAVE_LVT3_N
gpio_export IRQ_DIMM_SAVE_LVT3_N GPION4

# FM_REMOTE_DEBUG_EN_DET
gpio_export FM_REMOTE_DEBUG_EN_DET GPION5

# Set debounce timer #1 value to 0x12E1FC ~= 100ms
$DEVMEM 0x1e780050 32 0x12E1FC

# Select debounce timer #1 for GPION6
devmem_clear_bit 0x1e780100 14
devmem_set_bit 0x1e780104 14

# FM_POST_CARD_PRES_BMC_N
gpio_export FM_POST_CARD_PRES_BMC_N GPION6

# FM_PWRBRK_N
gpio_export FM_PWRBRK_N GPION7

# RST_TCA9545_DDR_MUX_N
gpio_export RST_TCA9545_DDR_MUX_N GPIOQ6

# FM_BMC_CPU_PWR_DEBUG_N
gpio_export FM_BMC_CPU_PWR_DEBUG_N GPIOQ7

# DBP_PRESENT_N
gpio_export DBP_PRESENT_N GPIOR1

# SMB_BMC_MM5_ALT_N
gpio_export SMB_BMC_MM5_ALT_N GPIOR2

# SMB_BMC_MM5_RST_N
gpio_export SMB_BMC_MM5_RST_N GPIOR3

# SMB_SLOT1_ALT_N
gpio_export SMB_SLOT1_ALT_N GPIOR4

# SMB_SLOT2_ALT_N
gpio_export SMB_SLOT2_ALT_N GPIOR5

# FM_BIOS_SPI_BMC_CTRL
gpio_export FM_BIOS_SPI_BMC_CTRL GPIOR6

# FM_SYS_THROTTLE_LVC3
gpio_export FM_SYS_THROTTLE_LVC3 GPIOR7

# FM_BMC_REMOTE_DEBUG_EN
gpio_export FM_BMC_REMOTE_DEBUG_EN GPIOS0

# DBP_SYSPWROK
gpio_export DBP_SYSPWROK GPIOS1

# RST_RSMRST_BMC_N
gpio_export RST_RSMRST_BMC_N GPIOS2

# IRQ_SML0_ALERT_MUX_N
gpio_export IRQ_SML0_ALERT_MUX_N GPIOS3

# FP_LOCATE_LED, ID LED
gpio_export FP_LOCATE_LED GPIOS4
gpio_set FP_LOCATE_LED 0

# FM_MRC_DEBUG_EN
gpio_export FM_MRC_DEBUG_EN GPIOS5

# FM_2S_FAN_IO_EXP_RST
gpio_export FM_2S_FAN_IO_EXP_RST GPIOS6

# RST_SMB_OCP_MUX_N
gpio_export RST_SMB_OCP_MUX_N GPIOS7

# FM_FORCE_BMC_UPDATE_N
gpio_export FM_FORCE_BMC_UPDATE_N GPIOT0

# FP_LOCATE_LED_ACT
gpio_export FP_LOCATE_LED_ACT GPIOT5

# FM_CPU0_MEMHOT_OUT_N
gpio_export FM_CPU0_MEMHOT_OUT_N GPIOU5

# FM_SLPS3_N
gpio_export FM_SLPS3_N GPIOY0

# FM_SLPS4_N
gpio_export FM_SLPS4_N GPIOY1

# PWRGD_SYS_PWROK, power GOOD from CPLD
gpio_export PWRGD_SYS_PWROK GPIOY2

# FM_BMC_ONCTL_N
gpio_export FM_BMC_ONCTL_N GPIOY3

# FM_CPU_CATERR_LVT3_N
gpio_export FM_CPU_CATERR_LVT3_N GPIOZ0

# PWRGD_CPU0_LVC3
gpio_export PWRGD_CPU0_LVC3 GPIOZ1

# FM_CPU_MSMI_LVT3_N
gpio_export FM_CPU_MSMI_LVT3_N GPIOZ2

# FM_BMC_PCH_SCI_LPC_N
gpio_export FM_BMC_PCH_SCI_LPC_N GPIOZ3

# UV_HIGH_SET
gpio_export UV_HIGH_SET GPIOZ4

# FM_OC_DETECT_EN_N
gpio_export FM_OC_DETECT_EN_N GPIOZ5

# FM_SML1_PMBUS_ALERT_EN_N
gpio_export FM_SML1_PMBUS_ALERT_EN_N GPIOZ6

# FM_BMC_LED_CATERR_N
gpio_export FM_BMC_LED_CATERR_N GPIOZ7
gpio_set FM_BMC_LED_CATERR_N 1

# P3V_BAT_SCALED_EN
gpio_export P3V_BAT_SCALED_EN GPIOAA0
gpio_set P3V_BAT_SCALED_EN 0

# IRQ_SML1_PMBUS_BMC_ALERT_N
gpio_export IRQ_SML1_PMBUS_BMC_ALERT_N GPIOAA1

# FM_PVCCIN_CPU0_PWR_IN_ALERT_N
gpio_export FM_PVCCIN_CPU0_PWR_IN_ALERT_N GPIOAA2

# FM_PVCCIN_CPU1_PWR_IN_ALERT_N
gpio_export FM_PVCCIN_CPU1_PWR_IN_ALERT_N GPIOAA3

# JTAG_DBP_CPU_PREQ_N
gpio_export JTAG_DBP_CPU_PREQ_N GPIOAA4

# FM_JTAG_TCK_MUX_SEL
gpio_export FM_JTAG_TCK_MUX_SEL GPIOAA5

# IRQ_SMI_ACTIVE_BMC_N
gpio_export IRQ_SMI_ACTIVE_BMC_N GPIOAA6

# FM_BIOS_POST_CMPLT_BMC_N
gpio_export FM_BIOS_POST_CMPLT_BMC_N GPIOAA7

# FM_BMC_BMCINIT
gpio_export FM_BMC_BMCINIT GPIOAB0

# PECI_MUX_SELECT
gpio_export PECI_MUX_SELECT GPIOAB1
gpio_set PECI_MUX_SELECT 1

# PWRGD_BMC_PS_PWROK
gpio_export PWRGD_BMC_PS_PWROK GPIOAB3

# I/O Expander TCA9539 0xEC
gpio_export_ioexp 4-0076 FM_SLOT1_PRSNT_N 0
gpio_export_ioexp 4-0076 FM_SLOT2_PRSNT_N 1
gpio_export_ioexp 4-0076 BMC_PWM_EN 7
gpio_set BMC_PWM_EN 1

# I/O Expander TCA9539 0xEE
gpio_export_ioexp 4-0077 IRQ_PVCCIN_CPU0_VRHOT_LVC3_N 0
gpio_export_ioexp 4-0077 IRQ_PVCCIN_CPU1_VRHOT_LVC3_N 1
gpio_export_ioexp 4-0077 IRQ_PVDDQ_ABC_CPU0_VRHOT_LVC3_N 2
gpio_export_ioexp 4-0077 IRQ_PVDDQ_DEF_CPU0_VRHOT_LVC3_N 3
gpio_export_ioexp 4-0077 IRQ_PVDDQ_ABC_CPU1_VRHOT_LVC3_N 4
gpio_export_ioexp 4-0077 IRQ_PVDDQ_DEF_CPU1_VRHOT_LVC3_N 5
gpio_export_ioexp 4-0077 FM_CPU0_SKTOCC_LVT3_PLD_N 6
gpio_export_ioexp 4-0077 FM_CPU1_SKTOCC_LVT3_PLD_N 7
gpio_export_ioexp 4-0077 HP_LVC3_OCP_V3_2_PRSNT2_N 8
gpio_export_ioexp 4-0077 HP_LVC3_OCP_V3_1_PRSNT2_N 9

# I/O Expander PCA9552PW 0xC0
for i in {0..3};
do
  gpio_export_ioexp 8-0060 FM_FAN${i}_POWER_LED_N ${i}
  gpio_export_ioexp 8-0060 FM_FAN${i}_FAULT_LED_N $((i+4))
  gpio_export_ioexp 8-0060 FM_FAN${i}_BUF_PRESENT_R $((i+8))
  gpio_export_ioexp 8-0060 FM_FAN${i}_P12V_EN  $((i+12))
done

# I/O Expander PCA9552PW 0xC2
for i in {4..7};
do
  gpio_export_ioexp 8-0061 FM_FAN${i}_POWER_LED_N $((i-4))
  gpio_export_ioexp 8-0061 FM_FAN${i}_FAULT_LED_N $((i))
  gpio_export_ioexp 8-0061 FM_FAN${i}_BUF_PRESENT_R $((i+4))
  gpio_export_ioexp 8-0061 FM_FAN${i}_P12V_EN $((i+8))
done

# Disable JTAG engine
devmem 0x1e6e4008 32 0x0


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

rev=$(cat /tmp/mb_rev)
sku=$(cat /tmp/mb_sku)

#REV[1:0]: 01(EVT), 10(DVT)
#EVT SKU_ID[2:1] = 00(INFINEON), 01(T1)
#DVT SKU_ID[2:1] = 00(TI), 01(INFINEON), TODO: 10(3rd Source)
if [ $rev -eq 1 ]; then
  sku=$((sku & 0x4))
  if [ $sku -eq 4 ]; then
    i2c_device_add 1 0x60 tps53688
    i2c_device_add 1 0x62 tps53688
    i2c_device_add 1 0x66 tps53688
    i2c_device_add 1 0x68 tps53688
    i2c_device_add 1 0x6c tps53688
    i2c_device_add 1 0x70 tps53688
    i2c_device_add 1 0x72 tps53688
    i2c_device_add 1 0x76 tps53688
  else
    i2c_device_add 1 0x60 xdpe12284
    i2c_device_add 1 0x62 xdpe12284
    i2c_device_add 1 0x66 xdpe12284
    i2c_device_add 1 0x68 xdpe12284
    i2c_device_add 1 0x6c xdpe12284
    i2c_device_add 1 0x70 xdpe12284
    i2c_device_add 1 0x72 xdpe12284
    i2c_device_add 1 0x76 xdpe12284
  fi
elif [ $rev -eq 2 ]; then
  sku=$((sku & 0x2))
  if [ $sku -eq 0 ]; then
    i2c_device_add 1 0x60 tps53688
    i2c_device_add 1 0x62 tps53688
    i2c_device_add 1 0x66 tps53688
    i2c_device_add 1 0x68 tps53688
    i2c_device_add 1 0x6c tps53688
    i2c_device_add 1 0x70 tps53688
    i2c_device_add 1 0x72 tps53688
    i2c_device_add 1 0x76 tps53688
  else
    i2c_device_add 1 0x60 xdpe12284
    i2c_device_add 1 0x62 xdpe12284
    i2c_device_add 1 0x66 xdpe12284
    i2c_device_add 1 0x68 xdpe12284
    i2c_device_add 1 0x6c xdpe12284
    i2c_device_add 1 0x70 xdpe12284
    i2c_device_add 1 0x72 xdpe12284
    i2c_device_add 1 0x76 xdpe12284
  fi
fi

if [ "$(gpio_get HP_LVC3_OCP_V3_1_PRSNT2_N)" == "0" ]; then
  i2c_device_add 17 0x50 24c32
else
  echo "Unknown or no device on the NIC#0"
fi

if [ "$(gpio_get HP_LVC3_OCP_V3_2_PRSNT2_N)" == "0" ]; then
  i2c_device_add 18 0x52 24c32
  ifconfig eth1 up
else
  echo "Unknown or no device on the NIC#1"
fi

if [ "$(gpio_get FM_SLOT1_PRSNT_N)" == "0" ]; then
  i2c_device_add 2 0x50 24c32
else
  echo "Unknown or no device on the RISER_SLOT#1"
fi

if [ "$(gpio_get FM_SLOT2_PRSNT_N)" == "0" ]; then
  i2c_device_add 6 0x50 24c32
else
  echo "Unknown or no device on the RISER_SLOT#2"
fi


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

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

# CHKPT_COMPLETE
for i in {1..3}; do
  /usr/sbin/i2cset -y 4 0x58 0x0f 0x09 b > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    break
  fi
  sleep 0.5
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-por
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

store_clear_por


#!/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
MAX_RETRY_CNT=5

# Sync BMC's date with the server
sync_date()
{
  # if no ntp peer has been declared the system peer
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    retry=0
    while [ $retry -lt $MAX_RETRY_CNT ]
    do
      # Use standard IPMI command 'get-sel-time' to read RTC time
      output=$(/usr/local/bin/me-util 0x28 0x48)
      if [ ${#output} == 12 ]; then
        ts=$(date -s @$((16#$(echo "$output" | awk '{print $4$3$2$1}'))))
        test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop

        logger -s -p user.info -t sync_date "Syncing up BMC time with server..."
        logger -s -p user.info -t sync_date "Date: $ts"
        return
      else
        retry=$((retry+1))
        logger -s -p user.info -t sync_date "Get SEL time from ME failed, syncing up BMC time with server failed for $retry times."
        sleep 1
      fi
    done
  fi
}

sync_date


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

# Call "fw-util vr --version" once before sensor monitoring to store vr information
echo "Get VR FW version... "
/usr/bin/fw-util vr --version > /dev/null

echo -n "Setup sensor monitoring for FBSP... "
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
. /usr/local/fbpackages/utils/ast-functions

#Set PWR_AVR to 128 samples
i2cset -y -f 7 0x11 0xd4 0x3F1C w

#calibrtion to get HSC to trigger 123A based on EE team input
# 0xDC3 = 3523 (dec)
#(3523*10-20475)/ (800*0.15)= 122.958A
i2cset -y -f 7 0x11 0x4a 0x0DC3 w

#Enable IOUT_OC_WARN_EN1
i2cset -y -f 7 0x11 0xD5 0x0400 w


#!/bin/bash

PID=$$
# File format autodump<fru>.pid (See pal_is_crashdump_ongoing()
# function definition)
PID_FILE='/var/run/autodump1.pid'
DWR_FILE='/tmp/DWR'

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

# check if running auto dump
[ -r $PID_FILE ] && OLDPID=`cat $PID_FILE` || OLDPID=''

# Set current pid
echo $PID > $PID_FILE

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

# kill previous autodump if exist
if [ ! -z "$OLDPID" ] && (grep "autodump" /proc/$OLDPID/cmdline &> /dev/null) ; then

  LOG_FILE='/tmp/autodump.log'
  LOG_ARCHIVE='/mnt/data/autodump_uncompleted.tar.gz'
  echo -n "(uncompleted) Auto Dump End at " >> $LOG_FILE
  date >> $LOG_FILE

  tar zcf $LOG_ARCHIVE -C `dirname $LOG_FILE` `basename $LOG_FILE` && \
  rm -rf $LOG_FILE && \

  echo "kill pid $OLDPID..."
  kill -s 9 $OLDPID
  killall peci-util &> /dev/null
  killall me-util &> /dev/null
fi
unset OLDPID

DUMP_SCRIPT='/usr/local/bin/dump.sh'
LOG_FILE='/tmp/autodump.log'
LOG_ARCHIVE='/mnt/data/autodump.tar.gz'
LOG_MSG_PREFIX=""

CPU1_SKTOCC_N="FM_CPU1_SKTOCC_LVT3_N"
DWR=0
SECOND_DUMP=0
DELAY_SEC=30
SENSOR_HISTORY=180

while test $# -gt 0
do
  case "$1" in
  --dwr)
    DWR=1
    ;;
  --now)
    DELAY_SEC=0
    ;;
  --second)
    SECOND_DUMP=1
    ;;
  *)
    echo "unknown argument $1"
    ;;
  esac
  shift
done

if [ "$DWR" == "1" ] || [ "$SECOND_DUMP" == "1" ]; then
  echo "Auto Dump after System Reset or Demoted Warm Reset"
fi

if [ "$DELAY_SEC" != "0" ]; then
  echo "Auto Dump will start after ${DELAY_SEC}s..."

  sleep ${DELAY_SEC}
fi

echo "Auto Dump Started"

# Start to dump and clear LOG_FILE
echo -n "Auto Dump Start at " > $LOG_FILE
date >> $LOG_FILE

# CPU0
$DUMP_SCRIPT 48 coreid >> $LOG_FILE 2>&1
$DUMP_SCRIPT 48 msr >> $LOG_FILE 2>&1

# CPU1
if [ "$(gpio_get_value ${CPU1_SKTOCC_N})" -eq "0" ]; then
  $DUMP_SCRIPT 49 coreid >> $LOG_FILE 2>&1
  $DUMP_SCRIPT 49 msr >> $LOG_FILE 2>&1
else
  echo "<<---------- CPU1 is not present, skip it. ---------->>" >> $LOG_FILE
fi

# PCIe
$DUMP_SCRIPT pcie >> $LOG_FILE 2>&1

# Sensors
echo "Sensor history of last ${SENSOR_HISTORY}s at dump:" >> $LOG_FILE 2>&1
/usr/local/bin/sensor-util all --history $SENSOR_HISTORY >> $LOG_FILE 2>&1

echo "Sensor threshold at dump: " >> $LOG_FILE 2>&1
/usr/local/bin/sensor-util all --threshold >> $LOG_FILE 2>&1

# only second/dwr autodump need to rename accordingly
if [ "$DWR" == "1" ] || [ "$SECOND_DUMP" == "1" ]; then
# dwr
$DUMP_SCRIPT dwr >> $LOG_FILE 2>&1

# rename the archieve file based on whether dump in DWR mode or not
if [ "$?" == "2" ]; then
  LOG_ARCHIVE='/mnt/data/autodump_dwr.tar.gz'
  LOG_MSG_PREFIX="DWR "
else
  LOG_ARCHIVE='/mnt/data/autodump_second.tar.gz'
  LOG_MSG_PREFIX="SECOND_DUMP "
fi
fi

echo -n "Auto Dump End at " >> $LOG_FILE
date >> $LOG_FILE

tar zcf $LOG_ARCHIVE -C `dirname $LOG_FILE` `basename $LOG_FILE` && \
rm -rf $LOG_FILE && \
logger -t "ipmid" -p daemon.crit "${LOG_MSG_PREFIX}Crashdump for FRU: 1 is generated at $LOG_ARCHIVE"

# Remove current pid file
rm $PID_FILE
rm -r $DWR_FILE >/dev/null 2>&1

echo "${LOG_MSG_PREFIX}Auto Dump Stored in $LOG_ARCHIVE"
exit 0


#!/bin/bash

PROGRAM=$0
ME_UTIL="/usr/local/bin/me-util"
PECI_UTIL="/usr/local/bin/peci-util"
CMD_DIR="/etc/peci"
INTERFACE="ME_INTERFACE"

function print_help_msg {
  echo "$PROGRAM 48 coreid  ==> for CPU 1 CoreID"
  echo "$PROGRAM 49 coreid  ==> for CPU 2 CoreID"
  echo "$PROGRAM 48 msr     ==> for CPU 1 MSR"
  echo "$PROGRAM 49 msr     ==> for CPU 2 MSR"
  echo "$PROGRAM pcie       ==> for PCIe"
  echo "$PROGRAM dwr        ==> for DWR check"
}

# read command from stdin
function execute_via_me {
  # if ME_UTIL cannot be executed, return
  [ -x $ME_UTIL ] || \
    return

  # Adapt command to ME-UTIL format
  cat | \
    sed 's/^0x/0xb8 0x40 0x57 0x01 0x00 0x/g' $1 | \
      $ME_UTIL --file /dev/stdin | \
        sed 's/^57 01 00 //g'
}

# read command from stdin
function execute_via_peci {
  # if PECI_UTIL cannot be executed, return
  [ -x $PECI_UTIL ] || \
    return

  cat | \
    $PECI_UTIL --file /dev/stdin
}

# read command from stdin
function execute_cmd {
  if [ "$INTERFACE" == "PECI_INTERFACE" ]; then
    cat | execute_via_peci
    return
  fi

  RES=$($ME_UTIL 0x18 0x01)
  RET=$?
  # if ME has response and in operational mode, PECI through ME
  if [ "$RET" -eq "0" ] && [ "${RES:6:1}" == "0" ]; then
    cat | execute_via_me
  # else use wired PECI directly
  else
  #  echo "Use BMC wired PECI interface due to ME abnormal"
    INTERFACE="PECI_INTERFACE"
    cat | execute_via_peci
  fi
}

# ARG: Bus[8], Device[5], Function[3], Register[12]
function PCI_Config_Addr {
  RESULT=0
  # BUS
  RESULT=$((RESULT | (($1 & 0xFF) << 20) ))
  # DEV
  RESULT=$((RESULT | (($2 & 0x1F) << 15) ))
  # FUN
  RESULT=$((RESULT | (($3 & 0x7) << 12) ))
  # REG
  RESULT=$((RESULT | (($4 & 0xFFF) << 0) ))
  printf "0x%x 0x%x 0x%x 0x%x"\
    $(( RESULT & 0xFF )) \
    $(( (RESULT >> 8) & 0xFF )) \
    $(( (RESULT >> 16) & 0xFF )) \
    $(( (RESULT >> 24) & 0xFF ))
  return 0
}

function pcie_dump {
  # CPU and PCH
  [ -r $CMD_DIR/crashdump_pcie ] && \
    cat $CMD_DIR/crashdump_pcie | execute_cmd

  # Buses
  RES=$(echo "0x30 0x06 0x05 0x61 0x00 0xcc 0x20 0x04 0x00" | execute_cmd)
  echo "Get CPUBUSNO: $RES"
  # Completion Code
  CC=$(echo $RES| awk '{print $1;}')
  # Root port buses
  ROOT_BUSES=$(echo $RES | awk '{printf "0x%s 0x%s 0x%s", $3, $4, $5;}')

  # Success
  if [ "$CC" == "40" ]; then
    for ROOT in $ROOT_BUSES; do
      for DEV in {0..3}; do
        echo "Find Root Port Bus $ROOT Dev $DEV..."
        RES=$(echo "0x30 0x06 0x05 0x61 0x00 $(PCI_Config_Addr $ROOT $DEV 0x00 0x18)" | execute_cmd)
        echo "$RES"
        CC=$(echo $RES| awk '{print $1;}')
        SECONDARY_BUS=0x$(echo $RES| awk '{print $3;}')
        SUBORDINATE_BUS=0x$(echo $RES| awk '{print $4;}')
        if [ "$CC" == "40" ] && [ "$SECONDARY_BUS" != "0x00" ] && [ "$SUBORDINATE_BUS" != "0x00" ]; then
          for (( BUS=$SECONDARY_BUS; BUS<=$SUBORDINATE_BUS; BUS++ )); do
            BUS_MS_NIBBLE=$(printf "%x" $((BUS >> 4)) )
            BUS_LS_NIBBLE=$(printf "%x" $((BUS & 0xF)) )
            [ -r $CMD_DIR/crashdump_pcie_bus ] && \
              sed "s/<BUSM>/$BUS_MS_NIBBLE/g; s/<BUSL>/$BUS_LS_NIBBLE/g" $CMD_DIR/crashdump_pcie_bus | execute_cmd
          done
        fi
      done
    done
  fi
}

function dwr_dump {

  echo
  echo DWR assert check:
  echo =================
  echo

  # Get biosscratchpad7[26]: DWR assert check
  RES=$(echo "0x30 0x06 0x05 0x61 0x00 0xbc 0x20 0x04 0x00" | execute_cmd)

  echo "< Get biosscratchpad7[26]: DWR assert check >"
  echo "$RES"
  echo

  # Completion Code
  CC=$(echo $RES| awk '{print $1;}')
  DWR=0x$(echo $RES| awk '{print $5;}')

  # Success
  if [ "$CC" == "40" ]; then
    if [ $((DWR & 0x04)) == 4 ]; then
      echo "Auto Dump in DWR mode"
      return 2
    else
      echo "Auto Dump in non-DWR mode"
      return 1
    fi
  else
    echo "get DWR mode fail"
    return 0
  fi
}

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

echo ""

if [ "$#" -eq 1 ] && [ $1 = "pcie" ]; then

  pcie_dump

  exit 0
fi

if [ "$#" -eq 1 ] && [ $1 = "dwr" ]; then

  dwr_dump

  exit $?
fi

if [ "$#" -ne 2 ]; then

  print_help_msg

  exit 1

elif [ "$1" -ne 48 ] && [ "$1" -ne 49 ]; then

  print_help_msg

  exit 1

elif [ "$2" = "coreid" ]; then

  if [ "$1" -eq 48 ]; then
    [ -r $CMD_DIR/crashdump_p0_coreid ] && \
      cat $CMD_DIR/crashdump_p0_coreid | execute_cmd
  elif [ "$1" -eq 49 ]; then
    [ -r $CMD_DIR/crashdump_p1_coreid ] && \
      cat $CMD_DIR/crashdump_p1_coreid | execute_cmd
  fi

  exit 0

elif [ "$2" = "msr" ]; then

  if [ "$1" -eq 48 ]; then
    [ -r $CMD_DIR/crashdump_p0_msr ] && \
      cat $CMD_DIR/crashdump_p0_msr | execute_cmd
  elif [ "$1" -eq 49 ]; then
    [ -r $CMD_DIR/crashdump_p1_msr ] && \
      cat $CMD_DIR/crashdump_p1_msr | execute_cmd
  fi

  exit 0

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.."
  /usr/local/bin/front-paneld > /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
#
PWM_DIR=/sys/devices/platform/ast_pwm_tacho.0

set -e

# 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 FBTP, there are 2 fans connected.
# Here is the mapping between the fan and PWN/Tacho,
# Fan 0: PWM 0, Tacho0
# Fan 1: PWM 1, Tacho2

# For each fan, setting the type, and 100% initially
for pwm in 0 1; 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, 2
echo 0 > $PWM_DIR/tacho0_source
echo 1 > $PWM_DIR/tacho0_en

echo 1 > $PWM_DIR/tacho2_source
echo 1 > $PWM_DIR/tacho2_en


#!/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-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/init_pwm.sh
/usr/local/bin/fan-util --set 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
#

### BEGIN INIT INFO
# Provides:          setup-gpiod
# 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 gpio monitoring for FBTP... "

/usr/local/bin/gpiod

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
#

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

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

. /usr/local/bin/openbmc-utils.sh
KERNEL_VERSION=$(uname -r)
if [[ "${KERNEL_VERSION}" != 4.1.* ]]; then
    i2c_mslave_add 1 0x10
    i2c_mslave_add 4 0x10
    i2c_mslave_add 9 0x10
fi

echo -n "Starting IPMB Rx/Tx Daemon.."

runsv /etc/sv/ipmbd_1 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_4 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_9 > /dev/null 2>&1 &

echo "done."


#!/bin/sh

# PLATFORM MAY CHOOSE TO OVERRIDE THIS FILE
echo -n "Starting mTerm console server..."
runsv /etc/sv/mTerm > /dev/null 2>&1 &
runsv /etc/sv/mTermBMC > /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:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions
KV_STORE="/mnt/data/kv_store"

pwr_on_host() {
  pwr_sts=$(gpio_get PWRGD_SYS_PWROK)
  if [ "$pwr_sts" != "1" ]; then
    gpio_set FM_BMC_PWRBTN_OUT_N 0
    sleep 1
    gpio_set FM_BMC_PWRBTN_OUT_N 1
    sleep 1
    pwr_sts=$(gpio_get PWRGD_SYS_PWROK)
    if [ "$pwr_sts" == "1" ]; then
      logger "Successfully power on the server"
    fi
  else
    logger "The server was powered on"
  fi
}

check_por_config() {
  #0:Power on reset 1:first power on
  echo "$(cat /tmp/ast_por)"
}

echo -n "Power on the server..."

#get por cfg
por_conf="$KV_STORE/server_por_cfg"
lps_conf="$KV_STORE/pwr_server_last_state"
if ! [ -f $por_conf ]; then
    # power on the server directly
    # healthd is S91 and power-on.sh is S66 in rc5.d
    # when the system is first powered on, BMC would power on the server directly
    logger -s -p user.info -t power-on "Power Policy can't be found, power on the server directly"
    pwr_on_host
else
    # control the system power based on $por_conf
    por_val=$(cat $por_conf)
    lps_val=$(cat $lps_conf)
    logger -s -p user.info -t power-on "Power Policy: $por_val, Last Power State: $lps_val"
    if [ "$por_val" == "on" ]; then
      pwr_on_host
    elif [ "$por_val" == "lps" ] && [ "$lps_val" == "on" ]; then
      pwr_on_host
    fi
fi

echo "Done"

echo "Check if P12V_STBY was turned off before..."

check_log=$($LOGUTIL_CMD all --print | grep -v grep | grep "Turn off P12V_STBY" | tail -n 1)

if [ -n "$check_log" ]; then
   logger -s -p user.crit -t power-on "Turn off P12V_STBY because it was turned off before"
   turn_off_p12v_stby
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
#

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

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

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

set -e

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

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

gpio_set SERVER_POWER_LED $val


#!/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-consoled
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup console history buffering
### END INIT INFO

# Enable AST SNOOP DMA with default port 80
echo 1 > /sys/devices/platform/ast-snoop-dma.0/enable


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

# Sync BMC's date with the server
sync_date()
{
  # if no ntp peer has been declared the system peer
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    echo Syncing up BMC time with server...
    # Use standard IPMI command 'get-sel-time' to read RTC time
    date -s @$((16#$(/usr/local/bin/me-util 0x28 0x48 | awk '{print $4$3$2$1}')))
  fi
}

sync_date


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

ARGS="--json /etc/platform-svcd-config.json"
exec /usr/local/bin/platform-svcd $ARGS


#!/bin/sh
#
# Copyright 2017-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:          platform-svcd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: dbus based platform service
### END INIT INFO

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

echo -n "Setup platform svcd for FBTP... "

runsv /etc/sv/platform-svcd > /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
#

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

# Call "fw-util mb --version" once before sensor monitoring to store vr information
echo "Get MB FW version... "
/usr/bin/fw-util mb --version > /dev/null

if [ "$(gpio_get FM_BOARD_SKU_ID0)" = "0" ]; then
  ln -s /etc/sensor-correction-sku0-conf.json /etc/sensor-correction-conf.json
else
  new_source=$(/usr/local/bin/fruid-util mb | grep "KSP2907ATF")
  if [ -z "$new_source" ];
  then
    ln -s /etc/sensor-correction-sku1-conf.json /etc/sensor-correction-conf.json
  else
    ln -s /etc/sensor-correction-sku1-diode-conf.json /etc/sensor-correction-conf.json
  fi
fi

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

# Vout is not enabled, set HSC_0xD4 register to 3F1Eh to enable Vout
/usr/local/bin/me-util mb 0xb8 0xd9 0x57 0x01 0x0 0x88 0x8a 0x0 0x0 0x3 0x0 0xd4 0x1e 0x3f

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

echo "done."