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