pollux_dpc_set integrated
[gpsp.git] / gp2x / gp2x.c
CommitLineData
2823a4c8 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
638cc626 22#define _BSD_SOURCE
23#define _GNU_SOURCE
2823a4c8 24#include <sys/mman.h>
25#include <sys/ioctl.h>
26#include <sys/soundcard.h>
638cc626 27#include <sys/types.h>
28#include <unistd.h>
2823a4c8 29#include "../common.h"
30#include "gp2x.h"
4742480d 31#include "warm.h"
e8f5db5d 32#include "pollux_dpc_set.h"
2823a4c8 33
90206450 34u32 gp2x_audio_volume = 74/2;
2823a4c8 35u32 gpsp_gp2x_dev_audio = 0;
36u32 gpsp_gp2x_dev = 0;
4742480d 37u32 gpsp_gp2x_gpiodev = 0;
2823a4c8 38
4742480d 39static volatile u16 *gpsp_gp2x_memregs;
40static volatile u32 *gpsp_gp2x_memregl;
2823a4c8 41
4cdfc0bc 42#ifdef WIZ_BUILD
43#include <linux/fb.h>
44void *gpsp_gp2x_screen;
5c6e71a0 45#define fb_buf_count 4
46static u32 fb_paddr[fb_buf_count];
47static void *fb_vaddr[fb_buf_count];
4cdfc0bc 48static u32 fb_work_buf;
5c6e71a0 49static int fb_buf_use;
42c81190 50static int fbdev;
4cdfc0bc 51
52static void fb_video_init()
53{
54 struct fb_fix_screeninfo fbfix;
55 int i, ret;
4cdfc0bc 56
57 fbdev = open("/dev/fb0", O_RDWR);
58 if (fbdev < 0) {
59 perror("can't open fbdev");
60 exit(1);
61 }
62
63 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
64 if (ret == -1)
65 {
66 perror("ioctl(fbdev) failed");
67 exit(1);
68 }
69
70 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
71 fb_paddr[0] = fbfix.smem_start;
4cdfc0bc 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
96void pollux_video_flip()
97{
4cdfc0bc 98 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf];
99 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
100 fb_work_buf++;
101 if (fb_work_buf >= fb_buf_use)
102 fb_work_buf = 0;
103 gpsp_gp2x_screen = fb_vaddr[fb_work_buf];
104}
105
106void fb_use_buffers(int count)
107{
108 if (count < 1)
109 count = 1;
110 else if (count > fb_buf_count)
111 count = fb_buf_count;
112 fb_buf_use = count;
5c6e71a0 113 memset(fb_vaddr[0], 0, 320*240*2*count);
4cdfc0bc 114}
115
42c81190 116void wiz_lcd_set_portrait(int y)
117{
118 static int old_y = -1;
119 int cmd[2] = { 0, 0 };
120
121 if (old_y == y)
122 return;
123 cmd[0] = y ? 6 : 5;
124 ioctl(fbdev, _IOW('D', 90, int[2]), cmd);
125 gpsp_gp2x_memregl[0x4004>>2] = y ? 0x013f00ef : 0x00ef013f;
126 gpsp_gp2x_memregl[0x4000>>2] |= 1 << 3;
127 old_y = y;
e8f5db5d 128
129 /* the above ioctl resets LCD timings, so set them here */
130 pollux_dpc_set(gpsp_gp2x_memregs, getenv("pollux_dpc_set"));
42c81190 131}
132
4cdfc0bc 133static void fb_video_exit()
134{
42c81190 135 /* switch to default fb mem, turn portrait off */
4cdfc0bc 136 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0];
137 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
42c81190 138 wiz_lcd_set_portrait(0);
139 close(fbdev);
140}
141
142static int wiz_gamepak_fd = -1;
143static u32 wiz_gamepak_size;
144
145static void wiz_gamepak_cleanup()
146{
147 if (wiz_gamepak_size)
148 munmap(gamepak_rom, wiz_gamepak_size);
149 if (wiz_gamepak_fd >= 0)
150 close(wiz_gamepak_fd);
151 gamepak_rom = NULL;
152 wiz_gamepak_size = 0;
153 wiz_gamepak_fd = -1;
4cdfc0bc 154}
42c81190 155
156u32 wiz_load_gamepak(char *name)
157{
158 char *dot_position = strrchr(name, '.');
159 u32 ret;
160
161 if (!strcasecmp(dot_position, ".zip"))
162 {
163 if (wiz_gamepak_fd >= 0)
164 {
165 wiz_gamepak_cleanup();
166 printf("switching to ROM malloc\n");
167 init_gamepak_buffer();
168 }
169 return load_file_zip(name);
170 }
171
172 if (wiz_gamepak_fd < 0)
173 {
174 extern void *gamepak_memory_map;
175 free(gamepak_rom);
176 free(gamepak_memory_map);
177 gamepak_memory_map = NULL;
178 printf("switching to ROM mmap\n");
179 }
180 else
181 wiz_gamepak_cleanup();
182
183 wiz_gamepak_fd = open(name, O_RDONLY|O_NOATIME, S_IRUSR);
184 if (wiz_gamepak_fd < 0)
185 {
186 perror("wiz_load_gamepak: open failed");
187 return -1;
188 }
189
190 ret = lseek(wiz_gamepak_fd, 0, SEEK_END);
191 wiz_gamepak_size = gamepak_ram_buffer_size = ret;
192
193 gamepak_rom = mmap(0, ret, PROT_READ, MAP_SHARED, wiz_gamepak_fd, 0);
194 if (gamepak_rom == MAP_FAILED)
195 {
196 perror("wiz_load_gamepak: mmap failed");
197 return -1;
198 }
199
200 return ret;
201}
202
4cdfc0bc 203#endif
204
5a01fba6 205static int get_romdir(char *buff, size_t size)
2823a4c8 206{
638cc626 207 FILE *f;
5a01fba6 208 char *s;
209 int r = -1;
210
211 f = fopen("romdir.txt", "r");
212 if (f == NULL)
213 return -1;
214
215 s = fgets(buff, size, f);
216 if (s)
217 {
218 r = strlen(s);
219 while (r > 0 && isspace(buff[r-1]))
220 buff[--r] = 0;
221 }
222
223 fclose(f);
224 return r;
225}
226
227void gp2x_init()
228{
229 char buff[256];
230
2823a4c8 231 gpsp_gp2x_dev = open("/dev/mem", O_RDWR);
232 gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR);
233 gpsp_gp2x_memregl =
234 (unsigned long *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED,
235 gpsp_gp2x_dev, 0xc0000000);
236 gpsp_gp2x_memregs = (unsigned short *)gpsp_gp2x_memregl;
638cc626 237 warm_init();
4742480d 238#ifdef WIZ_BUILD
239 gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
4cdfc0bc 240 fb_video_init();
241#endif
2823a4c8 242
5a01fba6 243 if (get_romdir(buff, sizeof(buff)) > 0)
244 chdir(buff);
638cc626 245
2823a4c8 246 gp2x_sound_volume(1);
247}
248
249void gp2x_quit()
250{
5a01fba6 251 char buff1[256], buff2[256];
638cc626 252
5a01fba6 253 getcwd(buff1, sizeof(buff1));
638cc626 254 chdir(main_path);
5a01fba6 255 if (get_romdir(buff2, sizeof(buff2)) >= 0 &&
256 strcmp(buff1, buff2) != 0)
638cc626 257 {
5a01fba6 258 FILE *f = fopen("romdir.txt", "w");
259 if (f != NULL)
260 {
261 printf("writing romdir: %s\n", buff1);
262 fprintf(f, "%s", buff1);
263 fclose(f);
264 }
638cc626 265 }
266
267 warm_finish();
4742480d 268#ifdef WIZ_BUILD
269 close(gpsp_gp2x_gpiodev);
4cdfc0bc 270 fb_video_exit();
42c81190 271 wiz_gamepak_cleanup();
4742480d 272#endif
4cdfc0bc 273 munmap((void *)gpsp_gp2x_memregl, 0x10000);
274 close(gpsp_gp2x_dev_audio);
275 close(gpsp_gp2x_dev);
90206450 276
638cc626 277 fcloseall();
5a01fba6 278 sync();
42c81190 279 exit(0);
2823a4c8 280}
281
282void gp2x_sound_volume(u32 volume_up)
283{
284 u32 volume;
285 if((volume_up == 0) && (gp2x_audio_volume > 0))
286 gp2x_audio_volume--;
287
288 if((volume_up != 0) && (gp2x_audio_volume < 100))
289 gp2x_audio_volume++;
290
291 volume = (gp2x_audio_volume * 0x50) / 100;
292 volume = (gp2x_audio_volume << 8) | gp2x_audio_volume;
293 ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume);
294}
295
4742480d 296u32 gpsp_gp2x_joystick_read(void)
297{
298#ifdef WIZ_BUILD
299 u32 value = 0;
300 read(gpsp_gp2x_gpiodev, &value, 4);
301 if(value & 0x02)
302 value |= 0x05;
303 if(value & 0x08)
304 value |= 0x14;
305 if(value & 0x20)
306 value |= 0x50;
307 if(value & 0x80)
308 value |= 0x41;
309 return value;
310#else
311 u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
312
313 if(value == 0xFD)
314 value = 0xFA;
315 if(value == 0xF7)
316 value = 0xEB;
317 if(value == 0xDF)
318 value = 0xAF;
319 if(value == 0x7F)
320 value = 0xBE;
321
322 return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
323 (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
324#endif
325}
326
638cc626 327// Fout = (m * Fin) / (p * 2^s)
4742480d 328void set_FCLK(u32 MHZ)
329{
638cc626 330 u32 v;
331 u32 mdiv, pdiv, sdiv = 0;
332#ifdef WIZ_BUILD
333 #define SYS_CLK_FREQ 27
334 // m = MDIV, p = PDIV, s = SDIV
335 pdiv = 9;
336 mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
337 mdiv &= 0x3ff;
338 v = (pdiv<<18) | (mdiv<<8) | sdiv;
339
340 gpsp_gp2x_memregl[0xf004>>2] = v;
341 gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000;
5a01fba6 342 while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000)
343 ;
638cc626 344#else
345 #define SYS_CLK_FREQ 7372800
346 // m = MDIV + 8, p = PDIV + 2, s = SDIV
347 pdiv = 3;
348 mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ;
349 mdiv &= 0xff;
350 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
351 gpsp_gp2x_memregs[0x910>>1] = v;
4742480d 352#endif
638cc626 353}
4742480d 354