#!/bin/bash
#
# fpga_platform_data_config - Configure ncs5700 platform data that are
#                             required for functions implemented in the
#                             fpga-functions script.
#
# September 2018, Chi-Heng Lu
#
# Copyright (c) 2018-2022 by cisco Systems, Inc.
# All rights reserved
#


if [ -f /usr/local/etc/functions.platform ]; then
    source /usr/local/etc/functions.platform
else 
    source /usr/local/etc/ncs5700/functions.platform
fi

function pd_config_get_aikido_fpga_region()
{
    # If this function is defined,
    # fpga-functions script will search for regions to get aikido base 
    if [ $AIKIDO_PCI_ID -eq 0281 ]; then
        # ShadowTower boards
        echo "0"
    else
        # Twins boards
        echo "2" 
    fi
}

PLATFORM_ID_FIXED=1

AIKIDO_PCI_ID=$(get_stdby_fpga_device_id)
AIKIDO_PCI_FPGA_REGION=$(pd_config_get_aikido_fpga_region)

# IOFPGA PCI definitions
IOFPGA_PCI_ID=0282
IOFPGA_PCI_BAR=0

# MB IOFPGA PCI definitions
MB_IOFPGA_PCI_ID=0283
MB_IOFPGA_PCI_BAR=0

# Supported FPGA IDs
FPGA_ID_KALARI=2
NCS5700_F_FPGA_ID_EXCELSIOR_X86_CPU_FPGA=24


function cfg_st_f_data_based_on_fpga_id ()
{
     case ${FPGA_ID} in
         ${NCS5700_F_FPGA_ID_EXCELSIOR_X86_CPU_FPGA})
             FPGA_NAME="Elmdon X86 Aikido FPGA"
             AIKIDO_IP_BLK_OFFSET=0x17000
             MSXIL_OFFSET=1000
             AIKIDO_PCI_BAR=2
             WATCHDOG_IP_BLK_OFFSET=0x5000
             SMBMUX_IP_BLK_OFFSET=0x7000
             LED_IP_BLK_OFFSET=0x3b000
             FPGA_INST_FOR_LED_CTRL=${FPGA_INST_AIKIDO}
             FPGA_SHUTDOWN_KLM_OP_MODE=0
             CARD_PID="Twins"
             ;;
         *)
             FPGA_NAME="ShadowTower FPGA for FPGA ID ${FPGA_ID} on Platform \
 ID ${PLATFORM_ID}"
             AIKIDO_PCI_BAR=0
             FPGA_SHUTDOWN_KLM_OP_MODE=0
             # Set magic cookie offset
             MAGIC_COOKIE_OFFSET=11000
             MAGIC_COOKIE_BASE_VALUE=94CB2F00
             #Platform Auto reset disabled(bit 6)
             FPGA_SHUTDOWN_KLM_OP_MODE=0x40
             ;;
     esac
}

#
# Main routine called by fpga-functions script to configure ncs5700 platform
# and FPGA specific values
# The following variables are set by the fpga-functions script before calling
# this routine:
# - PLATFORM_ID
# - FPGA_ID
# - FPGA_INST
#
# Please refers to the fpga-functions.PD_API_README file for detail
# description of all the variables that we have to set with this function.
function pd_config_data_based_on_platform_and_fpga_id ()
{
    # Set the platform common variables
    MY_CARD_TYPE="RP"
    FPGA_INST_FOR_SENDING_DOORBELL=${FPGA_INST_AIKIDO}
    FPGA_INST_FOR_RESET_CTRL=${FPGA_INST_AIKIDO}
    
    # Set the platform and FPGA specific variables
    case ${PLATFORM_ID} in
    ${PLATFORM_ID_FIXED})
            PLATFORM_NAME="NCS5700 Fixed"
            MY_CARD_TYPE="RP"
            FPGA_INST_FOR_RESET_CTRL=${FPGA_INST_AIKIDO}
            FPGA_INST_FOR_SMBMUX_CTRL=${FPGA_INST_AIKIDO}
            #MSDEV_IP_BLK_OFFSET=0x1000
            # Set operation mode for shutdown kernel module to 3 because card
            # level power-off is not supported (bit 0) and
            # IOFPGA has to be handled (bit 1)
            #FPGA_SHUTDOWN_KLM_OP_MODE=3
            #EXTRA_FPGA_SHUTDOWN_KLM_PARAMS="secondary_devid=0x${IOFPGA_PCI_ID} secondary_bar=${IOFPGA_PCI_BAR}"
            
            #FPGA_DBE_IP_BLK_OFFSET=${MSDEV_IP_BLK_OFFSET}
            #FPGA_SEND_X86_DOORBELL_REG_OFFSET=0x34
            #FPGA_SEND_BMC_DOORBELL_REG_OFFSET=0x38
            #FPGA_INST_IP_BLK_P2PM=${FPGA_INST_AIKIDO}
            # Determine other value based on FPGA ID value
            cfg_st_f_data_based_on_fpga_id
            ;;
        *)
            PLATFORM_NAME="Unknown platform ID : ${PLATFORM_ID}"
            ;;
    esac
    
    # Determine other value based on FPGA ID value
    case ${FPGA_ID} in
        ${FPGA_ID_KALARI})
            FPGA_NAME="Kalari Aikido FPGA"
            WATCHDOG_IP_BLK_OFFSET=0x3000
            ;;
    esac

    if [ -z "${CARD_PID}" ]; then
        CARD_PID=$(get_product_id)
    fi
}

