lib/sw_services/xilflash/src/xilflash_amd.c
int XFlashAmd_Unlock(XFlash * InstancePtr, u32 Offset, u32 Bytes)
{
	u8 Mode;
	u8 AddrMul;
	u8 TopBoot = 0;
	u8 MakeUnprotectStep = 0;
	u8 NoOfBlockInGroup;
	volatile u16 StatusReg;
	u16 Region = 0;
	u16 NumAttempt = 0;
	u16 Block = 0;
	u32 BaseAddress;
	u32 Index;
	u32 GroupAddress;
	int Status = (int)XST_FAILURE;
	XFlashGeometry *GeomPtr;
	XFlashVendorData_Amd *DevDataPtr;

	/*
	 * Verify inputs are valid.
	 */
	if(InstancePtr == NULL) {
		return XST_FAILURE;
	}

	DevDataPtr = GET_PARTDATA(InstancePtr);
	GeomPtr = &InstancePtr->Geometry;
	BaseAddress = GeomPtr->BaseAddress;

	if (InstancePtr->Geometry.BootMode == XFL_AMD_TOP_BOOT) {
		TopBoot = 1;
	}

	Mode = (InstancePtr->Geometry.MemoryLayout &
		XFL_LAYOUT_PART_MODE_MASK) >> 8;

	if (Mode == 2) {
		AddrMul = 1;
	}
	else if (Mode == 1) {
		AddrMul = 2;
	}
	else {
		return (XST_FAILURE);
	}


	/*
	 * Protect all groups of block in the device.
	 */
	for (Block = 0; Block < GeomPtr->NumBlocks; Block++) {
		/*
		 * Get the physical address.
		 */
		(void) XFlashGeometry_ToAbsolute(GeomPtr, Region, Block, 0,
						 &GroupAddress);

		if (XFlashAmd_Lock(InstancePtr, GroupAddress, 0) !=
			XST_SUCCESS ) {
			return XST_FAILURE;
		}

		if (TopBoot) {
			if ((Block == 0) || (Block >= 63)) {
				NoOfBlockInGroup = 1;
			}
			else if ((Block == 1) || (Block == 60)) {
				NoOfBlockInGroup = 3;
			}
			else {
				NoOfBlockInGroup = 4;
			}
		}
		else {
			if ((Block <= 7) || (Block == 70)) {
				NoOfBlockInGroup = 1;
			}
			else if ((Block == 8) || (Block == 67)) {
				NoOfBlockInGroup = 3;
			}
			else {
				NoOfBlockInGroup = 4;
			}
		}

		for (Index = 0; Index < NoOfBlockInGroup; Index++) {
			/*
			 * Increment Region/Block.
			 */
			XFL_GEOMETRY_INCREMENT(GeomPtr, Region, Block);
			Block++;
		}
	}

	/*
	 * Setup Phase.
	 */
	GroupAddress = (0x0000 | (AddrMul * XFL_AMD_CHIP_UNPROTECT_ADDR));

	DevDataPtr->WriteFlash(BaseAddress,
				GroupAddress, XFL_AMD_CMD_GROUP_PROTECT1);

	Block = 0;
	Region = 0;
	MakeUnprotectStep = 0;
	while (NumAttempt < 1000) {
		(void) XFlashGeometry_ToAbsolute(GeomPtr, Region, Block, 0,
				 &GroupAddress);

		GroupAddress = (GroupAddress |
				(AddrMul * XFL_AMD_CHIP_UNPROTECT_ADDR));

		if (MakeUnprotectStep == 0) {
			/*
			 * Unprotect phase.
			 */
			 DevDataPtr->WriteFlash(InstancePtr->Geometry.
					BaseAddress,GroupAddress,
					XFL_AMD_CMD_GROUP_PROTECT1);

			 FlashPause(10000);
		}

		/*
		 * Verify Phase.
		 */
		DevDataPtr->WriteFlash(InstancePtr->Geometry.
				BaseAddress,GroupAddress,
				XFL_AMD_CMD_GROUP_PROTECT2);

		FlashPause(4);

		StatusReg = DevDataPtr->GetStatus(InstancePtr->Geometry.
					BaseAddress, GroupAddress);

		if (StatusReg == XFL_AMD_GROUP_UNPROTECTED) {
			if (Block == ((GeomPtr->NumBlocks) - 1)) {
				Status = XST_SUCCESS;
				break;
			}
			else {
				MakeUnprotectStep = 1;
				if (TopBoot) {
					if ((Block == 0) || (Block >= 63)) {
						NoOfBlockInGroup = 1;
					}
					else if ((Block == 1) || (Block == 60))
					{
						NoOfBlockInGroup = 3;
					}
					else {
						NoOfBlockInGroup = 4;
					}
				}
				else {
					if ((Block <= 7) || (Block == 70)) {
						NoOfBlockInGroup = 1;
					}
					else if ((Block == 8) || (Block == 67))
					{
						NoOfBlockInGroup = 3;
					}
					else {
						NoOfBlockInGroup = 4;
					}
				}

				for (Index = 0; Index < NoOfBlockInGroup;
						Index++) {
					/*
					 * Increment Region/Block.
					 */
					XFL_GEOMETRY_INCREMENT(GeomPtr, Region,
					Block);
					Block++;
				}
			}
		}
		else {
			MakeUnprotectStep = 0;
			NumAttempt++;
		}
	}

	/*
	 * Reset the bank(s) so that it returns to the read mode.
	 */
	(void) XFlashAmd_ResetBank(InstancePtr, Offset, Bytes);

	return (Status);
}
/*****************************************************************************/
/**
*
* Unlocks the all blocks in the AMD flash device.
*
* @param	InstancePtr is the pointer to the XFlash instance.
* @param	Offset is the offset into the device(s) address space from which
*		to begin block UnLocking. The first three bytes of every block
*		is reserved for special purpose. The offset should be at least
*		three bytes from start of the block.
* @param	Bytes indicates the number of bytes to UnLock in the Block
*		starting from Offset.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		This API should be called after applying VID (voltage) to the RP
*		pin of flash device. This API is not tested.
*
******************************************************************************/
Unlocks the all blocks in the AMD flash device.
Unlocks the all blocks in the AMD flash device .
int XFlashAmd_Unlock(XFlash * InstancePtr, u32 Offset, u32 Bytes)
{
	u8 Mode;
	u8 AddrMul;
	u8 TopBoot = 0;
	u8 MakeUnprotectStep = 0;
	u8 NoOfBlockInGroup;
	volatile u16 StatusReg;
	u16 Region = 0;
	u16 NumAttempt = 0;
	u16 Block = 0;
	u32 BaseAddress;
	u32 Index;
	u32 GroupAddress;
	int Status = (int)XST_FAILURE;
	XFlashGeometry *GeomPtr;
	XFlashVendorData_Amd *DevDataPtr;
	if(InstancePtr == NULL) {
		return XST_FAILURE;
	}
	DevDataPtr = GET_PARTDATA(InstancePtr);
	GeomPtr = &InstancePtr->Geometry;
	BaseAddress = GeomPtr->BaseAddress;
	if (InstancePtr->Geometry.BootMode == XFL_AMD_TOP_BOOT) {
		TopBoot = 1;
	}
	Mode = (InstancePtr->Geometry.MemoryLayout &
		XFL_LAYOUT_PART_MODE_MASK) >> 8;
	if (Mode == 2) {
		AddrMul = 1;
	}
	else if (Mode == 1) {
		AddrMul = 2;
	}
	else {
		return (XST_FAILURE);
	}
	for (Block = 0; Block < GeomPtr->NumBlocks; Block++) {
		(void) XFlashGeometry_ToAbsolute(GeomPtr, Region, Block, 0,
						 &GroupAddress);
		if (XFlashAmd_Lock(InstancePtr, GroupAddress, 0) !=
			XST_SUCCESS ) {
			return XST_FAILURE;
		}
		if (TopBoot) {
			if ((Block == 0) || (Block >= 63)) {
				NoOfBlockInGroup = 1;
			}
			else if ((Block == 1) || (Block == 60)) {
				NoOfBlockInGroup = 3;
			}
			else {
				NoOfBlockInGroup = 4;
			}
		}
		else {
			if ((Block <= 7) || (Block == 70)) {
				NoOfBlockInGroup = 1;
			}
			else if ((Block == 8) || (Block == 67)) {
				NoOfBlockInGroup = 3;
			}
			else {
				NoOfBlockInGroup = 4;
			}
		}
		for (Index = 0; Index < NoOfBlockInGroup; Index++) {
			XFL_GEOMETRY_INCREMENT(GeomPtr, Region, Block);
			Block++;
		}
	}
	GroupAddress = (0x0000 | (AddrMul * XFL_AMD_CHIP_UNPROTECT_ADDR));
	DevDataPtr->WriteFlash(BaseAddress,
				GroupAddress, XFL_AMD_CMD_GROUP_PROTECT1);
	Block = 0;
	Region = 0;
	MakeUnprotectStep = 0;
	while (NumAttempt < 1000) {
		(void) XFlashGeometry_ToAbsolute(GeomPtr, Region, Block, 0,
				 &GroupAddress);
		GroupAddress = (GroupAddress |
				(AddrMul * XFL_AMD_CHIP_UNPROTECT_ADDR));
		if (MakeUnprotectStep == 0) {
			 DevDataPtr->WriteFlash(InstancePtr->Geometry.
					BaseAddress,GroupAddress,
					XFL_AMD_CMD_GROUP_PROTECT1);
			 FlashPause(10000);
		}
		DevDataPtr->WriteFlash(InstancePtr->Geometry.
				BaseAddress,GroupAddress,
				XFL_AMD_CMD_GROUP_PROTECT2);
		FlashPause(4);
		StatusReg = DevDataPtr->GetStatus(InstancePtr->Geometry.
					BaseAddress, GroupAddress);
		if (StatusReg == XFL_AMD_GROUP_UNPROTECTED) {
			if (Block == ((GeomPtr->NumBlocks) - 1)) {
				Status = XST_SUCCESS;
				break;
			}
			else {
				MakeUnprotectStep = 1;
				if (TopBoot) {
					if ((Block == 0) || (Block >= 63)) {
						NoOfBlockInGroup = 1;
					}
					else if ((Block == 1) || (Block == 60))
					{
						NoOfBlockInGroup = 3;
					}
					else {
						NoOfBlockInGroup = 4;
					}
				}
				else {
					if ((Block <= 7) || (Block == 70)) {
						NoOfBlockInGroup = 1;
					}
					else if ((Block == 8) || (Block == 67))
					{
						NoOfBlockInGroup = 3;
					}
					else {
						NoOfBlockInGroup = 4;
					}
				}
				for (Index = 0; Index < NoOfBlockInGroup;
						Index++) {
					XFL_GEOMETRY_INCREMENT(GeomPtr, Region,
					Block);
					Block++;
				}
			}
		}
		else {
			MakeUnprotectStep = 0;
			NumAttempt++;
		}
	}
	(void) XFlashAmd_ResetBank(InstancePtr, Offset, Bytes);
	return (Status);
}
int XFlashAmd_Unlock ( XFlash * InstancePtr , u32 Offset , u32 Bytes ) { u8 Mode ; u8 AddrMul ; u8 TopBoot = 0 ; u8 MakeUnprotectStep = 0 ; u8 NoOfBlockInGroup ; volatile u16 StatusReg ; u16 Region = 0 ; u16 NumAttempt = 0 ; u16 Block = 0 ; u32 BaseAddress ; u32 Index ; u32 GroupAddress ; int Status = ( int ) XST_FAILURE ; XFlashGeometry * GeomPtr ; XFlashVendorData_Amd * DevDataPtr ; if ( InstancePtr == NULL ) { return XST_FAILURE ; } DevDataPtr = GET_PARTDATA ( InstancePtr ) ; GeomPtr = & InstancePtr -> Geometry ; BaseAddress = GeomPtr -> BaseAddress ; if ( InstancePtr -> Geometry . BootMode == XFL_AMD_TOP_BOOT ) { TopBoot = 1 ; } Mode = ( InstancePtr -> Geometry . MemoryLayout & XFL_LAYOUT_PART_MODE_MASK ) >> 8 ; if ( Mode == 2 ) { AddrMul = 1 ; } else if ( Mode == 1 ) { AddrMul = 2 ; } else { return ( XST_FAILURE ) ; } for ( Block = 0 ; Block < GeomPtr -> NumBlocks ; Block ++ ) { ( void ) XFlashGeometry_ToAbsolute ( GeomPtr , Region , Block , 0 , & GroupAddress ) ; if ( XFlashAmd_Lock ( InstancePtr , GroupAddress , 0 ) != XST_SUCCESS ) { return XST_FAILURE ; } if ( TopBoot ) { if ( ( Block == 0 ) || ( Block >= 63 ) ) { NoOfBlockInGroup = 1 ; } else if ( ( Block == 1 ) || ( Block == 60 ) ) { NoOfBlockInGroup = 3 ; } else { NoOfBlockInGroup = 4 ; } } else { if ( ( Block <= 7 ) || ( Block == 70 ) ) { NoOfBlockInGroup = 1 ; } else if ( ( Block == 8 ) || ( Block == 67 ) ) { NoOfBlockInGroup = 3 ; } else { NoOfBlockInGroup = 4 ; } } for ( Index = 0 ; Index < NoOfBlockInGroup ; Index ++ ) { XFL_GEOMETRY_INCREMENT ( GeomPtr , Region , Block ) ; Block ++ ; } } GroupAddress = ( 0x0000 | ( AddrMul * XFL_AMD_CHIP_UNPROTECT_ADDR ) ) ; DevDataPtr -> WriteFlash ( BaseAddress , GroupAddress , XFL_AMD_CMD_GROUP_PROTECT1 ) ; Block = 0 ; Region = 0 ; MakeUnprotectStep = 0 ; while ( NumAttempt < 1000 ) { ( void ) XFlashGeometry_ToAbsolute ( GeomPtr , Region , Block , 0 , & GroupAddress ) ; GroupAddress = ( GroupAddress | ( AddrMul * XFL_AMD_CHIP_UNPROTECT_ADDR ) ) ; if ( MakeUnprotectStep == 0 ) { DevDataPtr -> WriteFlash ( InstancePtr -> Geometry . BaseAddress , GroupAddress , XFL_AMD_CMD_GROUP_PROTECT1 ) ; FlashPause ( 10000 ) ; } DevDataPtr -> WriteFlash ( InstancePtr -> Geometry . BaseAddress , GroupAddress , XFL_AMD_CMD_GROUP_PROTECT2 ) ; FlashPause ( 4 ) ; StatusReg = DevDataPtr -> GetStatus ( InstancePtr -> Geometry . BaseAddress , GroupAddress ) ; if ( StatusReg == XFL_AMD_GROUP_UNPROTECTED ) { if ( Block == ( ( GeomPtr -> NumBlocks ) - 1 ) ) { Status = XST_SUCCESS ; break ; } else { MakeUnprotectStep = 1 ; if ( TopBoot ) { if ( ( Block == 0 ) || ( Block >= 63 ) ) { NoOfBlockInGroup = 1 ; } else if ( ( Block == 1 ) || ( Block == 60 ) ) { NoOfBlockInGroup = 3 ; } else { NoOfBlockInGroup = 4 ; } } else { if ( ( Block <= 7 ) || ( Block == 70 ) ) { NoOfBlockInGroup = 1 ; } else if ( ( Block == 8 ) || ( Block == 67 ) ) { NoOfBlockInGroup = 3 ; } else { NoOfBlockInGroup = 4 ; } } for ( Index = 0 ; Index < NoOfBlockInGroup ; Index ++ ) { XFL_GEOMETRY_INCREMENT ( GeomPtr , Region , Block ) ; Block ++ ; } } } else { MakeUnprotectStep = 0 ; NumAttempt ++ ; } } ( void ) XFlashAmd_ResetBank ( InstancePtr , Offset , Bytes ) ; return ( Status ) ; }
Unlocks the all blocks in the AMD flash device.
Unlocks the all blocks in the AMD flash device .
/*
	 * Verify inputs are valid.
	 */ /*
	 * Protect all groups of block in the device.
	 */ /*
		 * Get the physical address.
		 */ /*
			 * Increment Region/Block.
			 */ /*
	 * Setup Phase.
	 */ /*
			 * Unprotect phase.
			 */ /*
		 * Verify Phase.
		 */ /*
					 * Increment Region/Block.
					 */ /*
	 * Reset the bank(s) so that it returns to the read mode.
	 */