-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathflash.c
127 lines (105 loc) · 2.82 KB
/
flash.c
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
#include "include/types.h"
#include <vsprintf.h>
#define STATUS (1)
#define COMMAND (2)
#define ADDRESS (3)
#define DATA (4)
static inline uint32_t bswap_32(uint32_t t)
{
return ((t & 0xFF) << 24) | ((t & 0xFF00) << 8) | ((t & 0xFF0000) >> 8) | ((t & 0xFF000000) >> 24);
}
void sfcx_writereg(int addr, unsigned long data)
{
*(volatile unsigned int*)(0x200ea00c000ULL|(addr*4)) = bswap_32(data);
}
unsigned long sfcx_readreg(int addr)
{
return bswap_32(*(volatile unsigned int*)(0x200ea00c000ULL|(addr*4)));
}
void readsector(unsigned char *data, int sector, int raw)
{
int status;
sfcx_writereg(STATUS, sfcx_readreg(STATUS));
sfcx_writereg(ADDRESS, sector);
sfcx_writereg(COMMAND, raw ? 3 : 2);
while ((status = sfcx_readreg(STATUS))&1);
if (status != 0x200)
{
if (status & 0x40)
printf(" * Bad block found at %08x\n", sector);
else if (status & 0x1c)
printf(" * (corrected) ECC error %08x: %08x\n", sector, status);
else if (!raw && (status & 0x800))
printf(" * illegal logical block %08x (status: %08x)\n", sector, status);
else
printf(" * Unknown error at %08x: %08x. Please worry.\n", sector, status);
}
sfcx_writereg(ADDRESS, 0);
int i;
for (i = 0; i < 0x210; i+=4)
{
sfcx_writereg(COMMAND, 0);
*(int*)(data + i) = bswap_32(sfcx_readreg(DATA));
}
}
void flash_erase(int address)
{
sfcx_writereg(0, sfcx_readreg(0) | 8);
sfcx_writereg(STATUS, 0xFF);
sfcx_writereg(ADDRESS, address);
while (sfcx_readreg(STATUS) & 1);
sfcx_writereg(COMMAND, 0xAA);
sfcx_writereg(COMMAND, 0x55);
while (sfcx_readreg(STATUS) & 1);
sfcx_writereg(COMMAND, 0x5);
while (sfcx_readreg(STATUS) & 1);
int status = sfcx_readreg(STATUS);
if (status != 0x200)
printf("[%08x]", status);
sfcx_writereg(STATUS, 0xFF);
sfcx_writereg(0, sfcx_readreg(0) & ~8);
}
void write_page(int address, unsigned char *data)
{
sfcx_writereg(STATUS, 0xFF);
sfcx_writereg(0, sfcx_readreg(0) | 8);
sfcx_writereg(ADDRESS, 0);
int i;
for (i = 0; i < 0x210; i+=4)
{
sfcx_writereg(DATA, bswap_32(*(int*)(data + i)));
sfcx_writereg(COMMAND, 1);
}
sfcx_writereg(ADDRESS, address);
sfcx_writereg(COMMAND, 0x55);
while (sfcx_readreg(STATUS) & 1);
sfcx_writereg(COMMAND, 0xAA);
while (sfcx_readreg(STATUS) & 1);
sfcx_writereg(COMMAND, 0x4);
while (sfcx_readreg(STATUS) & 1);
int status = sfcx_readreg(STATUS);
if (status != 0x200)
printf("[%08x]", status);
sfcx_writereg(0, sfcx_readreg(0) & ~8);
}
void calcecc(unsigned int *data)
{
unsigned int i=0, val=0;
unsigned char *edc = ((unsigned char*)data) + 0x200;
unsigned int v;
for (i = 0; i < 0x1066; i++)
{
if (!(i & 31))
v = ~bswap_32(*data++);
val ^= v & 1;
v>>=1;
if (val & 1)
val ^= 0x6954559;
val >>= 1;
}
val = ~val;
edc[0xC] |= (val << 6) & 0xC0;
edc[0xD] = (val >> 2) & 0xFF;
edc[0xE] = (val >> 10) & 0xFF;
edc[0xF] = (val >> 18) & 0xFF;
}