bt485.c revision 1.10 1 /* $NetBSD: bt485.c,v 1.10 2003/11/13 03:09:29 chs Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 /* This code was derived from and originally located in sys/dev/pci/
31 * NetBSD: tga_bt485.c,v 1.4 1999/03/24 05:51:21 mrg Exp
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: bt485.c,v 1.10 2003/11/13 03:09:29 chs Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/buf.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <dev/pci/pcivar.h>
47 #include <dev/ic/bt485reg.h>
48 #include <dev/ic/bt485var.h>
49 #include <dev/ic/ramdac.h>
50
51 #include <dev/wscons/wsconsio.h>
52
53 /*
54 * Functions exported via the RAMDAC configuration table.
55 */
56 void bt485_init __P((struct ramdac_cookie *));
57 int bt485_set_cmap __P((struct ramdac_cookie *,
58 struct wsdisplay_cmap *));
59 int bt485_get_cmap __P((struct ramdac_cookie *,
60 struct wsdisplay_cmap *));
61 int bt485_set_cursor __P((struct ramdac_cookie *,
62 struct wsdisplay_cursor *));
63 int bt485_get_cursor __P((struct ramdac_cookie *,
64 struct wsdisplay_cursor *));
65 int bt485_set_curpos __P((struct ramdac_cookie *,
66 struct wsdisplay_curpos *));
67 int bt485_get_curpos __P((struct ramdac_cookie *,
68 struct wsdisplay_curpos *));
69 int bt485_get_curmax __P((struct ramdac_cookie *,
70 struct wsdisplay_curpos *));
71
72 /* XXX const */
73 struct ramdac_funcs bt485_funcsstruct = {
74 "Bt485",
75 bt485_register,
76 bt485_init,
77 bt485_set_cmap,
78 bt485_get_cmap,
79 bt485_set_cursor,
80 bt485_get_cursor,
81 bt485_set_curpos,
82 bt485_get_curpos,
83 bt485_get_curmax,
84 NULL, /* check_curcmap; not needed */
85 NULL, /* set_curcmap; not needed */
86 NULL, /* get_curcmap; not needed */
87 NULL, /* no dot clock to set */
88 };
89
90 /*
91 * Private data.
92 */
93 struct bt485data {
94 void *cookie; /* This is what is passed
95 * around, and is probably
96 * struct tga_devconfig *
97 */
98
99 int (*ramdac_sched_update) __P((void *, void (*)(void *)));
100 void (*ramdac_wr) __P((void *, u_int, u_int8_t));
101 u_int8_t (*ramdac_rd) __P((void *, u_int));
102
103 int changed; /* what changed; see below */
104 int curenb; /* cursor enabled */
105 struct wsdisplay_curpos curpos; /* current cursor position */
106 struct wsdisplay_curpos curhot; /* cursor hotspot */
107 char curcmap_r[2]; /* cursor colormap */
108 char curcmap_g[2];
109 char curcmap_b[2];
110 struct wsdisplay_curpos cursize; /* current cursor size */
111 char curimage[512]; /* cursor image data */
112 char curmask[512]; /* cursor mask data */
113 char cmap_r[256]; /* colormap */
114 char cmap_g[256];
115 char cmap_b[256];
116 };
117
118 #define DATA_ENB_CHANGED 0x01 /* cursor enable changed */
119 #define DATA_CURCMAP_CHANGED 0x02 /* cursor colormap changed */
120 #define DATA_CURSHAPE_CHANGED 0x04 /* cursor size, image, mask changed */
121 #define DATA_CMAP_CHANGED 0x08 /* colormap changed */
122 #define DATA_ALL_CHANGED 0x0f
123
124 #define CURSOR_MAX_SIZE 64
125
126 /*
127 * Internal functions.
128 */
129 inline void bt485_wr_i __P((struct bt485data *, u_int8_t, u_int8_t));
130 inline u_int8_t bt485_rd_i __P((struct bt485data *, u_int8_t));
131 void bt485_update __P((void *));
132 void bt485_update_curpos __P((struct bt485data *));
133
134 /*****************************************************************************/
135
136 /*
137 * Functions exported via the RAMDAC configuration table.
138 */
139
140 struct ramdac_funcs *
141 bt485_funcs(void)
142 {
143 return &bt485_funcsstruct;
144 }
145
146 struct ramdac_cookie *
147 bt485_register(v, sched_update, wr, rd)
148 void *v;
149 int (*sched_update)(void *, void (*)(void *));
150 void (*wr)(void *, u_int, u_int8_t);
151 u_int8_t (*rd)(void *, u_int);
152 {
153 struct bt485data *data;
154 /*
155 * XXX -- comment out of date. rcd.
156 * If we should allocate a new private info struct, do so.
157 * Otherwise, use the one we have (if it's there), or
158 * use the temporary one on the stack.
159 */
160 data = malloc(sizeof *data, M_DEVBUF, M_WAITOK);
161 /* XXX -- if !data */
162 data->cookie = v;
163 data->ramdac_sched_update = sched_update;
164 data->ramdac_wr = wr;
165 data->ramdac_rd = rd;
166 return (struct ramdac_cookie *)data;
167 }
168
169 /*
170 * This function exists solely to provide a means to init
171 * the RAMDAC without first registering. It is useful for
172 * initializing the console early on.
173 */
174 void
175 bt485_cninit(v, sched_update, wr, rd)
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 {
181 struct bt485data tmp, *data = &tmp;
182 data->cookie = v;
183 data->ramdac_sched_update = sched_update;
184 data->ramdac_wr = wr;
185 data->ramdac_rd = rd;
186 bt485_init((struct ramdac_cookie *)data);
187 }
188
189 void
190 bt485_init(rc)
191 struct ramdac_cookie *rc;
192 {
193 u_int8_t regval;
194 struct bt485data *data = (struct bt485data *)rc;
195 int i;
196
197 /*
198 * Init the BT485 for normal operation.
199 */
200
201 /*
202 * Allow indirect register access. (Actually, this is
203 * already enabled. In fact, if it is _disabled_, for
204 * some reason the monitor appears to lose sync!!! (?!?!)
205 */
206 regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_0);
207 regval |= 0x80;
208 /*
209 * Set the RAMDAC to 8 bit resolution, rather than 6 bit
210 * resolution.
211 */
212 regval |= 0x02;
213 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_0, regval);
214
215 /* Set the RAMDAC to 8BPP (no interestion options). */
216 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_1, 0x40);
217
218 /* Disable the cursor (for now) */
219 regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_2);
220 regval &= ~0x03;
221 regval |= 0x24;
222 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_2, regval);
223
224 /* Use a 64x64x2 cursor */
225 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
226 regval |= 0x04;
227 regval |= 0x08;
228 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
229
230 /* Set the Pixel Mask to something useful */
231 data->ramdac_wr(data->cookie, BT485_REG_PIXMASK, 0xff);
232
233 /*
234 * Initialize the RAMDAC info struct to hold all of our
235 * data, and fill it in.
236 */
237 data->changed = DATA_ALL_CHANGED;
238
239 data->curenb = 0; /* cursor disabled */
240 data->curpos.x = data->curpos.y = 0; /* right now at 0,0 */
241 data->curhot.x = data->curhot.y = 0; /* hot spot at 0,0 */
242
243 /* initial cursor colormap: 0 is black, 1 is white */
244 data->curcmap_r[0] = data->curcmap_g[0] = data->curcmap_b[0] = 0;
245 data->curcmap_r[1] = data->curcmap_g[1] = data->curcmap_b[1] = 0xff;
246
247 /* initial cursor data: 64x64 block of white. */
248 data->cursize.x = data->cursize.y = 64;
249 for (i = 0; i < 512; i++)
250 data->curimage[i] = data->curmask[i] = 0xff;
251
252 /* Initial colormap: 0 is black, everything else is white */
253 data->cmap_r[0] = data->cmap_g[0] = data->cmap_b[0] = 0;
254 for (i = 1; i < 256; i++)
255 data->cmap_r[i] = data->cmap_g[i] = data->cmap_b[i] = 255;
256
257 bt485_update((void *)data);
258 }
259
260 int
261 bt485_set_cmap(rc, cmapp)
262 struct ramdac_cookie *rc;
263 struct wsdisplay_cmap *cmapp;
264 {
265 struct bt485data *data = (struct bt485data *)rc;
266 u_int count, index;
267 uint8_t r[256], g[256], b[256];
268 int s, error;
269
270 if (cmapp->index >= 256 || cmapp->count > 256 - cmapp->index)
271 return (EINVAL);
272
273 index = cmapp->index;
274 count = cmapp->count;
275 error = copyin(cmapp->red, &r[index], count);
276 if (error)
277 return error;
278 error = copyin(cmapp->green, &g[index], count);
279 if (error)
280 return error;
281 error = copyin(cmapp->blue, &b[index], count);
282 if (error)
283 return error;
284 s = spltty();
285 memcpy(&data->cmap_r[index], &r[index], count);
286 memcpy(&data->cmap_g[index], &g[index], count);
287 memcpy(&data->cmap_b[index], &b[index], count);
288 data->changed |= DATA_CMAP_CHANGED;
289 data->ramdac_sched_update(data->cookie, bt485_update);
290 splx(s);
291 return (0);
292 }
293
294 int
295 bt485_get_cmap(rc, cmapp)
296 struct ramdac_cookie *rc;
297 struct wsdisplay_cmap *cmapp;
298 {
299 struct bt485data *data = (struct bt485data *)rc;
300 u_int count, index;
301 int error;
302
303 if (cmapp->index >= 256 || cmapp->count > 256 - cmapp->index )
304 return (EINVAL);
305
306 count = cmapp->count;
307 index = cmapp->index;
308 error = copyout(&data->cmap_r[index], cmapp->red, count);
309 if (error)
310 return (error);
311 error = copyout(&data->cmap_g[index], cmapp->green, count);
312 if (error)
313 return (error);
314 error = copyout(&data->cmap_b[index], cmapp->blue, count);
315 return (error);
316 }
317
318 int
319 bt485_set_cursor(rc, cursorp)
320 struct ramdac_cookie *rc;
321 struct wsdisplay_cursor *cursorp;
322 {
323 struct bt485data *data = (struct bt485data *)rc;
324 u_int count = 0, icount = 0, index = 0, v;
325 char r[2], g[2], b[2], image[512], mask[512];
326 int s, error;
327
328 v = cursorp->which;
329
330 /*
331 * For DOCMAP and DOSHAPE, copy in the new data
332 * before we do anything that we can't recover from.
333 */
334 if (v & WSDISPLAY_CURSOR_DOCMAP) {
335 if (cursorp->cmap.index > 2 ||
336 (cursorp->cmap.index + cursorp->cmap.count) > 2)
337 return (EINVAL);
338 count = cursorp->cmap.count;
339 index = cursorp->cmap.index;
340 error = copyin(cursorp->cmap.red, &r[index], count);
341 if (error)
342 return error;
343 error = copyin(cursorp->cmap.green, &g[index], count);
344 if (error)
345 return error;
346 error = copyin(cursorp->cmap.blue, &b[index], count);
347 if (error)
348 return error;
349 }
350 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
351 if (cursorp->size.x > CURSOR_MAX_SIZE ||
352 cursorp->size.y > CURSOR_MAX_SIZE)
353 return (EINVAL);
354 icount = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
355 error = copyin(cursorp->image, image, icount);
356 if (error)
357 return error;
358 error = copyin(cursorp->mask, mask, icount);
359 if (error)
360 return error;
361 }
362
363 if (v & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOCUR)) {
364 if (v & WSDISPLAY_CURSOR_DOPOS)
365 data->curpos = cursorp->pos;
366 if (v & WSDISPLAY_CURSOR_DOCUR)
367 data->curhot = cursorp->hot;
368 bt485_update_curpos(data);
369 }
370
371 s = spltty();
372
373 /* Data is all available; perform the requested operations. */
374 if (v & WSDISPLAY_CURSOR_DOCUR) {
375 data->curenb = cursorp->enable;
376 data->changed |= DATA_ENB_CHANGED;
377 }
378 if (v & WSDISPLAY_CURSOR_DOCMAP) {
379 memcpy(&data->curcmap_r[index], &r[index], count);
380 memcpy(&data->curcmap_g[index], &g[index], count);
381 memcpy(&data->curcmap_b[index], &b[index], count);
382 data->changed |= DATA_CURCMAP_CHANGED;
383 }
384 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
385 data->cursize = cursorp->size;
386 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
387 memset(data->curimage, 0, sizeof data->curimage);
388 memset(data->curmask, 0, sizeof data->curmask);
389 memcpy(data->curimage, image, icount);
390 memcpy(data->curmask, mask, icount);
391 data->changed |= DATA_CURSHAPE_CHANGED;
392 }
393
394 if (data->changed)
395 data->ramdac_sched_update(data->cookie, bt485_update);
396 splx(s);
397
398 return (0);
399 }
400
401 int
402 bt485_get_cursor(rc, cursorp)
403 struct ramdac_cookie *rc;
404 struct wsdisplay_cursor *cursorp;
405 {
406 struct bt485data *data = (struct bt485data *)rc;
407 int error, count;
408
409 /* we return everything they want */
410 cursorp->which = WSDISPLAY_CURSOR_DOALL;
411
412 cursorp->enable = data->curenb; /* DOCUR */
413 cursorp->pos = data->curpos; /* DOPOS */
414 cursorp->hot = data->curhot; /* DOHOT */
415
416 cursorp->cmap.index = 0; /* DOCMAP */
417 cursorp->cmap.count = 2;
418 if (cursorp->cmap.red != NULL) {
419 error = copyout(data->curcmap_r, cursorp->cmap.red, 2);
420 if (error)
421 return (error);
422 }
423 if (cursorp->cmap.green != NULL) {
424 error = copyout(data->curcmap_g, cursorp->cmap.green, 2);
425 if (error)
426 return (error);
427 }
428 if (cursorp->cmap.blue != NULL) {
429 error = copyout(data->curcmap_b, cursorp->cmap.blue, 2);
430 if (error)
431 return (error);
432 }
433
434 cursorp->size = data->cursize; /* DOSHAPE */
435 if (cursorp->image != NULL) {
436 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
437 error = copyout(data->curimage, cursorp->image, count);
438 if (error)
439 return (error);
440 error = copyout(data->curmask, cursorp->mask, count);
441 if (error)
442 return (error);
443 }
444
445 return (0);
446 }
447
448 int
449 bt485_set_curpos(rc, curposp)
450 struct ramdac_cookie *rc;
451 struct wsdisplay_curpos *curposp;
452 {
453 struct bt485data *data = (struct bt485data *)rc;
454
455 data->curpos = *curposp;
456 bt485_update_curpos(data);
457
458 return (0);
459 }
460
461 int
462 bt485_get_curpos(rc, curposp)
463 struct ramdac_cookie *rc;
464 struct wsdisplay_curpos *curposp;
465 {
466 struct bt485data *data = (struct bt485data *)rc;
467
468 *curposp = data->curpos;
469 return (0);
470 }
471
472 int
473 bt485_get_curmax(rc, curposp)
474 struct ramdac_cookie *rc;
475 struct wsdisplay_curpos *curposp;
476 {
477
478 curposp->x = curposp->y = CURSOR_MAX_SIZE;
479 return (0);
480 }
481
482 /*****************************************************************************/
483
484 /*
485 * Internal functions.
486 */
487
488 inline void
489 bt485_wr_i(data, ireg, val)
490 struct bt485data *data;
491 u_int8_t ireg;
492 u_int8_t val;
493 {
494 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
495 data->ramdac_wr(data->cookie, BT485_REG_EXTENDED, val);
496 }
497
498 inline u_int8_t
499 bt485_rd_i(data, ireg)
500 struct bt485data *data;
501 u_int8_t ireg;
502 {
503 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
504 return (data->ramdac_rd(data->cookie, BT485_REG_EXTENDED));
505 }
506
507 void
508 bt485_update(vp)
509 void *vp;
510 {
511 struct bt485data *data = vp;
512 u_int8_t regval;
513 int count, i, v;
514
515 v = data->changed;
516 data->changed = 0;
517
518 if (v & DATA_ENB_CHANGED) {
519 regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_2);
520 if (data->curenb)
521 regval |= 0x01;
522 else
523 regval &= ~0x03;
524 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_2, regval);
525 }
526
527 if (v & DATA_CURCMAP_CHANGED) {
528 /* addr[9:0] assumed to be 0 */
529 /* set addr[7:0] to 1 */
530 data->ramdac_wr(data->cookie, BT485_REG_COC_WRADDR, 0x01);
531
532 /* spit out the cursor data */
533 for (i = 0; i < 2; i++) {
534 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
535 data->curcmap_r[i]);
536 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
537 data->curcmap_g[i]);
538 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
539 data->curcmap_b[i]);
540 }
541 }
542
543 if (v & DATA_CURSHAPE_CHANGED) {
544 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
545
546 /*
547 * Write the cursor image data:
548 * set addr[9:8] to 0,
549 * set addr[7:0] to 0,
550 * spit it all out.
551 */
552 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
553 regval &= ~0x03;
554 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
555 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
556 for (i = 0; i < count; i++)
557 data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
558 data->curimage[i]);
559
560 /*
561 * Write the cursor mask data:
562 * set addr[9:8] to 2,
563 * set addr[7:0] to 0,
564 * spit it all out.
565 */
566 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
567 regval &= ~0x03; regval |= 0x02;
568 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
569 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
570 for (i = 0; i < count; i++)
571 data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
572 data->curmask[i]);
573
574 /* set addr[9:0] back to 0 */
575 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
576 regval &= ~0x03;
577 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
578 }
579
580 if (v & DATA_CMAP_CHANGED) {
581 /* addr[9:0] assumed to be 0 */
582 /* set addr[7:0] to 0 */
583 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0x00);
584
585 /* spit out the cursor data */
586 for (i = 0; i < 256; i++) {
587 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
588 data->cmap_r[i]);
589 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
590 data->cmap_g[i]);
591 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
592 data->cmap_b[i]);
593 }
594 }
595 }
596
597 void
598 bt485_update_curpos(data)
599 struct bt485data *data;
600 {
601 void *cookie = data->cookie;
602 int s, x, y;
603
604 s = spltty();
605
606 x = data->curpos.x + CURSOR_MAX_SIZE - data->curhot.x;
607 y = data->curpos.y + CURSOR_MAX_SIZE - data->curhot.y;
608 data->ramdac_wr(cookie, BT485_REG_CURSOR_X_LOW, x & 0xff);
609 data->ramdac_wr(cookie, BT485_REG_CURSOR_X_HIGH, (x >> 8) & 0x0f);
610 data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_LOW, y & 0xff);
611 data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_HIGH, (y >> 8) & 0x0f);
612
613 splx(s);
614 }
615