wiz blitters, fb restore, tweaks
[ginge.git] / common / wiz_video.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include <sys/ioctl.h>
8#include <sys/mman.h>
9#include <unistd.h>
10#include <linux/fb.h>
11#include "warm.h"
12
13static volatile unsigned short *memregs;
14static volatile unsigned long *memregl;
15int memdev = -1;
16
17#define FB_BUF_COUNT 4
18static unsigned int fb_paddr[FB_BUF_COUNT];
19static int fb_buf_count = FB_BUF_COUNT;
20static int fb_work_buf;
21static int fbdev = -1;
22
23static void *gp2x_screens[FB_BUF_COUNT];
24static void *g_screen_ptr;
25
26
27static void vout_gp2x_flip(void)
28{
29 memregl[0x406C>>2] = fb_paddr[fb_work_buf];
30 memregl[0x4058>>2] |= 0x10;
31
32 fb_work_buf++;
33 if (fb_work_buf >= fb_buf_count)
34 fb_work_buf = 0;
35 g_screen_ptr = gp2x_screens[fb_work_buf];
36}
37
38static int vout_gp2x_init(int no_dblbuf)
39{
40 struct fb_fix_screeninfo fbfix;
41 int i, ret;
42
43 memdev = open("/dev/mem", O_RDWR);
44 if (memdev == -1) {
45 perror("open(/dev/mem) failed");
46 exit(1);
47 }
48
49 memregs = mmap(0, 0x20000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
50 if (memregs == MAP_FAILED) {
51 perror("mmap(memregs) failed");
52 exit(1);
53 }
54 memregl = (volatile void *)memregs;
55
56 fbdev = open("/dev/fb0", O_RDWR);
57 if (fbdev < 0) {
58 perror("can't open fbdev");
59 exit(1);
60 }
61
62 ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix);
63 if (ret == -1) {
64 perror("ioctl(fbdev) failed");
65 exit(1);
66 }
67
68 printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start);
69 fb_paddr[0] = fbfix.smem_start;
70
71 gp2x_screens[0] = mmap(0, 320*240*2*FB_BUF_COUNT, PROT_READ|PROT_WRITE,
72 MAP_SHARED, memdev, fb_paddr[0]);
73 if (gp2x_screens[0] == MAP_FAILED)
74 {
75 perror("mmap(gp2x_screens) failed");
76 exit(1);
77 }
78 memset(gp2x_screens[0], 0, 320*240*2*FB_BUF_COUNT);
79
80 if (!no_dblbuf) {
81 warm_init();
82 ret = warm_change_cb_range(WCB_B_BIT, 1, gp2x_screens[0], 320*240*2*FB_BUF_COUNT);
83 if (ret != 0)
84 fprintf(stderr, "could not make fb buferable.\n");
85 }
86
87 printf(" %p -> %08x\n", gp2x_screens[0], fb_paddr[0]);
88 for (i = 1; i < FB_BUF_COUNT; i++)
89 {
90 fb_paddr[i] = fb_paddr[i-1] + 320*240*2;
91 gp2x_screens[i] = (char *)gp2x_screens[i-1] + 320*240*2;
92 printf(" %p -> %08x\n", gp2x_screens[i], fb_paddr[i]);
93 }
94 fb_work_buf = 0;
95 g_screen_ptr = gp2x_screens[0];
96
97 if (no_dblbuf)
98 fb_buf_count = 1;
99
100 return 0;
101}
102
103static void vout_gp2x_set_mode(int bpp, int rot)
104{
105 int rot_cmd[2] = { 0, 0 };
106 int code = 0, bytes = 2;
107 unsigned int r;
108 int ret;
109
110 rot_cmd[0] = rot ? 6 : 5;
111 ret = ioctl(fbdev, _IOW('D', 90, int[2]), rot_cmd);
112 if (ret < 0)
113 perror("rot ioctl failed");
114
115 memregl[0x4004>>2] = rot ? 0x013f00ef : 0x00ef013f;
116 memregl[0x4000>>2] |= 1 << 3;
117
118 switch (bpp)
119 {
120 case 8:
121 code = 0x443a;
122 bytes = 1;
123 break;
124
125 case 15:
126 case 16:
127 code = 0x4432;
128 bytes = 2;
129 break;
130
131 default:
132 fprintf(stderr, "unhandled bpp request: %d\n", abs(bpp));
133 return;
134 }
135
136 memregl[0x405c>>2] = bytes;
137 memregl[0x4060>>2] = bytes * (rot ? 240 : 320);
138
139 r = memregl[0x4058>>2];
140 r = (r & 0xffff) | (code << 16) | 0x10;
141 memregl[0x4058>>2] = r;
142}
143
144static void vout_gp2x_set_palette(unsigned int *pal, int len)
145{
146 /* pollux palette is 16bpp only.. */
147 int i;
148 for (i = 0; i < len; i++)
149 {
150 int c = pal[i];
151 c = ((c >> 8) & 0xf800) | ((c >> 5) & 0x07c0) | ((c >> 3) & 0x001f);
152 memregl[0x4070>>2] = (i << 24) | c;
153 }
154}
155
156void vout_gp2x_finish(void)
157{
158 memregl[0x406C>>2] = fb_paddr[0];
159 memregl[0x4058>>2] |= 0x10;
160 close(fbdev);
161
162 munmap((void *)memregs, 0x20000);
163 close(memdev);
164
165 warm_finish();
166}
167