Home | History | Annotate | Line # | Download | only in pci
tga.c revision 1.29
      1 /* $NetBSD: tga.c,v 1.29 2000/12/17 22:23:12 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 		bzero(sc->sc_dc, 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->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    391 	    pa->pa_intrline, &intrh)) {
    392 		printf(": couldn't map interrupt");
    393 		return;
    394 	}
    395 	intrstr = pci_intr_string(pa->pa_pc, intrh);
    396 	sc->sc_intr = pci_intr_establish(pa->pa_pc, intrh, IPL_TTY, tga_intr,
    397 	    sc->sc_dc);
    398 	if (sc->sc_intr == NULL) {
    399 		printf(": couldn't establish interrupt");
    400 		if (intrstr != NULL)
    401 			printf("at %s", intrstr);
    402 		printf("\n");
    403 		return;
    404 	}
    405 
    406 	rev = PCI_REVISION(pa->pa_class);
    407 	switch (rev) {
    408 	case 0x1:
    409 	case 0x2:
    410 	case 0x3:
    411 		printf(": DC21030 step %c", 'A' + rev - 1);
    412 		break;
    413 	case 0x20:
    414 		printf(": TGA2 abstract software model");
    415 		break;
    416 	case 0x21:
    417 	case 0x22:
    418 		printf(": TGA2 pass %d", rev - 0x20);
    419 		break;
    420 
    421 	default:
    422 		printf("unknown stepping (0x%x)", rev);
    423 		break;
    424 	}
    425 	printf(", ");
    426 
    427 	/*
    428 	 * Get RAMDAC function vectors and call the RAMDAC functions
    429 	 * to allocate its private storage and pass that back to us.
    430 	 */
    431 
    432 	sc->sc_dc->dc_ramdac_funcs = sc->sc_dc->dc_tgaconf->ramdac_funcs();
    433 	if (!sc->sc_dc->dc_tga2) {
    434 	    if (sc->sc_dc->dc_tgaconf->ramdac_funcs == bt485_funcs)
    435 		  sc->sc_dc->dc_ramdac_cookie =
    436 			sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
    437 		    tga_sched_update, tga_ramdac_wr, tga_ramdac_rd);
    438 		else
    439 		  sc->sc_dc->dc_ramdac_cookie =
    440 			sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
    441 		    tga_sched_update, tga_bt463_wr, tga_bt463_rd);
    442 	} else {
    443 		sc->sc_dc->dc_ramdac_cookie =
    444 			sc->sc_dc->dc_ramdac_funcs->ramdac_register(sc->sc_dc,
    445 			tga_sched_update, tga2_ramdac_wr, tga2_ramdac_rd);
    446 	}
    447 
    448 	/*
    449 	 * Initialize the RAMDAC.  Initialization includes disabling
    450 	 * cursor, setting a sane colormap, etc.
    451 	 */
    452 	(*sc->sc_dc->dc_ramdac_funcs->ramdac_init)(sc->sc_dc->dc_ramdac_cookie);
    453 	TGAWREG(sc->sc_dc, TGA_REG_SISR, 0x00000001); /* XXX */
    454 
    455 	if (sc->sc_dc->dc_tgaconf == NULL) {
    456 		printf("unknown board configuration\n");
    457 		return;
    458 	}
    459 	printf("board type %s\n", sc->sc_dc->dc_tgaconf->tgac_name);
    460 	printf("%s: %d x %d, %dbpp, %s RAMDAC\n", sc->sc_dev.dv_xname,
    461 	    sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
    462 	    sc->sc_dc->dc_tgaconf->tgac_phys_depth,
    463 	    sc->sc_dc->dc_ramdac_funcs->ramdac_name);
    464 
    465 	if (intrstr != NULL)
    466 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    467 		    intrstr);
    468 
    469 	aa.console = console;
    470 	aa.scrdata = &tga_screenlist;
    471 	aa.accessops = &tga_accessops;
    472 	aa.accesscookie = sc;
    473 
    474 	config_found(self, &aa, wsemuldisplaydevprint);
    475 
    476 	config_interrupts(self, tga_config_interrupts);
    477 }
    478 
    479 static void
    480 tga_config_interrupts (d)
    481 	struct device *d;
    482 {
    483 	struct tga_softc *sc = (struct tga_softc *)d;
    484 	sc->sc_dc->dc_intrenabled = 1;
    485 }
    486 
    487 
    488 int
    489 tga_ioctl(v, cmd, data, flag, p)
    490 	void *v;
    491 	u_long cmd;
    492 	caddr_t data;
    493 	int flag;
    494 	struct proc *p;
    495 {
    496 	struct tga_softc *sc = v;
    497 	struct tga_devconfig *dc = sc->sc_dc;
    498 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    499 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    500 
    501 	switch (cmd) {
    502 	case WSDISPLAYIO_GTYPE:
    503 		*(u_int *)data = WSDISPLAY_TYPE_TGA;
    504 		return (0);
    505 
    506 	case WSDISPLAYIO_GINFO:
    507 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    508 		wsd_fbip->height = sc->sc_dc->dc_ht;
    509 		wsd_fbip->width = sc->sc_dc->dc_wid;
    510 		wsd_fbip->depth = sc->sc_dc->dc_tgaconf->tgac_phys_depth;
    511 		wsd_fbip->cmsize = 256;		/* XXX ??? */
    512 #undef wsd_fbip
    513 		return (0);
    514 
    515 	case WSDISPLAYIO_GETCMAP:
    516 		return (*dcrf->ramdac_get_cmap)(dcrc,
    517 		    (struct wsdisplay_cmap *)data);
    518 
    519 	case WSDISPLAYIO_PUTCMAP:
    520 		return (*dcrf->ramdac_set_cmap)(dcrc,
    521 		    (struct wsdisplay_cmap *)data);
    522 
    523 	case WSDISPLAYIO_SVIDEO:
    524 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
    525 			tga_blank(sc->sc_dc);
    526 		else
    527 			tga_unblank(sc->sc_dc);
    528 		return (0);
    529 
    530 	case WSDISPLAYIO_GVIDEO:
    531 		*(u_int *)data = dc->dc_blanked ?
    532 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    533 		return (0);
    534 
    535 	case WSDISPLAYIO_GCURPOS:
    536 		return (*dcrf->ramdac_get_curpos)(dcrc,
    537 		    (struct wsdisplay_curpos *)data);
    538 
    539 	case WSDISPLAYIO_SCURPOS:
    540 		return (*dcrf->ramdac_set_curpos)(dcrc,
    541 		    (struct wsdisplay_curpos *)data);
    542 
    543 	case WSDISPLAYIO_GCURMAX:
    544 		return (*dcrf->ramdac_get_curmax)(dcrc,
    545 		    (struct wsdisplay_curpos *)data);
    546 
    547 	case WSDISPLAYIO_GCURSOR:
    548 		return (*dcrf->ramdac_get_cursor)(dcrc,
    549 		    (struct wsdisplay_cursor *)data);
    550 
    551 	case WSDISPLAYIO_SCURSOR:
    552 		return (*dcrf->ramdac_set_cursor)(dcrc,
    553 		    (struct wsdisplay_cursor *)data);
    554 	}
    555 	return (-1);
    556 }
    557 
    558 static int
    559 tga_sched_update(v, f)
    560 	void	*v;
    561 	void	(*f) __P((void *));
    562 {
    563 	struct tga_devconfig *dc = v;
    564 
    565 	if (dc->dc_intrenabled) {
    566 		/* Arrange for f to be called at the next end-of-frame interrupt */
    567 		dc->dc_ramdac_intr = f;
    568 		TGAWREG(dc, TGA_REG_SISR, 0x00010000);
    569 	} else {
    570 		/* Spin until the end-of-frame, then call f */
    571 		TGAWREG(dc, TGA_REG_SISR, 0x00010001);
    572 		TGAREGWB(dc, TGA_REG_SISR, 1);
    573 		while ((TGARREG(dc, TGA_REG_SISR) & 0x00000001) == 0)
    574 			;
    575 		f(dc->dc_ramdac_cookie);
    576 		TGAWREG(dc, TGA_REG_SISR, 0x00000001);
    577 		TGAREGWB(dc, TGA_REG_SISR, 1);
    578 	}
    579 
    580 	return 0;
    581 }
    582 
    583 static int
    584 tga_intr(v)
    585 	void *v;
    586 {
    587 	struct tga_devconfig *dc = v;
    588 	struct ramdac_cookie *dcrc= dc->dc_ramdac_cookie;
    589 
    590 	u_int32_t reg;
    591 
    592 	reg = TGARREG(dc, TGA_REG_SISR);
    593 	if (( reg & 0x00010001) != 0x00010001) {
    594 		/* Odd. We never set any of the other interrupt enables. */
    595 		if ((reg & 0x1f) != 0) {
    596 			/* Clear the mysterious pending interrupts. */
    597 			TGAWREG(dc, TGA_REG_SISR, (reg & 0x1f));
    598 			TGAREGWB(dc, TGA_REG_SISR, 1);
    599 			/* This was our interrupt, even if we're puzzled as to why
    600 			 * we got it.  Don't make the interrupt handler think it
    601 			 * was a stray.
    602 			 */
    603 			return -1;
    604 		} else {
    605 			return 0;
    606 		}
    607 	}
    608 	dc->dc_ramdac_intr(dcrc);
    609 	dc->dc_ramdac_intr = NULL;
    610 	TGAWREG(dc, TGA_REG_SISR, 0x00000001);
    611 	TGAREGWB(dc, TGA_REG_SISR, 1);
    612 	return (1);
    613 }
    614 
    615 paddr_t
    616 tga_mmap(v, offset, prot)
    617 	void *v;
    618 	off_t offset;
    619 	int prot;
    620 {
    621 
    622 	/* XXX NEW MAPPING CODE... */
    623 
    624 #if defined(__alpha__)
    625 	struct tga_softc *sc = v;
    626 
    627 	if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
    628 		return -1;
    629 	return alpha_btop(sc->sc_dc->dc_paddr + offset);
    630 #elif defined(__mips__)
    631 	struct tga_softc *sc = v;
    632 
    633 	if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
    634 		return -1;
    635 	return mips_btop(sc->sc_dc->dc_paddr + offset);
    636 #else
    637 	return (-1);
    638 #endif
    639 }
    640 
    641 static int
    642 tga_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    643 	void *v;
    644 	const struct wsscreen_descr *type;
    645 	void **cookiep;
    646 	int *curxp, *curyp;
    647 	long *attrp;
    648 {
    649 	struct tga_softc *sc = v;
    650 	long defattr;
    651 
    652 	if (sc->nscreens > 0)
    653 		return (ENOMEM);
    654 
    655 	*cookiep = &sc->sc_dc->dc_rinfo; /* one and only for now */
    656 	*curxp = 0;
    657 	*curyp = 0;
    658 	sc->sc_dc->dc_rinfo.ri_ops.alloc_attr(&sc->sc_dc->dc_rinfo,
    659 		0, 0, 0, &defattr);
    660 	*attrp = defattr;
    661 	sc->nscreens++;
    662 	return (0);
    663 }
    664 
    665 static void
    666 tga_free_screen(v, cookie)
    667 	void *v;
    668 	void *cookie;
    669 {
    670 	struct tga_softc *sc = v;
    671 
    672 	if (sc->sc_dc == &tga_console_dc)
    673 		panic("tga_free_screen: console");
    674 
    675 	sc->nscreens--;
    676 }
    677 
    678 static int
    679 tga_show_screen(v, cookie, waitok, cb, cbarg)
    680 	void *v;
    681 	void *cookie;
    682 	int waitok;
    683 	void (*cb) __P((void *, int, int));
    684 	void *cbarg;
    685 {
    686 
    687 	return (0);
    688 }
    689 
    690 int
    691 tga_cnattach(iot, memt, pc, bus, device, function)
    692 	bus_space_tag_t iot, memt;
    693 	pci_chipset_tag_t pc;
    694 	int bus, device, function;
    695 {
    696 	struct tga_devconfig *dcp = &tga_console_dc;
    697 	long defattr;
    698 
    699 	tga_getdevconfig(memt, pc,
    700 	    pci_make_tag(pc, bus, device, function), dcp);
    701 
    702 	/* sanity checks */
    703 	if (dcp->dc_vaddr == NULL)
    704 		panic("tga_console(%d, %d): couldn't map memory space",
    705 		    device, function);
    706 	if (dcp->dc_tgaconf == NULL)
    707 		panic("tga_console(%d, %d): unknown board configuration",
    708 		    device, function);
    709 
    710 	/*
    711 	 * Initialize the RAMDAC but DO NOT allocate any private storage.
    712 	 * Initialization includes disabling cursor, setting a sane
    713 	 * colormap, etc.  It will be reinitialized in tgaattach().
    714 	 */
    715 	if (dcp->dc_tga2)
    716 		bt485_cninit(dcp, tga_sched_update, tga2_ramdac_wr,
    717 		    tga2_ramdac_rd);
    718 	else {
    719 		if (dcp->dc_tgaconf->ramdac_funcs == bt485_funcs)
    720 			bt485_cninit(dcp, tga_sched_update, tga_ramdac_wr,
    721 				tga_ramdac_rd);
    722 		else {
    723 			bt463_cninit(dcp, tga_sched_update, tga_bt463_wr,
    724 				tga_bt463_rd);
    725 		}
    726 	}
    727 	dcp->dc_rinfo.ri_ops.alloc_attr(&dcp->dc_rinfo, 0, 0, 0, &defattr);
    728 	wsdisplay_cnattach(&tga_stdscreen, &dcp->dc_rinfo, 0, 0, defattr);
    729 
    730 	return(0);
    731 }
    732 
    733 /*
    734  * Functions to blank and unblank the display.
    735  */
    736 static void
    737 tga_blank(dc)
    738 	struct tga_devconfig *dc;
    739 {
    740 
    741 	if (!dc->dc_blanked) {
    742 		dc->dc_blanked = 1;
    743 		/* XXX */
    744 		TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | VVR_BLANK);
    745 	}
    746 }
    747 
    748 static void
    749 tga_unblank(dc)
    750 	struct tga_devconfig *dc;
    751 {
    752 
    753 	if (dc->dc_blanked) {
    754 		dc->dc_blanked = 0;
    755 		/* XXX */
    756 		TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~VVR_BLANK);
    757 	}
    758 }
    759 
    760 /*
    761  * Functions to manipulate the built-in cursor handing hardware.
    762  */
    763 int
    764 tga_builtin_set_cursor(dc, cursorp)
    765 	struct tga_devconfig *dc;
    766 	struct wsdisplay_cursor *cursorp;
    767 {
    768 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    769 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    770 	int count, error, v;
    771 
    772 	v = cursorp->which;
    773 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    774 		error = dcrf->ramdac_check_curcmap(dcrc, cursorp);
    775 		if (error)
    776 			return (error);
    777 	}
    778 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    779 		if ((u_int)cursorp->size.x != 64 ||
    780 		    (u_int)cursorp->size.y > 64)
    781 			return (EINVAL);
    782 		/* The cursor is 2 bits deep, and there is no mask */
    783 		count = (cursorp->size.y * 64 * 2) / NBBY;
    784 		if (!uvm_useracc(cursorp->image, count, B_READ))
    785 			return (EFAULT);
    786 	}
    787 	if (v & WSDISPLAY_CURSOR_DOHOT)		/* not supported */
    788 		return EINVAL;
    789 
    790 	/* parameters are OK; do it */
    791 	if (v & WSDISPLAY_CURSOR_DOCUR) {
    792 		if (cursorp->enable)
    793 			/* XXX */
    794 			TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 0x04);
    795 		else
    796 			/* XXX */
    797 			TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~0x04);
    798 	}
    799 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    800 		TGAWREG(dc, TGA_REG_CXYR,
    801 				((cursorp->pos.y & 0xfff) << 12) | (cursorp->pos.x & 0xfff));
    802 	}
    803 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    804 		/* can't fail. */
    805 		dcrf->ramdac_set_curcmap(dcrc, cursorp);
    806 	}
    807 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    808 		count = ((64 * 2) / NBBY) * cursorp->size.y;
    809 		TGAWREG(dc, TGA_REG_CCBR,
    810 		    (TGARREG(dc, TGA_REG_CCBR) & ~0xfc00) | (cursorp->size.y << 10));
    811 		copyin(cursorp->image, (char *)(dc->dc_vaddr +
    812 		    (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
    813 		    count);				/* can't fail. */
    814 	}
    815 	return (0);
    816 }
    817 
    818 int
    819 tga_builtin_get_cursor(dc, cursorp)
    820 	struct tga_devconfig *dc;
    821 	struct wsdisplay_cursor *cursorp;
    822 {
    823 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    824 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    825 	int count, error;
    826 
    827 	cursorp->which = WSDISPLAY_CURSOR_DOALL &
    828 	    ~(WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOCMAP);
    829 	cursorp->enable = (TGARREG(dc, TGA_REG_VVVR) & 0x04) != 0;
    830 	cursorp->pos.x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
    831 	cursorp->pos.y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
    832 	cursorp->size.x = 64;
    833 	cursorp->size.y = (TGARREG(dc, TGA_REG_CCBR) >> 10) & 0x3f;
    834 
    835 	if (cursorp->image != NULL) {
    836 		count = (cursorp->size.y * 64 * 2) / NBBY;
    837 		error = copyout((char *)(dc->dc_vaddr +
    838 		      (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
    839 		    cursorp->image, count);
    840 		if (error)
    841 			return (error);
    842 		/* No mask */
    843 	}
    844 	error = dcrf->ramdac_get_curcmap(dcrc, cursorp);
    845 	return (error);
    846 }
    847 
    848 int
    849 tga_builtin_set_curpos(dc, curposp)
    850 	struct tga_devconfig *dc;
    851 	struct wsdisplay_curpos *curposp;
    852 {
    853 
    854 	TGAWREG(dc, TGA_REG_CXYR,
    855 	    ((curposp->y & 0xfff) << 12) | (curposp->x & 0xfff));
    856 	return (0);
    857 }
    858 
    859 int
    860 tga_builtin_get_curpos(dc, curposp)
    861 	struct tga_devconfig *dc;
    862 	struct wsdisplay_curpos *curposp;
    863 {
    864 
    865 	curposp->x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
    866 	curposp->y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
    867 	return (0);
    868 }
    869 
    870 int
    871 tga_builtin_get_curmax(dc, curposp)
    872 	struct tga_devconfig *dc;
    873 	struct wsdisplay_curpos *curposp;
    874 {
    875 
    876 	curposp->x = curposp->y = 64;
    877 	return (0);
    878 }
    879 
    880 /*
    881  * Copy columns (characters) in a row (line).
    882  */
    883 static void
    884 tga_copycols(id, row, srccol, dstcol, ncols)
    885 	void *id;
    886 	int row, srccol, dstcol, ncols;
    887 {
    888 	struct rasops_info *ri = id;
    889 	int y, srcx, dstx, nx;
    890 
    891 	y = ri->ri_font->fontheight * row;
    892 	srcx = ri->ri_font->fontwidth * srccol;
    893 	dstx = ri->ri_font->fontwidth * dstcol;
    894 	nx = ri->ri_font->fontwidth * ncols;
    895 
    896 	tga_rop(ri, dstx, y,
    897 	    nx, ri->ri_font->fontheight, RAS_SRC,
    898 	    ri, srcx, y);
    899 }
    900 
    901 /*
    902  * Copy rows (lines).
    903  */
    904 static void
    905 tga_copyrows(id, srcrow, dstrow, nrows)
    906 	void *id;
    907 	int srcrow, dstrow, nrows;
    908 {
    909 	struct rasops_info *ri = id;
    910 	int srcy, dsty, ny;
    911 
    912 	srcy = ri->ri_font->fontheight * srcrow;
    913 	dsty = ri->ri_font->fontheight * dstrow;
    914 	ny = ri->ri_font->fontheight * nrows;
    915 
    916 	tga_rop(ri, 0, dsty,
    917 	    ri->ri_emuwidth, ny, RAS_SRC,
    918 	    ri, 0, srcy);
    919 }
    920 
    921 /* Do we need the src? */
    922 static int needsrc[16] = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 };
    923 
    924 /* A mapping between our API and the TGA card */
    925 static int map_rop[16] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6,
    926 	0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
    927 };
    928 
    929 /*
    930  *  Generic TGA raster op.
    931  *   This covers all possible raster ops, and
    932  *   clips the sizes and all of that.
    933  */
    934 static int
    935 tga_rop(dst, dx, dy, w, h, rop, src, sx, sy)
    936 	struct rasops_info *dst;
    937 	int dx, dy, w, h, rop;
    938 	struct rasops_info *src;
    939 	int sx, sy;
    940 {
    941 	if (!dst)
    942 		return -1;
    943 	if (needsrc[RAS_GETOP(rop)]) {
    944 		if (src == NULL)
    945 			return -1;	/* We want a src */
    946 		/* Clip against src */
    947 		if (sx < 0) {
    948 			w += sx;
    949 			sx = 0;
    950 		}
    951 		if (sy < 0) {
    952 			h += sy;
    953 			sy = 0;
    954 		}
    955 		if (sx + w > src->ri_emuwidth)
    956 			w = src->ri_emuwidth - sx;
    957 		if (sy + h > src->ri_emuheight)
    958 			h = src->ri_emuheight - sy;
    959 	} else {
    960 		if (src != NULL)
    961 			return -1;	/* We need no src */
    962 	}
    963 	/* Clip against dst.  We modify src regardless of using it,
    964 	 * since it really doesn't matter.
    965 	 */
    966 	if (dx < 0) {
    967 		w += dx;
    968 		sx -= dx;
    969 		dx = 0;
    970 	}
    971 	if (dy < 0) {
    972 		h += dy;
    973 		sy -= dy;
    974 		dy = 0;
    975 	}
    976 	if (dx + w > dst->ri_emuwidth)
    977 		w = dst->ri_emuwidth - dx;
    978 	if (dy + h > dst->ri_emuheight)
    979 		h = dst->ri_emuheight - dy;
    980 	if (w <= 0 || h <= 0)
    981 		return 0;	/* Vacuously true; */
    982 	if (!src) {
    983 		/* XXX Punt! */
    984 		return -1;
    985 	}
    986 	return tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy);
    987 }
    988 
    989 
    990 
    991 /*
    992  * Video to Video raster ops.
    993  * This function deals with all raster ops that have a src and dst
    994  * that are on the card.
    995  */
    996 static int
    997 tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy)
    998 	struct rasops_info *dst;
    999 	int dx, dy, w, h, rop;
   1000 	struct rasops_info *src;
   1001 	int sx, sy;
   1002 {
   1003 	struct tga_devconfig *dc = (struct tga_devconfig *)dst->ri_hw;
   1004 	int srcb, dstb;
   1005 	int x, y;
   1006 	int xstart, xend, xdir, xinc;
   1007 	int ystart, yend, ydir, yinc;
   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 	if (sy >= dy) {
   1023 		ystart = 0;
   1024 		yend = h;
   1025 		ydir = 1;
   1026 	} else {
   1027 		ystart = h;
   1028 		yend = 0;
   1029 		ydir = -1;
   1030 	}
   1031 	if (sx >= dx) {
   1032 		xstart = 0;
   1033 		xend = w * (dst->ri_depth / 8);
   1034 		xdir = 1;
   1035 	} else {
   1036 		xstart = w * (dst->ri_depth / 8);
   1037 		xend = 0;
   1038 		xdir = -1;
   1039 	}
   1040 	xinc = xdir * 4 * 64;
   1041 	yinc = ydir * dst->ri_stride;
   1042 	ystart *= dst->ri_stride;
   1043 	yend *= dst->ri_stride;
   1044 	srcb = offset + (sy + src->ri_yorigin) * src->ri_stride +
   1045 		            (sx + src->ri_xorigin) * (src->ri_depth/8);
   1046 	dstb = offset + (dy + dst->ri_yorigin) * dst->ri_stride +
   1047 		            (dx + dst->ri_xorigin ) * (dst->ri_depth/8);
   1048 	TGAWALREG(dc, TGA_REG_GMOR, 3, 0x0007); /* Copy mode */
   1049 	TGAWALREG(dc, TGA_REG_GOPR, 3, map_rop[rop]);	/* Set up the op */
   1050 	for (y = ystart; (ydir * y) < (ydir * yend); y += yinc) {
   1051 		for (x = xstart; (xdir * x) < (xdir * xend); x += xinc) {
   1052 		  /* XXX XXX Eight writes to different addresses should fill
   1053 		   * XXX XXX up the write buffers on 21064 and 21164 chips,
   1054 		   * XXX XXX but later CPUs might have larger write buffers which
   1055 		   * XXX XXX require further unrolling of this loop, or the
   1056 		   * XXX XXX insertion of memory barriers.
   1057 		   */
   1058 			TGAWALREG(dc, TGA_REG_GCSR, 0, srcb + y + x + 3 * 64);
   1059 			TGAWALREG(dc, TGA_REG_GCDR, 0, dstb + y + x + 3 * 64);
   1060 			TGAWALREG(dc, TGA_REG_GCSR, 1, srcb + y + x + 2 * 64);
   1061 			TGAWALREG(dc, TGA_REG_GCDR, 1, dstb + y + x + 2 * 64);
   1062 			TGAWALREG(dc, TGA_REG_GCSR, 2, srcb + y + x + 1 * 64);
   1063 			TGAWALREG(dc, TGA_REG_GCDR, 2, dstb + y + x + 1 * 64);
   1064 			TGAWALREG(dc, TGA_REG_GCSR, 3, srcb + y + x + 0 * 64);
   1065 			TGAWALREG(dc, TGA_REG_GCDR, 3, dstb + y + x + 0 * 64);
   1066 		}
   1067 	}
   1068 	TGAWALREG(dc, TGA_REG_GOPR, 0, 0x0003); /* op -> dst = src */
   1069 	TGAWALREG(dc, TGA_REG_GMOR, 0, 0x0000); /* Simple mode */
   1070 	return 0;
   1071 }
   1072 
   1073 
   1074 void tga_putchar (c, row, col, uc, attr)
   1075 	void *c;
   1076 	int row, col;
   1077 	u_int uc;
   1078 	long attr;
   1079 {
   1080 	struct rasops_info *ri = c;
   1081 	struct tga_devconfig *dc = ri->ri_hw;
   1082 	int fs, height, width;
   1083 	u_char *fr;
   1084 	int32_t *rp;
   1085 
   1086 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
   1087 
   1088 	height = ri->ri_font->fontheight;
   1089 	width = ri->ri_font->fontwidth;
   1090 
   1091 	uc -= ri->ri_font->firstchar;
   1092 	fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
   1093 	fs = ri->ri_font->stride;
   1094 
   1095 	/* Set foreground and background color. XXX memoize this somehow?
   1096 	 * The rasops code has already expanded the color entry to 32 bits
   1097 	 * for us, even for 8-bit displays, so we don't have to do anything.
   1098 	 */
   1099 	TGAWREG(dc, TGA_REG_GFGR, ri->ri_devcmap[(attr >> 24) & 15]);
   1100 	TGAWREG(dc, TGA_REG_GBGR, ri->ri_devcmap[(attr >> 16) & 15]);
   1101 
   1102 	/* Set raster operation to "copy"... */
   1103 	if (ri->ri_depth == 8)
   1104 		TGAWREG(dc, TGA_REG_GOPR, 0x3);
   1105 	else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
   1106 		TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
   1107 
   1108 	/* Set which pixels we're drawing (of a possible 32). */
   1109 	TGAWREG(dc, TGA_REG_GPXR_P, (1 << width) - 1);
   1110 
   1111 	/* Set drawing mode to opaque stipple. */
   1112 	TGAWREG(dc, TGA_REG_GMOR, 0x1);
   1113 
   1114 	/* Insert write barrier before actually sending data */
   1115 	/* XXX Abuses the fact that there is only one write barrier on Alphas */
   1116 	TGAREGWB(dc, TGA_REG_GMOR, 1);
   1117 
   1118 	while(height--) {
   1119 		/* The actual stipple write */
   1120 		*rp = fr[0] | (fr[1] << 8) | (fr[2] << 16) | (fr[3] << 24);
   1121 
   1122 		fr += fs;
   1123 		rp = (int32_t *)((caddr_t)rp + ri->ri_stride);
   1124 	}
   1125 
   1126 	/* Do underline */
   1127 	if ((attr & 1) != 0) {
   1128 		rp = (int32_t *)((caddr_t)rp - (ri->ri_stride << 1));
   1129 		*rp = 0xffffffff;
   1130 	}
   1131 
   1132 	/* Set grapics mode back to normal. */
   1133 	TGAWREG(dc, TGA_REG_GMOR, 0);
   1134 	TGAWREG(dc, TGA_REG_GPXR_P, 0xffffffff);
   1135 
   1136 }
   1137 
   1138 static void
   1139 tga_eraserows(c, row, num, attr)
   1140 	void *c;
   1141 	int row, num;
   1142 	long attr;
   1143 {
   1144 	struct rasops_info *ri = c;
   1145 	struct tga_devconfig *dc = ri->ri_hw;
   1146 	int32_t color, lines, pixels;
   1147 	int32_t *rp;
   1148 
   1149 	color = ri->ri_devcmap[(attr >> 16) & 15];
   1150 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale);
   1151 	lines = num * ri->ri_font->fontheight;
   1152 	pixels = ri->ri_emuwidth - 1;
   1153 
   1154 	/* Set fill color in block-color registers */
   1155 	TGAWREG(dc, TGA_REG_GBCR0, color);
   1156 	TGAWREG(dc, TGA_REG_GBCR1, color);
   1157 	if (ri->ri_depth != 8) {
   1158 		TGAWREG(dc, TGA_REG_GBCR2, color);
   1159 		TGAWREG(dc, TGA_REG_GBCR3, color);
   1160 		TGAWREG(dc, TGA_REG_GBCR4, color);
   1161 		TGAWREG(dc, TGA_REG_GBCR5, color);
   1162 		TGAWREG(dc, TGA_REG_GBCR6, color);
   1163 		TGAWREG(dc, TGA_REG_GBCR7, color);
   1164 	}
   1165 
   1166 	/* Set raster operation to "copy"... */
   1167 	if (ri->ri_depth == 8)
   1168 		TGAWREG(dc, TGA_REG_GOPR, 0x3);
   1169 	else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
   1170 		TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
   1171 
   1172 	/* Set which pixels we're drawing (of a possible 32). */
   1173 	TGAWREG(dc, TGA_REG_GDAR, 0xffffffff);
   1174 
   1175 	/* Set drawing mode to block fill. */
   1176 	TGAWREG(dc, TGA_REG_GMOR, 0x2d);
   1177 
   1178 	/* Insert write barrier before actually sending data */
   1179 	/* XXX Abuses the fact that there is only one write barrier on Alphas */
   1180 	TGAREGWB(dc, TGA_REG_GMOR, 1);
   1181 
   1182 	while (lines--) {
   1183 		*rp = pixels;
   1184 		rp = (int32_t *)((caddr_t)rp + ri->ri_stride);
   1185 	}
   1186 
   1187 	/* Set grapics mode back to normal. */
   1188 	TGAWREG(dc, TGA_REG_GMOR, 0);
   1189 
   1190 }
   1191 
   1192 static void
   1193 tga_erasecols (c, row, col, num, attr)
   1194 void *c;
   1195 int row, col, num;
   1196 long attr;
   1197 {
   1198 	struct rasops_info *ri = c;
   1199 	struct tga_devconfig *dc = ri->ri_hw;
   1200 	int32_t color, lines, pixels;
   1201 	int32_t *rp;
   1202 
   1203 	color = ri->ri_devcmap[(attr >> 16) & 15];
   1204 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
   1205 	lines = ri->ri_font->fontheight;
   1206 	pixels = (num * ri->ri_font->fontwidth) - 1;
   1207 
   1208 	/* Set fill color in block-color registers */
   1209 	TGAWREG(dc, TGA_REG_GBCR0, color);
   1210 	TGAWREG(dc, TGA_REG_GBCR1, color);
   1211 	if (ri->ri_depth != 8) {
   1212 		TGAWREG(dc, TGA_REG_GBCR2, color);
   1213 		TGAWREG(dc, TGA_REG_GBCR3, color);
   1214 		TGAWREG(dc, TGA_REG_GBCR4, color);
   1215 		TGAWREG(dc, TGA_REG_GBCR5, color);
   1216 		TGAWREG(dc, TGA_REG_GBCR6, color);
   1217 		TGAWREG(dc, TGA_REG_GBCR7, color);
   1218 	}
   1219 
   1220 	/* Set raster operation to "copy"... */
   1221 	if (ri->ri_depth == 8)
   1222 		TGAWREG(dc, TGA_REG_GOPR, 0x3);
   1223 	else /* ... and in 24-bit mode, set the destination bitmap to 24-bit. */
   1224 		TGAWREG(dc, TGA_REG_GOPR, 0x3 | (0x3 << 8));
   1225 
   1226 	/* Set which pixels we're drawing (of a possible 32). */
   1227 	TGAWREG(dc, TGA_REG_GDAR, 0xffffffff);
   1228 
   1229 	/* Set drawing mode to block fill. */
   1230 	TGAWREG(dc, TGA_REG_GMOR, 0x2d);
   1231 
   1232 	/* Insert write barrier before actually sending data */
   1233 	/* XXX Abuses the fact that there is only one write barrier on Alphas */
   1234 	TGAREGWB(dc, TGA_REG_GMOR, 1);
   1235 
   1236 	while (lines--) {
   1237 		*rp = pixels;
   1238 		rp = (int32_t *)((caddr_t)rp + ri->ri_stride);
   1239 	}
   1240 
   1241 	/* Set grapics mode back to normal. */
   1242 	TGAWREG(dc, TGA_REG_GMOR, 0);
   1243 }
   1244 
   1245 
   1246 static void
   1247 tga_ramdac_wr(v, btreg, val)
   1248 	void *v;
   1249 	u_int btreg;
   1250 	u_int8_t val;
   1251 {
   1252 	struct tga_devconfig *dc = v;
   1253 
   1254 	if (btreg > BT485_REG_MAX)
   1255 		panic("tga_ramdac_wr: reg %d out of range\n", btreg);
   1256 
   1257 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 9) | (0 << 8 ) | val); /* XXX */
   1258 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1259 }
   1260 
   1261 static void
   1262 tga2_ramdac_wr(v, btreg, val)
   1263 	void *v;
   1264 	u_int btreg;
   1265 	u_int8_t val;
   1266 {
   1267 	struct tga_devconfig *dc = v;
   1268 	bus_space_handle_t ramdac;
   1269 
   1270 	if (btreg > BT485_REG_MAX)
   1271 		panic("tga_ramdac_wr: reg %d out of range\n", btreg);
   1272 
   1273 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
   1274 		(0xe << 12) + (btreg << 8), 4, &ramdac);
   1275 	bus_space_write_4(dc->dc_memt, ramdac, 0, val & 0xff);
   1276 	bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_WRITE);
   1277 }
   1278 
   1279 static u_int8_t
   1280 tga_bt463_rd(v, btreg)
   1281 	void *v;
   1282 	u_int btreg;
   1283 {
   1284 	struct tga_devconfig *dc = v;
   1285 	tga_reg_t rdval;
   1286 
   1287 	/*
   1288 	 * Strobe CE# (high->low->high) since status and data are latched on
   1289 	 * the falling and rising edges (repsectively) of this active-low signal.
   1290 	 */
   1291 
   1292 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1293 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 1);
   1294 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1295 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 0);
   1296 
   1297 	TGAREGRB(dc, TGA_REG_EPSR, 1);
   1298 
   1299 	rdval = TGARREG(dc, TGA_REG_EPDR);
   1300 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1301 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 2) | 2 | 1);
   1302 
   1303 	return (rdval >> 16) & 0xff;
   1304 }
   1305 
   1306 static void
   1307 tga_bt463_wr(v, btreg, val)
   1308 	void *v;
   1309 	u_int btreg;
   1310 	u_int8_t val;
   1311 {
   1312 	struct tga_devconfig *dc = v;
   1313 
   1314 	/*
   1315 	 * In spite of the 21030 documentation, to set the MPU bus bits for
   1316 	 * a write, you set them in the upper bits of EPDR, not EPSR.
   1317 	 */
   1318 
   1319 	/*
   1320 	 * Strobe CE# (high->low->high) since status and data are latched on
   1321 	 * the falling and rising edges of this active-low signal.
   1322 	 */
   1323 
   1324 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1325 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x100 | val);
   1326 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1327 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x000 | val);
   1328 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1329 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 10) | 0x100 | val);
   1330 
   1331 }
   1332 
   1333 static u_int8_t
   1334 tga_ramdac_rd(v, btreg)
   1335 	void *v;
   1336 	u_int btreg;
   1337 {
   1338 	struct tga_devconfig *dc = v;
   1339 	tga_reg_t rdval;
   1340 
   1341 	if (btreg > BT485_REG_MAX)
   1342 		panic("tga_ramdac_rd: reg %d out of range\n", btreg);
   1343 
   1344 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 1) | 0x1); /* XXX */
   1345 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1346 
   1347 	rdval = TGARREG(dc, TGA_REG_EPDR);
   1348 	return (rdval >> 16) & 0xff;				/* XXX */
   1349 }
   1350 
   1351 static u_int8_t
   1352 tga2_ramdac_rd(v, btreg)
   1353 	void *v;
   1354 	u_int btreg;
   1355 {
   1356 	struct tga_devconfig *dc = v;
   1357 	bus_space_handle_t ramdac;
   1358 	u_int8_t retval;
   1359 
   1360 	if (btreg > BT485_REG_MAX)
   1361 		panic("tga_ramdac_rd: reg %d out of range\n", btreg);
   1362 
   1363 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
   1364 		(0xe << 12) + (btreg << 8), 4, &ramdac);
   1365 	retval = bus_space_read_4(dc->dc_memt, ramdac, 0) & 0xff;
   1366 	bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_READ);
   1367 	return retval;
   1368 }
   1369 
   1370 #include <dev/ic/decmonitors.c>
   1371 void tga2_ics9110_wr __P((
   1372 	struct tga_devconfig *dc,
   1373 	int dotclock
   1374 ));
   1375 
   1376 void
   1377 tga2_init(dc, m)
   1378 	struct tga_devconfig *dc;
   1379 	int m;
   1380 {
   1381 
   1382 	tga2_ics9110_wr(dc, decmonitors[m].dotclock);
   1383 #if 0
   1384 	TGAWREG(dc, TGA_REG_VHCR,
   1385 	     ((decmonitors[m].hbp / 4) << 21) |
   1386 	     ((decmonitors[m].hsync / 4) << 14) |
   1387 	    (((decmonitors[m].hfp - 4) / 4) << 9) |
   1388 	     ((decmonitors[m].cols + 4) / 4));
   1389 #else
   1390 	TGAWREG(dc, TGA_REG_VHCR,
   1391 	     ((decmonitors[m].hbp / 4) << 21) |
   1392 	     ((decmonitors[m].hsync / 4) << 14) |
   1393 	    (((decmonitors[m].hfp) / 4) << 9) |
   1394 	     ((decmonitors[m].cols) / 4));
   1395 #endif
   1396 	TGAWREG(dc, TGA_REG_VVCR,
   1397 	    (decmonitors[m].vbp << 22) |
   1398 	    (decmonitors[m].vsync << 16) |
   1399 	    (decmonitors[m].vfp << 11) |
   1400 	    (decmonitors[m].rows));
   1401 	TGAWREG(dc, TGA_REG_VVBR, 1);
   1402 	TGAREGRWB(dc, TGA_REG_VHCR, 3);
   1403 	TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 1);
   1404 	TGAREGRWB(dc, TGA_REG_VVVR, 1);
   1405 	TGAWREG(dc, TGA_REG_GPMR, 0xffffffff);
   1406 	TGAREGRWB(dc, TGA_REG_GPMR, 1);
   1407 }
   1408 
   1409 void
   1410 tga2_ics9110_wr(dc, dotclock)
   1411 	struct tga_devconfig *dc;
   1412 	int dotclock;
   1413 {
   1414 	bus_space_handle_t clock;
   1415 	u_int32_t valU;
   1416 	int N, M, R, V, X;
   1417 	int i;
   1418 
   1419 	switch (dotclock) {
   1420 	case 130808000:
   1421 		N = 0x40; M = 0x7; V = 0x0; X = 0x1; R = 0x1; break;
   1422 	case 119840000:
   1423 		N = 0x2d; M = 0x2b; V = 0x1; X = 0x1; R = 0x1; break;
   1424 	case 108180000:
   1425 		N = 0x11; M = 0x9; V = 0x1; X = 0x1; R = 0x2; break;
   1426 	case 103994000:
   1427 		N = 0x6d; M = 0xf; V = 0x0; X = 0x1; R = 0x1; break;
   1428 	case 175000000:
   1429 		N = 0x5F; M = 0x3E; V = 0x1; X = 0x1; R = 0x1; break;
   1430 	case  75000000:
   1431 		N = 0x6e; M = 0x15; V = 0x0; X = 0x1; R = 0x1; break;
   1432 	case  74000000:
   1433 		N = 0x2a; M = 0x41; V = 0x1; X = 0x1; R = 0x1; break;
   1434 	case  69000000:
   1435 		N = 0x35; M = 0xb; V = 0x0; X = 0x1; R = 0x1; break;
   1436 	case  65000000:
   1437 		N = 0x6d; M = 0x0c; V = 0x0; X = 0x1; R = 0x2; break;
   1438 	case  50000000:
   1439 		N = 0x37; M = 0x3f; V = 0x1; X = 0x1; R = 0x2; break;
   1440 	case  40000000:
   1441 		N = 0x5f; M = 0x11; V = 0x0; X = 0x1; R = 0x2; break;
   1442 	case  31500000:
   1443 		N = 0x16; M = 0x05; V = 0x0; X = 0x1; R = 0x2; break;
   1444 	case  25175000:
   1445 		N = 0x66; M = 0x1d; V = 0x0; X = 0x1; R = 0x2; break;
   1446 	case 135000000:
   1447 		N = 0x42; M = 0x07; V = 0x0; X = 0x1; R = 0x1; break;
   1448 	case 110000000:
   1449 		N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
   1450 	case 202500000:
   1451 		N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
   1452 	default:
   1453 		panic("unrecognized clock rate %d\n", dotclock);
   1454 	}
   1455 
   1456 	/* XXX -- hard coded, bad */
   1457 	valU  = N | ( M << 7 ) | (V << 14);
   1458 	valU |= (X << 15) | (R << 17);
   1459 	valU |= 0x17 << 19;
   1460 
   1461 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
   1462 	    TGA2_MEM_CLOCK + (0xe << 12), 4, &clock); /* XXX */
   1463 
   1464 	for (i=24; i>0; i--) {
   1465 		u_int32_t       writeval;
   1466 
   1467 		writeval = valU & 0x1;
   1468 		if (i == 1)
   1469 			writeval |= 0x2;
   1470 		valU >>= 1;
   1471 		bus_space_write_4(dc->dc_memt, clock, 0, writeval);
   1472 		bus_space_barrier(dc->dc_memt, clock, 0, 4, BUS_SPACE_BARRIER_WRITE);
   1473         }
   1474 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
   1475 	    TGA2_MEM_CLOCK + (0xe << 12) + (0x1 << 11) + (0x1 << 11), 4,
   1476 		&clock); /* XXX */
   1477 	bus_space_write_4(dc->dc_memt, clock, 0, 0x0);
   1478 	bus_space_barrier(dc->dc_memt, clock, 0, 0, BUS_SPACE_BARRIER_WRITE);
   1479 }
   1480