embeddedsw/lib/sw_services/xilflash/examples/xilflash_protection_example.c
Jagannadha Sutradharudu Teki 2c8f92039d embeddesw: Add initial code support
Added initial support Xilinx Embedded Software.

Signed-off-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
2014-06-24 16:45:01 +05:30

251 lines
7.6 KiB
C
Executable file

/******************************************************************************
*
* Copyright (C) 2007 - 2014 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* XILINX CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*****************************************************************************/
/**
* @file xilflash_protection_example.c
*
*
* This file contains a design example using the Generic Flash Library.
* This example tests the Lock and UnLock features of the Flash Device.
*
* A block is Locked and an attempt is made to erase a Page in that block. This
* should return an error. The block is then unlocked and an attempt is made to
* erase a page in that block which should succeed. A page of data is written,
* read back and compared with the data written for correctness.
*
* @note This example has been tested with an Intel CFI compliant
* Flash device.
* This example has not been tested with an AMD CFI compliant Flash
* device. The AMD CFI compliant Flash devices require the user to
* apply a 12V DC voltage on the RP pin while performing the Lock
* and Unlock operations.
*
*<pre>
* MODIFICATION HISTORY:
*
* Ver Who Date Changes
* ----- ---- -------- ---------------------------------------------------------
* 1.01a mta 10/09/07 First release
* 1.01a sdm 02/24/09 Updated the notes with information about using the example
* with AMD CFI compliant Flash devices
* 2.00a ktn 12/04/09 Updated to use the HAL processor APIs/macros
* 3.00a sdm 03/03/11 Updated to pass BaseAddress and Flash Width to _Initialize
* API, as required by the new version of the library
*</pre>
******************************************************************************/
/***************************** Include Files *********************************/
#include <xilflash.h>
#include <xil_types.h>
/************************** Constant Definitions *****************************/
/*
* The following constants define the baseaddress and width the flash memory.
* These constants map to the XPAR parameters created in the xparameters.h file.
* They are defined here such that a user can easily change all the needed
* parameters in one place.
*/
#define FLASH_BASE_ADDRESS XPAR_EMC_0_MEM0_BASEADDR
/*
* The following constant defines the total byte width of the flash memory. The
* user needs to update this width based on the flash width in the design/board.
* The total flash width on some of the Xilinx boards is listed below.
* -------------------------------
* Board Width
* -------------------------------
* ML403 4 (32 bit)
* ML5xx 2 (16 bit)
* Spartan3S 1600E 2 (16 bit)
* Spartan-3A DSP 2 (16 bit)
* Spartan-3A 2 (16 bit)
* Spartan-3AN 2 (16 bit)
* ML605 2 (16 bit)
* SP605 2 (16 bit)
* SP601 1 (8 bit)
*/
#define FLASH_MEM_WIDTH 2
#define FLASH_TEST_SIZE 256
#define START_ADDRESS 0x060000
#define BLOCK_OFFSET_ADDR 0x068000
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
int FlashProtectionExample(void);
/************************** Variable Definitions *****************************/
XFlash FlashInstance; /* XFlash Instance. */
/*
* Buffers used during read and write transactions.
*/
u8 ReadBuffer[FLASH_TEST_SIZE]; /* Buffer used to store the data read. */
u8 WriteBuffer[FLASH_TEST_SIZE]; /* Write buffer. */
/************************** Function Definitions ******************************/
/*****************************************************************************/
/**
*
* Main function to execute the Flash protection example.
*
* @param None
*
* @return XST_SUCCESS if successful else XST_FAILURE.
*
* @note None.
*
******************************************************************************/
int main(void)
{
int Status;
Status = FlashProtectionExample();
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function verifies the locking and unlocking features of the Flash device.
*
* @param None
*
* @return XST_SUCCESS if successful else XST_FAILURE.
*
* @note None.
*
******************************************************************************/
int FlashProtectionExample(void)
{
int Status;
u32 Index;
/*
* Initialize the Flash Library.
*/
Status = XFlash_Initialize(&FlashInstance, FLASH_BASE_ADDRESS,
FLASH_MEM_WIDTH, 0);
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Reset the Flash Device. This clears the Status registers and puts
* the device in Read mode.
*/
Status = XFlash_Reset(&FlashInstance);
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Lock the Block.
*/
Status = XFlash_Lock(&FlashInstance, BLOCK_OFFSET_ADDR, 0);
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Perform the Erase operation. This should fail as the block is locked.
*/
Status = XFlash_Erase(&FlashInstance, START_ADDRESS, FLASH_TEST_SIZE);
if(Status == XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Unlock the Block.
*/
Status = XFlash_Unlock(&FlashInstance, BLOCK_OFFSET_ADDR, 0);
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Perform the Erase operation. This should succeed as the block is
* unlocked.
*/
Status = XFlash_Erase(&FlashInstance, START_ADDRESS, FLASH_TEST_SIZE);
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Prepare the write buffer. Fill in the data need to be written into
* Flash Device.
*/
for(Index = 0; Index < FLASH_TEST_SIZE; Index++) {
WriteBuffer[Index] = (u8)Index;
}
/*
* Perform the Write operation.
*/
Status = XFlash_Write(&FlashInstance, START_ADDRESS, FLASH_TEST_SIZE,
WriteBuffer);
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Perform the read operation.
*/
Status = XFlash_Read(&FlashInstance, START_ADDRESS, FLASH_TEST_SIZE,
ReadBuffer);
if(Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Compare the data read against the data Written.
*/
for(Index = 0; Index < FLASH_TEST_SIZE; Index++) {
if(ReadBuffer[Index] != (u8)Index) {
return XST_FAILURE;
}
}
return XST_SUCCESS;
}