2 * quick tool to set various timings for Wiz
4 * Copyright (c) GraÅžvydas "notaz" Ignotas, 2009
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the organization nor the
14 * names of its contributors may be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * HTOTAL: X VTOTAL: 341
29 * HSWIDTH: 1 VSWIDTH: 0
30 * HASTART: 37 VASTART: 17
31 * HAEND: 277 VAEND: 337
36 * pcd 10, 357: - 523us
37 * pcd 11, 325: +1153us
39 * 'lcd_timings=397,1,37,277,341,0,17,337;dpc_clkdiv0=9'
40 * 'ram_timings=2,9,4,1,1,1,1'
46 #include "pollux_set.h"
49 static int parse_lcd_timings(const char *str, void *data)
51 int *lcd_timings = data;
54 ret = sscanf(str, "%d,%d,%d,%d,%d,%d,%d,%d",
55 &lcd_timings[0], &lcd_timings[1], &lcd_timings[2], &lcd_timings[3],
56 &lcd_timings[4], &lcd_timings[5], &lcd_timings[6], &lcd_timings[7]);
59 /* skip seven commas */
60 for (c = 0; c < 7 && *p != 0; p++)
65 /* skip last number */
66 while ('0' <= *p && *p <= '9')
72 static int parse_ram_timings(const char *str, void *data)
74 int *ram_timings = data;
79 ret = sscanf(p, "%f,%d,%d,%d,%d,%d,%d",
80 &cas, &ram_timings[1], &ram_timings[2], &ram_timings[3],
81 &ram_timings[4], &ram_timings[5], &ram_timings[6]);
92 for (c = 0; c < 6 && *p != 0; p++)
97 while ('0' <= *p && *p <= '9')
103 static int parse_decimal(const char *str, void *data)
107 *(int *)data = strtoul(str, &ep, 10);
114 /* validate and apply stuff */
115 static int apply_lcd_timings(volatile unsigned short *memregs, void *data)
117 int *lcd_timings = data;
120 for (i = 0; i < 8; i++) {
121 if (lcd_timings[i] & ~0xffff) {
122 fprintf(stderr, "pollux_set: invalid lcd timing %d: %d\n", i, lcd_timings[i]);
127 for (i = 0; i < 8; i++)
128 memregs[(0x307c>>1) + i] = lcd_timings[i];
133 static const struct {
134 signed char adj; /* how to adjust value passed by user */
135 signed short min; /* range of */
136 signed short max; /* allowed values (inclusive) */
139 { 0, 1, 3 }, /* cas (cl) */
140 { -2, 0, 15 }, /* trc */
141 { -2, 0, 15 }, /* tras */
142 { 0, 0, 15 }, /* twr */
143 { 0, 0, 15 }, /* tmrd */
144 { 0, 0, 15 }, /* trp */
145 { 0, 0, 15 }, /* trcd */
148 static int apply_ram_timings(volatile unsigned short *memregs, void *data)
150 int *ram_timings = data;
153 for (i = 0; i < 7; i++)
155 ram_timings[i] += ram_ranges[i].adj;
156 if (ram_timings[i] < ram_ranges[i].min || ram_timings[i] > ram_ranges[i].max) {
157 fprintf(stderr, "pollux_set: invalid RAM timing %d\n", i);
162 val = memregs[0x14802>>1] & 0x0f00;
163 val |= (ram_timings[4] << 12) | (ram_timings[5] << 4) | ram_timings[6];
164 memregs[0x14802>>1] = val;
166 val = memregs[0x14804>>1] & 0x4000;
167 val |= (ram_timings[0] << 12) | (ram_timings[1] << 8) |
168 (ram_timings[2] << 4) | ram_timings[3];
170 memregs[0x14804>>1] = val;
172 for (i = 0; i < 0x100000 && (memregs[0x14804>>1] & 0x8000); i++)
178 static int apply_dpc_clkdiv0(volatile unsigned short *memregs, void *data)
180 int pcd = *(int *)data;
183 if ((pcd - 1) & ~0x3f) {
184 fprintf(stderr, "pollux_set: invalid lcd clkdiv0: %d\n", pcd);
188 pcd = (pcd - 1) & 0x3f;
189 tmp = memregs[0x31c4>>1];
190 memregs[0x31c4>>1] = (tmp & ~0x3f0) | (pcd << 4);
195 static int apply_cpuclk(volatile unsigned short *memregs, void *data)
197 volatile unsigned int *memregl = (volatile void *)memregs;
198 int mhz = *(int *)data;
199 int adiv, mdiv, pdiv, sdiv = 0;
202 // m = MDIV, p = PDIV, s = SDIV
203 #define SYS_CLK_FREQ 27
205 mdiv = (mhz * pdiv) / SYS_CLK_FREQ;
208 vf004 = (pdiv<<18) | (mdiv<<8) | sdiv;
210 // attempt to keep AHB the divider close to 250, but not higher
211 for (adiv = 1; mhz / adiv > 250; adiv++)
214 vf000 = memregl[0xf000>>2];
215 vf000 = (vf000 & ~0x3c0) | ((adiv - 1) << 6);
216 memregl[0xf000>>2] = vf000;
217 memregl[0xf004>>2] = vf004;
218 memregl[0xf07c>>2] |= 0x8000;
219 for (i = 0; (memregl[0xf07c>>2] & 0x8000) && i < 0x100000; i++)
222 printf("clock set to %dMHz, AHB set to %dMHz\n", mhz, mhz / adiv);
226 static int lcd_timings[8];
227 static int ram_timings[7];
228 static int dpc_clkdiv0;
231 static const char lcd_t_help[] = "htotal,hswidth,hastart,haend,vtotal,vswidth,vastart,vaend";
232 static const char ram_t_help[] = "CAS,tRC,tRAS,tWR,tMRD,tRP,tRCD";
234 static const struct {
237 int (*parse)(const char *str, void *data);
238 int (*apply)(volatile unsigned short *memregs, void *data);
242 { "lcd_timings", lcd_t_help, parse_lcd_timings, apply_lcd_timings, lcd_timings },
243 { "ram_timings", ram_t_help, parse_ram_timings, apply_ram_timings, ram_timings },
244 { "dpc_clkdiv0", "divider", parse_decimal, apply_dpc_clkdiv0, &dpc_clkdiv0 },
245 { "clkdiv0", "divider", parse_decimal, apply_dpc_clkdiv0, &dpc_clkdiv0 }, /* alias */
246 { "cpuclk", "MHZ", parse_decimal, apply_cpuclk, &cpuclk },
248 #define ALL_PARAM_COUNT (sizeof(all_params) / sizeof(all_params[0]))
251 * set timings based on preformated string
252 * returns 0 on success.
254 int pollux_set(volatile unsigned short *memregs, const char *str)
256 int parsed_params[ALL_PARAM_COUNT];
257 int applied_params[ALL_PARAM_COUNT];
258 int applied_something = 0;
265 memset(parsed_params, 0, sizeof(parsed_params));
266 memset(applied_params, 0, sizeof(applied_params));
272 while (*p == ';' || *p == ' ')
277 for (i = 0; i < ALL_PARAM_COUNT; i++)
279 int param_len = strlen(all_params[i].name);
280 if (strncmp(p, all_params[i].name, param_len) == 0 && p[param_len] == '=')
283 ret = all_params[i].parse(p, all_params[i].data);
285 fprintf(stderr, "pollux_set parser: error at %-10s\n", p);
286 fprintf(stderr, " valid format is: <%s>\n", all_params[i].help);
289 parsed_params[i] = 1;
295 /* Unknown param. Attempt to be forward compatible and ignore it. */
296 for (po = p; *p != 0 && *p != ';'; p++)
299 fprintf(stderr, "unhandled param: ");
300 fwrite(po, 1, p - po, stderr);
301 fprintf(stderr, "\n");
304 /* validate and apply */
305 for (i = 0; i < ALL_PARAM_COUNT; i++)
307 if (!parsed_params[i])
310 ret = all_params[i].apply(memregs, all_params[i].data);
312 fprintf(stderr, "pollux_set: failed to apply %s (bad value?)\n",
317 applied_something = 1;
318 applied_params[i] = 1;
321 if (applied_something)
325 for (i = c = 0; i < ALL_PARAM_COUNT; i++)
327 if (!applied_params[i])
331 printf("%s", all_params[i].name);
341 #include <sys/types.h>
342 #include <sys/stat.h>
344 #include <sys/mman.h>
347 static void usage(const char *binary)
350 printf("usage:\n%s <set_str[;set_str[;...]]>\n"
351 "set_str:\n", binary);
352 for (i = 0; i < ALL_PARAM_COUNT; i++)
353 printf(" %s=<%s>\n", all_params[i].name, all_params[i].help);
356 int main(int argc, char *argv[])
358 volatile unsigned short *memregs;
366 memdev = open("/dev/mem", O_RDWR);
369 perror("open(/dev/mem) failed");
373 memregs = mmap(0, 0x20000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0xc0000000);
374 if (memregs == MAP_FAILED)
376 perror("mmap(memregs) failed");
381 ret = pollux_set(memregs, argv[1]);
383 munmap((void *)memregs, 0x20000);