| 1 | /* |
| 2 | * (C) GraÅžvydas "notaz" Ignotas, 2006-2012 |
| 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 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <math.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <sys/mman.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <unistd.h> |
| 22 | #include <linux/soundcard.h> |
| 23 | |
| 24 | #include "soc.h" |
| 25 | #include "soc_mmsp2.h" |
| 26 | #include "plat_gp2x.h" |
| 27 | #include "../linux/sndout_oss.h" |
| 28 | #include "../plat.h" |
| 29 | |
| 30 | static int mixerdev = -1; |
| 31 | static int touchdev = -1; |
| 32 | static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 }; |
| 33 | |
| 34 | static char gamma_was_changed = 0; |
| 35 | static char cpuclk_was_changed = 0; |
| 36 | static unsigned short memtimex_old[2]; |
| 37 | static unsigned short reg0910; |
| 38 | |
| 39 | /* 940 */ |
| 40 | void pause940(int yes) |
| 41 | { |
| 42 | if (yes) |
| 43 | memregs[0x0904>>1] &= 0xFFFE; |
| 44 | else |
| 45 | memregs[0x0904>>1] |= 1; |
| 46 | } |
| 47 | |
| 48 | void reset940(int yes, int bank) |
| 49 | { |
| 50 | memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * CPU clock |
| 55 | * Fout = (m * Fin) / (p * 2^s) |
| 56 | * m = MDIV+8, p = PDIV+2, s = SDIV |
| 57 | * |
| 58 | * m = (Fout * p * 2^s) / Fin |
| 59 | */ |
| 60 | |
| 61 | #define SYS_CLK_FREQ 7372800 |
| 62 | |
| 63 | static int cpu_current_mhz = 200; |
| 64 | |
| 65 | static int mmsp2_clock_get(void) |
| 66 | { |
| 67 | // TODO: read the actual value? |
| 68 | return cpu_current_mhz; |
| 69 | } |
| 70 | |
| 71 | static int mmsp2_clock_set(int mhz) |
| 72 | { |
| 73 | unsigned int mdiv, pdiv, sdiv = 0; |
| 74 | unsigned int v; |
| 75 | int i; |
| 76 | |
| 77 | pdiv = 3; |
| 78 | mdiv = (mhz * pdiv * 1000000) / SYS_CLK_FREQ; |
| 79 | if (mdiv & ~0xff) { |
| 80 | fprintf(stderr, "invalid cpuclk MHz: %u\n", mhz); |
| 81 | return -1; |
| 82 | } |
| 83 | v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv; |
| 84 | memregs[0x910>>1] = v; |
| 85 | |
| 86 | for (i = 0; i < 10000; i++) |
| 87 | if (!(memregs[0x902>>1] & 1)) |
| 88 | break; |
| 89 | |
| 90 | cpuclk_was_changed = 1; |
| 91 | cpu_current_mhz = mhz; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* RAM timings */ |
| 96 | #define TIMING_CHECK(t, adj, mask) \ |
| 97 | t += adj; \ |
| 98 | if (t & ~mask) \ |
| 99 | goto bad |
| 100 | |
| 101 | static __attribute__((noinline)) void spend_cycles(int c) |
| 102 | { |
| 103 | asm volatile( |
| 104 | "0: subs %0, %0, #1 ;" |
| 105 | "bgt 0b" |
| 106 | : "=r" (c) : "0" (c) : "cc"); |
| 107 | } |
| 108 | |
| 109 | static void set_ram_timing_vals(int tCAS, int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD) |
| 110 | { |
| 111 | int i; |
| 112 | TIMING_CHECK(tCAS, -2, 0x1); |
| 113 | TIMING_CHECK(tRC, -1, 0xf); |
| 114 | TIMING_CHECK(tRAS, -1, 0xf); |
| 115 | TIMING_CHECK(tWR, -1, 0xf); |
| 116 | TIMING_CHECK(tMRD, -1, 0xf); |
| 117 | TIMING_CHECK(tRFC, -1, 0xf); |
| 118 | TIMING_CHECK(tRP, -1, 0xf); |
| 119 | TIMING_CHECK(tRCD, -1, 0xf); |
| 120 | |
| 121 | /* get spend_cycles() into cache */ |
| 122 | spend_cycles(1); |
| 123 | |
| 124 | memregs[0x3802>>1] = ((tMRD & 0xF) << 12) | ((tRFC & 0xF) << 8) | ((tRP & 0xF) << 4) | (tRCD & 0xF); |
| 125 | memregs[0x3804>>1] = 0x8000 | ((tCAS & 1) << 12) | ((tRC & 0xF) << 8) | ((tRAS & 0xF) << 4) | (tWR & 0xF); |
| 126 | |
| 127 | /* be sure we don't access the mem while it's being reprogrammed */ |
| 128 | spend_cycles(128*1024); |
| 129 | for (i = 0; i < 8*1024; i++) |
| 130 | if (!(memregs[0x3804>>1] & 0x8000)) |
| 131 | break; |
| 132 | |
| 133 | printf("RAM timings set.\n"); |
| 134 | return; |
| 135 | bad: |
| 136 | fprintf(stderr, "RAM timings invalid.\n"); |
| 137 | } |
| 138 | |
| 139 | static void set_ram_timings_(void) |
| 140 | { |
| 141 | /* craigix: --cas 2 --trc 6 --tras 4 --twr 1 --tmrd 1 --trfc 1 --trp 2 --trcd 2 */ |
| 142 | set_ram_timing_vals(2, 6, 4, 1, 1, 1, 2, 2); |
| 143 | } |
| 144 | |
| 145 | static void unset_ram_timings_(void) |
| 146 | { |
| 147 | memregs[0x3802>>1] = memtimex_old[0]; |
| 148 | memregs[0x3804>>1] = memtimex_old[1] | 0x8000; |
| 149 | printf("RAM timings reset to startup values.\n"); |
| 150 | } |
| 151 | |
| 152 | /* LCD refresh */ |
| 153 | typedef struct |
| 154 | { |
| 155 | unsigned short reg, valmask, val; |
| 156 | } |
| 157 | reg_setting; |
| 158 | |
| 159 | /* 120.00 97/0/2/7|25/ 7/ 7/11/37 */ |
| 160 | static const reg_setting lcd_rate_120[] = |
| 161 | { |
| 162 | { 0x0914, 0xffff, (97<<8)|(0<<2)|2 }, /* UPLLSETVREG */ |
| 163 | { 0x0924, 0xff00, (2<<14)|(7<<8) }, /* DISPCSETREG */ |
| 164 | { 0x281A, 0x00ff, 25 }, /* .HSWID(T2) */ |
| 165 | { 0x281C, 0x00ff, 7 }, /* .HSSTR(T8) */ |
| 166 | { 0x281E, 0x00ff, 7 }, /* .HSEND(T7) */ |
| 167 | { 0x2822, 0x01ff, 11 }, /* .VSEND (T9) */ |
| 168 | { 0x2826, 0x0ff0, 37<<4 }, /* .DESTR(T3) */ |
| 169 | { 0, 0, 0 } |
| 170 | }; |
| 171 | |
| 172 | /* 100.00 96/0/2/7|29/25/53/15/37 */ |
| 173 | static const reg_setting lcd_rate_100[] = |
| 174 | { |
| 175 | { 0x0914, 0xffff, (96<<8)|(0<<2)|2 }, /* UPLLSETVREG */ |
| 176 | { 0x0924, 0xff00, (2<<14)|(7<<8) }, /* DISPCSETREG */ |
| 177 | { 0x281A, 0x00ff, 29 }, /* .HSWID(T2) */ |
| 178 | { 0x281C, 0x00ff, 25 }, /* .HSSTR(T8) */ |
| 179 | { 0x281E, 0x00ff, 53 }, /* .HSEND(T7) */ |
| 180 | { 0x2822, 0x01ff, 15 }, /* .VSEND (T9) */ |
| 181 | { 0x2826, 0x0ff0, 37<<4 }, /* .DESTR(T3) */ |
| 182 | { 0, 0, 0 } |
| 183 | }; |
| 184 | |
| 185 | static reg_setting lcd_rate_defaults[] = |
| 186 | { |
| 187 | { 0x0914, 0xffff, 0 }, |
| 188 | { 0x0924, 0xff00, 0 }, |
| 189 | { 0x281A, 0x00ff, 0 }, |
| 190 | { 0x281C, 0x00ff, 0 }, |
| 191 | { 0x281E, 0x00ff, 0 }, |
| 192 | { 0x2822, 0x01ff, 0 }, |
| 193 | { 0x2826, 0x0ff0, 0 }, |
| 194 | { 0, 0, 0 } |
| 195 | }; |
| 196 | |
| 197 | static void get_reg_setting(reg_setting *set) |
| 198 | { |
| 199 | for (; set->reg; set++) |
| 200 | { |
| 201 | unsigned short val = memregs[set->reg >> 1]; |
| 202 | val &= set->valmask; |
| 203 | set->val = val; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void set_reg_setting(const reg_setting *set) |
| 208 | { |
| 209 | for (; set->reg; set++) |
| 210 | { |
| 211 | unsigned short val = memregs[set->reg >> 1]; |
| 212 | val &= ~set->valmask; |
| 213 | val |= set->val; |
| 214 | memregs[set->reg >> 1] = val; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static int mmsp2_lcdrate_set(int is_pal) |
| 219 | { |
| 220 | if (memregs[0x2800>>1] & 0x100) // tv-out |
| 221 | return 0; |
| 222 | |
| 223 | printf("setting custom LCD refresh (%d Hz)... ", is_pal ? 100 : 120); |
| 224 | fflush(stdout); |
| 225 | |
| 226 | set_reg_setting(is_pal ? lcd_rate_100 : lcd_rate_120); |
| 227 | printf("done.\n"); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static void unset_lcd_custom_rate_(void) |
| 232 | { |
| 233 | printf("reset to prev LCD refresh.\n"); |
| 234 | set_reg_setting(lcd_rate_defaults); |
| 235 | } |
| 236 | |
| 237 | static void set_lcd_gamma_(int g100, int A_SNs_curve) |
| 238 | { |
| 239 | float gamma = (float) g100 / 100.0f; |
| 240 | int i; |
| 241 | gamma = 1 / gamma; |
| 242 | |
| 243 | if (g100 == 100) |
| 244 | A_SNs_curve = 0; |
| 245 | |
| 246 | /* enable gamma */ |
| 247 | memregs[0x2880>>1] &= ~(1<<12); |
| 248 | |
| 249 | memregs[0x295C>>1] = 0; |
| 250 | for (i = 0; i < 256; i++) |
| 251 | { |
| 252 | unsigned char g; |
| 253 | unsigned short s; |
| 254 | const unsigned short grey50=143, grey75=177, grey25=97; |
| 255 | double blah; |
| 256 | |
| 257 | if (A_SNs_curve) |
| 258 | { |
| 259 | // The next formula is all about gaussian interpolation |
| 260 | blah = (( -128 * exp(-powf((float) i/64.0f + 2.0f , 2.0f))) + |
| 261 | ( -64 * exp(-powf((float) i/64.0f + 1.0f , 2.0f))) + |
| 262 | (grey25 * exp(-powf((float) i/64.0f - 1.0f , 2.0f))) + |
| 263 | (grey50 * exp(-powf((float) i/64.0f - 2.0f , 2.0f))) + |
| 264 | (grey75 * exp(-powf((float) i/64.0f - 3.0f , 2.0f))) + |
| 265 | ( 256 * exp(-powf((float) i/64.0f - 4.0f , 2.0f))) + |
| 266 | ( 320 * exp(-powf((float) i/64.0f - 5.0f , 2.0f))) + |
| 267 | ( 384 * exp(-powf((float) i/64.0f - 6.0f , 2.0f)))) / 1.772637; |
| 268 | blah += 0.5; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | blah = (double)i; |
| 273 | } |
| 274 | |
| 275 | g = (unsigned char)(255.0 * pow(blah/255.0, gamma)); |
| 276 | //printf("%d : %d\n", i, g); |
| 277 | s = (g<<8) | g; |
| 278 | memregs[0x295E>>1]= s; |
| 279 | memregs[0x295E>>1]= g; |
| 280 | } |
| 281 | |
| 282 | gamma_was_changed = 1; |
| 283 | } |
| 284 | |
| 285 | static int mmsp2_gamma_set(int val, int black_level) |
| 286 | { |
| 287 | set_lcd_gamma_(val, 1); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* these are not quite MMSP2 related, |
| 292 | * more to GP2X F100/F200 consoles themselves. */ |
| 293 | typedef struct ucb1x00_ts_event |
| 294 | { |
| 295 | unsigned short pressure; |
| 296 | unsigned short x; |
| 297 | unsigned short y; |
| 298 | unsigned short pad; |
| 299 | struct timeval stamp; |
| 300 | } UCB1X00_TS_EVENT; |
| 301 | |
| 302 | int gp2x_touchpad_read(int *x, int *y) |
| 303 | { |
| 304 | UCB1X00_TS_EVENT event; |
| 305 | static int zero_seen = 0; |
| 306 | int retval; |
| 307 | |
| 308 | if (touchdev < 0) return -1; |
| 309 | |
| 310 | retval = read(touchdev, &event, sizeof(event)); |
| 311 | if (retval <= 0) { |
| 312 | perror("touch read failed"); |
| 313 | return -1; |
| 314 | } |
| 315 | // this is to ignore the messed-up 4.1.x driver |
| 316 | if (event.pressure == 0) zero_seen = 1; |
| 317 | |
| 318 | if (x) *x = (event.x * touchcal[0] + touchcal[2]) >> 16; |
| 319 | if (y) *y = (event.y * touchcal[4] + touchcal[5]) >> 16; |
| 320 | // printf("read %i %i %i\n", event.pressure, *x, *y); |
| 321 | |
| 322 | return zero_seen ? event.pressure : 0; |
| 323 | } |
| 324 | |
| 325 | static void proc_set(const char *path, const char *val) |
| 326 | { |
| 327 | FILE *f; |
| 328 | char tmp[16]; |
| 329 | |
| 330 | f = fopen(path, "w"); |
| 331 | if (f == NULL) { |
| 332 | printf("failed to open: %s\n", path); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | fprintf(f, "0\n"); |
| 337 | fclose(f); |
| 338 | |
| 339 | printf("\"%s\" is set to: ", path); |
| 340 | f = fopen(path, "r"); |
| 341 | if (f == NULL) { |
| 342 | printf("(open failed)\n"); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | fgets(tmp, sizeof(tmp), f); |
| 347 | printf("%s", tmp); |
| 348 | fclose(f); |
| 349 | } |
| 350 | |
| 351 | static int step_volume(int *volume, int diff) |
| 352 | { |
| 353 | int ret, val; |
| 354 | |
| 355 | if (mixerdev < 0) |
| 356 | return -1; |
| 357 | |
| 358 | *volume += diff; |
| 359 | if (*volume >= 100) |
| 360 | *volume = 100; |
| 361 | else if (*volume < 0) |
| 362 | *volume = 0; |
| 363 | |
| 364 | val = *volume; |
| 365 | val |= val << 8; |
| 366 | |
| 367 | ret = ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &val); |
| 368 | if (ret == -1) { |
| 369 | perror("WRITE_PCM"); |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | void mmsp2_init(void) |
| 377 | { |
| 378 | memdev = open("/dev/mem", O_RDWR); |
| 379 | if (memdev == -1) |
| 380 | { |
| 381 | perror("open(\"/dev/mem\")"); |
| 382 | exit(1); |
| 383 | } |
| 384 | |
| 385 | memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000); |
| 386 | if (memregs == MAP_FAILED) |
| 387 | { |
| 388 | perror("mmap(memregs)"); |
| 389 | exit(1); |
| 390 | } |
| 391 | memregl = (volatile unsigned int *) memregs; |
| 392 | |
| 393 | memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers |
| 394 | |
| 395 | /* save startup values: LCD refresh */ |
| 396 | get_reg_setting(lcd_rate_defaults); |
| 397 | |
| 398 | /* CPU and RAM timings */ |
| 399 | reg0910 = memregs[0x0910>>1]; |
| 400 | memtimex_old[0] = memregs[0x3802>>1]; |
| 401 | memtimex_old[1] = memregs[0x3804>>1]; |
| 402 | |
| 403 | /* touchscreen */ |
| 404 | touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY); |
| 405 | if (touchdev >= 0) { |
| 406 | FILE *pcf = fopen("/etc/pointercal", "r"); |
| 407 | if (pcf) { |
| 408 | fscanf(pcf, "%d %d %d %d %d %d %d", &touchcal[0], &touchcal[1], |
| 409 | &touchcal[2], &touchcal[3], &touchcal[4], &touchcal[5], &touchcal[6]); |
| 410 | fclose(pcf); |
| 411 | } |
| 412 | printf("found touchscreen/wm97xx\n"); |
| 413 | } |
| 414 | |
| 415 | /* disable Linux read-ahead */ |
| 416 | proc_set("/proc/sys/vm/max-readahead", "0\n"); |
| 417 | proc_set("/proc/sys/vm/min-readahead", "0\n"); |
| 418 | |
| 419 | mixerdev = open("/dev/mixer", O_RDWR); |
| 420 | if (mixerdev == -1) |
| 421 | perror("open(/dev/mixer)"); |
| 422 | |
| 423 | set_ram_timings_(); |
| 424 | |
| 425 | plat_target.cpu_clock_get = mmsp2_clock_get; |
| 426 | plat_target.cpu_clock_set = mmsp2_clock_set; |
| 427 | plat_target.lcdrate_set = mmsp2_lcdrate_set; |
| 428 | plat_target.gamma_set = mmsp2_gamma_set; |
| 429 | plat_target.step_volume = step_volume; |
| 430 | |
| 431 | gp2x_get_ticks_ms = plat_get_ticks_ms_good; |
| 432 | gp2x_get_ticks_us = plat_get_ticks_us_good; |
| 433 | |
| 434 | sndout_oss_can_restart = 0; |
| 435 | } |
| 436 | |
| 437 | void mmsp2_finish(void) |
| 438 | { |
| 439 | reset940(1, 3); |
| 440 | pause940(1); |
| 441 | |
| 442 | unset_lcd_custom_rate_(); |
| 443 | if (gamma_was_changed) |
| 444 | set_lcd_gamma_(100, 0); |
| 445 | unset_ram_timings_(); |
| 446 | if (cpuclk_was_changed) |
| 447 | memregs[0x910>>1] = reg0910; |
| 448 | |
| 449 | munmap((void *)memregs, 0x10000); |
| 450 | close(memdev); |
| 451 | if (touchdev >= 0) |
| 452 | close(touchdev); |
| 453 | if (mixerdev >= 0) |
| 454 | close(mixerdev); |
| 455 | } |