2 * (C) GraÅžvydas "notaz" Ignotas, 2006-2012
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.
16 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <linux/soundcard.h>
25 #include "soc_mmsp2.h"
26 #include "plat_gp2x.h"
27 #include "../linux/sndout_oss.h"
30 static int mixerdev = -1;
31 static int touchdev = -1;
32 static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
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;
40 void pause940(int yes)
43 memregs[0x0904>>1] &= 0xFFFE;
45 memregs[0x0904>>1] |= 1;
48 void reset940(int yes, int bank)
50 memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);
55 * Fout = (m * Fin) / (p * 2^s)
56 * m = MDIV+8, p = PDIV+2, s = SDIV
58 * m = (Fout * p * 2^s) / Fin
61 #define SYS_CLK_FREQ 7372800
63 static int cpu_current_mhz = 200;
65 static int mmsp2_clock_get(void)
67 // TODO: read the actual value?
68 return cpu_current_mhz;
71 static int mmsp2_clock_set(int mhz)
73 unsigned int mdiv, pdiv, sdiv = 0;
78 mdiv = (mhz * pdiv * 1000000) / SYS_CLK_FREQ;
80 fprintf(stderr, "invalid cpuclk MHz: %u\n", mhz);
83 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
84 memregs[0x910>>1] = v;
86 for (i = 0; i < 10000; i++)
87 if (!(memregs[0x902>>1] & 1))
90 cpuclk_was_changed = 1;
91 cpu_current_mhz = mhz;
96 #define TIMING_CHECK(t, adj, mask) \
101 static __attribute__((noinline)) void spend_cycles(int c)
104 "0: subs %0, %0, #1 ;"
106 : "=r" (c) : "0" (c) : "cc");
109 static void set_ram_timing_vals(int tCAS, int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
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);
121 /* get spend_cycles() into cache */
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);
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))
133 printf("RAM timings set.\n");
136 fprintf(stderr, "RAM timings invalid.\n");
139 static void set_ram_timings_(void)
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);
145 static void unset_ram_timings_(void)
147 memregs[0x3802>>1] = memtimex_old[0];
148 memregs[0x3804>>1] = memtimex_old[1] | 0x8000;
149 printf("RAM timings reset to startup values.\n");
155 unsigned short reg, valmask, val;
159 /* 120.00 97/0/2/7|25/ 7/ 7/11/37 */
160 static const reg_setting lcd_rate_120[] =
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) */
172 /* 100.00 96/0/2/7|29/25/53/15/37 */
173 static const reg_setting lcd_rate_100[] =
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) */
185 static reg_setting lcd_rate_defaults[] =
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 },
197 static void get_reg_setting(reg_setting *set)
199 for (; set->reg; set++)
201 unsigned short val = memregs[set->reg >> 1];
207 static void set_reg_setting(const reg_setting *set)
209 for (; set->reg; set++)
211 unsigned short val = memregs[set->reg >> 1];
212 val &= ~set->valmask;
214 memregs[set->reg >> 1] = val;
218 static int mmsp2_lcdrate_set(int is_pal)
220 if (memregs[0x2800>>1] & 0x100) // tv-out
223 printf("setting custom LCD refresh (%d Hz)... ", is_pal ? 100 : 120);
226 set_reg_setting(is_pal ? lcd_rate_100 : lcd_rate_120);
231 static void unset_lcd_custom_rate_(void)
233 printf("reset to prev LCD refresh.\n");
234 set_reg_setting(lcd_rate_defaults);
237 static void set_lcd_gamma_(int g100, int A_SNs_curve)
239 float gamma = (float) g100 / 100.0f;
247 memregs[0x2880>>1] &= ~(1<<12);
249 memregs[0x295C>>1] = 0;
250 for (i = 0; i < 256; i++)
254 const unsigned short grey50=143, grey75=177, grey25=97;
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;
275 g = (unsigned char)(255.0 * pow(blah/255.0, gamma));
276 //printf("%d : %d\n", i, g);
278 memregs[0x295E>>1]= s;
279 memregs[0x295E>>1]= g;
282 gamma_was_changed = 1;
285 static int mmsp2_gamma_set(int val, int black_level)
287 set_lcd_gamma_(val, 1);
291 /* these are not quite MMSP2 related,
292 * more to GP2X F100/F200 consoles themselves. */
293 typedef struct ucb1x00_ts_event
295 unsigned short pressure;
299 struct timeval stamp;
302 int gp2x_touchpad_read(int *x, int *y)
304 UCB1X00_TS_EVENT event;
305 static int zero_seen = 0;
308 if (touchdev < 0) return -1;
310 retval = read(touchdev, &event, sizeof(event));
312 perror("touch read failed");
315 // this is to ignore the messed-up 4.1.x driver
316 if (event.pressure == 0) zero_seen = 1;
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);
322 return zero_seen ? event.pressure : 0;
325 static void proc_set(const char *path, const char *val)
330 f = fopen(path, "w");
332 printf("failed to open: %s\n", path);
339 printf("\"%s\" is set to: ", path);
340 f = fopen(path, "r");
342 printf("(open failed)\n");
346 fgets(tmp, sizeof(tmp), f);
351 static int step_volume(int *volume, int diff)
361 else if (*volume < 0)
367 ret = ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &val);
376 void mmsp2_init(void)
378 memdev = open("/dev/mem", O_RDWR);
381 perror("open(\"/dev/mem\")");
385 memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
386 if (memregs == MAP_FAILED)
388 perror("mmap(memregs)");
391 memregl = (volatile unsigned int *) memregs;
393 memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers
395 /* save startup values: LCD refresh */
396 get_reg_setting(lcd_rate_defaults);
398 /* CPU and RAM timings */
399 reg0910 = memregs[0x0910>>1];
400 memtimex_old[0] = memregs[0x3802>>1];
401 memtimex_old[1] = memregs[0x3804>>1];
404 touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);
406 FILE *pcf = fopen("/etc/pointercal", "r");
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]);
412 printf("found touchscreen/wm97xx\n");
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");
419 mixerdev = open("/dev/mixer", O_RDWR);
421 perror("open(/dev/mixer)");
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;
431 gp2x_get_ticks_ms = plat_get_ticks_ms_good;
432 gp2x_get_ticks_us = plat_get_ticks_us_good;
434 sndout_oss_can_restart = 0;
437 void mmsp2_finish(void)
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;
449 munmap((void *)memregs, 0x10000);