/*******************************************************************************
* 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 2022 Renesas Electronics Corporation All rights reserved.
*******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : RT-VRAM driver
 ******************************************************************************/
/******************************************************************************
 * @file          RTVRAM.c
 * - Version      : 0.03
 * @brief         RT-VRAM driver.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 17.11.2021 0.01     First Release
 *         : 03.12.2021 0.02     remove Cache flush.
 *         : 06.01.2022 0.03     Static analysis support
 *****************************************************************************/

#if defined(__RH850G3K__)
#include <ram_def.h>
#else
#include <rcar_register.h>
#endif
#include <rtvram.h>
#include <mem_io.h>
#include <log.h>
#include <inline_asm.h>

#define RTVRAM_VBUF_CFG_CACHE_MODE_8WAY    (1U << 8U)
#define RTVRAM_VBUF_CFG_VBUF_SIZE_28M      (6U << 0U)

#define RTVRAM_EXT_MODE_EXT     (1U << 0U)

#define RTVRAM_VBUF_NUM    (7U)

#define RTVRAM_EXTEND_ENABLE    (1U)

void rtvram_extendmode(void)
{
#if (RTVRAM_EXTEND == RTVRAM_EXTEND_ENABLE)
    uint32_t reg;
    uint32_t loop;

    /* Set each 4MB from the top of SDRAM as the buffer area of RT-VRAM. */
    for(loop = 0; loop < RTVRAM_VBUF_NUM; loop++)
    {
        mem_write32(get_vbuf_baddr_addr(loop), (uint32_t)((SDRAM_40BIT_ADDR_TOP + (RTVRAM_VBUF_AREA_SIZE * loop)) >> 16U));
    }

    reg = mem_read32(RTVRAM_VBUF_CFG);
    reg |= (RTVRAM_VBUF_CFG_CACHE_MODE_8WAY | RTVRAM_VBUF_CFG_VBUF_SIZE_28M);  /* Cache Mode: 8-way, VBF size: 28M */
    mem_write32(RTVRAM_VBUF_CFG, reg);

    /* Set at the end */
    mem_write32(RTVRAM_EXT_MODE, RTVRAM_EXT_MODE_EXT); /* Change from Compatible Mode to Extended Mode */

    syncm();
#endif
}
/* End of function rtvram_extendmode(void) */
