bt485.c revision 1.8 1 /* $NetBSD: bt485.c,v 1.8 2001/12/12 07:47:46 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/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: bt485.c,v 1.8 2001/12/12 07:47:46 elric 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 int s;
268
269 if (cmapp->index >= 256 || (cmapp->index + cmapp->count) > 256)
270 return (EINVAL);
271 if (!uvm_useracc(cmapp->red, cmapp->count, B_READ) ||
272 !uvm_useracc(cmapp->green, cmapp->count, B_READ) ||
273 !uvm_useracc(cmapp->blue, cmapp->count, B_READ))
274 return (EFAULT);
275
276 s = spltty();
277
278 index = cmapp->index;
279 count = cmapp->count;
280 copyin(cmapp->red, &data->cmap_r[index], count);
281 copyin(cmapp->green, &data->cmap_g[index], count);
282 copyin(cmapp->blue, &data->cmap_b[index], count);
283
284 data->changed |= DATA_CMAP_CHANGED;
285
286 data->ramdac_sched_update(data->cookie, bt485_update);
287 splx(s);
288
289 return (0);
290 }
291
292 int
293 bt485_get_cmap(rc, cmapp)
294 struct ramdac_cookie *rc;
295 struct wsdisplay_cmap *cmapp;
296 {
297 struct bt485data *data = (struct bt485data *)rc;
298 int error, count, index;
299
300 if ((u_int)cmapp->index >= 256 ||
301 ((u_int)cmapp->index + (u_int)cmapp->count) > 256)
302 return (EINVAL);
303
304 count = cmapp->count;
305 index = cmapp->index;
306
307 error = copyout(&data->cmap_r[index], cmapp->red, count);
308 if (error)
309 return (error);
310 error = copyout(&data->cmap_g[index], cmapp->green, count);
311 if (error)
312 return (error);
313 error = copyout(&data->cmap_b[index], cmapp->blue, count);
314 return (error);
315 }
316
317 int
318 bt485_set_cursor(rc, cursorp)
319 struct ramdac_cookie *rc;
320 struct wsdisplay_cursor *cursorp;
321 {
322 struct bt485data *data = (struct bt485data *)rc;
323 u_int count, index, v;
324 int s;
325
326 v = cursorp->which;
327
328 /*
329 * For DOCMAP and DOSHAPE, verify that parameters are OK
330 * before we do anything that we can't recover from.
331 */
332 if (v & WSDISPLAY_CURSOR_DOCMAP) {
333 if (cursorp->cmap.index > 2 ||
334 (cursorp->cmap.index + cursorp->cmap.count) > 2)
335 return (EINVAL);
336 count = cursorp->cmap.count;
337 if (!uvm_useracc(cursorp->cmap.red, count, B_READ) ||
338 !uvm_useracc(cursorp->cmap.green, count, B_READ) ||
339 !uvm_useracc(cursorp->cmap.blue, count, B_READ))
340 return (EFAULT);
341 }
342 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
343 if (cursorp->size.x > CURSOR_MAX_SIZE ||
344 cursorp->size.y > CURSOR_MAX_SIZE)
345 return (EINVAL);
346 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
347 if (!uvm_useracc(cursorp->image, count, B_READ) ||
348 !uvm_useracc(cursorp->mask, count, B_READ))
349 return (EFAULT);
350 }
351
352 if (v & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOCUR)) {
353 if (v & WSDISPLAY_CURSOR_DOPOS)
354 data->curpos = cursorp->pos;
355 if (v & WSDISPLAY_CURSOR_DOCUR)
356 data->curhot = cursorp->hot;
357 bt485_update_curpos(data);
358 }
359
360 s = spltty();
361
362 /* Parameters are OK; perform the requested operations. */
363 if (v & WSDISPLAY_CURSOR_DOCUR) {
364 data->curenb = cursorp->enable;
365 data->changed |= DATA_ENB_CHANGED;
366 }
367 if (v & WSDISPLAY_CURSOR_DOCMAP) {
368 count = cursorp->cmap.count;
369 index = cursorp->cmap.index;
370 copyin(cursorp->cmap.red, &data->curcmap_r[index], count);
371 copyin(cursorp->cmap.green, &data->curcmap_g[index], count);
372 copyin(cursorp->cmap.blue, &data->curcmap_b[index], count);
373 data->changed |= DATA_CURCMAP_CHANGED;
374 }
375 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
376 data->cursize = cursorp->size;
377 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
378 memset(data->curimage, 0, sizeof data->curimage);
379 memset(data->curmask, 0, sizeof data->curmask);
380 copyin(cursorp->image, data->curimage, count); /* can't fail */
381 copyin(cursorp->mask, data->curmask, count); /* can't fail */
382 data->changed |= DATA_CURSHAPE_CHANGED;
383 }
384
385 if (data->changed)
386 data->ramdac_sched_update(data->cookie, bt485_update);
387 splx(s);
388
389 return (0);
390 }
391
392 int
393 bt485_get_cursor(rc, cursorp)
394 struct ramdac_cookie *rc;
395 struct wsdisplay_cursor *cursorp;
396 {
397 struct bt485data *data = (struct bt485data *)rc;
398 int error, count;
399
400 /* we return everything they want */
401 cursorp->which = WSDISPLAY_CURSOR_DOALL;
402
403 cursorp->enable = data->curenb; /* DOCUR */
404 cursorp->pos = data->curpos; /* DOPOS */
405 cursorp->hot = data->curhot; /* DOHOT */
406
407 cursorp->cmap.index = 0; /* DOCMAP */
408 cursorp->cmap.count = 2;
409 if (cursorp->cmap.red != NULL) {
410 error = copyout(data->curcmap_r, cursorp->cmap.red, 2);
411 if (error)
412 return (error);
413 }
414 if (cursorp->cmap.green != NULL) {
415 error = copyout(data->curcmap_g, cursorp->cmap.green, 2);
416 if (error)
417 return (error);
418 }
419 if (cursorp->cmap.blue != NULL) {
420 error = copyout(data->curcmap_b, cursorp->cmap.blue, 2);
421 if (error)
422 return (error);
423 }
424
425 cursorp->size = data->cursize; /* DOSHAPE */
426 if (cursorp->image != NULL) {
427 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
428 error = copyout(data->curimage, cursorp->image, count);
429 if (error)
430 return (error);
431 error = copyout(data->curmask, cursorp->mask, count);
432 if (error)
433 return (error);
434 }
435
436 return (0);
437 }
438
439 int
440 bt485_set_curpos(rc, curposp)
441 struct ramdac_cookie *rc;
442 struct wsdisplay_curpos *curposp;
443 {
444 struct bt485data *data = (struct bt485data *)rc;
445
446 data->curpos = *curposp;
447 bt485_update_curpos(data);
448
449 return (0);
450 }
451
452 int
453 bt485_get_curpos(rc, curposp)
454 struct ramdac_cookie *rc;
455 struct wsdisplay_curpos *curposp;
456 {
457 struct bt485data *data = (struct bt485data *)rc;
458
459 *curposp = data->curpos;
460 return (0);
461 }
462
463 int
464 bt485_get_curmax(rc, curposp)
465 struct ramdac_cookie *rc;
466 struct wsdisplay_curpos *curposp;
467 {
468
469 curposp->x = curposp->y = CURSOR_MAX_SIZE;
470 return (0);
471 }
472
473 /*****************************************************************************/
474
475 /*
476 * Internal functions.
477 */
478
479 inline void
480 bt485_wr_i(data, ireg, val)
481 struct bt485data *data;
482 u_int8_t ireg;
483 u_int8_t val;
484 {
485 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
486 data->ramdac_wr(data->cookie, BT485_REG_EXTENDED, val);
487 }
488
489 inline u_int8_t
490 bt485_rd_i(data, ireg)
491 struct bt485data *data;
492 u_int8_t ireg;
493 {
494 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
495 return (data->ramdac_rd(data->cookie, BT485_REG_EXTENDED));
496 }
497
498 void
499 bt485_update(vp)
500 void *vp;
501 {
502 struct bt485data *data = vp;
503 u_int8_t regval;
504 int count, i, v;
505
506 v = data->changed;
507 data->changed = 0;
508
509 if (v & DATA_ENB_CHANGED) {
510 regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_2);
511 if (data->curenb)
512 regval |= 0x01;
513 else
514 regval &= ~0x03;
515 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_2, regval);
516 }
517
518 if (v & DATA_CURCMAP_CHANGED) {
519 /* addr[9:0] assumed to be 0 */
520 /* set addr[7:0] to 1 */
521 data->ramdac_wr(data->cookie, BT485_REG_COC_WRADDR, 0x01);
522
523 /* spit out the cursor data */
524 for (i = 0; i < 2; i++) {
525 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
526 data->curcmap_r[i]);
527 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
528 data->curcmap_g[i]);
529 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
530 data->curcmap_b[i]);
531 }
532 }
533
534 if (v & DATA_CURSHAPE_CHANGED) {
535 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
536
537 /*
538 * Write the cursor image data:
539 * set addr[9:8] to 0,
540 * set addr[7:0] to 0,
541 * spit it all out.
542 */
543 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
544 regval &= ~0x03;
545 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
546 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
547 for (i = 0; i < count; i++)
548 data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
549 data->curimage[i]);
550
551 /*
552 * Write the cursor mask data:
553 * set addr[9:8] to 2,
554 * set addr[7:0] to 0,
555 * spit it all out.
556 */
557 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
558 regval &= ~0x03; regval |= 0x02;
559 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
560 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
561 for (i = 0; i < count; i++)
562 data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
563 data->curmask[i]);
564
565 /* set addr[9:0] back to 0 */
566 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
567 regval &= ~0x03;
568 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
569 }
570
571 if (v & DATA_CMAP_CHANGED) {
572 /* addr[9:0] assumed to be 0 */
573 /* set addr[7:0] to 0 */
574 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0x00);
575
576 /* spit out the cursor data */
577 for (i = 0; i < 256; i++) {
578 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
579 data->cmap_r[i]);
580 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
581 data->cmap_g[i]);
582 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
583 data->cmap_b[i]);
584 }
585 }
586 }
587
588 void
589 bt485_update_curpos(data)
590 struct bt485data *data;
591 {
592 void *cookie = data->cookie;
593 int s, x, y;
594
595 s = spltty();
596
597 x = data->curpos.x + CURSOR_MAX_SIZE - data->curhot.x;
598 y = data->curpos.y + CURSOR_MAX_SIZE - data->curhot.y;
599 data->ramdac_wr(cookie, BT485_REG_CURSOR_X_LOW, x & 0xff);
600 data->ramdac_wr(cookie, BT485_REG_CURSOR_X_HIGH, (x >> 8) & 0x0f);
601 data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_LOW, y & 0xff);
602 data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_HIGH, (y >> 8) & 0x0f);
603
604 splx(s);
605 }
606