/*
 * @file exo_ahw_al_ext_flash_wrapper.c
 *
 * @brief This file has mapping functions which resolves the
 * instance Id and invokes the vendor driver API for external flash memory chip based on the part number
 *
 * @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_al_ext_flash_wrapper.h"
#include "exo_ahw_vdp_mx25l512.h"
#include "exo_ahw_vdp_mt25ql512.h"
#include "exo_ahw_vdp_mt28ew128.h"
#include "exo_ahw_vdp_eeprom_m24c16.h"


#ifndef LINUX_TEMP_PORT

/**
 * @brief Mapping API to initializes the external memory chip
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_init(ahw_al_ext_mem_hdl *al_em_h)
{
	hal_ret_sts sts;
	switch(al_em_h->ahw_gen_info.ahw_inst_id)
	{
	case EXT_MEM_MT28EW128_FMC:
		sts = ahw_al_vdp_mt28ew128_init(al_em_h);
		break;
	case EXT_MEM_MX25L512_QSPI:
		sts = ahw_al_vdp_mx25l512_init(al_em_h);
		break;
	case EXT_MEM_MT25QL512_QSPI:
		sts = ahw_al_vdp_mt25ql512_init(al_em_h);
		break;
	case EXT_MEM_M24C16_EEPROM_1:
	case EXT_MEM_M24C16_EEPROM_2:
		sts = ahw_al_vdp_m24c16_init(al_em_h);
		break;
	case EXT_MEM_S25HL512T_QSPI:
		sts = HAL_MAX_ERR;
		break;
	default:
		sts = HAL_AH_INVLD_INST_ID;
	}
	return sts;
}


/**
 * @brief Mapping API to write data into memory
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_write(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
	hal_ret_sts sts = HAL_SCS;
	switch(al_em_h->ahw_gen_info.ahw_inst_id)
	{
	case EXT_MEM_MT28EW128_FMC:
		sts = ahw_vdp_mt28ew128_write(al_em_h, address, data, size);
		break;
	case EXT_MEM_MX25L512_QSPI:
		sts = ahw_vdp_mx25l512_write(al_em_h, address, data, size);
		break;
	case EXT_MEM_MT25QL512_QSPI:
		sts = ahw_vdp_mt25ql512_write(al_em_h, address, data, size);
		break;
	case EXT_MEM_S25HL512T_QSPI:
		sts = HAL_IO_VDP_ERR;
		break;
	case EXT_MEM_M24C16_EEPROM_1:
	case EXT_MEM_M24C16_EEPROM_2:
                sts = ahw_vdp_m24c16_eeprom_write(al_em_h, address, data, size);
		break;

	default:
		sts = HAL_AH_INVLD_INST_ID;
	}



	return sts;
}

/**
 * @brief Mapping API to write data into memory
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_erase_and_write(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
	hal_ret_sts sts = HAL_SCS;
	switch(al_em_h->ahw_gen_info.ahw_inst_id)
	{
	case EXT_MEM_MT28EW128_FMC:
		sts = ahw_vdp_mt28ew128_erase_and_write(al_em_h, address, data, size);
		break;
	case EXT_MEM_MX25L512_QSPI:
	case EXT_MEM_MT25QL512_QSPI:
	case EXT_MEM_S25HL512T_QSPI:
		sts = HAL_IO_VDP_ERR;
		break;
	default:
		sts = HAL_AH_INVLD_INST_ID;
	}
	return sts;
}
/**
 * @brief Mapping API to read data from specific memory
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_read(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
	hal_ret_sts sts=HAL_SCS;
	switch(al_em_h->ahw_gen_info.ahw_inst_id)
	{
	case EXT_MEM_MT28EW128_FMC:
		sts = ahw_vdp_mt28ew128_read(al_em_h, address, data, size);
		break;
	case EXT_MEM_MX25L512_QSPI:
		sts = ahw_vdp_mx25l512_read(al_em_h, address, data, size);
		break;
	case EXT_MEM_MT25QL512_QSPI:
		sts = ahw_vdp_mt25ql512_read(al_em_h, address, data, size);
		break;
	case EXT_MEM_S25HL512T_QSPI:
		break;
	case EXT_MEM_M24C16_EEPROM_1:
	case EXT_MEM_M24C16_EEPROM_2:
                sts = ahw_vdp_m24c16_eeprom_read(al_em_h, address, data, size);
		break;
	default:
		sts = HAL_AH_INVLD_INST_ID;
	}
	return sts;
}


/**
 * @brief Mapping API to erase memory
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_erase(ahw_al_ext_mem_hdl *al_em_h, uint32_t start_address, uint32_t end_address)
{
	hal_ret_sts sts = HAL_SCS;
	switch(al_em_h->ahw_gen_info.ahw_inst_id)
	{
	case EXT_MEM_MT28EW128_FMC:
		sts = ahw_vdp_mt28ew128_erase(al_em_h,start_address,end_address-start_address);
		break;
	case EXT_MEM_MX25L512_QSPI:
		sts = ahw_vdp_mx25l512_erase(al_em_h, start_address, end_address);
		break;
	case EXT_MEM_MT25QL512_QSPI:
		sts = ahw_vdp_mt25ql512_erase(al_em_h, start_address, end_address);
		break;
	case EXT_MEM_S25HL512T_QSPI:
		sts = HAL_IO_VDP_ERR;
		break;
	case EXT_MEM_M24C16_EEPROM_1:
	case EXT_MEM_M24C16_EEPROM_2:
                sts = ahw_vdp_m24c16_eeprom_erase_full_chip(al_em_h);
		break;
	default:
		sts = HAL_AH_INVLD_INST_ID;
	}
	return sts;
}

/**
 * @brief Mapping API to Re-initializes the external memory chip
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_reinit(ahw_al_ext_mem_hdl *al_em_h)
{
	hal_ret_sts sts;
	switch(al_em_h->ahw_gen_info.ahw_inst_id)
	{
	case EXT_MEM_MT28EW128_FMC:
		sts = ahw_al_vdp_mt28ew128_reinit(al_em_h);
		break;
	case EXT_MEM_MT25QL512_QSPI:
		sts = ahw_al_vdp_mt25ql512_reinit(al_em_h);
		break;
/*	case EXT_MEM_MX25L512_QSPI:
		sts = ahw_al_vdp_mx25l512_init(al_em_h);
		break;
	case EXT_MEM_S25HL512T_QSPI:
		sts = HAL_MAX_ERR;
		break;*/
	default:
		sts = HAL_AH_INVLD_INST_ID;
	}
	return sts;
}


/**
 * @brief Mapping API to enable memory mapped mode
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_memmap_enable(ahw_al_ext_mem_hdl *al_em_h)
{
	hal_ret_sts sts = HAL_SCS;
	switch(al_em_h->ahw_gen_info.ahw_inst_id)
	{
	case EXT_MEM_MX25L512_QSPI:
		sts = ahw_vdp_mx25l512_memmap_enable(al_em_h);
		break;
	case EXT_MEM_MT25QL512_QSPI:
		sts = ahw_vdp_mt25ql512_memmap_enable(al_em_h);
		break;
	case EXT_MEM_MT28EW128_FMC:
	case EXT_MEM_S25HL512T_QSPI:
		sts = HAL_IO_VDP_ERR;
		break;
	default:
		sts = HAL_AH_INVLD_INST_ID;
	}
	return sts;
}

#else




/**
 * @brief Mapping API to initializes the external memory chip
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_init(ahw_al_ext_mem_hdl *al_em_h)
{
	return 0;
}


/**
 * @brief Mapping API to write data into memory
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_write(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
	return 0;

}


/**
 * @brief Mapping API to read data from specific memory
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_read(ahw_al_ext_mem_hdl *al_em_h, uint32_t address, uint8_t *data, uint32_t size)
{
	return 0;

}


/**
 * @brief Mapping API to erase memory
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_erase(ahw_al_ext_mem_hdl *al_em_h, uint32_t start_address, uint32_t end_address)
{
	return 0;

}

/**
 * @brief Mapping API to enable memory mapped mode
 *
 */
hal_ret_sts	ahw_al_wrpr_ext_flash_memmap_enable(ahw_al_ext_mem_hdl *al_em_h)
{
	return 0;
}



#endif

