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