summaryrefslogtreecommitdiff
path: root/gp2x/cpuctrl.c
blob: ab165fc87d1dc71c8ea0fdc2f19e6b69a0104aaa (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*  cpuctrl.c for GP2X (CPU/LCD/RAM-Tuner Version 2.0)
    Copyright (C) 2006 god_at_hell 
    original CPU-Overclocker (c) by Hermes/PS2Reality 
	the gamma-routine was provided by theoddbot
	parts (c) Rlyehs Work

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


/****************************************************************************************************************************************/
// CPU CONTROL
/****************************************************************************************************************************************/

#include <sys/mman.h>
#include <math.h>
#include <stdio.h>
#include "gp2xminilib.h"

#define SYS_CLK_FREQ 7372800

//from minimal library rlyeh

extern unsigned long   gp2x_dev[4];
extern unsigned short *gp2x_memregs;

// system registers
static struct 
{
	unsigned short SYSCLKENREG,SYSCSETREG,FPLLVSETREG,DUALINT920,DUALINT940,DUALCTRL940,DISPCSETREG,MEMTIMEX0;
	unsigned short MEMTIMEX1,MEMREFX,MLC_GAMM_BYPATH,MLC_GAMMA_A,MLC_GAMMA_D,YBNKLVL;
}
system_reg;

volatile unsigned short *MEM_REG;
unsigned MDIV,PDIV,SCALE;
volatile unsigned *arm940code;

void cpuctrl_init()
{
	MEM_REG=&gp2x_memregs[0];
}

void save_system_regs()
{
	system_reg.SYSCSETREG=MEM_REG[0x91c>>1];
	system_reg.FPLLVSETREG=MEM_REG[0x912>>1];
	system_reg.SYSCLKENREG=MEM_REG[0x904>>1];
	system_reg.DUALINT920=MEM_REG[0x3B40>>1];	
	system_reg.DUALINT940=MEM_REG[0x3B42>>1];
	system_reg.DUALCTRL940=MEM_REG[0x3B48>>1];
	system_reg.DISPCSETREG=MEM_REG[0x924>>1];
	system_reg.MEMTIMEX0=MEM_REG[0x3802>>1];
	system_reg.MEMTIMEX1=MEM_REG[0x3804>>1];
	system_reg.MEMREFX=MEM_REG[0x3808>>1];
	system_reg.MLC_GAMM_BYPATH=MEM_REG[0x2880>>1];
	system_reg.MLC_GAMMA_A=MEM_REG[0x295C>>1];
	system_reg.MLC_GAMMA_D=MEM_REG[0x295E>>1];
	system_reg.YBNKLVL=MEM_REG[0x283A>>1];
}

void load_system_regs()
{
	MEM_REG[0x91c>>1]=system_reg.SYSCSETREG;
	MEM_REG[0x910>>1]=system_reg.FPLLVSETREG;
	MEM_REG[0x3B40>>1]=system_reg.DUALINT920;
	MEM_REG[0x3B42>>1]=system_reg.DUALINT940;
	MEM_REG[0x3B48>>1]=system_reg.DUALCTRL940;
	MEM_REG[0x904>>1]=system_reg.SYSCLKENREG;
	/* Set UPLLSETVREG to 0x4F02, which gives 80MHz */
	MEM_REG[0x0914>>1] = 0x4F02;
	/* Wait for clock change to start */
	while (MEM_REG[0x0902>>1] & 2);
	/* Wait for clock change to be verified */
	while (MEM_REG[0x0916>>1] != 0x4F02);
	MEM_REG[0x3802>>1]=system_reg.MEMTIMEX0;
	MEM_REG[0x3804>>1]=system_reg.MEMTIMEX1;
	MEM_REG[0x3808>>1]=system_reg.MEMREFX;
	MEM_REG[0x2880>>1]=system_reg.MLC_GAMM_BYPATH;
	MEM_REG[0x295C>>1]=system_reg.MLC_GAMMA_A;
	MEM_REG[0x295E>>1]=system_reg.MLC_GAMMA_D;
	MEM_REG[0x283A>>1]=system_reg.YBNKLVL;
}


void set_FCLK(unsigned MHZ)
{
	printf ("set CPU-Frequency = %uMHz\r\n",MHZ);
	unsigned v;
	unsigned mdiv,pdiv=3,scale=0;
	MHZ*=1000000;
	mdiv=(MHZ*pdiv)/SYS_CLK_FREQ;
	//printf ("Old value = %04X\r",MEM_REG[0x924>>1]," ");
	//printf ("APLL = %04X\r",MEM_REG[0x91A>>1]," ");
	mdiv=((mdiv-8)<<8) & 0xff00;
	pdiv=((pdiv-2)<<2) & 0xfc;
	scale&=3;
	v=mdiv | pdiv | scale;
	MEM_REG[0x910>>1]=v;
}

unsigned get_FCLK()
{
	return MEM_REG[0x910>>1];
}

void set_add_FLCDCLK(int addclock)
{
	//Set LCD controller to use FPLL
	printf ("...set to FPLL-Clockgen...\r\n");
	printf ("set Timing-Prescaler = %i\r\n",addclock);
	MEM_REG[0x924>>1]= 0x5A00 + ((addclock)<<8); 
	//If you change the initial timing, don't forget to shift your intervall-borders in "cpu_speed.c"
}

void set_add_ULCDCLK(int addclock)
{
	//Set LCD controller to use UPLL
	printf ("...set to UPLL-Clockgen...\r\n");
	printf ("set Timing-Prescaler = %i\r\n",addclock);
	MEM_REG[0x0924>>1] = 0x8900 + ((addclock)<<8);
	//If you change the initial timing, don't forget to shift your intervall-borders in "cpu_speed.c"
}

unsigned get_LCDClk()
{
	if (MEM_REG[0x0924>>1] < 0x7A01) return((MEM_REG[0x0924>>1] - 0x5A00)>>8);
	else return((MEM_REG[0x0924>>1] - 0x8900)>>8);
}

char get_Clkgen()
{
	if (MEM_REG[0x0924>>1] < 0x7A01) return(0);
	else return(1);
}

unsigned get_freq_UCLK()
{
	unsigned i;
	unsigned reg,mdiv,pdiv,scale;
	i = MEM_REG[0x900>>1];
	i = ((i >> 7) & 1) ;
	if(i) return 0;
	reg=MEM_REG[0x916>>1];
	mdiv = ((reg & 0xff00) >> 8) + 8;
	pdiv = ((reg & 0xfc) >> 2) + 2;
	scale = reg & 3;	
	return ((SYS_CLK_FREQ * mdiv) / (pdiv << scale));
}

unsigned get_freq_ACLK()
{
	unsigned i;
	unsigned reg,mdiv,pdiv,scale;
	i = MEM_REG[0x900>>1];
	i = ((i >> 8) & 1) ;
	if(i) return 0;
	reg=MEM_REG[0x918>>1];
	mdiv = ((reg & 0xff00) >> 8) + 8;
	pdiv = ((reg & 0xfc) >> 2) + 2;
	scale = reg & 3;
	return ((SYS_CLK_FREQ * mdiv)/(pdiv << scale));
}

unsigned get_freq_920_CLK()
{
	unsigned i;
	unsigned reg,mdiv,pdiv,scale;
	reg=MEM_REG[0x912>>1];
	mdiv = ((reg & 0xff00) >> 8) + 8;
	pdiv = ((reg & 0xfc) >> 2) + 2;
	scale = reg & 3;
	MDIV=mdiv;
	PDIV=pdiv;
	SCALE=scale;
	i = (MEM_REG[0x91c>>1] & 7)+1;
	return ((SYS_CLK_FREQ * mdiv)/(pdiv << scale))/i;
}

unsigned get_freq_940_CLK()
{
	unsigned i;
	unsigned reg,mdiv,pdiv,scale;
	reg=MEM_REG[0x912>>1];
	mdiv = ((reg & 0xff00) >> 8) + 8;
	pdiv = ((reg & 0xfc) >> 2) + 2;
	scale = reg & 3;
	i = ((MEM_REG[0x91c>>1]>>3) & 7)+1;
	return ((SYS_CLK_FREQ * mdiv) / (pdiv << scale))/i;
}

unsigned get_freq_DCLK()
{
	unsigned i;
	unsigned reg,mdiv,pdiv,scale;
	reg=MEM_REG[0x912>>1];
	mdiv = ((reg & 0xff00) >> 8) + 8;
	pdiv = ((reg & 0xfc) >> 2) + 2;
	scale = reg & 3;
	i = ((MEM_REG[0x91c>>1]>>6) & 7)+1;
	return ((SYS_CLK_FREQ * mdiv) / (pdiv << scale))/i;
}

void set_920_Div(unsigned short div)
{
	printf ("set divider for CPU-Clock = %u\r\n",div+1);
	unsigned short v;
	v = MEM_REG[0x91c>>1] & (~0x3);
	MEM_REG[0x91c>>1] = (div & 0x7) | v; 
}

unsigned short get_920_Div()
{
	return (MEM_REG[0x91c>>1] & 0x7); 
}

void set_940_Div(unsigned short div)
{	
	unsigned short v;
	v = (unsigned short)( MEM_REG[0x91c>>1] & (~(0x7 << 3)));
	MEM_REG[0x91c>>1] = ((div & 0x7) << 3) | v; 
}

unsigned short get_940_Div()
{
	return ((MEM_REG[0x91c>>1] >> 3) & 0x7); 
}

void set_DCLK_Div( unsigned short div )
{
	printf ("set divider for RAM-Clock = %u\r\n",div+1);
	unsigned short v;
	v = (unsigned short)( MEM_REG[0x91c>>1] & (~(0x7 << 6)));
	MEM_REG[0x91c>>1] = ((div & 0x7) << 6) | v; 
}

unsigned short get_DCLK_Div()
{
	return ((MEM_REG[0x91c>>1] >> 6) & 0x7); 
}

unsigned short Disable_Int_920()
{
	unsigned short ret;
	ret=MEM_REG[0x3B40>>1];
	MEM_REG[0x3B40>>1]=0;
	MEM_REG[0x3B44>>1]=0xffff;	
	return ret;	
}

unsigned short Disable_Int_940()
{
	unsigned short ret;
	ret=MEM_REG[0x3B42>>1];
	MEM_REG[0x3B42>>1]=0;
	MEM_REG[0x3B46>>1]=0xffff;	
	return ret;	
}

unsigned get_state940()
{
	return MEM_REG[0x904>>1];	
}


void Enable_Int_920(unsigned short flag)
{
	MEM_REG[0x3B40>>1]=flag;
}

void Enable_Int_940(unsigned short flag)
{
	MEM_REG[0x3B42>>1]=flag;
}

void Disable_940()
{
	Disable_Int_940();
	MEM_REG[0x3B48>>1]|= (1 << 7);
	MEM_REG[0x904>>1]&=0xfffe;
}

void Load_940_code(unsigned *code,int size)
{
	unsigned *cp;
	int i;
	arm940code=(unsigned short *)mmap(0, 0x100000, PROT_READ|PROT_WRITE, MAP_SHARED, gp2x_dev[2], 0x03000000);
	Disable_940();
	cp=(unsigned *) code;
	for (i = 0; i < size/4; i ++)
	{
		arm940code[i] = cp[i];
	}
	for (i = 0; i < 64; i ++)
	{
		arm940code[0x3FC0+i] = 0;
	}
	MEM_REG[0x3B48>>1]=(MEM_REG[0x3B48>>1]  & 0xFF00) | 0x03; // allow 940
}

void clock_940_off()
{
	MEM_REG[0x904>>1]&=0xfffe;
}

void clock_940_on()
{
	MEM_REG[0x904>>1]|=1;
}


//--------------
//Memory Timings
//--------------

//get

unsigned get_CAS()
{
	return ((MEM_REG[0x3804>>1] >> 12) & 0x1);	
}

unsigned get_tRC()
{
	return ((MEM_REG[0x3804>>1] >> 8) & 0xF);	
}

unsigned get_tRAS()
{
	return ((MEM_REG[0x3804>>1] >> 4) & 0xF);	
}

unsigned get_tWR()
{
	return (MEM_REG[0x3804>>1] & 0xF);	
}

unsigned get_tMRD()
{
	return ((MEM_REG[0x3802>>1] >> 12) & 0xF);	
}

unsigned get_tRFC()
{
	return ((MEM_REG[0x3802>>1] >> 8) & 0xF);	
}

unsigned get_tRP()
{
	return ((MEM_REG[0x3802>>1] >> 4) & 0xF);	
}

unsigned get_tRCD()
{
	return (MEM_REG[0x3802>>1] & 0xF);	
}

unsigned get_REFPERD()
{
	return MEM_REG[0x3808>>1];	
}


//set

void set_CAS(unsigned short timing)
{
	printf ("set CAS = %u\r\n",timing+2);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3804>>1] & (~(0x1 << 12)));
	MEM_REG[0x3804>>1] = ((timing & 0x1) << 12) | v;	
}

void set_tRC(unsigned short timing)
{
	printf ("set tRC = %u\r\n",timing+1);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3804>>1] & (~(0xF << 8)));
	MEM_REG[0x3804>>1] = ((timing & 0xF) << 8) | v;	
}

void set_tRAS(unsigned short timing)
{
	printf ("set tRAS = %u\r\n",timing+1);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3804>>1] & (~(0xF << 4)));
	MEM_REG[0x3804>>1] = ((timing & 0xF) << 4) | v;	
}

void set_tWR(unsigned short timing)
{	
	printf ("set tWR = %u\r\n",timing+1);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3804>>1] & (~(0xF)));
	MEM_REG[0x3804>>1] = (timing & 0xF) | v;	
}

void set_tMRD(unsigned short timing)
{
	printf ("set tMRD = %u\r\n",timing+1);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3802>>1] & (~(0xF << 12)));
	MEM_REG[0x3802>>1] = ((timing & 0xF) << 12) | v;	
}

void set_tRFC(unsigned short timing)
{
	printf ("set tRFC = %u\r\n",timing+1);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3802>>1] & (~(0xF << 8)));
	MEM_REG[0x3802>>1] = ((timing & 0xF) << 8) | v;	
}

void set_tRP(unsigned short timing)
{
	printf ("set tRP = %u\r\n",timing+1);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3802>>1] & (~(0xF << 4)));
	MEM_REG[0x3802>>1] = ((timing & 0xF) << 4) | v;	
}

void set_tRCD(unsigned short timing)
{
	printf ("set tRCD = %u\r\n",timing+1);
	unsigned short v;
	v = (unsigned short)(MEM_REG[0x3802>>1] & (~(0xF)));
	MEM_REG[0x3802>>1] = (timing & 0xF) | v;	
}

void set_REFPERD(unsigned short timing)
{
	printf ("set Refresh Period = %u\r\n",timing+1);
	MEM_REG[0x3808>>1] = timing;	
}


//-----
//Gamma
//-----

void set_gamma(float gamma)
{
	printf ("set gamma = %f\r\n",gamma);
	int i;
	gamma = 1/gamma;

    //enable gamma
    MEM_REG[0x2880>>1]&=~(1<<12);

    MEM_REG[0x295C>>1]=0;
    for(i=0; i<256; i++)
    {
		unsigned char g;
        unsigned short s;
        g =(unsigned char)(255.0*pow(i/255.0,gamma));
        s = (g<<8) | g;
		MEM_REG[0x295E>>1]= s;
        MEM_REG[0x295E>>1]= g;
    }
}

unsigned get_YBNKLVL()
{
	return (MEM_REG[0x283A>>1] & 0x3FF);	
}

void set_YBNKLVL(unsigned short val)
{
	unsigned short temp = (unsigned short)(MEM_REG[0x3808>>1] & (~(0x3FF)));
	MEM_REG[0x3808>>1] = (val & 0x3FF) | temp;	
}