/*
 * Copyright (c) 2015-2021, Renesas Electronics Corporation
 * All rights reserved.
 */

/******************************************************************************
 * @file          rom_api.c
 * - Version      : 0.01
 * @brief         1.Call ROM_SecureBootAPI.
 *                2.Call ROM_GetLcsAPI.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 29.05.2020 0.01     First Release
 *****************************************************************************/

#include <stdint.h>
#include <mmio.h>
#include "rcar_def.h"
#include "rom_api.h"

#define V3M_API_TABLE   (0U)        /* V3M ES1.0 */
#define NEW_API_TABLE   (1U)        /* V3M ES2.0 */
#define API_TABLE_MAX   (2U)        /* table max */

typedef uint32_t (*ROM_SECURE_BOOT_API)(uint32_t *pKeyCert,
                    uint32_t *pContentCert,
                    ROM_FuncReadFlash pFuncReadFlash);
typedef uint32_t (*ROM_GETLCS_API)(uint32_t *pLcs);

static uint32_t get_table_index(void);

uint32_t ROM_SecureBootAPI( uint32_t *pKeyCert,
               uint32_t *pContentCert,
               ROM_FuncReadFlash pFuncReadFlash)
{
    /* Secure Boot API address table */
    static const uintptr_t ROM_SecureBootAPI_table[API_TABLE_MAX] = {
        0xEB103DECU,        /* V3M ES1.0 */
        0xEB100100U     /* V3M ES2.0 or later */
    };

    ROM_SECURE_BOOT_API func;
    uint32_t index;

    index = get_table_index();
    func = (ROM_SECURE_BOOT_API)ROM_SecureBootAPI_table[index];

    return func(pKeyCert, pContentCert, pFuncReadFlash);
}
/* End of function ROM_SecureBootAPI */

uint32_t ROM_GetLcs(uint32_t *pLcs)
{
    /* Get LCS stete API address table */
    static const uintptr_t ROM_GetLcs_table[API_TABLE_MAX] = {
        0xEB10404CU,        /* V3M ES1.0 */
        0xEB10010CU     /* V3M ES2.0 or later */
    };

    ROM_GETLCS_API func;
    uint32_t index;

    index = get_table_index();
    func = (ROM_GETLCS_API)ROM_GetLcs_table[index];

    return func(pLcs);
}
/* End of function ROM_GetLcs */

static uint32_t get_table_index(void)
{
    uint32_t cut_ver;
    uint32_t index;

    cut_ver = mmio_read_32(RCAR_PRR) & RCAR_CUT_MASK;

    if (cut_ver == RCAR_CUT_VER10) {
        index = V3M_API_TABLE;  /* V3M ES1.0 */
    } else {
        index = NEW_API_TABLE;  /* V3M ES2.0 or later */
    }

    return index;
}
/* End of function get_table_index */

