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

/******************************************************************************
 * @file          axi_bus_timeout.c
 * - Version      : 0.01
 * @brief         AXI bus timeout setting.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 29.05.2020 0.01     First Release
 *****************************************************************************/

#include <mmio.h>
#include "axi_register.h"
#include "axi_init.h"

#define FDT_NON_SAFETY_RELATED  (0x00000000U)
#define FDT_SAFETY_RELATED  (0x00000001U)
#define FDT_TIMEOUT_CNT     (0x00000002U)

#define TIMEOUT_TBL_MAX (sizeof(s_timeout_tbl) / sizeof((s_timeout_tbl)[0]))

#define EDC_FIX_ENABLE_BIT  (0x00000001U)

#define FDT_CCP         (0xE6620500U)
#define FDT_SDHI0       (0xE7751304U)
#define FDT_SDM1B       (0xE7751308U)
#define FDT_SDM2B       (0xE775130CU)
#define FDT_SDM1M       (0xE7751310U)
#define FDT_SDM2M       (0xE7751314U)
#define FDT_SDM1P       (0xE7780704U)
#define FDT_SDM2P       (0xE7780708U)
#define FDT_FCPVD0      (0xFEBF0600U)
#define FDT_IMR01       (0xFEBF0610U)
#define FDT_IMR23       (0xFEBF0614U)
#define FDT_VIN0        (0xFEBF0630U)
#define FDT_IV1E        (0xFEBF0680U)
#define FDT_ISP0        (0xFEBF0690U)
#define FDT_ISP1        (0xFEBF0694U)
#define FDT_FBAC530     (0xFF860200U)
#define FDT_FBAC531     (0xFF860204U)
#define FDT_FBAC53D     (0xFF86020CU)
#define FDT_FBAIMR01    (0xFF860210U)
#define FDT_FBAIMR23    (0xFF860214U)
#define FDT_FBAIMP0     (0xFF860218U)
#define FDT_FBAIMP1     (0xFF86021CU)
#define FDT_FBAIMP2     (0xFF860220U)
#define FDT_FBAIMP3     (0xFF860224U)
#define FDT_FBAMM       (0xFF860228U)
#define FDT_FBAOCV0     (0xFF86022CU)
#define FDT_FBAOCV1     (0xFF860230U)
#define FDT_IMP0        (0xFF880600U)
#define FDT_IMP1        (0xFF880604U)
#define FDT_RTD0P       (0xFFC30700U)
#define FDT_RTD1P       (0xFFC30704U)
#define FDT_CR7         (0xFFC40500U)
#define FDT_RTD0B       (0xFFC40504U)
#define FDT_RTD0M       (0xFFC40508U)
#define FDT_CSD         (0xFFC40520U)
#define FDT_EAVB        (0xFFC405C0U)
#define FDT_IPC64       (0xFFC405C4U)
#define FDT_FRAY        (0xFFC405CCU)
#define FDT_CAIP        (0xFFC405D0U)
#define FDT_RTD1B       (0xFFC405D4U)
#define FDT_RTD1M       (0xFFC405D8U)

typedef struct {
    uintptr_t addr;
    uintptr_t safety_related;
} st_axi_bus_timeout_counter_data_t;

void axi_bus_timeout_setting(void)
{
    uint32_t loop;

    static const st_axi_bus_timeout_counter_data_t s_timeout_tbl[] = {
        { FDT_CCP, FDT_NON_SAFETY_RELATED },
        { FDT_SDHI0, FDT_NON_SAFETY_RELATED },
        { FDT_SDM1B, FDT_SAFETY_RELATED },
        { FDT_SDM2B, FDT_SAFETY_RELATED },
        { FDT_SDM1M, FDT_SAFETY_RELATED },
        { FDT_SDM2M, FDT_SAFETY_RELATED },
        { FDT_SDM1P, FDT_SAFETY_RELATED },
        { FDT_SDM2P, FDT_SAFETY_RELATED },
        { FDT_IMR23, FDT_SAFETY_RELATED },
        { FDT_FCPVD0, FDT_NON_SAFETY_RELATED },
        { FDT_IMR01, FDT_SAFETY_RELATED },
        { FDT_VIN0, FDT_SAFETY_RELATED },
        { FDT_IV1E, FDT_NON_SAFETY_RELATED },
        { FDT_ISP0, FDT_SAFETY_RELATED },
        { FDT_ISP1, FDT_SAFETY_RELATED },
        { FDT_FBAC530, FDT_SAFETY_RELATED },
        { FDT_FBAC531, FDT_SAFETY_RELATED },
        { FDT_FBAC53D, FDT_SAFETY_RELATED },
        { FDT_FBAIMR01, FDT_SAFETY_RELATED },
        { FDT_FBAIMR23, FDT_SAFETY_RELATED },
        { FDT_FBAIMP0, FDT_SAFETY_RELATED },
        { FDT_FBAIMP1, FDT_SAFETY_RELATED },
        { FDT_FBAIMP2, FDT_SAFETY_RELATED },
        { FDT_FBAIMP3, FDT_SAFETY_RELATED },
        { FDT_FBAMM, FDT_SAFETY_RELATED },
        { FDT_FBAOCV0, FDT_SAFETY_RELATED },
        { FDT_FBAOCV1, FDT_SAFETY_RELATED },
        { FDT_IMP0, FDT_NON_SAFETY_RELATED },
        { FDT_IMP1, FDT_NON_SAFETY_RELATED },
        { FDT_RTD0P, FDT_SAFETY_RELATED },
        { FDT_RTD1P, FDT_SAFETY_RELATED },
        { FDT_CR7, FDT_SAFETY_RELATED },
        { FDT_RTD0B, FDT_SAFETY_RELATED },
        { FDT_RTD0M, FDT_SAFETY_RELATED },
        { FDT_CSD, FDT_NON_SAFETY_RELATED },
        { FDT_EAVB, FDT_SAFETY_RELATED },
        { FDT_IPC64, FDT_SAFETY_RELATED },
        { FDT_FRAY, FDT_SAFETY_RELATED },
        { FDT_CAIP, FDT_NON_SAFETY_RELATED },
        { FDT_RTD1B, FDT_SAFETY_RELATED },
        { FDT_RTD1M, FDT_SAFETY_RELATED }
    };

    for (loop = 0U; loop < TIMEOUT_TBL_MAX; loop++)
    {
        if (s_timeout_tbl[loop].safety_related == FDT_SAFETY_RELATED)
        {
            mmio_write_32(s_timeout_tbl[loop].addr, FDT_TIMEOUT_CNT);
        }
    }

    if ((mmio_read_32(EDC_EDCFIXERREN) &
                EDC_FIX_ENABLE_BIT) != 0x00000001U) {
        mmio_write_32(EDC_EDCFIXERREN, 0x00000001U);
    }

    mmio_write_32(EDC_EDCFIXERRINTEN0, 0x11B40078U);
    mmio_write_32(EDC_EDCFIXERRINTEN1, 0x00000000U);
    mmio_write_32(EDC_EDCFIXERRINTEN2, 0xC0000E00U);
    mmio_write_32(EDC_EDCFIXERRINTEN3, 0x7E18BAFFU);

}
/* End of function axi_bus_timeout_setting */

