/*-------------------------------------------------------------------------------------------------
--
-- File Name    : coaxpressXcvrSpeed.c



-- Description  : This module replicates the CoaXPress Device Link speed settings.




-- Targeted device : Microchip FPGAs
-- Author          : India Solutions Team




--
-- COPYRIGHT 2023 BY MICROCHIP
-- THE INFORMATION CONTAINED IN THIS DOCUMENT IS SUBJECT TO LICENSING RESTRICTIONS
-- FROM MICROCHIP CORP.  IF YOU ARE NOT IN POSSESSION OF WRITTEN AUTHORIZATION FROM
-- MICROCHIP FOR USE OF THIS FILE, THEN THE FILE SHOULD BE IMMEDIATELY DESTROYED AND
-- NO BACK-UP OF THE FILE SHOULD BE MADE.
--



-------------------------------------------------------------------------------------------------*/

#include "hal/hal.h"

#include "coaxpressXcvrSpeed.h"

uint32_t pll_lock;


void update_cxp_xcvr_uplink_speed(uint8_t data)
{
    /* Common settings */
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RSTPD)=0x33;
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_RSTPD)=0x3;
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CMD) = 0x00;
    *(volatile unsigned int *)(CMD_RATE_SEL_ADDR)=0x0;  //default  Low speed Upconnection Rate Select

    //Tx SI settings
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_TERM_CTRL)=0xF300;
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_DRV_DATA_CTRL)=0x6082AA;
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_DRV_CTRL)=0x160B9FFF;
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_DRV_CTRL_SEL)=0xC;

    if (data == RATE_1DOT25G) //1.25G
    {
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_2)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_3)=0x40100;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_1)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_2)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_3)=0x0;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CTRL_2)=0x4000005;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_EM_CTRL_2)=0x5;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RXPLL_DIV)=0x4419;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_CLK_CTRL)=0x73;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x0;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f0715;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x101481d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400010;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x0019000A;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x01000000;

    }
    else if (data == RATE_2DOT5G) //2.5G
    {
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_2)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_3)=0x40100;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_1)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_2)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_3)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CTRL_2)=0x4000005;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_EM_CTRL_2)=0x5;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RXPLL_DIV)=0x2219;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_CLK_CTRL)=0x73;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f0715;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x101491d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400010;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x320014;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x1000000;

    }
    else if (data == RATE_3DOT125G) //3.125
    {
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_2)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_3)=0x40100;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_1)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_2)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_3)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CTRL_2)=0x4000005;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_EM_CTRL_2)=0x5;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RXPLL_DIV)=0x287d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_CLK_CTRL)=0x73;

        //*(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x0;//10000;
        //*(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f0715;//ff3f071d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x10000;
         *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f071d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x101491d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400010;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x7d0019;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x2000000;
    }
    else if (data == RATE_5DOT0G) //5G
    {

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_2)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_3)=0x40100;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_1)=0x15;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_2)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_3)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CTRL_2)=0x4000005;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_EM_CTRL_2)=0x5;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RXPLL_DIV)=0x232;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_CLK_CTRL)=0x71;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f0715;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x101491d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400010;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x320014;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x1000000;

    }
    else if (data == RATE_6DOT25G) //6.25G
    {

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_2)=0x2a;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_3)=0x4024f;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_1)=0x2a;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_2)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_3)=0x4f00;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CTRL_2)=0x4000006;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_EM_CTRL_2)=0x6;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RXPLL_DIV)=0x47d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_CLK_CTRL)=0x71;
#if 0
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x0;//10000;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f0715;//ff3f071d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x101491d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400010;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x007D000D ;//7d0019;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x04000000;//2000000;
#endif


        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x10000;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f071d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x101491d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400010;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x7d0019;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x2000000;

    }
    else if (data == RATE_10DOT0G) //10G
    {
        *(volatile unsigned int *)(CMD_RATE_SEL_ADDR)=0x1;   // Low speed Upconnection Rate Select

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_2)=0x2a;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_3)=0x4036f;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_1)=0x2a;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_2)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_3)=0x6f00;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CTRL_2)=0x4000007;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_EM_CTRL_2)=0x7;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RXPLL_DIV)=0x8264;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_CLK_CTRL)=0x70;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x0;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f0715;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x1014a1d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400010;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x320014;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x1000000;

    }
    else if (data == RATE_12DOT5G) //12.5G
    {
        *(volatile unsigned int *)(CMD_RATE_SEL_ADDR)=0x1;   // Low speed Upconnection Rate Select

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_2)=0x3f;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_CDR_CTRL_3)=0x4037f;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_1)=0x3f;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_2)=0x11121306;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFEEM_CTRL_3)=0x7f11;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CTRL_2)=0x4000007;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_EM_CTRL_2)=0x7;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RXPLL_DIV)=0x827d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_CLK_CTRL)=0x70;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SERDES_RTL_CTRL)=0x10000;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_0)=0xff3f071d;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_1)=0x105431c;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CTRL_2)=0x400000;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_1)=0x7d0019;
        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + TXPLL_DIV_2)=0x2000000;

        *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_DFE_CAL_CMD)=0x400;
    }

    /* Common settings */
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + SER_RSTPD)=0x1;
    *(volatile unsigned int *)(CXP_DEV_XCVR_BASE + DES_RSTPD)=0x30;

}
