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