1 /* Parts used from cpuctrl */
3 Copyright (C) 2005 Hermes/PS2Reality
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
25 #include <sys/ioctl.h>
26 #include <sys/soundcard.h>
27 #include <sys/types.h>
29 #include "../common.h"
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;
38 static volatile u16 *gpsp_gp2x_memregs;
39 static volatile u32 *gpsp_gp2x_memregl;
43 void *gpsp_gp2x_screen;
44 #define fb_buf_count 4
45 static u32 fb_paddr[fb_buf_count];
46 static void *fb_vaddr[fb_buf_count];
47 static u32 fb_work_buf;
48 static int fb_buf_use;
50 static void fb_video_init()
52 struct fb_fix_screeninfo fbfix;
56 fbdev = open("/dev/fb0", O_RDWR);
58 perror("can't open fbdev");
62 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
65 perror("ioctl(fbdev) failed");
69 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
70 fb_paddr[0] = fbfix.smem_start;
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)
77 perror("mmap(fb_vaddr) failed");
80 memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count);
82 printf(" %p -> %08x\n", fb_vaddr[0], fb_paddr[0]);
83 for (i = 1; i < fb_buf_count; i++)
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]);
90 fb_buf_use = fb_buf_count;
93 warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
96 void pollux_video_flip()
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;
102 if (fb_work_buf >= fb_buf_use)
104 gpsp_gp2x_screen = fb_vaddr[fb_work_buf];
107 void fb_use_buffers(int count)
111 else if (count > fb_buf_count)
112 count = fb_buf_count;
114 memset(fb_vaddr[0], 0, 320*240*2*count);
117 static void fb_video_exit()
119 /* switch to default fb mem */
120 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0];
121 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
125 static int get_romdir(char *buff, size_t size)
131 f = fopen("romdir.txt", "r");
135 s = fgets(buff, size, f);
139 while (r > 0 && isspace(buff[r-1]))
151 gpsp_gp2x_dev = open("/dev/mem", O_RDWR);
152 gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR);
154 (unsigned long *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED,
155 gpsp_gp2x_dev, 0xc0000000);
156 gpsp_gp2x_memregs = (unsigned short *)gpsp_gp2x_memregl;
159 gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
163 if (get_romdir(buff, sizeof(buff)) > 0)
166 gp2x_sound_volume(1);
171 char buff1[256], buff2[256];
173 getcwd(buff1, sizeof(buff1));
175 if (get_romdir(buff2, sizeof(buff2)) >= 0 &&
176 strcmp(buff1, buff2) != 0)
178 FILE *f = fopen("romdir.txt", "w");
181 printf("writing romdir: %s\n", buff1);
182 fprintf(f, "%s", buff1);
189 close(gpsp_gp2x_gpiodev);
192 munmap((void *)gpsp_gp2x_memregl, 0x10000);
193 close(gpsp_gp2x_dev_audio);
194 close(gpsp_gp2x_dev);
199 execl("gp2xmenu", "gp2xmenu", NULL);
202 void gp2x_sound_volume(u32 volume_up)
205 if((volume_up == 0) && (gp2x_audio_volume > 0))
208 if((volume_up != 0) && (gp2x_audio_volume < 100))
211 volume = (gp2x_audio_volume * 0x50) / 100;
212 volume = (gp2x_audio_volume << 8) | gp2x_audio_volume;
213 ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume);
216 u32 gpsp_gp2x_joystick_read(void)
220 read(gpsp_gp2x_gpiodev, &value, 4);
231 u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
242 return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
243 (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
247 // Fout = (m * Fin) / (p * 2^s)
248 void set_FCLK(u32 MHZ)
251 u32 mdiv, pdiv, sdiv = 0;
253 #define SYS_CLK_FREQ 27
254 // m = MDIV, p = PDIV, s = SDIV
256 mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
258 v = (pdiv<<18) | (mdiv<<8) | sdiv;
260 gpsp_gp2x_memregl[0xf004>>2] = v;
261 gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000;
262 while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000)
265 #define SYS_CLK_FREQ 7372800
266 // m = MDIV + 8, p = PDIV + 2, s = SDIV
268 mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ;
270 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
271 gpsp_gp2x_memregs[0x910>>1] = v;