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