start splitting plat menu code; batt + clock in menu; bugfix
[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 int  (*gp2x_read_battery)(void);
27
28
29 gp2x_soc_t soc_detect(void)
30 {
31         volatile unsigned short *memregs;
32         volatile unsigned int *memregl;
33         static gp2x_soc_t ret = -1;
34         int pollux_chipname[0x30/4 + 1];
35         char *pollux_chipname_c = (char *)pollux_chipname;
36         int memdev;
37         int i;
38
39         if (ret != -1)
40                 /* already detected */
41                 return ret;
42
43         memdev = open("/dev/mem", O_RDONLY);
44         if (memdev == -1)
45         {
46                 perror("open(/dev/mem)");
47                 return -1;
48         }
49
50         memregs = mmap(0, 0x20000, PROT_READ, MAP_SHARED, memdev, 0xc0000000);
51         if (memregs == MAP_FAILED)
52         {
53                 perror("mmap(memregs)");
54                 close(memdev);
55                 return -1;
56         }
57         memregl = (volatile void *)memregs;
58
59         if (memregs[0x1836>>1] == 0x2330)
60         {
61                 printf("looks like this is MMSP2\n");
62                 ret = SOCID_MMSP2;
63                 goto out;
64         }
65
66         /* perform word reads. Byte reads might also work,
67          * but we don't want to play with that. */
68         for (i = 0; i < 0x30; i += 4)
69         {
70                 pollux_chipname[i >> 2] = memregl[(0x1f810 + i) >> 2];
71         }
72         pollux_chipname_c[0x30] = 0;
73
74         for (i = 0; i < 0x30; i++)
75         {
76                 unsigned char c = pollux_chipname_c[i];
77                 if (c < 0x20 || c > 0x7f)
78                         goto not_pollux_like;
79         }
80
81         printf("found pollux-like id: \"%s\"\n", pollux_chipname_c);
82
83         if (strncmp(pollux_chipname_c, "MAGICEYES-LEAPFROG-LF1000", 25) ||
84                 strncmp(pollux_chipname_c, "MAGICEYES-POLLUX", 16))
85         {
86                 ret = SOCID_POLLUX;
87                 goto out;
88         }
89
90 not_pollux_like:
91 out:
92         munmap((void *)memregs, 0x20000);
93         close(memdev);
94         return ret;     
95 }
96