2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
11 * <random_info=mem_map>
12 * 00000000-029fffff linux (42MB)
13 * 02a00000-02dfffff fb (4MB, 153600B really used)
14 * 02e00000-02ffffff sound dma (2MB)
15 * 03000000-03ffffff MPEGDEC (?, 16MB)
23 #include <sys/types.h>
28 #include <sys/ioctl.h>
30 #include <linux/soundcard.h>
33 #include "plat_gp2x.h"
36 volatile unsigned short *memregs;
37 volatile unsigned int *memregl;
41 static int battdev = -1, mixerdev = -1;
42 static int cpu_clock_allowed;
43 static unsigned int saved_video_regs[2][6];
46 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
51 extern void *gp2x_screens[4];
53 #define fb_buf_count 4
54 static unsigned int fb_paddr[fb_buf_count];
55 static int fb_work_buf;
56 static int fbdev = -1;
58 static char cpuclk_was_changed = 0;
59 static unsigned short memtimex_old[2];
60 static unsigned int pllsetreg0_old;
61 static unsigned int timer_drift; // count per real second
62 static int last_pal_setting = 0;
66 static void pollux_set_fromenv(const char *env_var)
68 const char *set_string;
69 set_string = getenv(env_var);
71 pollux_set(memregs, set_string);
73 printf("env var %s not defined.\n", env_var);
77 static void pollux_video_flip(int buf_count)
79 memregl[0x406C>>2] = fb_paddr[fb_work_buf];
80 memregl[0x4058>>2] |= 0x10;
82 if (fb_work_buf >= buf_count)
84 g_screen_ptr = gp2x_screens[fb_work_buf];
87 static void gp2x_video_flip_(void)
89 pollux_video_flip(fb_buf_count);
92 /* doulblebuffered flip */
93 static void gp2x_video_flip2_(void)
98 static void gp2x_video_changemode_ll_(int bpp)
100 static int prev_bpp = 0;
101 int code = 0, bytes = 2;
102 int rot_cmd[2] = { 0, 0 };
111 printf("changemode: %dbpp rot=%d\n", abs(bpp), bpp < 0);
113 /* negative bpp means rotated mode */
114 rot_cmd[0] = (bpp < 0) ? 6 : 5;
115 ret = ioctl(fbdev, _IOW('D', 90, int[2]), rot_cmd);
117 perror("rot ioctl failed");
118 memregl[0x4004>>2] = (bpp < 0) ? 0x013f00ef : 0x00ef013f;
119 memregl[0x4000>>2] |= 1 << 3;
121 /* the above ioctl resets LCD timings, so set them here */
122 snprintf(buff, sizeof(buff), "POLLUX_LCD_TIMINGS_%s", last_pal_setting ? "PAL" : "NTSC");
123 pollux_set_fromenv(buff);
139 printf("unhandled bpp request: %d\n", abs(bpp));
143 memregl[0x405c>>2] = bytes;
144 memregl[0x4060>>2] = bytes * (bpp < 0 ? 240 : 320);
146 r = memregl[0x4058>>2];
147 r = (r & 0xffff) | (code << 16) | 0x10;
148 memregl[0x4058>>2] = r;
151 static void gp2x_video_setpalette_(int *pal, int len)
153 /* pollux palette is 16bpp only.. */
155 for (i = 0; i < len; i++)
158 c = ((c >> 8) & 0xf800) | ((c >> 5) & 0x07c0) | ((c >> 3) & 0x001f);
159 memregl[0x4070>>2] = (i << 24) | c;
163 static void gp2x_video_RGB_setscaling_(int ln_offs, int W, int H)
165 /* maybe a job for 3d hardware? */
168 static void gp2x_video_wait_vsync_(void)
170 while (!(memregl[0x308c>>2] & (1 << 10)))
172 memregl[0x308c>>2] |= 1 << 10;
176 static void gp2x_set_cpuclk_(unsigned int mhz)
179 snprintf(buff, sizeof(buff), "cpuclk=%u", mhz);
180 pollux_set(memregs, buff);
182 cpuclk_was_changed = 1;
186 static void set_ram_timings_(void)
188 pollux_set_fromenv("POLLUX_RAM_TIMINGS");
191 static void unset_ram_timings_(void)
195 memregs[0x14802>>1] = memtimex_old[0];
196 memregs[0x14804>>1] = memtimex_old[1] | 0x8000;
198 for (i = 0; i < 0x100000; i++)
199 if (!(memregs[0x14804>>1] & 0x8000))
202 printf("RAM timings reset to startup values.\n");
206 static void set_lcd_custom_rate_(int is_pal)
208 /* just remember PAL/NTSC. We always set timings in _changemode_ll() */
209 last_pal_setting = is_pal;
212 static void unset_lcd_custom_rate_(void)
216 static void set_lcd_gamma_(int g100, int A_SNs_curve)
218 /* hm, the LCD possibly can do it (but not POLLUX) */
221 static int gp2x_read_battery_(void)
223 unsigned short magic_val = 0;
227 if (read(battdev, &magic_val, sizeof(magic_val)) != sizeof(magic_val))
238 #define TIMER_BASE3 0x1980
239 #define TIMER_REG(x) memregl[(TIMER_BASE3 + x) >> 2]
241 static unsigned int gp2x_get_ticks_us_(void)
243 TIMER_REG(0x08) = 0x4b; /* run timer, latch value */
247 static unsigned int gp2x_get_ticks_ms_(void)
249 /* approximate /= 1000 */
250 unsigned long long v64;
251 v64 = (unsigned long long)gp2x_get_ticks_us_() * 4294968;
255 int pollux_get_real_snd_rate(int req_rate)
257 int clk0_src, clk1_src, rate, div;
259 clk0_src = (memregl[0xdbc4>>2] >> 1) & 7;
260 clk1_src = (memregl[0xdbc8>>2] >> 1) & 7;
261 if (clk0_src > 1 || clk1_src != 7) {
262 fprintf(stderr, "get_real_snd_rate: bad clk sources: %d %d\n", clk0_src, clk1_src);
266 rate = decode_pll(clk0_src ? memregl[0xf008>>2] : memregl[0xf004>>2]);
269 div = ((memregl[0xdbc4>>2] >> 4) & 0x1f) + 1;
271 div = ((memregl[0xdbc8>>2] >> 4) & 0x1f) + 1;
275 //printf("rate %d\n", rate);
276 rate -= rate * timer_drift / 1000000;
277 printf("adjusted rate: %d\n", rate);
279 if (rate < 8000-1000 || rate > 44100+1000) {
280 fprintf(stderr, "get_real_snd_rate: got bad rate: %d\n", rate);
287 void pollux_init(void)
289 struct fb_fix_screeninfo fbfix;
290 int i, ret, rate, timer_div;
292 memdev = open("/dev/mem", O_RDWR);
294 perror("open(/dev/mem) failed");
298 memregs = mmap(0, 0x20000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
299 if (memregs == MAP_FAILED) {
300 perror("mmap(memregs) failed");
303 memregl = (volatile void *)memregs;
305 fbdev = open("/dev/fb0", O_RDWR);
307 perror("can't open fbdev");
311 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
313 perror("ioctl(fbdev) failed");
317 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
318 fb_paddr[0] = fbfix.smem_start;
320 gp2x_screens[0] = mmap(0, 320*240*2*fb_buf_count, PROT_READ|PROT_WRITE,
321 MAP_SHARED, memdev, fb_paddr[0]);
322 if (gp2x_screens[0] == MAP_FAILED)
324 perror("mmap(gp2x_screens) failed");
327 memset(gp2x_screens[0], 0, 320*240*2*fb_buf_count);
329 printf(" %p -> %08x\n", gp2x_screens[0], fb_paddr[0]);
330 for (i = 1; i < fb_buf_count; i++)
332 fb_paddr[i] = fb_paddr[i-1] + 320*240*2;
333 gp2x_screens[i] = (char *)gp2x_screens[i-1] + 320*240*2;
334 printf(" %p -> %08x\n", gp2x_screens[i], fb_paddr[i]);
337 g_screen_ptr = gp2x_screens[0];
339 battdev = open("/dev/pollux_batt", O_RDONLY);
341 perror("Warning: could't open pollux_batt");
343 /* find what PLL1 runs at, for the timer */
344 rate = decode_pll(memregl[0xf008>>2]);
345 printf("PLL1 @ %dHz\n", rate);
348 timer_div = (rate + 500000) / 1000000;
349 if (1 <= timer_div && timer_div <= 256) {
350 timer_drift = (rate - (timer_div * 1000000)) / timer_div;
352 if (TIMER_REG(0x08) & 8) {
353 fprintf(stderr, "warning: timer in use, overriding!\n");
357 TIMER_REG(0x44) = ((timer_div - 1) << 4) | 2; /* using PLL1, divide by it's rate */
358 TIMER_REG(0x40) = 0x0c; /* clocks on */
359 TIMER_REG(0x08) = 0x6b; /* run timer, clear irq, latch value */
361 gp2x_get_ticks_ms = gp2x_get_ticks_ms_;
362 gp2x_get_ticks_us = gp2x_get_ticks_us_;
365 fprintf(stderr, "warning: could not make use of timer\n");
367 // those functions are actually not good at all on Wiz kernel
368 gp2x_get_ticks_ms = plat_get_ticks_ms_good;
369 gp2x_get_ticks_us = plat_get_ticks_us_good;
372 pllsetreg0_old = memregl[0xf004>>2];
373 memtimex_old[0] = memregs[0x14802>>1];
374 memtimex_old[1] = memregs[0x14804>>1];
376 gp2x_video_flip = gp2x_video_flip_;
377 gp2x_video_flip2 = gp2x_video_flip2_;
378 gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
379 gp2x_video_setpalette = gp2x_video_setpalette_;
380 gp2x_video_RGB_setscaling = gp2x_video_RGB_setscaling_;
381 gp2x_video_wait_vsync = gp2x_video_wait_vsync_;
383 /* some firmwares have sys clk on PLL0, we can't adjust CPU clock
384 * by reprogramming the PLL0 then, as it overclocks system bus */
385 if ((memregl[0xf000>>2] & 0x03000030) == 0x01000000)
386 gp2x_set_cpuclk = gp2x_set_cpuclk_;
388 fprintf(stderr, "unexpected PLL config (%08x), overclocking disabled\n",
390 gp2x_set_cpuclk = NULL;
393 set_lcd_custom_rate = set_lcd_custom_rate_;
394 unset_lcd_custom_rate = unset_lcd_custom_rate_;
395 set_lcd_gamma = set_lcd_gamma_;
397 set_ram_timings = set_ram_timings_;
398 unset_ram_timings = unset_ram_timings_;
399 gp2x_read_battery = gp2x_read_battery_;
402 void pollux_finish(void)
404 /* switch to default fb mem, turn portrait off */
405 memregl[0x406C>>2] = fb_paddr[0];
406 memregl[0x4058>>2] |= 0x10;
409 gp2x_video_changemode_ll_(16);
410 unset_ram_timings_();
411 if (cpuclk_was_changed) {
412 memregl[0xf004>>2] = pllsetreg0_old;
413 memregl[0xf07c>>2] |= 0x8000;
417 munmap((void *)memregs, 0x20000);
424 /* note: both PLLs are programmed the same way,
425 * the databook incorrectly states that PLL1 differs */
426 static int decode_pll(unsigned int reg)
431 p = (reg >> 18) & 0x3f;
432 m = (reg >> 8) & 0x3ff;
438 v = 27000000; // master clock
439 v = v * m / (p << s);
443 #define TIMER_BASE3 0x1980
444 #define TIMER_REG(x) memregl[(TIMER_BASE3 + x) >> 2]
446 static void timer_cleanup(void)
448 TIMER_REG(0x40) = 0x0c; /* be sure clocks are on */
449 TIMER_REG(0x08) = 0x23; /* stop the timer, clear irq in case it's pending */
450 TIMER_REG(0x00) = 0; /* clear counter */
451 TIMER_REG(0x40) = 0; /* clocks off */
452 TIMER_REG(0x44) = 0; /* dividers back to default */
455 static void save_multiple_regs(unsigned int *dest, int base, int count)
457 const volatile unsigned int *regs = memregl + base / 4;
460 for (i = 0; i < count; i++)
464 static void restore_multiple_regs(int base, const unsigned int *src, int count)
466 volatile unsigned int *regs = memregl + base / 4;
469 for (i = 0; i < count; i++)
474 static int pollux_cpu_clock_get(void)
476 return decode_pll(memregl[0xf004>>2]) / 1000000;
479 int pollux_cpu_clock_set(int mhz)
481 int adiv, mdiv, pdiv, sdiv = 0;
484 if (!cpu_clock_allowed)
486 if (mhz == pollux_cpu_clock_get())
489 // m = MDIV, p = PDIV, s = SDIV
490 #define SYS_CLK_FREQ 27
492 mdiv = (mhz * pdiv) / SYS_CLK_FREQ;
495 vf004 = (pdiv<<18) | (mdiv<<8) | sdiv;
497 // attempt to keep the AHB divider close to 250, but not higher
498 for (adiv = 1; mhz / adiv > 250; adiv++)
501 vf000 = memregl[0xf000>>2];
502 vf000 = (vf000 & ~0x3c0) | ((adiv - 1) << 6);
503 memregl[0xf000>>2] = vf000;
504 memregl[0xf004>>2] = vf004;
505 memregl[0xf07c>>2] |= 0x8000;
506 for (i = 0; (memregl[0xf07c>>2] & 0x8000) && i < 0x100000; i++)
509 printf("clock set to %dMHz, AHB set to %dMHz\n", mhz, mhz / adiv);
513 static int pollux_bat_capacity_get(void)
515 unsigned short magic_val = 0;
519 if (read(battdev, &magic_val, sizeof(magic_val)) != sizeof(magic_val))
530 static int step_volume(int is_up)
532 static int volume = 50;
540 if (volume > 255) volume = 255;
544 if (volume < 0) volume = 0;
549 ret = ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &val);
556 struct plat_target plat_target = {
557 pollux_cpu_clock_get,
558 pollux_cpu_clock_set,
559 pollux_bat_capacity_get,
561 .step_volume = step_volume,
564 int plat_target_init(void)
566 int rate, timer_div, timer_div2;
569 memdev = open("/dev/mem", O_RDWR);
571 perror("open(/dev/mem) failed");
575 memregs = mmap(0, 0x20000, PROT_READ|PROT_WRITE, MAP_SHARED,
577 if (memregs == MAP_FAILED) {
578 perror("mmap(memregs) failed");
581 memregl = (volatile void *)memregs;
583 // save video regs of both MLCs
584 save_multiple_regs(saved_video_regs[0], 0x4058, ARRAY_SIZE(saved_video_regs[0]));
585 save_multiple_regs(saved_video_regs[1], 0x4458, ARRAY_SIZE(saved_video_regs[1]));
587 /* some firmwares have sys clk on PLL0, we can't adjust CPU clock
588 * by reprogramming the PLL0 then, as it overclocks system bus */
589 if ((memregl[0xf000>>2] & 0x03000030) == 0x01000000)
590 cpu_clock_allowed = 1;
592 cpu_clock_allowed = 0;
593 fprintf(stderr, "unexpected PLL config (%08x), overclocking disabled\n",
597 /* find what PLL1 runs at, for the timer */
598 rate = decode_pll(memregl[0xf008>>2]);
599 printf("PLL1 @ %dHz\n", rate);
602 timer_div = (rate + 500000) / 1000000;
604 while (timer_div > 256) {
608 if (1 <= timer_div && timer_div <= 256 && timer_div2 < 4) {
609 int timer_rate = (rate >> timer_div2) / timer_div;
610 if (TIMER_REG(0x08) & 8) {
611 fprintf(stderr, "warning: timer in use, overriding!\n");
614 if (timer_rate != 1000000)
615 fprintf(stderr, "warning: timer drift %d us\n", timer_rate - 1000000);
617 timer_div2 = (timer_div2 + 3) & 3;
618 TIMER_REG(0x44) = ((timer_div - 1) << 4) | 2; /* using PLL1 */
619 TIMER_REG(0x40) = 0x0c; /* clocks on */
620 TIMER_REG(0x08) = 0x68 | timer_div2; /* run timer, clear irq, latch value */
623 fprintf(stderr, "warning: could not make use of timer\n");
625 battdev = open("/dev/pollux_batt", O_RDONLY);
627 perror("Warning: could't open pollux_batt");
629 f = fopen("/dev/accel", "rb");
631 printf("detected Caanoo\n");
632 gp2x_dev_id = GP2X_DEV_CAANOO;
636 printf("detected Wiz\n");
637 gp2x_dev_id = GP2X_DEV_WIZ;
640 mixerdev = open("/dev/mixer", O_RDWR);
642 perror("open(/dev/mixer)");
647 /* to be called after in_probe */
648 void plat_target_setup_input(void)
652 void plat_target_finish(void)
656 restore_multiple_regs(0x4058, saved_video_regs[0],
657 ARRAY_SIZE(saved_video_regs[0]));
658 restore_multiple_regs(0x4458, saved_video_regs[1],
659 ARRAY_SIZE(saved_video_regs[1]));
660 memregl[0x4058>>2] |= 0x10;
661 memregl[0x4458>>2] |= 0x10;
667 munmap((void *)memregs, 0x20000);