switch over to libpicofe
[pcsx_rearmed.git] / frontend / plat_pollux.c
index 52a09b1..32af9a6 100644 (file)
 #include <linux/soundcard.h>
 #include <linux/input.h>
 
-#include "common/input.h"
-#include "gp2x/in_gp2x.h"
-#include "linux/in_evdev.h"
-#include "common/menu.h"
+#include "libpicofe/plat.h"
+#include "libpicofe/input.h"
+#include "libpicofe/gp2x/in_gp2x.h"
+#include "libpicofe/gp2x/soc_pollux.h"
+#include "libpicofe/linux/in_evdev.h"
+#include "libpicofe/menu.h"
 #include "warm/warm.h"
 #include "plugin_lib.h"
 #include "pl_gun_ts.h"
 #include "pcnt.h"
 #include "../plugins/gpulib/cspace.h"
 
-int gp2x_dev_id;
 
-static int fbdev = -1, memdev = -1, battdev = -1, mixerdev = -1;
-static volatile unsigned short *memregs;
-static volatile unsigned int   *memregl;
+static int fbdev = -1;
 static void *fb_vaddrs[2];
 static unsigned int fb_paddrs[2];
 static int fb_work_buf;
-static int cpu_clock_allowed, have_warm;
-static unsigned int saved_video_regs[2][6];
+static int have_warm;
 #define FB_VRAM_SIZE (320*240*2*2*2) // 2 buffers with space for 24bpp mode
 
 static unsigned short *psx_vram;
@@ -123,88 +121,20 @@ static void pollux_changemode(int bpp, int is_bgr)
        memregl[0x4458>>2] = r;
 }
 
-/* note: both PLLs are programmed the same way,
- * the databook incorrectly states that PLL1 differs */
-static int decode_pll(unsigned int reg)
+static int cpu_clock_wrapper(int mhz)
 {
-       long long v;
-       int p, m, s;
-
-       p = (reg >> 18) & 0x3f;
-       m = (reg >> 8) & 0x3ff;
-       s = reg & 0xff;
-
-       if (p == 0)
-               p = 1;
-
-       v = 27000000; // master clock
-       v = v * m / (p << s);
-       return v;
-}
-
-int plat_cpu_clock_get(void)
-{
-       return decode_pll(memregl[0xf004>>2]) / 1000000;
-}
-
-int plat_cpu_clock_apply(int mhz)
-{
-       int adiv, mdiv, pdiv, sdiv = 0;
-       int i, vf000, vf004;
-
-       if (!cpu_clock_allowed)
-               return -1;
-       if (mhz == plat_cpu_clock_get())
-               return 0;
-
-       // m = MDIV, p = PDIV, s = SDIV
-       #define SYS_CLK_FREQ 27
-       pdiv = 9;
-       mdiv = (mhz * pdiv) / SYS_CLK_FREQ;
-       if (mdiv & ~0x3ff)
-               return -1;
-       vf004 = (pdiv<<18) | (mdiv<<8) | sdiv;
-
-       // attempt to keep the AHB divider close to 250, but not higher
-       for (adiv = 1; mhz / adiv > 250; adiv++)
-               ;
-
-       vf000 = memregl[0xf000>>2];
-       vf000 = (vf000 & ~0x3c0) | ((adiv - 1) << 6);
-       memregl[0xf000>>2] = vf000;
-       memregl[0xf004>>2] = vf004;
-       memregl[0xf07c>>2] |= 0x8000;
-       for (i = 0; (memregl[0xf07c>>2] & 0x8000) && i < 0x100000; i++)
-               ;
-
-       printf("clock set to %dMHz, AHB set to %dMHz\n", mhz, mhz / adiv);
-
        // stupid pll share hack - must restart audio
+       int pollux_cpu_clock_set(int cpu_clock);
        extern long SPUopen(void);
        extern long SPUclose(void);
+
+       pollux_cpu_clock_set(mhz);
        SPUclose();
        SPUopen();
 
        return 0;
 }
 
-int plat_get_bat_capacity(void)
-{
-       unsigned short magic_val = 0;
-
-       if (battdev < 0)
-               return -1;
-       if (read(battdev, &magic_val, sizeof(magic_val)) != sizeof(magic_val))
-               return -1;
-       switch (magic_val) {
-       default:
-       case 1: return 100;
-       case 2: return 66;
-       case 3: return 40;
-       case 4: return 0;
-       }
-}
-
 #define TIMER_BASE3 0x1980
 #define TIMER_REG(x) memregl[(TIMER_BASE3 + x) >> 2]
 
@@ -214,15 +144,6 @@ static __attribute__((unused)) unsigned int timer_get(void)
        return TIMER_REG(0);
 }
 
-static void timer_cleanup(void)
-{
-       TIMER_REG(0x40) = 0x0c; /* be sure clocks are on */
-       TIMER_REG(0x08) = 0x23; /* stop the timer, clear irq in case it's pending */
-       TIMER_REG(0x00) = 0;    /* clear counter */
-       TIMER_REG(0x40) = 0;    /* clocks off */
-       TIMER_REG(0x44) = 0;    /* dividers back to default */
-}
-
 void plat_video_menu_enter(int is_rom_loaded)
 {
        if (pl_vout_buf != NULL) {
@@ -476,48 +397,13 @@ void plat_gvideo_close(void)
 {
 }
 
-static void save_multiple_regs(unsigned int *dest, int base, int count)
-{
-       const volatile unsigned int *regs = memregl + base / 4;
-       int i;
-
-       for (i = 0; i < count; i++)
-               dest[i] = regs[i];
-}
-
-static void restore_multiple_regs(int base, const unsigned int *src, int count)
-{
-       volatile unsigned int *regs = memregl + base / 4;
-       int i;
-
-       for (i = 0; i < count; i++)
-               regs[i] = src[i];
-}
-
 void plat_init(void)
 {
        const char *main_fb_name = "/dev/fb0";
        struct fb_fix_screeninfo fbfix;
-       int rate, timer_div, timer_div2;
-       int fbdev, ret, warm_ret;
-       FILE *f;
-
-       memdev = open("/dev/mem", O_RDWR);
-       if (memdev == -1) {
-               perror("open(/dev/mem) failed");
-               exit(1);
-       }
-
-       memregs = mmap(0, 0x20000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
-       if (memregs == MAP_FAILED) {
-               perror("mmap(memregs) failed");
-               exit(1);
-       }
-       memregl = (volatile void *)memregs;
+       int ret;
 
-       // save video regs of both MLCs
-       save_multiple_regs(saved_video_regs[0], 0x4058, ARRAY_SIZE(saved_video_regs[0]));
-       save_multiple_regs(saved_video_regs[1], 0x4458, ARRAY_SIZE(saved_video_regs[1]));
+       plat_target_init();
 
        fbdev = open(main_fb_name, O_RDWR);
        if (fbdev == -1) {
@@ -544,71 +430,19 @@ void plat_init(void)
        fb_vaddrs[1] = (char *)fb_vaddrs[0] + 320*240*4;
 
        memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
+
        pollux_changemode(16, 0);
        g_menuscreen_w = 320;
        g_menuscreen_h = 240;
        g_menuscreen_ptr = fb_flip();
 
-       warm_ret = warm_init();
-       have_warm = warm_ret == 0;
+       ret = warm_init();
+       have_warm = (ret == 0);
        warm_change_cb_upper(WCB_B_BIT, 1);
 
-       /* some firmwares have sys clk on PLL0, we can't adjust CPU clock
-        * by reprogramming the PLL0 then, as it overclocks system bus */
-       if ((memregl[0xf000>>2] & 0x03000030) == 0x01000000)
-               cpu_clock_allowed = 1;
-       else {
-               cpu_clock_allowed = 0;
-               fprintf(stderr, "unexpected PLL config (%08x), overclocking disabled\n",
-                       memregl[0xf000>>2]);
-       }
-
-       /* find what PLL1 runs at, for the timer */
-       rate = decode_pll(memregl[0xf008>>2]);
-       printf("PLL1 @ %dHz\n", rate);
-
-       /* setup timer */
-       timer_div = (rate + 500000) / 1000000;
-       timer_div2 = 0;
-       while (timer_div > 256) {
-               timer_div /= 2;
-               timer_div2++;
-       }
-       if (1 <= timer_div && timer_div <= 256 && timer_div2 < 4) {
-               int timer_rate = (rate >> timer_div2) / timer_div;
-               if (TIMER_REG(0x08) & 8) {
-                       fprintf(stderr, "warning: timer in use, overriding!\n");
-                       timer_cleanup();
-               }
-               if (timer_rate != 1000000)
-                       fprintf(stderr, "warning: timer drift %d us\n", timer_rate - 1000000);
-
-               timer_div2 = (timer_div2 + 3) & 3;
-               TIMER_REG(0x44) = ((timer_div - 1) << 4) | 2;   /* using PLL1 */
-               TIMER_REG(0x40) = 0x0c;                         /* clocks on */
-               TIMER_REG(0x08) = 0x68 | timer_div2;            /* run timer, clear irq, latch value */
-       }
-       else
-               fprintf(stderr, "warning: could not make use of timer\n");
-
        /* setup DMA */
        DMA_REG(0x0c) = 0x20000; // pending IRQ clear
 
-       battdev = open("/dev/pollux_batt", O_RDONLY);
-       if (battdev < 0)
-               perror("Warning: could't open pollux_batt");
-
-       f = fopen("/dev/accel", "rb");
-       if (f) {
-               printf("detected Caanoo\n");
-               gp2x_dev_id = GP2X_DEV_CAANOO;
-               fclose(f);
-       }
-       else {
-               printf("detected Wiz\n");
-               gp2x_dev_id = GP2X_DEV_WIZ;
-       }
-
        in_tsbutton_init();
        in_evdev_init(in_evdev_defbinds);
        if (gp2x_dev_id == GP2X_DEV_CAANOO)
@@ -616,10 +450,6 @@ void plat_init(void)
        else
                wiz_init();
 
-       mixerdev = open("/dev/mixer", O_RDWR);
-       if (mixerdev == -1)
-               perror("open(/dev/mixer)");
-
        pl_rearmed_cbs.pl_vout_flip = have_warm ? raw_flip_dma : raw_flip_soft;
        pl_rearmed_cbs.pl_vout_set_raw_vram = pl_vout_set_raw_vram;
 
@@ -629,27 +459,19 @@ void plat_init(void)
 
        pl_rearmed_cbs.screen_w = 320;
        pl_rearmed_cbs.screen_h = 240;
+
+       plat_target_setup_input();
+
+       plat_target.cpu_clock_set = cpu_clock_wrapper;
 }
 
 void plat_finish(void)
 {
        warm_finish();
-       timer_cleanup();
-
        memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
-       restore_multiple_regs(0x4058, saved_video_regs[0], ARRAY_SIZE(saved_video_regs[0]));
-       restore_multiple_regs(0x4458, saved_video_regs[1], ARRAY_SIZE(saved_video_regs[1]));
-       memregl[0x4058>>2] |= 0x10;
-       memregl[0x4458>>2] |= 0x10;
        munmap(fb_vaddrs[0], FB_VRAM_SIZE);
        close(fbdev);
-
-       if (battdev >= 0)
-               close(battdev);
-       if (mixerdev >= 0)
-               close(mixerdev);
-       munmap((void *)memregs, 0x20000);
-       close(memdev);
+       plat_target_finish();
 }
 
 /* WIZ RAM lack workaround */
@@ -822,30 +644,6 @@ static const struct in_default_bind in_gp2x_defbinds[] =
        { 0, 0, 0 },
 };
 
-void plat_step_volume(int is_up)
-{
-       static int volume = 50;
-       int ret, val;
-
-       if (mixerdev < 0)
-               return;
-
-       if (is_up) {
-               volume += 5;
-               if (volume > 255) volume = 255;
-       }
-       else {
-               volume -= 5;
-               if (volume < 0) volume = 0;
-       }
-       val = volume;
-       val |= val << 8;
-
-       ret = ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &val);
-       if (ret == -1)
-               perror("WRITE_PCM");
-}
-
 // unused dummy for in_gp2x
 volatile unsigned short *gp2x_memregs;