forked from walczakp/xbox360-rpi-flasher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XNAND.cpp
110 lines (81 loc) · 1.86 KB
/
XNAND.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
#include "XNAND.h"
#include "XSPI.h"
#include <stdio.h>
void XNAND_ClearStatus() {
uint8_t* tmp;
tmp = XSPI_Read(0x04);
XSPI_Write(0x04, tmp);
// cleanup
delete tmp;
}
uint16_t XNAND_GetStatus() { return XSPI_ReadWord(0x04); }
uint8_t XNAND_WaitReady(uint16_t timeout) {
do {
if (!(XSPI_ReadByte(0x04) & 0x01)) {
return 1;
}
delay(10);
} while (timeout--);
printf("Delay on WaitReady reached\n");
return 0;
}
uint16_t XNAND_Erase(uint32_t blockNum) {
uint8_t* tmp;
XNAND_ClearStatus();
tmp = XSPI_Read(0);
tmp[0] |= 0x08;
XSPI_Write(0, tmp);
delete tmp;
tmp = NULL;
XSPI_WriteDword(0x0C, blockNum << 9);
if (!XNAND_WaitReady(0x1000))
return 0x8001;
XSPI_WriteByte(0x08, 0xAA);
XSPI_WriteByte(0x08, 0x55);
if (!XNAND_WaitReady(0x1000))
return 0x8002;
XSPI_WriteByte(0x08, 0x5);
if (!XNAND_WaitReady(0x1000))
return 0x8003;
return XNAND_GetStatus();
}
uint16_t XNAND_StartRead(uint32_t blockNum) {
uint16_t res;
XNAND_ClearStatus();
XSPI_WriteDword(0x0C, blockNum << 9);
XSPI_WriteByte(0x08, 0x03);
if (!XNAND_WaitReady(0x1000))
return 0x8011;
res = XNAND_GetStatus();
XSPI_Write0(0x0C);
return res;
}
void XNAND_ReadWords(uint8_t wordCount) {
uint8_t* buff;
while (wordCount--) {
XSPI_Write0(0x08);
buff = XSPI_Read(0x10);
Serial.write(buff, 4);
delete buff;
}
}
void XNAND_StartWrite() {
XNAND_ClearStatus();
XSPI_Write0(0x0C);
}
void XNAND_WriteProcess(uint8_t *buf, uint8_t wordCount) {
while (wordCount--) {
XSPI_Write(0x10, buf);
XSPI_WriteByte(0x08, 0x01);
buf += 4;
}
}
uint16_t XNAND_WriteExecute(uint32_t blockNum) {
XSPI_WriteDword(0x0C, blockNum << 9);
XSPI_WriteByte(0x08, 0x55);
XSPI_WriteByte(0x08, 0xAA);
XSPI_WriteByte(0x08, 0x4);
if (!XNAND_WaitReady(0x1000))
return 0x8021;
return XNAND_GetStatus();
}