random fixes (root path and mmap svp tcache)
[libpicofe.git] / gp2x / soc_mmsp2.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <math.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <sys/mman.h>
9#include <unistd.h>
10
11#include "soc.h"
12#include "soc_mmsp2.h"
13#include "plat_gp2x.h"
14#include "../common/emu.h"
15#include "../common/arm_utils.h"
16#include "940ctl.h"
17
18volatile unsigned short *gp2x_memregs;
19volatile unsigned long *gp2x_memregl;
20extern void *gp2x_screens[4];
21static int screensel = 0;
22
23int memdev = -1; /* used by code940 */
24static int touchdev = -1;
25static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
26
27#define FRAMEBUFF_SIZE 0x30000
28#define FRAMEBUFF_WHOLESIZE (FRAMEBUFF_SIZE*4) // 320*240*2 + some more
29#define FRAMEBUFF_ADDR0 (0x4000000 - FRAMEBUFF_WHOLESIZE)
30#define FRAMEBUFF_ADDR1 (FRAMEBUFF_ADDR0 + FRAMEBUFF_SIZE)
31#define FRAMEBUFF_ADDR2 (FRAMEBUFF_ADDR1 + FRAMEBUFF_SIZE)
32#define FRAMEBUFF_ADDR3 (FRAMEBUFF_ADDR2 + FRAMEBUFF_SIZE)
33
34static const int gp2x_screenaddrs[4] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };
35static int gp2x_screenaddrs_use[4];
36
37static char gamma_was_changed = 0;
38static char cpuclk_was_changed = 0;
39static unsigned short gp2x_screenaddr_old[4];
40static unsigned short memtimex_old[2];
41static unsigned short reg0910;
42
43
44/* video stuff */
45static void gp2x_video_flip_(void)
46{
47 unsigned short lsw = (unsigned short) gp2x_screenaddrs_use[screensel&3];
48 unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&3] >> 16);
49
50 gp2x_memregs[0x2910>>1] = msw;
51 gp2x_memregs[0x2914>>1] = msw;
52 gp2x_memregs[0x290E>>1] = lsw;
53 gp2x_memregs[0x2912>>1] = lsw;
54
55 // jump to other buffer:
56 g_screen_ptr = gp2x_screens[++screensel&3];
57}
58
59/* doulblebuffered flip */
60static void gp2x_video_flip2_(void)
61{
62 unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&1] >> 16);
63
64 gp2x_memregs[0x2910>>1] = msw;
65 gp2x_memregs[0x2914>>1] = msw;
66 gp2x_memregs[0x290E>>1] = 0;
67 gp2x_memregs[0x2912>>1] = 0;
68
69 // jump to other buffer:
70 g_screen_ptr = gp2x_screens[++screensel&1];
71}
72
73static void gp2x_video_changemode_ll_(int bpp)
74{
75 gp2x_memregs[0x28DA>>1] = (((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/
76 gp2x_memregs[0x290C>>1] = 320*((bpp+1)/8); /*line width in bytes*/
77}
78
79static void gp2x_video_setpalette_(int *pal, int len)
80{
81 unsigned short *g = (unsigned short *)pal;
82 volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];
83
84 gp2x_memregs[0x2958>>1] = 0;
85
86 len *= 2;
87 while (len--)
88 *memreg = *g++;
89}
90
91static void gp2x_video_RGB_setscaling_(int ln_offs, int W, int H)
92{
93 float escalaw, escalah;
94 int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;
95 unsigned short scalw;
96
97 // set offset
98 gp2x_screenaddrs_use[0] = gp2x_screenaddrs[0] + ln_offs * 320 * bpp;
99 gp2x_screenaddrs_use[1] = gp2x_screenaddrs[1] + ln_offs * 320 * bpp;
100 gp2x_screenaddrs_use[2] = gp2x_screenaddrs[2] + ln_offs * 320 * bpp;
101 gp2x_screenaddrs_use[3] = gp2x_screenaddrs[3] + ln_offs * 320 * bpp;
102
103 escalaw = 1024.0; // RGB Horiz LCD
104 escalah = 320.0; // RGB Vert LCD
105
106 if (gp2x_memregs[0x2800>>1]&0x100) //TV-Out
107 {
108 escalaw=489.0; // RGB Horiz TV (PAL, NTSC)
109 if (gp2x_memregs[0x2818>>1] == 287) //PAL
110 escalah=274.0; // RGB Vert TV PAL
111 else if (gp2x_memregs[0x2818>>1] == 239) //NTSC
112 escalah=331.0; // RGB Vert TV NTSC
113 }
114
115 // scale horizontal
116 scalw = (unsigned short)((float)escalaw *(W/320.0));
117 /* if there is no horizontal scaling, vertical doesn't work.
118 * Here is a nasty wrokaround... */
119 if (H != 240 && W == 320) scalw--;
120 gp2x_memregs[0x2906>>1]=scalw;
121 // scale vertical
122 gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));
123}
124
125static void gp2x_video_wait_vsync_(void)
126{
127 unsigned short v = gp2x_memregs[0x1182>>1];
128 while (!((v ^ gp2x_memregs[0x1182>>1]) & 0x10))
129 spend_cycles(1024);
130}
131
132/* 940 */
133void pause940(int yes)
134{
135 if (yes)
136 gp2x_memregs[0x0904>>1] &= 0xFFFE;
137 else
138 gp2x_memregs[0x0904>>1] |= 1;
139}
140
141void reset940(int yes, int bank)
142{
143 gp2x_memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);
144}
145
146/*
147 * CPU clock
148 * Fout = (m * Fin) / (p * 2^s)
149 * m = MDIV+8, p = PDIV+2, s = SDIV
150 *
151 * m = (Fout * p * 2^s) / Fin
152 */
153
154#define SYS_CLK_FREQ 7372800
155
156static void gp2x_set_cpuclk_(unsigned int mhz)
157{
158 unsigned int mdiv, pdiv, sdiv = 0;
159 unsigned int v;
160 int i;
161
162 pdiv = 3;
163 mdiv = (mhz * pdiv * 1000000) / SYS_CLK_FREQ;
164 if (mdiv & ~0xff) {
165 fprintf(stderr, "invalid cpuclk MHz: %u\n", mhz);
166 return;
167 }
168 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
169 gp2x_memregs[0x910>>1] = v;
170
171 for (i = 0; i < 10000; i++)
172 if (!(gp2x_memregs[0x902>>1] & 1))
173 break;
174
175 cpuclk_was_changed = 1;
176}
177
178/* RAM timings */
179#define TIMING_CHECK(t, adj, mask) \
180 t += adj; \
181 if (t & ~mask) \
182 goto bad
183
184static void set_ram_timing_vals(int tCAS, int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
185{
186 int i;
187 TIMING_CHECK(tCAS, -2, 0x1);
188 TIMING_CHECK(tRC, -1, 0xf);
189 TIMING_CHECK(tRAS, -1, 0xf);
190 TIMING_CHECK(tWR, -1, 0xf);
191 TIMING_CHECK(tMRD, -1, 0xf);
192 TIMING_CHECK(tRFC, -1, 0xf);
193 TIMING_CHECK(tRP, -1, 0xf);
194 TIMING_CHECK(tRCD, -1, 0xf);
195
196 /* get spend_cycles() into cache */
197 spend_cycles(1);
198
199 gp2x_memregs[0x3802>>1] = ((tMRD & 0xF) << 12) | ((tRFC & 0xF) << 8) | ((tRP & 0xF) << 4) | (tRCD & 0xF);
200 gp2x_memregs[0x3804>>1] = 0x8000 | ((tCAS & 1) << 12) | ((tRC & 0xF) << 8) | ((tRAS & 0xF) << 4) | (tWR & 0xF);
201
202 /* be sure we don't access the mem while it's being reprogrammed */
203 spend_cycles(128*1024);
204 for (i = 0; i < 8*1024; i++)
205 if (!(gp2x_memregs[0x3804>>1] & 0x8000))
206 break;
207
208 printf("RAM timings set.\n");
209 return;
210bad:
211 fprintf(stderr, "RAM timings invalid.\n");
212}
213
214static void set_ram_timings_(void)
215{
216 /* craigix: --cas 2 --trc 6 --tras 4 --twr 1 --tmrd 1 --trfc 1 --trp 2 --trcd 2 */
217 set_ram_timing_vals(2, 6, 4, 1, 1, 1, 2, 2);
218}
219
220static void unset_ram_timings_(void)
221{
222 gp2x_memregs[0x3802>>1] = memtimex_old[0];
223 gp2x_memregs[0x3804>>1] = memtimex_old[1] | 0x8000;
224 printf("RAM timings reset to startup values.\n");
225}
226
227/* LCD refresh */
228typedef struct
229{
230 unsigned short reg, valmask, val;
231}
232reg_setting;
233
234/* 120.00 97/0/2/7|25/ 7/ 7/11/37 */
235static const reg_setting lcd_rate_120[] =
236{
237 { 0x0914, 0xffff, (97<<8)|(0<<2)|2 }, /* UPLLSETVREG */
238 { 0x0924, 0xff00, (2<<14)|(7<<8) }, /* DISPCSETREG */
239 { 0x281A, 0x00ff, 25 }, /* .HSWID(T2) */
240 { 0x281C, 0x00ff, 7 }, /* .HSSTR(T8) */
241 { 0x281E, 0x00ff, 7 }, /* .HSEND(T7) */
242 { 0x2822, 0x01ff, 11 }, /* .VSEND (T9) */
243 { 0x2826, 0x0ff0, 37<<4 }, /* .DESTR(T3) */
244 { 0, 0, 0 }
245};
246
247/* 100.00 96/0/2/7|29/25/53/15/37 */
248static const reg_setting lcd_rate_100[] =
249{
250 { 0x0914, 0xffff, (96<<8)|(0<<2)|2 }, /* UPLLSETVREG */
251 { 0x0924, 0xff00, (2<<14)|(7<<8) }, /* DISPCSETREG */
252 { 0x281A, 0x00ff, 29 }, /* .HSWID(T2) */
253 { 0x281C, 0x00ff, 25 }, /* .HSSTR(T8) */
254 { 0x281E, 0x00ff, 53 }, /* .HSEND(T7) */
255 { 0x2822, 0x01ff, 15 }, /* .VSEND (T9) */
256 { 0x2826, 0x0ff0, 37<<4 }, /* .DESTR(T3) */
257 { 0, 0, 0 }
258};
259
260static reg_setting lcd_rate_defaults[] =
261{
262 { 0x0914, 0xffff, 0 },
263 { 0x0924, 0xff00, 0 },
264 { 0x281A, 0x00ff, 0 },
265 { 0x281C, 0x00ff, 0 },
266 { 0x281E, 0x00ff, 0 },
267 { 0x2822, 0x01ff, 0 },
268 { 0x2826, 0x0ff0, 0 },
269 { 0, 0, 0 }
270};
271
272static void get_reg_setting(reg_setting *set)
273{
274 for (; set->reg; set++)
275 {
276 unsigned short val = gp2x_memregs[set->reg >> 1];
277 val &= set->valmask;
278 set->val = val;
279 }
280}
281
282static void set_reg_setting(const reg_setting *set)
283{
284 for (; set->reg; set++)
285 {
286 unsigned short val = gp2x_memregs[set->reg >> 1];
287 val &= ~set->valmask;
288 val |= set->val;
289 gp2x_memregs[set->reg >> 1] = val;
290 }
291}
292
293static void set_lcd_custom_rate_(int is_pal)
294{
295 if (gp2x_memregs[0x2800>>1] & 0x100) // tv-out
296 return;
297
298 printf("setting custom LCD refresh (%d Hz)... ", is_pal ? 100 : 120);
299 fflush(stdout);
300
301 set_reg_setting(is_pal ? lcd_rate_100 : lcd_rate_120);
302 printf("done.\n");
303}
304
305static void unset_lcd_custom_rate_(void)
306{
307 printf("reset to prev LCD refresh.\n");
308 set_reg_setting(lcd_rate_defaults);
309}
310
311static void set_lcd_gamma_(int g100, int A_SNs_curve)
312{
313 float gamma = (float) g100 / 100;
314 int i;
315 gamma = 1 / gamma;
316
317 /* enable gamma */
318 gp2x_memregs[0x2880>>1] &= ~(1<<12);
319
320 gp2x_memregs[0x295C>>1] = 0;
321 for (i = 0; i < 256; i++)
322 {
323 unsigned char g;
324 unsigned short s;
325 const unsigned short grey50=143, grey75=177, grey25=97;
326 double blah;
327
328 if (A_SNs_curve)
329 {
330 // The next formula is all about gaussian interpolation
331 blah = (( -128 * exp(-powf((float) i/64.0f + 2.0f , 2.0f))) +
332 ( -64 * exp(-powf((float) i/64.0f + 1.0f , 2.0f))) +
333 (grey25 * exp(-powf((float) i/64.0f - 1.0f , 2.0f))) +
334 (grey50 * exp(-powf((float) i/64.0f - 2.0f , 2.0f))) +
335 (grey75 * exp(-powf((float) i/64.0f - 3.0f , 2.0f))) +
336 ( 256 * exp(-powf((float) i/64.0f - 4.0f , 2.0f))) +
337 ( 320 * exp(-powf((float) i/64.0f - 5.0f , 2.0f))) +
338 ( 384 * exp(-powf((float) i/64.0f - 6.0f , 2.0f)))) / 1.772637;
339 blah += 0.5;
340 }
341 else
342 {
343 blah = i;
344 }
345
346 g = (unsigned char)(255.0 * pow(blah/255.0, gamma));
347 //printf("%d : %d\n", i, g);
348 s = (g<<8) | g;
349 gp2x_memregs[0x295E>>1]= s;
350 gp2x_memregs[0x295E>>1]= g;
351 }
352
353 gamma_was_changed = 1;
354}
355
356
357/* these are not quite MMSP2 related,
358 * more to GP2X F100/F200 consoles themselves. */
359typedef struct ucb1x00_ts_event
360{
361 unsigned short pressure;
362 unsigned short x;
363 unsigned short y;
364 unsigned short pad;
365 struct timeval stamp;
366} UCB1X00_TS_EVENT;
367
368int gp2x_touchpad_read(int *x, int *y)
369{
370 UCB1X00_TS_EVENT event;
371 static int zero_seen = 0;
372 int retval;
373
374 if (touchdev < 0) return -1;
375
376 retval = read(touchdev, &event, sizeof(event));
377 if (retval <= 0) {
378 perror("touch read failed");
379 return -1;
380 }
381 // this is to ignore the messed-up 4.1.x driver
382 if (event.pressure == 0) zero_seen = 1;
383
384 if (x) *x = (event.x * touchcal[0] + touchcal[2]) >> 16;
385 if (y) *y = (event.y * touchcal[4] + touchcal[5]) >> 16;
386 // printf("read %i %i %i\n", event.pressure, *x, *y);
387
388 return zero_seen ? event.pressure : 0;
389}
390
391static void proc_set(const char *path, const char *val)
392{
393 FILE *f;
394 char tmp[16];
395
396 f = fopen(path, "w");
397 if (f == NULL) {
398 printf("failed to open: %s\n", path);
399 return;
400 }
401
402 fprintf(f, "0\n");
403 fclose(f);
404
405 printf("\"%s\" is set to: ", path);
406 f = fopen(path, "r");
407 if (f == NULL) {
408 printf("(open failed)\n");
409 return;
410 }
411
412 fgets(tmp, sizeof(tmp), f);
413 printf("%s", tmp);
414 fclose(f);
415}
416
417
418void mmsp2_init(void)
419{
420 int i;
421
422 memdev = open("/dev/mem", O_RDWR);
423 if (memdev == -1)
424 {
425 perror("open(\"/dev/mem\")");
426 exit(1);
427 }
428
429 gp2x_memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
430 if (gp2x_memregs == MAP_FAILED)
431 {
432 perror("mmap(memregs)");
433 exit(1);
434 }
435 gp2x_memregl = (unsigned long *) gp2x_memregs;
436
437 gp2x_memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers
438
439 gp2x_screens[0] = mmap(0, FRAMEBUFF_WHOLESIZE, PROT_WRITE, MAP_SHARED,
440 memdev, gp2x_screenaddrs[0]);
441 if (gp2x_screens[0] == MAP_FAILED)
442 {
443 perror("mmap(g_screen_ptr)");
444 exit(1);
445 }
446 printf("framebuffers:\n");
447 printf(" %08x -> %p\n", gp2x_screenaddrs[0], gp2x_screens[0]);
448 for (i = 1; i < 4; i++)
449 {
450 gp2x_screens[i] = (char *) gp2x_screens[i - 1] + FRAMEBUFF_SIZE;
451 printf(" %08x -> %p\n", gp2x_screenaddrs[i], gp2x_screens[i]);
452 }
453
454 g_screen_ptr = gp2x_screens[0];
455 screensel = 0;
456
457 gp2x_screenaddr_old[0] = gp2x_memregs[0x290E>>1];
458 gp2x_screenaddr_old[1] = gp2x_memregs[0x2910>>1];
459 gp2x_screenaddr_old[2] = gp2x_memregs[0x2912>>1];
460 gp2x_screenaddr_old[3] = gp2x_memregs[0x2914>>1];
461
462 memcpy(gp2x_screenaddrs_use, gp2x_screenaddrs, sizeof(gp2x_screenaddrs));
463
464 /* save startup values: LCD refresh */
465 get_reg_setting(lcd_rate_defaults);
466
467 /* CPU and RAM timings */
468 reg0910 = gp2x_memregs[0x0910>>1];
469 memtimex_old[0] = gp2x_memregs[0x3802>>1];
470 memtimex_old[1] = gp2x_memregs[0x3804>>1];
471
472 /* touchscreen */
473 touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);
474 if (touchdev >= 0) {
475 FILE *pcf = fopen("/etc/pointercal", "r");
476 if (pcf) {
477 fscanf(pcf, "%d %d %d %d %d %d %d", &touchcal[0], &touchcal[1],
478 &touchcal[2], &touchcal[3], &touchcal[4], &touchcal[5], &touchcal[6]);
479 fclose(pcf);
480 }
481 printf("found touchscreen/wm97xx\n");
482 }
483
484 /* disable Linux read-ahead */
485 proc_set("/proc/sys/vm/max-readahead", "0\n");
486 proc_set("/proc/sys/vm/min-readahead", "0\n");
487
488 /* code940 portion */
489 sharedmem940_init();
490
491 gp2x_video_flip = gp2x_video_flip_;
492 gp2x_video_flip2 = gp2x_video_flip2_;
493 gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
494 gp2x_video_setpalette = gp2x_video_setpalette_;
495 gp2x_video_RGB_setscaling = gp2x_video_RGB_setscaling_;
496 gp2x_video_wait_vsync = gp2x_video_wait_vsync_;
497
498 gp2x_set_cpuclk = gp2x_set_cpuclk_;
499
500 set_lcd_custom_rate = set_lcd_custom_rate_;
501 unset_lcd_custom_rate = unset_lcd_custom_rate_;
502 set_lcd_gamma = set_lcd_gamma_;
503
504 set_ram_timings = set_ram_timings_;
505 unset_ram_timings = unset_ram_timings_;
506}
507
508void mmsp2_finish(void)
509{
510 reset940(1, 3);
511 pause940(1);
512 sharedmem940_finish();
513
514 gp2x_video_RGB_setscaling_(0, 320, 240);
515 gp2x_video_changemode_ll_(16);
516
517 gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];
518 gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];
519 gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];
520 gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];
521
522 unset_lcd_custom_rate_();
523 if (gamma_was_changed)
524 set_lcd_gamma_(100, 0);
525 unset_ram_timings_();
526 if (cpuclk_was_changed)
527 gp2x_memregs[0x910>>1] = reg0910;
528
529 munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);
530 munmap((void *)gp2x_memregs, 0x10000);
531 close(memdev);
532 if (touchdev >= 0)
533 close(touchdev);
534}
535