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