/*
 * @file exo_ahw_vdp_mt28ew128.c
 *
 * @brief This file contains the vendor driver porting function of mt28ew128_ flash memory
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "exo_ahw_vdp_mt28ew128.h"
#include "mt28ew128.h"
#include "exo_osal_mem_management.h"
#include "exo_common.h"
/**
 * @brief Ahw_io mapping with the Ahw_io_lookup_table
 */
extern ahw_io_map *ahw_io_lookup_tble[MAX_AH_INST_ID];

/**
 * @brief  Ahw Instance handle pointer.
 *
 */
extern void *intf_inst_hdle_ptr[MAX_IO_INST_ID];

/**
 * @brief Wrapper API to initializes the mt28ew128_ memory chip
 *
 */
hal_ret_sts	ahw_al_vdp_mt28ew128_init(ahw_al_ext_mem_hdl *al_em_h)
{
    hal_ret_sts sts = HAL_SCS;
    mt28ew128_mem_t* vdh_mem = (mt28ew128_mem_t*)os_malloc(sizeof(mt28ew128_mem_t));
    vdh_mem->io_intf_hdle = intf_inst_hdle_ptr[ahw_io_lookup_tble[al_em_h->ahw_gen_info.ahw_inst_id]->io_instance_id];
    /* update io interface & vdp handle */
    al_em_h->ahw_gen_info.vdp_inst_hdle = (void*)vdh_mem;
    al_em_h->ahw_gen_info.io_intf_hdle=vdh_mem->io_intf_hdle;
    al_em_h->erase_mode = ERASE_BY_SECTOR;
    al_em_h->flash_size = MT28EW128_MEMORY_FLASH_SIZE;
    if(HAL_SCS == (hal_ret_sts)mt28ew128_init(vdh_mem))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }

#ifdef LINUX_TEMP_PORT
    sts = HAL_SCS;
#endif
    return sts;
}

/**
 * @brief Wrapper API to de initializes mt28ew128_ memory chip
 */
hal_ret_sts	ahw_al_vdp_mt28ew128_deinit(ahw_al_ext_mem_hdl *al_em_h)
{
    hal_ret_sts sts = HAL_SCS;

    // TODO : De initialize needs to be done

#ifdef LINUX_TEMP_PORT
    sts = HAL_SCS;
#endif
    return sts;
}

/**
 * @brief Wrapper API to write data into mt28ew128_ memory chip
 *
 */
hal_ret_sts ahw_vdp_mt28ew128_write(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
    hal_ret_sts sts;
    mt28ew128_mem_t vdh_mem;
    vdh_mem.io_intf_hdle = al_em_h->ahw_gen_info.io_intf_hdle;
        if(HAL_SCS == (hal_ret_sts)mt28ew128_write(&vdh_mem,address,data,size))
        {
            sts = HAL_SCS;
        }
        else
        {
            sts = HAL_AH_DRIVER_ERR;
        }
    return sts;
}


/**
 * @brief Wrapper API to read data from mt28ew128_ memory chip
 *
 */
hal_ret_sts ahw_vdp_mt28ew128_read(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
    hal_ret_sts sts;
    mt28ew128_mem_t vdh_mem;
    vdh_mem.io_intf_hdle = al_em_h->ahw_gen_info.io_intf_hdle;
    if(HAL_SCS == (hal_ret_sts)mt28ew128_read(&vdh_mem,address,data,size))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}

/**
 * @brief Wrapper API to erase memory of mt28ew128_ memory chip
 *
 */
hal_ret_sts ahw_vdp_mt28ew128_erase_chip(ahw_al_ext_mem_hdl *al_em_h)
{
    hal_ret_sts sts;
    mt28ew128_mem_t vdh_mem;
    vdh_mem.io_intf_hdle = al_em_h->ahw_gen_info.io_intf_hdle;
    if(HAL_SCS == (hal_ret_sts)mt28ew128_erase_chip(&vdh_mem))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}

/**
 * @brief Wrapper API to erase memory of mt28ew128_ memory chip
 *
 */
hal_ret_sts ahw_vdp_mt28ew128_erase(ahw_al_ext_mem_hdl *al_em_h, uint32_t start_address, uint32_t data_len)
{
    hal_ret_sts sts;
    mt28ew128_mem_t vdh_mem;
    vdh_mem.io_intf_hdle = al_em_h->ahw_gen_info.io_intf_hdle;
    if(HAL_SCS == (hal_ret_sts)mt28ew128_erase(&vdh_mem, start_address,data_len))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}

hal_ret_sts ahw_vdp_mt28ew128_erase_and_write(ahw_al_ext_mem_hdl *al_em_h, uint32_t w_address, uint8_t *data, uint32_t data_len)
{
    hal_ret_sts sts;
    mt28ew128_mem_t vdh_mem;
    vdh_mem.io_intf_hdle = al_em_h->ahw_gen_info.io_intf_hdle;
    if(HAL_SCS == (hal_ret_sts)mt28ew128_erase_and_write(&vdh_mem,w_address,data,data_len))
    {
        sts = HAL_SCS;
    }
    else
    {
        sts = HAL_AH_DRIVER_ERR;
    }
    return sts;
}

hal_ret_sts	ahw_al_vdp_mt28ew128_reinit(ahw_al_ext_mem_hdl *al_em_h)
{
	hal_ret_sts sts = 0;
	mt28ew128_mem_t vdh_mem;
	vdh_mem.io_intf_hdle = al_em_h->ahw_gen_info.io_intf_hdle;
	if(HAL_SCS == (hal_ret_sts)mt28ew128_init(&vdh_mem))
	    {
	        sts = HAL_SCS;
	    }
	    else
	    {
	        sts = HAL_AH_DRIVER_ERR;
	    }
	    return sts;
}
