flatbs.c
5.01 KB
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
#include "cpusim.h"
#include "bbnand.h"
void
passed(void) {
message("test flatbs passed\n");
test_postamble();
}
void
failed(const char* code) {
message("test flatbs failed (");
message(code);
message(")\n");
test_postamble();
}
static int wait_page(void) {
do {
if (IO_READ(MI_EINTR_REG)&MI_INTR_MD) return 1;
} while(IO_READ(PI_FLASH_CTRL_REG)&PI_FLASH_CTRL_BUSY);
return (IO_READ(PI_FLASH_CTRL_REG) & PI_FLASH_CTRL_DBERR) ? 1 : 0;
}
static int
read_page(unsigned addr, int which_buf, int wait) {
IO_WRITE(PI_FLASH_ADDR_REG, addr << PI_FLASH_PAGE_ADDR_SHIFT);
IO_WRITE(PI_FLASH_CTRL_REG, PI_FLASH_CTRL_START |
PI_FLASH_CTRL_RDPH |
0xf << PI_FLASH_CTRL_ADPH_SHIFT |
0x0 << PI_FLASH_CTRL_CMD_SHIFT |
PI_FLASH_CTRL_WRDY |
which_buf << PI_FLASH_CTRL_BUF_SHIFT |
0 << PI_FLASH_CTRL_DEV_SHIFT |
PI_FLASH_CTRL_ECC |
528);
return wait ? wait_page() : 0;
}
static void
aes_setup(int buffer, int chain) {
IO_WRITE(PI_AES_CTRL_REG, PI_AES_CTRL_START |
((512/16)-1) << PI_AES_CTRL_SIZE_SHIFT |
(buffer ? 32 : 0) << PI_AES_CTRL_DATA_SHIFT |
(chain ? PI_AES_CTRL_HC :
(PI_AES_INIT_INDX16 << PI_AES_CTRL_IV_SHIFT)));
}
static const unsigned int aes_ekey[44] = {
0xbabecafe, 0xfeedbeef, 0xbad22dad, 0xdeadd00d,
0xd729d838, 0x700ebb99, 0x99370e14, 0x307feebb,
0x9aed5431, 0xeae3efa8, 0x73d4e1bc, 0x43ab0f07,
0xa63fcd14, 0x4cdc22bc, 0x3f08c300, 0x7ca3cc07,
0xe19a7022, 0xad46529e, 0x924e919e, 0xeeed5d99,
0x9aa5496d, 0x37e31bf3, 0xa5ad8a6d, 0x4b40d7f4,
0x52ca4230, 0x652959c3, 0xc084d3ae, 0x8bc4045a,
0x4e8c2dc2, 0x2ba57401, 0xeb21a7af, 0x60e5a3f5,
0x00c39741, 0x2b66e340, 0xc04744ef, 0xa0a2e71a,
0x68f0195f, 0x4396fa1f, 0x83d1bef0, 0x237359ea,
0xb721863f, 0x755f9da8, 0x4e4ca3a2, 0x2e886422,
};
static const unsigned int aes_iv[4] = {
0xdeadd00d, 0xdad22bad, 0xdeadbeef, 0xcafebabe,
};
/* map a contiguous region */
static void
atb_setup(unsigned int base, unsigned int fladdr, unsigned int size) {
int i, j;
IO_WRITE(PI_ATBU_REG, PI_ATBU_IV | /* IV */
(0<<PI_ATBU_DEV_SHIFT) | /* device */
PI_ATBU_PERM_PIO | PI_ATBU_PERM_DMA | /* perm */
(0<<PI_ATBU_SIZE_SHIFT)); /* size */
IO_WRITE(PI_ATB_BUFFER_LO_REG,
0 | (((base>>14)-1) << PI_ATBL_VADDR_SHIFT) );
IO_WRITE(PI_ATBU_REG, (0<<PI_ATBU_DEV_SHIFT) | /* device */
PI_ATBU_PERM_PIO | PI_ATBU_PERM_DMA | /* perm */
(0<<PI_ATBU_SIZE_SHIFT)); /* size */
for(i = 1; i < size/(16*1024)+1; i++) {
IO_WRITE(PI_ATB_BUFFER_LO_REG+i*4,
((fladdr+i-1) << PI_ATBL_PADDR_SHIFT) |
(((base+(i-1)*16*1024)>>14) << PI_ATBL_VADDR_SHIFT) );
}
/* map rest of entries with same last entry */
for(j = i; j < PI_ATB_NUM_ENTRIES; j++)
IO_WRITE(PI_ATB_BUFFER_LO_REG+j*4,
((fladdr+i-1) << PI_ATBL_PADDR_SHIFT) |
(((base+(i-1)*16*1024)>>14) << PI_ATBL_VADDR_SHIFT) );
}
/* table of 4096 random 8-byte offsets */
const unsigned short tab[4096] = {
#include "rtab.h"
};
int
main() {
int i, j;
unsigned short csum = 0, z;
test_preamble();
/* initialize memory and caches */
init_ddr();
initICache();
initDCache();
setcp0reg(C0_SR, SR_CU0|SR_CU1|SR_BEV);
setcp0reg(C0_CONFIG, CONFIG_BE|CONFIG_NONCOHRNT);
/* configure flash */
IO_WRITE(PI_FLASH_CONFIG_REG, 7 << 28 | // end of cycle time - 2
5 << 24 | // read data sample time - 1
0x3e << 16 | // RE active time
0x1f << 8 | // WE active time
0x3f << 0); // CLE/ALE active time
IO_WRITE(PI_FLASH_CTRL_REG, PI_FLASH_CTRL_RDPH |
0xf << PI_FLASH_CTRL_ADPH_SHIFT |
0x0 << PI_FLASH_CTRL_CMD_SHIFT |
PI_FLASH_CTRL_WRDY |
0 << PI_FLASH_CTRL_BUF_SHIFT |
0 << PI_FLASH_CTRL_DEV_SHIFT |
PI_FLASH_CTRL_ECC |
0x3ff);
for(i = 0; i < 11; i++)
for(j = 0; j < 4; j++)
IO_WRITE(PI_AES_EKEY_REG+(i*4+j)*4, aes_ekey[(10-i)*4+j]);
for(i = 0; i < 4; i++)
IO_WRITE(PI_AES_INIT_REG+4*i, aes_iv[i]);
IO_WRITE(PI_AES_CTRL_REG, 0);
atb_setup(PI_DOM1_ADDR1, 0, 32*1024);
invalDCache((void*)K0BASE, 32*1024);
z = getcp0reg(C0_COUNT);
for(j = 0; j < (32*1024)/8; j++) {
#if 1
IO_WRITE(PI_DRAM_ADDR_REG, K0_TO_PHYS(K0BASE)+tab[j]*8);
IO_WRITE(PI_CART_ADDR_REG, PI_DOM1_ADDR1+tab[j]*8);
#else
IO_WRITE(PI_DRAM_ADDR_REG, K0_TO_PHYS(K0BASE)+j*8);
IO_WRITE(PI_CART_ADDR_REG, PI_DOM1_ADDR1+j*8);
#endif
IO_WRITE(PI_WR_LEN_REG, 7);
while(IO_READ(PI_STATUS_REG) & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)) {
if (IO_READ(PI_STATUS_REG) & PI_STATUS_ERROR) failed("0");
}
}
IO_WRITE(PI_IDE3_BASE_REG, getcp0reg(C0_COUNT)-z);
for(j = 0; j < (32*1024)/4; j++) {
unsigned x = *((volatile unsigned *)K0BASE+j);
//IO_WRITE(PI_IDE3_BASE_REG, x);
csum += (x >> 24) + ((x >> 16)&0xff) + ((x >> 8)&0xff) + (x&0xff);
}
IO_WRITE(PI_IDE3_BASE_REG, csum);
if (csum != 0x3844) failed("5");
passed();
return 0;
}