d03a548fadb6e40d2a32d056efa41fc1906aa4f1
[gpsp.git] / gp2x / gp2x.c
1 /*  
2     Parts used from cpuctrl, Copyright (C) 2005  Hermes/PS2Reality
3     Portions Copyright (C) 2009 notaz
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 #define _GNU_SOURCE
23 #include "../common.h"
24 #include <sys/mman.h>
25 #include <sys/ioctl.h>
26 #include <sys/soundcard.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <ctype.h>
30 #include "gp2x.h"
31 #include "pollux_dpc_set.h"
32
33 static u32 gpsp_gp2x_dev_audio;
34 static u32 gpsp_gp2x_dev;
35 #ifdef WIZ_BUILD
36 static u32 gpsp_gp2x_gpiodev;
37 #endif
38
39 static u32 gp2x_audio_volume = 74/2;
40
41 static volatile u16 *gpsp_gp2x_memregs;
42 static volatile u32 *gpsp_gp2x_memregl;
43
44 u32 button_plat_mask_to_config[PLAT_BUTTON_COUNT] =
45 {
46   GP2X_UP,
47   GP2X_LEFT,
48   GP2X_DOWN,
49   GP2X_RIGHT,
50   GP2X_START,
51   GP2X_SELECT,
52   GP2X_L,
53   GP2X_R,
54   GP2X_A,
55   GP2X_B,
56   GP2X_X,
57   GP2X_Y,
58   GP2X_VOL_DOWN,
59   GP2X_VOL_UP,
60   GP2X_PUSH,
61   GP2X_VOL_MIDDLE
62 };
63
64 u32 gamepad_config_map[PLAT_BUTTON_COUNT] =
65 {
66   BUTTON_ID_UP,                 // Up
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
74   BUTTON_ID_FPS,                // A
75   BUTTON_ID_A,                  // B
76   BUTTON_ID_B,                  // X
77   BUTTON_ID_MENU,               // Y
78   BUTTON_ID_VOLDOWN,            // Vol down
79   BUTTON_ID_VOLUP,              // Vol up
80   BUTTON_ID_FPS,                // Push
81   BUTTON_ID_MENU                // Vol middle
82 };
83
84 #ifdef WIZ_BUILD
85 #include <linux/fb.h>
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;
92 static int fbdev;
93
94 static void fb_video_init()
95 {
96   struct fb_fix_screeninfo fbfix;
97   int i, ret;
98
99   fbdev = open("/dev/fb0", O_RDWR);
100   if (fbdev < 0) {
101     perror("can't open fbdev");
102     exit(1);
103   }
104
105   ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
106   if (ret == -1)
107   {
108     perror("ioctl(fbdev) failed");
109     exit(1);
110   }
111
112   printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
113   fb_paddr[0] = fbfix.smem_start;
114
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)
118   {
119     perror("mmap(fb_vaddr) failed");
120     exit(1);
121   }
122   memset(fb_vaddr[0], 0, 320*240*2*fb_buf_count);
123
124   printf("  %p -> %08x\n", fb_vaddr[0], fb_paddr[0]);
125   for (i = 1; i < fb_buf_count; i++)
126   {
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]);
130   }
131   fb_work_buf = 0;
132   fb_buf_use = fb_buf_count;
133
134   pollux_video_flip();
135   warm_change_cb_upper(WCB_C_BIT|WCB_B_BIT, 1);
136 }
137
138 void pollux_video_flip()
139 {
140   gpsp_gp2x_memregl[0x406C>>2] = fb_paddr[fb_work_buf];
141   gpsp_gp2x_memregl[0x4058>>2] |= 0x10;
142   fb_work_buf++;
143   if (fb_work_buf >= fb_buf_use)
144     fb_work_buf = 0;
145   gpsp_gp2x_screen = fb_vaddr[fb_work_buf];
146 }
147
148 void fb_use_buffers(int count)
149 {
150   if (count < 1)
151     count = 1;
152   else if (count > fb_buf_count)
153     count = fb_buf_count;
154   fb_buf_use = count;
155   memset(fb_vaddr[0], 0, 320*240*2*count);
156 }
157
158 void wiz_lcd_set_portrait(int y)
159 {
160   static int old_y = -1;
161   int cmd[2] = { 0, 0 };
162
163   if (old_y == y)
164     return;
165   cmd[0] = y ? 6 : 5;
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;
169   old_y = y;
170
171   /* the above ioctl resets LCD timings, so set them here */
172   pollux_dpc_set(gpsp_gp2x_memregs, getenv("pollux_dpc_set"));
173 }
174
175 static void fb_video_exit()
176 {
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);
181   close(fbdev);
182 }
183
184 static int wiz_gamepak_fd = -1;
185 static u32 wiz_gamepak_size;
186
187 static void wiz_gamepak_cleanup()
188 {
189   if (wiz_gamepak_size)
190     munmap(gamepak_rom, wiz_gamepak_size);
191   if (wiz_gamepak_fd >= 0)
192     close(wiz_gamepak_fd);
193   gamepak_rom = NULL;
194   wiz_gamepak_size = 0;
195   wiz_gamepak_fd = -1;
196 }
197
198 u32 wiz_load_gamepak(char *name)
199 {
200   char *dot_position = strrchr(name, '.');
201   u32 ret;
202
203   if (!strcasecmp(dot_position, ".zip"))
204   {
205     if (wiz_gamepak_fd >= 0)
206     {
207       wiz_gamepak_cleanup();
208       printf("switching to ROM malloc\n");
209       init_gamepak_buffer();
210     }
211     return load_file_zip(name);
212   }
213
214   if (wiz_gamepak_fd < 0)
215   {
216     extern void *gamepak_memory_map;
217     free(gamepak_rom);
218     free(gamepak_memory_map);
219     gamepak_memory_map = NULL;
220     printf("switching to ROM mmap\n");
221   }
222   else
223     wiz_gamepak_cleanup();
224
225   wiz_gamepak_fd = open(name, O_RDONLY|O_NOATIME, S_IRUSR);
226   if (wiz_gamepak_fd < 0)
227   {
228     perror("wiz_load_gamepak: open failed");
229     return -1;
230   }
231
232   ret = lseek(wiz_gamepak_fd, 0, SEEK_END);
233   wiz_gamepak_size = gamepak_ram_buffer_size = ret;
234
235   gamepak_rom = mmap(0, ret, PROT_READ, MAP_SHARED, wiz_gamepak_fd, 0);
236   if (gamepak_rom == MAP_FAILED)
237   {
238     perror("wiz_load_gamepak: mmap failed");
239     return -1;
240   }
241
242   return ret;
243 }
244
245 #endif
246
247 void gpsp_plat_init(void)
248 {
249   char buff[256];
250
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;
256   warm_init();
257 #ifdef WIZ_BUILD
258   gpsp_gp2x_gpiodev = open("/dev/GPIO", O_RDONLY);
259   fb_video_init();
260 #endif
261
262   gp2x_sound_volume(1);
263 }
264
265 void gpsp_plat_quit(void)
266 {
267   char buff1[256], buff2[256];
268
269   getcwd(buff1, sizeof(buff1));
270   chdir(main_path);
271
272   warm_finish();
273 #ifdef WIZ_BUILD
274   close(gpsp_gp2x_gpiodev);
275   fb_video_exit();
276   wiz_gamepak_cleanup();
277 #endif
278   munmap((void *)gpsp_gp2x_memregl, 0x10000);
279   close(gpsp_gp2x_dev_audio);
280   close(gpsp_gp2x_dev);
281
282   fcloseall();
283   sync();
284   exit(0);
285 }
286
287 void gp2x_sound_volume(u32 volume_up)
288 {
289   u32 volume;
290   if((volume_up == 0) && (gp2x_audio_volume > 0))
291     gp2x_audio_volume--;
292
293   if((volume_up != 0)  && (gp2x_audio_volume < 100))
294     gp2x_audio_volume++;
295
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);
299 }
300
301 u32 gpsp_plat_joystick_read(void)
302 {
303 #ifdef WIZ_BUILD
304   u32 value = 0;
305   read(gpsp_gp2x_gpiodev, &value, 4);
306   if(value & 0x02)
307    value |= 0x05;
308   if(value & 0x08)
309    value |= 0x14;
310   if(value & 0x20)
311    value |= 0x50;
312   if(value & 0x80)
313    value |= 0x41;
314   return value;
315 #else
316   u32 value = (gpsp_gp2x_memregs[0x1198 >> 1] & 0x00FF);
317
318   if(value == 0xFD)
319    value = 0xFA;
320   if(value == 0xF7)
321    value = 0xEB;
322   if(value == 0xDF)
323    value = 0xAF;
324   if(value == 0x7F)
325    value = 0xBE;
326
327   return ~((gpsp_gp2x_memregs[0x1184 >> 1] & 0xFF00) | value |
328    (gpsp_gp2x_memregs[0x1186 >> 1] << 16));
329 #endif
330 }
331
332 u32 gpsp_plat_buttons_to_cursor(u32 buttons)
333 {
334   gui_action_type new_button = CURSOR_NONE;
335
336   if(buttons & GP2X_A)
337     new_button = CURSOR_BACK;
338
339   if(buttons & GP2X_X)
340     new_button = CURSOR_EXIT;
341
342   if(buttons & GP2X_B)
343     new_button = CURSOR_SELECT;
344
345   if(buttons & GP2X_UP)
346     new_button = CURSOR_UP;
347
348   if(buttons & GP2X_DOWN)
349     new_button = CURSOR_DOWN;
350
351   if(buttons & GP2X_LEFT)
352     new_button = CURSOR_LEFT;
353
354   if(buttons & GP2X_RIGHT)
355     new_button = CURSOR_RIGHT;
356
357   if(buttons & GP2X_L)
358     new_button = CURSOR_L;
359
360   if(buttons & GP2X_R)
361     new_button = CURSOR_R;
362
363   return new_button;
364 }
365
366 // Fout = (m * Fin) / (p * 2^s)
367 void set_FCLK(u32 MHZ)
368 {
369   u32 v;
370   u32 mdiv, pdiv, sdiv = 0;
371 #ifdef WIZ_BUILD
372   #define SYS_CLK_FREQ 27
373   // m = MDIV, p = PDIV, s = SDIV
374   pdiv = 9;
375   mdiv = (MHZ * pdiv) / SYS_CLK_FREQ;
376   mdiv &= 0x3ff;
377   v = (pdiv<<18) | (mdiv<<8) | sdiv;
378
379   gpsp_gp2x_memregl[0xf004>>2] = v;
380   gpsp_gp2x_memregl[0xf07c>>2] |= 0x8000;
381   while (gpsp_gp2x_memregl[0xf07c>>2] & 0x8000)
382     ;
383 #else
384   #define SYS_CLK_FREQ 7372800
385   // m = MDIV + 8, p = PDIV + 2, s = SDIV
386   pdiv = 3;
387   mdiv = (MHZ * pdiv * 1000000) / SYS_CLK_FREQ;
388   mdiv &= 0xff;
389   v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
390   gpsp_gp2x_memregs[0x910>>1] = v;
391 #endif
392 }
393