Home | History | Annotate | Line # | Download | only in pci
tga.c revision 1.21
      1  1.21   nathanw /* $NetBSD: tga.c,v 1.21 2000/03/12 05:32:29 nathanw 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.1  drochner 
     39   1.8   thorpej #include <vm/vm.h>
     40   1.8   thorpej 
     41   1.1  drochner #include <machine/bus.h>
     42   1.1  drochner #include <machine/intr.h>
     43   1.1  drochner 
     44   1.1  drochner #include <dev/pci/pcireg.h>
     45   1.1  drochner #include <dev/pci/pcivar.h>
     46   1.1  drochner #include <dev/pci/pcidevs.h>
     47   1.1  drochner #include <dev/pci/tgareg.h>
     48   1.1  drochner #include <dev/pci/tgavar.h>
     49   1.1  drochner #include <dev/ic/bt485reg.h>
     50  1.17     elric #include <dev/ic/bt485var.h>
     51   1.1  drochner 
     52   1.1  drochner #include <dev/rcons/raster.h>
     53   1.1  drochner #include <dev/wscons/wsconsio.h>
     54   1.1  drochner #include <dev/wscons/wscons_raster.h>
     55   1.1  drochner #include <dev/wscons/wsdisplayvar.h>
     56   1.1  drochner 
     57   1.1  drochner #ifdef __alpha__
     58   1.1  drochner #include <machine/pte.h>
     59   1.1  drochner #endif
     60   1.1  drochner 
     61   1.1  drochner int	tgamatch __P((struct device *, struct cfdata *, void *));
     62   1.1  drochner void	tgaattach __P((struct device *, struct device *, void *));
     63   1.1  drochner int	tgaprint __P((void *, const char *));
     64   1.1  drochner 
     65   1.1  drochner struct cfattach tga_ca = {
     66   1.1  drochner 	sizeof(struct tga_softc), tgamatch, tgaattach,
     67   1.1  drochner };
     68   1.1  drochner 
     69  1.21   nathanw int	tga_identify __P((struct tga_devconfig *));
     70   1.1  drochner const struct tga_conf *tga_getconf __P((int));
     71   1.1  drochner void	tga_getdevconfig __P((bus_space_tag_t memt, pci_chipset_tag_t pc,
     72  1.19     elric 	    pcitag_t tag, struct tga_devconfig *dc));
     73   1.1  drochner 
     74   1.1  drochner struct tga_devconfig tga_console_dc;
     75   1.1  drochner 
     76  1.14      ross int tga_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     77  1.14      ross int tga_mmap __P((void *, off_t, int));
     78  1.14      ross static void tga_copyrows __P((void *, int, int, int));
     79  1.14      ross static void tga_copycols __P((void *, int, int, int, int));
     80  1.14      ross static int tga_alloc_screen __P((void *, const struct wsscreen_descr *,
     81  1.14      ross 				      void **, int *, int *, long *));
     82  1.14      ross static void tga_free_screen __P((void *, void *));
     83  1.15  drochner static int tga_show_screen __P((void *, void *, int,
     84  1.15  drochner 				void (*) (void *, int, int), void *));
     85  1.14      ross static int tga_rop __P((struct raster *, int, int, int, int, int,
     86  1.14      ross 	struct raster *, int, int));
     87  1.14      ross static int tga_rop_nosrc __P((struct raster *, int, int, int, int, int));
     88  1.14      ross static int tga_rop_htov __P((struct raster *, int, int, int, int,
     89  1.14      ross 	int, struct raster *, int, int ));
     90  1.14      ross static int tga_rop_vtov __P((struct raster *, int, int, int, int,
     91  1.14      ross 	int, struct raster *, int, int ));
     92  1.17     elric void tga2_init __P((struct tga_devconfig *, int));
     93  1.17     elric 
     94  1.17     elric /* RAMDAC interface functions */
     95  1.17     elric int		tga_sched_update __P((void *, void (*)(void *)));
     96  1.17     elric void		tga_ramdac_wr __P((void *, u_int, u_int8_t));
     97  1.17     elric u_int8_t 	tga_ramdac_rd __P((void *, u_int));
     98  1.17     elric void		tga2_ramdac_wr __P((void *, u_int, u_int8_t));
     99  1.17     elric u_int8_t 	tga2_ramdac_rd __P((void *, u_int));
    100  1.17     elric 
    101  1.17     elric /* Interrupt handler */
    102  1.17     elric int	tga_intr __P((void *));
    103  1.14      ross 
    104   1.1  drochner struct wsdisplay_emulops tga_emulops = {
    105   1.1  drochner 	rcons_cursor,			/* could use hardware cursor; punt */
    106   1.6  drochner 	rcons_mapchar,
    107   1.5  drochner 	rcons_putchar,
    108  1.14      ross 	tga_copycols,
    109   1.1  drochner 	rcons_erasecols,
    110  1.14      ross 	tga_copyrows,
    111   1.1  drochner 	rcons_eraserows,
    112   1.4  drochner 	rcons_alloc_attr
    113   1.1  drochner };
    114   1.1  drochner 
    115   1.1  drochner struct wsscreen_descr tga_stdscreen = {
    116   1.1  drochner 	"std",
    117   1.4  drochner 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
    118   1.1  drochner 	&tga_emulops,
    119   1.4  drochner 	0, 0,
    120   1.4  drochner 	WSSCREEN_REVERSE
    121   1.1  drochner };
    122   1.1  drochner 
    123   1.1  drochner const struct wsscreen_descr *_tga_scrlist[] = {
    124   1.1  drochner 	&tga_stdscreen,
    125   1.1  drochner 	/* XXX other formats, graphics screen? */
    126   1.1  drochner };
    127   1.1  drochner 
    128   1.1  drochner struct wsscreen_list tga_screenlist = {
    129   1.1  drochner 	sizeof(_tga_scrlist) / sizeof(struct wsscreen_descr *), _tga_scrlist
    130   1.1  drochner };
    131   1.1  drochner 
    132   1.1  drochner struct wsdisplay_accessops tga_accessops = {
    133   1.1  drochner 	tga_ioctl,
    134   1.1  drochner 	tga_mmap,
    135   1.1  drochner 	tga_alloc_screen,
    136   1.1  drochner 	tga_free_screen,
    137   1.1  drochner 	tga_show_screen,
    138  1.11  drochner 	0 /* load_font */
    139   1.1  drochner };
    140   1.1  drochner 
    141   1.1  drochner void	tga_blank __P((struct tga_devconfig *));
    142   1.1  drochner void	tga_unblank __P((struct tga_devconfig *));
    143   1.1  drochner 
    144   1.1  drochner int
    145   1.1  drochner tgamatch(parent, match, aux)
    146   1.1  drochner 	struct device *parent;
    147   1.1  drochner 	struct cfdata *match;
    148   1.1  drochner 	void *aux;
    149   1.1  drochner {
    150   1.1  drochner 	struct pci_attach_args *pa = aux;
    151   1.1  drochner 
    152  1.17     elric 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC)
    153   1.1  drochner 		return (0);
    154   1.1  drochner 
    155  1.17     elric 	switch (PCI_PRODUCT(pa->pa_id)) {
    156  1.17     elric 	case PCI_PRODUCT_DEC_21030:
    157  1.17     elric 	case PCI_PRODUCT_DEC_PBXGB:
    158  1.17     elric 		return 10;
    159  1.17     elric 	default:
    160  1.17     elric 		return 0;
    161  1.17     elric 	}
    162  1.17     elric 	return (0);
    163   1.1  drochner }
    164   1.1  drochner 
    165   1.1  drochner void
    166  1.19     elric tga_getdevconfig(memt, pc, tag, dc)
    167   1.1  drochner 	bus_space_tag_t memt;
    168   1.1  drochner 	pci_chipset_tag_t pc;
    169   1.1  drochner 	pcitag_t tag;
    170   1.1  drochner 	struct tga_devconfig *dc;
    171   1.1  drochner {
    172   1.1  drochner 	const struct tga_conf *tgac;
    173   1.1  drochner 	struct raster *rap;
    174   1.1  drochner 	struct rcons *rcp;
    175   1.1  drochner 	bus_size_t pcisize;
    176   1.1  drochner 	int i, flags;
    177   1.1  drochner 
    178   1.1  drochner 	dc->dc_memt = memt;
    179   1.1  drochner 
    180   1.1  drochner 	dc->dc_pcitag = tag;
    181   1.1  drochner 
    182   1.1  drochner 	/* XXX magic number */
    183   1.1  drochner 	if (pci_mapreg_info(pc, tag, 0x10,
    184   1.1  drochner 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    185   1.1  drochner 	    &dc->dc_pcipaddr, &pcisize, &flags))
    186   1.1  drochner 		return;
    187  1.16  drochner 	if ((flags & BUS_SPACE_MAP_PREFETCHABLE) == 0)		/* XXX */
    188  1.16  drochner 		panic("tga memory not prefetchable");
    189   1.1  drochner 
    190   1.1  drochner 	if (bus_space_map(memt, dc->dc_pcipaddr, pcisize,
    191  1.21   nathanw 	    BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR, &dc->dc_memh))
    192   1.1  drochner 		return;
    193  1.21   nathanw 	dc->dc_vaddr = dc->dc_memh; /* XXX Cheat-o-matic */
    194   1.1  drochner #ifdef __alpha__
    195   1.1  drochner 	dc->dc_paddr = ALPHA_K0SEG_TO_PHYS(dc->dc_vaddr);	/* XXX */
    196   1.1  drochner #endif
    197   1.1  drochner 
    198  1.21   nathanw 	bus_space_subregion(dc->dc_memt, dc->dc_memh,
    199  1.21   nathanw 						TGA_MEM_CREGS, TGA_CREGS_SIZE,
    200  1.21   nathanw 						&dc->dc_regs);
    201  1.21   nathanw 	dc->dc_tga_type = tga_identify(dc);
    202  1.17     elric 
    203   1.1  drochner 	tgac = dc->dc_tgaconf = tga_getconf(dc->dc_tga_type);
    204   1.1  drochner 	if (tgac == NULL)
    205   1.1  drochner 		return;
    206   1.1  drochner 
    207   1.1  drochner #if 0
    208   1.1  drochner 	/* XXX on the Alpha, pcisize = 4 * cspace_size. */
    209   1.1  drochner 	if (tgac->tgac_cspace_size != pcisize)			/* sanity */
    210   1.1  drochner 		panic("tga_getdevconfig: memory size mismatch?");
    211   1.1  drochner #endif
    212   1.1  drochner 
    213  1.21   nathanw 	switch (TGARREG(dc, TGA_REG_GREV) & 0xff) {
    214  1.19     elric 	case 0x01:
    215  1.19     elric 	case 0x02:
    216  1.19     elric 	case 0x03:
    217  1.19     elric 	case 0x04:
    218  1.19     elric 		dc->dc_tga2 = 0;
    219  1.19     elric 		break;
    220  1.19     elric 	case 0x20:
    221  1.19     elric 	case 0x21:
    222  1.19     elric 	case 0x22:
    223  1.19     elric 		dc->dc_tga2 = 1;
    224  1.19     elric 		break;
    225  1.19     elric 	default:
    226  1.19     elric 		panic("tga_getdevconfig: TGA Revision not recognized");
    227  1.19     elric 	}
    228  1.19     elric 
    229  1.19     elric 	if (dc->dc_tga2) {
    230  1.17     elric 		int	monitor;
    231  1.17     elric 
    232  1.21   nathanw 		monitor = (~TGARREG(dc, TGA_REG_GREV) >> 16) & 0x0f;
    233  1.17     elric 		tga2_init(dc, monitor);
    234  1.17     elric 	}
    235   1.1  drochner 
    236  1.21   nathanw 	switch (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) {		/* XXX */
    237   1.1  drochner 	case 0:
    238   1.1  drochner 		dc->dc_wid = 8192;
    239   1.1  drochner 		break;
    240   1.1  drochner 
    241   1.1  drochner 	case 1:
    242   1.1  drochner 		dc->dc_wid = 8196;
    243   1.1  drochner 		break;
    244   1.1  drochner 
    245   1.1  drochner 	default:
    246  1.21   nathanw 		dc->dc_wid = (TGARREG(dc, TGA_REG_VHCR) & 0x1ff) * 4; /* XXX */
    247   1.1  drochner 		break;
    248   1.1  drochner 	}
    249   1.1  drochner 
    250   1.1  drochner 	dc->dc_rowbytes = dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
    251   1.1  drochner 
    252  1.21   nathanw 	if ((TGARREG(dc, TGA_REG_VHCR) & 0x00000001) != 0 &&	/* XXX */
    253  1.21   nathanw 	    (TGARREG(dc, TGA_REG_VHCR) & 0x80000000) != 0) {	/* XXX */
    254   1.1  drochner 		dc->dc_wid -= 4;
    255   1.1  drochner 		/*
    256   1.1  drochner 		 * XXX XXX turning off 'odd' shouldn't be necesssary,
    257   1.1  drochner 		 * XXX XXX but i can't make X work with the weird size.
    258   1.1  drochner 		 */
    259  1.21   nathanw 		TGAWREG(dc, TGA_REG_VHCR, TGARREG(dc, TGA_REG_VHCR) & ~0x80000001);
    260   1.1  drochner 		dc->dc_rowbytes =
    261   1.1  drochner 		    dc->dc_wid * (dc->dc_tgaconf->tgac_phys_depth / 8);
    262   1.1  drochner 	}
    263   1.1  drochner 
    264  1.21   nathanw 	dc->dc_ht = (TGARREG(dc, TGA_REG_VVCR) & 0x7ff);	/* XXX */
    265   1.1  drochner 
    266   1.1  drochner 	/* XXX this seems to be what DEC does */
    267  1.21   nathanw 	TGAWREG(dc, TGA_REG_CCBR, 0);
    268  1.21   nathanw 	TGAWREG(dc, TGA_REG_VVBR, 1);
    269   1.1  drochner 	dc->dc_videobase = dc->dc_vaddr + tgac->tgac_dbuf[0] +
    270   1.1  drochner 	    1 * tgac->tgac_vvbr_units;
    271   1.1  drochner 	dc->dc_blanked = 1;
    272   1.1  drochner 	tga_unblank(dc);
    273   1.1  drochner 
    274   1.1  drochner 	/*
    275   1.1  drochner 	 * Set all bits in the pixel mask, to enable writes to all pixels.
    276   1.1  drochner 	 * It seems that the console firmware clears some of them
    277   1.1  drochner 	 * under some circumstances, which causes cute vertical stripes.
    278   1.1  drochner 	 */
    279  1.21   nathanw 	TGAWREG(dc, TGA_REG_GPXR_P, 0xffffffff);
    280   1.1  drochner 
    281   1.1  drochner 	/* clear the screen */
    282   1.1  drochner 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
    283   1.1  drochner 		*(u_int32_t *)(dc->dc_videobase + i) = 0;
    284   1.1  drochner 
    285   1.1  drochner 	/* initialize the raster */
    286   1.1  drochner 	rap = &dc->dc_raster;
    287   1.1  drochner 	rap->width = dc->dc_wid;
    288   1.1  drochner 	rap->height = dc->dc_ht;
    289   1.1  drochner 	rap->depth = tgac->tgac_phys_depth;
    290   1.1  drochner 	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
    291   1.1  drochner 	rap->pixels = (u_int32_t *)dc->dc_videobase;
    292  1.14      ross 	rap->data = (caddr_t)dc;
    293   1.1  drochner 
    294   1.1  drochner 	/* initialize the raster console blitter */
    295   1.1  drochner 	rcp = &dc->dc_rcons;
    296   1.1  drochner 	rcp->rc_sp = rap;
    297   1.1  drochner 	rcp->rc_crow = rcp->rc_ccol = -1;
    298   1.1  drochner 	rcp->rc_crowp = &rcp->rc_crow;
    299   1.1  drochner 	rcp->rc_ccolp = &rcp->rc_ccol;
    300   1.1  drochner 	rcons_init(rcp, 34, 80);
    301   1.1  drochner 
    302   1.1  drochner 	tga_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
    303   1.1  drochner 	tga_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
    304   1.1  drochner }
    305   1.1  drochner 
    306   1.1  drochner void
    307   1.1  drochner tgaattach(parent, self, aux)
    308   1.1  drochner 	struct device *parent, *self;
    309   1.1  drochner 	void *aux;
    310   1.1  drochner {
    311   1.1  drochner 	struct pci_attach_args *pa = aux;
    312   1.1  drochner 	struct tga_softc *sc = (struct tga_softc *)self;
    313   1.1  drochner 	struct wsemuldisplaydev_attach_args aa;
    314   1.1  drochner 	pci_intr_handle_t intrh;
    315   1.1  drochner 	const char *intrstr;
    316   1.1  drochner 	u_int8_t rev;
    317   1.1  drochner 	int console;
    318   1.1  drochner 
    319   1.1  drochner #ifdef __alpha__
    320   1.1  drochner 	console = (pa->pa_tag == tga_console_dc.dc_pcitag);
    321   1.1  drochner #else
    322   1.1  drochner 	console = 0;
    323   1.1  drochner #endif
    324   1.1  drochner 	if (console) {
    325   1.1  drochner 		sc->sc_dc = &tga_console_dc;
    326   1.1  drochner 		sc->nscreens = 1;
    327   1.1  drochner 	} else {
    328   1.1  drochner 		sc->sc_dc = (struct tga_devconfig *)
    329   1.1  drochner 		    malloc(sizeof(struct tga_devconfig), M_DEVBUF, M_WAITOK);
    330   1.9  drochner 		bzero(sc->sc_dc, sizeof(struct tga_devconfig));
    331  1.17     elric 		tga_getdevconfig(pa->pa_memt, pa->pa_pc, pa->pa_tag,
    332  1.19     elric 		    sc->sc_dc);
    333   1.1  drochner 	}
    334   1.1  drochner 	if (sc->sc_dc->dc_vaddr == NULL) {
    335   1.1  drochner 		printf(": couldn't map memory space; punt!\n");
    336   1.1  drochner 		return;
    337   1.1  drochner 	}
    338   1.1  drochner 
    339   1.1  drochner 	/* XXX say what's going on. */
    340   1.1  drochner 	intrstr = NULL;
    341  1.17     elric 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    342  1.17     elric 	    pa->pa_intrline, &intrh)) {
    343  1.17     elric 		printf(": couldn't map interrupt");
    344  1.17     elric 		return;
    345  1.17     elric 	}
    346  1.17     elric 	intrstr = pci_intr_string(pa->pa_pc, intrh);
    347  1.17     elric 	sc->sc_intr = pci_intr_establish(pa->pa_pc, intrh, IPL_TTY, tga_intr,
    348  1.17     elric 	    sc->sc_dc);
    349  1.17     elric 	if (sc->sc_intr == NULL) {
    350  1.17     elric 		printf(": couldn't establish interrupt");
    351  1.17     elric 		if (intrstr != NULL)
    352  1.17     elric 			printf("at %s", intrstr);
    353  1.17     elric 		printf("\n");
    354  1.17     elric 		return;
    355   1.1  drochner 	}
    356   1.1  drochner 
    357   1.1  drochner 	rev = PCI_REVISION(pa->pa_class);
    358   1.1  drochner 	switch (rev) {
    359  1.17     elric 	case 0x1:
    360  1.17     elric 	case 0x2:
    361  1.17     elric 	case 0x3:
    362  1.17     elric 		printf(": DC21030 step %c", 'A' + rev - 1);
    363  1.17     elric 		break;
    364  1.17     elric 	case 0x20:
    365  1.17     elric 		printf(": TGA2 abstract software model");
    366  1.17     elric 		break;
    367  1.19     elric 	case 0x21:
    368  1.19     elric 	case 0x22:
    369  1.17     elric 		printf(": TGA2 pass %d", rev - 0x20);
    370   1.1  drochner 		break;
    371   1.1  drochner 
    372   1.1  drochner 	default:
    373   1.1  drochner 		printf("unknown stepping (0x%x)", rev);
    374   1.1  drochner 		break;
    375   1.1  drochner 	}
    376   1.1  drochner 	printf(", ");
    377   1.1  drochner 
    378  1.17     elric 	/*
    379  1.17     elric 	 * Get RAMDAC function vectors and call the RAMDAC functions
    380  1.17     elric 	 * to allocate its private storage and pass that back to us.
    381  1.17     elric 	 */
    382  1.17     elric 	sc->sc_dc->dc_ramdac_funcs = bt485_funcs();
    383  1.19     elric 	if (!sc->sc_dc->dc_tga2) {
    384  1.17     elric 		sc->sc_dc->dc_ramdac_cookie = bt485_register(
    385  1.17     elric 		    sc->sc_dc, tga_sched_update, tga_ramdac_wr,
    386  1.17     elric 		    tga_ramdac_rd);
    387  1.17     elric 	} else {
    388  1.17     elric 		sc->sc_dc->dc_ramdac_cookie = bt485_register(
    389  1.17     elric 		    sc->sc_dc, tga_sched_update, tga2_ramdac_wr,
    390  1.17     elric 		    tga2_ramdac_rd);
    391  1.17     elric 	}
    392  1.17     elric 
    393  1.17     elric 	/*
    394  1.17     elric 	 * Initialize the RAMDAC.  Initialization includes disabling
    395  1.17     elric 	 * cursor, setting a sane colormap, etc.
    396  1.17     elric 	 */
    397  1.17     elric 	(*sc->sc_dc->dc_ramdac_funcs->ramdac_init)(sc->sc_dc->dc_ramdac_cookie);
    398  1.21   nathanw 	TGAWREG(sc->sc_dc, TGA_REG_SISR, 0x00000001); /* XXX */
    399  1.17     elric 
    400   1.1  drochner 	if (sc->sc_dc->dc_tgaconf == NULL) {
    401   1.1  drochner 		printf("unknown board configuration\n");
    402   1.1  drochner 		return;
    403   1.1  drochner 	}
    404   1.1  drochner 	printf("board type %s\n", sc->sc_dc->dc_tgaconf->tgac_name);
    405   1.1  drochner 	printf("%s: %d x %d, %dbpp, %s RAMDAC\n", sc->sc_dev.dv_xname,
    406   1.1  drochner 	    sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
    407   1.1  drochner 	    sc->sc_dc->dc_tgaconf->tgac_phys_depth,
    408  1.17     elric 	    sc->sc_dc->dc_ramdac_funcs->ramdac_name);
    409   1.1  drochner 
    410   1.1  drochner 	if (intrstr != NULL)
    411   1.1  drochner 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    412   1.1  drochner 		    intrstr);
    413   1.1  drochner 
    414   1.1  drochner 	aa.console = console;
    415   1.1  drochner 	aa.scrdata = &tga_screenlist;
    416   1.1  drochner 	aa.accessops = &tga_accessops;
    417   1.1  drochner 	aa.accesscookie = sc;
    418   1.1  drochner 
    419   1.1  drochner 	config_found(self, &aa, wsemuldisplaydevprint);
    420   1.1  drochner }
    421   1.1  drochner 
    422   1.1  drochner int
    423   1.1  drochner tga_ioctl(v, cmd, data, flag, p)
    424   1.1  drochner 	void *v;
    425   1.1  drochner 	u_long cmd;
    426   1.1  drochner 	caddr_t data;
    427   1.1  drochner 	int flag;
    428   1.1  drochner 	struct proc *p;
    429   1.1  drochner {
    430   1.1  drochner 	struct tga_softc *sc = v;
    431   1.1  drochner 	struct tga_devconfig *dc = sc->sc_dc;
    432  1.17     elric 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    433  1.17     elric 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    434   1.1  drochner 
    435   1.1  drochner 	switch (cmd) {
    436   1.1  drochner 	case WSDISPLAYIO_GTYPE:
    437   1.1  drochner 		*(u_int *)data = WSDISPLAY_TYPE_TGA;
    438   1.1  drochner 		return (0);
    439   1.1  drochner 
    440   1.1  drochner 	case WSDISPLAYIO_GINFO:
    441   1.1  drochner #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    442   1.1  drochner 		wsd_fbip->height = sc->sc_dc->dc_ht;
    443   1.1  drochner 		wsd_fbip->width = sc->sc_dc->dc_wid;
    444   1.1  drochner 		wsd_fbip->depth = sc->sc_dc->dc_tgaconf->tgac_phys_depth;
    445   1.1  drochner 		wsd_fbip->cmsize = 256;		/* XXX ??? */
    446  1.12   thorpej #undef wsd_fbip
    447   1.1  drochner 		return (0);
    448   1.1  drochner 
    449   1.1  drochner 	case WSDISPLAYIO_GETCMAP:
    450  1.17     elric 		return (*dcrf->ramdac_get_cmap)(dcrc,
    451   1.1  drochner 		    (struct wsdisplay_cmap *)data);
    452   1.1  drochner 
    453   1.1  drochner 	case WSDISPLAYIO_PUTCMAP:
    454  1.17     elric 		return (*dcrf->ramdac_set_cmap)(dcrc,
    455   1.1  drochner 		    (struct wsdisplay_cmap *)data);
    456   1.1  drochner 
    457  1.12   thorpej 	case WSDISPLAYIO_SVIDEO:
    458   1.1  drochner 		if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF)
    459   1.1  drochner 			tga_blank(sc->sc_dc);
    460   1.1  drochner 		else
    461   1.1  drochner 			tga_unblank(sc->sc_dc);
    462   1.1  drochner 		return (0);
    463   1.1  drochner 
    464  1.12   thorpej 	case WSDISPLAYIO_GVIDEO:
    465   1.1  drochner 		*(u_int *)data = dc->dc_blanked ?
    466   1.1  drochner 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    467   1.1  drochner 		return (0);
    468   1.1  drochner 
    469   1.1  drochner 	case WSDISPLAYIO_GCURPOS:
    470  1.17     elric 		return (*dcrf->ramdac_get_curpos)(dcrc,
    471   1.1  drochner 		    (struct wsdisplay_curpos *)data);
    472   1.1  drochner 
    473   1.1  drochner 	case WSDISPLAYIO_SCURPOS:
    474  1.17     elric 		return (*dcrf->ramdac_set_curpos)(dcrc,
    475   1.1  drochner 		    (struct wsdisplay_curpos *)data);
    476   1.1  drochner 
    477   1.1  drochner 	case WSDISPLAYIO_GCURMAX:
    478  1.17     elric 		return (*dcrf->ramdac_get_curmax)(dcrc,
    479   1.1  drochner 		    (struct wsdisplay_curpos *)data);
    480   1.1  drochner 
    481   1.1  drochner 	case WSDISPLAYIO_GCURSOR:
    482  1.17     elric 		return (*dcrf->ramdac_get_cursor)(dcrc,
    483   1.1  drochner 		    (struct wsdisplay_cursor *)data);
    484   1.1  drochner 
    485   1.1  drochner 	case WSDISPLAYIO_SCURSOR:
    486  1.17     elric 		return (*dcrf->ramdac_set_cursor)(dcrc,
    487   1.1  drochner 		    (struct wsdisplay_cursor *)data);
    488   1.1  drochner 	}
    489   1.1  drochner 	return (-1);
    490   1.1  drochner }
    491   1.1  drochner 
    492   1.1  drochner int
    493  1.17     elric tga_sched_update(v, f)
    494  1.17     elric 	void	*v;
    495  1.17     elric 	void	(*f) __P((void *));
    496  1.17     elric {
    497  1.17     elric 	struct tga_devconfig *dc = v;
    498  1.17     elric 
    499  1.21   nathanw 	TGAWREG(dc, TGA_REG_SISR, 0x00010000);
    500  1.17     elric 	dc->dc_ramdac_intr = f;
    501  1.17     elric 	return 0;
    502  1.17     elric }
    503  1.17     elric 
    504  1.17     elric int
    505  1.17     elric tga_intr(v)
    506  1.17     elric 	void *v;
    507  1.17     elric {
    508  1.17     elric 	struct tga_devconfig *dc = v;
    509  1.17     elric 	struct ramdac_cookie *dcrc= dc->dc_ramdac_cookie;
    510  1.17     elric 
    511  1.21   nathanw 	if ((TGARREG(dc, TGA_REG_SISR) & 0x00010001) != 0x00010001)
    512  1.17     elric 		return 0;
    513  1.17     elric 	dc->dc_ramdac_intr(dcrc);
    514  1.17     elric 	dc->dc_ramdac_intr = NULL;
    515  1.21   nathanw 	TGAWREG(dc, TGA_REG_SISR, 0x00000001);
    516  1.17     elric 	return (1);
    517  1.17     elric }
    518  1.17     elric 
    519  1.17     elric int
    520   1.1  drochner tga_mmap(v, offset, prot)
    521   1.1  drochner 	void *v;
    522   1.1  drochner 	off_t offset;
    523   1.1  drochner 	int prot;
    524   1.1  drochner {
    525   1.1  drochner 
    526   1.1  drochner 	/* XXX NEW MAPPING CODE... */
    527   1.1  drochner 
    528   1.1  drochner #ifdef __alpha__
    529   1.1  drochner 	struct tga_softc *sc = v;
    530   1.1  drochner 
    531  1.10       mrg 	if (offset >= sc->sc_dc->dc_tgaconf->tgac_cspace_size || offset < 0)
    532   1.1  drochner 		return -1;
    533   1.1  drochner 	return alpha_btop(sc->sc_dc->dc_paddr + offset);
    534   1.1  drochner #else
    535   1.1  drochner 	return (-1);
    536   1.1  drochner #endif
    537   1.1  drochner }
    538   1.1  drochner 
    539   1.1  drochner int
    540   1.4  drochner tga_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    541   1.1  drochner 	void *v;
    542   1.1  drochner 	const struct wsscreen_descr *type;
    543   1.1  drochner 	void **cookiep;
    544   1.1  drochner 	int *curxp, *curyp;
    545   1.4  drochner 	long *attrp;
    546   1.1  drochner {
    547   1.1  drochner 	struct tga_softc *sc = v;
    548   1.4  drochner 	long defattr;
    549   1.1  drochner 
    550   1.1  drochner 	if (sc->nscreens > 0)
    551   1.1  drochner 		return (ENOMEM);
    552   1.1  drochner 
    553   1.1  drochner 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
    554   1.1  drochner 	*curxp = 0;
    555   1.1  drochner 	*curyp = 0;
    556   1.4  drochner 	rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
    557   1.4  drochner 	*attrp = defattr;
    558   1.2  drochner 	sc->nscreens++;
    559   1.1  drochner 	return (0);
    560   1.1  drochner }
    561   1.1  drochner 
    562   1.1  drochner void
    563   1.1  drochner tga_free_screen(v, cookie)
    564   1.1  drochner 	void *v;
    565   1.1  drochner 	void *cookie;
    566   1.1  drochner {
    567   1.1  drochner 	struct tga_softc *sc = v;
    568   1.1  drochner 
    569   1.1  drochner 	if (sc->sc_dc == &tga_console_dc)
    570   1.1  drochner 		panic("tga_free_screen: console");
    571   1.1  drochner 
    572   1.1  drochner 	sc->nscreens--;
    573   1.1  drochner }
    574   1.1  drochner 
    575  1.15  drochner int
    576  1.15  drochner tga_show_screen(v, cookie, waitok, cb, cbarg)
    577   1.1  drochner 	void *v;
    578   1.1  drochner 	void *cookie;
    579  1.15  drochner 	int waitok;
    580  1.15  drochner 	void (*cb) __P((void *, int, int));
    581  1.15  drochner 	void *cbarg;
    582   1.1  drochner {
    583  1.15  drochner 
    584  1.15  drochner 	return (0);
    585   1.1  drochner }
    586   1.1  drochner 
    587   1.1  drochner int
    588   1.1  drochner tga_cnattach(iot, memt, pc, bus, device, function)
    589   1.1  drochner 	bus_space_tag_t iot, memt;
    590   1.1  drochner 	pci_chipset_tag_t pc;
    591   1.1  drochner 	int bus, device, function;
    592   1.1  drochner {
    593   1.1  drochner 	struct tga_devconfig *dcp = &tga_console_dc;
    594   1.4  drochner 	long defattr;
    595   1.1  drochner 
    596   1.1  drochner 	tga_getdevconfig(memt, pc,
    597  1.19     elric 	    pci_make_tag(pc, bus, device, function), dcp);
    598   1.1  drochner 
    599   1.1  drochner 	/* sanity checks */
    600   1.1  drochner 	if (dcp->dc_vaddr == NULL)
    601   1.1  drochner 		panic("tga_console(%d, %d): couldn't map memory space",
    602   1.1  drochner 		    device, function);
    603   1.1  drochner 	if (dcp->dc_tgaconf == NULL)
    604   1.1  drochner 		panic("tga_console(%d, %d): unknown board configuration",
    605   1.1  drochner 		    device, function);
    606   1.1  drochner 
    607   1.1  drochner 	/*
    608   1.1  drochner 	 * Initialize the RAMDAC but DO NOT allocate any private storage.
    609   1.1  drochner 	 * Initialization includes disabling cursor, setting a sane
    610   1.1  drochner 	 * colormap, etc.  It will be reinitialized in tgaattach().
    611   1.1  drochner 	 */
    612  1.17     elric 
    613  1.17     elric 	/* XXX -- this only works for bt485, but then we only support that,
    614  1.19     elric 	 *  currently.
    615  1.17     elric 	 */
    616  1.19     elric 	if (dcp->dc_tga2)
    617  1.19     elric 		bt485_cninit(dcp, tga_sched_update, tga2_ramdac_wr,
    618  1.19     elric 		    tga2_ramdac_rd);
    619  1.19     elric 	else
    620  1.19     elric 		bt485_cninit(dcp, tga_sched_update, tga_ramdac_wr,
    621  1.19     elric 		    tga_ramdac_rd);
    622   1.1  drochner 
    623   1.4  drochner 	rcons_alloc_attr(&dcp->dc_rcons, 0, 0, 0, &defattr);
    624   1.4  drochner 
    625   1.1  drochner 	wsdisplay_cnattach(&tga_stdscreen, &dcp->dc_rcons,
    626   1.4  drochner 			   0, 0, defattr);
    627   1.1  drochner 
    628   1.1  drochner 	return(0);
    629   1.1  drochner }
    630   1.1  drochner 
    631   1.1  drochner /*
    632   1.1  drochner  * Functions to blank and unblank the display.
    633   1.1  drochner  */
    634   1.1  drochner void
    635   1.1  drochner tga_blank(dc)
    636   1.1  drochner 	struct tga_devconfig *dc;
    637   1.1  drochner {
    638   1.1  drochner 
    639   1.1  drochner 	if (!dc->dc_blanked) {
    640   1.1  drochner 		dc->dc_blanked = 1;
    641  1.21   nathanw 		/* XXX */
    642  1.21   nathanw 		TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | VVR_BLANK);
    643   1.1  drochner 	}
    644   1.1  drochner }
    645   1.1  drochner 
    646   1.1  drochner void
    647   1.1  drochner tga_unblank(dc)
    648   1.1  drochner 	struct tga_devconfig *dc;
    649   1.1  drochner {
    650   1.1  drochner 
    651   1.1  drochner 	if (dc->dc_blanked) {
    652   1.1  drochner 		dc->dc_blanked = 0;
    653  1.21   nathanw 		/* XXX */
    654  1.21   nathanw 		TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~VVR_BLANK);
    655   1.1  drochner 	}
    656   1.1  drochner }
    657   1.1  drochner 
    658   1.1  drochner /*
    659   1.1  drochner  * Functions to manipulate the built-in cursor handing hardware.
    660   1.1  drochner  */
    661   1.1  drochner int
    662   1.1  drochner tga_builtin_set_cursor(dc, cursorp)
    663   1.1  drochner 	struct tga_devconfig *dc;
    664   1.1  drochner 	struct wsdisplay_cursor *cursorp;
    665   1.1  drochner {
    666  1.17     elric 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    667  1.17     elric 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    668   1.8   thorpej 	int count, error, v;
    669   1.1  drochner 
    670   1.1  drochner 	v = cursorp->which;
    671   1.8   thorpej 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    672  1.17     elric 		error = dcrf->ramdac_check_curcmap(dcrc, cursorp);
    673   1.8   thorpej 		if (error)
    674   1.8   thorpej 			return (error);
    675   1.8   thorpej 	}
    676   1.1  drochner 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    677   1.1  drochner 		if ((u_int)cursorp->size.x != 64 ||
    678   1.1  drochner 		    (u_int)cursorp->size.y > 64)
    679   1.1  drochner 			return (EINVAL);
    680   1.1  drochner 		/* The cursor is 2 bits deep, and there is no mask */
    681   1.1  drochner 		count = (cursorp->size.y * 64 * 2) / NBBY;
    682   1.8   thorpej 		if (!uvm_useracc(cursorp->image, count, B_READ))
    683   1.8   thorpej 			return (EFAULT);
    684   1.1  drochner 	}
    685   1.1  drochner 	if (v & WSDISPLAY_CURSOR_DOHOT)		/* not supported */
    686   1.1  drochner 		return EINVAL;
    687   1.1  drochner 
    688   1.1  drochner 	/* parameters are OK; do it */
    689   1.1  drochner 	if (v & WSDISPLAY_CURSOR_DOCUR) {
    690   1.1  drochner 		if (cursorp->enable)
    691  1.21   nathanw 			/* XXX */
    692  1.21   nathanw 			TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 0x04);
    693   1.1  drochner 		else
    694  1.21   nathanw 			/* XXX */
    695  1.21   nathanw 			TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) & ~0x04);
    696   1.1  drochner 	}
    697   1.1  drochner 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    698  1.21   nathanw 		TGAWREG(dc, TGA_REG_CXYR,
    699  1.21   nathanw 				((cursorp->pos.y & 0xfff) << 12) | (cursorp->pos.x & 0xfff));
    700   1.1  drochner 	}
    701   1.1  drochner 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    702   1.8   thorpej 		/* can't fail. */
    703  1.17     elric 		dcrf->ramdac_set_curcmap(dcrc, cursorp);
    704   1.1  drochner 	}
    705   1.1  drochner 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    706   1.8   thorpej 		count = ((64 * 2) / NBBY) * cursorp->size.y;
    707  1.21   nathanw 		TGAWREG(dc, TGA_REG_CCBR,
    708  1.21   nathanw 		    (TGARREG(dc, TGA_REG_CCBR) & ~0xfc00) | (cursorp->size.y << 10));
    709   1.1  drochner 		copyin(cursorp->image, (char *)(dc->dc_vaddr +
    710  1.21   nathanw 		    (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
    711   1.1  drochner 		    count);				/* can't fail. */
    712   1.1  drochner 	}
    713   1.1  drochner 	return (0);
    714   1.1  drochner }
    715   1.1  drochner 
    716   1.1  drochner int
    717   1.1  drochner tga_builtin_get_cursor(dc, cursorp)
    718   1.1  drochner 	struct tga_devconfig *dc;
    719   1.1  drochner 	struct wsdisplay_cursor *cursorp;
    720   1.1  drochner {
    721  1.17     elric 	struct ramdac_funcs *dcrf = dc->dc_ramdac_funcs;
    722  1.17     elric 	struct ramdac_cookie *dcrc = dc->dc_ramdac_cookie;
    723   1.1  drochner 	int count, error;
    724   1.1  drochner 
    725   1.1  drochner 	cursorp->which = WSDISPLAY_CURSOR_DOALL &
    726   1.1  drochner 	    ~(WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOCMAP);
    727  1.21   nathanw 	cursorp->enable = (TGARREG(dc, TGA_REG_VVVR) & 0x04) != 0;
    728  1.21   nathanw 	cursorp->pos.x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
    729  1.21   nathanw 	cursorp->pos.y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
    730   1.1  drochner 	cursorp->size.x = 64;
    731  1.21   nathanw 	cursorp->size.y = (TGARREG(dc, TGA_REG_CCBR) >> 10) & 0x3f;
    732   1.1  drochner 
    733   1.1  drochner 	if (cursorp->image != NULL) {
    734   1.1  drochner 		count = (cursorp->size.y * 64 * 2) / NBBY;
    735   1.1  drochner 		error = copyout((char *)(dc->dc_vaddr +
    736  1.21   nathanw 		      (TGARREG(dc, TGA_REG_CCBR) & 0x3ff)),
    737   1.1  drochner 		    cursorp->image, count);
    738   1.1  drochner 		if (error)
    739   1.1  drochner 			return (error);
    740   1.1  drochner 		/* No mask */
    741   1.1  drochner 	}
    742  1.17     elric 	error = dcrf->ramdac_get_curcmap(dcrc, cursorp);
    743   1.8   thorpej 	return (error);
    744   1.1  drochner }
    745   1.1  drochner 
    746   1.1  drochner int
    747   1.1  drochner tga_builtin_set_curpos(dc, curposp)
    748   1.1  drochner 	struct tga_devconfig *dc;
    749   1.1  drochner 	struct wsdisplay_curpos *curposp;
    750   1.1  drochner {
    751   1.1  drochner 
    752  1.21   nathanw 	TGAWREG(dc, TGA_REG_CXYR,
    753  1.21   nathanw 	    ((curposp->y & 0xfff) << 12) | (curposp->x & 0xfff));
    754   1.1  drochner 	return (0);
    755   1.1  drochner }
    756   1.1  drochner 
    757   1.1  drochner int
    758   1.1  drochner tga_builtin_get_curpos(dc, curposp)
    759   1.1  drochner 	struct tga_devconfig *dc;
    760   1.1  drochner 	struct wsdisplay_curpos *curposp;
    761   1.1  drochner {
    762   1.1  drochner 
    763  1.21   nathanw 	curposp->x = TGARREG(dc, TGA_REG_CXYR) & 0xfff;
    764  1.21   nathanw 	curposp->y = (TGARREG(dc, TGA_REG_CXYR) >> 12) & 0xfff;
    765   1.1  drochner 	return (0);
    766   1.1  drochner }
    767   1.1  drochner 
    768   1.1  drochner int
    769   1.1  drochner tga_builtin_get_curmax(dc, curposp)
    770   1.1  drochner 	struct tga_devconfig *dc;
    771   1.1  drochner 	struct wsdisplay_curpos *curposp;
    772   1.1  drochner {
    773   1.1  drochner 
    774   1.1  drochner 	curposp->x = curposp->y = 64;
    775   1.1  drochner 	return (0);
    776  1.14      ross }
    777  1.14      ross 
    778  1.14      ross /*
    779  1.14      ross  * Copy columns (characters) in a row (line).
    780  1.14      ross  */
    781  1.14      ross void
    782  1.14      ross tga_copycols(id, row, srccol, dstcol, ncols)
    783  1.14      ross 	void *id;
    784  1.14      ross 	int row, srccol, dstcol, ncols;
    785  1.14      ross {
    786  1.14      ross 	struct rcons *rc = id;
    787  1.14      ross 	int y, srcx, dstx, nx;
    788  1.14      ross 
    789  1.14      ross 	y = rc->rc_yorigin + rc->rc_font->height * row;
    790  1.14      ross 	srcx = rc->rc_xorigin + rc->rc_font->width * srccol;
    791  1.14      ross 	dstx = rc->rc_xorigin + rc->rc_font->width * dstcol;
    792  1.14      ross 	nx = rc->rc_font->width * ncols;
    793  1.14      ross 
    794  1.14      ross 	tga_rop(rc->rc_sp, dstx, y,
    795  1.14      ross 	    nx, rc->rc_font->height, RAS_SRC,
    796  1.14      ross 	    rc->rc_sp, srcx, y);
    797  1.14      ross }
    798  1.14      ross 
    799  1.14      ross /*
    800  1.14      ross  * Copy rows (lines).
    801  1.14      ross  */
    802  1.14      ross void
    803  1.14      ross tga_copyrows(id, srcrow, dstrow, nrows)
    804  1.14      ross 	void *id;
    805  1.14      ross 	int srcrow, dstrow, nrows;
    806  1.14      ross {
    807  1.14      ross 	struct rcons *rc = id;
    808  1.14      ross 	int srcy, dsty, ny;
    809  1.14      ross 
    810  1.14      ross 	srcy = rc->rc_yorigin + rc->rc_font->height * srcrow;
    811  1.14      ross 	dsty = rc->rc_yorigin + rc->rc_font->height * dstrow;
    812  1.14      ross 	ny = rc->rc_font->height * nrows;
    813  1.14      ross 
    814  1.14      ross 	tga_rop(rc->rc_sp, rc->rc_xorigin, dsty,
    815  1.14      ross 	    rc->rc_raswidth, ny, RAS_SRC,
    816  1.14      ross 	    rc->rc_sp, rc->rc_xorigin, srcy);
    817  1.14      ross }
    818  1.14      ross 
    819  1.14      ross /* Do we need the src? */
    820  1.14      ross static int needsrc[16] = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 };
    821  1.14      ross 
    822  1.14      ross /* A mapping between our API and the TGA card */
    823  1.14      ross static int map_rop[16] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6,
    824  1.14      ross 	0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf
    825  1.14      ross };
    826  1.14      ross 
    827  1.14      ross /*
    828  1.14      ross  *  Generic TGA raster op.
    829  1.14      ross  *   This covers all possible raster ops, and
    830  1.14      ross  *   clips the sizes and all of that.
    831  1.14      ross  */
    832  1.14      ross static int
    833  1.14      ross tga_rop(dst, dx, dy, w, h, rop, src, sx, sy)
    834  1.14      ross 	struct raster *dst;
    835  1.14      ross 	int dx, dy, w, h, rop;
    836  1.14      ross 	struct raster *src;
    837  1.14      ross 	int sx, sy;
    838  1.14      ross {
    839  1.14      ross 	if (!dst)
    840  1.14      ross 		return -1;
    841  1.14      ross 	if (dst->data == NULL)
    842  1.14      ross 		return -1;	/* we should be writing to a screen */
    843  1.14      ross 	if (needsrc[RAS_GETOP(rop)]) {
    844  1.14      ross 		if (src == (struct raster *) 0)
    845  1.14      ross 			return -1;	/* We want a src */
    846  1.14      ross 		/* Clip against src */
    847  1.14      ross 		if (sx < 0) {
    848  1.14      ross 			w += sx;
    849  1.14      ross 			sx = 0;
    850  1.14      ross 		}
    851  1.14      ross 		if (sy < 0) {
    852  1.14      ross 			h += sy;
    853  1.14      ross 			sy = 0;
    854  1.14      ross 		}
    855  1.14      ross 		if (sx + w > src->width)
    856  1.14      ross 			w = src->width - sx;
    857  1.14      ross 		if (sy + h > src->height)
    858  1.14      ross 			h = src->height - sy;
    859  1.14      ross 	} else {
    860  1.14      ross 		if (src != (struct raster *) 0)
    861  1.14      ross 			return -1;	/* We need no src */
    862  1.14      ross 	}
    863  1.14      ross 	/* Clip against dst.  We modify src regardless of using it,
    864  1.14      ross 	 * since it really doesn't matter.
    865  1.14      ross 	 */
    866  1.14      ross 	if (dx < 0) {
    867  1.14      ross 		w += dx;
    868  1.14      ross 		sx -= dx;
    869  1.14      ross 		dx = 0;
    870  1.14      ross 	}
    871  1.14      ross 	if (dy < 0) {
    872  1.14      ross 		h += dy;
    873  1.14      ross 		sy -= dy;
    874  1.14      ross 		dy = 0;
    875  1.14      ross 	}
    876  1.14      ross 	if (dx + w > dst->width)
    877  1.14      ross 		w = dst->width - dx;
    878  1.14      ross 	if (dy + h > dst->height)
    879  1.14      ross 		h = dst->height - dy;
    880  1.14      ross 	if (w <= 0 || h <= 0)
    881  1.14      ross 		return 0;	/* Vacuously true; */
    882  1.14      ross 	if (!src)
    883  1.14      ross 		return tga_rop_nosrc(dst, dx, dy, w, h, rop);
    884  1.14      ross 	if (src->data == NULL)
    885  1.14      ross 		return tga_rop_htov(dst, dx, dy, w, h, rop, src, sx, sy);
    886  1.14      ross 	else
    887  1.14      ross 		return tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy);
    888  1.14      ross }
    889  1.14      ross 
    890  1.14      ross /*
    891  1.14      ross  * No source raster ops.
    892  1.14      ross  * This function deals with all raster ops that don't require a src.
    893  1.14      ross  */
    894  1.14      ross static int
    895  1.14      ross tga_rop_nosrc(dst, dx, dy, w, h, rop)
    896  1.14      ross 	struct raster *dst;
    897  1.14      ross 	int dx, dy, w, h, rop;
    898  1.14      ross {
    899  1.14      ross 	return raster_op(dst, dx, dy, w, h, rop, NULL, 0, 0);
    900  1.14      ross }
    901  1.14      ross 
    902  1.14      ross /*
    903  1.14      ross  * Host to Video raster ops.
    904  1.14      ross  * This function deals with all raster ops that have a src that is host memory.
    905  1.14      ross  */
    906  1.14      ross static int
    907  1.14      ross tga_rop_htov(dst, dx, dy, w, h, rop, src, sx, sy)
    908  1.14      ross 	struct raster *dst;
    909  1.14      ross 	int dx, dy, w, h, rop;
    910  1.14      ross 	struct raster *src;
    911  1.14      ross 	int sx, sy;
    912  1.14      ross {
    913  1.14      ross 	return raster_op(dst, dx, dy, w, h, rop, src, sx, sy);
    914  1.14      ross }
    915  1.14      ross 
    916  1.14      ross /*
    917  1.14      ross  * Video to Video raster ops.
    918  1.14      ross  * This function deals with all raster ops that have a src and dst
    919  1.14      ross  * that are on the card.
    920  1.14      ross  */
    921  1.14      ross static int
    922  1.14      ross tga_rop_vtov(dst, dx, dy, w, h, rop, src, sx, sy)
    923  1.14      ross 	struct raster *dst;
    924  1.14      ross 	int dx, dy, w, h, rop;
    925  1.14      ross 	struct raster *src;
    926  1.14      ross 	int sx, sy;
    927  1.14      ross {
    928  1.14      ross 	struct tga_devconfig *dc = (struct tga_devconfig *)dst->data;
    929  1.14      ross 	int srcb, dstb;
    930  1.14      ross 	int x, y;
    931  1.14      ross 	int xstart, xend, xdir, xinc;
    932  1.14      ross 	int ystart, yend, ydir, yinc;
    933  1.14      ross 	int offset = 1 * dc->dc_tgaconf->tgac_vvbr_units;
    934  1.14      ross 
    935  1.14      ross 	/*
    936  1.14      ross 	 * I don't yet want to deal with unaligned guys, really.  And we don't
    937  1.14      ross 	 * deal with copies from one card to another.
    938  1.14      ross 	 */
    939  1.14      ross 	if (dx % 8 != 0 || sx % 8 != 0 || src != dst)
    940  1.14      ross 		return raster_op(dst, dx, dy, w, h, rop, src, sx, sy);
    941  1.14      ross 
    942  1.14      ross 	if (sy >= dy) {
    943  1.14      ross 		ystart = 0;
    944  1.14      ross 		yend = h;
    945  1.14      ross 		ydir = 1;
    946  1.14      ross 	} else {
    947  1.14      ross 		ystart = h;
    948  1.14      ross 		yend = 0;
    949  1.14      ross 		ydir = -1;
    950  1.14      ross 	}
    951  1.14      ross 	if (sx >= dx) {
    952  1.14      ross 		xstart = 0;
    953  1.14      ross 		xend = w * (dst->depth / 8);
    954  1.14      ross 		xdir = 1;
    955  1.14      ross 	} else {
    956  1.14      ross 		xstart = w * (dst->depth / 8);
    957  1.14      ross 		xend = 0;
    958  1.14      ross 		xdir = -1;
    959  1.14      ross 	}
    960  1.14      ross 	xinc = xdir * 4 * 64;
    961  1.14      ross 	yinc = ydir * dst->linelongs * 4;
    962  1.14      ross 	ystart *= dst->linelongs * 4;
    963  1.14      ross 	yend *= dst->linelongs * 4;
    964  1.14      ross 	srcb = offset + sy  * src->linelongs * 4 + sx;
    965  1.14      ross 	dstb = offset + dy  * dst->linelongs * 4 + dx;
    966  1.21   nathanw 	TGAWALREG(dc, TGA_REG_GMOR, 3, 0x0007); /* Copy mode */
    967  1.21   nathanw 	TGAWALREG(dc, TGA_REG_GOPR, 3, map_rop[rop]);	/* Set up the op */
    968  1.14      ross 	for (y = ystart; (ydir * y) < (ydir * yend); y += yinc) {
    969  1.14      ross 		for (x = xstart; (xdir * x) < (xdir * xend); x += xinc) {
    970  1.21   nathanw 		  /* XXX XXX Eight writes to different addresses should fill
    971  1.21   nathanw 		   * XXX XXX up the write buffers on 21064 and 21164 chips,
    972  1.21   nathanw 		   * XXX XXX but later CPUs might have larger write buffers which
    973  1.21   nathanw 		   * XXX XXX require further unrolling of this loop, or the
    974  1.21   nathanw 		   * XXX XXX insertion of memory barriers.
    975  1.21   nathanw 		   */
    976  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCSR, 0, srcb + y + x + 3 * 64);
    977  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCDR, 0, dstb + y + x + 3 * 64);
    978  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCSR, 1, srcb + y + x + 2 * 64);
    979  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCDR, 1, dstb + y + x + 2 * 64);
    980  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCSR, 2, srcb + y + x + 1 * 64);
    981  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCDR, 2, dstb + y + x + 1 * 64);
    982  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCSR, 3, srcb + y + x + 0 * 64);
    983  1.21   nathanw 			TGAWALREG(dc, TGA_REG_GCDR, 3, dstb + y + x + 0 * 64);
    984  1.14      ross 		}
    985  1.14      ross 	}
    986  1.21   nathanw 	TGAWALREG(dc, TGA_REG_GOPR, 0, 0x0003); /* op -> dst = src */
    987  1.21   nathanw 	TGAWALREG(dc, TGA_REG_GMOR, 0, 0x0000); /* Simple mode */
    988  1.14      ross 	return 0;
    989  1.17     elric }
    990  1.17     elric 
    991  1.17     elric void
    992  1.17     elric tga_ramdac_wr(v, btreg, val)
    993  1.17     elric 	void *v;
    994  1.17     elric 	u_int btreg;
    995  1.17     elric 	u_int8_t val;
    996  1.17     elric {
    997  1.17     elric 	struct tga_devconfig *dc = v;
    998  1.17     elric 
    999  1.17     elric 	if (btreg > BT485_REG_MAX)
   1000  1.17     elric 		panic("tga_ramdac_wr: reg %d out of range\n", btreg);
   1001  1.17     elric 
   1002  1.21   nathanw 	TGAWREG(dc, TGA_REG_EPDR, (btreg << 9) | (0 << 8 ) | val); /* XXX */
   1003  1.21   nathanw 	TGAREGWB(dc, TGA_REG_EPDR, 1);
   1004  1.17     elric }
   1005  1.17     elric 
   1006  1.17     elric void
   1007  1.17     elric tga2_ramdac_wr(v, btreg, val)
   1008  1.17     elric 	void *v;
   1009  1.17     elric 	u_int btreg;
   1010  1.17     elric 	u_int8_t val;
   1011  1.17     elric {
   1012  1.17     elric 	struct tga_devconfig *dc = v;
   1013  1.21   nathanw 	bus_space_handle_t ramdac;
   1014  1.17     elric 
   1015  1.17     elric 	if (btreg > BT485_REG_MAX)
   1016  1.17     elric 		panic("tga_ramdac_wr: reg %d out of range\n", btreg);
   1017  1.17     elric 
   1018  1.21   nathanw 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
   1019  1.21   nathanw 		(0xe << 12) + (btreg << 8), 4, &ramdac);
   1020  1.21   nathanw 	bus_space_write_4(dc->dc_memt, ramdac, 0, val & 0xff);
   1021  1.21   nathanw 	bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_WRITE);
   1022  1.17     elric }
   1023  1.17     elric 
   1024  1.17     elric u_int8_t
   1025  1.17     elric tga_ramdac_rd(v, btreg)
   1026  1.17     elric 	void *v;
   1027  1.17     elric 	u_int btreg;
   1028  1.17     elric {
   1029  1.17     elric 	struct tga_devconfig *dc = v;
   1030  1.17     elric 	tga_reg_t rdval;
   1031  1.17     elric 
   1032  1.17     elric 	if (btreg > BT485_REG_MAX)
   1033  1.17     elric 		panic("tga_ramdac_rd: reg %d out of range\n", btreg);
   1034  1.17     elric 
   1035  1.21   nathanw 	TGAWREG(dc, TGA_REG_EPSR, (btreg << 1) | 0x1); /* XXX */
   1036  1.21   nathanw 	TGAREGWB(dc, TGA_REG_EPSR, 1);
   1037  1.17     elric 
   1038  1.21   nathanw 	rdval = TGARREG(dc, TGA_REG_EPDR);
   1039  1.17     elric 	return (rdval >> 16) & 0xff;				/* XXX */
   1040  1.17     elric }
   1041  1.17     elric 
   1042  1.17     elric u_int8_t
   1043  1.17     elric tga2_ramdac_rd(v, btreg)
   1044  1.17     elric 	void *v;
   1045  1.17     elric 	u_int btreg;
   1046  1.17     elric {
   1047  1.17     elric 	struct tga_devconfig *dc = v;
   1048  1.21   nathanw 	bus_space_handle_t ramdac;
   1049  1.17     elric 	u_int8_t retval;
   1050  1.17     elric 
   1051  1.17     elric 	if (btreg > BT485_REG_MAX)
   1052  1.17     elric 		panic("tga_ramdac_rd: reg %d out of range\n", btreg);
   1053  1.17     elric 
   1054  1.21   nathanw 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_RAMDAC +
   1055  1.21   nathanw 		(0xe << 12) + (btreg << 8), 4, &ramdac);
   1056  1.21   nathanw 	retval = bus_space_read_4(dc->dc_memt, ramdac, 0) & 0xff;
   1057  1.21   nathanw 	bus_space_barrier(dc->dc_memt, ramdac, 0, 4, BUS_SPACE_BARRIER_READ);
   1058  1.17     elric 	return retval;
   1059  1.17     elric }
   1060  1.17     elric 
   1061  1.17     elric #include <dev/ic/decmonitors.c>
   1062  1.17     elric void tga2_ics9110_wr __P((
   1063  1.17     elric 	struct tga_devconfig *dc,
   1064  1.17     elric 	int dotclock
   1065  1.17     elric ));
   1066  1.17     elric 
   1067  1.17     elric void
   1068  1.17     elric tga2_init(dc, m)
   1069  1.17     elric 	struct tga_devconfig *dc;
   1070  1.17     elric 	int m;
   1071  1.17     elric {
   1072  1.17     elric 
   1073  1.17     elric 	tga2_ics9110_wr(dc, decmonitors[m].dotclock);
   1074  1.21   nathanw #if 0
   1075  1.21   nathanw 	TGAWREG(dc, TGA_REG_VHCR,
   1076  1.17     elric 	     ((decmonitors[m].hbp / 4) << 21) |
   1077  1.17     elric 	     ((decmonitors[m].hsync / 4) << 14) |
   1078  1.17     elric 	    (((decmonitors[m].hfp - 4) / 4) << 9) |
   1079  1.21   nathanw 	     ((decmonitors[m].cols + 4) / 4));
   1080  1.17     elric #else
   1081  1.21   nathanw 	TGAWREG(dc, TGA_REG_VHCR,
   1082  1.21   nathanw 	     ((decmonitors[m].hbp / 4) << 21) |
   1083  1.21   nathanw 	     ((decmonitors[m].hsync / 4) << 14) |
   1084  1.17     elric 	    (((decmonitors[m].hfp) / 4) << 9) |
   1085  1.21   nathanw 	     ((decmonitors[m].cols) / 4));
   1086  1.17     elric #endif
   1087  1.21   nathanw 	TGAWREG(dc, TGA_REG_VVCR,
   1088  1.17     elric 	    (decmonitors[m].vbp << 22) |
   1089  1.17     elric 	    (decmonitors[m].vsync << 16) |
   1090  1.17     elric 	    (decmonitors[m].vfp << 11) |
   1091  1.21   nathanw 	    (decmonitors[m].rows));
   1092  1.21   nathanw 	TGAWREG(dc, TGA_REG_VVBR, 1);
   1093  1.21   nathanw 	TGAREGRWB(dc, TGA_REG_VHCR, 3);
   1094  1.21   nathanw 	TGAWREG(dc, TGA_REG_VVVR, TGARREG(dc, TGA_REG_VVVR) | 1);
   1095  1.21   nathanw 	TGAREGRWB(dc, TGA_REG_VVVR, 1);
   1096  1.21   nathanw 	TGAWREG(dc, TGA_REG_GPMR, 0xffffffff);
   1097  1.21   nathanw 	TGAREGRWB(dc, TGA_REG_GPMR, 1);
   1098  1.17     elric }
   1099  1.17     elric 
   1100  1.17     elric void
   1101  1.17     elric tga2_ics9110_wr(dc, dotclock)
   1102  1.17     elric 	struct tga_devconfig *dc;
   1103  1.17     elric 	int dotclock;
   1104  1.17     elric {
   1105  1.21   nathanw 	bus_space_handle_t clock;
   1106  1.17     elric 	u_int32_t valU;
   1107  1.17     elric 	int N, M, R, V, X;
   1108  1.17     elric 	int i;
   1109  1.17     elric 
   1110  1.17     elric 	switch (dotclock) {
   1111  1.17     elric 	case 130808000:
   1112  1.17     elric 		N = 0x40; M = 0x7; V = 0x0; X = 0x1; R = 0x1; break;
   1113  1.17     elric 	case 119840000:
   1114  1.17     elric 		N = 0x2d; M = 0x2b; V = 0x1; X = 0x1; R = 0x1; break;
   1115  1.17     elric 	case 108180000:
   1116  1.17     elric 		N = 0x11; M = 0x9; V = 0x1; X = 0x1; R = 0x2; break;
   1117  1.17     elric 	case 103994000:
   1118  1.17     elric 		N = 0x6d; M = 0xf; V = 0x0; X = 0x1; R = 0x1; break;
   1119  1.17     elric 	case 175000000:
   1120  1.17     elric 		N = 0x5F; M = 0x3E; V = 0x1; X = 0x1; R = 0x1; break;
   1121  1.17     elric 	case  75000000:
   1122  1.17     elric 		N = 0x6e; M = 0x15; V = 0x0; X = 0x1; R = 0x1; break;
   1123  1.17     elric 	case  74000000:
   1124  1.17     elric 		N = 0x2a; M = 0x41; V = 0x1; X = 0x1; R = 0x1; break;
   1125  1.17     elric 	case  69000000:
   1126  1.17     elric 		N = 0x35; M = 0xb; V = 0x0; X = 0x1; R = 0x1; break;
   1127  1.17     elric 	case  65000000:
   1128  1.17     elric 		N = 0x6d; M = 0x0c; V = 0x0; X = 0x1; R = 0x2; break;
   1129  1.17     elric 	case  50000000:
   1130  1.17     elric 		N = 0x37; M = 0x3f; V = 0x1; X = 0x1; R = 0x2; break;
   1131  1.17     elric 	case  40000000:
   1132  1.17     elric 		N = 0x5f; M = 0x11; V = 0x0; X = 0x1; R = 0x2; break;
   1133  1.17     elric 	case  31500000:
   1134  1.17     elric 		N = 0x16; M = 0x05; V = 0x0; X = 0x1; R = 0x2; break;
   1135  1.17     elric 	case  25175000:
   1136  1.17     elric 		N = 0x66; M = 0x1d; V = 0x0; X = 0x1; R = 0x2; break;
   1137  1.17     elric 	case 135000000:
   1138  1.17     elric 		N = 0x42; M = 0x07; V = 0x0; X = 0x1; R = 0x1; break;
   1139  1.17     elric 	case 110000000:
   1140  1.17     elric 		N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
   1141  1.17     elric 	case 202500000:
   1142  1.17     elric 		N = 0x60; M = 0x32; V = 0x1; X = 0x1; R = 0x2; break;
   1143  1.17     elric 	default:
   1144  1.17     elric 		panic("unrecognized clock rate %d\n", dotclock);
   1145  1.17     elric 	}
   1146  1.17     elric 
   1147  1.17     elric 	/* XXX -- hard coded, bad */
   1148  1.17     elric 	valU  = N | ( M << 7 ) | (V << 14);
   1149  1.17     elric 	valU |= (X << 15) | (R << 17);
   1150  1.17     elric 	valU |= 0x17 << 19;
   1151  1.17     elric 
   1152  1.21   nathanw 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
   1153  1.21   nathanw 	    TGA2_MEM_CLOCK + (0xe << 12), 4, &clock); /* XXX */
   1154  1.17     elric 
   1155  1.21   nathanw 	for (i=24; i>0; i--) {
   1156  1.21   nathanw 		u_int32_t       writeval;
   1157  1.17     elric 
   1158  1.21   nathanw 		writeval = valU & 0x1;
   1159  1.21   nathanw 		if (i == 1)
   1160  1.21   nathanw 			writeval |= 0x2;
   1161  1.21   nathanw 		valU >>= 1;
   1162  1.21   nathanw 		bus_space_write_4(dc->dc_memt, clock, 0, writeval);
   1163  1.21   nathanw 		bus_space_barrier(dc->dc_memt, clock, 0, 4, BUS_SPACE_BARRIER_WRITE);
   1164  1.17     elric         }
   1165  1.21   nathanw 	bus_space_subregion(dc->dc_memt, dc->dc_memh, TGA2_MEM_EXTDEV +
   1166  1.21   nathanw 	    TGA2_MEM_CLOCK + (0xe << 12) + (0x1 << 11) + (0x1 << 11), 4,
   1167  1.21   nathanw 		&clock); /* XXX */
   1168  1.21   nathanw 	bus_space_write_4(dc->dc_memt, clock, 0, 0x0);
   1169  1.21   nathanw 	bus_space_barrier(dc->dc_memt, clock, 0, 0, BUS_SPACE_BARRIER_WRITE);
   1170   1.1  drochner }
   1171