-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnandop.cpp
231 lines (214 loc) · 4.62 KB
/
nandop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "nandop.h"
#include "hard.h"
#include "inram.h"
#include "bgFunc.h"
#include "stdio.h"
/* leave here in case for use of later time.
//#pragma arm section rwdata = "foo", code ="foo"
//name the rw section and code section
*/
#define pNandCommand (*((u8*)0x9FFFFE2))
#define pNandAddress (*((u8*)0x9FFFFE0))
#define p16NandData (*((u16*)0x9FFC000))
#define p8NandData (*((u8*)0x9FFC000))
#define NADNPAGESIZE 2048
extern u16 ReadKey,Cont,Trg; // Key input
u16 NANDSize ;
void SetNandControl(u16 control)
{
*(u16 *)0x9fe0000 = 0xd200;
*(u16 *)0x8000000 = 0x1500;
*(u16 *)0x8020000 = 0xd200;
*(u16 *)0x8040000 = 0x1500;
*(u16 *)0x9400000 = control;
*(u16 *)0x9fc0000 = 0x1500;
}
void EnableNand8bit()
{
SetNandControl(1);
}
void EnableNand16bit()
{
SetNandControl(3);
}
void DisableNandbit()
{
SetNandControl(0);
}
u32 NAND_ReadID()
{
u8 id[4];
pNandCommand = 0x90 ;
pNandAddress = 0 ;
id[0]=p8NandData;
id[1]=p8NandData;
id[2]=p8NandData;
id[3]=p8NandData;
NANDSize = 0 ;
if((*(u32*)id == 0x1580daec)||(*(u32*)id == 0x1580daad))
NANDSize = 2;
else if((*(u32*)id == 0x1580dcec)||(*(u32*)id == 0x1580dcad))
NANDSize = 4;
else if((*(u32*)id==0x15C1dcec)||(*(u32*)id==0x15C1dcad)||(*(u32*)id==0x9551d3ec))
NANDSize = 8 ;
return *(u32*)id ;
}
bool IsValidID(u32 id)
{
bool ret ;
if((id == 0x1580f1ec)||(id == 0x1580f1ad))
ret = true ;
else if((id == 0x1580daec)||(id == 0x1580daad))
ret = true ;
else if((id == 0x1580dcec)||(id == 0x1580dcad))
ret = true ;
else if((id==0x15C1dcec)||(id==0x15C1dcad)||(*(u32*)id==0x9551d3ec))
ret = true ;
else return false ;
return ret ;
}
void NAND_Erase(u32 BlockAdress)
{
u32 a1,a2;
u8 add1 = 0 , add2 = 0 , add3 = 0 , add4 = 0 , add5 = 0;
a1 = (u16)(BlockAdress%NADNPAGESIZE)&0xFFFF; //低位
a2 = (BlockAdress/NADNPAGESIZE); //高位
add1 =a1&0xFF ;
add2 =(a1>>8)&0xFF;
add3 =a2&0xFF ;
add4 =(a2>>8)&0xFF;
add5 = (a2>>16)&0xFF ;
pNandCommand = 0x60 ;
pNandAddress = add3;
pNandAddress = add4;
if(NANDSize)
pNandAddress = add5;
pNandCommand = 0xd0 ;
do
{
add1 = NAND_ReadStatus();
}
while((add1&0xC1)!=0xc0);
}
u8 NAND_ReadBusy() //1 busy . 0 not busy
{
u8 st ;
Clock_Enable();
st = *(u16*)CLOCK_DATA;
Clock_Disable();
return !(st&0x8) ;
}
u8 NAND_ReadStatus()
{
u8 dd ;
pNandCommand = 0x70 ;
dd=p8NandData;
return dd ;
}
void NAND_Reset()
{
pNandCommand = 0xFF ;
}
void Nand_Read16(u32 address ,u8* pdata,u32 size)
{
u16 nop ;
u32 a1,a2;
u8 add1 = 0 , add2 = 0 , add3 = 0 , add4 = 0 , add5 =0 ;
a1 = (u16)(address%NADNPAGESIZE)&0xFFFF; //低位
a2 = (address/NADNPAGESIZE) ; //高位
add1 =a1&0xFF ;
add2 =(a1>>8)&0xFF;
add3 =a2&0xFF ;
add4 =(a2>>8)&0xFF;
add5= (a2>>16)&0xFF ;
Clock_Enable();
pNandCommand = 0x00 ;
pNandAddress = add1;
pNandAddress = add2;
pNandAddress = add3;
pNandAddress = add4;
if(NANDSize)
pNandAddress = add5;
pNandCommand = 0x30;
do{
a1 = *(u16*)CLOCK_DATA;
}while(!(a1&0x8));
/*
*/
DmaCopy(3,0x9FFC000,pdata,size,32);
Clock_Disable();
}
void Nand_Read8(u32 address ,u8* pdata,u32 size)
{
register u16 i;
u16 nop ;
u8 d1,d2;
u32 a1,a2;
u8 add1 = 0 , add2 = 0 , add3 = 0 , add4 = 0 , add5 = 0 ;
a1 = (u16)(address%NADNPAGESIZE)&0xFFFF; //低位
a2 = (address/NADNPAGESIZE); //高位
add1 =a1&0xFF ;
add2 =(a1>>8)&0xFF;
add3 =a2&0xFF ;
add4 =(a2>>8)&0xFF;
add5= (a2>>16)&0xFF ;
Clock_Enable();
pNandCommand = 0x00 ;
pNandAddress = add1;
pNandAddress = add2;
pNandAddress = add3;
pNandAddress = add4;
if(NANDSize)
pNandAddress = add5;
pNandCommand = 0x30;
do{
a1 = *(u16*)CLOCK_DATA;
}while(!(a1&0x8));
/*
*/
for(i=0;i<(size>>1);i++)
{
d1 = p8NandData ;
d2 = p8NandData ;
((u16*)pdata)[i] = (d2<<8)+d1 ;
}
Clock_Disable();
}
void Nand_Write(u32 address, u8* pdata,u32 size)
{
register u16 i ;
u16 nop ;
u32 a1,a2;
u8 add1 = 0 , add2 = 0 , add3 = 0 , add4 = 0 ,add5 = 0;
a1 = (u16)(address%NADNPAGESIZE)&0xFFFF; //低位
a2 = (address/NADNPAGESIZE); //高位
add1 =a1&0xFF ;
add2 =(a1>>8)&0xFF;
add3 =a2&0xFF ;
add4 =(a2>>8)&0xFF;
add5= (a2>>16)&0xFF ;
Clock_Enable();
pNandCommand = 0x80 ;
pNandAddress = add1;
pNandAddress = add2;
pNandAddress = add3;
pNandAddress = add4;
if(NANDSize)
pNandAddress = add5;
for(i=0;i<size;i++)
{
p8NandData = pdata[i] ;
}
pNandCommand = 0x10;
NULL;
NULL;
do{
a1 = *(u16*)CLOCK_DATA;
}while(!(a1&0x8));
do
{
add1 = NAND_ReadStatus();
}
while((add1&0xC1)!=0xc0);
Clock_Disable();
}