| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2009,2013 |
| 3 | * |
| 4 | * This work is licensed under the terms of any of these licenses |
| 5 | * (at your option): |
| 6 | * - GNU GPL, version 2 or later. |
| 7 | * - GNU LGPL, version 2.1 or later. |
| 8 | * - MAME license. |
| 9 | * See the COPYING file in the top-level directory. |
| 10 | * |
| 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) |
| 16 | * </random_info> |
| 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <linux/fb.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include "../libpicofe/gp2x/soc.h" |
| 31 | #include "../libpicofe/gp2x/plat_gp2x.h" |
| 32 | #include "../libpicofe/gp2x/pollux_set.h" |
| 33 | #include "../common/emu.h" |
| 34 | #include "../common/arm_utils.h" |
| 35 | #include "plat.h" |
| 36 | |
| 37 | #define FB_BUF_COUNT 4 |
| 38 | #define FB_MEM_SIZE (320*240*2 * FB_BUF_COUNT) |
| 39 | |
| 40 | static unsigned int fb_paddr[FB_BUF_COUNT]; |
| 41 | static int fb_work_buf; |
| 42 | static int fbdev = -1; |
| 43 | |
| 44 | |
| 45 | /* video stuff */ |
| 46 | static void pollux_video_flip(int buf_count) |
| 47 | { |
| 48 | memregl[0x406C>>2] = memregl[0x446C>>2] = fb_paddr[fb_work_buf]; |
| 49 | memregl[0x4058>>2] |= 0x10; |
| 50 | memregl[0x4458>>2] |= 0x10; |
| 51 | |
| 52 | fb_work_buf++; |
| 53 | if (fb_work_buf >= buf_count) |
| 54 | fb_work_buf = 0; |
| 55 | g_screen_ptr = gp2x_screens[fb_work_buf]; |
| 56 | } |
| 57 | |
| 58 | static void gp2x_video_flip_(void) |
| 59 | { |
| 60 | pollux_video_flip(FB_BUF_COUNT); |
| 61 | } |
| 62 | |
| 63 | /* doulblebuffered flip */ |
| 64 | static void gp2x_video_flip2_(void) |
| 65 | { |
| 66 | pollux_video_flip(2); |
| 67 | } |
| 68 | |
| 69 | static void gp2x_video_changemode_ll_(int bpp, int is_pal) |
| 70 | { |
| 71 | static int prev_bpp = 0; |
| 72 | int code = 0, bytes = 2; |
| 73 | int rot_cmd[2] = { 0, 0 }; |
| 74 | unsigned int r; |
| 75 | char buff[32]; |
| 76 | int ret; |
| 77 | |
| 78 | if (bpp == prev_bpp) |
| 79 | return; |
| 80 | prev_bpp = bpp; |
| 81 | |
| 82 | printf("changemode: %dbpp rot=%d\n", abs(bpp), bpp < 0); |
| 83 | |
| 84 | /* negative bpp means rotated mode */ |
| 85 | rot_cmd[0] = (bpp < 0) ? 6 : 5; |
| 86 | ret = ioctl(fbdev, _IOW('D', 90, int[2]), rot_cmd); |
| 87 | if (ret < 0) |
| 88 | perror("rot ioctl failed"); |
| 89 | memregl[0x4004>>2] = (bpp < 0) ? 0x013f00ef : 0x00ef013f; |
| 90 | memregl[0x4000>>2] |= 1 << 3; |
| 91 | |
| 92 | /* the above ioctl resets LCD timings, so set them here */ |
| 93 | snprintf(buff, sizeof(buff), "POLLUX_LCD_TIMINGS_%s", |
| 94 | is_pal ? "PAL" : "NTSC"); |
| 95 | pollux_set_fromenv(memregs, buff); |
| 96 | |
| 97 | switch (abs(bpp)) |
| 98 | { |
| 99 | case 8: |
| 100 | code = 0x443a; |
| 101 | bytes = 1; |
| 102 | break; |
| 103 | |
| 104 | case 15: |
| 105 | case 16: |
| 106 | code = 0x4432; |
| 107 | bytes = 2; |
| 108 | break; |
| 109 | |
| 110 | default: |
| 111 | printf("unhandled bpp request: %d\n", abs(bpp)); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // program both MLCs so that TV-out works |
| 116 | memregl[0x405c>>2] = memregl[0x445c>>2] = bytes; |
| 117 | memregl[0x4060>>2] = memregl[0x4460>>2] = |
| 118 | bytes * (bpp < 0 ? 240 : 320); |
| 119 | |
| 120 | r = memregl[0x4058>>2]; |
| 121 | r = (r & 0xffff) | (code << 16) | 0x10; |
| 122 | memregl[0x4058>>2] = r; |
| 123 | |
| 124 | r = memregl[0x4458>>2]; |
| 125 | r = (r & 0xffff) | (code << 16) | 0x10; |
| 126 | memregl[0x4458>>2] = r; |
| 127 | } |
| 128 | |
| 129 | static void gp2x_video_setpalette_(int *pal, int len) |
| 130 | { |
| 131 | /* pollux palette is 16bpp only.. */ |
| 132 | int i; |
| 133 | for (i = 0; i < len; i++) |
| 134 | { |
| 135 | int c = pal[i]; |
| 136 | c = ((c >> 8) & 0xf800) | ((c >> 5) & 0x07c0) | ((c >> 3) & 0x001f); |
| 137 | memregl[0x4070>>2] = (i << 24) | c; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static void gp2x_video_RGB_setscaling_(int ln_offs, int W, int H) |
| 142 | { |
| 143 | /* maybe a job for 3d hardware? */ |
| 144 | } |
| 145 | |
| 146 | static void gp2x_video_wait_vsync_(void) |
| 147 | { |
| 148 | while (!(memregl[0x308c>>2] & (1 << 10))) |
| 149 | spend_cycles(128); |
| 150 | memregl[0x308c>>2] |= 1 << 10; |
| 151 | } |
| 152 | |
| 153 | void vid_pollux_init(void) |
| 154 | { |
| 155 | struct fb_fix_screeninfo fbfix; |
| 156 | int i, ret; |
| 157 | |
| 158 | fbdev = open("/dev/fb0", O_RDWR); |
| 159 | if (fbdev < 0) { |
| 160 | perror("can't open fbdev"); |
| 161 | exit(1); |
| 162 | } |
| 163 | |
| 164 | ret = ioctl(fbdev, FBIOGET_FSCREENINFO, &fbfix); |
| 165 | if (ret == -1) { |
| 166 | perror("ioctl(fbdev) failed"); |
| 167 | exit(1); |
| 168 | } |
| 169 | |
| 170 | printf("framebuffer: \"%s\" @ %08lx\n", fbfix.id, fbfix.smem_start); |
| 171 | fb_paddr[0] = fbfix.smem_start; |
| 172 | |
| 173 | gp2x_screens[0] = mmap(0, FB_MEM_SIZE, PROT_READ|PROT_WRITE, |
| 174 | MAP_SHARED, memdev, fb_paddr[0]); |
| 175 | if (gp2x_screens[0] == MAP_FAILED) |
| 176 | { |
| 177 | perror("mmap(gp2x_screens) failed"); |
| 178 | exit(1); |
| 179 | } |
| 180 | memset(gp2x_screens[0], 0, FB_MEM_SIZE); |
| 181 | |
| 182 | printf(" %p -> %08x\n", gp2x_screens[0], fb_paddr[0]); |
| 183 | for (i = 1; i < FB_BUF_COUNT; i++) |
| 184 | { |
| 185 | fb_paddr[i] = fb_paddr[i-1] + 320*240*2; |
| 186 | gp2x_screens[i] = (char *)gp2x_screens[i-1] + 320*240*2; |
| 187 | printf(" %p -> %08x\n", gp2x_screens[i], fb_paddr[i]); |
| 188 | } |
| 189 | fb_work_buf = 0; |
| 190 | g_screen_ptr = gp2x_screens[0]; |
| 191 | |
| 192 | gp2x_video_flip = gp2x_video_flip_; |
| 193 | gp2x_video_flip2 = gp2x_video_flip2_; |
| 194 | gp2x_video_changemode_ll = gp2x_video_changemode_ll_; |
| 195 | gp2x_video_setpalette = gp2x_video_setpalette_; |
| 196 | gp2x_video_RGB_setscaling = gp2x_video_RGB_setscaling_; |
| 197 | gp2x_video_wait_vsync = gp2x_video_wait_vsync_; |
| 198 | } |
| 199 | |
| 200 | void vid_pollux_finish(void) |
| 201 | { |
| 202 | memset(gp2x_screens[0], 0, FB_MEM_SIZE); |
| 203 | munmap(gp2x_screens[0], FB_MEM_SIZE); |
| 204 | close(fbdev); |
| 205 | fbdev = -1; |
| 206 | } |