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