nvram.c
4.84 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
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
#include <PR/bbnvram.h>
#include "ultra64.h"
#include <PR/bcp.h>
#include "nvram.h"
/* fill virage data with data to be written, then
call using: nms_store_and_verify(1, VIRAGE0_CTRL_REG, 16); or
nms_store_and_verify(1, VIRAGE1_CTRL_REG, 16); or
nms_store_and_verify(1, VIRAGE2_CTRL_REG, 64);
*/
static int nms_store(int ctrl_reg);
static void
__delay(count) {
int i, dummy = 0;
for(i=0; i< count; i++){
dummy++;
}
}
static void
nms_tune(int ctrl_reg) {
#define NMS_REG(base,reg) ((base)+((reg)-(VIRAGE0_CTRL_REG&0xffff0000)))
int base = ctrl_reg & 0xffff0000;
IO_WRITE(NMS_REG(base, VIRAGE0_NMS_CRSTO_0_REG), 0x8a);
IO_WRITE(NMS_REG(base, VIRAGE0_NMS_CRSTO_1_REG), 0x13);
IO_WRITE(NMS_REG(base, VIRAGE0_NMS_CRM_0_REG), 0x80);
IO_WRITE(NMS_REG(base, VIRAGE0_NMS_CRM_1_REG), 0x92);
IO_WRITE(NMS_REG(base, VIRAGE0_NMS_CRM_2_REG), 0x18);
IO_WRITE(NMS_REG(base, VIRAGE0_NMS_CRM_3_REG), 0x05);
#undef NMS_REG
}
void
nms_init(void) {
IO_WRITE(VIRAGE2_CTRL_REG, 0); /* take v2 out of bypass */
nms_tune(VIRAGE0_CTRL_REG);
nms_tune(VIRAGE1_CTRL_REG);
nms_tune(VIRAGE2_CTRL_REG);
}
int nms_store_and_verify(int ctrl_reg, const void* data, int size)
{
int x;
int i;
int array_addr = ctrl_reg & 0xffff0000;
const unsigned int* virage_data = data;
int sysclk = get_sys_clk_ns();
/*
* Make sure the NMS is not bypassed
*/
IO_WRITE(ctrl_reg, 0x0);
/*
* This should be set to give 1uSec. It uses sysclock as input. Wait
* a little afterwards to make sure a few clocks of time-base go through
*/
IO_WRITE(MI_SEC_VTIMER_REG, 1000/sysclk + 1);
__delay((4*160000)/sysclk);
/*
BCP_STALL((4 * 160000)/sysclk);
*/
/*
* Wait for Charge pump to exit PORST
*/
x = IO_READ(ctrl_reg);
if (x & VIRAGE_CTRL_CP_PORST) {
__delay(20000/sysclk);
/*
BCP_STALL(sysclk_scale *20000/sysclk);
*/
__delay(20000/sysclk);
x = IO_READ(ctrl_reg);
if (x & VIRAGE_CTRL_CP_PORST) {
/* power on reset not reset */
return FAIL;
}
}
x = IO_READ(ctrl_reg);
if ((x & VIRAGE_CTRL_NMS_READY) == 0) {
/* not ready */
return FAIL;
}
for (i=0; i<size; i++) {
IO_WRITE(array_addr+(i<<2), virage_data[i]);
x = IO_READ(array_addr+(i<<2));
if (x != virage_data[i]) {
/* problem storing in array */
return FAIL;
}
}
if (nms_store(ctrl_reg) != PASS) {
return FAIL;
}
/*
* remove old data: zero out
*/
for (i=0; i<size; i++) {
IO_WRITE(array_addr+(i<<2), 0);
x = IO_READ(array_addr+(i<<2));
if (x != 0) {
/* couldnt zero sram */
return FAIL;
}
}
if (nms_recall(ctrl_reg) != PASS) {
/* recall did not pass */
return FAIL;
}
/*
* Check all values
*/
for (i=0; i<size; i++) {
x = IO_READ(array_addr+(i<<2));
if (x != virage_data[i]) {
/* data not same as that written */
return FAIL;
}
}
/* data written, verified correct */
return PASS;
}
/*
* Get the system clock and scale it by some factor to speed up the NoVEA operations
*/
int get_sys_clk_ns(void)
{
int clk = (IO_READ(PI_GPIO_REG) & PI_ID_SYS_CLOCK_MASK) >> PI_ID_SYS_CLOCK_SHIFT;
if (clk == PI_ID_SYS_CLOCK_62_5)
return 1000/62.5;
else if (clk == PI_ID_SYS_CLOCK_80)
return 1000/80;
else
return 1000/96;
}
int nms_recall(int ctrl_reg)
{
int x;
int nms_ctrl_reg = ctrl_reg | 0x2000;
int sysclk = get_sys_clk_ns();
/*
* Issue a recall to make sure it got stored
*/
IO_WRITE(nms_ctrl_reg, NMS_CMD_RECALL << VIRAGE_CTRL_NMS_CMD_SHIFT);
/*
* Wait for the RCready time
*/
__delay((EXTRA_WAIT + Trcready/sysclk)*DELAY_MULT);
/*
BCP_STALL(EXTRA_WAIT + Trcready/sysclk);
*/
/*
* Extra wait since we are going through NMS - Don't have a timing spec here
*/
/*
BCP_STALL((MORE_EXTRA_WAIT) + Trcready/sysclk);
*/
__delay((MORE_EXTRA_WAIT + Trcready/sysclk)*DELAY_MULT);
x = IO_READ(ctrl_reg);
if ((x & VIRAGE_CTRL_NMS_READY) == 0) {
/* nms not ready */
return FAIL;
}
return PASS;
}
/* this version does not set CRST* registers */
static int nms_store(int ctrl_reg)
{
u32 x;
int nms_ctrl_reg = ctrl_reg | 0x2000;
IO_WRITE(nms_ctrl_reg, NMS_CMD_STORE << VIRAGE_CTRL_NMS_CMD_SHIFT);
__delay(100);
x = IO_READ(ctrl_reg);
if (x & VIRAGE_CTRL_NMS_READY) {
/* read not low */
return FAIL;
}
while(((x = IO_READ(ctrl_reg)) & VIRAGE_CTRL_NMS_READY) == 0){
__delay(100);
}
if ((x & VIRAGE_CTRL_NMS_PASS) == 0) {
return FAIL;
}
return PASS;
}