//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//-----------------------------------------------------------------------------
//
//  Copyright (C) 2004-2007, Freescale Semiconductor, Inc. All Rights Reserved.
//  THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
//  AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//-----------------------------------------------------------------------------
//
//  File:  power.c
//
//  Power BSP callback functions implementation. This function are called as
//  last function before OALCPUPowerOff. The KITL was already disabled by
//  OALKitlPowerOff.
//
//-----------------------------------------------------------------------------

#include <bsp.h>


//-----------------------------------------------------------------------------
// External Functions
extern VOID OALCleanL2Cache();
extern VOID OALFlushL2Cache();
extern void OALCPUEnterWFI(void);

//-----------------------------------------------------------------------------
// External Variables
extern PCSP_CCM_REGS g_pCCM;
extern UINT32 *g_pL2CC;

//-----------------------------------------------------------------------------
VOID BSPPowerOff()
{
    // Request state retention low-power mode
    // Activate well bias
    // Deactivate L2 power gating
    // Request standby supply voltage
    INSREG32(&g_pCCM->CCMR,
        CSP_BITFMASK(CCM_CCMR_VSTBY) |
        CSP_BITFMASK(CCM_CCMR_LPM)   |
        CSP_BITFMASK(CCM_CCMR_L2PG)  |
        CSP_BITFMASK(CCM_CCMR_WBEN)  |
        CSP_BITFMASK(CCM_CCMR_SBYCS),
        CSP_BITFVAL(CCM_CCMR_VSTBY, CCM_CCMR_VSTBY_ENABLE)      |
        CSP_BITFVAL(CCM_CCMR_LPM, CCM_CCMR_LPM_STATE_RETENTION) |
        CSP_BITFVAL(CCM_CCMR_L2PG, CCM_CCMR_L2PG_DISABLE)       |
        CSP_BITFVAL(CCM_CCMR_WBEN, CCM_CCMR_WBEN_ENABLE)        |
        CSP_BITFVAL(CCM_CCMR_SBYCS, CCM_CCMR_SBYCS_DISABLED));

#if 0
    // If L2 will be power gated in state retention mode, request L2 flush
    // to push out incoherent entries into memory and invalidate the
    // cache
    if (g_pL2CC)
    {
        OALFlushL2Cache();
    }
#endif

   // Turn Off Debug LED
   OEMWriteDebugLED(RUNNING_INDICATOR_DLED_INDEX, 0);
   
    // Enter wait-for-interrupt mode and the system will transition
    // to the low-power mode configured in the CCMR above.
    OALCPUEnterWFI();

    // software workaround for MX31 Errata TLSbo72912
#if defined(ENABLE_WATCH_DOG) && defined(NEED_WATCH_DOG_WORKAROUND)
    {
        extern void (* pfnOEMRefreshWatchDog) (void);
        pfnOEMRefreshWatchDog();
    }
#endif

   // Turn On Debug LED
   OEMWriteDebugLED(RUNNING_INDICATOR_DLED_INDEX, 1);
}

//-----------------------------------------------------------------------------

VOID BSPPowerOn()
{

}

//-----------------------------------------------------------------------------

