/* $Id: xflash_cfi.h,v 1.6 2002/08/13 21:14:26 robertm Exp $ */
/******************************************************************************
*
*       XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
*       AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
*       SOLUTIONS FOR XILINX DEVICES.  BY PROVIDING THIS DESIGN, CODE,
*       OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
*       APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
*       THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
*       AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
*       FOR YOUR IMPLEMENTATION.  XILINX EXPRESSLY DISCLAIMS ANY
*       WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
*       IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
*       REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
*       INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*       FOR A PARTICULAR PURPOSE.
*
*       (c) Copyright 2002 Xilinx Inc.
*       All rights reserved.
*
******************************************************************************/
/*****************************************************************************/
/**
*
* @file xflash_cfi.h
*
* This is a helper component for XFlash. It contains methods used to extract
* and interpret Common Flash Interface (CFI) from a flash memory part that
* supports the CFI query command.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who  Date     Changes
* ----- ---- -------- -----------------------------------------------
* 1.00a rmm  07/16/01 First release
* </pre>
*
***************************************************************************/

#ifndef XFLASH_CFI_H /* prevent circular inclusions */
#define XFLASH_CFI_H /* by using protection macros */

/***************************** Include Files *********************************/
#include "xbasic_types.h"
#include "xstatus.h"
#include "xflash.h"
#include "xflash_geometry.h"
#include "xflash_properties.h"

/************************** Constant Definitions *****************************/

/*
 * Vendor command set codes
 * Refer to industry document "CFI publication 100" for the latest list
 */
#define XFL_CMDSET_INTEL_STANDARD       3
#define XFL_CMDSET_INTEL_EXTENDED       1    /* Also includes Sharp */
#define XFL_CMDSET_AMD_STANDARD         2    /* Also includes Fujitsu */
#define XFL_CMDSET_AMD_EXTENDED         4    /* Also includes Fujitsu */
#define XFL_CMDSET_MITSUBISHI_STANDARD  256
#define XFL_CMDSET_MITSUBISHI_EXTENDED  257


/**************************** Type Definitions *******************************/


/***************** Macros (Inline Functions) Definitions *********************/

/*
 * XFL_CFI_ macros are used by the base and derived components to access data
 * from the flash device when it is in CFI query mode. The query data begins
 * at the base address of the part and is interleaved based on the part
 * layout:
 *         XFL_LAYOUT_X16_X16_X1   Interleave=2
 *         XFL_LAYOUT_X16_X16_X2   Interleave=4
 */

/*****************************************************************************/
/**
 *
 * Moves the CFI data pointer to a physical address that corresponds to a
 * specific CFI byte offset.
 *
 * @param   Ptr is the pointer to modify. Can be of any type
 * @param   BaseAddr is the base address of flash part
 * @param   Interleave is the byte interleaving (based on part layout)
 * @param   ByteAddr is the byte offset within CFI data to read
 *
 * @return
 *
 * The Ptr argument is set to point at the the CFI byte specified by the
 * ByteAddr parameter.
 *
 */
#define XFL_CFI_POSITION_PTR(Ptr, BaseAddr, Interleave, ByteAddr) \
(                                                                 \
    Ptr = (void*)((Xuint32)BaseAddr + ((Xuint32)Interleave *      \
                                         (Xuint32)ByteAddr))      \
)

/*****************************************************************************/
/**
 *
 * Reads 8-bits of data from the CFI data location into a local variable.
 *
 * @param   Ptr is the pointer to read. Can be a pointer to any type.
 * @param   Interleave is the byte interleaving (based on part layout)
 *
 * @return
 *
 * The byte at Ptr adjusted for the interleave factor.
 *
 */
#define XFL_CFI_READ8(Ptr, Interleave) \
    READ_FLASH_8((Xuint32)Ptr + Interleave - 1)

/*****************************************************************************/
/**
 *
 * Reads 16-bits of data from the CFI data location into a local variable.
 *
 * @param   Ptr is the pointer to read. Can be a pointer to any type.
 * @param   Interleave is the byte interleaving (based on part layout)
 * @param   Data is the 16-bit storage location for the data to be read.
 *
 * @return
 *
 * The 16-bit value at Ptr adjusted for the interleave factor.
 *
 */
#define XFL_CFI_READ16(Ptr, Interleave, Data)  \
{                                              \
    (Data) = (Xuint16)READ_FLASH_8((Xuint8*)(Ptr) + ((Interleave) * 2) - 1);  \
    (Data) <<= 8;                                                             \
    (Data) |= (Xuint16)READ_FLASH_8((Xuint8*)(Ptr) + (Interleave) - 1);       \
}

/*****************************************************************************/
/**
 *
 * Advances the CFI pointer to the next byte
 *
 * @param   Ptr is the pointer to advance. Can be a pointer to any type.
 * @param   Interleave is the byte interleaving (based on part layout)
 *
 * @return
 *
 * Adjusted Ptr.
 *
 */
#define XFL_CFI_ADVANCE_PTR8(Ptr, Interleave) \
    (Ptr = (void*)((Xuint32)Ptr + (Interleave)))

/*****************************************************************************/
/**
 *
 * Advances the CFI pointer to the next 16-bit quantity.
 *
 * @param   Ptr is the pointer to advance. Can be a pointer to any type.
 * @param   Interleave is the byte interleaving (based on part layout)
 *
 * @return
 *
 * Adjusted Ptr.
 *
 */
#define XFL_CFI_ADVANCE_PTR16(Ptr, Interleave) \
    (Ptr = (void*)((Xuint32)Ptr + ((Interleave) << 1)))


/************************** Function Prototypes ******************************/

XStatus XFlashCFI_ReadCommon(XFlashGeometry *GeometryPtr,
                             XFlashProperties *PropertiesPtr);

#endif            /* end of protection macro */
