1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - CTRDG - libraries
3   File:     ctrdg_backup.c
4 
5   Copyright 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:: 2007-11-15#$
14   $Rev: 2414 $
15   $Author: hatamoto_minoru $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nitro.h>
19 
20 // Extern data declaration----------------------
21 extern const CTRDGiFlashTypePlus defaultFlash1M;
22 extern const CTRDGiFlashTypePlus MX29L010;
23 extern const CTRDGiFlashTypePlus LE26FV10N1TS_10;
24 
25 extern const CTRDGiFlashTypePlus defaultFlash512;
26 extern const CTRDGiFlashTypePlus LE39FW512;
27 extern const CTRDGiFlashTypePlus AT29LV512_lib;
28 extern const CTRDGiFlashTypePlus MN63F805MNP;
29 
30 /*Exclusive control*/
31 extern u16 ctrdgi_flash_lock_id;
32 extern u16 ctrdgi_sram_lock_id;
33 
34 // Const data-----------------------------------
35 static const u8 AgbLib1MFlash_ver[] = "AGBFLASH1M_V102";
36 static const u8 AgbLibFlash_ver[] = "AGBFLASH512_V131";
37 
38 static const CTRDGiFlashTypePlus *const flash1M_list[] = {
39     &MX29L010,
40     &LE26FV10N1TS_10,
41     &defaultFlash1M,
42 };
43 
44 static const CTRDGiFlashTypePlus *const flash512_list[] = {
45     &LE39FW512,
46     &AT29LV512_lib,                    //Atmel
47     &MN63F805MNP,
48     &defaultFlash512,
49 };
50 
51 static const u16 readidtime[] = {
52     20,                                //20ms
53 };
54 
55 // Global variables-----------------------------
56 
57 
58 /*******************************************************
59 
60     Function's description
61 
62 ********************************************************/
63 
64 // Flash Identify functions---------------------
65 
CTRDG_IdentifyAgbBackup(CTRDGBackupType type)66 u16 CTRDG_IdentifyAgbBackup(CTRDGBackupType type)
67 {
68     u16     result = 1;
69     u16     flashID;
70     const CTRDGiFlashTypePlus *const *flp;
71     MICartridgeRamCycle ram_cycle;
72 
73 #ifndef SDK_TWLLTD
74     SDK_ASSERT(CTRDGi_IsInitialized());
75 #endif
76 
77     if (type == CTRDG_BACKUP_TYPE_FLASH_512K || type == CTRDG_BACKUP_TYPE_FLASH_1M)
78     {
79         /*Exclusive control */
80         ctrdgi_flash_lock_id = (u16)OS_GetLockID();
81 
82         /*Exclusive control (lock) */
83         (void)OS_LockCartridge(ctrdgi_flash_lock_id);
84 
85         /*Access cycle settings */
86         ram_cycle = MI_GetCartridgeRamCycle();
87         MI_SetCartridgeRamCycle(MI_CTRDG_RAMCYCLE_18);
88 
89         ctrdgi_fl_maxtime = readidtime; //Unless tentative settings are made, the timer within ReadflashID won't work
90         flashID = CTRDGi_ReadFlashID();
91         if (type == CTRDG_BACKUP_TYPE_FLASH_512K)
92             flp = flash512_list;
93         if (type == CTRDG_BACKUP_TYPE_FLASH_1M)
94             flp = flash1M_list;
95 
96         /*Access cycle settings */
97         MI_SetCartridgeRamCycle(ram_cycle);
98         /*Exclusive control (unlock) */
99         (void)OS_UnlockCartridge(ctrdgi_flash_lock_id);
100 
101         result = 1;
102         while ((*flp)->type.makerID != 0x00)
103         {
104             if ((flashID & 0xff) == *(u16 *)&((*flp)->type.makerID))
105             {
106                 result = 0;
107                 break;
108             }
109             flp++;
110         }
111         CTRDGi_WriteAgbFlashSector = (*flp)->CTRDGi_WriteAgbFlashSector;
112         CTRDGi_EraseAgbFlashChip = (*flp)->CTRDGi_EraseAgbFlashChip;
113         CTRDGi_EraseAgbFlashSector = (*flp)->CTRDGi_EraseAgbFlashSector;
114         CTRDGi_WriteAgbFlashSectorAsync = (*flp)->CTRDGi_WriteAgbFlashSectorAsync;
115         CTRDGi_EraseAgbFlashChipAsync = (*flp)->CTRDGi_EraseAgbFlashChipAsync;
116         CTRDGi_EraseAgbFlashSectorAsync = (*flp)->CTRDGi_EraseAgbFlashSectorAsync;
117         CTRDGi_PollingSR = (*flp)->CTRDGi_PollingSR;
118         ctrdgi_fl_maxtime = (*flp)->maxtime;
119         AgbFlash = &(*flp)->type;
120     }
121     else if (type == CTRDG_BACKUP_TYPE_SRAM)
122     {
123         ctrdgi_sram_lock_id = (u16)OS_GetLockID();
124         result = 0;
125     }
126     return result;
127 }
128