/**
 * \file   osdrv_spiflash.h
 *
 * \brief This file demonstrates usage of StarterWare coding guidelines in a
 *  header file.
*/

/*
 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
 */
 
#ifndef _OSDRV_SPIFLASH_H
#define _OSDRV_SPIFLASH_H
/* ========================================================================== */
/*                             Include Files                                  */
/* ========================================================================== */
#include "plat_mcspi.h"

#ifdef __cplusplus
extern "C" {
#endif

/* ========================================================================== */
/*                           Macros & Typedefs                                */
/* ========================================================================== */
/** \brief */
#define SPIFLASH_SIZE             0x00800000
/** \brief */
#define SPIFLASH_BASE             0x00000000
/** \brief */
#define SPIFLASH_PAGESIZE         256
/** \brief */
#define SPIFLASH_SECTORSIZE       4096

/** \brief */
#define SPIFLASH_PAGEMASK         0xffffffc0
/** \brief */
#define SPIFLASH_MANUF_ID	      0xEF

/** \brief */
#define MFID_DEVID_LEN		6
/** \brief */
#define SPI_WPEN_LEN		1
/** \brief */
#define SPI_STATUS_LEN		2
/** \brief */
#define SPI_ERASE_LEN		4
/** \brief */
#define TX_EMPTY_FLAG		0x02
/** \brief */
#define RX_FULL_FLAG		0x01
/** \brief */
#define	SHIFT_BYTE			8
/** \brief */
#define	SHIFT_WORD			16
    
/* SPI FLASH Commands */
/** \brief */
#define SPIFLASH_CMD_WRSR         0x01
/** \brief */
#define SPIFLASH_CMD_WRITE        0x02
/** \brief */
#define SPIFLASH_CMD_READ         0x03
/** \brief */
#define SPIFLASH_CMD_WRDI         0x04
/** \brief */
#define SPIFLASH_CMD_RDSR         0x05
/** \brief */
#define SPIFLASH_CMD_WREN         0x06
/** \brief */
#define SPIFLASH_CMD_READID		  0x90
/** \brief */
#define SPIFLASH_CMD_ERASE		  0xD8
/** \brief */
#define SPIFLASH_CMD_SCTR_ERASE	  0x20

/** \brief */
#define FLASH_STATUS		0x01
/** \brief */
#define FLASH_WEL_STATUS		0x02


/** \brief Chip select */
#define SPI_FLASH_CS 0

/** \brief Clock Divide Value For Clock 24MHZ*/
#define SPI_FLASH_CD 2

/** \brief DIRECTION PINS FOR SPI*/
#define SPI_FLASH_DIR 0

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

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


/* ========================================================================== */
/*                          Function Declarations                             */
/* ========================================================================== */


/**
 * \brief   Intialize SPI flash 
 *
 * \param   instance    McSPI instance on which SPI flash is connected to
 * \param   chNum       McSPI channel number 
 *
 * \return  0 on success and -1 on failure
 *
 **/
int SpiFlashInit(int instance,unsigned int chNum);
/**
 * \brief   Routine to read data from SPI flash
 *
 * \param   offset      SPI flash offset from where the data to be written
 * \param   noOfbytes   Number of bytes to be erased ( will be rounded to 256 bytes)
 * \param   rxBuff      Buffer to store data read from SPI Flash
 *
 * \return  0 on success and -1 on failure
 *
 **/
int SpiFlashRead(unsigned int offset,unsigned int noOfbytes,unsigned char* rxBuff);
/**
 * \brief   Write data to SPI flash
 *
 * \param   offset      SPI flash offset from where the data to be written
 * \param   noOfbytes   Number of bytes to be erased ( will be rounded to 256 bytes)
 * \param   txBuff      Bufer holding data to be written
 * \param   erase_flag  Flasg specifiying whether the data need to erqsed before write
 *
 * \return  0 on success and -1 on failure
 *
 **/
int SpiFlashWrite(unsigned int offset,unsigned int noOfbytes,unsigned char* txBuff,unsigned char erase_flag);
/**
 * \brief   Erase data from SPI flash
 *
 * \param   offset          SPI flash offset from where the erase to be done  
 * \param   noOfbytes       Number of bytes to be erased ( will be rounded to 4 KB)
 *
 * \return  0 on success and -1 on failure
 *
 **/
int SpiFlashErase(unsigned int offset,unsigned int noOfbytes);
/**
 * \brief   Routine to read the Manufacturer ID and device ID of the initialized 
 *          SPI Flash 
 * 
 *          This routine sends the readid command to flash and reads both Ids and 
 *          verifies the Manufacturer ID.
 *
 *
 * \return  ID on success and 0 otherwise.
 *
 **/
int SPIFlashReadID();

/**
 * \brief   Routine to get the SPI instance used with the SPI Flash
 *
 *
 * \return  SPI instance number
 *
 **/
int SPIFlashGetInstance();


#ifdef __cplusplus
}
#endif
#endif  //_OSDRV_SPIFLASH_H
