#!/bin/sh

# Copyright 2016,2019 Cumulus Networks, Inc.
# All rights reserved

# amd64 architecture initramfs-tools script.
#
# This script is run *before* the common "cumulus-top" script to
# arrange things specific for this CPU architecture.

PREREQ=""

prereqs()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

# Parse kernel command line options for cl_platform
for x in $(cat /proc/cmdline); do
    parm=${x%%=*}
    val=${x#*=}
    case $parm in
	cl_platform)
            cl_platform="$val"
            break
            ;;
    esac
done

if [ $cl_platform = 'quanta_ly8_rangeley' ] || [ $cl_platform = 'quanta_ly6_rangeley' ] || \
    [ $cl_platform = 'quanta_ly9_rangeley' ]; then
    # Quanta LY8/LY6/LY9 use an external RTC present on the I2C bus.
    # Start using this instead of built-in SOC RTC as soon as you can.
    echo ds1339 0x68 > /sys/bus/i2c/devices/i2c-0/new_device
    # instantiating the device and /dev/rtc1 takes time.
    sleep 1
    # Set system time from RTC1
    hwclock -f /dev/rtc1 -s --utc
fi

accton_td3_clk_setting()
{
    modprobe i2c_dev
    i2cset -f -y 0 0x77 0x1
    i2cset -f -y 0 $1 0x1

    i2cget -f -y 0 0x54 0 b > /dev/null
    if [ $? -ne 0 ];then
	echo "Device 8v89307(0x54) not found\n"
	exit 1
    fi
    # Title = --- IDT 82V89307 Registers ---
    # Page0_Parameters
    i2cset -f -y 0 0x54 0x2D 0x00 # Select to Page 0
    i2cset -f -y 0 0x54 0x7F 0x05
    i2cset -f -y 0 0x54 0x7E 0x85
    i2cset -f -y 0 0x54 0x7B 0x00
    i2cset -f -y 0 0x54 0x7A 0x00
    i2cset -f -y 0 0x54 0x79 0x40
    i2cset -f -y 0 0x54 0x78 0x06
    i2cset -f -y 0 0x54 0x73 0x40
    i2cset -f -y 0 0x54 0x72 0x40
    i2cset -f -y 0 0x54 0x71 0x0A # OUT3:25MHz
    i2cset -f -y 0 0x54 0x70 0x00
    i2cset -f -y 0 0x54 0x6B 0x4E # OUT1:1pps
    i2cset -f -y 0 0x54 0x69 0x00
    i2cset -f -y 0 0x54 0x68 0x00
    i2cset -f -y 0 0x54 0x67 0x19
    i2cset -f -y 0 0x54 0x66 0xAB
    i2cset -f -y 0 0x54 0x65 0x8C
    i2cset -f -y 0 0x54 0x64 0x00
    i2cset -f -y 0 0x54 0x63 0x00
    i2cset -f -y 0 0x54 0x62 0x00
    i2cset -f -y 0 0x54 0x5F 0x00
    i2cset -f -y 0 0x54 0x5E 0x00
    i2cset -f -y 0 0x54 0x5D 0x00
    i2cset -f -y 0 0x54 0x5C 0x78
    i2cset -f -y 0 0x54 0x5B 0x02
    i2cset -f -y 0 0x54 0x5A 0xE5
    i2cset -f -y 0 0x54 0x59 0x88
    i2cset -f -y 0 0x54 0x58 0x4B
    i2cset -f -y 0 0x54 0x57 0x6C
    i2cset -f -y 0 0x54 0x56 0x6C
    i2cset -f -y 0 0x54 0x55 0x80 # Lock to DPLL, output 625MHz
    i2cset -f -y 0 0x54 0x53 0x00
    i2cset -f -y 0 0x54 0x52 0x81
    i2cset -f -y 0 0x54 0x50 0x00
    i2cset -f -y 0 0x54 0x4F 0x00
    i2cset -f -y 0 0x54 0x4E 0x00
    i2cset -f -y 0 0x54 0x4C 0xCB
    i2cset -f -y 0 0x54 0x4A 0x00
    i2cset -f -y 0 0x54 0x45 0x66
    i2cset -f -y 0 0x54 0x44 0x66
    i2cset -f -y 0 0x54 0x42 0x80
    i2cset -f -y 0 0x54 0x41 0x03
    i2cset -f -y 0 0x54 0x40 0x01
    i2cset -f -y 0 0x54 0x3F 0x08
    i2cset -f -y 0 0x54 0x3E 0x04
    i2cset -f -y 0 0x54 0x3D 0x20
    i2cset -f -y 0 0x54 0x3C 0x13
    i2cset -f -y 0 0x54 0x3B 0x00
    i2cset -f -y 0 0x54 0x3A 0x98
    i2cset -f -y 0 0x54 0x39 0x01
    i2cset -f -y 0 0x54 0x38 0xE6
    i2cset -f -y 0 0x54 0x37 0x04
    i2cset -f -y 0 0x54 0x36 0xCE
    i2cset -f -y 0 0x54 0x35 0x7C
    i2cset -f -y 0 0x54 0x34 0x01
    i2cset -f -y 0 0x54 0x33 0x08
    i2cset -f -y 0 0x54 0x32 0x08
    i2cset -f -y 0 0x54 0x31 0x08
    i2cset -f -y 0 0x54 0x30 0x03
    i2cset -f -y 0 0x54 0x2F 0x23
    i2cset -f -y 0 0x54 0x2E 0x0B
    i2cset -f -y 0 0x54 0x2D 0x00
    i2cset -f -y 0 0x54 0x28 0x76
    i2cset -f -y 0 0x54 0x27 0x54
    i2cset -f -y 0 0x54 0x25 0x00
    i2cset -f -y 0 0x54 0x24 0x03
    i2cset -f -y 0 0x54 0x23 0x06
    i2cset -f -y 0 0x54 0x1A 0x8C
    i2cset -f -y 0 0x54 0x19 0x8C
    i2cset -f -y 0 0x54 0x18 0x00
    i2cset -f -y 0 0x54 0x16 0x0D
    i2cset -f -y 0 0x54 0x11 0x00
    i2cset -f -y 0 0x54 0x10 0x00
    i2cset -f -y 0 0x54 0x0E 0x3F
    i2cset -f -y 0 0x54 0x0D 0xFF
    i2cset -f -y 0 0x54 0x0C 0x02
    i2cset -f -y 0 0x54 0x0B 0xA1
    i2cset -f -y 0 0x54 0x0A 0x89
    i2cset -f -y 0 0x54 0x09 0xA2
    i2cset -f -y 0 0x54 0x08 0x32
    i2cset -f -y 0 0x54 0x06 0x00
    i2cset -f -y 0 0x54 0x05 0x00
    i2cset -f -y 0 0x54 0x04 0x00
    i2cset -f -y 0 0x54 0x03 0x00
    i2cset -f -y 0 0x54 0x02 0x05
    i2cset -f -y 0 0x54 0x01 0x33
    i2cset -f -y 0 0x54 0x00 0x91

    # PreDivider_Parameters
    i2cset -f -y 0 0x54 0x23 0x05 #IN1
    i2cset -f -y 0 0x54 0x24 0x03
    i2cset -f -y 0 0x54 0x25 0x00
    i2cset -f -y 0 0x54 0x23 0x06 #IN2
    i2cset -f -y 0 0x54 0x24 0x03
    i2cset -f -y 0 0x54 0x25 0x00
    i2cset -f -y 0 0x54 0x23 0x03 #IN3
    i2cset -f -y 0 0x54 0x24 0x00
    i2cset -f -y 0 0x54 0x25 0x00

    # Page1_Parameters
    i2cset -f -y 0 0x54 0x2D 0x01 # Select to Page 1
    i2cset -f -y 0 0x54 0x30 0x03
    i2cset -f -y 0 0x54 0x31 0x08
    i2cset -f -y 0 0x54 0x32 0x08
    i2cset -f -y 0 0x54 0x33 0x08
    i2cset -f -y 0 0x54 0x35 0x7C
    i2cset -f -y 0 0x54 0x36 0xCE
    i2cset -f -y 0 0x54 0x37 0x04
    i2cset -f -y 0 0x54 0x38 0xE6
    i2cset -f -y 0 0x54 0x39 0x01
    i2cset -f -y 0 0x54 0x3A 0x98
    i2cset -f -y 0 0x54 0x3B 0x00
    i2cset -f -y 0 0x54 0x3C 0x13
    i2cset -f -y 0 0x54 0x3D 0x20
    i2cset -f -y 0 0x54 0x2D 0x00 # Return to Page 0

    i2cset -f -y 0 $1 0x0
    i2cset -f -y 0 0x77 0x0
}

# Disable i2c channels of ir3570
# chip on i2c-0.
# The chip has an issue where a
# block read of any size causes
# i2c bus to hang if the block
# has 0x8 or 0x9 as the last
# digit.
accton_disable_ir3570()
{
    i2cset -y 0 0x4 0xE5 0x01 2>/dev/null
    i2cset -y 0 0x4 0x12 0x02 2>/dev/null
}

if [ $cl_platform = 'accton_as7726_32x' ]; then
    mux2=0x76
    accton_td3_clk_setting $mux2
    accton_disable_ir3570
fi

if [ $cl_platform = 'accton_as7326_56x' ]; then
    mux2=0x70
    accton_td3_clk_setting $mux2
    accton_disable_ir3570
fi

if [ $cl_platform = 'accton_as7816_64x' ]; then
    modprobe i2c_dev
    accton_disable_ir3570
fi
