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