tga.c revision 1.5 1 /* $NetBSD: tga.c,v 1.5 1998/06/20 21:56:40 drochner 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 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/device.h>
34 #include <sys/conf.h>
35 #include <sys/malloc.h>
36 #include <sys/buf.h>
37 #include <sys/ioctl.h>
38
39 #include <machine/bus.h>
40 #include <machine/intr.h>
41
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcidevs.h>
45 #include <dev/pci/tgareg.h>
46 #include <dev/pci/tgavar.h>
47 #include <dev/ic/bt485reg.h>
48
49 #include <dev/rcons/raster.h>
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wscons_raster.h>
52 #include <dev/wscons/wsdisplayvar.h>
53
54 #ifdef __alpha__
55 #include <machine/pte.h>
56 #endif
57
58 int tgamatch __P((struct device *, struct cfdata *, void *));
59 void tgaattach __P((struct device *, struct device *, void *));
60 int tgaprint __P((void *, const char *));
61
62 struct cfattach tga_ca = {
63 sizeof(struct tga_softc), tgamatch, tgaattach,
64 };
65
66 int tga_identify __P((tga_reg_t *));
67 const struct tga_conf *tga_getconf __P((int));
68 void tga_getdevconfig __P((bus_space_tag_t memt, pci_chipset_tag_t pc,
69 pcitag_t tag, struct tga_devconfig *dc));
70
71 struct tga_devconfig tga_console_dc;
72
73 struct wsdisplay_emulops tga_emulops = {
74 rcons_cursor, /* could use hardware cursor; punt */
75 rcons_putchar,
76 rcons_copycols,
77 rcons_erasecols,
78 rcons_copyrows,
79 rcons_eraserows,
80 rcons_alloc_attr
81 };
82
83 struct wsscreen_descr tga_stdscreen = {
84 "std",
85 0, 0, /* will be filled in -- XXX shouldn't, it's global */
86 &tga_emulops,
87 0, 0,
88 WSSCREEN_REVERSE
89 };
90
91 const struct wsscreen_descr *_tga_scrlist[] = {
92 &tga_stdscreen,
93 /* XXX other formats, graphics screen? */
94 };
95
96 struct wsscreen_list tga_screenlist = {
97 sizeof(_tga_scrlist) / sizeof(struct wsscreen_descr *), _tga_scrlist
98 };
99
100 int tga_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
101 int tga_mmap __P((void *, off_t, int));
102 static int tga_alloc_screen __P((void *, const struct wsscreen_descr *,
103 void **, int *, int *, long *));
104 static void tga_free_screen __P((void *, void *));
105 static void tga_show_screen __P((void *, void *));
106 static int tga_load_font __P((void *, void *, int, int, int, void *));
107
108 struct wsdisplay_accessops tga_accessops = {
109 tga_ioctl,
110 tga_mmap,
111 tga_alloc_screen,
112 tga_free_screen,
113 tga_show_screen,
114 tga_load_font
115 };
116
117 void tga_blank __P((struct tga_devconfig *));
118 void tga_unblank __P((struct tga_devconfig *));
119
120 int
121 tgamatch(parent, match, aux)
122 struct device *parent;
123 struct cfdata *match;
124 void *aux;
125 {
126 struct pci_attach_args *pa = aux;
127
128 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC ||
129 PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_DEC_21030)
130 return (0);
131
132 return (10);
133 }
134
135 void
136 tga_getdevconfig(memt, pc, tag, dc)
137 bus_space_tag_t memt;
138 pci_chipset_tag_t pc;
139 pcitag_t tag;
140 struct tga_devconfig *dc;
141 {
142 const struct tga_conf *tgac;
143 const struct tga_ramdac_conf *tgar;
144 struct raster *rap;
145 struct rcons *rcp;
146 bus_size_t pcisize;
147 int i, flags;
148
149 dc->dc_memt = memt;
150 dc->dc_pc = pc;
151
152 dc->dc_pcitag = tag;
153
154 /* XXX magic number */
155 if (pci_mapreg_info(pc, tag, 0x10,
156 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
157 &dc->dc_pcipaddr, &pcisize, &flags))
158 return;
159 if ((flags & BUS_SPACE_MAP_CACHEABLE) == 0) /* XXX */
160 panic("tga memory not cacheable");
161
162 if (bus_space_map(memt, dc->dc_pcipaddr, pcisize,
163 BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_LINEAR, &dc->dc_vaddr))
164 return;
165 #ifdef __alpha__
166 dc->dc_paddr = ALPHA_K0SEG_TO_PHYS(dc->dc_vaddr); /* XXX */
167 #endif
168
169 dc->dc_regs = (tga_reg_t *)(dc->dc_vaddr + TGA_MEM_CREGS);
170 dc->dc_tga_type = tga_identify(dc->dc_regs);
171 tgac = dc->dc_tgaconf = tga_getconf(dc->dc_tga_type);
172 if (tgac == NULL)
173 return;
174
175 #if 0
176 /* XXX on the Alpha, pcisize = 4 * cspace_size. */
177 if (tgac->tgac_cspace_size != pcisize) /* sanity */
178 panic("tga_getdevconfig: memory size mismatch?");
179 #endif
180
181 tgar = tgac->tgac_ramdac;
182
183 switch (dc->dc_regs[TGA_REG_VHCR] & 0x1ff) { /* XXX */
184 case 0:
185 dc->dc_wid = 8192;
186 break;
187
188 case 1:
189 dc->dc_wid = 8196;
190 break;
191
192 default:
193 dc->dc_wid = (dc->dc_regs[TGA_REG_VHCR] & 0x1ff) * 4; /* XXX */
194 break;
195 }
196
197 dc->dc_rowbytes = dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
198
199 if ((dc->dc_regs[TGA_REG_VHCR] & 0x00000001) != 0 && /* XXX */
200 (dc->dc_regs[TGA_REG_VHCR] & 0x80000000) != 0) { /* XXX */
201 dc->dc_wid -= 4;
202 /*
203 * XXX XXX turning off 'odd' shouldn't be necesssary,
204 * XXX XXX but i can't make X work with the weird size.
205 */
206 dc->dc_regs[TGA_REG_VHCR] &= ~0x80000001;
207 dc->dc_rowbytes =
208 dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
209 }
210
211 dc->dc_ht = (dc->dc_regs[TGA_REG_VVCR] & 0x7ff); /* XXX */
212
213 /* XXX this seems to be what DEC does */
214 dc->dc_regs[TGA_REG_CCBR] = 0;
215 dc->dc_regs[TGA_REG_VVBR] = 1;
216 dc->dc_videobase = dc->dc_vaddr + tgac->tgac_dbuf[0] +
217 1 * tgac->tgac_vvbr_units;
218 dc->dc_blanked = 1;
219 tga_unblank(dc);
220
221 /*
222 * Set all bits in the pixel mask, to enable writes to all pixels.
223 * It seems that the console firmware clears some of them
224 * under some circumstances, which causes cute vertical stripes.
225 */
226 dc->dc_regs[TGA_REG_GPXR_P] = 0xffffffff;
227
228 /* clear the screen */
229 for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
230 *(u_int32_t *)(dc->dc_videobase + i) = 0;
231
232 /* initialize the raster */
233 rap = &dc->dc_raster;
234 rap->width = dc->dc_wid;
235 rap->height = dc->dc_ht;
236 rap->depth = tgac->tgac_phys_depth;
237 rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
238 rap->pixels = (u_int32_t *)dc->dc_videobase;
239
240 /* initialize the raster console blitter */
241 rcp = &dc->dc_rcons;
242 rcp->rc_sp = rap;
243 rcp->rc_crow = rcp->rc_ccol = -1;
244 rcp->rc_crowp = &rcp->rc_crow;
245 rcp->rc_ccolp = &rcp->rc_ccol;
246 rcons_init(rcp, 34, 80);
247
248 tga_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
249 tga_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
250 }
251
252 void
253 tgaattach(parent, self, aux)
254 struct device *parent, *self;
255 void *aux;
256 {
257 struct pci_attach_args *pa = aux;
258 struct tga_softc *sc = (struct tga_softc *)self;
259 struct wsemuldisplaydev_attach_args aa;
260 pci_intr_handle_t intrh;
261 const char *intrstr;
262 u_int8_t rev;
263 int console;
264
265 #ifdef __alpha__
266 console = (pa->pa_tag == tga_console_dc.dc_pcitag);
267 #else
268 console = 0;
269 #endif
270 if (console) {
271 sc->sc_dc = &tga_console_dc;
272 sc->nscreens = 1;
273 } else {
274 sc->sc_dc = (struct tga_devconfig *)
275 malloc(sizeof(struct tga_devconfig), M_DEVBUF, M_WAITOK);
276 tga_getdevconfig(pa->pa_memt, pa->pa_pc, pa->pa_tag, sc->sc_dc);
277 }
278 if (sc->sc_dc->dc_vaddr == NULL) {
279 printf(": couldn't map memory space; punt!\n");
280 return;
281 }
282
283 /* XXX say what's going on. */
284 intrstr = NULL;
285 if (sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_intr != NULL) {
286 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
287 pa->pa_intrline, &intrh)) {
288 printf(": couldn't map interrupt");
289 return;
290 }
291 intrstr = pci_intr_string(pa->pa_pc, intrh);
292 sc->sc_intr = pci_intr_establish(pa->pa_pc, intrh, IPL_TTY,
293 sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_intr, sc->sc_dc);
294 if (sc->sc_intr == NULL) {
295 printf(": couldn't establish interrupt");
296 if (intrstr != NULL)
297 printf("at %s", intrstr);
298 printf("\n");
299 return;
300 }
301 }
302
303 /*
304 * Initialize the RAMDAC and allocate any private storage it needs.
305 * Initialization includes disabling cursor, setting a sane
306 * colormap, etc.
307 */
308 (*sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_init)(sc->sc_dc, 1);
309
310 printf(": DC21030 ");
311 rev = PCI_REVISION(pa->pa_class);
312 switch (rev) {
313 case 1: case 2: case 3:
314 printf("step %c", 'A' + rev - 1);
315 break;
316
317 default:
318 printf("unknown stepping (0x%x)", rev);
319 break;
320 }
321 printf(", ");
322
323 if (sc->sc_dc->dc_tgaconf == NULL) {
324 printf("unknown board configuration\n");
325 return;
326 }
327 printf("board type %s\n", sc->sc_dc->dc_tgaconf->tgac_name);
328 printf("%s: %d x %d, %dbpp, %s RAMDAC\n", sc->sc_dev.dv_xname,
329 sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
330 sc->sc_dc->dc_tgaconf->tgac_phys_depth,
331 sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_name);
332
333 if (intrstr != NULL)
334 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
335 intrstr);
336
337 aa.console = console;
338 aa.scrdata = &tga_screenlist;
339 aa.accessops = &tga_accessops;
340 aa.accesscookie = sc;
341
342 config_found(self, &aa, wsemuldisplaydevprint);
343 }
344
345 int
346 tga_ioctl(v, cmd, data, flag, p)
347 void *v;
348 u_long cmd;
349 caddr_t data;
350 int flag;
351 struct proc *p;
352 {
353 struct tga_softc *sc = v;
354 struct tga_devconfig *dc = sc->sc_dc;
355 const struct tga_ramdac_conf *tgar = dc->dc_tgaconf->tgac_ramdac;
356
357 switch (cmd) {
358 case WSDISPLAYIO_GTYPE:
359 *(u_int *)data = WSDISPLAY_TYPE_TGA;
360 return (0);
361
362 case WSDISPLAYIO_GINFO:
363 #define wsd_fbip ((struct wsdisplay_fbinfo *)data)
364 wsd_fbip->height = sc->sc_dc->dc_ht;
365 wsd_fbip->width = sc->sc_dc->dc_wid;
366 wsd_fbip->depth = sc->sc_dc->dc_tgaconf->tgac_phys_depth;
367 wsd_fbip->cmsize = 256; /* XXX ??? */
368 #undef fbt
369 return (0);
370
371 case WSDISPLAYIO_GETCMAP:
372 return (*tgar->tgar_get_cmap)(dc,
373 (struct wsdisplay_cmap *)data);
374
375 case WSDISPLAYIO_PUTCMAP:
376 return (*tgar->tgar_set_cmap)(dc,
377 (struct wsdisplay_cmap *)data);
378
379 case WSDISPLAYIO_GVIDEO:
380 if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
381 tga_blank(sc->sc_dc);
382 else
383 tga_unblank(sc->sc_dc);
384 return (0);
385
386 case WSDISPLAYIO_SVIDEO:
387 *(u_int *)data = dc->dc_blanked ?
388 WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
389 return (0);
390
391 case WSDISPLAYIO_GCURPOS:
392 return (*tgar->tgar_get_curpos)(dc,
393 (struct wsdisplay_curpos *)data);
394
395 case WSDISPLAYIO_SCURPOS:
396 return (*tgar->tgar_set_curpos)(dc,
397 (struct wsdisplay_curpos *)data);
398
399 case WSDISPLAYIO_GCURMAX:
400 return (*tgar->tgar_get_curmax)(dc,
401 (struct wsdisplay_curpos *)data);
402
403 case WSDISPLAYIO_GCURSOR:
404 return (*tgar->tgar_get_cursor)(dc,
405 (struct wsdisplay_cursor *)data);
406
407 case WSDISPLAYIO_SCURSOR:
408 return (*tgar->tgar_set_cursor)(dc,
409 (struct wsdisplay_cursor *)data);
410 }
411 return (-1);
412 }
413
414 int
415 tga_mmap(v, offset, prot)
416 void *v;
417 off_t offset;
418 int prot;
419 {
420
421 /* XXX NEW MAPPING CODE... */
422
423 #ifdef __alpha__
424 struct tga_softc *sc = v;
425
426 if (offset > sc->sc_dc->dc_tgaconf->tgac_cspace_size)
427 return -1;
428 return alpha_btop(sc->sc_dc->dc_paddr + offset);
429 #else
430 return (-1);
431 #endif
432 }
433
434 int
435 tga_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
436 void *v;
437 const struct wsscreen_descr *type;
438 void **cookiep;
439 int *curxp, *curyp;
440 long *attrp;
441 {
442 struct tga_softc *sc = v;
443 long defattr;
444
445 if (sc->nscreens > 0)
446 return (ENOMEM);
447
448 *cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
449 *curxp = 0;
450 *curyp = 0;
451 rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
452 *attrp = defattr;
453 sc->nscreens++;
454 return (0);
455 }
456
457 void
458 tga_free_screen(v, cookie)
459 void *v;
460 void *cookie;
461 {
462 struct tga_softc *sc = v;
463
464 if (sc->sc_dc == &tga_console_dc)
465 panic("tga_free_screen: console");
466
467 sc->nscreens--;
468 }
469
470 void
471 tga_show_screen(v, cookie)
472 void *v;
473 void *cookie;
474 {
475 }
476
477 static int
478 tga_load_font(v, cookie, first, num, stride, data)
479 void *v;
480 void *cookie;
481 int first, num, stride;
482 void *data;
483 {
484 return (EINVAL);
485 }
486
487 int
488 tga_cnattach(iot, memt, pc, bus, device, function)
489 bus_space_tag_t iot, memt;
490 pci_chipset_tag_t pc;
491 int bus, device, function;
492 {
493 struct tga_devconfig *dcp = &tga_console_dc;
494 long defattr;
495
496 tga_getdevconfig(memt, pc,
497 pci_make_tag(pc, bus, device, function), dcp);
498
499 /* sanity checks */
500 if (dcp->dc_vaddr == NULL)
501 panic("tga_console(%d, %d): couldn't map memory space",
502 device, function);
503 if (dcp->dc_tgaconf == NULL)
504 panic("tga_console(%d, %d): unknown board configuration",
505 device, function);
506
507 /*
508 * Initialize the RAMDAC but DO NOT allocate any private storage.
509 * Initialization includes disabling cursor, setting a sane
510 * colormap, etc. It will be reinitialized in tgaattach().
511 */
512 (*dcp->dc_tgaconf->tgac_ramdac->tgar_init)(dcp, 0);
513
514 rcons_alloc_attr(&dcp->dc_rcons, 0, 0, 0, &defattr);
515
516 wsdisplay_cnattach(&tga_stdscreen, &dcp->dc_rcons,
517 0, 0, defattr);
518
519 return(0);
520 }
521
522 /*
523 * Functions to blank and unblank the display.
524 */
525 void
526 tga_blank(dc)
527 struct tga_devconfig *dc;
528 {
529
530 if (!dc->dc_blanked) {
531 dc->dc_blanked = 1;
532 dc->dc_regs[TGA_REG_VVVR] |= VVR_BLANK; /* XXX */
533 }
534 }
535
536 void
537 tga_unblank(dc)
538 struct tga_devconfig *dc;
539 {
540
541 if (dc->dc_blanked) {
542 dc->dc_blanked = 0;
543 dc->dc_regs[TGA_REG_VVVR] &= ~VVR_BLANK; /* XXX */
544 }
545 }
546
547 /*
548 * Functions to manipulate the built-in cursor handing hardware.
549 */
550 int
551 tga_builtin_set_cursor(dc, cursorp)
552 struct tga_devconfig *dc;
553 struct wsdisplay_cursor *cursorp;
554 {
555 int v;
556 #if 0
557 int count;
558 #endif
559
560 v = cursorp->which;
561 #if 0
562 if (v & WSDISPLAY_CURSOR_DOCMAP) /* XXX should be supported */
563 return EINVAL;
564 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
565 if ((u_int)cursorp->size.x != 64 ||
566 (u_int)cursorp->size.y > 64)
567 return (EINVAL);
568 /* The cursor is 2 bits deep, and there is no mask */
569 count = (cursorp->size.y * 64 * 2) / NBBY;
570 if (!useracc(cursorp->image, count, B_READ))
571 return (EFAULT);
572 }
573 if (v & WSDISPLAY_CURSOR_DOHOT) /* not supported */
574 return EINVAL;
575 #endif
576
577 /* parameters are OK; do it */
578 if (v & WSDISPLAY_CURSOR_DOCUR) {
579 if (cursorp->enable)
580 dc->dc_regs[TGA_REG_VVVR] |= 0x04; /* XXX */
581 else
582 dc->dc_regs[TGA_REG_VVVR] &= ~0x04; /* XXX */
583 }
584 #if 0
585 if (v & WSDISPLAY_CURSOR_DOPOS) {
586 dc->dc_regs[TGA_REG_CXYR] = ((cursorp->pos.y & 0xfff) << 12) |
587 (cursorp->pos.x & 0xfff);
588 }
589 if (v & WSDISPLAY_CURSOR_DOCMAP) {
590 /* XXX */
591 }
592 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
593 dc->dc_regs[TGA_REG_CCBR] =
594 (dc->dc_regs[TGA_REG_CCBR] & ~0xfc00) |
595 (cursorp->size.y << 10);
596 copyin(cursorp->image, (char *)(dc->dc_vaddr +
597 (dc->dc_regs[TGA_REG_CCBR] & 0x3ff)),
598 count); /* can't fail. */
599 }
600 #endif
601 return (0);
602 }
603
604 int
605 tga_builtin_get_cursor(dc, cursorp)
606 struct tga_devconfig *dc;
607 struct wsdisplay_cursor *cursorp;
608 {
609 int count, error;
610
611 cursorp->which = WSDISPLAY_CURSOR_DOALL &
612 ~(WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOCMAP);
613 cursorp->enable = (dc->dc_regs[TGA_REG_VVVR] & 0x04) != 0;
614 cursorp->pos.x = dc->dc_regs[TGA_REG_CXYR] & 0xfff;
615 cursorp->pos.y = (dc->dc_regs[TGA_REG_CXYR] >> 12) & 0xfff;
616 cursorp->size.x = 64;
617 cursorp->size.y = (dc->dc_regs[TGA_REG_CCBR] >> 10) & 0x3f;
618
619 if (cursorp->image != NULL) {
620 count = (cursorp->size.y * 64 * 2) / NBBY;
621 error = copyout((char *)(dc->dc_vaddr +
622 (dc->dc_regs[TGA_REG_CCBR] & 0x3ff)),
623 cursorp->image, count);
624 if (error)
625 return (error);
626 /* No mask */
627 }
628 /* XXX No color map */
629 return (0);
630 }
631
632 int
633 tga_builtin_set_curpos(dc, curposp)
634 struct tga_devconfig *dc;
635 struct wsdisplay_curpos *curposp;
636 {
637
638 dc->dc_regs[TGA_REG_CXYR] =
639 ((curposp->y & 0xfff) << 12) | (curposp->x & 0xfff);
640 return (0);
641 }
642
643 int
644 tga_builtin_get_curpos(dc, curposp)
645 struct tga_devconfig *dc;
646 struct wsdisplay_curpos *curposp;
647 {
648
649 curposp->x = dc->dc_regs[TGA_REG_CXYR] & 0xfff;
650 curposp->y = (dc->dc_regs[TGA_REG_CXYR] >> 12) & 0xfff;
651 return (0);
652 }
653
654 int
655 tga_builtin_get_curmax(dc, curposp)
656 struct tga_devconfig *dc;
657 struct wsdisplay_curpos *curposp;
658 {
659
660 curposp->x = curposp->y = 64;
661 return (0);
662 }
663