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