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