/******************************************************************************/
/*                                                                            */
/* Broadcom BCM5700 Linux Network Driver, Copyright (c) 2001 Broadcom         */
/* Corporation.                                                               */
/* All rights reserved.                                                       */
/*                                                                            */
/* This program 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, located in the file LICENSE.                 */
/*                                                                            */
/* History:                                                                   */
/******************************************************************************/
#if !defined(CONFIG_NET_MULTI)
#if INCLUDE_TBI_SUPPORT
#include "bcm570x_autoneg.h"
#include "bcm570x_mm.h"


/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
void
MM_AnTxConfig(
    PAN_STATE_INFO pAnInfo)
{
    PLM_DEVICE_BLOCK pDevice;

    pDevice = (PLM_DEVICE_BLOCK) pAnInfo->pContext;

    REG_WR(pDevice, MacCtrl.TxAutoNeg, (LM_UINT32) pAnInfo->TxConfig.AsUSHORT);

    pDevice->MacMode |= MAC_MODE_SEND_CONFIGS;
    REG_WR(pDevice, MacCtrl.Mode, pDevice->MacMode);
}


/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
void
MM_AnTxIdle(
    PAN_STATE_INFO pAnInfo)
{
    PLM_DEVICE_BLOCK pDevice;

    pDevice = (PLM_DEVICE_BLOCK) pAnInfo->pContext;

    pDevice->MacMode &= ~MAC_MODE_SEND_CONFIGS;
    REG_WR(pDevice, MacCtrl.Mode, pDevice->MacMode);
}


/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
char
MM_AnRxConfig(
    PAN_STATE_INFO pAnInfo,
    unsigned short *pRxConfig)
{
    PLM_DEVICE_BLOCK pDevice;
    LM_UINT32 Value32;
    char Retcode;

    Retcode = AN_FALSE;

    pDevice = (PLM_DEVICE_BLOCK) pAnInfo->pContext;

    Value32 = REG_RD(pDevice, MacCtrl.Status);
    if(Value32 & MAC_STATUS_RECEIVING_CFG)
    {
	Value32 = REG_RD(pDevice, MacCtrl.RxAutoNeg);
	*pRxConfig = (unsigned short) Value32;

	Retcode = AN_TRUE;
    }

    return Retcode;
}


/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
void
AutonegInit(
    PAN_STATE_INFO pAnInfo)
{
    unsigned long j;

    for(j = 0; j < sizeof(AN_STATE_INFO); j++)
    {
	((unsigned char *) pAnInfo)[j] = 0;
    }

    /* Initialize the default advertisement register. */
    pAnInfo->mr_adv_full_duplex = 1;
    pAnInfo->mr_adv_sym_pause = 1;
    pAnInfo->mr_adv_asym_pause = 1;
    pAnInfo->mr_an_enable = 1;
}


/******************************************************************************/
/* Description:                                                               */
/*                                                                            */
/* Return:                                                                    */
/******************************************************************************/
AUTONEG_STATUS
Autoneg8023z(
    PAN_STATE_INFO pAnInfo)
{
    unsigned short RxConfig;
    unsigned long Delta_us;
    AUTONEG_STATUS AnRet;

    /* Get the current time. */
    if(pAnInfo->State == AN_STATE_UNKNOWN)
    {
	pAnInfo->RxConfig.AsUSHORT = 0;
	pAnInfo->CurrentTime_us = 0;
	pAnInfo->LinkTime_us = 0;
	pAnInfo->AbilityMatchCfg = 0;
	pAnInfo->AbilityMatchCnt = 0;
	pAnInfo->AbilityMatch = AN_FALSE;
	pAnInfo->IdleMatch = AN_FALSE;
	pAnInfo->AckMatch = AN_FALSE;
    }

    /* Increment the timer tick.  This function is called every microsecon. */
/*    pAnInfo->CurrentTime_us++; */

    /* Set the AbilityMatch, IdleMatch, and AckMatch flags if their */
    /* corresponding conditions are satisfied. */
    if(MM_AnRxConfig(pAnInfo, &RxConfig))
    {
	if(RxConfig != pAnInfo->AbilityMatchCfg)
	{
	    pAnInfo->AbilityMatchCfg = RxConfig;
	    pAnInfo->AbilityMatch = AN_FALSE;
	    pAnInfo->AbilityMatchCnt = 0;
	}
	else
	{
	    pAnInfo->AbilityMatchCnt++;
	    if(pAnInfo->AbilityMatchCnt > 1)
	    {
		pAnInfo->AbilityMatch = AN_TRUE;
		pAnInfo->AbilityMatchCfg = RxConfig;
	    }
	}

	if(RxConfig & AN_CONFIG_ACK)
	{
	    pAnInfo->AckMatch = AN_TRUE;
	}
	else
	{
	    pAnInfo->AckMatch = AN_FALSE;
	}

	pAnInfo->IdleMatch = AN_FALSE;
    }
    else
    {
	pAnInfo->IdleMatch = AN_TRUE;

	pAnInfo->AbilityMatchCfg = 0;
	pAnInfo->AbilityMatchCnt = 0;
	pAnInfo->AbilityMatch = AN_FALSE;
	pAnInfo->AckMatch = AN_FALSE;

	RxConfig = 0;
    }

    /* Save the last Config. */
    pAnInfo->RxConfig.AsUSHORT = RxConfig;

    /* Default return code. */
    AnRet = AUTONEG_STATUS_OK;

    /* Autoneg state machine as defined in 802.3z section 37.3.1.5. */
    switch(pAnInfo->State)
    {
	case AN_STATE_UNKNOWN:
	    if(pAnInfo->mr_an_enable || pAnInfo->mr_restart_an)
	    {
		pAnInfo->CurrentTime_us = 0;
		pAnInfo->State = AN_STATE_AN_ENABLE;
	    }

	    /* Fall through.*/

	case AN_STATE_AN_ENABLE:
	    pAnInfo->mr_an_complete = AN_FALSE;
	    pAnInfo->mr_page_rx = AN_FALSE;

	    if(pAnInfo->mr_an_enable)
	    {
		pAnInfo->LinkTime_us = 0;
		pAnInfo->AbilityMatchCfg = 0;
		pAnInfo->AbilityMatchCnt = 0;
		pAnInfo->AbilityMatch = AN_FALSE;
		pAnInfo->IdleMatch = AN_FALSE;
		pAnInfo->AckMatch = AN_FALSE;

		pAnInfo->State = AN_STATE_AN_RESTART_INIT;
	    }
	    else
	    {
		pAnInfo->State = AN_STATE_DISABLE_LINK_OK;
	    }
	    break;

	case AN_STATE_AN_RESTART_INIT:
	    pAnInfo->LinkTime_us = pAnInfo->CurrentTime_us;
	    pAnInfo->mr_np_loaded = AN_FALSE;

	    pAnInfo->TxConfig.AsUSHORT = 0;
	    MM_AnTxConfig(pAnInfo);

	    AnRet = AUTONEG_STATUS_TIMER_ENABLED;

	    pAnInfo->State = AN_STATE_AN_RESTART;

	    /* Fall through.*/

	case AN_STATE_AN_RESTART:
	    /* Get the current time and compute the delta with the saved */
	    /* link timer. */
	    Delta_us = pAnInfo->CurrentTime_us - pAnInfo->LinkTime_us;
	    if(Delta_us > AN_LINK_TIMER_INTERVAL_US)
	    {
		pAnInfo->State = AN_STATE_ABILITY_DETECT_INIT;
	    }
	    else
	    {
		AnRet = AUTONEG_STATUS_TIMER_ENABLED;
	    }
	    break;

	case AN_STATE_DISABLE_LINK_OK:
	    AnRet = AUTONEG_STATUS_DONE;
	    break;

	case AN_STATE_ABILITY_DETECT_INIT:
	    /* Note: in the state diagram, this variable is set to */
	    /* mr_adv_ability<12>.  Is this right?. */
	    pAnInfo->mr_toggle_tx = AN_FALSE;

	    /* Send the config as advertised in the advertisement register. */
	    pAnInfo->TxConfig.AsUSHORT = 0;
	    pAnInfo->TxConfig.D5_FD = pAnInfo->mr_adv_full_duplex;
	    pAnInfo->TxConfig.D6_HD = pAnInfo->mr_adv_half_duplex;
	    pAnInfo->TxConfig.D7_PS1 = pAnInfo->mr_adv_sym_pause;
	    pAnInfo->TxConfig.D8_PS2 = pAnInfo->mr_adv_asym_pause;
	    pAnInfo->TxConfig.D12_RF1 = pAnInfo->mr_adv_remote_fault1;
	    pAnInfo->TxConfig.D13_RF2 = pAnInfo->mr_adv_remote_fault2;
	    pAnInfo->TxConfig.D15_NP = pAnInfo->mr_adv_next_page;

	    MM_AnTxConfig(pAnInfo);

	    pAnInfo->State = AN_STATE_ABILITY_DETECT;

	    break;

	case AN_STATE_ABILITY_DETECT:
	    if(pAnInfo->AbilityMatch == AN_TRUE &&
		pAnInfo->RxConfig.AsUSHORT != 0)
	    {
		pAnInfo->State = AN_STATE_ACK_DETECT_INIT;
	    }

	    break;

	case AN_STATE_ACK_DETECT_INIT:
	    pAnInfo->TxConfig.D14_ACK = 1;
	    MM_AnTxConfig(pAnInfo);

	    pAnInfo->State = AN_STATE_ACK_DETECT;

	    /* Fall through. */

	case AN_STATE_ACK_DETECT:
	    if(pAnInfo->AckMatch == AN_TRUE)
	    {
		if((pAnInfo->RxConfig.AsUSHORT & ~AN_CONFIG_ACK) ==
		    (pAnInfo->AbilityMatchCfg & ~AN_CONFIG_ACK))
		{
		    pAnInfo->State = AN_STATE_COMPLETE_ACK_INIT;
		}
		else
		{
		    pAnInfo->State = AN_STATE_AN_ENABLE;
		}
	    }
	    else if(pAnInfo->AbilityMatch == AN_TRUE &&
		pAnInfo->RxConfig.AsUSHORT == 0)
	    {
		pAnInfo->State = AN_STATE_AN_ENABLE;
	    }

	    break;

	case AN_STATE_COMPLETE_ACK_INIT:
	    /* Make sure invalid bits are not set. */
	    if(pAnInfo->RxConfig.bits.D0 || pAnInfo->RxConfig.bits.D1 ||
		pAnInfo->RxConfig.bits.D2 || pAnInfo->RxConfig.bits.D3 ||
		pAnInfo->RxConfig.bits.D4 || pAnInfo->RxConfig.bits.D9 ||
		pAnInfo->RxConfig.bits.D10 || pAnInfo->RxConfig.bits.D11)
	    {
		AnRet = AUTONEG_STATUS_FAILED;
		break;
	    }

	    /* Set up the link partner advertisement register. */
	    pAnInfo->mr_lp_adv_full_duplex = pAnInfo->RxConfig.D5_FD;
	    pAnInfo->mr_lp_adv_half_duplex = pAnInfo->RxConfig.D6_HD;
	    pAnInfo->mr_lp_adv_sym_pause = pAnInfo->RxConfig.D7_PS1;
	    pAnInfo->mr_lp_adv_asym_pause = pAnInfo->RxConfig.D8_PS2;
	    pAnInfo->mr_lp_adv_remote_fault1 = pAnInfo->RxConfig.D12_RF1;
	    pAnInfo->mr_lp_adv_remote_fault2 = pAnInfo->RxConfig.D13_RF2;
	    pAnInfo->mr_lp_adv_next_page = pAnInfo->RxConfig.D15_NP;

	    pAnInfo->LinkTime_us = pAnInfo->CurrentTime_us;

	    pAnInfo->mr_toggle_tx = !pAnInfo->mr_toggle_tx;
	    pAnInfo->mr_toggle_rx = pAnInfo->RxConfig.bits.D11;
	    pAnInfo->mr_np_rx = pAnInfo->RxConfig.D15_NP;
	    pAnInfo->mr_page_rx = AN_TRUE;

	    pAnInfo->State = AN_STATE_COMPLETE_ACK;
	    AnRet = AUTONEG_STATUS_TIMER_ENABLED;

	    break;

	case AN_STATE_COMPLETE_ACK:
	    if(pAnInfo->AbilityMatch == AN_TRUE &&
		pAnInfo->RxConfig.AsUSHORT == 0)
	    {
		pAnInfo->State = AN_STATE_AN_ENABLE;
		break;
	    }

	    Delta_us = pAnInfo->CurrentTime_us - pAnInfo->LinkTime_us;

	    if(Delta_us > AN_LINK_TIMER_INTERVAL_US)
	    {
		if(pAnInfo->mr_adv_next_page == 0 ||
		    pAnInfo->mr_lp_adv_next_page == 0)
		{
		    pAnInfo->State = AN_STATE_IDLE_DETECT_INIT;
		}
		else
		{
		    if(pAnInfo->TxConfig.bits.D15 == 0 &&
			pAnInfo->mr_np_rx == 0)
		    {
			pAnInfo->State = AN_STATE_IDLE_DETECT_INIT;
		    }
		    else
		    {
			AnRet = AUTONEG_STATUS_FAILED;
		    }
		}
	    }

	    break;

	case AN_STATE_IDLE_DETECT_INIT:
	    pAnInfo->LinkTime_us = pAnInfo->CurrentTime_us;

	    MM_AnTxIdle(pAnInfo);

	    pAnInfo->State = AN_STATE_IDLE_DETECT;

	    AnRet = AUTONEG_STATUS_TIMER_ENABLED;

	    break;

	case AN_STATE_IDLE_DETECT:
	    if(pAnInfo->AbilityMatch == AN_TRUE &&
		pAnInfo->RxConfig.AsUSHORT == 0)
	    {
		pAnInfo->State = AN_STATE_AN_ENABLE;
		break;
	    }

	    Delta_us = pAnInfo->CurrentTime_us - pAnInfo->LinkTime_us;
	    if(Delta_us > AN_LINK_TIMER_INTERVAL_US)
	    {
#if 0
/*                if(pAnInfo->IdleMatch == AN_TRUE) */
/*                { */
#endif
		    pAnInfo->State = AN_STATE_LINK_OK;
#if 0
/*                } */
/*                else */
/*                { */
/*                    AnRet = AUTONEG_STATUS_FAILED; */
/*                    break; */
/*                } */
#endif
	    }

	    break;

	case AN_STATE_LINK_OK:
	    pAnInfo->mr_an_complete = AN_TRUE;
	    pAnInfo->mr_link_ok = AN_TRUE;
	    AnRet = AUTONEG_STATUS_DONE;

	    break;

	case AN_STATE_NEXT_PAGE_WAIT_INIT:
	    break;

	case AN_STATE_NEXT_PAGE_WAIT:
	    break;

	default:
	    AnRet = AUTONEG_STATUS_FAILED;
	    break;
    }

    return AnRet;
}
#endif /* INCLUDE_TBI_SUPPORT */

#endif /* !defined(CONFIG_NET_MULTI) */