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"
29 static int mixerdev = -1;
30 static int touchdev = -1;
31 static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
33 static char gamma_was_changed = 0;
34 static char cpuclk_was_changed = 0;
35 static unsigned short memtimex_old[2];
36 static unsigned short reg0910;
39 void pause940(int yes)
42 memregs[0x0904>>1] &= 0xFFFE;
44 memregs[0x0904>>1] |= 1;
47 void reset940(int yes, int bank)
49 memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);
54 * Fout = (m * Fin) / (p * 2^s)
55 * m = MDIV+8, p = PDIV+2, s = SDIV
57 * m = (Fout * p * 2^s) / Fin
60 #define SYS_CLK_FREQ 7372800
62 static int cpu_current_mhz = 200;
64 static int mmsp2_clock_get(void)
66 // TODO: read the actual value?
67 return cpu_current_mhz;
70 static int mmsp2_clock_set(int mhz)
72 unsigned int mdiv, pdiv, sdiv = 0;
77 mdiv = (mhz * pdiv * 1000000) / SYS_CLK_FREQ;
79 fprintf(stderr, "invalid cpuclk MHz: %u\n", mhz);
82 v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
83 memregs[0x910>>1] = v;
85 for (i = 0; i < 10000; i++)
86 if (!(memregs[0x902>>1] & 1))
89 cpuclk_was_changed = 1;
90 cpu_current_mhz = mhz;
95 #define TIMING_CHECK(t, adj, mask) \
100 static __attribute__((noinline)) void spend_cycles(int c)
103 "0: subs %0, %0, #1 ;"
105 : "=r" (c) : "0" (c) : "cc");
108 static void set_ram_timing_vals(int tCAS, int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
111 TIMING_CHECK(tCAS, -2, 0x1);
112 TIMING_CHECK(tRC, -1, 0xf);
113 TIMING_CHECK(tRAS, -1, 0xf);
114 TIMING_CHECK(tWR, -1, 0xf);
115 TIMING_CHECK(tMRD, -1, 0xf);
116 TIMING_CHECK(tRFC, -1, 0xf);
117 TIMING_CHECK(tRP, -1, 0xf);
118 TIMING_CHECK(tRCD, -1, 0xf);
120 /* get spend_cycles() into cache */
123 memregs[0x3802>>1] = ((tMRD & 0xF) << 12) | ((tRFC & 0xF) << 8) | ((tRP & 0xF) << 4) | (tRCD & 0xF);
124 memregs[0x3804>>1] = 0x8000 | ((tCAS & 1) << 12) | ((tRC & 0xF) << 8) | ((tRAS & 0xF) << 4) | (tWR & 0xF);
126 /* be sure we don't access the mem while it's being reprogrammed */
127 spend_cycles(128*1024);
128 for (i = 0; i < 8*1024; i++)
129 if (!(memregs[0x3804>>1] & 0x8000))
132 printf("RAM timings set.\n");
135 fprintf(stderr, "RAM timings invalid.\n");
138 static void set_ram_timings_(void)
140 /* craigix: --cas 2 --trc 6 --tras 4 --twr 1 --tmrd 1 --trfc 1 --trp 2 --trcd 2 */
141 set_ram_timing_vals(2, 6, 4, 1, 1, 1, 2, 2);
144 static void unset_ram_timings_(void)
146 memregs[0x3802>>1] = memtimex_old[0];
147 memregs[0x3804>>1] = memtimex_old[1] | 0x8000;
148 printf("RAM timings reset to startup values.\n");
154 unsigned short reg, valmask, val;
158 /* 120.00 97/0/2/7|25/ 7/ 7/11/37 */
159 static const reg_setting lcd_rate_120[] =
161 { 0x0914, 0xffff, (97<<8)|(0<<2)|2 }, /* UPLLSETVREG */
162 { 0x0924, 0xff00, (2<<14)|(7<<8) }, /* DISPCSETREG */
163 { 0x281A, 0x00ff, 25 }, /* .HSWID(T2) */
164 { 0x281C, 0x00ff, 7 }, /* .HSSTR(T8) */
165 { 0x281E, 0x00ff, 7 }, /* .HSEND(T7) */
166 { 0x2822, 0x01ff, 11 }, /* .VSEND (T9) */
167 { 0x2826, 0x0ff0, 37<<4 }, /* .DESTR(T3) */
171 /* 100.00 96/0/2/7|29/25/53/15/37 */
172 static const reg_setting lcd_rate_100[] =
174 { 0x0914, 0xffff, (96<<8)|(0<<2)|2 }, /* UPLLSETVREG */
175 { 0x0924, 0xff00, (2<<14)|(7<<8) }, /* DISPCSETREG */
176 { 0x281A, 0x00ff, 29 }, /* .HSWID(T2) */
177 { 0x281C, 0x00ff, 25 }, /* .HSSTR(T8) */
178 { 0x281E, 0x00ff, 53 }, /* .HSEND(T7) */
179 { 0x2822, 0x01ff, 15 }, /* .VSEND (T9) */
180 { 0x2826, 0x0ff0, 37<<4 }, /* .DESTR(T3) */
184 static reg_setting lcd_rate_defaults[] =
186 { 0x0914, 0xffff, 0 },
187 { 0x0924, 0xff00, 0 },
188 { 0x281A, 0x00ff, 0 },
189 { 0x281C, 0x00ff, 0 },
190 { 0x281E, 0x00ff, 0 },
191 { 0x2822, 0x01ff, 0 },
192 { 0x2826, 0x0ff0, 0 },
196 static void get_reg_setting(reg_setting *set)
198 for (; set->reg; set++)
200 unsigned short val = memregs[set->reg >> 1];
206 static void set_reg_setting(const reg_setting *set)
208 for (; set->reg; set++)
210 unsigned short val = memregs[set->reg >> 1];
211 val &= ~set->valmask;
213 memregs[set->reg >> 1] = val;
217 static int mmsp2_lcdrate_set(int is_pal)
219 if (memregs[0x2800>>1] & 0x100) // tv-out
222 printf("setting custom LCD refresh (%d Hz)... ", is_pal ? 100 : 120);
225 set_reg_setting(is_pal ? lcd_rate_100 : lcd_rate_120);
230 static void unset_lcd_custom_rate_(void)
232 printf("reset to prev LCD refresh.\n");
233 set_reg_setting(lcd_rate_defaults);
236 static void set_lcd_gamma_(int g100, int A_SNs_curve)
238 float gamma = (float) g100 / 100.0f;
246 memregs[0x2880>>1] &= ~(1<<12);
248 memregs[0x295C>>1] = 0;
249 for (i = 0; i < 256; i++)
253 const unsigned short grey50=143, grey75=177, grey25=97;
258 // The next formula is all about gaussian interpolation
259 blah = (( -128 * exp(-powf((float) i/64.0f + 2.0f , 2.0f))) +
260 ( -64 * exp(-powf((float) i/64.0f + 1.0f , 2.0f))) +
261 (grey25 * exp(-powf((float) i/64.0f - 1.0f , 2.0f))) +
262 (grey50 * exp(-powf((float) i/64.0f - 2.0f , 2.0f))) +
263 (grey75 * exp(-powf((float) i/64.0f - 3.0f , 2.0f))) +
264 ( 256 * exp(-powf((float) i/64.0f - 4.0f , 2.0f))) +
265 ( 320 * exp(-powf((float) i/64.0f - 5.0f , 2.0f))) +
266 ( 384 * exp(-powf((float) i/64.0f - 6.0f , 2.0f)))) / 1.772637;
274 g = (unsigned char)(255.0 * pow(blah/255.0, gamma));
275 //printf("%d : %d\n", i, g);
277 memregs[0x295E>>1]= s;
278 memregs[0x295E>>1]= g;
281 gamma_was_changed = 1;
284 static int mmsp2_gamma_set(int val, int black_level)
286 set_lcd_gamma_(val, 1);
290 /* these are not quite MMSP2 related,
291 * more to GP2X F100/F200 consoles themselves. */
292 typedef struct ucb1x00_ts_event
294 unsigned short pressure;
298 struct timeval stamp;
301 int gp2x_touchpad_read(int *x, int *y)
303 UCB1X00_TS_EVENT event;
304 static int zero_seen = 0;
307 if (touchdev < 0) return -1;
309 retval = read(touchdev, &event, sizeof(event));
311 perror("touch read failed");
314 // this is to ignore the messed-up 4.1.x driver
315 if (event.pressure == 0) zero_seen = 1;
317 if (x) *x = (event.x * touchcal[0] + touchcal[2]) >> 16;
318 if (y) *y = (event.y * touchcal[4] + touchcal[5]) >> 16;
319 // printf("read %i %i %i\n", event.pressure, *x, *y);
321 return zero_seen ? event.pressure : 0;
324 static void proc_set(const char *path, const char *val)
329 f = fopen(path, "w");
331 printf("failed to open: %s\n", path);
338 printf("\"%s\" is set to: ", path);
339 f = fopen(path, "r");
341 printf("(open failed)\n");
345 fgets(tmp, sizeof(tmp), f);
350 static int step_volume(int *volume, int diff)
360 else if (*volume < 0)
366 ret = ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &val);
375 void mmsp2_init(void)
377 memdev = open("/dev/mem", O_RDWR);
380 perror("open(\"/dev/mem\")");
384 memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
385 if (memregs == MAP_FAILED)
387 perror("mmap(memregs)");
390 memregl = (volatile unsigned int *) memregs;
392 memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers
394 /* save startup values: LCD refresh */
395 get_reg_setting(lcd_rate_defaults);
397 /* CPU and RAM timings */
398 reg0910 = memregs[0x0910>>1];
399 memtimex_old[0] = memregs[0x3802>>1];
400 memtimex_old[1] = memregs[0x3804>>1];
403 touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);
405 FILE *pcf = fopen("/etc/pointercal", "r");
407 fscanf(pcf, "%d %d %d %d %d %d %d", &touchcal[0], &touchcal[1],
408 &touchcal[2], &touchcal[3], &touchcal[4], &touchcal[5], &touchcal[6]);
411 printf("found touchscreen/wm97xx\n");
414 /* disable Linux read-ahead */
415 proc_set("/proc/sys/vm/max-readahead", "0\n");
416 proc_set("/proc/sys/vm/min-readahead", "0\n");
418 mixerdev = open("/dev/mixer", O_RDWR);
420 perror("open(/dev/mixer)");
424 plat_target.cpu_clock_get = mmsp2_clock_get;
425 plat_target.cpu_clock_set = mmsp2_clock_set;
426 plat_target.lcdrate_set = mmsp2_lcdrate_set;
427 plat_target.gamma_set = mmsp2_gamma_set;
428 plat_target.step_volume = step_volume;
430 gp2x_get_ticks_ms = plat_get_ticks_ms_good;
431 gp2x_get_ticks_us = plat_get_ticks_us_good;
434 void mmsp2_finish(void)
439 unset_lcd_custom_rate_();
440 if (gamma_was_changed)
441 set_lcd_gamma_(100, 0);
442 unset_ram_timings_();
443 if (cpuclk_was_changed)
444 memregs[0x910>>1] = reg0910;
446 munmap((void *)memregs, 0x10000);