/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * 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 https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author: brianpc.huang $
 * $Date: 2015/05/29 $
 * $RCSfile: bsp_start.c,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file board.c
 *  board.c provides functions of board initialization, board detection, and
 *
 */

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------

#ifdef CC_MTK_LOADER

#include "x_typedef.h"
#include "x_lint.h"
#include "x_hal_arm.h"
#include "x_hal_5381.h"
#include "x_serial.h"
#include "x_assert.h"
#include "x_timer.h"
#include "x_pinmux.h"
#include "x_printf.h"
#if defined(CC_MT5396) || defined(CC_MT5398)
#include "pcie_if.h"
#endif
//-----------------------------------------------------------------------------
// External functions
//-----------------------------------------------------------------------------

#ifndef CC_LOADER_MEM_OFFSET
#define CC_LOADER_MEM_OFFSET            (DRAM_START + 0xd00000)
#endif /* CC_LOADER_MEM_OFFSET */

#if !defined(__GCC__)
EXTERN UINT32 Image$$BSS$$ZI$$Base;
EXTERN UINT32 Image$$BSS$$ZI$$Length;
#else
EXTERN UINT32 __bss_start;
EXTERN UINT32 __bss_end;
#endif
EXTERN void *x_memset(void *s, int c, UINT32 n);
EXTERN void *x_memcpy(void *dest, const void *src, UINT32 z_size);
EXTERN void RemapJumpToDram(void);
EXTERN void CMain(void);


//-----------------------------------------------------------------------------
// Public functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/** BSP_Remap() Call assembly function to remap flash/dram and jump to 0
 */
//-----------------------------------------------------------------------------
void BSP_Remap(void)
{
    RemapJumpToDram();
}

//-----------------------------------------------------------------------------
/** BSP_Main() The entry point of BSP main routine
 */
//-----------------------------------------------------------------------------
void BSP_Main(void)
{
    volatile UINT32 u4Loop = 1;

#ifndef CC_MTK_PRELOADER

    #if defined(CC_MT5396) || defined(CC_MT5398)
    // Set pcie phy and pull pcie reset pin.
    PCIE_PreInit();
    #endif

    #if defined(CC_DISABLE_UART_UPGRADE)
    // turn off UART into RX off
    //vIO32Write4B(0xf000c020, 0x0000005a);
    UINT32 current = 0;
    current = IO_READ32(PDWNC_CLKPDN, 0x8);
    IO_WRITE32(PDWNC_CLKPDN, 0x8, current | 0x2000); // Disable UART RX with 0xF0028008[13]=0
    #endif
    #if 0 //!defined(CC_MT5863) && !defined(CC_MT5893)
    // Copy loader exception table for IRQ handler on loader
    x_memcpy(0, (void *)CC_LOADER_MEM_OFFSET, 0x40);
    #endif
    // BSS zeroing.
    #if !defined(__GCC__)
    x_memset(&Image$$BSS$$ZI$$Base, 0, (UINT32)(&Image$$BSS$$ZI$$Length));
    #else
    x_memset(&__bss_start, 0, (UINT32)(&__bss_end) - (UINT32)(&__bss_start));
    #endif

    // Initial Timer again after stack is clear
    VERIFY(HAL_InitTimer());

    BSP_CkgenInit();
#endif /* CC_MTK_PRELOADER */

    // Call loader CMain() function.
    CMain();

    // Never goes here
    while (u4Loop);
}

//-----------------------------------------------------------------------------
/** BSP_HaltSystem() Halt the system
 */
//-----------------------------------------------------------------------------
void BSP_HaltSystem(void)
{
    volatile UINT32 u4Loop = 1;

    //SerWaitTxBufClear();

    // Disable interrupts
#ifndef CC_MTK_PRELOADER
    HalDisableIRQ();
    HalDisableFIQ();

    SerEnd();
#endif /* CC_MTK_PRELOADER */

    // Halt the system
    while (u4Loop);
}

#endif /* CC_MTK_LOADER */
