/*******************************************************************************
 * DISCLAIMER
 * This software is supplied by Renesas Electronics Corporation and is only
 * intended for use with Renesas products. No other uses are authorized. This
 * software is owned by Renesas Electronics Corporation and is protected under
 * all applicable laws, including copyright laws.
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software
 * and to discontinue the availability of this software. By using this software,
 * you agree to the additional terms and conditions found by accessing the
 * following link:
 * http://www.renesas.com/disclaimer
 * Copyright 2021-2023 Renesas Electronics Corporation All rights reserved.
 *******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : IP's control function
 ******************************************************************************/
/******************************************************************************
 * @file          ip_control.c
 * - Version      : 0.08
 * @brief         Initial setting controller.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 28.07.2021 0.01     First Release
 *         : 30.09.2021 0.02     Support of eMMC boot.
 *         : 15.10.2021 0.03     modify include of flash /eMMC.
 *         : 03.12.2021 0.04     CA IPL boot support (workaround)
 *         : 06.01.2022 0.05     Add exception handling for ICUMX_WDTA.
 *         : 02.02.2022 0.06     Add MFIS Lock/Unlock.
 *         : 23.05.2022 0.07     Integration of S4 and V4H
 *         : 23.08.2023 0.08     Add support for V4M.
 *         : 13.10.2023 0.09     Add calling of sysc_c4_power_on function.
 *****************************************************************************/

#include <cpg.h>
#include <scif.h>
#include <emmc_boot.h>
#if (RCAR_SA9_TYPE == FLASH_BOOT)
#include <dma.h>
#include <rpc.h>
#include <mfis.h>
#elif (RCAR_SA9_TYPE == EMMC_BOOT)
#include <emmc_def.h>
#else
/* no process */
#endif
#include <i2c.h>
#include <wdt.h>
#include <vect_set.h>
#include <ip_control.h>

#if ((RCAR_LSI == RCAR_V4H) || (RCAR_LSI == RCAR_V4M))
#include <fcpr.h>
#endif

#if (RCAR_LSI == RCAR_V4M)
#include <sysc.h>
#endif

void ip_init(void)
{
    scif_init();
    set_vect_table();
    wdt_init();
    cpg_init();
#if (RCAR_SA9_TYPE == FLASH_BOOT)
    dma_init();
    rpc_init();
    mfis_init();
 #if (CA_LOAD_TYPE == CA_IPL)
    emmc_initialize();  /* workaround  */
 #endif  /* (CA_LOAD_TYPE == CA_IPL) */
#elif (RCAR_SA9_TYPE == EMMC_BOOT) /* (RCAR_SA9_TYPE == FLASH_BOOT) */
    emmc_initialize();
#else   /* (RCAR_SA9_TYPE == FLASH_BOOT) */
    /* No process */
#endif  /* (RCAR_SA9_TYPE == FLASH_BOOT) */

#if ((RCAR_LSI == RCAR_V4H) || (RCAR_LSI == RCAR_V4M))
    fcpr_init();
#endif /* ((RCAR_LSI == RCAR_V4H) || (RCAR_LSI == RCAR_V4M)) */

#if (RCAR_LSI == RCAR_V4M)
    /*
     * For accessing Region ID registers by ICUMX,
     * and starting Cortex-A76 by CR52 2nd IPL.
     */
    sysc_c4_power_on();
#endif /* RCAR_LSI == RCAR_V4M */
#if (SAN_ENABLE == 1)
    i2c5_init();
#endif
}
/* End of function ip_init(void) */

void ip_release(void)
{
#if (SAN_ENABLE == 1)
    i2c5_release();
#endif
#if (RCAR_SA9_TYPE == FLASH_BOOT)
    rpc_release();
    dma_release();
#elif (RCAR_SA9_TYPE == EMMC_BOOT)
    emmc_terminate();
#else
    /* No process */
#endif
    wdt_restart();
}
/* End of function ip_release(void) */
