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

/******************************************************************************
 * @file          lifec_init.c
 * - Version      : 0.01
 * @brief         Initial setting process of LifeC.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 29.05.2020 0.01     First Release
 *****************************************************************************/

#include <stdint.h>     /* for uint32_t */
#include <mmio.h>
#include "lifec_register.h"
#include "lifec_init.h"
#include "micro_wait.h"

static void lifec_reg_write(uint32_t addr, uint32_t data);

static void lifec_reg_write(uint32_t addr, uint32_t data)
{
    mmio_write_32((uintptr_t)addr, data);
}
/* End of function lifec_reg_write */

void lifec_init(void)
{
    uint32_t loop;
    const uint32_t lifec_reg_tbl[][2] = {
        {   SEC_GRP0CR2 ,   0x3002981BU },
        {   SEC_GRP1CR2 ,   0x00000002U },
        {   SAFE_GRP0CR2,   0x3002981BU },
        {   SAFE_GRP1CR2,   0x20001803U },

        {   SEC_GRP0CR0 ,   0x63000081U },
        {   SEC_GRP1CR0 ,   0x00000000U },
        {   SAFE_GRP0CR0,   0x63000081U },
        {   SAFE_GRP1CR0,   0x20000000U },

        {   SEC_GRP0CR1 ,   0x30000C00U },
        {   SEC_GRP1CR1 ,   0x00000000U },
        {   SAFE_GRP0CR1,   0x30000C00U },
        {   SAFE_GRP1CR1,   0x20000000U },

        {   SEC_GRP0CR3 ,   0x72F04781U },
        {   SEC_GRP1CR3 ,   0x00000000U },
        {   SAFE_GRP0CR3,   0x72F04781U },
        {   SAFE_GRP1CR3,   0x00004000U },

        {   SEC_SEL0        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND0   ,   0x02880F00U },
        {   SEC_GRP1COND0   ,   0x00000000U },
        {   SEC_READONLY0   ,   0x00000000U },
        {   SAFE_GRP0COND0  ,   0x02880F00U },
        {   SAFE_GRP1COND0  ,   0x00000000U },
        {   SAFE_READONLY0  ,   0x00000000U },

        {   SEC_SEL1        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND1   ,   0x00000086U },
        {   SEC_GRP1COND1   ,   0x00000000U },
        {   SEC_READONLY1   ,   0x00000000U },
        {   SAFE_GRP0COND1  ,   0x00000886U },
        {   SAFE_GRP1COND1  ,   0x00000800U },
        {   SAFE_READONLY1  ,   0x00000800U },

        {   SEC_SEL2        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND2   ,   0x00078000U },
        {   SEC_GRP1COND2   ,   0x00000000U },
        {   SEC_READONLY2   ,   0x00000000U },
        {   SAFE_GRP0COND2  ,   0x0007A000U },
        {   SAFE_GRP1COND2  ,   0x00002000U },
        {   SAFE_READONLY2  ,   0x00002000U },

        {   SEC_SEL3        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND3   ,   0x84080200U },
        {   SEC_GRP1COND3   ,   0x00000000U },
        {   SEC_READONLY3   ,   0x00000000U },
        {   SAFE_GRP0COND3  ,   0xC444047CU },
        {   SAFE_GRP1COND3  ,   0x4044047CU },
        {   SAFE_READONLY3  ,   0x4044047CU },

        {   SEC_SEL4        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND4   ,   0x80000000U },
        {   SEC_GRP1COND4   ,   0x00000000U },
        {   SEC_READONLY4   ,   0x00000000U },
        {   SAFE_GRP0COND4  ,   0xF0000001U },
        {   SAFE_GRP1COND4  ,   0x70000001U },
        {   SAFE_READONLY4  ,   0x70000001U },

        {   SEC_SEL5        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND5   ,   0x00000808U },
        {   SEC_GRP1COND5   ,   0x00000000U },
        {   SEC_READONLY5   ,   0x00000000U },
        {   SAFE_GRP0COND5  ,   0x0BDFD05BU },
        {   SAFE_GRP1COND5  ,   0x0BDFD053U },
        {   SAFE_READONLY5  ,   0x0BDFD053U },

        {   SEC_SEL6        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND6   ,   0x00000000U },
        {   SEC_GRP1COND6   ,   0x00000000U },
        {   SEC_READONLY6   ,   0x00000000U },
        {   SAFE_GRP0COND6  ,   0x000035FBU },
        {   SAFE_GRP1COND6  ,   0x000035FBU },
        {   SAFE_READONLY6  ,   0x000035FBU },

        {   SEC_SEL7        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND7   ,   0x00000000U },
        {   SEC_GRP1COND7   ,   0x00000000U },
        {   SEC_READONLY7   ,   0x00000000U },
        {   SAFE_GRP0COND7  ,   0x00000000U },
        {   SAFE_GRP1COND7  ,   0x00000000U },
        {   SAFE_READONLY7  ,   0x00000000U },

        {   SEC_SEL8        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND8   ,   0x00000000U },
        {   SEC_GRP1COND8   ,   0x00000000U },
        {   SEC_READONLY8   ,   0x00000000U },
        {   SAFE_GRP0COND8  ,   0x00000000U },
        {   SAFE_GRP1COND8  ,   0x00000000U },
        {   SAFE_READONLY8  ,   0x00000000U },

        {   SEC_SEL9        ,   0xFFFFFFFFU },
        {   SEC_GRP0COND9   ,   0x00000000U },
        {   SEC_GRP1COND9   ,   0x00000000U },
        {   SEC_READONLY9   ,   0x00000000U },
        {   SAFE_GRP0COND9  ,   0x00000000U },
        {   SAFE_GRP1COND9  ,   0x00000000U },
        {   SAFE_READONLY9  ,   0x00000000U },

        {   SEC_SEL10       ,   0xFFFFFFFFU },
        {   SEC_GRP0COND10  ,   0x00000000U },
        {   SEC_GRP1COND10  ,   0x00000000U },
        {   SEC_READONLY10  ,   0x00000000U },
        {   SAFE_GRP0COND10 ,   0x00000000U },
        {   SAFE_GRP1COND10 ,   0x00000000U },
        {   SAFE_READONLY10 ,   0x00000000U },

        {   SEC_SEL11       ,   0xFFFFFFFFU },
        {   SEC_GRP0COND11  ,   0x03000000U },
        {   SEC_GRP1COND11  ,   0x00000000U },
        {   SEC_READONLY11  ,   0x00000000U },
        {   SAFE_GRP0COND11 ,   0xE3000000U },
        {   SAFE_GRP1COND11 ,   0xE0000000U },
        {   SAFE_READONLY11 ,   0xE0000000U },

        {   SEC_SEL12       ,   0xFFFFFFFFU },
        {   SEC_GRP0COND12  ,   0x06884001U },
        {   SEC_GRP1COND12  ,   0x00000000U },
        {   SEC_READONLY12  ,   0x00000000U },
        {   SAFE_GRP0COND12 ,   0x07F8C001U },
        {   SAFE_GRP1COND12 ,   0x01708000U },
        {   SAFE_READONLY12 ,   0x0170C000U },

        {   SEC_SEL13       ,   0xFFFFFFFFU },
        {   SEC_GRP0COND13  ,   0x01407018U },
        {   SEC_GRP1COND13  ,   0x00000000U },
        {   SEC_READONLY13  ,   0x00000000U },
        {   SAFE_GRP0COND13 ,   0xF907F018U },
        {   SAFE_GRP1COND13 ,   0xF8078000U },
        {   SAFE_READONLY13 ,   0xF8078000U },

        {   SEC_SEL14       ,   0xFFFFFFFFU },
        {   SEC_GRP0COND14  ,   0xD18844C4U },
        {   SEC_GRP1COND14  ,   0x00000000U },
        {   SEC_READONLY14  ,   0x00000000U },
        {   SAFE_GRP0COND14 ,   0xFF8A46C4U },
        {   SAFE_GRP1COND14 ,   0x2E020200U },
        {   SAFE_READONLY14 ,   0x2E020200U },

        {   SEC_SEL15       ,   0xFFFFFFBFU },
        {   SEC_GRP0COND15  ,   0x00005208U },
        {   SEC_GRP1COND15  ,   0x00000000U },
        {   SEC_READONLY15  ,   0x00000180U },
        {   SAFE_GRP0COND15 ,   0x3E78BFCFU },
        {   SAFE_GRP1COND15 ,   0x3E78ADC7U },
        {   SAFE_READONLY15 ,   0x3E78AD87U },
    };

    for (loop = 0U;
        loop < (sizeof(lifec_reg_tbl) / sizeof(lifec_reg_tbl[0]));
        loop++)
    {
        lifec_reg_write(lifec_reg_tbl[loop][0], lifec_reg_tbl[loop][1]);
    }
}
/* End of function lifec_init */

void lifec_cr7_setting(void)
{
#if RCAR_LIFEC_NON_SECURE_MASTER
    /* Security attribute setting for master ports (CR7 Secure -> Non Secure)*/
    lifec_reg_write(SEC_SRC,             0x0000001EU);
    micro_wait(10U);
#endif
}
/* End of function lifec_cr7_setting */

