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