a8ec704221da0b2c8bbcad298cb0f899c177aa94
[gpsp.git] / gp2x / gp2x.c
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 #include <sys/mman.h>
23 #include <sys/ioctl.h>
24 #include <sys/soundcard.h>
25 #include "../common.h"
26 #include "gp2x.h"
27 #include "warm.h"
28
29 extern int main_cpuspeed(int argc, char *argv[]);
30 extern SDL_Surface* screen;
31
32 u32 gp2x_audio_volume = 74/2;
33 u32 gpsp_gp2x_dev_audio = 0;
34 u32 gpsp_gp2x_dev = 0;
35 u32 gpsp_gp2x_gpiodev = 0;
36
37 static volatile u16 *gpsp_gp2x_memregs;
38 static volatile u32 *gpsp_gp2x_memregl;
39 unsigned short *gp2x_memregs;
40
41 s32 gp2x_load_mmuhack()
42 {
43   s32 mmufd = open("/dev/mmuhack", O_RDWR);
44
45   if(mmufd < 0)
46   {
47     system("/sbin/insmod mmuhack.o");
48     mmufd = open("/dev/mmuhack", O_RDWR);
49   }
50
51   if(mmufd < 0)
52     return -1;
53
54   close(mmufd);
55   return 0;
56 }
57
58 #ifdef WIZ_BUILD
59 #include <linux/fb.h>
60 void *gpsp_gp2x_screen;
61 static u32 fb_paddr[3];
62 static void *fb_vaddr[3];
63 static u32 fb_work_buf;
64 const int fb_buf_count = 3;
65 static int fb_buf_use = 3;
66
67 static void fb_video_init()
68 {
69   struct fb_fix_screeninfo fbfix;
70   int i, ret;
71   int fbdev;
72
73   fbdev = open("/dev/fb0", O_RDWR);
74   if (fbdev < 0) {
75     perror("can't open fbdev");
76     exit(1);
77   }
78
79   ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
80   if (ret == -1)
81   {
82     perror("ioctl(fbdev) failed");
83     exit(1);
84   }
85
86   printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
87   fb_paddr[0] = fbfix.smem_start;
88   close(fbdev);
89
90   fb_vaddr[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE,
91     MAP_SHARED, gpsp_gp2x_dev, fb_paddr[0]);
92   if (fb_vaddr[0] == MAP_FAILED)
93   {
94     perror("mmap(fb_vaddr) failed");
95     exit(1);
96   }
97   memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count);
98
99   printf("  %p -> %08x\n", fb_vaddr[0], fb_paddr[0]);
100   for (i = 1; i < fb_buf_count; i++)
101   {
102     fb_paddr[i] = fb_paddr[i-1] + 320*240*2;
103     fb_vaddr[i] = (char *)fb_vaddr[i-1] + 320*240*2;
104     printf("  %p -> %08x\n", fb_vaddr[i], fb_paddr[i]);
105   }
106   fb_work_buf = 0;
107   fb_buf_use = fb_buf_count;
108
109   pollux_video_flip();
110   warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
111 }
112
113 void pollux_video_flip()
114 {
115   warm_cache_op_all(WOP_D_CLEAN);
116   gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf];
117   gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
118   fb_work_buf++;
119   if (fb_work_buf >= fb_buf_use)
120     fb_work_buf = 0;
121   gpsp_gp2x_screen = fb_vaddr[fb_work_buf];
122 }
123
124 void fb_use_buffers(int count)
125 {
126   if (count < 1)
127     count = 1;
128   else if (count > fb_buf_count)
129     count = fb_buf_count;
130   fb_buf_use = count;
131 }
132
133 static void fb_video_exit()
134 {
135   /* switch to default fb mem */
136   gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[0];
137   gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
138 }
139 #endif
140
141 void gp2x_init()
142 {
143   gpsp_gp2x_dev = open("/dev/mem",   O_RDWR);
144   gpsp_gp2x_dev_audio = open("/dev/mixer", O_RDWR);
145   gpsp_gp2x_memregl =
146    (unsigned long  *)mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED,
147    gpsp_gp2x_dev, 0xc0000000);
148   gpsp_gp2x_memregs = (unsigned short *)gpsp_gp2x_memregl;
149 #ifdef WIZ_BUILD
150   gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
151   warm_init();
152   fb_video_init();
153 #endif
154
155 //  clear_screen(0);
156 //  main_cpuspeed(0, NULL);
157   gp2x_memregs = (void *)gpsp_gp2x_memregs;
158   cpuctrl_init();
159   gp2x_sound_volume(1);
160 }
161
162 void gp2x_quit()
163 {
164 #ifdef WIZ_BUILD
165   close(gpsp_gp2x_gpiodev);
166   fb_video_exit();
167 #endif
168   munmap((void *)gpsp_gp2x_memregl, 0x10000);
169   close(gpsp_gp2x_dev_audio);
170   close(gpsp_gp2x_dev);
171
172   //chdir("/usr/gp2x");
173   //execl("gp2xmenu", "gp2xmenu", NULL);
174   exit(0);
175 }
176
177 void gp2x_sound_volume(u32 volume_up)
178 {
179   u32 volume;
180   if((volume_up == 0) && (gp2x_audio_volume > 0))
181     gp2x_audio_volume--;
182
183   if((volume_up != 0)  && (gp2x_audio_volume < 100))
184     gp2x_audio_volume++;
185
186   volume = (gp2x_audio_volume * 0x50) / 100;
187   volume = (gp2x_audio_volume << 8) | gp2x_audio_volume;
188   ioctl(gpsp_gp2x_dev_audio, SOUND_MIXER_WRITE_PCM, &volume);
189 }
190
191 u32 gpsp_gp2x_joystick_read(void)
192 {
193 #ifdef WIZ_BUILD
194   u32 value = 0;
195   read(gpsp_gp2x_gpiodev, &value, 4);
196   if(value & 0x02)
197    value |= 0x05;
198   if(value & 0x08)
199    value |= 0x14;
200   if(value & 0x20)
201    value |= 0x50;
202   if(value & 0x80)
203    value |= 0x41;
204   return value;
205 #else
206   u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
207
208   if(value == 0xFD)
209    value = 0xFA;
210   if(value == 0xF7)
211    value = 0xEB;
212   if(value == 0xDF)
213    value = 0xAF;
214   if(value == 0x7F)
215    value = 0xBE;
216
217   return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
218    (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
219 #endif
220 }
221
222 #ifdef WIZ_BUILD
223 void cpuctrl_init(void)
224 {
225 }
226
227 void set_FCLK(u32 MHZ)
228 {
229 }
230 #endif
231