gp2x+wiz binary support, wiz code wip
[libpicofe.git] / gp2x / soc.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <sys/mman.h>
7 #include <unistd.h>
8
9 #include "soc.h"
10
11 void (*gp2x_video_flip)(void);
12 void (*gp2x_video_flip2)(void);
13 void (*gp2x_video_changemode_ll)(int bpp);
14 void (*gp2x_video_setpalette)(int *pal, int len);
15 void (*gp2x_video_RGB_setscaling)(int ln_offs, int W, int H);
16 void (*gp2x_video_wait_vsync)(void);
17
18 void (*gp2x_set_cpuclk)(unsigned int mhz);
19
20 void (*set_lcd_custom_rate)(int is_pal);
21 void (*unset_lcd_custom_rate)(void);
22 void (*set_lcd_gamma)(int g100, int A_SNs_curve);
23
24 void (*set_ram_timings)(void);
25 void (*unset_ram_timings)(void);
26
27
28 gp2x_soc_t soc_detect(void)
29 {
30         volatile unsigned short *memregs;
31         volatile unsigned int *memregl;
32         static gp2x_soc_t ret = -1;
33         int pollux_chipname[0x30/4 + 1];
34         char *pollux_chipname_c = (char *)pollux_chipname;
35         int memdev;
36         int i;
37
38         if (ret != -1)
39                 /* already detected */
40                 return ret;
41
42         memdev = open("/dev/mem", O_RDONLY);
43         if (memdev == -1)
44         {
45                 perror("open(/dev/mem)");
46                 return -1;
47         }
48
49         memregs = mmap(0, 0x20000, PROT_READ, MAP_SHARED, memdev, 0xc0000000);
50         if (memregs == MAP_FAILED)
51         {
52                 perror("mmap(memregs)");
53                 close(memdev);
54                 return -1;
55         }
56         memregl = (volatile void *)memregs;
57
58         if (memregs[0x1836>>1] == 0x2330)
59         {
60                 printf("looks like this is MMSP2\n");
61                 ret = SOCID_MMSP2;
62                 goto out;
63         }
64
65         /* perform word reads. Byte reads might also work,
66          * but we don't want to play with that. */
67         for (i = 0; i < 0x30; i += 4)
68         {
69                 pollux_chipname[i >> 2] = memregl[(0x1f810 + i) >> 2];
70         }
71         pollux_chipname_c[0x30] = 0;
72
73         for (i = 0; i < 0x30; i++)
74         {
75                 unsigned char c = pollux_chipname_c[i];
76                 if (c < 0x20 || c > 0x7f)
77                         goto not_pollux_like;
78         }
79
80         printf("found pollux-like id: \"%s\"\n", pollux_chipname_c);
81
82         if (strncmp(pollux_chipname_c, "MAGICEYES-LEAPFROG-LF1000", 25) ||
83                 strncmp(pollux_chipname_c, "MAGICEYES-POLLUX", 16))
84         {
85                 ret = SOCID_POLLUX;
86                 goto out;
87         }
88
89 not_pollux_like:
90 out:
91         munmap((void *)memregs, 0x20000);
92         close(memdev);
93         return ret;     
94 }
95