/**
 * \file   osdrv_pruss.h
 *
 *  \brief This file contains the function prototypes for the device
 *              abstraction layer for PRUSS. It also contains some
 *              related macro definitions and some files to be included.
 *
*/
/*
 * Copyright (c) 2012 Texas Instruments Incorporated ALL RIGHTS RESERVED
 */

#ifndef _OSDRV_PRUSS_H
#define _OSDRV_PRUSS_H

#ifdef __cplusplus
extern "C" {
#endif

/* ========================================================================== */
/*                             Include Files                                  */
/* ========================================================================== */
#include "pruss.h"
#include "osdrv_prussdefs.h"
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/BIOS.h>

/* ========================================================================== */
/*                           Macros & Typedefs                                */
/* ========================================================================== */
/** \brief PRU 0 firmware binary file location in SPI flash - at an offset 
   of 480 KB from starting location */
#define SPI_APPL_BIN_OFFSET		0x20000
/** \brief PRU 0 firmware binary file location in SPI flash - at an offset 
   of 480 KB from starting location */
#define SPI_PRU0_BINARY_OFFSET		(SPI_APPL_BIN_OFFSET + 0x78000)
/** \brief PRU 1 firmware binary file location in SPI flash - at an offset 
   of 9KB from PRU0 binary location */
#define SPI_PRU1_BINARY_OFFSET		(SPI_PRU0_BINARY_OFFSET + 0x4000)

/** \brief Magic number used in application with PRU firmware */
#define MAGIC_NUM_PRU_BIN			0xEE3366AA
/** \brief Magic number used in application   */
#define MAGIC_NUM_APP_BIN	        0xEE3355BB

/* ========================================================================== */
/*                         Structures and Enums                               */
/* ========================================================================== */

/**
 *  \brief IRQ Function map structure 
 *
 */
typedef struct __pruss_irq_fn_map
{
	
	PRUSSDRV_IRQ_HANDLER irqHandler;
	/**< Interrupt handler*/
	Semaphore_Handle semHandle;
    /**< Binary semaphore to wait on */
}pruss_irq_fn_map;
/**
 *  \brief PRU Firmware binary header
 *
 */
typedef struct _pru_firmware_bin_header_
{
	unsigned int magic_number;
    /**< Magic number used to identify binary */
	unsigned int image_size;
    /**< Image size */
	unsigned int load_addr;
    /**< Address to which this binary need to be loaded */
	unsigned int run_addr;
    /**< Address at which the execution should start */
}PRU_FIRMWARE_BIN_HEADER;


/* ========================================================================== */
/*                            Global Variables Declarations                   */
/* ========================================================================== */

/* ========================================================================== */
/*                          Function Declarations                             */
/* ========================================================================== */
/**
 * \brief    Resets the given PRU: \n
 *
 * \param    pruNum          PRU instance number[0 or 1].
 *
 * \return   0 in case of successful reset, -1 otherwise.
 */
int PRUSSDRVPruReset(unsigned int pruNum);
/**
 * \brief    Disables the given PRU: \n
 *
 * \param    pruNum          PRU instance number[0 or 1].
 *
 * \return   0 in case of successful disable, -1 otherwise.
 */
int PRUSSDRVPruDisable(unsigned char pruNum);
/**
 * \brief    Enables the given PRU: \n
 *
 * \param    pruNum          PRU instance number[0 or 1].
 *
 * \return   0 in case of successful enable, -1 otherwise.
 */
int PRUSSDRVPruEnable(unsigned char pruNum);
/**
 *
 *  \brief   This function writes the given data to PRU memory
 *
 * \param     pruMem         PRU Memory Macro [DATARAM0_PHYS_BASE
 * \param     wordoffset     Offset at which the write will happen.
 * \param     source_mem     Source memory[ Array of integers ]
 * \param     bytelength     Total number of bytes to be writen
 *
 * pruMem can have values
 *		PRUSS0_PRU0_DATARAM\n
 *		PRUSS0_PRU1_DATARAM\n
 *		PRUSS0_PRU0_IRAM\n
 *		PRUSS0_PRU1_IRAM 
 * 		PRUSS0_SHARED_DATARAM (Incase of AM33XX)
 * \return                   0 in case of successful transition, -1 otherwise.
 *            
 */
int PRUSSDRVPruWriteMemory(unsigned int pruMem,
                           unsigned int wordoffset,
                           unsigned int *source_mem,
                           unsigned int bytelength);
/**
 * \brief   This function Executes the program in the specified PRU \n
 *
 * \param   prunum    PRU instance number.\n
 * \param   filename  Program file to be executed.\n
 *
 * \return  0 in case of successful transition, -1 otherwise. \n
 */
int PRUSSDRVPruExecProgram(int prunum, char *filename);						   
/**
 * \brief   Routine to 
 *
 * \param   pru_instance        PRU instance number
 * \param   spi_offset          Offset on SPI flash to where this need to be flashed
 *
 *
 * \return  None.
 *
 **/
signed char PRUSSDRVLoadBinary (unsigned char pru_instance,unsigned int spi_offset);
/**
 * \brief  This function Generates an INTC event \n
 *
 * \param   eventnum   The INTC Event number.\n
 *
 * \return  0 in case of successful transition, -1 otherwise. \n
 */
int PRUSSDRVPruSendEvent(unsigned int eventnum);						   
/**
 * \brief  This function clears an INTC event \n
 *
 * \param   eventnum   The INTC Event number.\n
 *
 * \return  0 in case of successful transition, -1 otherwise. \n
 */
int PRUSSDRVPruClearEvent(unsigned int eventnum);
/**
 * \brief  This function waits for a Syatem event to happen \n
 *
 * \param   pruEvtoutNum   The AINTC Event number.\n
 *
 * \return  0 in case of successful transition, -1 otherwise. \n
 */
int PRUSSDRVPruWaitEvent(unsigned int pruEvtoutNum);
/**
 * \brief  This function does Interrupt-Channel-host mapping.
 *
 * \param   prussintc_init_data   The pointer to structure containing mapping information.
 *
 * \return  0 in case of successful transition, -1 otherwise. \n
 */
int PRUSSDRVPruIntcInit(tpruss_intc_initdata * prussintc_init_data);
/**
 * \brief  This function generates and INTC event, waits for AINTC event and 
 *		   clears an INTC event \n
 *
 * \param   sendEventNum   Event number.\n
 * \param   pruEvtoutNum   PRU Event number.\n
 * \param   ackEventNum    Acknowlodegement event number.\n
 *
 * \return  0 in case of successful transition, -1 otherwise. \n
 */
int PRUSSDRVPruSendWaitCearEvent(unsigned int sendEventNum, 
										unsigned int pruEvtoutNum,
										unsigned int ackEventNum);

/**
 * \brief  This function Registers an Interrupt Handler for an ARM Side event.\n
 *
 * \param   pruEvtoutNum   The ARM side event number.\n
 * \param	channel     Flag specifying whether application can wait on this interrupt
 * 						   using PRUSSDRVPruWaitEvent function
 * \param   irqHandler     Pointer to a function which will be called on interrupt.\n
 *
 * \return  0 in case of successful registration, -1 otherwise. \n
 */
int PRUSSDRVRegisterIrqHandler(unsigned int pruEvtoutNum, 
						   unsigned char channel,
						   PRUSSDRV_IRQ_HANDLER irqHandler);
/**
 * \brief  This function sets the buffer pointer for PRU. The contents of this buffer will be \n
 *         loaded to IRAM.\n
 *
 * \param   pruNum     The PRU number.\n
 * \param   buffer	   Pointer to buffer.\n
 * \param   numBytes    Number of bytes in the buffer.\n
 *
 * \return  0 in case of successful transition, -1 otherwise. \n
 */
int PRUSSDRVSetPRUBuffer(unsigned int pruNum, 
						 void *buffer,	
						 unsigned int numBytes);
/**
 * \brief  This function returns the L3 Memeory Start Address.
 *
 * \param  address  Memory to which address to be copied. \n
 *
 * \return 0 in case of success, -1 otherwise. \n 
 */
int PRUSSDRVMapL3Mem(void **address);
/**
 * \brief  This function returns the External memory Start Address.
 *
 * \param  address  Memory to which address to be copied. \n
 *
 * \return 0 in case of success, -1 otherwise. \n 
 */
int PRUSSDRVMapExtMem(void **address);
/**
 * \brief  This function returns the address of PRU components.
 *
 * \param   pru_ram_id   PRU components' Id.
 *
 * \param  address  Memory to which address to be copied. \n
 *
 * \return 0 in case of success, -1 otherwise. \n
 */
int PRUSSDRVMapPruMem(unsigned int pru_ram_id, void **address);
/**
 * \brief  This function returns the base address of peripheral IO modules.
 *
 * \param   per_id   Peripheral Module's Id.
 *
 * \param   address  Memory to which address to be copied. \n
 *
 * \return 0 in case of success, -1 otherwise. \n  
 */
int PRUSSDRVMapPeripheralIO(unsigned int per_id, void **address);
/**
 * \brief   PRUSSDRVFireWallInit 
 *
 *
 * \return  None.
 *
 **/
void PRUSSDRVFireWallInit(void);
/**
 * \brief   PRUSSDRVFireWallHostAccessEnable
 *
 *
 *
 * \return  None.
 *
 **/
void PRUSSDRVFireWallHostAccessEnable(void);
/**
 * \brief   PRUSSDRVFireWallHostAccessDisable 
 *
 *
 * \return  None.
 *
 **/
void PRUSSDRVFireWallHostAccessDisable(void);

#ifdef __cplusplus
}
#endif
#endif  //_OSDRV_PRUSS_H

