u4 release
[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"
2823a4c8 32
90206450 33u32 gp2x_audio_volume = 74/2;
2823a4c8 34u32 gpsp_gp2x_dev_audio = 0;
35u32 gpsp_gp2x_dev = 0;
4742480d 36u32 gpsp_gp2x_gpiodev = 0;
2823a4c8 37
4742480d 38static volatile u16 *gpsp_gp2x_memregs;
39static volatile u32 *gpsp_gp2x_memregl;
2823a4c8 40
4cdfc0bc 41#ifdef WIZ_BUILD
42#include <linux/fb.h>
43void *gpsp_gp2x_screen;
5c6e71a0 44#define fb_buf_count 4
45static u32 fb_paddr[fb_buf_count];
46static void *fb_vaddr[fb_buf_count];
4cdfc0bc 47static u32 fb_work_buf;
5c6e71a0 48static int fb_buf_use;
4cdfc0bc 49
50static 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
96void 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
107void 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;
5c6e71a0 114 memset(fb_vaddr[0], 0, 320*240*2*count);
4cdfc0bc 115}
116
117static void fb_video_exit()
118{
119 /* switch to default fb mem */
120 gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0];
121 gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
122}
123#endif
124
5a01fba6 125static int get_romdir(char *buff, size_t size)
2823a4c8 126{
638cc626 127 FILE *f;
5a01fba6 128 char *s;
129 int r = -1;
130
131 f = fopen("romdir.txt", "r");
132 if (f == NULL)
133 return -1;
134
135 s = fgets(buff, size, f);
136 if (s)
137 {
138 r = strlen(s);
139 while (r > 0 && isspace(buff[r-1]))
140 buff[--r] = 0;
141 }
142
143 fclose(f);
144 return r;
145}
146
147void gp2x_init()
148{
149 char buff[256];
150
2823a4c8 151 gpsp_gp2x_dev = open("/dev/mem", O_RDWR);
152 gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR);
153 gpsp_gp2x_memregl =
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;
638cc626 157 warm_init();
4742480d 158#ifdef WIZ_BUILD
159 gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
4cdfc0bc 160 fb_video_init();
161#endif
2823a4c8 162
5a01fba6 163 if (get_romdir(buff, sizeof(buff)) > 0)
164 chdir(buff);
638cc626 165
2823a4c8 166 gp2x_sound_volume(1);
167}
168
169void gp2x_quit()
170{
5a01fba6 171 char buff1[256], buff2[256];
638cc626 172
5a01fba6 173 getcwd(buff1, sizeof(buff1));
638cc626 174 chdir(main_path);
5a01fba6 175 if (get_romdir(buff2, sizeof(buff2)) >= 0 &&
176 strcmp(buff1, buff2) != 0)
638cc626 177 {
5a01fba6 178 FILE *f = fopen("romdir.txt", "w");
179 if (f != NULL)
180 {
181 printf("writing romdir: %s\n", buff1);
182 fprintf(f, "%s", buff1);
183 fclose(f);
184 }
638cc626 185 }
186
187 warm_finish();
4742480d 188#ifdef WIZ_BUILD
189 close(gpsp_gp2x_gpiodev);
4cdfc0bc 190 fb_video_exit();
4742480d 191#endif
4cdfc0bc 192 munmap((void *)gpsp_gp2x_memregl, 0x10000);
193 close(gpsp_gp2x_dev_audio);
194 close(gpsp_gp2x_dev);
90206450 195
638cc626 196 fcloseall();
5a01fba6 197 sync();
198 chdir("/usr/gp2x");
199 execl("gp2xmenu", "gp2xmenu", NULL);
2823a4c8 200}
201
202void gp2x_sound_volume(u32 volume_up)
203{
204 u32 volume;
205 if((volume_up == 0) && (gp2x_audio_volume > 0))
206 gp2x_audio_volume--;
207
208 if((volume_up != 0) && (gp2x_audio_volume < 100))
209 gp2x_audio_volume++;
210
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);
214}
215
4742480d 216u32 gpsp_gp2x_joystick_read(void)
217{
218#ifdef WIZ_BUILD
219 u32 value = 0;
220 read(gpsp_gp2x_gpiodev, &value, 4);
221 if(value & 0x02)
222 value |= 0x05;
223 if(value & 0x08)
224 value |= 0x14;
225 if(value & 0x20)
226 value |= 0x50;
227 if(value & 0x80)
228 value |= 0x41;
229 return value;
230#else
231 u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
232
233 if(value == 0xFD)
234 value = 0xFA;
235 if(value == 0xF7)
236 value = 0xEB;
237 if(value == 0xDF)
238 value = 0xAF;
239 if(value == 0x7F)
240 value = 0xBE;
241
242 return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
243 (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
244#endif
245}
246
638cc626 247// Fout = (m * Fin) / (p * 2^s)
4742480d 248void set_FCLK(u32 MHZ)
249{
638cc626 250 u32 v;
251 u32 mdiv, pdiv, sdiv = 0;
252#ifdef WIZ_BUILD
253 #define SYS_CLK_FREQ 27
254 // m = MDIV, p = PDIV, s = SDIV
255 pdiv = 9;
256 mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
257 mdiv &= 0x3ff;
258 v = (pdiv<<18) | (mdiv<<8) | sdiv;
259
260 gpsp_gp2x_memregl[0xf004>>2] = v;
261 gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000;
5a01fba6 262 while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000)
263 ;
638cc626 264#else
265 #define SYS_CLK_FREQ 7372800
266 // m = MDIV + 8, p = PDIV + 2, s = SDIV
267 pdiv = 3;
268 mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ;
269 mdiv &= 0xff;
270 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
271 gpsp_gp2x_memregs[0x910>>1] = v;
4742480d 272#endif
638cc626 273}
4742480d 274