2 Parts used from cpuctrl, Copyright (C) 2005 Hermes/PS2Reality
3 Portions Copyright (C) 2009 notaz
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.
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.
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
23 #include "../common.h"
25 #include <sys/ioctl.h>
26 #include <sys/soundcard.h>
27 #include <sys/types.h>
31 #include "pollux_dpc_set.h"
33 static u32 gpsp_gp2x_dev_audio;
34 static u32 gpsp_gp2x_dev;
36 static u32 gpsp_gp2x_gpiodev;
39 static u32 gp2x_audio_volume = 74/2;
41 static volatile u16 *gpsp_gp2x_memregs;
42 static volatile u32 *gpsp_gp2x_memregl;
44 u32 button_plat_mask_to_config[PLAT_BUTTON_COUNT] =
64 u32 gamepad_config_map[PLAT_BUTTON_COUNT] =
67 BUTTON_ID_LEFT, // Left
68 BUTTON_ID_DOWN, // Down
69 BUTTON_ID_RIGHT, // Right
70 BUTTON_ID_START, // Start
71 BUTTON_ID_SELECT, // Select
72 BUTTON_ID_L, // Ltrigger
73 BUTTON_ID_R, // Rtrigger
78 BUTTON_ID_VOLDOWN, // Vol down
79 BUTTON_ID_VOLUP, // Vol up
80 BUTTON_ID_FPS, // Push
81 BUTTON_ID_MENU // Vol middle
86 void *gpsp_gp2x_screen;
87 #define fb_buf_count 4
88 static u32 fb_paddr[fb_buf_count];
89 static void *fb_vaddr[fb_buf_count];
90 static u32 fb_work_buf;
91 static int fb_buf_use;
94 static void fb_video_init()
96 struct fb_fix_screeninfo fbfix;
99 fbdev = open("/dev/fb0", O_RDWR);
101 perror("can't open fbdev");
105 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
108 perror("ioctl(fbdev) failed");
112 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
113 fb_paddr[0] = fbfix.smem_start;
115 fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE,
116 MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]);
117 if (fb_vaddr[0] == MAP_FAILED)
119 perror("mmap(fb_vaddr) failed");
122 memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count);
124 printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]);
125 for (i = 1; i < fb_buf_count; i++)
127 fb_paddr[i] = fb_paddr[i-1] + 320*240*2;
128 fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2;
129 printf(" %p -> %08x\n", fb_vaddr[i], fb_paddr[i]);
132 fb_buf_use = fb_buf_count;
135 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
138 void pollux_video_flip()
140 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf];
141 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
143 if (fb_work_buf >= fb_buf_use)
145 gpsp_gp2x_screen = fb_vaddr[fb_work_buf];
148 void fb_use_buffers(int count)
152 else if (count > fb_buf_count)
153 count = fb_buf_count;
155 memset(fb_vaddr[0], 0, 320*240*2*count);
158 void wiz_lcd_set_portrait(int y)
160 static int old_y = -1;
161 int cmd[2] = { 0, 0 };
166 ioctl(fbdev, _IOW('D', 90, int[2]), cmd);
167 gpsp_gp2x_memregl[0x4004>>2] = y ? 0x013f00ef : 0x00ef013f;
168 gpsp_gp2x_memregl[0x4000>>2] |= 1 << 3;
171 /* the above ioctl resets LCD timings, so set them here */
172 pollux_dpc_set(gpsp_gp2x_memregs, getenv("pollux_dpc_set"));
175 static void fb_video_exit()
177 /* switch to default fb mem, turn portrait off */
178 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0];
179 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
180 wiz_lcd_set_portrait(0);
184 static int wiz_gamepak_fd = -1;
185 static u32 wiz_gamepak_size;
187 static void wiz_gamepak_cleanup()
189 if (wiz_gamepak_size)
190 munmap(gamepak_rom, wiz_gamepak_size);
191 if (wiz_gamepak_fd >= 0)
192 close(wiz_gamepak_fd);
194 wiz_gamepak_size = 0;
198 u32 wiz_load_gamepak(char *name)
200 char *dot_position = strrchr(name, '.');
203 if (!strcasecmp(dot_position, ".zip"))
205 if (wiz_gamepak_fd >= 0)
207 wiz_gamepak_cleanup();
208 printf("switching to ROM malloc\n");
209 init_gamepak_buffer();
211 return load_file_zip(name);
214 if (wiz_gamepak_fd < 0)
216 extern void *gamepak_memory_map;
218 free(gamepak_memory_map);
219 gamepak_memory_map = NULL;
220 printf("switching to ROM mmap\n");
223 wiz_gamepak_cleanup();
225 wiz_gamepak_fd = open(name, O_RDONLY|O_NOATIME, S_IRUSR);
226 if (wiz_gamepak_fd < 0)
228 perror("wiz_load_gamepak: open failed");
232 ret = lseek(wiz_gamepak_fd, 0, SEEK_END);
233 wiz_gamepak_size = gamepak_ram_buffer_size = ret;
235 gamepak_rom = mmap(0, ret, PROT_READ, MAP_SHARED, wiz_gamepak_fd, 0);
236 if (gamepak_rom == MAP_FAILED)
238 perror("wiz_load_gamepak: mmap failed");
247 void gpsp_plat_init(void)
251 gpsp_gp2x_dev = open("/dev/mem", O_RDWR);
252 gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR);
253 gpsp_gp2x_memregl = (u32 *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED,
254 gpsp_gp2x_dev, 0xc0000000);
255 gpsp_gp2x_memregs = (u16 *)gpsp_gp2x_memregl;
258 gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
262 gp2x_sound_volume(1);
265 void gpsp_plat_quit(void)
267 char buff1[256], buff2[256];
269 getcwd(buff1, sizeof(buff1));
274 close(gpsp_gp2x_gpiodev);
276 wiz_gamepak_cleanup();
278 munmap((void *)gpsp_gp2x_memregl, 0x10000);
279 close(gpsp_gp2x_dev_audio);
280 close(gpsp_gp2x_dev);
287 void gp2x_sound_volume(u32 volume_up)
290 if((volume_up == 0) && (gp2x_audio_volume > 0))
293 if((volume_up != 0) && (gp2x_audio_volume < 100))
296 volume = (gp2x_audio_volume * 0x50) / 100;
297 volume = (gp2x_audio_volume << 8) | gp2x_audio_volume;
298 ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume);
301 u32 gpsp_plat_joystick_read(void)
305 read(gpsp_gp2x_gpiodev, &value, 4);
316 u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
327 return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
328 (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
332 u32 gpsp_plat_buttons_to_cursor(u32 buttons)
334 gui_action_type new_button = CURSOR_NONE;
337 new_button = CURSOR_BACK;
340 new_button = CURSOR_EXIT;
343 new_button = CURSOR_SELECT;
345 if(buttons & GP2X_UP)
346 new_button = CURSOR_UP;
348 if(buttons & GP2X_DOWN)
349 new_button = CURSOR_DOWN;
351 if(buttons & GP2X_LEFT)
352 new_button = CURSOR_LEFT;
354 if(buttons & GP2X_RIGHT)
355 new_button = CURSOR_RIGHT;
358 new_button = CURSOR_L;
361 new_button = CURSOR_R;
366 // Fout = (m * Fin) / (p * 2^s)
367 void set_FCLK(u32 MHZ)
370 u32 mdiv, pdiv, sdiv = 0;
372 #define SYS_CLK_FREQ 27
373 // m = MDIV, p = PDIV, s = SDIV
375 mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
377 v = (pdiv<<18) | (mdiv<<8) | sdiv;
379 gpsp_gp2x_memregl[0xf004>>2] = v;
380 gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000;
381 while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000)
384 #define SYS_CLK_FREQ 7372800
385 // m = MDIV + 8, p = PDIV + 2, s = SDIV
387 mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ;
389 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
390 gpsp_gp2x_memregs[0x910>>1] = v;