/******************************************************************************
 * Copyright (c) 2011-2012 Texas Instruments Incorporated - http://www.ti.com
 *
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions 
 *  are met:
 *
 *    Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the   
 *    distribution.
 *
 *    Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 *****************************************************************************/

#include <cerrno>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "platform.h"
#include "resource_mgr.h"


/*
The inputs to the test application can be controlled 
by the following file. User need to create this file in
pdk_#_##_##_##\packages\ti\platform\lib\platform_utils_test\bin
directory. 

The syntax to the input file is as follows
<input string> = <value 1 => enable, 0 => disable
*/

char    input_file_name[] = "../testconfig/platform_test_input.txt";
#define MAX_LINE_LENGTH 40

/* The input strings for the input config file is given below */
/* Input flags */
#define INIT_PLL1             "init_pll"
#define INIT_DDR3             "init_ddr"
#define INIT_UART             "init_uart"
#define INIT_TCSL             "init_tcsl"
#define INIT_PHY              "init_phy"
#define INIT_ECC              "init_ecc"

#define PRINT_INFO            "print_info"
#define PRINT_CURRENT_CORE_ID "print_current_core_id"
#define PRINT_SWITCH_STATE    "print_switch_state"
#define TEST_EEPROM           "test_eeprom"
#define TEST_NAND             "test_nand"
#define TEST_NOR              "test_nor"
#define TEST_LED              "test_led"
#define TEST_UART             "test_uart"
#define TEST_EXT_MEM          "run_external_memory_test"
#define TEST_INT_MEM          "run_internal_memory_test"

/* Input parameters */
#define INIT_CONFIG_PLL1_PLLM      "init_config_pll1_pllm"
#define INIT_CONFIG_UART_BAUDRATE  "init_config_uart_baudrate"

#define NAND_TEST_BLOCK_NUMBER     "nand_test_block_number"

#define EEPROM_TEST_SLAVE_ADDRESS  "eeprom_test_slave_address"

#define LED_TEST_LOOP_COUNT        "led_test_loop_count"
#define LED_TEST_LOOP_DELAY        "led_test_loop_delay"

#define EXT_MEM_TEST_BASE_ADDR     "ext_mem_test_base_addr"
#define EXT_MEM_TEST_LENGTH        "ext_mem_test_length"

#define INT_MEM_TEST_CORE_ID       "int_mem_test_core_id"

/* The default values for the input strings are given below */
#define DEF_INIT_CONFIG_PLL1_PLLM      0
#define DEF_INIT_CONFIG_UART_BAUDRATE  0

#define DEF_NAND_TEST_BLOCK_NUMBER    512

#define DEF_NOR_TEST_SECTOR_NUMBER    10

#define DEF_EEPROM_TEST_SLAVE_ADDRESS 0x51

#define DEF_LED_TEST_LOOP_COUNT    1
#define DEF_LED_TEST_LOOP_DELAY    3000000

#define DEF_EXT_MEM_TEST_BASE_ADDR 0x80000000
#define DEF_EXT_MEM_TEST_LENGTH    0x1FFFFFFF

#define DEF_INT_MEM_TEST_CORE_ID   1

#undef TEST_READ_IMAGE

#ifdef TEST_READ_IMAGE
#define IMAGE_STORE_ADDR        0x80000000
#define READ_EEPROM_IMAGE       1
#define READ_NAND_OOB_IMAGE     1
#endif

typedef struct test_config {
    uint8_t print_info;
    uint8_t print_current_core_id;
    uint8_t print_switch_state;
    uint8_t test_eeprom;
    uint8_t test_nand;
    uint8_t test_nor;
    uint8_t test_led;
    uint8_t test_uart;
    uint8_t test_external_mem;
    uint8_t test_internal_mem;

    uint32_t init_config_pll1_pllm;
    uint32_t init_config_uart_baudrate;
    uint32_t nand_test_block_number;
    uint32_t nor_test_sector_number;
    uint32_t eeprom_test_slave_address;
    uint32_t led_test_loop_count;
    uint32_t led_test_loop_delay;
    uint32_t ext_mem_test_base_addr;
    uint32_t ext_mem_test_length;
    uint32_t int_mem_test_core_id;
} test_config;

/* OSAL functions for Platform Library */
uint8_t *Osal_platformMalloc (uint32_t num_bytes, uint32_t alignment)
{
	return malloc(num_bytes);
}

void Osal_platformFree (uint8_t *dataPtr, uint32_t num_bytes)
{
    /* Free up the memory */
    if (dataPtr)
    {
        free(dataPtr);
    }
}

void Osal_platformSpiCsEnter(void)
{
    /* Get the hardware semaphore.
     *
     * Acquire Multi core CPPI synchronization lock
     */
    while ((CSL_semAcquireDirect (PLATFORM_SPI_HW_SEM)) == 0);

    return;
}

void Osal_platformSpiCsExit (void)
{
    /* Release the hardware semaphore
     *
     * Release multi-core lock.
     */
    CSL_semReleaseSemaphore (PLATFORM_SPI_HW_SEM);

    return;
}


/******************************************************************************
 * Function:    test_nand  
 ******************************************************************************/
/*static void test_nand(test_config * args)*/
uint8_t test_nand(test_config * args)
{
    uint32_t				offset;
	PLATFORM_DEVICE_info 	*p_device;
	uint8_t					buf_orig[2048];
	uint8_t					buf1[2048]; /*Get a new buffer so we can test writing some values */
	uint8_t					buf2[2048];
	uint8_t					return_code = 1;


	/* Open the device */
    p_device = platform_device_open(PLATFORM_DEVID_MT29F1G08ABCHC, 0);
    if (p_device == NULL) {
	   return_code = 0;
       goto NAND_TEST_DONE;
   }

     /* Read the test block page 0 - save the contents so we can write back */
    platform_blocknpage_to_offset(p_device->handle, &offset, args->nand_test_block_number, 0 );
    if (platform_device_read(p_device->handle, offset, buf_orig, p_device->page_size) != Platform_EOK) {
	   return_code = 0;
       goto NAND_TEST_DONE;
	   
    }

    /* Erase our test block so we can write to it */
    if (platform_device_erase_block(p_device->handle, args->nand_test_block_number) != Platform_EOK) {
	   return_code = 0;
	   goto NAND_TEST_DONE;
    }

    /* Fill in a test pattern */
    memset(buf1, 0x6d, p_device->page_size);

    /* Write the test pattern to the test block */
    platform_blocknpage_to_offset(p_device->handle, &offset, args->nand_test_block_number, 0 );
    if (platform_device_write(p_device->handle, offset, buf1, p_device->page_size) != Platform_EOK) {
	   return_code = 0;
        goto NAND_TEST_DONE;
    }
    platform_delay(1000);

    memset(buf2, 0xff, p_device->page_size);
    platform_blocknpage_to_offset(p_device->handle, &offset, args->nand_test_block_number, 0);
    if (platform_device_read(p_device->handle, offset, buf2, p_device->page_size) != Platform_EOK) {
	   return_code = 0;
	   goto NAND_TEST_DONE;
    }
    platform_delay(1000);

    /* Compare to see if what we wrote is what we read */
    if(memcmp(buf1, buf2, p_device->page_size))
    {
    	return_code = 0;
    } else {
    	return_code = 1;
    }

    /* Restore the original buffer */
    if (platform_device_erase_block(p_device->handle, args->nand_test_block_number) != Platform_EOK) {
    	return_code = 0;
        goto NAND_TEST_DONE;
    }
    platform_blocknpage_to_offset(p_device->handle, &offset, args->nand_test_block_number, 0);
    platform_device_write(p_device->handle, offset, buf_orig, p_device->page_size);

NAND_TEST_DONE:

    platform_device_close(p_device->handle);

    return(return_code);
}


/******************************************************************************
 * Function:    main  
 ******************************************************************************/
//void main (int argc, char *argv[])
uint8_t test_nand_device(void)
{
	platform_init_flags  	init_flags;
	platform_init_config	init_config;
	platform_info        	p_info;
	test_config 		 	args;
	uint8_t			return_code;
	
	/* Set default values */
	memset(&args, 0x01, sizeof(test_config));
	memset(&init_flags, 0x01, sizeof(platform_init_flags));
	args.init_config_pll1_pllm     = DEF_INIT_CONFIG_PLL1_PLLM;
	args.init_config_uart_baudrate = DEF_INIT_CONFIG_UART_BAUDRATE;
	args.nand_test_block_number    = DEF_NAND_TEST_BLOCK_NUMBER;
	args.nor_test_sector_number    = DEF_NOR_TEST_SECTOR_NUMBER;
	args.eeprom_test_slave_address = DEF_EEPROM_TEST_SLAVE_ADDRESS;
	args.led_test_loop_count       = DEF_LED_TEST_LOOP_COUNT;
	args.led_test_loop_delay       = DEF_LED_TEST_LOOP_DELAY;
	args.ext_mem_test_base_addr    = DEF_EXT_MEM_TEST_BASE_ADDR;
	args.ext_mem_test_length       = DEF_EXT_MEM_TEST_LENGTH;
	args.int_mem_test_core_id      = DEF_INT_MEM_TEST_CORE_ID;


	init_config.pllm    = args.init_config_pll1_pllm;


	/* Init platform */
	
	if (platform_init(&init_flags, &init_config) != Platform_EOK) {
		return(0);
	}

/*	platform_get_info(&p_info);*/


	/* Call test_nand() */
	return_code = test_nand(&args);
	return (return_code);




}

