/*---------------------------------------------------------------------------*
  Project:  NitroSDK - CARD - libraries
  File:     card_spi.h

  Copyright 2003-2004 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Log: card_spi.h,v $
  Revision 1.6  2004/08/23 10:38:23  yosizaki
  add write-command.
  add retry count on writing timeout.

  Revision 1.5  2004/08/18 08:18:54  yosizaki
  change CARDBackupType format.

  Revision 1.4  2004/07/28 13:18:14  yosizaki
  asynchronous mode support.

  Revision 1.3  2004/07/23 08:28:07  yosizaki
  change backup-routine to ARM7 only.

  Revision 1.2  2004/07/19 13:14:23  yosizaki
  add CARDi_ProgramBackupCore.

  Revision 1.1  2004/06/28 01:53:00  yosizaki
  (none)

  $NoKeywords: $
 *---------------------------------------------------------------------------*/

#ifndef __NITRO_CARD_SPI_H__
#define __NITRO_CARD_SPI_H__


/* for CARDi_Comm() */
#define	CSPI_CONTINUOUS_ON	0x0040
#define	CSPI_CONTINUOUS_OFF	0x0000


/* Commands */
#define COMM_WRITE_STATUS	0x01	/* Status write. */
#define COMM_PROGRAM_PAGE	0x02	/* Page program. */
#define COMM_READ_ARRAY		0x03	/* Read. */
#define COMM_WRITE_DISABLE	0x04	/* Write disable. (Not used) */
#define COMM_READ_STATUS	0x05	/* Status read. */
#define COMM_WRITE_ENABLE	0x06	/* Write enable. */

/* FLASH */
#define COMM_PAGE_WRITE		0x0A
#define COMM_PAGE_ERASE		0xDB
#define COMM_SECTOR_ERASE	0xD8
#define COMM_CHIP_ERASE		0xC7
#define CARDFLASH_READ_BYTES_FAST	0x0B	/* Not used */
#define CARDFLASH_DEEP_POWDOWN		0xB9	/* Not used */
#define CARDFLASH_WAKEUP			0xAB	/* Not used */

/* Status register bits */
#define	COMM_STATUS_WIP_BIT		0x01	/* WriteInProgress (bufy) */
#define	COMM_STATUS_WEL_BIT		0x02	/* WriteEnableLatch */
#define	COMM_STATUS_BP0_BIT		0x04
#define	COMM_STATUS_BP1_BIT		0x08
#define	COMM_STATUS_BP2_BIT		0x10
#define COMM_STATUS_WPBEN_BIT	0x80


/* Device attribute structure */
typedef	struct
{
	/* device type */
	CARDBackupType	type;
	/* memory property */
	u32	total_size;	/* memory capacity (BYTE) */
	u32	sect_size;	/* write-sector (BYTE) */
	u16	page_size;	/* write-page (BYTE) */
	u16	addr_width;	/* address width on command (BYTE) */
	/* maximun wait-time (as LCD-V-COUNT tick) */
	struct
	{
		u32	program_page;	/* force loop */
		u32	write_page;	/* force loop */
		u32	write_page_total;	/* time-out */
		u32	erase_chip;	/* force loop */
		u32	erase_sector;	/* force loop */
		u32	erase_page;	/* force loop */
	} spec;
} CardDeviceInfo;


extern	const CardDeviceInfo	*cardi_device;
extern	const CardDeviceInfo	cardi_device_list[];


/* Set the size of the command to send. */
void	CARDi_CommandBegin(int len);

/* Do nothing for now, but set up polling callback etc. here. */
void	CARDi_CommandEnd(u32 force_wait, u32 timeout);


/********************************************************************/
/* Processing that takes place after exclusive control and requests have been resolved */

const CardDeviceInfo*	CARDi_FindDeviceInfo(CARDBackupType type);

/* Specify device type */
void	CARDi_IdentifyBackupCore(CARDBackupType type);

/* Read */
void	CARDi_ReadBackupCore(u32 src, void *dst, u32 len);

/* Program */
void	CARDi_ProgramBackupCore(u32 dst, const void *src, u32 len);

/* Page Write. (Delete + Write) (FLASH) */
void	CARDi_WriteBackupCore(u32 dst, const void *src, u32 len);

/* Verify */
void	CARDi_VerifyBackupCore(u32 dst, const void *src, u32 len);

/* Sector erase */
void	CARDi_EraseBackupSectorCore(u32 dst);

/* Set status write protect bits */
void	CARDi_SetWriteProtectCore(u16 stat);


#endif	/*__NITRO_CARD_SPI_H__*/
