1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - CTRDG - include
3   File:     ctrdg_backup.h
4 
5   Copyright 2003-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_CTRDG_BACKUP_H_
19 #define NITRO_CTRDG_BACKUP_H_
20 
21 #include <nitro.h>
22 #include "ctrdg_task.h"
23 #include "ctrdg_flash.h"
24 #include "ctrdg_sram.h"
25 
26 // define data----------------------------------
27 #define CTRDG_BACKUP_PHASE_VERIFY           0x0000
28 #define CTRDG_BACKUP_PHASE_PROGRAM          0x0001
29 #define CTRDG_BACKUP_PHASE_SECTOR_ERASE     0x0002
30 #define CTRDG_BACKUP_PHASE_CHIP_ERASE       0x0003
31 #define CTRDG_BACKUP_PHASE_VERIFY_ERASE     0x0004
32 #define CTRDG_BACKUP_PHASE_PARAMETER_CHECK  0x00ff
33 
34 #define CTRDG_BACKUP_RESULT_OK              0x0000
35 #define CTRDG_BACKUP_RESULT_ERROR           0x8000
36 #define CTRDG_BACKUP_RESULT_TIMEOUT         0x4000
37 #define CTRDG_BACKUP_RESULT_Q5TIMEOUT       0x2000
38 #define CTRDG_BACKUP_RESULT_PULLEDOUT       0x1000
39 
40 /* For debugging */
41 //#ifndef   __FLASH_DEBUG
42 #define CTRDG_BACKUP_VENDER_ID_ADR      (CTRDG_AGB_FLASH_ADR+0x00000000)
43 #define CTRDG_BACKUP_DEVICE_ID_ADR      (CTRDG_AGB_FLASH_ADR+0x00000001)
44 #define CTRDG_BACKUP_COM_ADR1           (CTRDG_AGB_FLASH_ADR+0x00005555)
45 #define CTRDG_BACKUP_COM_ADR2           (CTRDG_AGB_FLASH_ADR+0x00002aaa)
46 //#else
47 //#define   VENDER_ID_ADR       0x02003ffc
48 //#define   DEVICE_ID_ADR       0x02003ffd
49 //#define   COM_ADR1    0x02003ffe
50 //#define COM_ADR2  0x02003fff
51 //#endif
52 
53 #define CTRDG_BACKUP_MEGA_512K_ID       0x1cc2
54 #define CTRDG_BACKUP_MEGA_1M_ID         0x09c2
55 //#define   CTRDG_BACKUP_MEGA_1M_ID         0x1cc2
56 
57 // data type declaration------------------------
58 typedef struct CTRDGiFlashTypePlusTag
59 {
60     u16     (*CTRDGi_WriteAgbFlashSector) (u16 secNo, u8 *src);
61     u16     (*CTRDGi_EraseAgbFlashChip) (void);
62     u16     (*CTRDGi_EraseAgbFlashSector) (u16 secNo);
63     void    (*CTRDGi_WriteAgbFlashSectorAsync) (u16 secNo, u8 *src, CTRDG_TASK_FUNC callback);
64     void    (*CTRDGi_EraseAgbFlashChipAsync) (CTRDG_TASK_FUNC callback);
65     void    (*CTRDGi_EraseAgbFlashSectorAsync) (u16 secNo, CTRDG_TASK_FUNC callback);
66     u16     (*CTRDGi_PollingSR) (u16 phase, u8 *adr, u16 lastData);
67     const u16 (*maxtime);
68     CTRDGFlashType type;
69 }
70 CTRDGiFlashTypePlus;
71 
72 typedef enum
73 {
74     CTRDG_BACKUP_TYPE_FLASH_512K,      /* 512K flash */
75     CTRDG_BACKUP_TYPE_FLASH_1M,        /* 1M flash */
76     CTRDG_BACKUP_TYPE_SRAM             /* 256K SRAM */
77 }
78 CTRDGBackupType;
79 
80 // External Variable Declarations----------------------
81 extern u16 ctrdg_flash_remainder;
82 extern const u16 (*ctrdgi_fl_maxtime);
83 extern u16 (*CTRDGi_PollingSR) (u16 phase, u8 *adr, u16 lastData);
84 
85 // Function Declarations-------------------------
86 extern const CTRDGFlashType *AgbFlash;
87 extern u16 (*CTRDGi_WriteAgbFlashSector) (u16 secNo, u8 *src);
88 extern u16 (*CTRDGi_EraseAgbFlashChip) (void);
89 extern u16 (*CTRDGi_EraseAgbFlashSector) (u16 secNo);
90 extern void (*CTRDGi_WriteAgbFlashSectorAsync) (u16 secNo, u8 *src, CTRDG_TASK_FUNC callback);
91 extern void (*CTRDGi_EraseAgbFlashChipAsync) (CTRDG_TASK_FUNC callback);
92 extern void (*CTRDGi_EraseAgbFlashSectorAsync) (u16 secNo, CTRDG_TASK_FUNC callback);
93 extern u16 (*pollingSR) (u16 phase, u8 *adr, u16 lastData);
94 
95 /*------------------------------------------------------------------*/
96 /*          Device Identifiers and Default Settings                            */
97 /*------------------------------------------------------------------*/
98 /*---------------------------------------------------------------------------*
99   Name:         CTRDG_IdentifyAgbBackup
100 
101                 The corresponding AGB function: extern u16 IdentifyFlash_512K()
102                 The corresponding AGB function: extern u16 IdentifyFlash_1M()
103 
104   Description:  Specifies the backup device on the NITRO-CTRDG.
105                 When the backup device is flash memory, the flash ID is read, the flash on the cartridge is identified, its volume and sector size are gotten, the access speed is set, and the corresponding flash access functions are set.
106 
107 
108 
109                 The gotten flash data can be referenced with the global variable, CTRDGFlashType *AgbFlash.
110                 (See the definitions above for more on CTRDGFlashType.)
111 
112                 This function must be called once before accessing the backup device (including reads).
113                 An error is returned if the device cannot be identified, and the following access functions can no longer be used.
114 
115                 Access cycle settings are made within the function, and do not need to occur in advance.
116                 As this function uses ticks to measure timeouts, the OS_InitTick function must be called in advance.
117 
118 
119                 * Be aware that within this function, all interrupts are prohibited for a certain period of time, and the cartridge is locked.
120                 In particular, when calling this function do not use DMAs that automatically launch at specific times, such as direct sound, V/H-Blank synchronization, display synchronization, cartridge requests, etc.
121 
122 
123                 In addition, when the arguments CTRDG_BACKUP_TYPE_FLASH_512K or CTRDG_BACKUP_TYPE_FLASH_1M are used, write operations for the device will begin, and if a device different than that of the argument is on the NITRO-CTRDG, be aware that the backup data may be corrupted.
124 
125 
126 
127 
128   Arguments:    type: Type of backup device on the NITRO-CTRDG.
129 
130   Returns:      0     : Normal end
131                 Non-zero: Identification error
132  *---------------------------------------------------------------------------*/
133 extern u16 CTRDG_IdentifyAgbBackup(CTRDGBackupType type);
134 
135 #endif // NITRO_CTRDG_BACKUP_H_
136