2 * <random_info=mem_map>
3 * 00000000-029fffff linux (42MB)
4 * 02a00000-02dfffff fb (4MB, 153600B really used)
5 * 02e00000-02ffffff sound dma (2MB)
6 * 03000000-03ffffff MPEGDEC (?, 16MB)
13 #include <sys/types.h>
18 #include <sys/ioctl.h>
22 #include "plat_gp2x.h"
23 #include "../common/emu.h"
24 #include "../common/arm_utils.h"
25 #include "pollux_set.h"
27 static volatile unsigned short *memregs;
28 static volatile unsigned long *memregl;
29 static int memdev = -1;
30 static int battdev = -1;
32 extern void *gp2x_screens[4];
34 #define fb_buf_count 4
35 static unsigned int fb_paddr[fb_buf_count];
36 static int fb_work_buf;
37 static int fbdev = -1;
39 static char cpuclk_was_changed = 0;
40 static unsigned short memtimex_old[2];
41 static unsigned int pllsetreg0;
42 static int last_pal_setting = 0;
46 static void pollux_set_fromenv(const char *env_var)
48 const char *set_string;
49 set_string = getenv(env_var);
51 pollux_set(memregs, set_string);
53 printf("env var %s not defined.\n", env_var);
57 static void pollux_video_flip(int buf_count)
59 memregl[0x406C>>2] = fb_paddr[fb_work_buf];
60 memregl[0x4058>>2] |= 0x10;
62 if (fb_work_buf >= buf_count)
64 g_screen_ptr = gp2x_screens[fb_work_buf];
67 static void gp2x_video_flip_(void)
69 pollux_video_flip(fb_buf_count);
72 /* doulblebuffered flip */
73 static void gp2x_video_flip2_(void)
78 static void gp2x_video_changemode_ll_(int bpp)
80 static int prev_bpp = 0;
81 int code = 0, bytes = 2;
82 int rot_cmd[2] = { 0, 0 };
91 printf("changemode: %dbpp rot=%d\n", abs(bpp), bpp < 0);
93 /* negative bpp means rotated mode */
94 rot_cmd[0] = (bpp < 0) ? 6 : 5;
95 ret = ioctl(fbdev, _IOW('D', 90, int[2]), rot_cmd);
97 perror("rot ioctl failed");
98 memregl[0x4004>>2] = (bpp < 0) ? 0x013f00ef : 0x00ef013f;
99 memregl[0x4000>>2] |= 1 << 3;
101 /* the above ioctl resets LCD timings, so set them here */
102 snprintf(buff, sizeof(buff), "POLLUX_LCD_TIMINGS_%s", last_pal_setting ? "PAL" : "NTSC");
103 pollux_set_fromenv(buff);
119 printf("unhandled bpp request: %d\n", abs(bpp));
123 memregl[0x405c>>2] = bytes;
124 memregl[0x4060>>2] = bytes * (bpp < 0 ? 240 : 320);
126 r = memregl[0x4058>>2];
127 r = (r & 0xffff) | (code << 16) | 0x10;
128 memregl[0x4058>>2] = r;
131 static void gp2x_video_setpalette_(int *pal, int len)
133 /* pollux palette is 16bpp only.. */
135 for (i = 0; i < len; i++)
138 c = ((c >> 8) & 0xf800) | ((c >> 5) & 0x07c0) | ((c >> 3) & 0x001f);
139 memregl[0x4070>>2] = (i << 24) | c;
143 static void gp2x_video_RGB_setscaling_(int ln_offs, int W, int H)
145 /* maybe a job for 3d hardware? */
148 static void gp2x_video_wait_vsync_(void)
150 while (!(memregl[0x308c>>2] & (1 << 10)));
152 memregl[0x308c>>2] |= 1 << 10;
156 static void gp2x_set_cpuclk_(unsigned int mhz)
159 snprintf(buff, sizeof(buff), "cpuclk=%u", mhz);
160 pollux_set(memregs, buff);
162 cpuclk_was_changed = 1;
166 static void set_ram_timings_(void)
168 pollux_set_fromenv("POLLUX_RAM_TIMINGS");
171 static void unset_ram_timings_(void)
175 memregs[0x14802>>1] = memtimex_old[0];
176 memregs[0x14804>>1] = memtimex_old[1] | 0x8000;
178 for (i = 0; i < 0x100000; i++)
179 if (!(memregs[0x14804>>1] & 0x8000))
182 printf("RAM timings reset to startup values.\n");
186 static void set_lcd_custom_rate_(int is_pal)
188 /* just remember PAL/NTSC. We always set timings in _changemode_ll() */
189 last_pal_setting = is_pal;
192 static void unset_lcd_custom_rate_(void)
196 static void set_lcd_gamma_(int g100, int A_SNs_curve)
198 /* hm, the LCD possibly can do it (but not POLLUX) */
201 static int gp2x_read_battery_(void)
203 unsigned short magic_val = 0;
207 if (read(battdev, &magic_val, sizeof(magic_val)) != sizeof(magic_val))
218 #define TIMER_BASE3 0x1980
219 #define TIMER_REG(x) memregl[(TIMER_BASE3 + x) >> 2]
221 unsigned int gp2x_get_ticks_us_(void)
223 TIMER_REG(0x08) = 0x4b; /* run timer, latch value */
227 unsigned int gp2x_get_ticks_ms_(void)
229 /* approximate /= 1000 */
230 unsigned long long v64;
231 v64 = (unsigned long long)gp2x_get_ticks_us_() * 4294968;
235 static void timer_cleanup(void)
237 TIMER_REG(0x40) = 0x0c; /* be sure clocks are on */
238 TIMER_REG(0x08) = 0x23; /* stop the timer, clear irq in case it's pending */
239 TIMER_REG(0x00) = 0; /* clear counter */
240 TIMER_REG(0x40) = 0; /* clocks off */
241 TIMER_REG(0x44) = 0; /* dividers back to default */
244 void pollux_init(void)
246 struct fb_fix_screeninfo fbfix;
249 memdev = open("/dev/mem", O_RDWR);
251 perror("open(/dev/mem) failed");
255 memregs = mmap(0, 0x20000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
256 if (memregs == MAP_FAILED) {
257 perror("mmap(memregs) failed");
260 memregl = (volatile void *)memregs;
262 fbdev = open("/dev/fb0", O_RDWR);
264 perror("can't open fbdev");
268 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
270 perror("ioctl(fbdev) failed");
274 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
275 fb_paddr[0] = fbfix.smem_start;
277 gp2x_screens[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE,
278 MAP_SHARED, memdev, fb_paddr[0]);
279 if (gp2x_screens[0] == MAP_FAILED)
281 perror("mmap(gp2x_screens) failed");
284 memset(gp2x_screens[0], 0, 320*240*2*fb_buf_count);
286 printf(" %p -> %08x\n", gp2x_screens[0], fb_paddr[0]);
287 for (i = 1; i < fb_buf_count; i++)
289 fb_paddr[i] = fb_paddr[i-1] + 320*240*2;
290 gp2x_screens[i] = (char *)gp2x_screens[i-1] + 320*240*2;
291 printf(" %p -> %08x\n", gp2x_screens[i], fb_paddr[i]);
294 g_screen_ptr = gp2x_screens[0];
296 battdev = open("/dev/pollux_batt", O_RDONLY);
298 perror("Warning: could't open pollux_batt");
301 if (TIMER_REG(0x08) & 8)
304 TIMER_REG(0x44) = 0x922; /* using PLL1, divider value 147 */
305 TIMER_REG(0x40) = 0x0c; /* clocks on */
306 TIMER_REG(0x08) = 0x6b; /* run timer, clear irq, latch value */
308 pllsetreg0 = memregl[0xf004>>2];
309 memtimex_old[0] = memregs[0x14802>>1];
310 memtimex_old[1] = memregs[0x14804>>1];
312 gp2x_video_flip = gp2x_video_flip_;
313 gp2x_video_flip2 = gp2x_video_flip2_;
314 gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
315 gp2x_video_setpalette = gp2x_video_setpalette_;
316 gp2x_video_RGB_setscaling = gp2x_video_RGB_setscaling_;
317 gp2x_video_wait_vsync = gp2x_video_wait_vsync_;
319 gp2x_set_cpuclk = gp2x_set_cpuclk_;
321 set_lcd_custom_rate = set_lcd_custom_rate_;
322 unset_lcd_custom_rate = unset_lcd_custom_rate_;
323 set_lcd_gamma = set_lcd_gamma_;
325 set_ram_timings = set_ram_timings_;
326 unset_ram_timings = unset_ram_timings_;
327 gp2x_read_battery = gp2x_read_battery_;
329 gp2x_get_ticks_ms = gp2x_get_ticks_ms_;
330 gp2x_get_ticks_us = gp2x_get_ticks_us_;
333 void pollux_finish(void)
335 /* switch to default fb mem, turn portrait off */
336 memregl[0x406C>>2] = fb_paddr[0];
337 memregl[0x4058>>2] |= 0x10;
340 gp2x_video_changemode_ll_(16);
341 unset_ram_timings_();
342 if (cpuclk_was_changed) {
343 memregl[0xf004>>2] = pllsetreg0;
344 memregl[0xf07c>>2] |= 0x8000;
347 munmap((void *)memregs, 0x20000);