| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2009-2010 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 6 | * - GNU GPL, version 2 or later. |
| 7 | * - GNU LGPL, version 2.1 or later. |
| 8 | * - MAME license. |
| 9 | * See the COPYING file in the top-level directory. |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include "soc.h" |
| 21 | |
| 22 | void (*gp2x_video_flip)(void); |
| 23 | void (*gp2x_video_flip2)(void); |
| 24 | void (*gp2x_video_changemode_ll)(int bpp); |
| 25 | void (*gp2x_video_setpalette)(int *pal, int len); |
| 26 | void (*gp2x_video_RGB_setscaling)(int ln_offs, int W, int H); |
| 27 | void (*gp2x_video_wait_vsync)(void); |
| 28 | |
| 29 | void (*gp2x_set_cpuclk)(unsigned int mhz); |
| 30 | |
| 31 | void (*set_lcd_custom_rate)(int is_pal); |
| 32 | void (*unset_lcd_custom_rate)(void); |
| 33 | void (*set_lcd_gamma)(int g100, int A_SNs_curve); |
| 34 | |
| 35 | void (*set_ram_timings)(void); |
| 36 | void (*unset_ram_timings)(void); |
| 37 | int (*gp2x_read_battery)(void); |
| 38 | |
| 39 | unsigned int (*gp2x_get_ticks_ms)(void); |
| 40 | unsigned int (*gp2x_get_ticks_us)(void); |
| 41 | |
| 42 | |
| 43 | gp2x_soc_t soc_detect(void) |
| 44 | { |
| 45 | volatile unsigned short *memregs; |
| 46 | volatile unsigned int *memregl; |
| 47 | static gp2x_soc_t ret = -2; |
| 48 | int pollux_chipname[0x30/4 + 1]; |
| 49 | char *pollux_chipname_c = (char *)pollux_chipname; |
| 50 | int memdev; |
| 51 | int i; |
| 52 | |
| 53 | if ((int)ret != -2) |
| 54 | /* already detected */ |
| 55 | return ret; |
| 56 | |
| 57 | memdev = open("/dev/mem", O_RDONLY); |
| 58 | if (memdev == -1) |
| 59 | { |
| 60 | perror("open(/dev/mem)"); |
| 61 | ret = -1; |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | memregs = mmap(0, 0x20000, PROT_READ, MAP_SHARED, memdev, 0xc0000000); |
| 66 | if (memregs == MAP_FAILED) |
| 67 | { |
| 68 | perror("mmap(memregs)"); |
| 69 | close(memdev); |
| 70 | ret = -1; |
| 71 | return -1; |
| 72 | } |
| 73 | memregl = (volatile void *)memregs; |
| 74 | |
| 75 | if (memregs[0x1836>>1] == 0x2330) |
| 76 | { |
| 77 | printf("looks like this is MMSP2\n"); |
| 78 | ret = SOCID_MMSP2; |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | /* perform word reads. Byte reads might also work, |
| 83 | * but we don't want to play with that. */ |
| 84 | for (i = 0; i < 0x30; i += 4) |
| 85 | { |
| 86 | pollux_chipname[i >> 2] = memregl[(0x1f810 + i) >> 2]; |
| 87 | } |
| 88 | pollux_chipname_c[0x30] = 0; |
| 89 | |
| 90 | for (i = 0; i < 0x30; i++) |
| 91 | { |
| 92 | unsigned char c = pollux_chipname_c[i]; |
| 93 | if (c < 0x20 || c > 0x7f) |
| 94 | goto not_pollux_like; |
| 95 | } |
| 96 | |
| 97 | printf("found pollux-like id: \"%s\"\n", pollux_chipname_c); |
| 98 | |
| 99 | if (strncmp(pollux_chipname_c, "MAGICEYES-LEAPFROG-LF1000", 25) || |
| 100 | strncmp(pollux_chipname_c, "MAGICEYES-POLLUX", 16)) |
| 101 | { |
| 102 | ret = SOCID_POLLUX; |
| 103 | goto out; |
| 104 | } |
| 105 | |
| 106 | not_pollux_like: |
| 107 | out: |
| 108 | munmap((void *)memregs, 0x20000); |
| 109 | close(memdev); |
| 110 | return ret; |
| 111 | } |
| 112 | |