GP2X updates
[libpicofe.git] / gp2x / soc_mmsp2.c
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 "../plat.h"
28
29 static int mixerdev = -1;
30 static int touchdev = -1;
31 static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
32
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;
37
38 /* 940 */
39 void pause940(int yes)
40 {
41         if (yes)
42                 memregs[0x0904>>1] &= 0xFFFE;
43         else
44                 memregs[0x0904>>1] |= 1;
45 }
46
47 void reset940(int yes, int bank)
48 {
49         memregs[0x3B48>>1] = ((yes&1) << 7) | (bank & 0x03);
50 }
51
52 /*
53  * CPU clock
54  * Fout = (m * Fin) / (p * 2^s)
55  * m = MDIV+8, p = PDIV+2, s = SDIV
56  *
57  * m = (Fout * p * 2^s) / Fin
58  */
59
60 #define SYS_CLK_FREQ 7372800
61
62 static int cpu_current_mhz = 200;
63
64 static int mmsp2_clock_get(void)
65 {
66         // TODO: read the actual value?
67         return cpu_current_mhz;
68 }
69
70 static int mmsp2_clock_set(int mhz)
71 {
72         unsigned int mdiv, pdiv, sdiv = 0;
73         unsigned int v;
74         int i;
75
76         pdiv = 3;
77         mdiv = (mhz * pdiv * 1000000) / SYS_CLK_FREQ;
78         if (mdiv & ~0xff) {
79                 fprintf(stderr, "invalid cpuclk MHz: %u\n", mhz);
80                 return -1;
81         }
82         v = ((mdiv-8)<<8) | ((pdiv-2)<<2) | sdiv;
83         memregs[0x910>>1] = v;
84
85         for (i = 0; i < 10000; i++)
86                 if (!(memregs[0x902>>1] & 1))
87                         break;
88
89         cpuclk_was_changed = 1;
90         cpu_current_mhz = mhz;
91         return 0;
92 }
93
94 /* RAM timings */
95 #define TIMING_CHECK(t, adj, mask) \
96         t += adj; \
97         if (t & ~mask) \
98                 goto bad
99
100 static __attribute__((noinline)) void spend_cycles(int c)
101 {
102         asm volatile(
103                 "0: subs %0, %0, #1 ;"
104                 "bgt 0b"
105                 : "=r" (c) : "0" (c) : "cc");
106 }
107
108 static void set_ram_timing_vals(int tCAS, int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
109 {
110         int i;
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);
119
120         /* get spend_cycles() into cache */
121         spend_cycles(1);
122
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);
125
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))
130                         break;
131
132         printf("RAM timings set.\n");
133         return;
134 bad:
135         fprintf(stderr, "RAM timings invalid.\n");
136 }
137
138 static void set_ram_timings_(void)
139 {
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);
142 }
143
144 static void unset_ram_timings_(void)
145 {
146         memregs[0x3802>>1] = memtimex_old[0];
147         memregs[0x3804>>1] = memtimex_old[1] | 0x8000;
148         printf("RAM timings reset to startup values.\n");
149 }
150
151 /* LCD refresh */
152 typedef struct
153 {
154         unsigned short reg, valmask, val;
155 }
156 reg_setting;
157
158 /* 120.00 97/0/2/7|25/ 7/ 7/11/37 */
159 static const reg_setting lcd_rate_120[] =
160 {
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) */
168         { 0, 0, 0 }
169 };
170
171 /* 100.00 96/0/2/7|29/25/53/15/37 */
172 static const reg_setting lcd_rate_100[] =
173 {
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) */
181         { 0, 0, 0 }
182 };
183
184 static reg_setting lcd_rate_defaults[] =
185 {
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 },
193         { 0, 0, 0 }
194 };
195
196 static void get_reg_setting(reg_setting *set)
197 {
198         for (; set->reg; set++)
199         {
200                 unsigned short val = memregs[set->reg >> 1];
201                 val &= set->valmask;
202                 set->val = val;
203         }
204 }
205
206 static void set_reg_setting(const reg_setting *set)
207 {
208         for (; set->reg; set++)
209         {
210                 unsigned short val = memregs[set->reg >> 1];
211                 val &= ~set->valmask;
212                 val |= set->val;
213                 memregs[set->reg >> 1] = val;
214         }
215 }
216
217 static int mmsp2_lcdrate_set(int is_pal)
218 {
219         if (memregs[0x2800>>1] & 0x100) // tv-out
220                 return 0;
221
222         printf("setting custom LCD refresh (%d Hz)... ", is_pal ? 100 : 120);
223         fflush(stdout);
224
225         set_reg_setting(is_pal ? lcd_rate_100 : lcd_rate_120);
226         printf("done.\n");
227         return 0;
228 }
229
230 static void unset_lcd_custom_rate_(void)
231 {
232         printf("reset to prev LCD refresh.\n");
233         set_reg_setting(lcd_rate_defaults);
234 }
235
236 static void set_lcd_gamma_(int g100, int A_SNs_curve)
237 {
238         float gamma = (float) g100 / 100.0f;
239         int i;
240         gamma = 1 / gamma;
241
242         if (g100 == 100)
243                 A_SNs_curve = 0;
244
245         /* enable gamma */
246         memregs[0x2880>>1] &= ~(1<<12);
247
248         memregs[0x295C>>1] = 0;
249         for (i = 0; i < 256; i++)
250         {
251                 unsigned char g;
252                 unsigned short s;
253                 const unsigned short grey50=143, grey75=177, grey25=97;
254                 double blah;
255
256                 if (A_SNs_curve)
257                 {
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;
267                         blah += 0.5;
268                 }
269                 else
270                 {
271                         blah = (double)i;
272                 }
273
274                 g = (unsigned char)(255.0 * pow(blah/255.0, gamma));
275                 //printf("%d : %d\n", i, g);
276                 s = (g<<8) | g;
277                 memregs[0x295E>>1]= s;
278                 memregs[0x295E>>1]= g;
279         }
280
281         gamma_was_changed = 1;
282 }
283
284 static int mmsp2_gamma_set(int val, int black_level)
285 {
286         set_lcd_gamma_(val, 1);
287         return 0;
288 }
289
290 /* these are not quite MMSP2 related,
291  * more to GP2X F100/F200 consoles themselves. */
292 typedef struct ucb1x00_ts_event
293 {
294         unsigned short pressure;
295         unsigned short x;
296         unsigned short y;
297         unsigned short pad;
298         struct timeval stamp;
299 } UCB1X00_TS_EVENT;
300
301 int gp2x_touchpad_read(int *x, int *y)
302 {
303         UCB1X00_TS_EVENT event;
304         static int zero_seen = 0;
305         int retval;
306
307         if (touchdev < 0) return -1;
308
309         retval = read(touchdev, &event, sizeof(event));
310         if (retval <= 0) {
311                 perror("touch read failed");
312                 return -1;
313         }
314         // this is to ignore the messed-up 4.1.x driver
315         if (event.pressure == 0) zero_seen = 1;
316
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);
320
321         return zero_seen ? event.pressure : 0;
322 }
323
324 static void proc_set(const char *path, const char *val)
325 {
326         FILE *f;
327         char tmp[16];
328
329         f = fopen(path, "w");
330         if (f == NULL) {
331                 printf("failed to open: %s\n", path);
332                 return;
333         }
334
335         fprintf(f, "0\n");
336         fclose(f);
337
338         printf("\"%s\" is set to: ", path);
339         f = fopen(path, "r");
340         if (f == NULL) {
341                 printf("(open failed)\n");
342                 return;
343         }
344
345         fgets(tmp, sizeof(tmp), f);
346         printf("%s", tmp);
347         fclose(f);
348 }
349
350 static int step_volume(int *volume, int diff)
351 {
352         int ret, val;
353
354         if (mixerdev < 0)
355                 return -1;
356
357         *volume += diff;
358         if (*volume >= 100)
359                 *volume = 100;
360         else if (*volume < 0)
361                 *volume = 0;
362
363         val = *volume;
364         val |= val << 8;
365
366         ret = ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &val);
367         if (ret == -1) {
368                 perror("WRITE_PCM");
369                 return ret;
370         }
371
372         return 0;
373 }
374
375 void mmsp2_init(void)
376 {
377         memdev = open("/dev/mem", O_RDWR);
378         if (memdev == -1)
379         {
380                 perror("open(\"/dev/mem\")");
381                 exit(1);
382         }
383
384         memregs = mmap(0, 0x10000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
385         if (memregs == MAP_FAILED)
386         {
387                 perror("mmap(memregs)");
388                 exit(1);
389         }
390         memregl = (volatile unsigned int *) memregs;
391
392         memregs[0x2880>>1] &= ~0x383; // disable cursor, subpict, osd, video layers
393
394         /* save startup values: LCD refresh */
395         get_reg_setting(lcd_rate_defaults);
396
397         /* CPU and RAM timings */
398         reg0910 = memregs[0x0910>>1];
399         memtimex_old[0] = memregs[0x3802>>1];
400         memtimex_old[1] = memregs[0x3804>>1];
401
402         /* touchscreen */
403         touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);
404         if (touchdev >= 0) {
405                 FILE *pcf = fopen("/etc/pointercal", "r");
406                 if (pcf) {
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]);
409                         fclose(pcf);
410                 }
411                 printf("found touchscreen/wm97xx\n");
412         }
413
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");
417
418         mixerdev = open("/dev/mixer", O_RDWR);
419         if (mixerdev == -1)
420                 perror("open(/dev/mixer)");
421
422         set_ram_timings_();
423
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;
429
430         gp2x_get_ticks_ms = plat_get_ticks_ms_good;
431         gp2x_get_ticks_us = plat_get_ticks_us_good;
432 }
433
434 void mmsp2_finish(void)
435 {
436         reset940(1, 3);
437         pause940(1);
438
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;
445
446         munmap((void *)memregs, 0x10000);
447         close(memdev);
448         if (touchdev >= 0)
449                 close(touchdev);
450         if (mixerdev >= 0)
451                 close(mixerdev);
452 }