Home | History | Annotate | Line # | Download | only in pci
tga.c revision 1.14
      1 /* $NetBSD: tga.c,v 1.14 1999/04/28 23:24:33 ross Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/kernel.h>
     33 #include <sys/device.h>
     34 #include <sys/conf.h>
     35 #include <sys/malloc.h>
     36 #include <sys/buf.h>
     37 #include <sys/ioctl.h>
     38 
     39 #include <vm/vm.h>
     40 
     41 #include <machine/bus.h>
     42 #include <machine/intr.h>
     43 
     44 #include <dev/pci/pcireg.h>
     45 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/pcidevs.h>
     47 #include <dev/pci/tgareg.h>
     48 #include <dev/pci/tgavar.h>
     49 #include <dev/ic/bt485reg.h>
     50 
     51 #include <dev/rcons/raster.h>
     52 #include <dev/wscons/wsconsio.h>
     53 #include <dev/wscons/wscons_raster.h>
     54 #include <dev/wscons/wsdisplayvar.h>
     55 
     56 #ifdef __alpha__
     57 #include <machine/pte.h>
     58 #endif
     59 
     60 int	tgamatch __P((struct device *, struct cfdata *, void *));
     61 void	tgaattach __P((struct device *, struct device *, void *));
     62 int	tgaprint __P((void *, const char *));
     63 
     64 struct cfattach tga_ca = {
     65 	sizeof(struct tga_softc), tgamatch, tgaattach,
     66 };
     67 
     68 int	tga_identify __P((tga_reg_t *));
     69 const struct tga_conf *tga_getconf __P((int));
     70 void	tga_getdevconfig __P((bus_space_tag_t memt, pci_chipset_tag_t pc,
     71 	    pcitag_t tag, struct tga_devconfig *dc));
     72 
     73 struct tga_devconfig tga_console_dc;
     74 
     75 int tga_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     76 int tga_mmap __P((void *, off_t, int));
     77 static void tga_copyrows __P((void *, int, int, int));
     78 static void tga_copycols __P((void *, int, int, int, int));
     79 static int tga_alloc_screen __P((void *, const struct wsscreen_descr *,
     80 				      void **, int *, int *, long *));
     81 static void tga_free_screen __P((void *, void *));
     82 static void tga_show_screen __P((void *, void *));
     83 static int tga_rop __P((struct raster *, int, int, int, int, int,
     84 	struct raster *, int, int));
     85 static int tga_rop_nosrc __P((struct raster *, int, int, int, int, int));
     86 static int tga_rop_htov __P((struct raster *, int, int, int, int,
     87 	int, struct raster *, int, int ));
     88 static int tga_rop_vtov __P((struct raster *, int, int, int, int,
     89 	int, struct raster *, int, int ));
     90 
     91 struct wsdisplay_emulops tga_emulops = {
     92 	rcons_cursor,			/* could use hardware cursor; punt */
     93 	rcons_mapchar,
     94 	rcons_putchar,
     95 	tga_copycols,
     96 	rcons_erasecols,
     97 	tga_copyrows,
     98 	rcons_eraserows,
     99 	rcons_alloc_attr
    100 };
    101 
    102 struct wsscreen_descr tga_stdscreen = {
    103 	"std",
    104 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
    105 	&tga_emulops,
    106 	0, 0,
    107 	WSSCREEN_REVERSE
    108 };
    109 
    110 const struct wsscreen_descr *_tga_scrlist[] = {
    111 	&tga_stdscreen,
    112 	/* XXX other formats, graphics screen? */
    113 };
    114 
    115 struct wsscreen_list tga_screenlist = {
    116 	sizeof(_tga_scrlist) / sizeof(struct wsscreen_descr *), _tga_scrlist
    117 };
    118 
    119 struct wsdisplay_accessops tga_accessops = {
    120 	tga_ioctl,
    121 	tga_mmap,
    122 	tga_alloc_screen,
    123 	tga_free_screen,
    124 	tga_show_screen,
    125 	0 /* load_font */
    126 };
    127 
    128 void	tga_blank __P((struct tga_devconfig *));
    129 void	tga_unblank __P((struct tga_devconfig *));
    130 
    131 int
    132 tgamatch(parent, match, aux)
    133 	struct device *parent;
    134 	struct cfdata *match;
    135 	void *aux;
    136 {
    137 	struct pci_attach_args *pa = aux;
    138 
    139 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC ||
    140 	    PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_DEC_21030)
    141 		return (0);
    142 
    143 	return (10);
    144 }
    145 
    146 void
    147 tga_getdevconfig(memt, pc, tag, dc)
    148 	bus_space_tag_t memt;
    149 	pci_chipset_tag_t pc;
    150 	pcitag_t tag;
    151 	struct tga_devconfig *dc;
    152 {
    153 	const struct tga_conf *tgac;
    154 	const struct tga_ramdac_conf *tgar;
    155 	struct raster *rap;
    156 	struct rcons *rcp;
    157 	bus_size_t pcisize;
    158 	int i, flags;
    159 
    160 	dc->dc_memt = memt;
    161 	dc->dc_pc = pc;
    162 
    163 	dc->dc_pcitag = tag;
    164 
    165 	/* XXX magic number */
    166 	if (pci_mapreg_info(pc, tag, 0x10,
    167 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    168 	    &dc->dc_pcipaddr, &pcisize, &flags))
    169 		return;
    170 	if ((flags & BUS_SPACE_MAP_CACHEABLE) == 0)		/* XXX */
    171 		panic("tga memory not cacheable");
    172 
    173 	if (bus_space_map(memt, dc->dc_pcipaddr, pcisize,
    174 	    BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_LINEAR, &dc->dc_vaddr))
    175 		return;
    176 #ifdef __alpha__
    177 	dc->dc_paddr = ALPHA_K0SEG_TO_PHYS(dc->dc_vaddr);	/* XXX */
    178 #endif
    179 
    180 	dc->dc_regs = (tga_reg_t *)(dc->dc_vaddr + TGA_MEM_CREGS);
    181 	dc->dc_tga_type = tga_identify(dc->dc_regs);
    182 	tgac = dc->dc_tgaconf = tga_getconf(dc->dc_tga_type);
    183 	if (tgac == NULL)
    184 		return;
    185 
    186 #if 0
    187 	/* XXX on the Alpha, pcisize = 4 * cspace_size. */
    188 	if (tgac->tgac_cspace_size != pcisize)			/* sanity */
    189 		panic("tga_getdevconfig: memory size mismatch?");
    190 #endif
    191 
    192 	tgar = tgac->tgac_ramdac;
    193 
    194 	switch (dc->dc_regs[TGA_REG_VHCR] & 0x1ff) {		/* XXX */
    195 	case 0:
    196 		dc->dc_wid = 8192;
    197 		break;
    198 
    199 	case 1:
    200 		dc->dc_wid = 8196;
    201 		break;
    202 
    203 	default:
    204 		dc->dc_wid = (dc->dc_regs[TGA_REG_VHCR] & 0x1ff) * 4; /* XXX */
    205 		break;
    206 	}
    207 
    208 	dc->dc_rowbytes = dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
    209 
    210 	if ((dc->dc_regs[TGA_REG_VHCR] & 0x00000001) != 0 &&	/* XXX */
    211 	    (dc->dc_regs[TGA_REG_VHCR] & 0x80000000) != 0) {	/* XXX */
    212 		dc->dc_wid -= 4;
    213 		/*
    214 		 * XXX XXX turning off 'odd' shouldn't be necesssary,
    215 		 * XXX XXX but i can't make X work with the weird size.
    216 		 */
    217 		dc->dc_regs[TGA_REG_VHCR] &= ~0x80000001;
    218 		dc->dc_rowbytes =
    219 		    dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
    220 	}
    221 
    222 	dc->dc_ht = (dc->dc_regs[TGA_REG_VVCR] & 0x7ff);	/* XXX */
    223 
    224 	/* XXX this seems to be what DEC does */
    225 	dc->dc_regs[TGA_REG_CCBR] = 0;
    226 	dc->dc_regs[TGA_REG_VVBR] = 1;
    227 	dc->dc_videobase = dc->dc_vaddr + tgac->tgac_dbuf[0] +
    228 	    1 * tgac->tgac_vvbr_units;
    229 	dc->dc_blanked = 1;
    230 	tga_unblank(dc);
    231 
    232 	/*
    233 	 * Set all bits in the pixel mask, to enable writes to all pixels.
    234 	 * It seems that the console firmware clears some of them
    235 	 * under some circumstances, which causes cute vertical stripes.
    236 	 */
    237 	dc->dc_regs[TGA_REG_GPXR_P] = 0xffffffff;
    238 
    239 	/* clear the screen */
    240 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
    241 		*(u_int32_t *)(dc->dc_videobase + i) = 0;
    242 
    243 	/* initialize the raster */
    244 	rap = &dc->dc_raster;
    245 	rap->width = dc->dc_wid;
    246 	rap->height = dc->dc_ht;
    247 	rap->depth = tgac->tgac_phys_depth;
    248 	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
    249 	rap->pixels = (u_int32_t *)dc->dc_videobase;
    250 	rap->data = (caddr_t)dc;
    251 
    252 	/* initialize the raster console blitter */
    253 	rcp = &dc->dc_rcons;
    254 	rcp->rc_sp = rap;
    255 	rcp->rc_crow = rcp->rc_ccol = -1;
    256 	rcp->rc_crowp = &rcp->rc_crow;
    257 	rcp->rc_ccolp = &rcp->rc_ccol;
    258 	rcons_init(rcp, 34, 80);
    259 
    260 	tga_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
    261 	tga_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
    262 }
    263 
    264 void
    265 tgaattach(parent, self, aux)
    266 	struct device *parent, *self;
    267 	void *aux;
    268 {
    269 	struct pci_attach_args *pa = aux;
    270 	struct tga_softc *sc = (struct tga_softc *)self;
    271 	struct wsemuldisplaydev_attach_args aa;
    272 	pci_intr_handle_t intrh;
    273 	const char *intrstr;
    274 	u_int8_t rev;
    275 	int console;
    276 
    277 #ifdef __alpha__
    278 	console = (pa->pa_tag == tga_console_dc.dc_pcitag);
    279 #else
    280 	console = 0;
    281 #endif
    282 	if (console) {
    283 		sc->sc_dc = &tga_console_dc;
    284 		sc->nscreens = 1;
    285 	} else {
    286 		sc->sc_dc = (struct tga_devconfig *)
    287 		    malloc(sizeof(struct tga_devconfig), M_DEVBUF, M_WAITOK);
    288 		bzero(sc->sc_dc, sizeof(struct tga_devconfig));
    289 		tga_getdevconfig(pa->pa_memt, pa->pa_pc, pa->pa_tag, sc->sc_dc);
    290 	}
    291 	if (sc->sc_dc->dc_vaddr == NULL) {
    292 		printf(": couldn't map memory space; punt!\n");
    293 		return;
    294 	}
    295 
    296 	/* XXX say what's going on. */
    297 	intrstr = NULL;
    298 	if (sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_intr != NULL) {
    299 		if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    300 		    pa->pa_intrline, &intrh)) {
    301 			printf(": couldn't map interrupt");
    302 			return;
    303 		}
    304 		intrstr = pci_intr_string(pa->pa_pc, intrh);
    305 		sc->sc_intr = pci_intr_establish(pa->pa_pc, intrh, IPL_TTY,
    306 		    sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_intr, sc->sc_dc);
    307 		if (sc->sc_intr == NULL) {
    308 			printf(": couldn't establish interrupt");
    309 			if (intrstr != NULL)
    310 				printf("at %s", intrstr);
    311 			printf("\n");
    312 			return;
    313 		}
    314 	}
    315 
    316 	/*
    317 	 * Initialize the RAMDAC and allocate any private storage it needs.
    318 	 * Initialization includes disabling cursor, setting a sane
    319 	 * colormap, etc.
    320 	 */
    321 	(*sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_init)(sc->sc_dc, 1);
    322 
    323 	printf(": DC21030 ");
    324 	rev = PCI_REVISION(pa->pa_class);
    325 	switch (rev) {
    326 	case 1: case 2: case 3:
    327 		printf("step %c", 'A' + rev - 1);
    328 		break;
    329 
    330 	default:
    331 		printf("unknown stepping (0x%x)", rev);
    332 		break;
    333 	}
    334 	printf(", ");
    335 
    336 	if (sc->sc_dc->dc_tgaconf == NULL) {
    337 		printf("unknown board configuration\n");
    338 		return;
    339 	}
    340 	printf("board type %s\n", sc->sc_dc->dc_tgaconf->tgac_name);
    341 	printf("%s: %d x %d, %dbpp, %s RAMDAC\n", sc->sc_dev.dv_xname,
    342 	    sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
    343 	    sc->sc_dc->dc_tgaconf->tgac_phys_depth,
    344 	    sc->sc_dc->dc_tgaconf->tgac_ramdac->tgar_name);
    345 
    346 	if (intrstr != NULL)
    347 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    348 		    intrstr);
    349 
    350 	aa.console = console;
    351 	aa.scrdata = &tga_screenlist;
    352 	aa.accessops = &tga_accessops;
    353 	aa.accesscookie = sc;
    354 
    355 	config_found(self, &aa, wsemuldisplaydevprint);
    356 }
    357 
    358 int
    359 tga_ioctl(v, cmd, data, flag, p)
    360 	void *v;
    361 	u_long cmd;
    362 	caddr_t data;
    363 	int flag;
    364 	struct proc *p;
    365 {
    366 	struct tga_softc *sc = v;
    367 	struct tga_devconfig *dc = sc->sc_dc;
    368 	const struct tga_ramdac_conf *tgar = dc->dc_tgaconf->tgac_ramdac;
    369 
    370 	switch (cmd) {
    371 	case WSDISPLAYIO_GTYPE:
    372 		*(u_int *)data = WSDISPLAY_TYPE_TGA;
    373 		return (0);
    374 
    375 	case WSDISPLAYIO_GINFO:
    376 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    377 		wsd_fbip->height = sc->sc_dc->dc_ht;
    378 		wsd_fbip->width = sc->sc_dc->dc_wid;
    379 		wsd_fbip->depth = sc->sc_dc->dc_tgaconf->tgac_phys_depth;
    380 		wsd_fbip->cmsize = 256;		/* XXX ??? */
    381 #undef wsd_fbip
    382 		return (0);
    383 
    384 	case WSDISPLAYIO_GETCMAP:
    385 		return (*tgar->tgar_get_cmap)(dc,
    386 		    (struct wsdisplay_cmap *)data);
    387 
    388 	case WSDISPLAYIO_PUTCMAP:
    389 		return (*tgar->tgar_set_cmap)(dc,
    390 		    (struct wsdisplay_cmap *)data);
    391 
    392 	case WSDISPLAYIO_SVIDEO:
    393 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
    394 			tga_blank(sc->sc_dc);
    395 		else
    396 			tga_unblank(sc->sc_dc);
    397 		return (0);
    398 
    399 	case WSDISPLAYIO_GVIDEO:
    400 		*(u_int *)data = dc->dc_blanked ?
    401 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    402 		return (0);
    403 
    404 	case WSDISPLAYIO_GCURPOS:
    405 		return (*tgar->tgar_get_curpos)(dc,
    406 		    (struct wsdisplay_curpos *)data);
    407 
    408 	case WSDISPLAYIO_SCURPOS:
    409 		return (*tgar->tgar_set_curpos)(dc,
    410 		    (struct wsdisplay_curpos *)data);
    411 
    412 	case WSDISPLAYIO_GCURMAX:
    413 		return (*tgar->tgar_get_curmax)(dc,
    414 		    (struct wsdisplay_curpos *)data);
    415 
    416 	case WSDISPLAYIO_GCURSOR:
    417 		return (*tgar->tgar_get_cursor)(dc,
    418 		    (struct wsdisplay_cursor *)data);
    419 
    420 	case WSDISPLAYIO_SCURSOR:
    421 		return (*tgar->tgar_set_cursor)(dc,
    422 		    (struct wsdisplay_cursor *)data);
    423 	}
    424 	return (-1);
    425 }
    426 
    427 int
    428 tga_mmap(v, offset, prot)
    429 	void *v;
    430 	off_t offset;
    431 	int prot;
    432 {
    433 
    434 	/* XXX NEW MAPPING CODE... */
    435 
    436 #ifdef __alpha__
    437 	struct tga_softc *sc = v;
    438 
    439 	if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
    440 		return -1;
    441 	return alpha_btop(sc->sc_dc->dc_paddr + offset);
    442 #else
    443 	return (-1);
    444 #endif
    445 }
    446 
    447 int
    448 tga_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    449 	void *v;
    450 	const struct wsscreen_descr *type;
    451 	void **cookiep;
    452 	int *curxp, *curyp;
    453 	long *attrp;
    454 {
    455 	struct tga_softc *sc = v;
    456 	long defattr;
    457 
    458 	if (sc->nscreens > 0)
    459 		return (ENOMEM);
    460 
    461 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
    462 	*curxp = 0;
    463 	*curyp = 0;
    464 	rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
    465 	*attrp = defattr;
    466 	sc->nscreens++;
    467 	return (0);
    468 }
    469 
    470 void
    471 tga_free_screen(v, cookie)
    472 	void *v;
    473 	void *cookie;
    474 {
    475 	struct tga_softc *sc = v;
    476 
    477 	if (sc->sc_dc == &tga_console_dc)
    478 		panic("tga_free_screen: console");
    479 
    480 	sc->nscreens--;
    481 }
    482 
    483 void
    484 tga_show_screen(v, cookie)
    485 	void *v;
    486 	void *cookie;
    487 {
    488 }
    489 
    490 int
    491 tga_cnattach(iot, memt, pc, bus, device, function)
    492 	bus_space_tag_t iot, memt;
    493 	pci_chipset_tag_t pc;
    494 	int bus, device, function;
    495 {
    496 	struct tga_devconfig *dcp = &tga_console_dc;
    497 	long defattr;
    498 
    499 	tga_getdevconfig(memt, pc,
    500 	    pci_make_tag(pc, bus, device, function), dcp);
    501 
    502 	/* sanity checks */
    503 	if (dcp->dc_vaddr == NULL)
    504 		panic("tga_console(%d, %d): couldn't map memory space",
    505 		    device, function);
    506 	if (dcp->dc_tgaconf == NULL)
    507 		panic("tga_console(%d, %d): unknown board configuration",
    508 		    device, function);
    509 
    510 	/*
    511 	 * Initialize the RAMDAC but DO NOT allocate any private storage.
    512 	 * Initialization includes disabling cursor, setting a sane
    513 	 * colormap, etc.  It will be reinitialized in tgaattach().
    514 	 */
    515 	(*dcp->dc_tgaconf->tgac_ramdac->tgar_init)(dcp, 0);
    516 
    517 	rcons_alloc_attr(&dcp->dc_rcons, 0, 0, 0, &defattr);
    518 
    519 	wsdisplay_cnattach(&tga_stdscreen, &dcp->dc_rcons,
    520 			   0, 0, defattr);
    521 
    522 	return(0);
    523 }
    524 
    525 /*
    526  * Functions to blank and unblank the display.
    527  */
    528 void
    529 tga_blank(dc)
    530 	struct tga_devconfig *dc;
    531 {
    532 
    533 	if (!dc->dc_blanked) {
    534 		dc->dc_blanked = 1;
    535 		dc->dc_regs[TGA_REG_VVVR] |= VVR_BLANK;		/* XXX */
    536 	}
    537 }
    538 
    539 void
    540 tga_unblank(dc)
    541 	struct tga_devconfig *dc;
    542 {
    543 
    544 	if (dc->dc_blanked) {
    545 		dc->dc_blanked = 0;
    546 		dc->dc_regs[TGA_REG_VVVR] &= ~VVR_BLANK;	/* XXX */
    547 	}
    548 }
    549 
    550 /*
    551  * Functions to manipulate the built-in cursor handing hardware.
    552  */
    553 int
    554 tga_builtin_set_cursor(dc, cursorp)
    555 	struct tga_devconfig *dc;
    556 	struct wsdisplay_cursor *cursorp;
    557 {
    558 	int count, error, v;
    559 
    560 	v = cursorp->which;
    561 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    562 		error = (*dc->dc_tgaconf->tgac_ramdac->tgar_check_curcmap)(dc,
    563 		    cursorp);
    564 		if (error)
    565 			return (error);
    566 	}
    567 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    568 		if ((u_int)cursorp->size.x != 64 ||
    569 		    (u_int)cursorp->size.y > 64)
    570 			return (EINVAL);
    571 		/* The cursor is 2 bits deep, and there is no mask */
    572 		count = (cursorp->size.y * 64 * 2) / NBBY;
    573 		if (!uvm_useracc(cursorp->image, count, B_READ))
    574 			return (EFAULT);
    575 	}
    576 	if (v & WSDISPLAY_CURSOR_DOHOT)		/* not supported */
    577 		return EINVAL;
    578 
    579 	/* parameters are OK; do it */
    580 	if (v & WSDISPLAY_CURSOR_DOCUR) {
    581 		if (cursorp->enable)
    582 			dc->dc_regs[TGA_REG_VVVR] |= 0x04;	/* XXX */
    583 		else
    584 			dc->dc_regs[TGA_REG_VVVR] &= ~0x04;	/* XXX */
    585 	}
    586 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    587 		dc->dc_regs[TGA_REG_CXYR] = ((cursorp->pos.y & 0xfff) << 12) |
    588 		    (cursorp->pos.x & 0xfff);
    589 	}
    590 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    591 		/* can't fail. */
    592 		(*dc->dc_tgaconf->tgac_ramdac->tgar_set_curcmap)(dc, cursorp);
    593 	}
    594 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    595 		count = ((64 * 2) / NBBY) * cursorp->size.y;
    596 		dc->dc_regs[TGA_REG_CCBR] =
    597 		    (dc->dc_regs[TGA_REG_CCBR] & ~0xfc00) |
    598 		    (cursorp->size.y << 10);
    599 		copyin(cursorp->image, (char *)(dc->dc_vaddr +
    600 		    (dc->dc_regs[TGA_REG_CCBR] & 0x3ff)),
    601 		    count);				/* can't fail. */
    602 	}
    603 	return (0);
    604 }
    605 
    606 int
    607 tga_builtin_get_cursor(dc, cursorp)
    608 	struct tga_devconfig *dc;
    609 	struct wsdisplay_cursor *cursorp;
    610 {
    611 	int count, error;
    612 
    613 	cursorp->which = WSDISPLAY_CURSOR_DOALL &
    614 	    ~(WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOCMAP);
    615 	cursorp->enable = (dc->dc_regs[TGA_REG_VVVR] & 0x04) != 0;
    616 	cursorp->pos.x = dc->dc_regs[TGA_REG_CXYR] & 0xfff;
    617 	cursorp->pos.y = (dc->dc_regs[TGA_REG_CXYR] >> 12) & 0xfff;
    618 	cursorp->size.x = 64;
    619 	cursorp->size.y = (dc->dc_regs[TGA_REG_CCBR] >> 10) & 0x3f;
    620 
    621 	if (cursorp->image != NULL) {
    622 		count = (cursorp->size.y * 64 * 2) / NBBY;
    623 		error = copyout((char *)(dc->dc_vaddr +
    624 		      (dc->dc_regs[TGA_REG_CCBR] & 0x3ff)),
    625 		    cursorp->image, count);
    626 		if (error)
    627 			return (error);
    628 		/* No mask */
    629 	}
    630 	error = (*dc->dc_tgaconf->tgac_ramdac->tgar_get_curcmap)(dc, cursorp);
    631 	return (error);
    632 }
    633 
    634 int
    635 tga_builtin_set_curpos(dc, curposp)
    636 	struct tga_devconfig *dc;
    637 	struct wsdisplay_curpos *curposp;
    638 {
    639 
    640 	dc->dc_regs[TGA_REG_CXYR] =
    641 	    ((curposp->y & 0xfff) << 12) | (curposp->x & 0xfff);
    642 	return (0);
    643 }
    644 
    645 int
    646 tga_builtin_get_curpos(dc, curposp)
    647 	struct tga_devconfig *dc;
    648 	struct wsdisplay_curpos *curposp;
    649 {
    650 
    651 	curposp->x = dc->dc_regs[TGA_REG_CXYR] & 0xfff;
    652 	curposp->y = (dc->dc_regs[TGA_REG_CXYR] >> 12) & 0xfff;
    653 	return (0);
    654 }
    655 
    656 int
    657 tga_builtin_get_curmax(dc, curposp)
    658 	struct tga_devconfig *dc;
    659 	struct wsdisplay_curpos *curposp;
    660 {
    661 
    662 	curposp->x = curposp->y = 64;
    663 	return (0);
    664 }
    665 
    666 /*
    667  * Copy columns (characters) in a row (line).
    668  */
    669 void
    670 tga_copycols(id, row, srccol, dstcol, ncols)
    671 	void *id;
    672 	int row, srccol, dstcol, ncols;
    673 {
    674 	struct rcons *rc = id;
    675 	int y, srcx, dstx, nx;
    676 
    677 	y = rc->rc_yorigin + rc->rc_font->height * row;
    678 	srcx = rc->rc_xorigin + rc->rc_font->width * srccol;
    679 	dstx = rc->rc_xorigin + rc->rc_font->width * dstcol;
    680 	nx = rc->rc_font->width * ncols;
    681 
    682 	tga_rop(rc->rc_sp, dstx, y,
    683 	    nx, rc->rc_font->height, RAS_SRC,
    684 	    rc->rc_sp, srcx, y);
    685 }
    686 
    687 /*
    688  * Copy rows (lines).
    689  */
    690 void
    691 tga_copyrows(id, srcrow, dstrow, nrows)
    692 	void *id;
    693 	int srcrow, dstrow, nrows;
    694 {
    695 	struct rcons *rc = id;
    696 	int srcy, dsty, ny;
    697 
    698 	srcy = rc->rc_yorigin + rc->rc_font->height * srcrow;
    699 	dsty = rc->rc_yorigin + rc->rc_font->height * dstrow;
    700 	ny = rc->rc_font->height * nrows;
    701 
    702 	tga_rop(rc->rc_sp, rc->rc_xorigin, dsty,
    703 	    rc->rc_raswidth, ny, RAS_SRC,
    704 	    rc->rc_sp, rc->rc_xorigin, srcy);
    705 }
    706 
    707 /* Do we need the src? */
    708 static int needsrc[16] = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 };
    709 
    710 /* A mapping between our API and the TGA card */
    711 static int map_rop[16] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6,
    712 	0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
    713 };
    714 
    715 /*
    716  *  Generic TGA raster op.
    717  *   This covers all possible raster ops, and
    718  *   clips the sizes and all of that.
    719  */
    720 static int
    721 tga_rop(dst, dx, dy, w, h, rop, src, sx, sy)
    722 	struct raster *dst;
    723 	int dx, dy, w, h, rop;
    724 	struct raster *src;
    725 	int sx, sy;
    726 {
    727 	if (!dst)
    728 		return -1;
    729 	if (dst->data == NULL)
    730 		return -1;	/* we should be writing to a screen */
    731 	if (needsrc[RAS_GETOP(rop)]) {
    732 		if (src == (struct raster *) 0)
    733 			return -1;	/* We want a src */
    734 		/* Clip against src */
    735 		if (sx < 0) {
    736 			w += sx;
    737 			sx = 0;
    738 		}
    739 		if (sy < 0) {
    740 			h += sy;
    741 			sy = 0;
    742 		}
    743 		if (sx + w > src->width)
    744 			w = src->width - sx;
    745 		if (sy + h > src->height)
    746 			h = src->height - sy;
    747 	} else {
    748 		if (src != (struct raster *) 0)
    749 			return -1;	/* We need no src */
    750 	}
    751 	/* Clip against dst.  We modify src regardless of using it,
    752 	 * since it really doesn't matter.
    753 	 */
    754 	if (dx < 0) {
    755 		w += dx;
    756 		sx -= dx;
    757 		dx = 0;
    758 	}
    759 	if (dy < 0) {
    760 		h += dy;
    761 		sy -= dy;
    762 		dy = 0;
    763 	}
    764 	if (dx + w > dst->width)
    765 		w = dst->width - dx;
    766 	if (dy + h > dst->height)
    767 		h = dst->height - dy;
    768 	if (w <= 0 || h <= 0)
    769 		return 0;	/* Vacuously true; */
    770 	if (!src)
    771 		return tga_rop_nosrc(dst, dx, dy, w, h, rop);
    772 	if (src->data == NULL)
    773 		return tga_rop_htov(dst, dx, dy, w, h, rop, src, sx, sy);
    774 	else
    775 		return tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy);
    776 }
    777 
    778 /*
    779  * No source raster ops.
    780  * This function deals with all raster ops that don't require a src.
    781  */
    782 static int
    783 tga_rop_nosrc(dst, dx, dy, w, h, rop)
    784 	struct raster *dst;
    785 	int dx, dy, w, h, rop;
    786 {
    787 	return raster_op(dst, dx, dy, w, h, rop, NULL, 0, 0);
    788 }
    789 
    790 /*
    791  * Host to Video raster ops.
    792  * This function deals with all raster ops that have a src that is host memory.
    793  */
    794 static int
    795 tga_rop_htov(dst, dx, dy, w, h, rop, src, sx, sy)
    796 	struct raster *dst;
    797 	int dx, dy, w, h, rop;
    798 	struct raster *src;
    799 	int sx, sy;
    800 {
    801 	return raster_op(dst, dx, dy, w, h, rop, src, sx, sy);
    802 }
    803 
    804 /*
    805  * Video to Video raster ops.
    806  * This function deals with all raster ops that have a src and dst
    807  * that are on the card.
    808  */
    809 static int
    810 tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy)
    811 	struct raster *dst;
    812 	int dx, dy, w, h, rop;
    813 	struct raster *src;
    814 	int sx, sy;
    815 {
    816 	struct tga_devconfig *dc = (struct tga_devconfig *)dst->data;
    817 	tga_reg_t *regs0 = dc->dc_regs;
    818 	tga_reg_t *regs1 = regs0 + 16 * 1024;	/* register alias 1 */
    819 	tga_reg_t *regs2 = regs1 + 16 * 1024;	/* register alias 2 */
    820 	tga_reg_t *regs3 = regs2 + 16 * 1024;	/* register alias 3 */
    821 	int srcb, dstb;
    822 	int x, y;
    823 	int xstart, xend, xdir, xinc;
    824 	int ystart, yend, ydir, yinc;
    825 	int offset = 1 * dc->dc_tgaconf->tgac_vvbr_units;
    826 
    827 	/*
    828 	 * I don't yet want to deal with unaligned guys, really.  And we don't
    829 	 * deal with copies from one card to another.
    830 	 */
    831 	if (dx % 8 != 0 || sx % 8 != 0 || src != dst)
    832 		return raster_op(dst, dx, dy, w, h, rop, src, sx, sy);
    833 
    834 	if (sy >= dy) {
    835 		ystart = 0;
    836 		yend = h;
    837 		ydir = 1;
    838 	} else {
    839 		ystart = h;
    840 		yend = 0;
    841 		ydir = -1;
    842 	}
    843 	if (sx >= dx) {
    844 		xstart = 0;
    845 		xend = w * (dst->depth / 8);
    846 		xdir = 1;
    847 	} else {
    848 		xstart = w * (dst->depth / 8);
    849 		xend = 0;
    850 		xdir = -1;
    851 	}
    852 	xinc = xdir * 4 * 64;
    853 	yinc = ydir * dst->linelongs * 4;
    854 	ystart *= dst->linelongs * 4;
    855 	yend *= dst->linelongs * 4;
    856 	srcb = offset + sy  * src->linelongs * 4 + sx;
    857 	dstb = offset + dy  * dst->linelongs * 4 + dx;
    858 	regs3[TGA_REG_GMOR] = 0x0007;		/* Copy mode */
    859 	regs3[TGA_REG_GOPR] = map_rop[rop];	/* Set up the op */
    860 	for (y = ystart; (ydir * y) < (ydir * yend); y += yinc) {
    861 		for (x = xstart; (xdir * x) < (xdir * xend); x += xinc) {
    862 			regs0[TGA_REG_GCSR] = srcb + y + x + 3 * 64;
    863 			regs0[TGA_REG_GCDR] = dstb + y + x + 3 * 64;
    864 			regs1[TGA_REG_GCSR] = srcb + y + x + 2 * 64;
    865 			regs1[TGA_REG_GCDR] = dstb + y + x + 2 * 64;
    866 			regs2[TGA_REG_GCSR] = srcb + y + x + 1 * 64;
    867 			regs2[TGA_REG_GCDR] = dstb + y + x + 1 * 64;
    868 			regs3[TGA_REG_GCSR] = srcb + y + x + 0 * 64;
    869 			regs3[TGA_REG_GCDR] = dstb + y + x + 0 * 64;
    870 		}
    871 	}
    872 	regs0[TGA_REG_GOPR] = 0x0003;		/* op -> dst = src */
    873 	regs0[TGA_REG_GMOR] = 0x0000;		/* Simple mode */
    874 	return 0;
    875 }
    876