Home | History | Annotate | Line # | Download | only in pci
tga.c revision 1.26
      1 /* $NetBSD: tga.c,v 1.26 2000/06/26 04:56:25 simonb 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 <vm/vm.h>
     40 
     41 #include <machine/bus.h>
     42 #include <machine/intr.h>
     43 
     44 #include <dev/pci/pcireg.h>
     45 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/pcidevs.h>
     47 #include <dev/pci/tgareg.h>
     48 #include <dev/pci/tgavar.h>
     49 #include <dev/ic/bt485reg.h>
     50 #include <dev/ic/bt485var.h>
     51 #include <dev/ic/bt463reg.h>
     52 #include <dev/ic/bt463var.h>
     53 
     54 #include <dev/wscons/wsconsio.h>
     55 #include <dev/wscons/wscons_raster.h>
     56 #include <dev/rasops/rasops.h>
     57 #include <dev/wsfont/wsfont.h>
     58 
     59 #ifdef __alpha__
     60 #include <machine/pte.h>
     61 #endif
     62 #ifdef __mips__
     63 #include <mips/pte.h>
     64 #endif
     65 
     66 int	tgamatch __P((struct device *, struct cfdata *, void *));
     67 void	tgaattach __P((struct device *, struct device *, void *));
     68 int	tgaprint __P((void *, const char *));
     69 
     70 struct cfattach tga_ca = {
     71 	sizeof(struct tga_softc), tgamatch, tgaattach,
     72 };
     73 
     74 int	tga_identify __P((struct tga_devconfig *));
     75 const struct tga_conf *tga_getconf __P((int));
     76 static void	tga_getdevconfig __P((bus_space_tag_t memt, pci_chipset_tag_t pc,
     77 	    pcitag_t tag, struct tga_devconfig *dc));
     78 
     79 struct tga_devconfig tga_console_dc;
     80 
     81 int tga_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     82 paddr_t tga_mmap __P((void *, off_t, int));
     83 static void tga_copyrows __P((void *, int, int, int));
     84 static void tga_copycols __P((void *, int, int, int, int));
     85 static int tga_alloc_screen __P((void *, const struct wsscreen_descr *,
     86 				      void **, int *, int *, long *));
     87 static void tga_free_screen __P((void *, void *));
     88 static int tga_show_screen __P((void *, void *, int,
     89 				void (*) (void *, int, int), void *));
     90 static int tga_rop __P((struct rasops_info *, int, int, int, int, int,
     91 	struct rasops_info *, int, int));
     92 static int tga_rop_vtov __P((struct rasops_info *, int, int, int, int,
     93 	int, struct rasops_info *, int, int ));
     94 static void tga_putchar __P((void *c, int row, int col,
     95 				u_int uc, long attr));
     96 static void tga_eraserows __P((void *, int, int, long));
     97 static void	tga_erasecols __P((void *, int, int, int, long));
     98 void tga2_init __P((struct tga_devconfig *, int));
     99 
    100 static void tga_config_interrupts __P((struct device *));
    101 
    102 /* RAMDAC interface functions */
    103 static int		tga_sched_update __P((void *, void (*)(void *)));
    104 static void		tga_ramdac_wr __P((void *, u_int, u_int8_t));
    105 static u_int8_t	tga_ramdac_rd __P((void *, u_int));
    106 static void		tga_bt463_wr __P((void *, u_int, u_int8_t));
    107 static u_int8_t	tga_bt463_rd __P((void *, u_int));
    108 static void		tga2_ramdac_wr __P((void *, u_int, u_int8_t));
    109 static u_int8_t	tga2_ramdac_rd __P((void *, u_int));
    110 
    111 /* Interrupt handler */
    112 static int	tga_intr __P((void *));
    113 
    114 /* The NULL entries will get filled in by rasops_init().
    115  * XXX and the non-NULL ones will be overwritten; reset after calling it.
    116  */
    117 struct wsdisplay_emulops tga_emulops = {
    118 	NULL,
    119 	NULL,
    120 	tga_putchar,
    121 	tga_copycols,
    122 	tga_erasecols,
    123 	tga_copyrows,
    124 	tga_eraserows,
    125 	NULL,
    126 };
    127 
    128 struct wsscreen_descr tga_stdscreen = {
    129 	"std",
    130 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
    131 	&tga_emulops,
    132 	0, 0,
    133 	WSSCREEN_REVERSE
    134 };
    135 
    136 const struct wsscreen_descr *_tga_scrlist[] = {
    137 	&tga_stdscreen,
    138 	/* XXX other formats, graphics screen? */
    139 };
    140 
    141 struct wsscreen_list tga_screenlist = {
    142 	sizeof(_tga_scrlist) / sizeof(struct wsscreen_descr *), _tga_scrlist
    143 };
    144 
    145 struct wsdisplay_accessops tga_accessops = {
    146 	tga_ioctl,
    147 	tga_mmap,
    148 	tga_alloc_screen,
    149 	tga_free_screen,
    150 	tga_show_screen,
    151 	0 /* load_font */
    152 };
    153 
    154 static void	tga_blank __P((struct tga_devconfig *));
    155 static void	tga_unblank __P((struct tga_devconfig *));
    156 
    157 int
    158 tgamatch(parent, match, aux)
    159 	struct device *parent;
    160 	struct cfdata *match;
    161 	void *aux;
    162 {
    163 	struct pci_attach_args *pa = aux;
    164 
    165 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC)
    166 		return (0);
    167 
    168 	switch (PCI_PRODUCT(pa->pa_id)) {
    169 	case PCI_PRODUCT_DEC_21030:
    170 	case PCI_PRODUCT_DEC_PBXGB:
    171 		return 10;
    172 	default:
    173 		return 0;
    174 	}
    175 	return (0);
    176 }
    177 
    178 static void
    179 tga_getdevconfig(memt, pc, tag, dc)
    180 	bus_space_tag_t memt;
    181 	pci_chipset_tag_t pc;
    182 	pcitag_t tag;
    183 	struct tga_devconfig *dc;
    184 {
    185 	const struct tga_conf *tgac;
    186 	struct rasops_info *rip;
    187 	int cookie;
    188 	bus_size_t pcisize;
    189 	int i, flags;
    190 
    191 	dc->dc_memt = memt;
    192 
    193 	dc->dc_pcitag = tag;
    194 
    195 	/* XXX magic number */
    196 	if (pci_mapreg_info(pc, tag, 0x10,
    197 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    198 	    &dc->dc_pcipaddr, &pcisize, &flags))
    199 		return;
    200 	if ((flags & BUS_SPACE_MAP_PREFETCHABLE) == 0)		/* XXX */
    201 		panic("tga memory not prefetchable");
    202 
    203 	if (bus_space_map(memt, dc->dc_pcipaddr, pcisize,
    204 	    BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR, &dc->dc_memh))
    205 		return;
    206 	dc->dc_vaddr = (vaddr_t) bus_space_vaddr(memt, dc->dc_memh);
    207 #ifdef __alpha__
    208 	dc->dc_paddr = ALPHA_K0SEG_TO_PHYS(dc->dc_vaddr);	/* XXX */
    209 #endif
    210 #ifdef arc
    211 	bus_space_paddr(memt, dc->dc_memh, &dc->dc_paddr);
    212 #endif
    213 
    214 	bus_space_subregion(dc->dc_memt, dc->dc_memh,
    215 						TGA_MEM_CREGS, TGA_CREGS_SIZE,
    216 						&dc->dc_regs);
    217 	dc->dc_tga_type = tga_identify(dc);
    218 
    219 	tgac = dc->dc_tgaconf = tga_getconf(dc->dc_tga_type);
    220 	if (tgac == NULL)
    221 		return;
    222 
    223 #if 0
    224 	/* XXX on the Alpha, pcisize = 4 * cspace_size. */
    225 	if (tgac->tgac_cspace_size != pcisize)			/* sanity */
    226 		panic("tga_getdevconfig: memory size mismatch?");
    227 #endif
    228 
    229 	switch (TGARREG(dc, TGA_REG_GREV) & 0xff) {
    230 	case 0x01:
    231 	case 0x02:
    232 	case 0x03:
    233 	case 0x04:
    234 		dc->dc_tga2 = 0;
    235 		break;
    236 	case 0x20:
    237 	case 0x21:
    238 	case 0x22:
    239 		dc->dc_tga2 = 1;
    240 		break;
    241 	default:
    242 		panic("tga_getdevconfig: TGA Revision not recognized");
    243 	}
    244 
    245 	if (dc->dc_tga2) {
    246 		int	monitor;
    247 
    248 		monitor = (~TGARREG(dc, TGA_REG_GREV) >> 16) & 0x0f;
    249 		tga2_init(dc, monitor);
    250 	}
    251 
    252 	switch (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) {		/* XXX */
    253 	case 0:
    254 		dc->dc_wid = 8192;
    255 		break;
    256 
    257 	case 1:
    258 		dc->dc_wid = 8196;
    259 		break;
    260 
    261 	default:
    262 		dc->dc_wid = (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) * 4; /* XXX */
    263 		break;
    264 	}
    265 
    266 	dc->dc_rowbytes = dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
    267 	dc->dc_ht = (TGARREG(dc, TGA_REG_VVCR) & 0x7ff);	/* XXX */
    268 
    269 	/* XXX this seems to be what DEC does */
    270 	TGAWREG(dc, TGA_REG_CCBR, 0);
    271 	TGAWREG(dc, TGA_REG_VVBR, 1);
    272 	dc->dc_videobase = dc->dc_vaddr + tgac->tgac_dbuf[0] +
    273 	    1 * tgac->tgac_vvbr_units;
    274 	dc->dc_blanked = 1;
    275 	tga_unblank(dc);
    276 
    277 	/*
    278 	 * Set all bits in the pixel mask, to enable writes to all pixels.
    279 	 * It seems that the console firmware clears some of them
    280 	 * under some circumstances, which causes cute vertical stripes.
    281 	 */
    282 	TGAWREG(dc, TGA_REG_GPXR_P, 0xffffffff);
    283 
    284 	/* clear the screen */
    285 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
    286 		*(u_int32_t *)(dc->dc_videobase + i) = 0;
    287 
    288 	/* Initialize rasops descriptor */
    289 	rip = &dc->dc_rinfo;
    290 	rip->ri_flg = RI_CENTER;
    291 	rip->ri_depth = tgac->tgac_phys_depth;
    292 	rip->ri_bits = (void *)dc->dc_videobase;
    293 	rip->ri_width = dc->dc_wid;
    294 	rip->ri_height = dc->dc_ht;
    295 	rip->ri_stride = dc->dc_rowbytes;
    296 	rip->ri_hw = dc;
    297 
    298 	if (tgac->tgac_phys_depth == 32) {
    299 		rip->ri_rnum = 8;
    300 		rip->ri_gnum = 8;
    301 		rip->ri_bnum = 8;
    302 		rip->ri_rpos = 16;
    303 		rip->ri_gpos = 8;
    304 		rip->ri_bpos = 0;
    305 	}
    306 
    307 	wsfont_init();
    308 	/* prefer 8 pixel wide font */
    309 	if ((cookie = wsfont_find(NULL, 8, 0, 0)) <= 0)
    310 		cookie = wsfont_find(NULL, 0, 0, 0);
    311 	if (cookie <= 0) {
    312 		printf("tga: no appropriate fonts.\n");
    313 		return;
    314 	}
    315 
    316 	/* the accelerated tga_putchar() needs LSbit left */
    317 	if (wsfont_lock(cookie, &dc->dc_rinfo.ri_font,
    318 	    WSDISPLAY_FONTORDER_R2L, WSDISPLAY_FONTORDER_L2R) <= 0) {
    319 		printf("tga: couldn't lock font\n");
    320 		return;
    321 	}
    322 	dc->dc_rinfo.ri_wsfcookie = cookie;
    323 
    324 	rasops_init(rip, 34, 80);
    325 
    326 	/* add our accelerated functions */
    327 	/* XXX shouldn't have to do this; rasops should leave non-NULL
    328 	 * XXX entries alone.
    329 	 */
    330 	dc->dc_rinfo.ri_ops.copyrows = tga_copyrows;
    331 	dc->dc_rinfo.ri_ops.eraserows = tga_eraserows;
    332 	dc->dc_rinfo.ri_ops.erasecols = tga_erasecols;
    333 	dc->dc_rinfo.ri_ops.copycols = tga_copycols;
    334 	dc->dc_rinfo.ri_ops.putchar = tga_putchar;
    335 
    336 	tga_stdscreen.nrows = dc->dc_rinfo.ri_rows;
    337 	tga_stdscreen.ncols = dc->dc_rinfo.ri_cols;
    338 	tga_stdscreen.textops = &dc->dc_rinfo.ri_ops;
    339 	tga_stdscreen.capabilities = dc->dc_rinfo.ri_caps;
    340 
    341 
    342 	dc->dc_intrenabled = 0;
    343 }
    344 
    345 void
    346 tgaattach(parent, self, aux)
    347 	struct device *parent, *self;
    348 	void *aux;
    349 {
    350 	struct pci_attach_args *pa = aux;
    351 	struct tga_softc *sc = (struct tga_softc *)self;
    352 	struct wsemuldisplaydev_attach_args aa;
    353 	pci_intr_handle_t intrh;
    354 	const char *intrstr;
    355 	u_int8_t rev;
    356 	int console;
    357 
    358 #if defined(__alpha__) || defined(arc)
    359 	console = (pa->pa_tag == tga_console_dc.dc_pcitag);
    360 #else
    361 	console = 0;
    362 #endif
    363 	if (console) {
    364 		sc->sc_dc = &tga_console_dc;
    365 		sc->nscreens = 1;
    366 	} else {
    367 		sc->sc_dc = (struct tga_devconfig *)
    368 		    malloc(sizeof(struct tga_devconfig), M_DEVBUF, M_WAITOK);
    369 		bzero(sc->sc_dc, sizeof(struct tga_devconfig));
    370 		tga_getdevconfig(pa->pa_memt, pa->pa_pc, pa->pa_tag,
    371 		    sc->sc_dc);
    372 	}
    373 	if (sc->sc_dc->dc_vaddr == NULL) {
    374 		printf(": couldn't map memory space; punt!\n");
    375 		return;
    376 	}
    377 
    378 	/* XXX say what's going on. */
    379 	intrstr = NULL;
    380 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    381 	    pa->pa_intrline, &intrh)) {
    382 		printf(": couldn't map interrupt");
    383 		return;
    384 	}
    385 	intrstr = pci_intr_string(pa->pa_pc, intrh);
    386 	sc->sc_intr = pci_intr_establish(pa->pa_pc, intrh, IPL_TTY, tga_intr,
    387 	    sc->sc_dc);
    388 	if (sc->sc_intr == NULL) {
    389 		printf(": couldn't establish interrupt");
    390 		if (intrstr != NULL)
    391 			printf("at %s", intrstr);
    392 		printf("\n");
    393 		return;
    394 	}
    395 
    396 	rev = PCI_REVISION(pa->pa_class);
    397 	switch (rev) {
    398 	case 0x1:
    399 	case 0x2:
    400 	case 0x3:
    401 		printf(": DC21030 step %c", 'A' + rev - 1);
    402 		break;
    403 	case 0x20:
    404 		printf(": TGA2 abstract software model");
    405 		break;
    406 	case 0x21:
    407 	case 0x22:
    408 		printf(": TGA2 pass %d", rev - 0x20);
    409 		break;
    410 
    411 	default:
    412 		printf("unknown stepping (0x%x)", rev);
    413 		break;
    414 	}
    415 	printf(", ");
    416 
    417 	/*
    418 	 * Get RAMDAC function vectors and call the RAMDAC functions
    419 	 * to allocate its private storage and pass that back to us.
    420 	 */
    421 
    422 	sc->sc_dc->dc_ramdac_funcs = sc->sc_dc->dc_tgaconf->ramdac_funcs();
    423 	if (!sc->sc_dc->dc_tga2) {
    424 	    if (sc->sc_dc->dc_tgaconf->ramdac_funcs == bt485_funcs)
    425 		  sc->sc_dc->dc_ramdac_cookie =
    426 			sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
    427 		    tga_sched_update, tga_ramdac_wr, tga_ramdac_rd);
    428 		else
    429 		  sc->sc_dc->dc_ramdac_cookie =
    430 			sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
    431 		    tga_sched_update, tga_bt463_wr, tga_bt463_rd);
    432 	} else {
    433 		sc->sc_dc->dc_ramdac_cookie =
    434 			sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
    435 			tga_sched_update, tga2_ramdac_wr, tga2_ramdac_rd);
    436 	}
    437 
    438 	/*
    439 	 * Initialize the RAMDAC.  Initialization includes disabling
    440 	 * cursor, setting a sane colormap, etc.
    441 	 */
    442 	(*sc->sc_dc->dc_ramdac_funcs->ramdac_init)(sc->sc_dc->dc_ramdac_cookie);
    443 	TGAWREG(sc->sc_dc, TGA_REG_SISR, 0x00000001); /* XXX */
    444 
    445 	if (sc->sc_dc->dc_tgaconf == NULL) {
    446 		printf("unknown board configuration\n");
    447 		return;
    448 	}
    449 	printf("board type %s\n", sc->sc_dc->dc_tgaconf->tgac_name);
    450 	printf("%s: %d x %d, %dbpp, %s RAMDAC\n", sc->sc_dev.dv_xname,
    451 	    sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
    452 	    sc->sc_dc->dc_tgaconf->tgac_phys_depth,
    453 	    sc->sc_dc->dc_ramdac_funcs->ramdac_name);
    454 
    455 	if (intrstr != NULL)
    456 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    457 		    intrstr);
    458 
    459 	aa.console = console;
    460 	aa.scrdata = &tga_screenlist;
    461 	aa.accessops = &tga_accessops;
    462 	aa.accesscookie = sc;
    463 
    464 	config_found(self, &aa, wsemuldisplaydevprint);
    465 
    466 	config_interrupts(self, tga_config_interrupts);
    467 }
    468 
    469 static void
    470 tga_config_interrupts (d)
    471 	struct device *d;
    472 {
    473 	struct tga_softc *sc = (struct tga_softc *)d;
    474 	sc->sc_dc->dc_intrenabled = 1;
    475 }
    476 
    477 
    478 int
    479 tga_ioctl(v, cmd, data, flag, p)
    480 	void *v;
    481 	u_long cmd;
    482 	caddr_t data;
    483 	int flag;
    484 	struct proc *p;
    485 {
    486 	struct tga_softc *sc = v;
    487 	struct tga_devconfig *dc = sc->sc_dc;
    488 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    489 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    490 
    491 	switch (cmd) {
    492 	case WSDISPLAYIO_GTYPE:
    493 		*(u_int *)data = WSDISPLAY_TYPE_TGA;
    494 		return (0);
    495 
    496 	case WSDISPLAYIO_GINFO:
    497 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    498 		wsd_fbip->height = sc->sc_dc->dc_ht;
    499 		wsd_fbip->width = sc->sc_dc->dc_wid;
    500 		wsd_fbip->depth = sc->sc_dc->dc_tgaconf->tgac_phys_depth;
    501 		wsd_fbip->cmsize = 256;		/* XXX ??? */
    502 #undef wsd_fbip
    503 		return (0);
    504 
    505 	case WSDISPLAYIO_GETCMAP:
    506 		return (*dcrf->ramdac_get_cmap)(dcrc,
    507 		    (struct wsdisplay_cmap *)data);
    508 
    509 	case WSDISPLAYIO_PUTCMAP:
    510 		return (*dcrf->ramdac_set_cmap)(dcrc,
    511 		    (struct wsdisplay_cmap *)data);
    512 
    513 	case WSDISPLAYIO_SVIDEO:
    514 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
    515 			tga_blank(sc->sc_dc);
    516 		else
    517 			tga_unblank(sc->sc_dc);
    518 		return (0);
    519 
    520 	case WSDISPLAYIO_GVIDEO:
    521 		*(u_int *)data = dc->dc_blanked ?
    522 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    523 		return (0);
    524 
    525 	case WSDISPLAYIO_GCURPOS:
    526 		return (*dcrf->ramdac_get_curpos)(dcrc,
    527 		    (struct wsdisplay_curpos *)data);
    528 
    529 	case WSDISPLAYIO_SCURPOS:
    530 		return (*dcrf->ramdac_set_curpos)(dcrc,
    531 		    (struct wsdisplay_curpos *)data);
    532 
    533 	case WSDISPLAYIO_GCURMAX:
    534 		return (*dcrf->ramdac_get_curmax)(dcrc,
    535 		    (struct wsdisplay_curpos *)data);
    536 
    537 	case WSDISPLAYIO_GCURSOR:
    538 		return (*dcrf->ramdac_get_cursor)(dcrc,
    539 		    (struct wsdisplay_cursor *)data);
    540 
    541 	case WSDISPLAYIO_SCURSOR:
    542 		return (*dcrf->ramdac_set_cursor)(dcrc,
    543 		    (struct wsdisplay_cursor *)data);
    544 	}
    545 	return (-1);
    546 }
    547 
    548 static int
    549 tga_sched_update(v, f)
    550 	void	*v;
    551 	void	(*f) __P((void *));
    552 {
    553 	struct tga_devconfig *dc = v;
    554 
    555 	if (dc->dc_intrenabled) {
    556 		/* Arrange for f to be called at the next end-of-frame interrupt */
    557 		dc->dc_ramdac_intr = f;
    558 		TGAWREG(dc, TGA_REG_SISR, 0x00010000);
    559 	} else {
    560 		/* Spin until the end-of-frame, then call f */
    561 		TGAWREG(dc, TGA_REG_SISR, 0x00010001);
    562 		TGAREGWB(dc, TGA_REG_SISR, 1);
    563 		while ((TGARREG(dc, TGA_REG_SISR) & 0x00000001) == 0)
    564 			;
    565 		f(dc->dc_ramdac_cookie);
    566 		TGAWREG(dc, TGA_REG_SISR, 0x00000001);
    567 		TGAREGWB(dc, TGA_REG_SISR, 1);
    568 	}
    569 
    570 	return 0;
    571 }
    572 
    573 static int
    574 tga_intr(v)
    575 	void *v;
    576 {
    577 	struct tga_devconfig *dc = v;
    578 	struct ramdac_cookie *dcrc= dc->dc_ramdac_cookie;
    579 
    580 	u_int32_t reg;
    581 
    582 	reg = TGARREG(dc, TGA_REG_SISR);
    583 	if (( reg & 0x00010001) != 0x00010001) {
    584 		/* Odd. We never set any of the other interrupt enables. */
    585 		if ((reg & 0x1f) != 0) {
    586 			/* Clear the mysterious pending interrupts. */
    587 			TGAWREG(dc, TGA_REG_SISR, (reg & 0x1f));
    588 			TGAREGWB(dc, TGA_REG_SISR, 1);
    589 			/* This was our interrupt, even if we're puzzled as to why
    590 			 * we got it.  Don't make the interrupt handler think it
    591 			 * was a stray.
    592 			 */
    593 			return -1;
    594 		} else {
    595 			return 0;
    596 		}
    597 	}
    598 	dc->dc_ramdac_intr(dcrc);
    599 	dc->dc_ramdac_intr = NULL;
    600 	TGAWREG(dc, TGA_REG_SISR, 0x00000001);
    601 	TGAREGWB(dc, TGA_REG_SISR, 1);
    602 	return (1);
    603 }
    604 
    605 paddr_t
    606 tga_mmap(v, offset, prot)
    607 	void *v;
    608 	off_t offset;
    609 	int prot;
    610 {
    611 
    612 	/* XXX NEW MAPPING CODE... */
    613 
    614 #if defined(__alpha__)
    615 	struct tga_softc *sc = v;
    616 
    617 	if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
    618 		return -1;
    619 	return alpha_btop(sc->sc_dc->dc_paddr + offset);
    620 #elif defined(__mips__)
    621 	struct tga_softc *sc = v;
    622 
    623 	if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
    624 		return -1;
    625 	return mips_btop(sc->sc_dc->dc_paddr + offset);
    626 #else
    627 	return (-1);
    628 #endif
    629 }
    630 
    631 static int
    632 tga_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    633 	void *v;
    634 	const struct wsscreen_descr *type;
    635 	void **cookiep;
    636 	int *curxp, *curyp;
    637 	long *attrp;
    638 {
    639 	struct tga_softc *sc = v;
    640 	long defattr;
    641 
    642 	if (sc->nscreens > 0)
    643 		return (ENOMEM);
    644 
    645 	*cookiep = &sc->sc_dc->dc_rinfo; /* one and only for now */
    646 	*curxp = 0;
    647 	*curyp = 0;
    648 	sc->sc_dc->dc_rinfo.ri_ops.alloc_attr(&sc->sc_dc->dc_rinfo,
    649 		0, 0, 0, &defattr);
    650 	*attrp = defattr;
    651 	sc->nscreens++;
    652 	return (0);
    653 }
    654 
    655 static void
    656 tga_free_screen(v, cookie)
    657 	void *v;
    658 	void *cookie;
    659 {
    660 	struct tga_softc *sc = v;
    661 
    662 	if (sc->sc_dc == &tga_console_dc)
    663 		panic("tga_free_screen: console");
    664 
    665 	sc->nscreens--;
    666 }
    667 
    668 static int
    669 tga_show_screen(v, cookie, waitok, cb, cbarg)
    670 	void *v;
    671 	void *cookie;
    672 	int waitok;
    673 	void (*cb) __P((void *, int, int));
    674 	void *cbarg;
    675 {
    676 
    677 	return (0);
    678 }
    679 
    680 int
    681 tga_cnattach(iot, memt, pc, bus, device, function)
    682 	bus_space_tag_t iot, memt;
    683 	pci_chipset_tag_t pc;
    684 	int bus, device, function;
    685 {
    686 	struct tga_devconfig *dcp = &tga_console_dc;
    687 	long defattr;
    688 
    689 	tga_getdevconfig(memt, pc,
    690 	    pci_make_tag(pc, bus, device, function), dcp);
    691 
    692 	/* sanity checks */
    693 	if (dcp->dc_vaddr == NULL)
    694 		panic("tga_console(%d, %d): couldn't map memory space",
    695 		    device, function);
    696 	if (dcp->dc_tgaconf == NULL)
    697 		panic("tga_console(%d, %d): unknown board configuration",
    698 		    device, function);
    699 
    700 	/*
    701 	 * Initialize the RAMDAC but DO NOT allocate any private storage.
    702 	 * Initialization includes disabling cursor, setting a sane
    703 	 * colormap, etc.  It will be reinitialized in tgaattach().
    704 	 */
    705 	if (dcp->dc_tga2)
    706 		bt485_cninit(dcp, tga_sched_update, tga2_ramdac_wr,
    707 		    tga2_ramdac_rd);
    708 	else {
    709 		if (dcp->dc_tgaconf->ramdac_funcs == bt485_funcs)
    710 			bt485_cninit(dcp, tga_sched_update, tga_ramdac_wr,
    711 				tga_ramdac_rd);
    712 		else {
    713 			bt463_cninit(dcp, tga_sched_update, tga_bt463_wr,
    714 				tga_bt463_rd);
    715 		}
    716 	}
    717 	dcp->dc_rinfo.ri_ops.alloc_attr(&dcp->dc_rinfo, 0, 0, 0, &defattr);
    718 	wsdisplay_cnattach(&tga_stdscreen, &dcp->dc_rinfo, 0, 0, defattr);
    719 
    720 	return(0);
    721 }
    722 
    723 /*
    724  * Functions to blank and unblank the display.
    725  */
    726 static void
    727 tga_blank(dc)
    728 	struct tga_devconfig *dc;
    729 {
    730 
    731 	if (!dc->dc_blanked) {
    732 		dc->dc_blanked = 1;
    733 		/* XXX */
    734 		TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | VVR_BLANK);
    735 	}
    736 }
    737 
    738 static void
    739 tga_unblank(dc)
    740 	struct tga_devconfig *dc;
    741 {
    742 
    743 	if (dc->dc_blanked) {
    744 		dc->dc_blanked = 0;
    745 		/* XXX */
    746 		TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~VVR_BLANK);
    747 	}
    748 }
    749 
    750 /*
    751  * Functions to manipulate the built-in cursor handing hardware.
    752  */
    753 int
    754 tga_builtin_set_cursor(dc, cursorp)
    755 	struct tga_devconfig *dc;
    756 	struct wsdisplay_cursor *cursorp;
    757 {
    758 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    759 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    760 	int count, error, v;
    761 
    762 	v = cursorp->which;
    763 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    764 		error = dcrf->ramdac_check_curcmap(dcrc, cursorp);
    765 		if (error)
    766 			return (error);
    767 	}
    768 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    769 		if ((u_int)cursorp->size.x != 64 ||
    770 		    (u_int)cursorp->size.y > 64)
    771 			return (EINVAL);
    772 		/* The cursor is 2 bits deep, and there is no mask */
    773 		count = (cursorp->size.y * 64 * 2) / NBBY;
    774 		if (!uvm_useracc(cursorp->image, count, B_READ))
    775 			return (EFAULT);
    776 	}
    777 	if (v & WSDISPLAY_CURSOR_DOHOT)		/* not supported */
    778 		return EINVAL;
    779 
    780 	/* parameters are OK; do it */
    781 	if (v & WSDISPLAY_CURSOR_DOCUR) {
    782 		if (cursorp->enable)
    783 			/* XXX */
    784 			TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 0x04);
    785 		else
    786 			/* XXX */
    787 			TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~0x04);
    788 	}
    789 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    790 		TGAWREG(dc, TGA_REG_CXYR,
    791 				((cursorp->pos.y & 0xfff) << 12) | (cursorp->pos.x & 0xfff));
    792 	}
    793 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    794 		/* can't fail. */
    795 		dcrf->ramdac_set_curcmap(dcrc, cursorp);
    796 	}
    797 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    798 		count = ((64 * 2) / NBBY) * cursorp->size.y;
    799 		TGAWREG(dc, TGA_REG_CCBR,
    800 		    (TGARREG(dc, TGA_REG_CCBR) & ~0xfc00) | (cursorp->size.y << 10));
    801 		copyin(cursorp->image, (char *)(dc->dc_vaddr +
    802 		    (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
    803 		    count);				/* can't fail. */
    804 	}
    805 	return (0);
    806 }
    807 
    808 int
    809 tga_builtin_get_cursor(dc, cursorp)
    810 	struct tga_devconfig *dc;
    811 	struct wsdisplay_cursor *cursorp;
    812 {
    813 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    814 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    815 	int count, error;
    816 
    817 	cursorp->which = WSDISPLAY_CURSOR_DOALL &
    818 	    ~(WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOCMAP);
    819 	cursorp->enable = (TGARREG(dc, TGA_REG_VVVR) & 0x04) != 0;
    820 	cursorp->pos.x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
    821 	cursorp->pos.y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
    822 	cursorp->size.x = 64;
    823 	cursorp->size.y = (TGARREG(dc, TGA_REG_CCBR) >> 10) & 0x3f;
    824 
    825 	if (cursorp->image != NULL) {
    826 		count = (cursorp->size.y * 64 * 2) / NBBY;
    827 		error = copyout((char *)(dc->dc_vaddr +
    828 		      (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
    829 		    cursorp->image, count);
    830 		if (error)
    831 			return (error);
    832 		/* No mask */
    833 	}
    834 	error = dcrf->ramdac_get_curcmap(dcrc, cursorp);
    835 	return (error);
    836 }
    837 
    838 int
    839 tga_builtin_set_curpos(dc, curposp)
    840 	struct tga_devconfig *dc;
    841 	struct wsdisplay_curpos *curposp;
    842 {
    843 
    844 	TGAWREG(dc, TGA_REG_CXYR,
    845 	    ((curposp->y & 0xfff) << 12) | (curposp->x & 0xfff));
    846 	return (0);
    847 }
    848 
    849 int
    850 tga_builtin_get_curpos(dc, curposp)
    851 	struct tga_devconfig *dc;
    852 	struct wsdisplay_curpos *curposp;
    853 {
    854 
    855 	curposp->x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
    856 	curposp->y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
    857 	return (0);
    858 }
    859 
    860 int
    861 tga_builtin_get_curmax(dc, curposp)
    862 	struct tga_devconfig *dc;
    863 	struct wsdisplay_curpos *curposp;
    864 {
    865 
    866 	curposp->x = curposp->y = 64;
    867 	return (0);
    868 }
    869 
    870 /*
    871  * Copy columns (characters) in a row (line).
    872  */
    873 static void
    874 tga_copycols(id, row, srccol, dstcol, ncols)
    875 	void *id;
    876 	int row, srccol, dstcol, ncols;
    877 {
    878 	struct rasops_info *ri = id;
    879 	int y, srcx, dstx, nx;
    880 
    881 	y = ri->ri_font->fontheight * row;
    882 	srcx = ri->ri_font->fontwidth * srccol;
    883 	dstx = ri->ri_font->fontwidth * dstcol;
    884 	nx = ri->ri_font->fontwidth * ncols;
    885 
    886 	tga_rop(ri, dstx, y,
    887 	    nx, ri->ri_font->fontheight, RAS_SRC,
    888 	    ri, srcx, y);
    889 }
    890 
    891 /*
    892  * Copy rows (lines).
    893  */
    894 static void
    895 tga_copyrows(id, srcrow, dstrow, nrows)
    896 	void *id;
    897 	int srcrow, dstrow, nrows;
    898 {
    899 	struct rasops_info *ri = id;
    900 	int srcy, dsty, ny;
    901 
    902 	srcy = ri->ri_font->fontheight * srcrow;
    903 	dsty = ri->ri_font->fontheight * dstrow;
    904 	ny = ri->ri_font->fontheight * nrows;
    905 
    906 	tga_rop(ri, 0, dsty,
    907 	    ri->ri_emuwidth, ny, RAS_SRC,
    908 	    ri, 0, srcy);
    909 }
    910 
    911 /* Do we need the src? */
    912 static int needsrc[16] = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 };
    913 
    914 /* A mapping between our API and the TGA card */
    915 static int map_rop[16] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6,
    916 	0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
    917 };
    918 
    919 /*
    920  *  Generic TGA raster op.
    921  *   This covers all possible raster ops, and
    922  *   clips the sizes and all of that.
    923  */
    924 static int
    925 tga_rop(dst, dx, dy, w, h, rop, src, sx, sy)
    926 	struct rasops_info *dst;
    927 	int dx, dy, w, h, rop;
    928 	struct rasops_info *src;
    929 	int sx, sy;
    930 {
    931 	if (!dst)
    932 		return -1;
    933 	if (needsrc[RAS_GETOP(rop)]) {
    934 		if (src == NULL)
    935 			return -1;	/* We want a src */
    936 		/* Clip against src */
    937 		if (sx < 0) {
    938 			w += sx;
    939 			sx = 0;
    940 		}
    941 		if (sy < 0) {
    942 			h += sy;
    943 			sy = 0;
    944 		}
    945 		if (sx + w > src->ri_emuwidth)
    946 			w = src->ri_emuwidth - sx;
    947 		if (sy + h > src->ri_emuheight)
    948 			h = src->ri_emuheight - sy;
    949 	} else {
    950 		if (src != NULL)
    951 			return -1;	/* We need no src */
    952 	}
    953 	/* Clip against dst.  We modify src regardless of using it,
    954 	 * since it really doesn't matter.
    955 	 */
    956 	if (dx < 0) {
    957 		w += dx;
    958 		sx -= dx;
    959 		dx = 0;
    960 	}
    961 	if (dy < 0) {
    962 		h += dy;
    963 		sy -= dy;
    964 		dy = 0;
    965 	}
    966 	if (dx + w > dst->ri_emuwidth)
    967 		w = dst->ri_emuwidth - dx;
    968 	if (dy + h > dst->ri_emuheight)
    969 		h = dst->ri_emuheight - dy;
    970 	if (w <= 0 || h <= 0)
    971 		return 0;	/* Vacuously true; */
    972 	if (!src) {
    973 		/* XXX Punt! */
    974 		return -1;
    975 	}
    976 	return tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy);
    977 }
    978 
    979 
    980 
    981 /*
    982  * Video to Video raster ops.
    983  * This function deals with all raster ops that have a src and dst
    984  * that are on the card.
    985  */
    986 static int
    987 tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy)
    988 	struct rasops_info *dst;
    989 	int dx, dy, w, h, rop;
    990 	struct rasops_info *src;
    991 	int sx, sy;
    992 {
    993 	struct tga_devconfig *dc = (struct tga_devconfig *)dst->ri_hw;
    994 	int srcb, dstb;
    995 	int x, y;
    996 	int xstart, xend, xdir, xinc;
    997 	int ystart, yend, ydir, yinc;
    998 	int offset = 1 * dc->dc_tgaconf->tgac_vvbr_units;
    999 
   1000 	/*
   1001 	 * I don't yet want to deal with unaligned guys, really.  And we don't
   1002 	 * deal with copies from one card to another.
   1003 	 */
   1004 	if (dx % 8 != 0 || sx % 8 != 0 || src != dst) {
   1005 		/* XXX Punt! */
   1006 		/* XXX should never happen, since it's only being used to
   1007 		 * XXX copy 8-pixel-wide characters.
   1008 		 */
   1009 		return -1;
   1010 	}
   1011 
   1012 	if (sy >= dy) {
   1013 		ystart = 0;
   1014 		yend = h;
   1015 		ydir = 1;
   1016 	} else {
   1017 		ystart = h;
   1018 		yend = 0;
   1019 		ydir = -1;
   1020 	}
   1021 	if (sx >= dx) {
   1022 		xstart = 0;
   1023 		xend = w * (dst->ri_depth / 8);
   1024 		xdir = 1;
   1025 	} else {
   1026 		xstart = w * (dst->ri_depth / 8);
   1027 		xend = 0;
   1028 		xdir = -1;
   1029 	}
   1030 	xinc = xdir * 4 * 64;
   1031 	yinc = ydir * dst->ri_stride;
   1032 	ystart *= dst->ri_stride;
   1033 	yend *= dst->ri_stride;
   1034 	srcb = offset + (sy + src->ri_yorigin) * src->ri_stride +
   1035 		            (sx + src->ri_xorigin) * (src->ri_depth/8);
   1036 	dstb = offset + (dy + dst->ri_yorigin) * dst->ri_stride +
   1037 		            (dx + dst->ri_xorigin ) * (dst->ri_depth/8);
   1038 	TGAWALREG(dc, TGA_REG_GMOR, 3, 0x0007); /* Copy mode */
   1039 	TGAWALREG(dc, TGA_REG_GOPR, 3, map_rop[rop]);	/* Set up the op */
   1040 	for (y = ystart; (ydir * y) < (ydir * yend); y += yinc) {
   1041 		for (x = xstart; (xdir * x) < (xdir * xend); x += xinc) {
   1042 		  /* XXX XXX Eight writes to different addresses should fill
   1043 		   * XXX XXX up the write buffers on 21064 and 21164 chips,
   1044 		   * XXX XXX but later CPUs might have larger write buffers which
   1045 		   * XXX XXX require further unrolling of this loop, or the
   1046 		   * XXX XXX insertion of memory barriers.
   1047 		   */
   1048 			TGAWALREG(dc, TGA_REG_GCSR, 0, srcb + y + x + 3 * 64);
   1049 			TGAWALREG(dc, TGA_REG_GCDR, 0, dstb + y + x + 3 * 64);
   1050 			TGAWALREG(dc, TGA_REG_GCSR, 1, srcb + y + x + 2 * 64);
   1051 			TGAWALREG(dc, TGA_REG_GCDR, 1, dstb + y + x + 2 * 64);
   1052 			TGAWALREG(dc, TGA_REG_GCSR, 2, srcb + y + x + 1 * 64);
   1053 			TGAWALREG(dc, TGA_REG_GCDR, 2, dstb + y + x + 1 * 64);
   1054 			TGAWALREG(dc, TGA_REG_GCSR, 3, srcb + y + x + 0 * 64);
   1055 			TGAWALREG(dc, TGA_REG_GCDR, 3, dstb + y + x + 0 * 64);
   1056 		}
   1057 	}
   1058 	TGAWALREG(dc, TGA_REG_GOPR, 0, 0x0003); /* op -> dst = src */
   1059 	TGAWALREG(dc, TGA_REG_GMOR, 0, 0x0000); /* Simple mode */
   1060 	return 0;
   1061 }
   1062 
   1063 
   1064 void tga_putchar (c, row, col, uc, attr)
   1065 	void *c;
   1066 	int row, col;
   1067 	u_int uc;
   1068 	long attr;
   1069 {
   1070 	struct rasops_info *ri = c;
   1071 	struct tga_devconfig *dc = ri->ri_hw;
   1072 	int fs, height, width;
   1073 	u_char *fr;
   1074 	int32_t *rp;
   1075 
   1076 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
   1077 
   1078 	height = ri->ri_font->fontheight;
   1079 	width = ri->ri_font->fontwidth;
   1080 
   1081 	uc -= ri->ri_font->firstchar;
   1082 	fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
   1083 	fs = ri->ri_font->stride;
   1084 
   1085 	/* Set foreground and background color. XXX memoize this somehow?
   1086 	 * The rasops code has already expanded the color entry to 32 bits
   1087 	 * for us, even for 8-bit displays, so we don't have to do anything.
   1088 	 */
   1089 	TGAWREG(dc, TGA_REG_GFGR, ri->ri_devcmap[(attr >> 24) & 15]);
   1090 	TGAWREG(dc, TGA_REG_GBGR, ri->ri_devcmap[(attr >> 16) & 15]);
   1091 
   1092 	/* Set raster operation to "copy"... */
   1093 	if (ri->ri_depth == 8)
   1094 		TGAWREG(dc, TGA_REG_GOPR, 0x3);
   1095 	else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
   1096 		TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
   1097 
   1098 	/* Set which pixels we're drawing (of a possible 32). */
   1099 	TGAWREG(dc, TGA_REG_GPXR_P, (1 << width) - 1);
   1100 
   1101 	/* Set drawing mode to opaque stipple. */
   1102 	TGAWREG(dc, TGA_REG_GMOR, 0x1);
   1103 
   1104 	/* Insert write barrier before actually sending data */
   1105 	/* XXX Abuses the fact that there is only one write barrier on Alphas */
   1106 	TGAREGWB(dc, TGA_REG_GMOR, 1);
   1107 
   1108 	while(height--) {
   1109 		/* The actual stipple write */
   1110 		*rp = fr[0] | (fr[1] << 8) | (fr[2] << 16) | (fr[3] << 24);
   1111 
   1112 		fr += fs;
   1113 		rp = (int32_t *)((caddr_t)rp + ri->ri_stride);
   1114 	}
   1115 
   1116 	/* Do underline */
   1117 	if ((attr & 1) != 0) {
   1118 		rp = (int32_t *)((caddr_t)rp - (ri->ri_stride << 1));
   1119 		*rp = 0xffffffff;
   1120 	}
   1121 
   1122 	/* Set grapics mode back to normal. */
   1123 	TGAWREG(dc, TGA_REG_GMOR, 0);
   1124 	TGAWREG(dc, TGA_REG_GPXR_P, 0xffffffff);
   1125 
   1126 }
   1127 
   1128 static void
   1129 tga_eraserows(c, row, num, attr)
   1130 	void *c;
   1131 	int row, num;
   1132 	long attr;
   1133 {
   1134 	struct rasops_info *ri = c;
   1135 	struct tga_devconfig *dc = ri->ri_hw;
   1136 	int32_t color, lines, pixels;
   1137 	int32_t *rp;
   1138 
   1139 	color = ri->ri_devcmap[(attr >> 16) & 15];
   1140 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale);
   1141 	lines = num * ri->ri_font->fontheight;
   1142 	pixels = ri->ri_emuwidth - 1;
   1143 
   1144 	/* Set fill color in block-color registers */
   1145 	TGAWREG(dc, TGA_REG_GBCR0, color);
   1146 	TGAWREG(dc, TGA_REG_GBCR1, color);
   1147 	if (ri->ri_depth != 8) {
   1148 		TGAWREG(dc, TGA_REG_GBCR2, color);
   1149 		TGAWREG(dc, TGA_REG_GBCR3, color);
   1150 		TGAWREG(dc, TGA_REG_GBCR4, color);
   1151 		TGAWREG(dc, TGA_REG_GBCR5, color);
   1152 		TGAWREG(dc, TGA_REG_GBCR6, color);
   1153 		TGAWREG(dc, TGA_REG_GBCR7, color);
   1154 	}
   1155 
   1156 	/* Set raster operation to "copy"... */
   1157 	if (ri->ri_depth == 8)
   1158 		TGAWREG(dc, TGA_REG_GOPR, 0x3);
   1159 	else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
   1160 		TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
   1161 
   1162 	/* Set which pixels we're drawing (of a possible 32). */
   1163 	TGAWREG(dc, TGA_REG_GDAR, 0xffffffff);
   1164 
   1165 	/* Set drawing mode to block fill. */
   1166 	TGAWREG(dc, TGA_REG_GMOR, 0x2d);
   1167 
   1168 	/* Insert write barrier before actually sending data */
   1169 	/* XXX Abuses the fact that there is only one write barrier on Alphas */
   1170 	TGAREGWB(dc, TGA_REG_GMOR, 1);
   1171 
   1172 	while (lines--) {
   1173 		*rp = pixels;
   1174 		rp = (int32_t *)((caddr_t)rp + ri->ri_stride);
   1175 	}
   1176 
   1177 	/* Set grapics mode back to normal. */
   1178 	TGAWREG(dc, TGA_REG_GMOR, 0);
   1179 
   1180 }
   1181 
   1182 static void
   1183 tga_erasecols (c, row, col, num, attr)
   1184 void *c;
   1185 int row, col, num;
   1186 long attr;
   1187 {
   1188 	struct rasops_info *ri = c;
   1189 	struct tga_devconfig *dc = ri->ri_hw;
   1190 	int32_t color, lines, pixels;
   1191 	int32_t *rp;
   1192 
   1193 	color = ri->ri_devcmap[(attr >> 16) & 15];
   1194 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
   1195 	lines = ri->ri_font->fontheight;
   1196 	pixels = (num * ri->ri_font->fontwidth) - 1;
   1197 
   1198 	/* Set fill color in block-color registers */
   1199 	TGAWREG(dc, TGA_REG_GBCR0, color);
   1200 	TGAWREG(dc, TGA_REG_GBCR1, color);
   1201 	if (ri->ri_depth != 8) {
   1202 		TGAWREG(dc, TGA_REG_GBCR2, color);
   1203 		TGAWREG(dc, TGA_REG_GBCR3, color);
   1204 		TGAWREG(dc, TGA_REG_GBCR4, color);
   1205 		TGAWREG(dc, TGA_REG_GBCR5, color);
   1206 		TGAWREG(dc, TGA_REG_GBCR6, color);
   1207 		TGAWREG(dc, TGA_REG_GBCR7, color);
   1208 	}
   1209 
   1210 	/* Set raster operation to "copy"... */
   1211 	if (ri->ri_depth == 8)
   1212 		TGAWREG(dc, TGA_REG_GOPR, 0x3);
   1213 	else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
   1214 		TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
   1215 
   1216 	/* Set which pixels we're drawing (of a possible 32). */
   1217 	TGAWREG(dc, TGA_REG_GDAR, 0xffffffff);
   1218 
   1219 	/* Set drawing mode to block fill. */
   1220 	TGAWREG(dc, TGA_REG_GMOR, 0x2d);
   1221 
   1222 	/* Insert write barrier before actually sending data */
   1223 	/* XXX Abuses the fact that there is only one write barrier on Alphas */
   1224 	TGAREGWB(dc, TGA_REG_GMOR, 1);
   1225 
   1226 	while (lines--) {
   1227 		*rp = pixels;
   1228 		rp = (int32_t *)((caddr_t)rp + ri->ri_stride);
   1229 	}
   1230 
   1231 	/* Set grapics mode back to normal. */
   1232 	TGAWREG(dc, TGA_REG_GMOR, 0);
   1233 }
   1234 
   1235 
   1236 static void
   1237 tga_ramdac_wr(v, btreg, val)
   1238 	void *v;
   1239 	u_int btreg;
   1240 	u_int8_t val;
   1241 {
   1242 	struct tga_devconfig *dc = v;
   1243 
   1244 	if (btreg > BT485_REG_MAX)
   1245 		panic("tga_ramdac_wr: reg %d out of range\n", btreg);
   1246 
   1247 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 9) | (0 << 8 ) | val); /* XXX */
   1248 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1249 }
   1250 
   1251 static void
   1252 tga2_ramdac_wr(v, btreg, val)
   1253 	void *v;
   1254 	u_int btreg;
   1255 	u_int8_t val;
   1256 {
   1257 	struct tga_devconfig *dc = v;
   1258 	bus_space_handle_t ramdac;
   1259 
   1260 	if (btreg > BT485_REG_MAX)
   1261 		panic("tga_ramdac_wr: reg %d out of range\n", btreg);
   1262 
   1263 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
   1264 		(0xe << 12) + (btreg << 8), 4, &ramdac);
   1265 	bus_space_write_4(dc->dc_memt, ramdac, 0, val & 0xff);
   1266 	bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_WRITE);
   1267 }
   1268 
   1269 static u_int8_t
   1270 tga_bt463_rd(v, btreg)
   1271 	void *v;
   1272 	u_int btreg;
   1273 {
   1274 	struct tga_devconfig *dc = v;
   1275 	tga_reg_t rdval;
   1276 
   1277 	/*
   1278 	 * Strobe CE# (high->low->high) since status and data are latched on
   1279 	 * the falling and rising edges (repsectively) of this active-low signal.
   1280 	 */
   1281 
   1282 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1283 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 1);
   1284 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1285 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 0);
   1286 
   1287 	TGAREGRB(dc, TGA_REG_EPSR, 1);
   1288 
   1289 	rdval = TGARREG(dc, TGA_REG_EPDR);
   1290 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1291 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 1);
   1292 
   1293 	return (rdval >> 16) & 0xff;
   1294 }
   1295 
   1296 static void
   1297 tga_bt463_wr(v, btreg, val)
   1298 	void *v;
   1299 	u_int btreg;
   1300 	u_int8_t val;
   1301 {
   1302 	struct tga_devconfig *dc = v;
   1303 
   1304 	/*
   1305 	 * In spite of the 21030 documentation, to set the MPU bus bits for
   1306 	 * a write, you set them in the upper bits of EPDR, not EPSR.
   1307 	 */
   1308 
   1309 	/*
   1310 	 * Strobe CE# (high->low->high) since status and data are latched on
   1311 	 * the falling and rising edges of this active-low signal.
   1312 	 */
   1313 
   1314 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1315 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x100 | val);
   1316 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1317 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x000 | val);
   1318 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1319 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x100 | val);
   1320 
   1321 }
   1322 
   1323 static u_int8_t
   1324 tga_ramdac_rd(v, btreg)
   1325 	void *v;
   1326 	u_int btreg;
   1327 {
   1328 	struct tga_devconfig *dc = v;
   1329 	tga_reg_t rdval;
   1330 
   1331 	if (btreg > BT485_REG_MAX)
   1332 		panic("tga_ramdac_rd: reg %d out of range\n", btreg);
   1333 
   1334 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 1) | 0x1); /* XXX */
   1335 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1336 
   1337 	rdval = TGARREG(dc, TGA_REG_EPDR);
   1338 	return (rdval >> 16) & 0xff;				/* XXX */
   1339 }
   1340 
   1341 static u_int8_t
   1342 tga2_ramdac_rd(v, btreg)
   1343 	void *v;
   1344 	u_int btreg;
   1345 {
   1346 	struct tga_devconfig *dc = v;
   1347 	bus_space_handle_t ramdac;
   1348 	u_int8_t retval;
   1349 
   1350 	if (btreg > BT485_REG_MAX)
   1351 		panic("tga_ramdac_rd: reg %d out of range\n", btreg);
   1352 
   1353 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
   1354 		(0xe << 12) + (btreg << 8), 4, &ramdac);
   1355 	retval = bus_space_read_4(dc->dc_memt, ramdac, 0) & 0xff;
   1356 	bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_READ);
   1357 	return retval;
   1358 }
   1359 
   1360 #include <dev/ic/decmonitors.c>
   1361 void tga2_ics9110_wr __P((
   1362 	struct tga_devconfig *dc,
   1363 	int dotclock
   1364 ));
   1365 
   1366 void
   1367 tga2_init(dc, m)
   1368 	struct tga_devconfig *dc;
   1369 	int m;
   1370 {
   1371 
   1372 	tga2_ics9110_wr(dc, decmonitors[m].dotclock);
   1373 #if 0
   1374 	TGAWREG(dc, TGA_REG_VHCR,
   1375 	     ((decmonitors[m].hbp / 4) << 21) |
   1376 	     ((decmonitors[m].hsync / 4) << 14) |
   1377 	    (((decmonitors[m].hfp - 4) / 4) << 9) |
   1378 	     ((decmonitors[m].cols + 4) / 4));
   1379 #else
   1380 	TGAWREG(dc, TGA_REG_VHCR,
   1381 	     ((decmonitors[m].hbp / 4) << 21) |
   1382 	     ((decmonitors[m].hsync / 4) << 14) |
   1383 	    (((decmonitors[m].hfp) / 4) << 9) |
   1384 	     ((decmonitors[m].cols) / 4));
   1385 #endif
   1386 	TGAWREG(dc, TGA_REG_VVCR,
   1387 	    (decmonitors[m].vbp << 22) |
   1388 	    (decmonitors[m].vsync << 16) |
   1389 	    (decmonitors[m].vfp << 11) |
   1390 	    (decmonitors[m].rows));
   1391 	TGAWREG(dc, TGA_REG_VVBR, 1);
   1392 	TGAREGRWB(dc, TGA_REG_VHCR, 3);
   1393 	TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 1);
   1394 	TGAREGRWB(dc, TGA_REG_VVVR, 1);
   1395 	TGAWREG(dc, TGA_REG_GPMR, 0xffffffff);
   1396 	TGAREGRWB(dc, TGA_REG_GPMR, 1);
   1397 }
   1398 
   1399 void
   1400 tga2_ics9110_wr(dc, dotclock)
   1401 	struct tga_devconfig *dc;
   1402 	int dotclock;
   1403 {
   1404 	bus_space_handle_t clock;
   1405 	u_int32_t valU;
   1406 	int N, M, R, V, X;
   1407 	int i;
   1408 
   1409 	switch (dotclock) {
   1410 	case 130808000:
   1411 		N = 0x40; M = 0x7; V = 0x0; X = 0x1; R = 0x1; break;
   1412 	case 119840000:
   1413 		N = 0x2d; M = 0x2b; V = 0x1; X = 0x1; R = 0x1; break;
   1414 	case 108180000:
   1415 		N = 0x11; M = 0x9; V = 0x1; X = 0x1; R = 0x2; break;
   1416 	case 103994000:
   1417 		N = 0x6d; M = 0xf; V = 0x0; X = 0x1; R = 0x1; break;
   1418 	case 175000000:
   1419 		N = 0x5F; M = 0x3E; V = 0x1; X = 0x1; R = 0x1; break;
   1420 	case  75000000:
   1421 		N = 0x6e; M = 0x15; V = 0x0; X = 0x1; R = 0x1; break;
   1422 	case  74000000:
   1423 		N = 0x2a; M = 0x41; V = 0x1; X = 0x1; R = 0x1; break;
   1424 	case  69000000:
   1425 		N = 0x35; M = 0xb; V = 0x0; X = 0x1; R = 0x1; break;
   1426 	case  65000000:
   1427 		N = 0x6d; M = 0x0c; V = 0x0; X = 0x1; R = 0x2; break;
   1428 	case  50000000:
   1429 		N = 0x37; M = 0x3f; V = 0x1; X = 0x1; R = 0x2; break;
   1430 	case  40000000:
   1431 		N = 0x5f; M = 0x11; V = 0x0; X = 0x1; R = 0x2; break;
   1432 	case  31500000:
   1433 		N = 0x16; M = 0x05; V = 0x0; X = 0x1; R = 0x2; break;
   1434 	case  25175000:
   1435 		N = 0x66; M = 0x1d; V = 0x0; X = 0x1; R = 0x2; break;
   1436 	case 135000000:
   1437 		N = 0x42; M = 0x07; V = 0x0; X = 0x1; R = 0x1; break;
   1438 	case 110000000:
   1439 		N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
   1440 	case 202500000:
   1441 		N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
   1442 	default:
   1443 		panic("unrecognized clock rate %d\n", dotclock);
   1444 	}
   1445 
   1446 	/* XXX -- hard coded, bad */
   1447 	valU  = N | ( M << 7 ) | (V << 14);
   1448 	valU |= (X << 15) | (R << 17);
   1449 	valU |= 0x17 << 19;
   1450 
   1451 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
   1452 	    TGA2_MEM_CLOCK + (0xe << 12), 4, &clock); /* XXX */
   1453 
   1454 	for (i=24; i>0; i--) {
   1455 		u_int32_t       writeval;
   1456 
   1457 		writeval = valU & 0x1;
   1458 		if (i == 1)
   1459 			writeval |= 0x2;
   1460 		valU >>= 1;
   1461 		bus_space_write_4(dc->dc_memt, clock, 0, writeval);
   1462 		bus_space_barrier(dc->dc_memt, clock, 0, 4, BUS_SPACE_BARRIER_WRITE);
   1463         }
   1464 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
   1465 	    TGA2_MEM_CLOCK + (0xe << 12) + (0x1 << 11) + (0x1 << 11), 4,
   1466 		&clock); /* XXX */
   1467 	bus_space_write_4(dc->dc_memt, clock, 0, 0x0);
   1468 	bus_space_barrier(dc->dc_memt, clock, 0, 0, BUS_SPACE_BARRIER_WRITE);
   1469 }
   1470