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

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

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

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

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

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

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

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