Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.67.6.2
      1  1.67.6.2       tls /*	$NetBSD: cgfourteen.c,v 1.67.6.2 2013/02/25 00:28:57 tls Exp $ */
      2       1.1    abrown 
      3       1.1    abrown /*
      4      1.12        pk  * Copyright (c) 1996
      5       1.1    abrown  *	The President and Fellows of Harvard College. All rights reserved.
      6       1.1    abrown  * Copyright (c) 1992, 1993
      7       1.1    abrown  *	The Regents of the University of California.  All rights reserved.
      8       1.1    abrown  *
      9       1.1    abrown  * This software was developed by the Computer Systems Engineering group
     10       1.1    abrown  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     11       1.1    abrown  * contributed to Berkeley.
     12       1.1    abrown  *
     13       1.1    abrown  * All advertising materials mentioning features or use of this software
     14       1.1    abrown  * must display the following acknowledgement:
     15       1.1    abrown  *	This product includes software developed by Harvard University.
     16       1.1    abrown  *	This product includes software developed by the University of
     17       1.1    abrown  *	California, Lawrence Berkeley Laboratory.
     18       1.1    abrown  *
     19       1.1    abrown  * Redistribution and use in source and binary forms, with or without
     20       1.1    abrown  * modification, are permitted provided that the following conditions
     21       1.1    abrown  * are met:
     22       1.1    abrown  * 1. Redistributions of source code must retain the above copyright
     23       1.1    abrown  *    notice, this list of conditions and the following disclaimer.
     24       1.1    abrown  * 2. Redistributions in binary form must reproduce the above copyright
     25       1.1    abrown  *    notice, this list of conditions and the following disclaimer in the
     26       1.1    abrown  *    documentation and/or other materials provided with the distribution.
     27       1.1    abrown  * 3. All advertising materials mentioning features or use of this software
     28       1.1    abrown  *    must display the following acknowledgement:
     29       1.1    abrown  *	This product includes software developed by the University of
     30       1.1    abrown  *	California, Berkeley and its contributors.
     31       1.1    abrown  *	This product includes software developed by Harvard University and
     32       1.1    abrown  *	its contributors.
     33       1.1    abrown  * 4. Neither the name of the University nor the names of its contributors
     34       1.1    abrown  *    may be used to endorse or promote products derived from this software
     35       1.1    abrown  *    without specific prior written permission.
     36       1.1    abrown  *
     37       1.1    abrown  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     38       1.1    abrown  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     39       1.1    abrown  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     40       1.1    abrown  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     41       1.1    abrown  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     42       1.1    abrown  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     43       1.1    abrown  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     44       1.1    abrown  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     45       1.1    abrown  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     46       1.1    abrown  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     47       1.1    abrown  * SUCH DAMAGE.
     48       1.1    abrown  *
     49       1.1    abrown  *   Based on:
     50       1.1    abrown  *	NetBSD: cgthree.c,v 1.28 1996/05/31 09:59:22 pk Exp
     51       1.1    abrown  *	NetBSD: cgsix.c,v 1.25 1996/04/01 17:30:00 christos Exp
     52       1.1    abrown  */
     53       1.1    abrown 
     54       1.1    abrown /*
     55       1.1    abrown  * Driver for Campus-II on-board mbus-based video (cgfourteen).
     56       1.1    abrown  *
     57       1.1    abrown  * Does not handle interrupts, even though they can occur.
     58       1.1    abrown  *
     59       1.1    abrown  * XXX should defer colormap updates to vertical retrace interrupts
     60       1.1    abrown  */
     61      1.10       mrg 
     62      1.12        pk /*
     63       1.1    abrown  * The following is for debugging only; it opens up a security hole
     64      1.12        pk  * enabled by allowing any user to map the control registers for the
     65       1.1    abrown  * cg14 into their space.
     66       1.1    abrown  */
     67      1.47      elad #undef CG14_MAP_REGS
     68       1.1    abrown 
     69  1.67.6.2       tls #include "opt_wsemul.h"
     70  1.67.6.2       tls #include "sx.h"
     71  1.67.6.2       tls 
     72       1.1    abrown #include <sys/param.h>
     73       1.1    abrown #include <sys/systm.h>
     74       1.1    abrown #include <sys/buf.h>
     75       1.1    abrown #include <sys/device.h>
     76       1.1    abrown #include <sys/ioctl.h>
     77       1.1    abrown #include <sys/malloc.h>
     78      1.55  macallan #include <sys/kmem.h>
     79       1.1    abrown #include <sys/mman.h>
     80       1.1    abrown #include <sys/tty.h>
     81       1.1    abrown #include <sys/conf.h>
     82      1.55  macallan #include <dev/pci/pciio.h>
     83       1.1    abrown 
     84      1.18       mrg #include <uvm/uvm_extern.h>
     85       1.1    abrown 
     86      1.42  christos #include <dev/sun/fbio.h>
     87      1.19        pk #include <machine/autoconf.h>
     88      1.42  christos #include <machine/pmap.h>
     89      1.42  christos #include <dev/sun/fbvar.h>
     90      1.42  christos #include <machine/cpu.h>
     91      1.19        pk #include <dev/sbus/sbusvar.h>
     92      1.19        pk 
     93      1.49  macallan #include "wsdisplay.h"
     94      1.49  macallan #include <dev/wscons/wsconsio.h>
     95      1.49  macallan #include <dev/wsfont/wsfont.h>
     96      1.49  macallan #include <dev/rasops/rasops.h>
     97      1.49  macallan 
     98      1.49  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     99      1.49  macallan 
    100  1.67.6.2       tls #include <sparc/sparc/asm.h>
    101       1.1    abrown #include <sparc/dev/cgfourteenreg.h>
    102       1.1    abrown #include <sparc/dev/cgfourteenvar.h>
    103  1.67.6.2       tls #include <sparc/dev/sxreg.h>
    104  1.67.6.2       tls #include <sparc/dev/sxvar.h>
    105      1.49  macallan 
    106       1.1    abrown /* autoconfiguration driver */
    107      1.53  macallan static int	cgfourteenmatch(device_t, struct cfdata *, void *);
    108      1.53  macallan static void	cgfourteenattach(device_t, device_t, void *);
    109      1.53  macallan static void	cgfourteenunblank(device_t);
    110       1.1    abrown 
    111      1.53  macallan CFATTACH_DECL_NEW(cgfourteen, sizeof(struct cgfourteen_softc),
    112      1.30   thorpej     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    113      1.42  christos 
    114       1.8   thorpej extern struct cfdriver cgfourteen_cd;
    115       1.1    abrown 
    116      1.26   gehenna dev_type_open(cgfourteenopen);
    117      1.26   gehenna dev_type_close(cgfourteenclose);
    118      1.26   gehenna dev_type_ioctl(cgfourteenioctl);
    119      1.26   gehenna dev_type_mmap(cgfourteenmmap);
    120      1.42  christos dev_type_poll(cgfourteenpoll);
    121      1.26   gehenna 
    122      1.26   gehenna const struct cdevsw cgfourteen_cdevsw = {
    123      1.42  christos         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    124      1.42  christos         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    125      1.26   gehenna };
    126      1.26   gehenna 
    127       1.1    abrown /* frame buffer generic driver */
    128       1.1    abrown static struct fbdriver cgfourteenfbdriver = {
    129      1.12        pk 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    130      1.42  christos 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    131       1.1    abrown };
    132       1.1    abrown 
    133      1.40       uwe static void cg14_set_video(struct cgfourteen_softc *, int);
    134      1.40       uwe static int  cg14_get_video(struct cgfourteen_softc *);
    135      1.40       uwe static int  cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
    136      1.40       uwe static int  cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
    137      1.40       uwe static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
    138      1.40       uwe static void cg14_init(struct cgfourteen_softc *);
    139      1.40       uwe static void cg14_reset(struct cgfourteen_softc *);
    140      1.23   darrenr 
    141      1.49  macallan #if NWSDISPLAY > 0
    142      1.49  macallan static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
    143      1.49  macallan static void cg14_init_cmap(struct cgfourteen_softc *);
    144      1.49  macallan static int  cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    145      1.49  macallan static int  cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
    146      1.49  macallan static void cg14_set_depth(struct cgfourteen_softc *, int);
    147      1.50  macallan static void cg14_move_cursor(struct cgfourteen_softc *, int, int);
    148      1.50  macallan static int  cg14_do_cursor(struct cgfourteen_softc *,
    149      1.50  macallan                            struct wsdisplay_cursor *);
    150  1.67.6.2       tls 
    151  1.67.6.2       tls #if NSX > 0
    152  1.67.6.2       tls static void cg14_wait_idle(struct cgfourteen_softc *);
    153  1.67.6.2       tls static void cg14_rectfill(struct cgfourteen_softc *, int, int, int, int,
    154  1.67.6.2       tls     uint32_t);
    155  1.67.6.2       tls static void cg14_invert(struct cgfourteen_softc *, int, int, int, int);
    156  1.67.6.2       tls static void cg14_bitblt(void *, int, int, int, int, int, int, int);
    157  1.67.6.2       tls 
    158  1.67.6.2       tls #if 0
    159  1.67.6.2       tls static void cg14_putchar_aa(void *, int, int, u_int, long);
    160      1.49  macallan #endif
    161  1.67.6.2       tls static void cg14_cursor(void *, int, int, int);
    162  1.67.6.2       tls static void cg14_putchar(void *, int, int, u_int, long);
    163  1.67.6.2       tls static void cg14_copycols(void *, int, int, int, int);
    164  1.67.6.2       tls static void cg14_erasecols(void *, int, int, int, long);
    165  1.67.6.2       tls static void cg14_copyrows(void *, int, int, int);
    166  1.67.6.2       tls static void cg14_eraserows(void *, int, int, long);
    167  1.67.6.2       tls #endif /* NSX > 0 */
    168      1.49  macallan 
    169      1.49  macallan #endif
    170      1.49  macallan 
    171      1.23   darrenr /*
    172       1.1    abrown  * Match a cgfourteen.
    173       1.1    abrown  */
    174      1.42  christos int
    175      1.53  macallan cgfourteenmatch(device_t parent, struct cfdata *cf, void *aux)
    176       1.1    abrown {
    177      1.11        pk 	union obio_attach_args *uoba = aux;
    178      1.11        pk 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    179       1.1    abrown 
    180       1.1    abrown 	/*
    181       1.1    abrown 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    182       1.1    abrown 	 * via the processor, and not through device space or an external
    183       1.1    abrown 	 * bus. Thus we look _only_ at the obio bus.
    184       1.1    abrown 	 * Additionally, these things exist only on the Sun4m.
    185       1.1    abrown 	 */
    186      1.11        pk 
    187      1.11        pk 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    188      1.11        pk 		return (0);
    189      1.11        pk 
    190      1.11        pk 	/* Check driver name */
    191      1.27   thorpej 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    192       1.1    abrown }
    193       1.1    abrown 
    194       1.1    abrown /*
    195      1.42  christos  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    196      1.42  christos  *  space for faked overlay junk for the cg8 emulation.
    197      1.42  christos  *
    198      1.42  christos  * As it happens, this value is correct for both cg3 and cg8 emulation!
    199      1.42  christos  */
    200      1.42  christos #define COLOUR_OFFSET (256*1024)
    201      1.42  christos 
    202      1.49  macallan #if NWSDISPLAY > 0
    203      1.49  macallan static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    204      1.49  macallan static paddr_t	cg14_mmap(void *, void *, off_t, int);
    205      1.49  macallan static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
    206      1.49  macallan 
    207      1.49  macallan 
    208      1.49  macallan struct wsdisplay_accessops cg14_accessops = {
    209      1.49  macallan 	cg14_ioctl,
    210      1.49  macallan 	cg14_mmap,
    211      1.49  macallan 	NULL,	/* alloc_screen */
    212      1.49  macallan 	NULL,	/* free_screen */
    213      1.49  macallan 	NULL,	/* show_screen */
    214      1.49  macallan 	NULL, 	/* load_font */
    215      1.49  macallan 	NULL,	/* pollc */
    216      1.49  macallan 	NULL	/* scroll */
    217      1.49  macallan };
    218      1.49  macallan #endif
    219      1.49  macallan 
    220      1.42  christos /*
    221       1.1    abrown  * Attach a display.  We need to notice if it is the console, too.
    222       1.1    abrown  */
    223      1.42  christos void
    224      1.53  macallan cgfourteenattach(device_t parent, device_t self, void *aux)
    225       1.1    abrown {
    226      1.11        pk 	union obio_attach_args *uoba = aux;
    227      1.11        pk 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    228      1.53  macallan 	struct cgfourteen_softc *sc = device_private(self);
    229      1.11        pk 	struct fbdevice *fb = &sc->sc_fb;
    230      1.11        pk 	bus_space_handle_t bh;
    231      1.11        pk 	int node, ramsize;
    232      1.40       uwe 	volatile uint32_t *lut;
    233      1.56  macallan 	int i, isconsole, items;
    234      1.56  macallan 	uint32_t fbva[2] = {0, 0};
    235      1.56  macallan 	uint32_t *ptr = fbva;
    236  1.67.6.2       tls #if NSX > 0
    237  1.67.6.2       tls 	device_t dv;
    238  1.67.6.2       tls 	deviter_t di;
    239  1.67.6.2       tls #endif
    240      1.11        pk 
    241      1.53  macallan 	sc->sc_dev = self;
    242      1.55  macallan 	sc->sc_opens = 0;
    243      1.11        pk 	node = sa->sa_node;
    244      1.11        pk 
    245      1.11        pk 	/* Remember cookies for cgfourteenmmap() */
    246      1.11        pk 	sc->sc_bustag = sa->sa_bustag;
    247       1.1    abrown 
    248      1.11        pk 	fb->fb_driver = &cgfourteenfbdriver;
    249      1.53  macallan 	fb->fb_device = sc->sc_dev;
    250      1.11        pk 	/* Mask out invalid flags from the user. */
    251      1.53  macallan 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    252       1.1    abrown 
    253      1.63  macallan 	fb->fb_type.fb_type = FBTYPE_MDICOLOR;
    254      1.11        pk 	fb->fb_type.fb_depth = 32;
    255      1.63  macallan 
    256      1.11        pk 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    257      1.42  christos 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    258      1.53  macallan 
    259      1.11        pk 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    260      1.42  christos 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    261      1.11        pk 
    262      1.13        pk 	if (sa->sa_nreg < 2) {
    263      1.13        pk 		printf("%s: only %d register sets\n",
    264  1.67.6.1       tls 			device_xname(self), sa->sa_nreg);
    265      1.11        pk 		return;
    266      1.11        pk 	}
    267      1.59    cegger 	memcpy(sc->sc_physadr, sa->sa_reg,
    268      1.42  christos 	      sa->sa_nreg * sizeof(struct sbus_reg));
    269       1.1    abrown 
    270      1.49  macallan 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
    271      1.49  macallan 
    272      1.49  macallan 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
    273       1.1    abrown 	/*
    274       1.1    abrown 	 * Now map in the 8 useful pages of registers
    275       1.1    abrown 	 */
    276      1.11        pk 	if (sa->sa_size < 0x10000) {
    277       1.1    abrown #ifdef DIAGNOSTIC
    278       1.5  christos 		printf("warning: can't find all cgfourteen registers...\n");
    279       1.1    abrown #endif
    280      1.11        pk 		sa->sa_size = 0x10000;
    281       1.1    abrown 	}
    282      1.42  christos 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    283      1.42  christos 			 sa->sa_offset,
    284      1.42  christos 			 sa->sa_size,
    285      1.55  macallan 			 0 /*BUS_SPACE_MAP_LINEAR*/,
    286      1.42  christos 			 &bh) != 0) {
    287  1.67.6.2       tls 		printf("%s: cannot map control registers\n",
    288  1.67.6.2       tls 		    device_xname(self));
    289      1.11        pk 		return;
    290      1.11        pk 	}
    291      1.49  macallan 	sc->sc_regh = bh;
    292      1.64  macallan 	sc->sc_regaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
    293      1.64  macallan 	sc->sc_fbaddr = BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    294      1.64  macallan 				sc->sc_physadr[CG14_PXL_IDX].sbr_offset);
    295      1.64  macallan 
    296      1.11        pk 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    297      1.11        pk 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    298      1.11        pk 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    299      1.11        pk 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    300      1.11        pk 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    301      1.11        pk 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    302      1.11        pk 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    303      1.11        pk 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    304       1.1    abrown 
    305       1.1    abrown 	/*
    306       1.1    abrown 	 * Let the user know that we're here
    307       1.1    abrown 	 */
    308      1.63  macallan 	printf(": %dx%d",
    309      1.11        pk 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    310      1.63  macallan 
    311       1.1    abrown 	/*
    312      1.42  christos 	 * Enable the video.
    313       1.1    abrown 	 */
    314       1.1    abrown 	cg14_set_video(sc, 1);
    315       1.1    abrown 
    316       1.1    abrown 	/*
    317       1.1    abrown 	 * Grab the initial colormap
    318       1.1    abrown 	 */
    319      1.39   tsutsui 	lut = sc->sc_clut1->clut_lut;
    320       1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    321       1.1    abrown 		sc->sc_cmap.cm_chip[i] = lut[i];
    322       1.1    abrown 
    323       1.1    abrown 	/* See if we're the console */
    324      1.42  christos         isconsole = fb_is_console(node);
    325       1.1    abrown 
    326      1.49  macallan #if NWSDISPLAY > 0
    327      1.56  macallan 	prom_getprop(sa->sa_node, "address", 4, &items, &ptr);
    328      1.56  macallan 	if (fbva[1] == 0) {
    329      1.60  macallan 		if (sbus_bus_map( sc->sc_bustag,
    330      1.56  macallan 		    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    331      1.56  macallan 		    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
    332      1.60  macallan 		    ramsize, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    333      1.60  macallan 		    &bh) != 0) {
    334      1.58  macallan 			printf("%s: cannot map pixels\n",
    335      1.58  macallan 				device_xname(sc->sc_dev));
    336      1.56  macallan 			return;
    337      1.56  macallan 		}
    338      1.56  macallan 		sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
    339      1.56  macallan 	} else {
    340      1.56  macallan 		sc->sc_fb.fb_pixels = (void *)fbva[1];
    341      1.49  macallan 	}
    342      1.49  macallan 
    343      1.49  macallan 	if (isconsole)
    344      1.49  macallan 		printf(" (console)\n");
    345      1.51       jdc 	else
    346      1.51       jdc 		printf("\n");
    347      1.49  macallan 
    348      1.49  macallan 	sc->sc_depth = 8;
    349  1.67.6.2       tls 
    350  1.67.6.2       tls #if NSX > 0
    351  1.67.6.2       tls 	/* see if we've got an SX to help us */
    352  1.67.6.2       tls 	sc->sc_sx = NULL;
    353  1.67.6.2       tls 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    354  1.67.6.2       tls 	    dv != NULL;
    355  1.67.6.2       tls 	    dv = deviter_next(&di)) {
    356  1.67.6.2       tls 		if (device_is_a(dv, "sx")) {
    357  1.67.6.2       tls 			sc->sc_sx = device_private(dv);
    358  1.67.6.2       tls 		}
    359  1.67.6.2       tls 	}
    360  1.67.6.2       tls 	deviter_release(&di);
    361  1.67.6.2       tls 	if (sc->sc_sx != NULL) {
    362  1.67.6.2       tls 		sc->sc_fb_paddr = bus_space_mmap(sc->sc_bustag,
    363  1.67.6.2       tls 		    sc->sc_fbaddr, 0, 0, 0) & 0xfffff000;
    364  1.67.6.2       tls 		aprint_normal_dev(sc->sc_dev, "using %s\n",
    365  1.67.6.2       tls 		    device_xname(sc->sc_sx->sc_dev));
    366  1.67.6.2       tls 		aprint_debug_dev(sc->sc_dev, "fb paddr: %08x\n",
    367  1.67.6.2       tls 		    sc->sc_fb_paddr);
    368  1.67.6.2       tls 		sx_write(sc->sc_sx, SX_PAGE_BOUND_LOWER, sc->sc_fb_paddr);
    369  1.67.6.2       tls 		sx_write(sc->sc_sx, SX_PAGE_BOUND_UPPER,
    370  1.67.6.2       tls 		    sc->sc_fb_paddr + 0x03ffffff);
    371  1.67.6.2       tls 	}
    372  1.67.6.2       tls 	cg14_wait_idle(sc);
    373  1.67.6.2       tls #endif
    374      1.49  macallan 	cg14_setup_wsdisplay(sc, isconsole);
    375      1.49  macallan #endif
    376       1.1    abrown 
    377       1.1    abrown 	/* Attach to /dev/fb */
    378      1.16        pk 	fb_attach(&sc->sc_fb, isconsole);
    379       1.1    abrown }
    380       1.1    abrown 
    381       1.1    abrown /*
    382      1.12        pk  * Keep track of the number of opens made. In the 24-bit driver, we need to
    383       1.1    abrown  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    384       1.1    abrown  * the last close. This kind of nonsense is needed to give screenblank
    385       1.1    abrown  * a fighting chance of working.
    386       1.1    abrown  */
    387       1.1    abrown 
    388       1.1    abrown int
    389      1.41  christos cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    390       1.1    abrown {
    391      1.42  christos 	struct cgfourteen_softc *sc;
    392      1.55  macallan 	int oldopens;
    393       1.1    abrown 
    394      1.54    cegger 	sc = device_lookup_private(&cgfourteen_cd, minor(dev));
    395      1.42  christos 	if (sc == NULL)
    396      1.42  christos 		return(ENXIO);
    397      1.55  macallan 	oldopens = sc->sc_opens++;
    398       1.1    abrown 
    399       1.1    abrown 	/* Setup the cg14 as we want it, and save the original PROM state */
    400       1.1    abrown 	if (oldopens == 0)	/* first open only, to make screenblank work */
    401       1.1    abrown 		cg14_init(sc);
    402       1.1    abrown 
    403       1.1    abrown 	return (0);
    404       1.1    abrown }
    405       1.1    abrown 
    406       1.1    abrown int
    407      1.41  christos cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
    408       1.1    abrown {
    409      1.53  macallan 	struct cgfourteen_softc *sc =
    410      1.54    cegger 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    411      1.55  macallan 	int opens;
    412       1.1    abrown 
    413      1.55  macallan 	opens = --sc->sc_opens;
    414      1.55  macallan 	if (sc->sc_opens < 0)
    415      1.55  macallan 		opens = sc->sc_opens = 0;
    416       1.1    abrown 
    417       1.1    abrown 	/*
    418       1.1    abrown 	 * Restore video state to make the PROM happy, on last close.
    419       1.1    abrown 	 */
    420       1.1    abrown 	if (opens == 0)
    421       1.1    abrown 		cg14_reset(sc);
    422       1.1    abrown 
    423       1.1    abrown 	return (0);
    424       1.1    abrown }
    425       1.1    abrown 
    426       1.1    abrown int
    427      1.48  christos cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    428       1.1    abrown {
    429      1.53  macallan 	struct cgfourteen_softc *sc =
    430      1.54    cegger 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    431      1.11        pk 	struct fbgattr *fba;
    432      1.42  christos 	int error;
    433       1.1    abrown 
    434       1.1    abrown 	switch (cmd) {
    435       1.1    abrown 
    436       1.1    abrown 	case FBIOGTYPE:
    437       1.1    abrown 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    438       1.1    abrown 		break;
    439       1.1    abrown 
    440       1.1    abrown 	case FBIOGATTR:
    441       1.1    abrown 		fba = (struct fbgattr *)data;
    442       1.1    abrown 		fba->real_type = FBTYPE_MDICOLOR;
    443       1.1    abrown 		fba->owner = 0;		/* XXX ??? */
    444       1.1    abrown 		fba->fbtype = sc->sc_fb.fb_type;
    445       1.1    abrown 		fba->sattr.flags = 0;
    446       1.1    abrown 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    447       1.1    abrown 		fba->sattr.dev_specific[0] = -1;
    448       1.1    abrown 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    449       1.1    abrown 		fba->emu_types[1] = -1;
    450       1.1    abrown 		break;
    451       1.1    abrown 
    452       1.1    abrown 	case FBIOGETCMAP:
    453      1.42  christos 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    454       1.1    abrown 				     CG14_CLUT_SIZE));
    455       1.1    abrown 
    456       1.1    abrown 	case FBIOPUTCMAP:
    457       1.1    abrown 		/* copy to software map */
    458       1.1    abrown #define p ((struct fbcmap *)data)
    459       1.1    abrown 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    460       1.1    abrown 		if (error)
    461       1.1    abrown 			return (error);
    462       1.1    abrown 		/* now blast them into the chip */
    463       1.1    abrown 		/* XXX should use retrace interrupt */
    464       1.1    abrown 		cg14_load_hwcmap(sc, p->index, p->count);
    465       1.1    abrown #undef p
    466       1.1    abrown 		break;
    467       1.1    abrown 
    468       1.1    abrown 	case FBIOGVIDEO:
    469       1.1    abrown 		*(int *)data = cg14_get_video(sc);
    470       1.1    abrown 		break;
    471       1.1    abrown 
    472       1.1    abrown 	case FBIOSVIDEO:
    473       1.1    abrown 		cg14_set_video(sc, *(int *)data);
    474       1.1    abrown 		break;
    475       1.1    abrown 
    476      1.64  macallan 	case CG14_SET_PIXELMODE: {
    477      1.64  macallan 		int depth = *(int *)data;
    478      1.64  macallan 
    479      1.66  macallan 		if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
    480      1.64  macallan 			return EINVAL;
    481      1.66  macallan 
    482      1.66  macallan 		cg14_set_depth(sc, depth);
    483      1.64  macallan 		}
    484      1.64  macallan 		break;
    485       1.1    abrown 	default:
    486       1.1    abrown 		return (ENOTTY);
    487       1.1    abrown 	}
    488       1.1    abrown 	return (0);
    489       1.1    abrown }
    490       1.1    abrown 
    491       1.1    abrown /*
    492       1.1    abrown  * Undo the effect of an FBIOSVIDEO that turns the video off.
    493       1.1    abrown  */
    494       1.1    abrown static void
    495      1.53  macallan cgfourteenunblank(device_t dev)
    496       1.1    abrown {
    497      1.53  macallan 	struct cgfourteen_softc *sc = device_private(dev);
    498       1.1    abrown 
    499      1.50  macallan 	cg14_set_video(sc, 1);
    500      1.50  macallan #if NWSDISPLAY > 0
    501      1.50  macallan 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
    502      1.50  macallan 		cg14_set_depth(sc, 8);
    503      1.50  macallan 		cg14_init_cmap(sc);
    504      1.50  macallan 		vcons_redraw_screen(sc->sc_vd.active);
    505      1.50  macallan 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    506      1.50  macallan 	}
    507      1.50  macallan #endif
    508       1.1    abrown }
    509       1.1    abrown 
    510       1.1    abrown /*
    511       1.1    abrown  * Return the address that would map the given device at the given
    512       1.1    abrown  * offset, allowing for the given protection, or return -1 for error.
    513      1.64  macallan   */
    514      1.17    simonb paddr_t
    515      1.40       uwe cgfourteenmmap(dev_t dev, off_t off, int prot)
    516       1.1    abrown {
    517      1.53  macallan 	struct cgfourteen_softc *sc =
    518      1.54    cegger 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    519      1.64  macallan 	off_t offset = -1;
    520      1.12        pk 
    521       1.1    abrown 	if (off & PGOFSET)
    522       1.1    abrown 		panic("cgfourteenmmap");
    523      1.14       mrg 
    524      1.14       mrg 	if (off < 0)
    525      1.14       mrg 		return (-1);
    526       1.1    abrown 
    527      1.64  macallan 	if (off >= 0 && off < 0x10000) {
    528      1.64  macallan 		offset = sc->sc_regaddr;
    529      1.64  macallan 	} else if (off >= CG14_CURSOR_VOFF &&
    530      1.64  macallan 		   off < (CG14_CURSOR_VOFF + 0x1000)) {
    531      1.64  macallan 		offset = sc->sc_regaddr + CG14_OFFSET_CURS;
    532      1.64  macallan 		off -= CG14_CURSOR_VOFF;
    533      1.64  macallan 	} else if (off >= CG14_DIRECT_VOFF &&
    534      1.64  macallan 		   off < (CG14_DIRECT_VOFF + sc->sc_vramsize)) {
    535      1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_VRAM;
    536      1.64  macallan 		off -= CG14_DIRECT_VOFF;
    537      1.64  macallan 	} else if (off >= CG14_BGR_VOFF &&
    538      1.64  macallan 		   off < (CG14_BGR_VOFF + sc->sc_vramsize)) {
    539      1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_CBGR;
    540      1.64  macallan 		off -= CG14_BGR_VOFF;
    541      1.64  macallan 	} else if (off >= CG14_X32_VOFF &&
    542      1.64  macallan 		   off < (CG14_X32_VOFF + (sc->sc_vramsize >> 2))) {
    543      1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PX32;
    544      1.64  macallan 		off -= CG14_X32_VOFF;
    545      1.64  macallan 	} else if (off >= CG14_B32_VOFF &&
    546      1.64  macallan 		   off < (CG14_B32_VOFF + (sc->sc_vramsize >> 2))) {
    547      1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PB32;
    548      1.64  macallan 		off -= CG14_B32_VOFF;
    549      1.64  macallan 	} else if (off >= CG14_G32_VOFF &&
    550      1.64  macallan 		   off < (CG14_G32_VOFF + (sc->sc_vramsize >> 2))) {
    551      1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PG32;
    552      1.64  macallan 		off -= CG14_G32_VOFF;
    553      1.64  macallan 	} else if (off >= CG14_R32_VOFF &&
    554      1.64  macallan 		   off < CG14_R32_VOFF + (sc->sc_vramsize >> 2)) {
    555      1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PR32;
    556      1.64  macallan 		off -= CG14_R32_VOFF;
    557      1.64  macallan 	} else
    558      1.64  macallan 		return -1;
    559      1.64  macallan 	return (bus_space_mmap(sc->sc_bustag, offset, off, prot,
    560      1.64  macallan 		    BUS_SPACE_MAP_LINEAR));
    561      1.12        pk }
    562       1.1    abrown 
    563      1.42  christos int
    564      1.42  christos cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    565      1.42  christos {
    566      1.42  christos 
    567      1.42  christos 	return (seltrue(dev, events, l));
    568      1.42  christos }
    569      1.42  christos 
    570       1.1    abrown /*
    571      1.12        pk  * Miscellaneous helper functions
    572       1.1    abrown  */
    573       1.1    abrown 
    574       1.1    abrown /* Initialize the framebuffer, storing away useful state for later reset */
    575      1.12        pk static void
    576      1.40       uwe cg14_init(struct cgfourteen_softc *sc)
    577       1.1    abrown {
    578      1.66  macallan #if 0
    579      1.45       uwe 	volatile uint32_t *clut;
    580      1.45       uwe 	volatile uint8_t  *xlut;
    581      1.11        pk 	int i;
    582       1.1    abrown 
    583       1.1    abrown 	/*
    584       1.1    abrown 	 * We stash away the following to restore on close:
    585       1.1    abrown 	 *
    586       1.1    abrown 	 * 	color look-up table 1 	(sc->sc_saveclut)
    587       1.1    abrown 	 *	x look-up table		(sc->sc_savexlut)
    588       1.1    abrown 	 *	control register	(sc->sc_savectl)
    589       1.1    abrown 	 *	cursor control register (sc->sc_savehwc)
    590       1.1    abrown 	 */
    591       1.1    abrown 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    592       1.1    abrown 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    593       1.1    abrown 
    594      1.45       uwe 	clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
    595      1.45       uwe 	xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
    596       1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    597       1.1    abrown 		sc->sc_saveclut.cm_chip[i] = clut[i];
    598       1.1    abrown 		sc->sc_savexlut[i] = xlut[i];
    599       1.1    abrown 	}
    600       1.1    abrown 
    601       1.1    abrown 	/*
    602       1.1    abrown 	 * Enable the video and put it in 8 bit mode
    603       1.1    abrown 	 */
    604       1.1    abrown 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    605       1.1    abrown 		CG14_MCTL_POWERCTL;
    606      1.66  macallan #else
    607      1.66  macallan 	cg14_set_depth(sc, 32);
    608      1.66  macallan #endif
    609       1.1    abrown }
    610       1.1    abrown 
    611      1.42  christos static void
    612      1.40       uwe /* Restore the state saved on cg14_init */
    613      1.40       uwe cg14_reset(struct cgfourteen_softc *sc)
    614       1.1    abrown {
    615      1.66  macallan #if 0
    616      1.40       uwe 	volatile uint32_t *clut;
    617      1.40       uwe 	volatile uint8_t  *xlut;
    618      1.11        pk 	int i;
    619       1.1    abrown 
    620       1.1    abrown 	/*
    621       1.1    abrown 	 * We restore the following, saved in cg14_init:
    622       1.1    abrown 	 *
    623       1.1    abrown 	 * 	color look-up table 1 	(sc->sc_saveclut)
    624       1.1    abrown 	 *	x look-up table		(sc->sc_savexlut)
    625       1.1    abrown 	 *	control register	(sc->sc_savectl)
    626       1.1    abrown 	 *	cursor control register (sc->sc_savehwc)
    627       1.1    abrown 	 *
    628       1.1    abrown 	 * Note that we don't touch the video enable bits in the
    629       1.1    abrown 	 * control register; otherwise, screenblank wouldn't work.
    630       1.1    abrown 	 */
    631       1.1    abrown 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    632       1.1    abrown 							CG14_MCTL_POWERCTL)) |
    633       1.1    abrown 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    634       1.1    abrown 						    CG14_MCTL_POWERCTL));
    635       1.1    abrown 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    636       1.1    abrown 
    637      1.39   tsutsui 	clut = sc->sc_clut1->clut_lut;
    638      1.39   tsutsui 	xlut = sc->sc_xlut->xlut_lut;
    639       1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    640       1.1    abrown 		clut[i] = sc->sc_saveclut.cm_chip[i];
    641       1.1    abrown 		xlut[i] = sc->sc_savexlut[i];
    642       1.1    abrown 	}
    643      1.66  macallan #else
    644      1.66  macallan 	cg14_set_depth(sc, 8);
    645      1.66  macallan #endif
    646       1.1    abrown }
    647       1.1    abrown 
    648       1.1    abrown /* Enable/disable video display; power down monitor if DPMS-capable */
    649       1.1    abrown static void
    650      1.40       uwe cg14_set_video(struct cgfourteen_softc *sc, int enable)
    651       1.1    abrown {
    652      1.12        pk 	/*
    653       1.1    abrown 	 * We can only use DPMS to power down the display if the chip revision
    654       1.1    abrown 	 * is greater than 0.
    655       1.1    abrown 	 */
    656       1.1    abrown 	if (enable) {
    657       1.1    abrown 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    658       1.1    abrown 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    659       1.1    abrown 						 CG14_MCTL_POWERCTL);
    660       1.1    abrown 		else
    661       1.1    abrown 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    662       1.1    abrown 	} else {
    663       1.1    abrown 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    664       1.1    abrown 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    665       1.1    abrown 						  CG14_MCTL_POWERCTL);
    666       1.1    abrown 		else
    667       1.1    abrown 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    668       1.1    abrown 	}
    669       1.1    abrown }
    670       1.1    abrown 
    671       1.1    abrown /* Get status of video display */
    672       1.1    abrown static int
    673      1.40       uwe cg14_get_video(struct cgfourteen_softc *sc)
    674       1.1    abrown {
    675       1.1    abrown 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    676       1.1    abrown }
    677       1.1    abrown 
    678       1.1    abrown /* Read the software shadow colormap */
    679      1.12        pk static int
    680      1.40       uwe cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    681       1.1    abrown {
    682      1.42  christos         u_int i, start, count;
    683      1.42  christos         u_char *cp;
    684      1.42  christos         int error;
    685      1.42  christos 
    686      1.42  christos         start = p->index;
    687      1.42  christos         count = p->count;
    688      1.42  christos         if (start >= cmsize || count > cmsize - start)
    689      1.42  christos                 return (EINVAL);
    690      1.42  christos 
    691      1.42  christos         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    692      1.42  christos                 error = copyout(&cp[3], &p->red[i], 1);
    693      1.42  christos                 if (error)
    694      1.42  christos                         return error;
    695      1.42  christos                 error = copyout(&cp[2], &p->green[i], 1);
    696      1.42  christos                 if (error)
    697      1.42  christos                         return error;
    698      1.42  christos                 error = copyout(&cp[1], &p->blue[i], 1);
    699      1.42  christos                 if (error)
    700      1.42  christos                         return error;
    701      1.42  christos         }
    702      1.42  christos         return (0);
    703       1.1    abrown }
    704       1.1    abrown 
    705       1.1    abrown /* Write the software shadow colormap */
    706       1.1    abrown static int
    707      1.40       uwe cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    708      1.24    itojun {
    709      1.42  christos         u_int i, start, count;
    710      1.42  christos         u_char *cp;
    711      1.42  christos         u_char cmap[256][4];
    712      1.42  christos         int error;
    713      1.42  christos 
    714      1.42  christos         start = p->index;
    715      1.42  christos         count = p->count;
    716      1.42  christos         if (start >= cmsize || count > cmsize - start)
    717      1.42  christos                 return (EINVAL);
    718      1.42  christos 
    719      1.42  christos         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    720      1.42  christos         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    721      1.42  christos                 error = copyin(&p->red[i], &cp[3], 1);
    722      1.42  christos                 if (error)
    723      1.42  christos                         return error;
    724      1.42  christos                 error = copyin(&p->green[i], &cp[2], 1);
    725      1.42  christos                 if (error)
    726      1.42  christos                         return error;
    727      1.42  christos                 error = copyin(&p->blue[i], &cp[1], 1);
    728      1.42  christos                 if (error)
    729      1.42  christos                         return error;
    730      1.42  christos                 cp[0] = 0;      /* no alpha channel */
    731      1.42  christos         }
    732      1.42  christos         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    733      1.42  christos         return (0);
    734       1.1    abrown }
    735       1.1    abrown 
    736       1.1    abrown static void
    737      1.40       uwe cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    738       1.1    abrown {
    739       1.1    abrown 	/* XXX switch to auto-increment, and on retrace intr */
    740      1.40       uwe 
    741       1.1    abrown 	/* Setup pointers to source and dest */
    742      1.40       uwe 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    743      1.40       uwe 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    744       1.1    abrown 
    745       1.1    abrown 	/* Copy by words */
    746       1.1    abrown 	while (--ncolors >= 0)
    747       1.1    abrown 		*lutp++ = *colp++;
    748       1.1    abrown }
    749      1.49  macallan 
    750      1.49  macallan #if NWSDISPLAY > 0
    751      1.49  macallan static void
    752      1.49  macallan cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
    753      1.49  macallan {
    754      1.49  macallan 	struct wsemuldisplaydev_attach_args aa;
    755      1.49  macallan 	struct rasops_info *ri;
    756      1.49  macallan 	long defattr;
    757      1.49  macallan 
    758      1.49  macallan  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    759      1.49  macallan 		"default",
    760      1.49  macallan 		0, 0,
    761      1.49  macallan 		NULL,
    762      1.49  macallan 		8, 16,
    763      1.49  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    764      1.49  macallan 		NULL
    765      1.49  macallan 	};
    766      1.49  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    767      1.49  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    768      1.49  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    769      1.49  macallan 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
    770      1.49  macallan 	    &cg14_accessops);
    771      1.49  macallan 	sc->sc_vd.init_screen = cg14_init_screen;
    772      1.49  macallan 
    773      1.49  macallan 	ri = &sc->sc_console_screen.scr_ri;
    774      1.49  macallan 
    775      1.49  macallan 	if (is_cons) {
    776      1.49  macallan 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
    777      1.49  macallan 		    &defattr);
    778      1.55  macallan 
    779      1.55  macallan 		/* clear the screen with the default background colour */
    780      1.55  macallan 		memset(sc->sc_fb.fb_pixels,
    781      1.55  macallan 		       (defattr >> 16) & 0xff,
    782      1.55  macallan 		       ri->ri_stride * ri->ri_height);
    783      1.49  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    784      1.49  macallan 
    785      1.49  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    786      1.49  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    787      1.49  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    788      1.49  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    789      1.49  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    790      1.49  macallan 		    defattr);
    791      1.62  macallan 		vcons_replay_msgbuf(&sc->sc_console_screen);
    792      1.49  macallan 	} else {
    793      1.49  macallan 		/*
    794      1.49  macallan 		 * since we're not the console we can postpone the rest
    795      1.49  macallan 		 * until someone actually allocates a screen for us
    796      1.49  macallan 		 */
    797      1.49  macallan 	}
    798      1.49  macallan 
    799      1.49  macallan 	cg14_init_cmap(sc);
    800      1.49  macallan 
    801      1.49  macallan 	aa.console = is_cons;
    802      1.49  macallan 	aa.scrdata = &sc->sc_screenlist;
    803      1.49  macallan 	aa.accessops = &cg14_accessops;
    804      1.49  macallan 	aa.accesscookie = &sc->sc_vd;
    805      1.49  macallan 
    806      1.53  macallan 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    807      1.49  macallan }
    808      1.49  macallan 
    809      1.49  macallan static void
    810      1.49  macallan cg14_init_cmap(struct cgfourteen_softc *sc)
    811      1.49  macallan {
    812      1.49  macallan 	int i, j = 0;
    813      1.49  macallan 
    814      1.49  macallan 	for (i = 0; i < 256; i++) {
    815      1.49  macallan 
    816      1.49  macallan 		sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
    817      1.49  macallan 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
    818      1.49  macallan 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
    819      1.49  macallan 		j += 3;
    820      1.49  macallan 	}
    821      1.49  macallan 	cg14_load_hwcmap(sc, 0, 256);
    822      1.49  macallan }
    823      1.49  macallan 
    824      1.49  macallan static int
    825      1.49  macallan cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    826      1.49  macallan {
    827      1.49  macallan 	u_int index = cm->index;
    828      1.49  macallan 	u_int count = cm->count;
    829      1.49  macallan 	int i, error;
    830      1.49  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    831      1.49  macallan 
    832      1.49  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    833      1.49  macallan 	    (cm->index + cm->count) > 256)
    834      1.49  macallan 		return EINVAL;
    835      1.49  macallan 	error = copyin(cm->red, &rbuf[index], count);
    836      1.49  macallan 	if (error)
    837      1.49  macallan 		return error;
    838      1.49  macallan 	error = copyin(cm->green, &gbuf[index], count);
    839      1.49  macallan 	if (error)
    840      1.49  macallan 		return error;
    841      1.49  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    842      1.49  macallan 	if (error)
    843      1.49  macallan 		return error;
    844      1.49  macallan 
    845      1.49  macallan 	for (i = 0; i < count; i++) {
    846      1.49  macallan 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
    847      1.49  macallan 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
    848      1.49  macallan 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
    849      1.49  macallan 
    850      1.49  macallan 		index++;
    851      1.49  macallan 	}
    852      1.49  macallan 	cg14_load_hwcmap(sc, 0, 256);
    853      1.49  macallan 	return 0;
    854      1.49  macallan }
    855      1.49  macallan 
    856      1.49  macallan static int
    857      1.49  macallan cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    858      1.49  macallan {
    859      1.49  macallan 	uint8_t rbuf[256], gbuf[256], bbuf[256];
    860      1.49  macallan 	u_int index = cm->index;
    861      1.49  macallan 	u_int count = cm->count;
    862      1.49  macallan 	int error, i;
    863      1.49  macallan 
    864      1.49  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    865      1.49  macallan 		return EINVAL;
    866      1.49  macallan 
    867      1.49  macallan 
    868      1.49  macallan 	for (i = 0; i < count; i++) {
    869      1.49  macallan 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
    870      1.49  macallan 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
    871      1.49  macallan 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
    872      1.49  macallan 
    873      1.49  macallan 		index++;
    874      1.49  macallan 	}
    875      1.49  macallan 	error = copyout(rbuf,   cm->red,   count);
    876      1.49  macallan 	if (error)
    877      1.49  macallan 		return error;
    878      1.49  macallan 	error = copyout(gbuf, cm->green, count);
    879      1.49  macallan 	if (error)
    880      1.49  macallan 		return error;
    881      1.49  macallan 	error = copyout(bbuf,  cm->blue,  count);
    882      1.49  macallan 	if (error)
    883      1.49  macallan 		return error;
    884      1.49  macallan 
    885      1.49  macallan 	return 0;
    886      1.49  macallan }
    887      1.63  macallan 
    888      1.49  macallan static int
    889      1.49  macallan cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    890      1.49  macallan 	struct lwp *l)
    891      1.49  macallan {
    892      1.49  macallan 	struct vcons_data *vd = v;
    893      1.49  macallan 	struct cgfourteen_softc *sc = vd->cookie;
    894      1.49  macallan 	struct wsdisplay_fbinfo *wdf;
    895      1.49  macallan 	struct vcons_screen *ms = vd->active;
    896      1.49  macallan 
    897      1.49  macallan 	switch (cmd) {
    898      1.49  macallan 
    899      1.49  macallan 		case WSDISPLAYIO_GTYPE:
    900      1.49  macallan 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
    901      1.49  macallan 			return 0;
    902      1.49  macallan 
    903      1.49  macallan 		case WSDISPLAYIO_GINFO:
    904      1.49  macallan 			wdf = (void *)data;
    905      1.49  macallan 			wdf->height = ms->scr_ri.ri_height;
    906      1.49  macallan 			wdf->width = ms->scr_ri.ri_width;
    907      1.49  macallan 			wdf->depth = 32;
    908      1.49  macallan 			wdf->cmsize = 256;
    909      1.49  macallan 			return 0;
    910      1.49  macallan 
    911      1.49  macallan 		case WSDISPLAYIO_GETCMAP:
    912      1.49  macallan 			return cg14_getcmap(sc,
    913      1.49  macallan 			    (struct wsdisplay_cmap *)data);
    914      1.49  macallan 
    915      1.49  macallan 		case WSDISPLAYIO_PUTCMAP:
    916      1.49  macallan 			return cg14_putcmap(sc,
    917      1.49  macallan 			    (struct wsdisplay_cmap *)data);
    918      1.49  macallan 
    919      1.49  macallan 		case WSDISPLAYIO_LINEBYTES:
    920      1.55  macallan 			*(u_int *)data = ms->scr_ri.ri_stride << 2;
    921      1.49  macallan 			return 0;
    922      1.49  macallan 
    923      1.49  macallan 		case WSDISPLAYIO_SMODE:
    924      1.49  macallan 			{
    925      1.49  macallan 				int new_mode = *(int*)data;
    926      1.49  macallan 				if (new_mode != sc->sc_mode) {
    927      1.49  macallan 					sc->sc_mode = new_mode;
    928      1.49  macallan 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    929      1.50  macallan 						bus_space_write_1(sc->sc_bustag,
    930      1.50  macallan 						    sc->sc_regh,
    931      1.50  macallan 						    CG14_CURSOR_CONTROL, 0);
    932      1.66  macallan 
    933      1.49  macallan 						cg14_set_depth(sc, 8);
    934      1.49  macallan 						cg14_init_cmap(sc);
    935      1.49  macallan 						vcons_redraw_screen(ms);
    936      1.55  macallan 					} else {
    937      1.66  macallan 
    938      1.49  macallan 						cg14_set_depth(sc, 32);
    939      1.55  macallan 					}
    940      1.49  macallan 				}
    941      1.49  macallan 			}
    942      1.49  macallan 			return 0;
    943      1.50  macallan 		case WSDISPLAYIO_SVIDEO:
    944      1.50  macallan 			cg14_set_video(sc, *(int *)data);
    945      1.50  macallan 			return 0;
    946      1.50  macallan 		case WSDISPLAYIO_GVIDEO:
    947      1.50  macallan 			return cg14_get_video(sc) ?
    948      1.50  macallan 			    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    949      1.50  macallan 		case WSDISPLAYIO_GCURPOS:
    950      1.50  macallan 			{
    951      1.50  macallan 				struct wsdisplay_curpos *cp = (void *)data;
    952      1.50  macallan 
    953      1.50  macallan 				cp->x = sc->sc_cursor.cc_pos.x;
    954      1.50  macallan 				cp->y = sc->sc_cursor.cc_pos.y;
    955      1.50  macallan 			}
    956      1.50  macallan 			return 0;
    957      1.50  macallan 		case WSDISPLAYIO_SCURPOS:
    958      1.50  macallan 			{
    959      1.50  macallan 				struct wsdisplay_curpos *cp = (void *)data;
    960      1.50  macallan 
    961      1.50  macallan 				cg14_move_cursor(sc, cp->x, cp->y);
    962      1.50  macallan 			}
    963      1.50  macallan 			return 0;
    964      1.50  macallan 		case WSDISPLAYIO_GCURMAX:
    965      1.50  macallan 			{
    966      1.50  macallan 				struct wsdisplay_curpos *cp = (void *)data;
    967      1.50  macallan 
    968      1.50  macallan 				cp->x = 32;
    969      1.50  macallan 				cp->y = 32;
    970      1.50  macallan 			}
    971      1.50  macallan 			return 0;
    972      1.50  macallan 		case WSDISPLAYIO_SCURSOR:
    973      1.50  macallan 			{
    974      1.50  macallan 				struct wsdisplay_cursor *cursor = (void *)data;
    975      1.50  macallan 
    976      1.50  macallan 				return cg14_do_cursor(sc, cursor);
    977      1.50  macallan 			}
    978      1.55  macallan 		case PCI_IOC_CFGREAD:
    979      1.55  macallan 		case PCI_IOC_CFGWRITE:
    980      1.55  macallan 			return EINVAL;
    981      1.55  macallan 
    982      1.49  macallan 	}
    983      1.49  macallan 	return EPASSTHROUGH;
    984      1.49  macallan }
    985      1.49  macallan 
    986      1.49  macallan static paddr_t
    987      1.49  macallan cg14_mmap(void *v, void *vs, off_t offset, int prot)
    988      1.49  macallan {
    989      1.49  macallan 	struct vcons_data *vd = v;
    990      1.49  macallan 	struct cgfourteen_softc *sc = vd->cookie;
    991      1.49  macallan 
    992      1.49  macallan 	/* allow mmap()ing the full framebuffer, not just what we use */
    993      1.49  macallan 	if (offset < sc->sc_vramsize)
    994      1.49  macallan 		return bus_space_mmap(sc->sc_bustag,
    995      1.49  macallan 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    996      1.49  macallan 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    997      1.49  macallan 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
    998      1.49  macallan 
    999      1.49  macallan 	return -1;
   1000      1.49  macallan }
   1001      1.49  macallan 
   1002      1.49  macallan static void
   1003      1.49  macallan cg14_init_screen(void *cookie, struct vcons_screen *scr,
   1004      1.49  macallan     int existing, long *defattr)
   1005      1.49  macallan {
   1006      1.49  macallan 	struct cgfourteen_softc *sc = cookie;
   1007      1.49  macallan 	struct rasops_info *ri = &scr->scr_ri;
   1008      1.49  macallan 
   1009      1.49  macallan 	ri->ri_depth = 8;
   1010      1.49  macallan 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
   1011      1.49  macallan 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
   1012      1.49  macallan 	ri->ri_stride = ri->ri_width;
   1013      1.49  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
   1014      1.49  macallan 
   1015      1.65  macallan 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
   1016  1.67.6.2       tls #if NSX > 0
   1017  1.67.6.2       tls 	/*
   1018  1.67.6.2       tls 	 * unaligned copies with horizontal overlap are slow, so don't bother
   1019  1.67.6.2       tls 	 * handling them in cg14_bitblt() and use putchar() instead
   1020  1.67.6.2       tls 	 */
   1021  1.67.6.2       tls 	if (sc->sc_sx != NULL) {
   1022  1.67.6.2       tls 		scr->scr_flags |= VCONS_NO_COPYCOLS;
   1023  1.67.6.2       tls 	} else
   1024  1.67.6.2       tls #endif
   1025      1.65  macallan 	scr->scr_flags |= VCONS_DONT_READ;
   1026      1.49  macallan 
   1027      1.49  macallan 	if (existing) {
   1028      1.49  macallan 		ri->ri_flg |= RI_CLEAR;
   1029      1.49  macallan 	}
   1030      1.49  macallan 
   1031      1.67  macallan 	rasops_init(ri, 0, 0);
   1032      1.49  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1033      1.49  macallan 
   1034      1.49  macallan 	rasops_reconfig(ri,
   1035      1.49  macallan 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
   1036      1.49  macallan 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
   1037      1.49  macallan 
   1038      1.49  macallan 	ri->ri_hw = scr;
   1039  1.67.6.2       tls #if NSX > 0
   1040  1.67.6.2       tls 	if (sc->sc_sx != NULL) {
   1041  1.67.6.2       tls 		ri->ri_ops.copyrows = cg14_copyrows;
   1042  1.67.6.2       tls 		ri->ri_ops.copycols = cg14_copycols;
   1043  1.67.6.2       tls 		ri->ri_ops.eraserows = cg14_eraserows;
   1044  1.67.6.2       tls 		ri->ri_ops.erasecols = cg14_erasecols;
   1045  1.67.6.2       tls 		ri->ri_ops.cursor = cg14_cursor;
   1046  1.67.6.2       tls #if 0
   1047  1.67.6.2       tls 		if (FONT_IS_ALPHA(ri->ri_font)) {
   1048  1.67.6.2       tls 			ri->ri_ops.putchar = cg14_putchar_aa;
   1049  1.67.6.2       tls 		} else
   1050  1.67.6.2       tls #endif
   1051  1.67.6.2       tls 			ri->ri_ops.putchar = cg14_putchar;
   1052  1.67.6.2       tls 	}
   1053  1.67.6.2       tls #endif /* NSX > 0 */
   1054      1.49  macallan }
   1055      1.49  macallan 
   1056      1.49  macallan static void
   1057      1.49  macallan cg14_set_depth(struct cgfourteen_softc *sc, int depth)
   1058      1.49  macallan {
   1059      1.49  macallan 	int i;
   1060      1.49  macallan 
   1061      1.49  macallan 	if (sc->sc_depth == depth)
   1062      1.49  macallan 		return;
   1063      1.66  macallan 
   1064      1.49  macallan 	switch (depth) {
   1065      1.49  macallan 		case 8:
   1066      1.49  macallan 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1067      1.49  macallan 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1068      1.49  macallan 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
   1069      1.49  macallan 			sc->sc_depth = 8;
   1070      1.49  macallan 			/* everything is CLUT1 */
   1071      1.49  macallan 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1072      1.49  macallan 			     sc->sc_xlut->xlut_lut[i] = 0;
   1073      1.49  macallan 			break;
   1074      1.49  macallan 		case 32:
   1075      1.49  macallan 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1076      1.49  macallan 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1077      1.49  macallan 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
   1078      1.49  macallan 			sc->sc_depth = 32;
   1079      1.49  macallan 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1080      1.49  macallan 			     sc->sc_xlut->xlut_lut[i] = 0;
   1081      1.49  macallan 			break;
   1082      1.49  macallan 		default:
   1083      1.49  macallan 			printf("%s: can't change to depth %d\n",
   1084      1.53  macallan 			    device_xname(sc->sc_dev), depth);
   1085      1.49  macallan 	}
   1086      1.49  macallan }
   1087      1.50  macallan 
   1088      1.50  macallan static void
   1089      1.50  macallan cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
   1090      1.50  macallan {
   1091      1.50  macallan 	uint32_t pos;
   1092      1.50  macallan 
   1093      1.50  macallan 	sc->sc_cursor.cc_pos.x = x;
   1094      1.50  macallan 	sc->sc_cursor.cc_pos.y = y;
   1095      1.50  macallan 	pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
   1096      1.50  macallan 	      ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
   1097      1.50  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
   1098      1.50  macallan }
   1099      1.50  macallan 
   1100      1.50  macallan static int
   1101      1.50  macallan cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
   1102      1.50  macallan {
   1103      1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1104      1.50  macallan 
   1105      1.50  macallan 		bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1106      1.50  macallan 		    CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
   1107      1.50  macallan 	}
   1108      1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1109      1.50  macallan 
   1110      1.50  macallan 		sc->sc_cursor.cc_hot.x = cur->hot.x;
   1111      1.50  macallan 		sc->sc_cursor.cc_hot.y = cur->hot.y;
   1112      1.50  macallan 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1113      1.50  macallan 	}
   1114      1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1115      1.50  macallan 
   1116      1.50  macallan 		cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
   1117      1.50  macallan 	}
   1118      1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1119      1.50  macallan 		int i;
   1120      1.50  macallan 		uint32_t val;
   1121      1.50  macallan 
   1122      1.55  macallan 		for (i = 0; i < min(cur->cmap.count, 3); i++) {
   1123      1.50  macallan 			val = (cur->cmap.red[i] ) |
   1124      1.50  macallan 			      (cur->cmap.green[i] << 8) |
   1125      1.50  macallan 			      (cur->cmap.blue[i] << 16);
   1126      1.50  macallan 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1127      1.50  macallan 			    CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
   1128      1.50  macallan 			    val);
   1129      1.50  macallan 		}
   1130      1.50  macallan 	}
   1131      1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1132      1.50  macallan 		uint32_t buffer[32], latch, tmp;
   1133      1.50  macallan 		int i;
   1134      1.50  macallan 
   1135      1.55  macallan 		copyin(cur->mask, buffer, 128);
   1136      1.50  macallan 		for (i = 0; i < 32; i++) {
   1137      1.50  macallan 			latch = 0;
   1138      1.50  macallan 			tmp = buffer[i] & 0x80808080;
   1139      1.50  macallan 			latch |= tmp >> 7;
   1140      1.50  macallan 			tmp = buffer[i] & 0x40404040;
   1141      1.50  macallan 			latch |= tmp >> 5;
   1142      1.50  macallan 			tmp = buffer[i] & 0x20202020;
   1143      1.50  macallan 			latch |= tmp >> 3;
   1144      1.50  macallan 			tmp = buffer[i] & 0x10101010;
   1145      1.50  macallan 			latch |= tmp >> 1;
   1146      1.50  macallan 			tmp = buffer[i] & 0x08080808;
   1147      1.50  macallan 			latch |= tmp << 1;
   1148      1.50  macallan 			tmp = buffer[i] & 0x04040404;
   1149      1.50  macallan 			latch |= tmp << 3;
   1150      1.50  macallan 			tmp = buffer[i] & 0x02020202;
   1151      1.50  macallan 			latch |= tmp << 5;
   1152      1.50  macallan 			tmp = buffer[i] & 0x01010101;
   1153      1.50  macallan 			latch |= tmp << 7;
   1154      1.50  macallan 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1155      1.50  macallan 			    CG14_CURSOR_PLANE0 + (i << 2), latch);
   1156      1.50  macallan 		}
   1157      1.55  macallan 		copyin(cur->image, buffer, 128);
   1158      1.50  macallan 		for (i = 0; i < 32; i++) {
   1159      1.50  macallan 			latch = 0;
   1160      1.50  macallan 			tmp = buffer[i] & 0x80808080;
   1161      1.50  macallan 			latch |= tmp >> 7;
   1162      1.50  macallan 			tmp = buffer[i] & 0x40404040;
   1163      1.50  macallan 			latch |= tmp >> 5;
   1164      1.50  macallan 			tmp = buffer[i] & 0x20202020;
   1165      1.50  macallan 			latch |= tmp >> 3;
   1166      1.50  macallan 			tmp = buffer[i] & 0x10101010;
   1167      1.50  macallan 			latch |= tmp >> 1;
   1168      1.50  macallan 			tmp = buffer[i] & 0x08080808;
   1169      1.50  macallan 			latch |= tmp << 1;
   1170      1.50  macallan 			tmp = buffer[i] & 0x04040404;
   1171      1.50  macallan 			latch |= tmp << 3;
   1172      1.50  macallan 			tmp = buffer[i] & 0x02020202;
   1173      1.50  macallan 			latch |= tmp << 5;
   1174      1.50  macallan 			tmp = buffer[i] & 0x01010101;
   1175      1.50  macallan 			latch |= tmp << 7;
   1176      1.50  macallan 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1177      1.50  macallan 			    CG14_CURSOR_PLANE1 + (i << 2), latch);
   1178      1.50  macallan 		}
   1179      1.50  macallan 	}
   1180      1.50  macallan 	return 0;
   1181      1.50  macallan }
   1182  1.67.6.2       tls 
   1183  1.67.6.2       tls #if NSX > 0
   1184  1.67.6.2       tls 
   1185  1.67.6.2       tls static void
   1186  1.67.6.2       tls cg14_wait_idle(struct cgfourteen_softc *sc)
   1187  1.67.6.2       tls {
   1188  1.67.6.2       tls }
   1189  1.67.6.2       tls 
   1190  1.67.6.2       tls static void
   1191  1.67.6.2       tls cg14_rectfill(struct cgfourteen_softc *sc, int x, int y, int wi, int he,
   1192  1.67.6.2       tls      uint32_t colour)
   1193  1.67.6.2       tls {
   1194  1.67.6.2       tls 	uint32_t addr, pptr;
   1195  1.67.6.2       tls 	int line, cnt, pre, words;
   1196  1.67.6.2       tls 	int stride = sc->sc_fb.fb_type.fb_width;
   1197  1.67.6.2       tls 
   1198  1.67.6.2       tls 	addr = sc->sc_fb_paddr + x + stride * y;
   1199  1.67.6.2       tls 	sx_write(sc->sc_sx, SX_QUEUED(8), colour);
   1200  1.67.6.2       tls 	sx_write(sc->sc_sx, SX_QUEUED(9), colour);
   1201  1.67.6.2       tls 	/*
   1202  1.67.6.2       tls 	 * Calculate the number of pixels we need to do one by one
   1203  1.67.6.2       tls 	 * until we're 32bit aligned, then do the rest in 32bit
   1204  1.67.6.2       tls 	 * mode. Assumes that stride is always a multiple of 4.
   1205  1.67.6.2       tls 	 */
   1206  1.67.6.2       tls 	pre = addr & 3;
   1207  1.67.6.2       tls 	if (pre != 0) pre = 4 - pre;
   1208  1.67.6.2       tls 	for (line = 0; line < he; line++) {
   1209  1.67.6.2       tls 		pptr = addr;
   1210  1.67.6.2       tls 		cnt = wi;
   1211  1.67.6.2       tls 		if (pre) {
   1212  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_STBS(8, pre - 1, pptr & 7));
   1213  1.67.6.2       tls 			pptr += pre;
   1214  1.67.6.2       tls 			cnt -= pre;
   1215  1.67.6.2       tls 		}
   1216  1.67.6.2       tls 		/* now do the aligned pixels in 32bit chunks */
   1217  1.67.6.2       tls 		while(cnt > 31) {
   1218  1.67.6.2       tls 			words = min(32, cnt >> 2);
   1219  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_STS(8, words - 1, pptr & 7));
   1220  1.67.6.2       tls 			pptr += words << 2;
   1221  1.67.6.2       tls 			cnt -= words << 2;
   1222  1.67.6.2       tls 		}
   1223  1.67.6.2       tls 		/* do any remaining pixels byte-wise again */
   1224  1.67.6.2       tls 		if (cnt > 0)
   1225  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_STBS(8, cnt - 1, pptr & 7));
   1226  1.67.6.2       tls 		addr += stride;
   1227  1.67.6.2       tls 	}
   1228  1.67.6.2       tls }
   1229  1.67.6.2       tls 
   1230  1.67.6.2       tls static void
   1231  1.67.6.2       tls cg14_invert(struct cgfourteen_softc *sc, int x, int y, int wi, int he)
   1232  1.67.6.2       tls {
   1233  1.67.6.2       tls 	uint32_t addr, pptr;
   1234  1.67.6.2       tls 	int line, cnt, pre, words;
   1235  1.67.6.2       tls 	int stride = sc->sc_fb.fb_type.fb_width;
   1236  1.67.6.2       tls 
   1237  1.67.6.2       tls 	addr = sc->sc_fb_paddr + x + stride * y;
   1238  1.67.6.2       tls 	sx_write(sc->sc_sx, SX_ROP_CONTROL, 0x33); /* ~src a */
   1239  1.67.6.2       tls 	/*
   1240  1.67.6.2       tls 	 * Calculate the number of pixels we need to do one by one
   1241  1.67.6.2       tls 	 * until we're 32bit aligned, then do the rest in 32bit
   1242  1.67.6.2       tls 	 * mode. Assumes that stride is always a multiple of 4.
   1243  1.67.6.2       tls 	 */
   1244  1.67.6.2       tls 	pre = addr & 3;
   1245  1.67.6.2       tls 	if (pre != 0) pre = 4 - pre;
   1246  1.67.6.2       tls 	for (line = 0; line < he; line++) {
   1247  1.67.6.2       tls 		pptr = addr;
   1248  1.67.6.2       tls 		cnt = wi;
   1249  1.67.6.2       tls 		if (pre) {
   1250  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_LDB(8, pre - 1, pptr & 7));
   1251  1.67.6.2       tls 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
   1252  1.67.6.2       tls 			    SX_ROP(8, 8, 32, pre - 1));
   1253  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_STB(32, pre - 1, pptr & 7));
   1254  1.67.6.2       tls 			pptr += pre;
   1255  1.67.6.2       tls 			cnt -= pre;
   1256  1.67.6.2       tls 		}
   1257  1.67.6.2       tls 		/* now do the aligned pixels in 32bit chunks */
   1258  1.67.6.2       tls 		while(cnt > 15) {
   1259  1.67.6.2       tls 			words = min(16, cnt >> 2);
   1260  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_LD(8, words - 1, pptr & 7));
   1261  1.67.6.2       tls 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
   1262  1.67.6.2       tls 			    SX_ROP(8, 8, 32, words - 1));
   1263  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_ST(32, words - 1, pptr & 7));
   1264  1.67.6.2       tls 			pptr += words << 2;
   1265  1.67.6.2       tls 			cnt -= words << 2;
   1266  1.67.6.2       tls 		}
   1267  1.67.6.2       tls 		/* do any remaining pixels byte-wise again */
   1268  1.67.6.2       tls 		if (cnt > 0)
   1269  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_LDB(8, cnt - 1, pptr & 7));
   1270  1.67.6.2       tls 			sx_write(sc->sc_sx, SX_INSTRUCTIONS,
   1271  1.67.6.2       tls 			    SX_ROP(8, 8, 32, cnt - 1));
   1272  1.67.6.2       tls 			sta(pptr, ASI_SX, SX_STB(32, cnt - 1, pptr & 7));
   1273  1.67.6.2       tls 		addr += stride;
   1274  1.67.6.2       tls 	}
   1275  1.67.6.2       tls }
   1276  1.67.6.2       tls 
   1277  1.67.6.2       tls static inline void
   1278  1.67.6.2       tls cg14_slurp(int reg, uint32_t addr, int cnt)
   1279  1.67.6.2       tls {
   1280  1.67.6.2       tls 	int num;
   1281  1.67.6.2       tls 	while (cnt > 0) {
   1282  1.67.6.2       tls 		num = min(32, cnt);
   1283  1.67.6.2       tls 		sta(addr, ASI_SX, SX_LD(reg, num - 1, addr & 7));
   1284  1.67.6.2       tls 		cnt -= num;
   1285  1.67.6.2       tls 		reg += num;
   1286  1.67.6.2       tls 		addr += (num << 2);
   1287  1.67.6.2       tls 	}
   1288  1.67.6.2       tls }
   1289  1.67.6.2       tls 
   1290  1.67.6.2       tls static inline void
   1291  1.67.6.2       tls cg14_spit(int reg, uint32_t addr, int cnt)
   1292  1.67.6.2       tls {
   1293  1.67.6.2       tls 	int num;
   1294  1.67.6.2       tls 	while (cnt > 0) {
   1295  1.67.6.2       tls 		num = min(32, cnt);
   1296  1.67.6.2       tls 		sta(addr, ASI_SX, SX_ST(reg, num - 1, addr & 7));
   1297  1.67.6.2       tls 		cnt -= num;
   1298  1.67.6.2       tls 		reg += num;
   1299  1.67.6.2       tls 		addr += (num << 2);
   1300  1.67.6.2       tls 	}
   1301  1.67.6.2       tls }
   1302  1.67.6.2       tls 
   1303  1.67.6.2       tls static void
   1304  1.67.6.2       tls cg14_bitblt(void *cookie, int xs, int ys, int xd, int yd,
   1305  1.67.6.2       tls     int wi, int he, int rop)
   1306  1.67.6.2       tls {
   1307  1.67.6.2       tls 	struct cgfourteen_softc *sc = cookie;
   1308  1.67.6.2       tls 	uint32_t saddr, daddr, sptr, dptr;
   1309  1.67.6.2       tls 	int line, cnt, stride = sc->sc_fb.fb_type.fb_width;
   1310  1.67.6.2       tls 	int num, words, skip;
   1311  1.67.6.2       tls 
   1312  1.67.6.2       tls 	if (ys < yd) {
   1313  1.67.6.2       tls 		/* need to go bottom-up */
   1314  1.67.6.2       tls 		saddr = sc->sc_fb_paddr + xs + stride * (ys + he - 1);
   1315  1.67.6.2       tls 		daddr = sc->sc_fb_paddr + xd + stride * (yd + he - 1);
   1316  1.67.6.2       tls 		skip = -stride;
   1317  1.67.6.2       tls 	} else {
   1318  1.67.6.2       tls 		saddr = sc->sc_fb_paddr + xs + stride * ys;
   1319  1.67.6.2       tls 		daddr = sc->sc_fb_paddr + xd + stride * yd;
   1320  1.67.6.2       tls 		skip = stride;
   1321  1.67.6.2       tls 	}
   1322  1.67.6.2       tls 
   1323  1.67.6.2       tls 	if ((saddr & 3) == (daddr & 3)) {
   1324  1.67.6.2       tls 		int pre = saddr & 3;	/* pixels to copy byte-wise */
   1325  1.67.6.2       tls 		if (pre != 0) pre = 4 - pre;
   1326  1.67.6.2       tls 		for (line = 0; line < he; line++) {
   1327  1.67.6.2       tls 			sptr = saddr;
   1328  1.67.6.2       tls 			dptr = daddr;
   1329  1.67.6.2       tls 			cnt = wi;
   1330  1.67.6.2       tls 			if (pre > 0) {
   1331  1.67.6.2       tls 				sta(sptr, ASI_SX,
   1332  1.67.6.2       tls 				    SX_LDB(32, pre - 1, sptr & 7));
   1333  1.67.6.2       tls 				sta(dptr, ASI_SX,
   1334  1.67.6.2       tls 				    SX_STB(32, pre - 1, dptr & 7));
   1335  1.67.6.2       tls 				cnt -= pre;
   1336  1.67.6.2       tls 				sptr += pre;
   1337  1.67.6.2       tls 				dptr += pre;
   1338  1.67.6.2       tls 			}
   1339  1.67.6.2       tls 			words = cnt >> 2;
   1340  1.67.6.2       tls 			while(cnt > 3) {
   1341  1.67.6.2       tls 				num = min(120, words);
   1342  1.67.6.2       tls 				cg14_slurp(8, sptr, num);
   1343  1.67.6.2       tls 				cg14_spit(8, dptr, num);
   1344  1.67.6.2       tls 				sptr += num << 2;
   1345  1.67.6.2       tls 				dptr += num << 2;
   1346  1.67.6.2       tls 				cnt -= num << 2;
   1347  1.67.6.2       tls 			}
   1348  1.67.6.2       tls 			if (cnt > 0) {
   1349  1.67.6.2       tls 				sta(sptr, ASI_SX,
   1350  1.67.6.2       tls 				    SX_LDB(32, cnt - 1, sptr & 7));
   1351  1.67.6.2       tls 				sta(dptr, ASI_SX,
   1352  1.67.6.2       tls 				    SX_STB(32, cnt - 1, dptr & 7));
   1353  1.67.6.2       tls 			}
   1354  1.67.6.2       tls 			saddr += skip;
   1355  1.67.6.2       tls 			daddr += skip;
   1356  1.67.6.2       tls 		}
   1357  1.67.6.2       tls 	} else {
   1358  1.67.6.2       tls 		/* unaligned, have to use byte mode */
   1359  1.67.6.2       tls 		for (line = 0; line < he; line++) {
   1360  1.67.6.2       tls 			sptr = saddr;
   1361  1.67.6.2       tls 			dptr = daddr;
   1362  1.67.6.2       tls 			cnt = wi;
   1363  1.67.6.2       tls 			while(cnt > 31) {
   1364  1.67.6.2       tls 				sta(sptr, ASI_SX, SX_LDB(32, 31, sptr & 7));
   1365  1.67.6.2       tls 				sta(dptr, ASI_SX, SX_STB(32, 31, dptr & 7));
   1366  1.67.6.2       tls 				sptr += 32;
   1367  1.67.6.2       tls 				dptr += 32;
   1368  1.67.6.2       tls 				cnt -= 32;
   1369  1.67.6.2       tls 			}
   1370  1.67.6.2       tls 			if (cnt > 0) {
   1371  1.67.6.2       tls 				sta(sptr, ASI_SX,
   1372  1.67.6.2       tls 				    SX_LDB(32, cnt - 1, sptr & 7));
   1373  1.67.6.2       tls 				sta(dptr, ASI_SX,
   1374  1.67.6.2       tls 				    SX_STB(32, cnt - 1, dptr & 7));
   1375  1.67.6.2       tls 			}
   1376  1.67.6.2       tls 			saddr += skip;
   1377  1.67.6.2       tls 			daddr += skip;
   1378  1.67.6.2       tls 		}
   1379  1.67.6.2       tls 	}
   1380  1.67.6.2       tls }
   1381  1.67.6.2       tls 
   1382  1.67.6.2       tls 
   1383  1.67.6.2       tls static void
   1384  1.67.6.2       tls cg14_putchar(void *cookie, int row, int col, u_int c, long attr)
   1385  1.67.6.2       tls {
   1386  1.67.6.2       tls 	struct rasops_info *ri = cookie;
   1387  1.67.6.2       tls 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1388  1.67.6.2       tls 	struct vcons_screen *scr = ri->ri_hw;
   1389  1.67.6.2       tls 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1390  1.67.6.2       tls 	void *data;
   1391  1.67.6.2       tls 	uint32_t fg, bg;
   1392  1.67.6.2       tls 	int i, x, y, wi, he;
   1393  1.67.6.2       tls 	uint32_t addr;
   1394  1.67.6.2       tls 	int stride = sc->sc_fb.fb_type.fb_width;
   1395  1.67.6.2       tls 
   1396  1.67.6.2       tls 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1397  1.67.6.2       tls 		return;
   1398  1.67.6.2       tls 
   1399  1.67.6.2       tls 	if (!CHAR_IN_FONT(c, font))
   1400  1.67.6.2       tls 		return;
   1401  1.67.6.2       tls 
   1402  1.67.6.2       tls 	wi = font->fontwidth;
   1403  1.67.6.2       tls 	he = font->fontheight;
   1404  1.67.6.2       tls 
   1405  1.67.6.2       tls 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1406  1.67.6.2       tls 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
   1407  1.67.6.2       tls 	sx_write(sc->sc_sx, SX_QUEUED(8), bg);
   1408  1.67.6.2       tls 	sx_write(sc->sc_sx, SX_QUEUED(9), fg);
   1409  1.67.6.2       tls 
   1410  1.67.6.2       tls 	x = ri->ri_xorigin + col * wi;
   1411  1.67.6.2       tls 	y = ri->ri_yorigin + row * he;
   1412  1.67.6.2       tls 
   1413  1.67.6.2       tls 	if (c == 0x20) {
   1414  1.67.6.2       tls 		cg14_rectfill(sc, x, y, wi, he, bg);
   1415  1.67.6.2       tls 		return;
   1416  1.67.6.2       tls 	}
   1417  1.67.6.2       tls 
   1418  1.67.6.2       tls 	data = WSFONT_GLYPH(c, font);
   1419  1.67.6.2       tls 	addr = sc->sc_fb_paddr + x + stride * y;
   1420  1.67.6.2       tls 
   1421  1.67.6.2       tls 	switch (font->stride) {
   1422  1.67.6.2       tls 		case 1: {
   1423  1.67.6.2       tls 			uint8_t *data8 = data;
   1424  1.67.6.2       tls 			uint32_t reg;
   1425  1.67.6.2       tls 			for (i = 0; i < he; i++) {
   1426  1.67.6.2       tls 				reg = *data8;
   1427  1.67.6.2       tls 				sx_write(sc->sc_sx, SX_QUEUED(R_MASK),
   1428  1.67.6.2       tls 				    reg << 24);
   1429  1.67.6.2       tls 				sta(addr, ASI_SX, SX_STBS(8, wi - 1, addr & 7));
   1430  1.67.6.2       tls 				data8++;
   1431  1.67.6.2       tls 				addr += stride;
   1432  1.67.6.2       tls 			}
   1433  1.67.6.2       tls 			break;
   1434  1.67.6.2       tls 		}
   1435  1.67.6.2       tls 		case 2: {
   1436  1.67.6.2       tls 			uint16_t *data16 = data;
   1437  1.67.6.2       tls 			uint32_t reg;
   1438  1.67.6.2       tls 			for (i = 0; i < he; i++) {
   1439  1.67.6.2       tls 				reg = *data16;
   1440  1.67.6.2       tls 				sx_write(sc->sc_sx, SX_QUEUED(R_MASK),
   1441  1.67.6.2       tls 				    reg << 16);
   1442  1.67.6.2       tls 				sta(addr, ASI_SX, SX_STBS(8, wi - 1, addr & 7));
   1443  1.67.6.2       tls 				data16++;
   1444  1.67.6.2       tls 				addr += stride;
   1445  1.67.6.2       tls 			}
   1446  1.67.6.2       tls 			break;
   1447  1.67.6.2       tls 		}
   1448  1.67.6.2       tls 	}
   1449  1.67.6.2       tls }
   1450  1.67.6.2       tls 
   1451  1.67.6.2       tls static void
   1452  1.67.6.2       tls cg14_cursor(void *cookie, int on, int row, int col)
   1453  1.67.6.2       tls {
   1454  1.67.6.2       tls 	struct rasops_info *ri = cookie;
   1455  1.67.6.2       tls 	struct vcons_screen *scr = ri->ri_hw;
   1456  1.67.6.2       tls 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1457  1.67.6.2       tls 	int x, y, wi, he;
   1458  1.67.6.2       tls 
   1459  1.67.6.2       tls 	wi = ri->ri_font->fontwidth;
   1460  1.67.6.2       tls 	he = ri->ri_font->fontheight;
   1461  1.67.6.2       tls 
   1462  1.67.6.2       tls 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1463  1.67.6.2       tls 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1464  1.67.6.2       tls 		y = ri->ri_crow * he + ri->ri_yorigin;
   1465  1.67.6.2       tls 		if (ri->ri_flg & RI_CURSOR) {
   1466  1.67.6.2       tls 			cg14_invert(sc, x, y, wi, he);
   1467  1.67.6.2       tls 			ri->ri_flg &= ~RI_CURSOR;
   1468  1.67.6.2       tls 		}
   1469  1.67.6.2       tls 		ri->ri_crow = row;
   1470  1.67.6.2       tls 		ri->ri_ccol = col;
   1471  1.67.6.2       tls 		if (on) {
   1472  1.67.6.2       tls 			x = ri->ri_ccol * wi + ri->ri_xorigin;
   1473  1.67.6.2       tls 			y = ri->ri_crow * he + ri->ri_yorigin;
   1474  1.67.6.2       tls 			cg14_invert(sc, x, y, wi, he);
   1475  1.67.6.2       tls 			ri->ri_flg |= RI_CURSOR;
   1476  1.67.6.2       tls 		}
   1477  1.67.6.2       tls 	} else {
   1478  1.67.6.2       tls 		scr->scr_ri.ri_crow = row;
   1479  1.67.6.2       tls 		scr->scr_ri.ri_ccol = col;
   1480  1.67.6.2       tls 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
   1481  1.67.6.2       tls 	}
   1482  1.67.6.2       tls 
   1483  1.67.6.2       tls }
   1484  1.67.6.2       tls 
   1485  1.67.6.2       tls #if 0
   1486  1.67.6.2       tls static void
   1487  1.67.6.2       tls r128fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
   1488  1.67.6.2       tls {
   1489  1.67.6.2       tls 	struct rasops_info *ri = cookie;
   1490  1.67.6.2       tls 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1491  1.67.6.2       tls 	struct vcons_screen *scr = ri->ri_hw;
   1492  1.67.6.2       tls 	struct r128fb_softc *sc = scr->scr_cookie;
   1493  1.67.6.2       tls 	uint32_t bg, latch = 0, bg8, fg8, pixel;
   1494  1.67.6.2       tls 	int i, x, y, wi, he, r, g, b, aval;
   1495  1.67.6.2       tls 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
   1496  1.67.6.2       tls 	uint8_t *data8;
   1497  1.67.6.2       tls 	int rv, cnt = 0;
   1498  1.67.6.2       tls 
   1499  1.67.6.2       tls 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1500  1.67.6.2       tls 		return;
   1501  1.67.6.2       tls 
   1502  1.67.6.2       tls 	if (!CHAR_IN_FONT(c, font))
   1503  1.67.6.2       tls 		return;
   1504  1.67.6.2       tls 
   1505  1.67.6.2       tls 	wi = font->fontwidth;
   1506  1.67.6.2       tls 	he = font->fontheight;
   1507  1.67.6.2       tls 
   1508  1.67.6.2       tls 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1509  1.67.6.2       tls 	x = ri->ri_xorigin + col * wi;
   1510  1.67.6.2       tls 	y = ri->ri_yorigin + row * he;
   1511  1.67.6.2       tls 	if (c == 0x20) {
   1512  1.67.6.2       tls 		r128fb_rectfill(sc, x, y, wi, he, bg);
   1513  1.67.6.2       tls 		return;
   1514  1.67.6.2       tls 	}
   1515  1.67.6.2       tls 
   1516  1.67.6.2       tls 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1517  1.67.6.2       tls 	if (rv == GC_OK)
   1518  1.67.6.2       tls 		return;
   1519  1.67.6.2       tls 
   1520  1.67.6.2       tls 	data8 = WSFONT_GLYPH(c, font);
   1521  1.67.6.2       tls 
   1522  1.67.6.2       tls 	r128fb_wait(sc, 5);
   1523  1.67.6.2       tls 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1524  1.67.6.2       tls 	    R128_DP_GUI_MASTER_CNTL,
   1525  1.67.6.2       tls 	    R128_GMC_BRUSH_SOLID_COLOR |
   1526  1.67.6.2       tls 	    R128_GMC_SRC_DATATYPE_COLOR |
   1527  1.67.6.2       tls 	    R128_ROP3_S |
   1528  1.67.6.2       tls 	    R128_DP_SRC_SOURCE_HOST_DATA |
   1529  1.67.6.2       tls 	    sc->sc_master_cntl);
   1530  1.67.6.2       tls 
   1531  1.67.6.2       tls 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1532  1.67.6.2       tls 	    R128_DP_CNTL,
   1533  1.67.6.2       tls 	    R128_DST_Y_TOP_TO_BOTTOM |
   1534  1.67.6.2       tls 	    R128_DST_X_LEFT_TO_RIGHT);
   1535  1.67.6.2       tls 
   1536  1.67.6.2       tls 	/* needed? */
   1537  1.67.6.2       tls 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
   1538  1.67.6.2       tls 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
   1539  1.67.6.2       tls 	    (x << 16) | y);
   1540  1.67.6.2       tls 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
   1541  1.67.6.2       tls 	    (wi << 16) | he);
   1542  1.67.6.2       tls 
   1543  1.67.6.2       tls 	/*
   1544  1.67.6.2       tls 	 * we need the RGB colours here, so get offsets into rasops_cmap
   1545  1.67.6.2       tls 	 */
   1546  1.67.6.2       tls 	fgo = ((attr >> 24) & 0xf) * 3;
   1547  1.67.6.2       tls 	bgo = ((attr >> 16) & 0xf) * 3;
   1548  1.67.6.2       tls 
   1549  1.67.6.2       tls 	r0 = rasops_cmap[bgo];
   1550  1.67.6.2       tls 	r1 = rasops_cmap[fgo];
   1551  1.67.6.2       tls 	g0 = rasops_cmap[bgo + 1];
   1552  1.67.6.2       tls 	g1 = rasops_cmap[fgo + 1];
   1553  1.67.6.2       tls 	b0 = rasops_cmap[bgo + 2];
   1554  1.67.6.2       tls 	b1 = rasops_cmap[fgo + 2];
   1555  1.67.6.2       tls #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1556  1.67.6.2       tls 	bg8 = R3G3B2(r0, g0, b0);
   1557  1.67.6.2       tls 	fg8 = R3G3B2(r1, g1, b1);
   1558  1.67.6.2       tls 
   1559  1.67.6.2       tls 	r128fb_wait(sc, 16);
   1560  1.67.6.2       tls 
   1561  1.67.6.2       tls 	for (i = 0; i < ri->ri_fontscale; i++) {
   1562  1.67.6.2       tls 		aval = *data8;
   1563  1.67.6.2       tls 		if (aval == 0) {
   1564  1.67.6.2       tls 			pixel = bg8;
   1565  1.67.6.2       tls 		} else if (aval == 255) {
   1566  1.67.6.2       tls 			pixel = fg8;
   1567  1.67.6.2       tls 		} else {
   1568  1.67.6.2       tls 			r = aval * r1 + (255 - aval) * r0;
   1569  1.67.6.2       tls 			g = aval * g1 + (255 - aval) * g0;
   1570  1.67.6.2       tls 			b = aval * b1 + (255 - aval) * b0;
   1571  1.67.6.2       tls 			pixel = ((r & 0xe000) >> 8) |
   1572  1.67.6.2       tls 				((g & 0xe000) >> 11) |
   1573  1.67.6.2       tls 				((b & 0xc000) >> 14);
   1574  1.67.6.2       tls 		}
   1575  1.67.6.2       tls 		latch = (latch << 8) | pixel;
   1576  1.67.6.2       tls 		/* write in 32bit chunks */
   1577  1.67.6.2       tls 		if ((i & 3) == 3) {
   1578  1.67.6.2       tls 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1579  1.67.6.2       tls 			    R128_HOST_DATA0, latch);
   1580  1.67.6.2       tls 			/*
   1581  1.67.6.2       tls 			 * not strictly necessary, old data should be shifted
   1582  1.67.6.2       tls 			 * out
   1583  1.67.6.2       tls 			 */
   1584  1.67.6.2       tls 			latch = 0;
   1585  1.67.6.2       tls 			cnt++;
   1586  1.67.6.2       tls 			if (cnt > 15) {
   1587  1.67.6.2       tls 				r128fb_wait(sc, 16);
   1588  1.67.6.2       tls 				cnt = 0;
   1589  1.67.6.2       tls 			}
   1590  1.67.6.2       tls 		}
   1591  1.67.6.2       tls 		data8++;
   1592  1.67.6.2       tls 	}
   1593  1.67.6.2       tls 	/* if we have pixels left in latch write them out */
   1594  1.67.6.2       tls 	if ((i & 3) != 0) {
   1595  1.67.6.2       tls 		latch = latch << ((4 - (i & 3)) << 3);
   1596  1.67.6.2       tls 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1597  1.67.6.2       tls 				    R128_HOST_DATA0, latch);
   1598  1.67.6.2       tls 	}
   1599  1.67.6.2       tls 	if (rv == GC_ADD) {
   1600  1.67.6.2       tls 		glyphcache_add(&sc->sc_gc, c, x, y);
   1601  1.67.6.2       tls 	}
   1602  1.67.6.2       tls }
   1603  1.67.6.2       tls #endif
   1604  1.67.6.2       tls 
   1605  1.67.6.2       tls static void
   1606  1.67.6.2       tls cg14_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1607  1.67.6.2       tls {
   1608  1.67.6.2       tls 	struct rasops_info *ri = cookie;
   1609  1.67.6.2       tls 	struct vcons_screen *scr = ri->ri_hw;
   1610  1.67.6.2       tls 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1611  1.67.6.2       tls 	int32_t xs, xd, y, width, height;
   1612  1.67.6.2       tls 
   1613  1.67.6.2       tls 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1614  1.67.6.2       tls 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1615  1.67.6.2       tls 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1616  1.67.6.2       tls 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1617  1.67.6.2       tls 		width = ri->ri_font->fontwidth * ncols;
   1618  1.67.6.2       tls 		height = ri->ri_font->fontheight;
   1619  1.67.6.2       tls 		cg14_bitblt(sc, xs, y, xd, y, width, height, 0x0c);
   1620  1.67.6.2       tls 	}
   1621  1.67.6.2       tls }
   1622  1.67.6.2       tls 
   1623  1.67.6.2       tls static void
   1624  1.67.6.2       tls cg14_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1625  1.67.6.2       tls {
   1626  1.67.6.2       tls 	struct rasops_info *ri = cookie;
   1627  1.67.6.2       tls 	struct vcons_screen *scr = ri->ri_hw;
   1628  1.67.6.2       tls 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1629  1.67.6.2       tls 	int32_t x, y, width, height, fg, bg, ul;
   1630  1.67.6.2       tls 
   1631  1.67.6.2       tls 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1632  1.67.6.2       tls 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1633  1.67.6.2       tls 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1634  1.67.6.2       tls 		width = ri->ri_font->fontwidth * ncols;
   1635  1.67.6.2       tls 		height = ri->ri_font->fontheight;
   1636  1.67.6.2       tls 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1637  1.67.6.2       tls 
   1638  1.67.6.2       tls 		cg14_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1639  1.67.6.2       tls 	}
   1640  1.67.6.2       tls }
   1641  1.67.6.2       tls 
   1642  1.67.6.2       tls static void
   1643  1.67.6.2       tls cg14_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1644  1.67.6.2       tls {
   1645  1.67.6.2       tls 	struct rasops_info *ri = cookie;
   1646  1.67.6.2       tls 	struct vcons_screen *scr = ri->ri_hw;
   1647  1.67.6.2       tls 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1648  1.67.6.2       tls 	int32_t x, ys, yd, width, height;
   1649  1.67.6.2       tls 
   1650  1.67.6.2       tls 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1651  1.67.6.2       tls 		x = ri->ri_xorigin;
   1652  1.67.6.2       tls 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1653  1.67.6.2       tls 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1654  1.67.6.2       tls 		width = ri->ri_emuwidth;
   1655  1.67.6.2       tls 		height = ri->ri_font->fontheight * nrows;
   1656  1.67.6.2       tls 		cg14_bitblt(sc, x, ys, x, yd, width, height, 0x0c);
   1657  1.67.6.2       tls 	}
   1658  1.67.6.2       tls }
   1659  1.67.6.2       tls 
   1660  1.67.6.2       tls static void
   1661  1.67.6.2       tls cg14_eraserows(void *cookie, int row, int nrows, long fillattr)
   1662  1.67.6.2       tls {
   1663  1.67.6.2       tls 	struct rasops_info *ri = cookie;
   1664  1.67.6.2       tls 	struct vcons_screen *scr = ri->ri_hw;
   1665  1.67.6.2       tls 	struct cgfourteen_softc *sc = scr->scr_cookie;
   1666  1.67.6.2       tls 	int32_t x, y, width, height, fg, bg, ul;
   1667  1.67.6.2       tls 
   1668  1.67.6.2       tls 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1669  1.67.6.2       tls 		x = ri->ri_xorigin;
   1670  1.67.6.2       tls 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1671  1.67.6.2       tls 		width = ri->ri_emuwidth;
   1672  1.67.6.2       tls 		height = ri->ri_font->fontheight * nrows;
   1673  1.67.6.2       tls 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1674  1.67.6.2       tls 
   1675  1.67.6.2       tls 		cg14_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1676  1.67.6.2       tls 	}
   1677  1.67.6.2       tls }
   1678  1.67.6.2       tls 
   1679  1.67.6.2       tls #endif /* NSX > 0 */
   1680  1.67.6.2       tls 
   1681      1.49  macallan #endif
   1682