| 1 | /* Parts used from cpuctrl */ |
| 2 | /* cpuctrl for GP2X |
| 3 | Copyright (C) 2005 Hermes/PS2Reality |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #define _BSD_SOURCE |
| 23 | #define _GNU_SOURCE |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/soundcard.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | #include "../common.h" |
| 30 | #include "gp2x.h" |
| 31 | #include "warm.h" |
| 32 | |
| 33 | u32 gp2x_audio_volume = 74/2; |
| 34 | u32 gpsp_gp2x_dev_audio = 0; |
| 35 | u32 gpsp_gp2x_dev = 0; |
| 36 | u32 gpsp_gp2x_gpiodev = 0; |
| 37 | |
| 38 | static volatile u16 *gpsp_gp2x_memregs; |
| 39 | static volatile u32 *gpsp_gp2x_memregl; |
| 40 | |
| 41 | #ifdef WIZ_BUILD |
| 42 | #include <linux/fb.h> |
| 43 | void *gpsp_gp2x_screen; |
| 44 | static u32 fb_paddr[3]; |
| 45 | static void *fb_vaddr[3]; |
| 46 | static u32 fb_work_buf; |
| 47 | const int fb_buf_count = 3; |
| 48 | static int fb_buf_use = 3; |
| 49 | |
| 50 | static void fb_video_init() |
| 51 | { |
| 52 | struct fb_fix_screeninfo fbfix; |
| 53 | int i, ret; |
| 54 | int fbdev; |
| 55 | |
| 56 | fbdev = open("/dev/fb0", O_RDWR); |
| 57 | if (fbdev < 0) { |
| 58 | perror("can't open fbdev"); |
| 59 | exit(1); |
| 60 | } |
| 61 | |
| 62 | ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix); |
| 63 | if (ret == -1) |
| 64 | { |
| 65 | perror("ioctl(fbdev) failed"); |
| 66 | exit(1); |
| 67 | } |
| 68 | |
| 69 | printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start); |
| 70 | fb_paddr[0] = fbfix.smem_start; |
| 71 | close(fbdev); |
| 72 | |
| 73 | fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE, |
| 74 | MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]); |
| 75 | if (fb_vaddr[0] == MAP_FAILED) |
| 76 | { |
| 77 | perror("mmap(fb_vaddr) failed"); |
| 78 | exit(1); |
| 79 | } |
| 80 | memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count); |
| 81 | |
| 82 | printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]); |
| 83 | for (i = 1; i < fb_buf_count; i++) |
| 84 | { |
| 85 | fb_paddr[i] = fb_paddr[i-1] + 320*240*2; |
| 86 | fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2; |
| 87 | printf(" %p -> %08x\n", fb_vaddr[i], fb_paddr[i]); |
| 88 | } |
| 89 | fb_work_buf = 0; |
| 90 | fb_buf_use = fb_buf_count; |
| 91 | |
| 92 | pollux_video_flip(); |
| 93 | warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1); |
| 94 | } |
| 95 | |
| 96 | void pollux_video_flip() |
| 97 | { |
| 98 | warm_cache_op_all(WOP_D_CLEAN); |
| 99 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf]; |
| 100 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
| 101 | fb_work_buf++; |
| 102 | if (fb_work_buf >= fb_buf_use) |
| 103 | fb_work_buf = 0; |
| 104 | gpsp_gp2x_screen = fb_vaddr[fb_work_buf]; |
| 105 | } |
| 106 | |
| 107 | void fb_use_buffers(int count) |
| 108 | { |
| 109 | if (count < 1) |
| 110 | count = 1; |
| 111 | else if (count > fb_buf_count) |
| 112 | count = fb_buf_count; |
| 113 | fb_buf_use = count; |
| 114 | } |
| 115 | |
| 116 | static void fb_video_exit() |
| 117 | { |
| 118 | /* switch to default fb mem */ |
| 119 | gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0]; |
| 120 | gpsp_gp2x_memregl[0x4058>>2] |= 0x10; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | void gp2x_init() |
| 125 | { |
| 126 | FILE *f; |
| 127 | gpsp_gp2x_dev = open("/dev/mem", O_RDWR); |
| 128 | gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR); |
| 129 | gpsp_gp2x_memregl = |
| 130 | (unsigned long *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 131 | gpsp_gp2x_dev, 0xc0000000); |
| 132 | gpsp_gp2x_memregs = (unsigned short *)gpsp_gp2x_memregl; |
| 133 | warm_init(); |
| 134 | #ifdef WIZ_BUILD |
| 135 | gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY); |
| 136 | fb_video_init(); |
| 137 | #endif |
| 138 | |
| 139 | f = fopen("romdir.txt", "r"); |
| 140 | if (f != NULL) |
| 141 | { |
| 142 | char buff[256]; |
| 143 | char *s = fgets(buff, sizeof(buff) - 1, f); |
| 144 | if (s) |
| 145 | { |
| 146 | int r = strlen(s); |
| 147 | while (r > 0 && isspace(buff[r-1])) |
| 148 | buff[--r] = 0; |
| 149 | chdir(buff); |
| 150 | } |
| 151 | fclose(f); |
| 152 | } |
| 153 | |
| 154 | gp2x_sound_volume(1); |
| 155 | } |
| 156 | |
| 157 | #include <errno.h> |
| 158 | void gp2x_quit() |
| 159 | { |
| 160 | char buff[256]; |
| 161 | FILE *f; |
| 162 | |
| 163 | getcwd(buff, sizeof(buff)); |
| 164 | chdir(main_path); |
| 165 | f = fopen("romdir.txt", "r+"); |
| 166 | if (f != NULL) |
| 167 | { |
| 168 | fprintf(f, "%s", buff); |
| 169 | fclose(f); |
| 170 | truncate("romdir.txt", strlen(buff)); |
| 171 | sync(); |
| 172 | } |
| 173 | |
| 174 | warm_finish(); |
| 175 | #ifdef WIZ_BUILD |
| 176 | close(gpsp_gp2x_gpiodev); |
| 177 | fb_video_exit(); |
| 178 | #endif |
| 179 | munmap((void *)gpsp_gp2x_memregl, 0x10000); |
| 180 | close(gpsp_gp2x_dev_audio); |
| 181 | close(gpsp_gp2x_dev); |
| 182 | |
| 183 | fcloseall(); |
| 184 | //chdir("/usr/gp2x"); |
| 185 | //execl("gp2xmenu", "gp2xmenu", NULL); |
| 186 | exit(0); |
| 187 | } |
| 188 | |
| 189 | void gp2x_sound_volume(u32 volume_up) |
| 190 | { |
| 191 | u32 volume; |
| 192 | if((volume_up == 0) && (gp2x_audio_volume > 0)) |
| 193 | gp2x_audio_volume--; |
| 194 | |
| 195 | if((volume_up != 0) && (gp2x_audio_volume < 100)) |
| 196 | gp2x_audio_volume++; |
| 197 | |
| 198 | volume = (gp2x_audio_volume * 0x50) / 100; |
| 199 | volume = (gp2x_audio_volume << 8) | gp2x_audio_volume; |
| 200 | ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume); |
| 201 | } |
| 202 | |
| 203 | u32 gpsp_gp2x_joystick_read(void) |
| 204 | { |
| 205 | #ifdef WIZ_BUILD |
| 206 | u32 value = 0; |
| 207 | read(gpsp_gp2x_gpiodev, &value, 4); |
| 208 | if(value & 0x02) |
| 209 | value |= 0x05; |
| 210 | if(value & 0x08) |
| 211 | value |= 0x14; |
| 212 | if(value & 0x20) |
| 213 | value |= 0x50; |
| 214 | if(value & 0x80) |
| 215 | value |= 0x41; |
| 216 | return value; |
| 217 | #else |
| 218 | u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF); |
| 219 | |
| 220 | if(value == 0xFD) |
| 221 | value = 0xFA; |
| 222 | if(value == 0xF7) |
| 223 | value = 0xEB; |
| 224 | if(value == 0xDF) |
| 225 | value = 0xAF; |
| 226 | if(value == 0x7F) |
| 227 | value = 0xBE; |
| 228 | |
| 229 | return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value | |
| 230 | (gpsp_gp2x_memregs[0x1186 >> 1] << 16)); |
| 231 | #endif |
| 232 | } |
| 233 | |
| 234 | // Fout = (m * Fin) / (p * 2^s) |
| 235 | void set_FCLK(u32 MHZ) |
| 236 | { |
| 237 | u32 v; |
| 238 | u32 mdiv, pdiv, sdiv = 0; |
| 239 | #ifdef WIZ_BUILD |
| 240 | #define SYS_CLK_FREQ 27 |
| 241 | // m = MDIV, p = PDIV, s = SDIV |
| 242 | pdiv = 9; |
| 243 | mdiv = (MHZ * pdiv) / SYS_CLK_FREQ; |
| 244 | mdiv &= 0x3ff; |
| 245 | v = (pdiv<<18) | (mdiv<<8) | sdiv; |
| 246 | |
| 247 | gpsp_gp2x_memregl[0xf004>>2] = v; |
| 248 | gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000; |
| 249 | #else |
| 250 | #define SYS_CLK_FREQ 7372800 |
| 251 | // m = MDIV + 8, p = PDIV + 2, s = SDIV |
| 252 | pdiv = 3; |
| 253 | mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ; |
| 254 | mdiv &= 0xff; |
| 255 | v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv; |
| 256 | gpsp_gp2x_memregs[0x910>>1] = v; |
| 257 | #endif |
| 258 | } |
| 259 | |