/*
 * app_driver.h
 */

#ifndef _TZ_ICCC_APP_DRIVER_H_
#define _TZ_ICCC_APP_DRIVER_H_

#define ICCC_MEM_ADDR_ERROR    0
#define PARAM_FOR_ICCC_SEC_MEM 0

// .../tima_common/inc/tima_mem_address.h
#if defined (EXYNOS9820)
    #define ICCC_SECURE_MEM_BASE_ADDR 0xBAB00000
#elif defined (EXYNOS9830) || defined (EXYNOS9630) || defined (EXYNOS3830) || defined (EXYNOS2100) || defined (s5e9925) || defined (s5e8825)
    #define ICCC_SECURE_MEM_BASE_ADDR 0xBF600000
#elif defined (EXYNOS9810)
    #define ICCC_SECURE_MEM_BASE_ADDR 0xBC800000
#elif defined (EXYNOS7904_V4)
    #define ICCC_SECURE_MEM_BASE_ADDR 0xBBC00000
#elif defined (MT6739)
    #define ICCC_SECURE_MEM_BASE_ADDR 0x44640000 /* 0x44600000 - 0x44640000 (ATF-RESERVED) */
#else
    #define ICCC_SECURE_MEM_BASE_ADDR 0x00000000
#endif

// .../tima_common/inc/logging.h
#define SVC_ICCCUTIL_ID        0x00070000
#define ICCCUTIL_CREATE_CMD(x) (SVC_ICCCUTIL_ID | x)

// .../tima_common/inc/tima_ioctl.h
struct ioctl_mem_access_data {
    uint32_t phys_addr;
    uint32_t len;
    char* rw_addr;
    int32_t status;
};

// .../tima_common/inc/tima_ioctl.h
#if defined(SUPPORT_ICCC_DRV)
typedef enum {
    ICCCUTIL_SECURE_PHYS_READ      = ICCCUTIL_CREATE_CMD(0x00000041),
    ICCCUTIL_SECURE_PHYS_WRITE     = ICCCUTIL_CREATE_CMD(0x00000042),
} icccutil_cmd_type;
#else
typedef enum {
    ICCCUTIL_DEBUG_LOG_WRITE       = ICCCUTIL_CREATE_CMD(0x00000000),
    ICCCUTIL_DEBUG_LOG_READ        = ICCCUTIL_CREATE_CMD(0x00000001),
    ICCCUTIL_SECURE_LOG_WRITE      = ICCCUTIL_CREATE_CMD(0x00000002),
    ICCCUTIL_SECURE_LOG_READ       = ICCCUTIL_CREATE_CMD(0x00000003),
    ICCCUTIL_SECURE_PHYS_READ      = ICCCUTIL_CREATE_CMD(0x00000041),
    ICCCUTIL_SECURE_PHYS_WRITE     = ICCCUTIL_CREATE_CMD(0x00000042),
    DMV_SRAM_READ_RECOVERY         = ICCCUTIL_CREATE_CMD(0x00000121)
} icccutil_cmd_type;
#endif

#if defined(SUPPORT_ICCC_DRV)
static const char drv_name[] = "/dev/iccc_driver";
#else
static const char drv_name[] = "/dev/tima_driver"; // TEEGRIS 4 - ICCC v4 (TA-to-TA)
#endif

static int drv_fd = -1;

uint32_t get_sec_ICCC_address(int type);
uint32_t Iccc_phys_write(void *offset, uint32_t length, void *data_buf);
uint32_t Iccc_phys_read(void *offset, uint32_t length, void *ret_buf);
uint32_t ICCC_init_memory();

#endif // _TZ_ICCC_APP_DRIVER_H_
