ibm561.c revision 1.2 1 /* $NetBSD: ibm561.c,v 1.2 2002/01/12 16:03:12 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Roland C. Dowdeswell of Ponte, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/buf.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #include <dev/pci/pcivar.h>
49 #include <dev/ic/ibm561reg.h>
50 #include <dev/ic/ibm561var.h>
51 #include <dev/ic/ramdac.h>
52
53 #include <dev/wscons/wsconsio.h>
54
55 /*
56 * Functions exported via the RAMDAC configuration table.
57 */
58 void ibm561_init __P((struct ramdac_cookie *));
59 int ibm561_set_cmap __P((struct ramdac_cookie *,
60 struct wsdisplay_cmap *));
61 int ibm561_get_cmap __P((struct ramdac_cookie *,
62 struct wsdisplay_cmap *));
63 int ibm561_set_cursor __P((struct ramdac_cookie *,
64 struct wsdisplay_cursor *));
65 int ibm561_get_cursor __P((struct ramdac_cookie *,
66 struct wsdisplay_cursor *));
67 int ibm561_set_curpos __P((struct ramdac_cookie *,
68 struct wsdisplay_curpos *));
69 int ibm561_get_curpos __P((struct ramdac_cookie *,
70 struct wsdisplay_curpos *));
71 int ibm561_get_curmax __P((struct ramdac_cookie *,
72 struct wsdisplay_curpos *));
73 int ibm561_set_dotclock __P((struct ramdac_cookie *,
74 unsigned));
75
76 /* XXX const */
77 struct ramdac_funcs ibm561_funcsstruct = {
78 "IBM561",
79 ibm561_register,
80 ibm561_init,
81 ibm561_set_cmap,
82 ibm561_get_cmap,
83 ibm561_set_cursor,
84 ibm561_get_cursor,
85 ibm561_set_curpos,
86 ibm561_get_curpos,
87 ibm561_get_curmax,
88 NULL, /* check_curcmap; not needed */
89 NULL, /* set_curcmap; not needed */
90 NULL, /* get_curcmap; not needed */
91 ibm561_set_dotclock,
92 };
93
94 /*
95 * Private data.
96 */
97 struct ibm561data {
98 void *cookie;
99
100 int (*ramdac_sched_update) __P((void *, void (*)(void *)));
101 void (*ramdac_wr) __P((void *, u_int, u_int8_t));
102 u_int8_t (*ramdac_rd) __P((void *, u_int));
103
104 #define CHANGED_CURCMAP 0x0001 /* cursor cmap */
105 #define CHANGED_CMAP 0x0002 /* color map */
106 #define CHANGED_WTYPE 0x0004 /* window types */
107 #define CHANGED_DOTCLOCK 0x0008 /* dot clock */
108 #define CHANGED_ALL 0x000f /* or of all above */
109 u_int8_t changed;
110
111 /* dotclock parameters */
112 u_int8_t vco_div;
113 u_int8_t pll_ref;
114 u_int8_t div_dotclock;
115
116 /* colormaps et al. */
117 u_int8_t curcmap_r[2];
118 u_int8_t curcmap_g[2];
119 u_int8_t curcmap_b[2];
120
121 u_int8_t cmap_r[IBM561_NCMAP_ENTRIES];
122 u_int8_t cmap_g[IBM561_NCMAP_ENTRIES];
123 u_int8_t cmap_b[IBM561_NCMAP_ENTRIES];
124
125 u_int16_t gamma_r[IBM561_NGAMMA_ENTRIES];
126 u_int16_t gamma_g[IBM561_NGAMMA_ENTRIES];
127 u_int16_t gamma_b[IBM561_NGAMMA_ENTRIES];
128
129 u_int16_t wtype[IBM561_NWTYPES];
130 };
131
132 /*
133 * private functions
134 */
135 void ibm561_update __P((void *));
136 static void ibm561_load_cmap(struct ibm561data *);
137 static void ibm561_load_dotclock(struct ibm561data *);
138 static void ibm561_regbegin(struct ibm561data *, u_int16_t);
139 static void ibm561_regcont(struct ibm561data *, u_int16_t, u_int8_t);
140 static void ibm561_regcont10bit(struct ibm561data *, u_int16_t, u_int16_t);
141 static void ibm561_regwr(struct ibm561data *, u_int16_t, u_int8_t);
142
143 struct ramdac_funcs *
144 ibm561_funcs(void)
145 {
146 return &ibm561_funcsstruct;
147 }
148
149 struct ramdac_cookie *
150 ibm561_register(v, sched_update, wr, rd)
151 void *v;
152 int (*sched_update)(void *, void (*)(void *));
153 void (*wr)(void *, u_int, u_int8_t);
154 u_int8_t (*rd)(void *, u_int);
155 {
156 struct ibm561data *data;
157
158 data = malloc(sizeof *data, M_DEVBUF, M_WAITOK|M_ZERO);
159 data->cookie = v;
160 data->ramdac_sched_update = sched_update;
161 data->ramdac_wr = wr;
162 data->ramdac_rd = rd;
163 return (struct ramdac_cookie *)data;
164 }
165
166 /*
167 * This function exists solely to provide a means to init
168 * the RAMDAC without first registering. It is useful for
169 * initializing the console early on.
170 */
171
172 struct ibm561data *saved_console_data;
173
174 void
175 ibm561_cninit(v, sched_update, wr, rd, dotclock)
176 void *v;
177 int (*sched_update)(void *, void (*)(void *));
178 void (*wr)(void *, u_int, u_int8_t);
179 u_int8_t (*rd)(void *, u_int);
180 u_int dotclock;
181 {
182 struct ibm561data tmp, *data = &tmp;
183 memset(data, 0x0, sizeof *data);
184 data->cookie = v;
185 data->ramdac_sched_update = sched_update;
186 data->ramdac_wr = wr;
187 data->ramdac_rd = rd;
188 ibm561_set_dotclock((struct ramdac_cookie *)data, dotclock);
189 saved_console_data = data;
190 ibm561_init((struct ramdac_cookie *)data);
191 saved_console_data = NULL;
192 }
193
194 void
195 ibm561_init(rc)
196 struct ramdac_cookie *rc;
197 {
198 struct ibm561data *data = (struct ibm561data *)rc;
199 int i;
200
201 /* XXX this is _essential_ */
202
203 ibm561_load_dotclock(data);
204
205 /* XXXrcd: bunch of magic of which I have no current clue */
206 ibm561_regwr(data, IBM561_CONFIG_REG1, 0x2a);
207 ibm561_regwr(data, IBM561_CONFIG_REG3, 0x41);
208 ibm561_regwr(data, IBM561_CONFIG_REG4, 0x20);
209
210 /* initialize the card a bit */
211 ibm561_regwr(data, IBM561_SYNC_CNTL, 0x1);
212 ibm561_regwr(data, IBM561_CONFIG_REG2, 0x19);
213
214 ibm561_regwr(data, IBM561_CONFIG_REG1, 0x2a);
215 ibm561_regwr(data, IBM561_CONFIG_REG4, 0x20);
216
217 ibm561_regbegin(data, IBM561_WAT_SEG_REG);
218 ibm561_regcont(data, IBM561_CMD, 0x00);
219 ibm561_regcont(data, IBM561_CMD, 0x00);
220 ibm561_regcont(data, IBM561_CMD, 0x00);
221 ibm561_regcont(data, IBM561_CMD, 0x00);
222 ibm561_regbegin(data, IBM561_CHROMAKEY0);
223 ibm561_regcont(data, IBM561_CMD, 0x00);
224 ibm561_regcont(data, IBM561_CMD, 0x00);
225 ibm561_regcont(data, IBM561_CMD, 0x00);
226 ibm561_regcont(data, IBM561_CMD, 0x00);
227
228 ibm561_regwr(data, IBM561_CURS_CNTL_REG, 0x00); /* XXX off? */
229
230 /* cursor `hot spot' registers */
231 ibm561_regbegin(data, IBM561_HOTSPOT_REG);
232 ibm561_regcont(data, IBM561_CMD, 0x00);
233 ibm561_regcont(data, IBM561_CMD, 0x00);
234 ibm561_regcont(data, IBM561_CMD, 0xff);
235 ibm561_regcont(data, IBM561_CMD, 0x00);
236 ibm561_regcont(data, IBM561_CMD, 0xff);
237 ibm561_regcont(data, IBM561_CMD, 0x00);
238
239 /* VRAM Mask Registers (diagnostics) */
240 ibm561_regbegin(data, IBM561_VRAM_MASK_REG);
241 ibm561_regcont(data, IBM561_CMD, 0xff);
242 ibm561_regcont(data, IBM561_CMD, 0xff);
243 ibm561_regcont(data, IBM561_CMD, 0xff);
244 ibm561_regcont(data, IBM561_CMD, 0xff);
245 ibm561_regcont(data, IBM561_CMD, 0xff);
246 ibm561_regcont(data, IBM561_CMD, 0xff);
247 ibm561_regcont(data, IBM561_CMD, 0xff);
248
249 /* let's set up some decent default colour maps and gammas */
250 for (i=0; i < IBM561_NCMAP_ENTRIES; i++)
251 data->cmap_r[i] = data->cmap_g[i] = data->cmap_b[i] = 0xff;
252 data->cmap_r[0] = data->cmap_g[0] = data->cmap_b[0] = 0x00;
253 data->cmap_r[256] = data->cmap_g[256] = data->cmap_b[256] = 0x00;
254 data->cmap_r[512] = data->cmap_g[512] = data->cmap_b[512] = 0x00;
255 data->cmap_r[768] = data->cmap_g[768] = data->cmap_b[768] = 0x00;
256
257 data->gamma_r[0] = data->gamma_g[0] = data->gamma_b[0] = 0x00;
258 for (i=0; i < IBM561_NGAMMA_ENTRIES; i++)
259 data->gamma_r[i] = data->gamma_g[i] = data->gamma_b[i] = 0xff;
260
261 for (i=0; i < IBM561_NWTYPES; i++)
262 data->wtype[i] = 0x0036;
263 data->wtype[1] = 0x0028;
264
265 /* the last step: */
266 data->changed = CHANGED_ALL;
267 data->ramdac_sched_update(data->cookie, ibm561_update);
268 }
269
270 int
271 ibm561_set_cmap(rc, cmapp)
272 struct ramdac_cookie *rc;
273 struct wsdisplay_cmap *cmapp;
274 {
275 struct ibm561data *data = (struct ibm561data *)rc;
276 int count;
277 int index;
278 int s;
279
280 if ((u_int)cmapp->index >= IBM561_NCMAP_ENTRIES ||
281 ((u_int)cmapp->index + (u_int)cmapp->count) > IBM561_NCMAP_ENTRIES)
282 return (EINVAL);
283 if (!uvm_useracc(cmapp->red, cmapp->count, B_READ) ||
284 !uvm_useracc(cmapp->green, cmapp->count, B_READ) ||
285 !uvm_useracc(cmapp->blue, cmapp->count, B_READ))
286 return (EFAULT);
287
288 s = spltty();
289 index = cmapp->index;
290 count = cmapp->count;
291 copyin(cmapp->red, &data->cmap_r[index], count);
292 copyin(cmapp->green, &data->cmap_g[index], count);
293 copyin(cmapp->blue, &data->cmap_b[index], count);
294 data->changed |= CHANGED_CMAP;
295 data->ramdac_sched_update(data->cookie, ibm561_update);
296 splx(s);
297 return (0);
298 }
299
300 int
301 ibm561_get_cmap(rc, cmapp)
302 struct ramdac_cookie *rc;
303 struct wsdisplay_cmap *cmapp;
304 {
305 struct ibm561data *data = (struct ibm561data *)rc;
306 int error;
307 int count;
308 int index;
309
310 if ((u_int)cmapp->index >= IBM561_NCMAP_ENTRIES ||
311 ((u_int)cmapp->index + (u_int)cmapp->count) > IBM561_NCMAP_ENTRIES)
312 return (EINVAL);
313 count = cmapp->count;
314 index = cmapp->index;
315 error = copyout(&data->cmap_r[index], cmapp->red, count);
316 if (error)
317 return (error);
318 error = copyout(&data->cmap_g[index], cmapp->green, count);
319 if (error)
320 return (error);
321 error = copyout(&data->cmap_b[index], cmapp->blue, count);
322 return (error);
323 }
324
325 /*
326 * XXX:
327 * I am leaving these functions returning EINVAL, as they are
328 * not strictly necessary for the correct functioning of the
329 * card and in fact are not used on the other TGA variants, except
330 * they are exported via ioctl(2) to userland, which does not in
331 * fact use them.
332 */
333
334 int
335 ibm561_set_cursor(rc, cursorp)
336 struct ramdac_cookie *rc;
337 struct wsdisplay_cursor *cursorp;
338 {
339 return EINVAL;
340 }
341
342 int
343 ibm561_get_cursor(rc, cursorp)
344 struct ramdac_cookie *rc;
345 struct wsdisplay_cursor *cursorp;
346 {
347 return EINVAL;
348 }
349
350 int
351 ibm561_set_curpos(rc, curposp)
352 struct ramdac_cookie *rc;
353 struct wsdisplay_curpos *curposp;
354 {
355 return EINVAL;
356 }
357
358 int
359 ibm561_get_curpos(rc, curposp)
360 struct ramdac_cookie *rc;
361 struct wsdisplay_curpos *curposp;
362 {
363 return EINVAL;
364 }
365
366 int
367 ibm561_get_curmax(rc, curposp)
368 struct ramdac_cookie *rc;
369 struct wsdisplay_curpos *curposp;
370 {
371 return EINVAL;
372 }
373
374 int
375 ibm561_set_dotclock(rc, dotclock)
376 struct ramdac_cookie *rc;
377 unsigned dotclock;
378 {
379 struct ibm561data *data = (struct ibm561data *)rc;
380
381 /* XXXrcd: a couple of these are a little hazy, vis a vis
382 * check 175MHz and 202MHz, which are wrong...
383 */
384 switch (dotclock) {
385 case 25175000: data->vco_div = 0x3e; data->pll_ref = 0x09; break;
386 case 31500000: data->vco_div = 0x17; data->pll_ref = 0x05; break;
387 case 40000000: data->vco_div = 0x42; data->pll_ref = 0x06; break;
388 case 50000000: data->vco_div = 0x45; data->pll_ref = 0x05; break;
389 case 65000000: data->vco_div = 0xac; data->pll_ref = 0x0c; break;
390 case 69000000: data->vco_div = 0xa9; data->pll_ref = 0x0b; break;
391 case 74000000: data->vco_div = 0x9c; data->pll_ref = 0x09; break;
392 case 75000000: data->vco_div = 0x93; data->pll_ref = 0x08; break;
393 case 103994000: data->vco_div = 0x96; data->pll_ref = 0x06; break;
394 case 108180000: data->vco_div = 0xb8; data->pll_ref = 0x08; break;
395 case 110000000: data->vco_div = 0xba; data->pll_ref = 0x08; break;
396 case 119840000: data->vco_div = 0x82; data->pll_ref = 0x04; break;
397 case 130808000: data->vco_div = 0xc8; data->pll_ref = 0x08; break;
398 case 135000000: data->vco_div = 0xc1; data->pll_ref = 0x07; break;
399 case 175000000: data->vco_div = 0xe2; data->pll_ref = 0x07; break;
400 case 202500000: data->vco_div = 0xe2; data->pll_ref = 0x07; break;
401 default:
402 return EINVAL;
403 }
404
405 data->div_dotclock = 0xb0;
406 data->changed |= CHANGED_DOTCLOCK;
407 return 0;
408 }
409
410 /*
411 * Internal Functions
412 */
413
414 void
415 ibm561_update(vp)
416 void *vp;
417 {
418 struct ibm561data *data = (struct ibm561data *)vp;
419 int i;
420
421 /* XXX see comment above ibm561_cninit() */
422 if (!data)
423 data = saved_console_data;
424
425 if (data->changed & CHANGED_WTYPE) {
426 ibm561_regbegin(data, IBM561_FB_WINTYPE);
427 for (i=0; i < IBM561_NWTYPES; i++)
428 ibm561_regcont10bit(data, IBM561_CMD_FB_WAT, data->wtype[i]);
429
430 /* XXXrcd: quick hack here for AUX FB table */
431 ibm561_regbegin(data, IBM561_AUXFB_WINTYPE);
432 for (i=0; i < IBM561_NWTYPES; i++)
433 ibm561_regcont(data, IBM561_CMD, 0x04);
434
435 /* XXXrcd: quick hack here for OL WAT table */
436 ibm561_regbegin(data, IBM561_OL_WINTYPE);
437 for (i=0; i < IBM561_NWTYPES; i++)
438 ibm561_regcont10bit(data, IBM561_CMD_FB_WAT, 0x0231);
439
440 /* XXXrcd: quick hack here for AUX OL WAT table */
441 ibm561_regbegin(data, IBM561_AUXOL_WINTYPE);
442 for (i=0; i < IBM561_NWTYPES; i++)
443 ibm561_regcont(data, IBM561_CMD, 0x0c);
444 }
445
446 if (data->changed & CHANGED_CMAP)
447 ibm561_load_cmap(data);
448
449 /* XXX: I am not sure in what situations it is safe to
450 * change the dotclock---hope this is good.
451 */
452 if (data->changed & CHANGED_DOTCLOCK)
453 ibm561_load_dotclock(data);
454 }
455
456 static void
457 ibm561_load_cmap(struct ibm561data *data)
458 {
459 int i;
460
461 ibm561_regbegin(data, IBM561_CMAP_TABLE);
462 for (i=0; i < IBM561_NCMAP_ENTRIES; i++) {
463 ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_r[i]);
464 ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_g[i]);
465 ibm561_regcont(data, IBM561_CMD_CMAP, data->cmap_b[i]);
466 }
467
468 ibm561_regbegin(data, IBM561_RED_GAMMA_TABLE);
469 for (i=0; i < 256; i++)
470 ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_r[i]);
471
472 ibm561_regbegin(data, IBM561_GREEN_GAMMA_TABLE);
473 for (i=1; i < 256; i++)
474 ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_g[i]);
475
476 ibm561_regbegin(data, IBM561_BLUE_GAMMA_TABLE);
477 for (i=1; i < 256; i++)
478 ibm561_regcont10bit(data, IBM561_CMD_GAMMA, data->gamma_b[i]);
479
480 }
481
482 static void
483 ibm561_load_dotclock(struct ibm561data *data)
484 {
485 /* XXX
486 * we should probably be more pro-active here, but it shouldn't
487 * actually happen...
488 */
489 if (!data->vco_div || !data->pll_ref || ! data->div_dotclock) {
490 panic("ibm561_load_dotclock: called uninitialized");
491 }
492
493 ibm561_regwr(data, IBM561_PLL_VCO_DIV, data->vco_div);
494 ibm561_regwr(data, IBM561_PLL_REF_REG, data->pll_ref);
495 ibm561_regwr(data, IBM561_DIV_DOTCLCK, data->div_dotclock);
496 }
497
498 static void
499 ibm561_regcont10bit(struct ibm561data *data, u_int16_t reg, u_int16_t val)
500 {
501 data->ramdac_wr(data->cookie, IBM561_CMD_GAMMA, (val >> 2) & 0xff);
502 data->ramdac_wr(data->cookie, IBM561_CMD_GAMMA, (val & 0x3) << 6);
503 }
504
505 static void
506 ibm561_regbegin(struct ibm561data *data, u_int16_t reg)
507 {
508 data->ramdac_wr(data->cookie, IBM561_ADDR_LOW, reg & 0xff);
509 data->ramdac_wr(data->cookie, IBM561_ADDR_HIGH, (reg >> 8) & 0xff);
510 }
511
512 static void
513 ibm561_regcont(struct ibm561data *data, u_int16_t reg, u_int8_t val)
514 {
515 data->ramdac_wr(data->cookie, reg, val);
516 }
517
518 static void
519 ibm561_regwr(struct ibm561data *data, u_int16_t reg, u_int8_t val)
520 {
521 ibm561_regbegin(data, reg);
522 ibm561_regcont(data, IBM561_CMD, val);
523 }
524