soc.c 2.1 KB

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