Home | History | Annotate | Line # | Download | only in dev
cgfourteen.c revision 1.65
      1  1.65  macallan /*	$NetBSD: cgfourteen.c,v 1.65 2010/08/31 21:14:57 macallan 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  * Provides minimum emulation of a Sun cgthree 8-bit framebuffer to
     57   1.1    abrown  * allow X to run.
     58   1.1    abrown  *
     59   1.1    abrown  * Does not handle interrupts, even though they can occur.
     60   1.1    abrown  *
     61   1.1    abrown  * XXX should defer colormap updates to vertical retrace interrupts
     62   1.1    abrown  */
     63  1.10       mrg 
     64  1.12        pk /*
     65   1.1    abrown  * The following is for debugging only; it opens up a security hole
     66  1.12        pk  * enabled by allowing any user to map the control registers for the
     67   1.1    abrown  * cg14 into their space.
     68   1.1    abrown  */
     69  1.47      elad #undef CG14_MAP_REGS
     70   1.1    abrown 
     71   1.1    abrown #include <sys/param.h>
     72   1.1    abrown #include <sys/systm.h>
     73   1.1    abrown #include <sys/buf.h>
     74   1.1    abrown #include <sys/device.h>
     75   1.1    abrown #include <sys/ioctl.h>
     76   1.1    abrown #include <sys/malloc.h>
     77  1.55  macallan #include <sys/kmem.h>
     78   1.1    abrown #include <sys/mman.h>
     79   1.1    abrown #include <sys/tty.h>
     80   1.1    abrown #include <sys/conf.h>
     81  1.55  macallan #include <dev/pci/pciio.h>
     82   1.1    abrown 
     83  1.18       mrg #include <uvm/uvm_extern.h>
     84   1.1    abrown 
     85  1.42  christos #include <dev/sun/fbio.h>
     86  1.19        pk #include <machine/autoconf.h>
     87  1.42  christos #include <machine/pmap.h>
     88  1.42  christos #include <dev/sun/fbvar.h>
     89  1.42  christos #include <machine/cpu.h>
     90  1.19        pk #include <dev/sbus/sbusvar.h>
     91  1.19        pk 
     92  1.49  macallan #include "wsdisplay.h"
     93  1.49  macallan #include <dev/wscons/wsconsio.h>
     94  1.49  macallan #include <dev/wsfont/wsfont.h>
     95  1.49  macallan #include <dev/rasops/rasops.h>
     96  1.49  macallan 
     97  1.49  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     98  1.49  macallan 
     99   1.1    abrown #include <sparc/dev/cgfourteenreg.h>
    100   1.1    abrown #include <sparc/dev/cgfourteenvar.h>
    101   1.1    abrown 
    102  1.49  macallan #include "opt_wsemul.h"
    103  1.49  macallan 
    104   1.1    abrown /* autoconfiguration driver */
    105  1.53  macallan static int	cgfourteenmatch(device_t, struct cfdata *, void *);
    106  1.53  macallan static void	cgfourteenattach(device_t, device_t, void *);
    107  1.53  macallan static void	cgfourteenunblank(device_t);
    108   1.1    abrown 
    109  1.53  macallan CFATTACH_DECL_NEW(cgfourteen, sizeof(struct cgfourteen_softc),
    110  1.30   thorpej     cgfourteenmatch, cgfourteenattach, NULL, NULL);
    111  1.42  christos 
    112   1.8   thorpej extern struct cfdriver cgfourteen_cd;
    113   1.1    abrown 
    114  1.26   gehenna dev_type_open(cgfourteenopen);
    115  1.26   gehenna dev_type_close(cgfourteenclose);
    116  1.26   gehenna dev_type_ioctl(cgfourteenioctl);
    117  1.26   gehenna dev_type_mmap(cgfourteenmmap);
    118  1.42  christos dev_type_poll(cgfourteenpoll);
    119  1.26   gehenna 
    120  1.26   gehenna const struct cdevsw cgfourteen_cdevsw = {
    121  1.42  christos         cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
    122  1.42  christos         nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
    123  1.26   gehenna };
    124  1.26   gehenna 
    125   1.1    abrown /* frame buffer generic driver */
    126   1.1    abrown static struct fbdriver cgfourteenfbdriver = {
    127  1.12        pk 	cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
    128  1.42  christos 	cgfourteenpoll, cgfourteenmmap, nokqfilter
    129   1.1    abrown };
    130   1.1    abrown 
    131  1.42  christos extern struct tty *fbconstty;
    132  1.42  christos 
    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.49  macallan #endif
    151  1.49  macallan 
    152  1.49  macallan #if defined(RASTERCONSOLE) && (NWSDISPLAY > 0)
    153  1.61       apb #error "You can't have it both ways - either RASTERCONSOLE or wsdisplay"
    154  1.49  macallan #endif
    155  1.49  macallan 
    156  1.23   darrenr /*
    157   1.1    abrown  * Match a cgfourteen.
    158   1.1    abrown  */
    159  1.42  christos int
    160  1.53  macallan cgfourteenmatch(device_t parent, struct cfdata *cf, void *aux)
    161   1.1    abrown {
    162  1.11        pk 	union obio_attach_args *uoba = aux;
    163  1.11        pk 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    164   1.1    abrown 
    165   1.1    abrown 	/*
    166   1.1    abrown 	 * The cgfourteen is a local-bus video adaptor, accessed directly
    167   1.1    abrown 	 * via the processor, and not through device space or an external
    168   1.1    abrown 	 * bus. Thus we look _only_ at the obio bus.
    169   1.1    abrown 	 * Additionally, these things exist only on the Sun4m.
    170   1.1    abrown 	 */
    171  1.11        pk 
    172  1.11        pk 	if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
    173  1.11        pk 		return (0);
    174  1.11        pk 
    175  1.11        pk 	/* Check driver name */
    176  1.27   thorpej 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    177   1.1    abrown }
    178   1.1    abrown 
    179   1.1    abrown /*
    180  1.42  christos  * Set COLOUR_OFFSET to the offset of the video RAM.  This is to provide
    181  1.42  christos  *  space for faked overlay junk for the cg8 emulation.
    182  1.42  christos  *
    183  1.42  christos  * As it happens, this value is correct for both cg3 and cg8 emulation!
    184  1.42  christos  */
    185  1.42  christos #define COLOUR_OFFSET (256*1024)
    186  1.42  christos 
    187  1.44        he #ifdef RASTERCONSOLE
    188  1.42  christos static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
    189  1.42  christos {
    190  1.42  christos 	int i;
    191  1.42  christos 
    192  1.42  christos 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
    193  1.42  christos 	for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
    194  1.42  christos 	sc->sc_clut2->clut_lut[0] = 0x00ffffff;
    195  1.42  christos 	sc->sc_clut2->clut_lut[255] = 0;
    196  1.42  christos }
    197  1.44        he #endif /* RASTERCONSOLE */
    198  1.42  christos 
    199  1.49  macallan #if NWSDISPLAY > 0
    200  1.49  macallan static int	cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    201  1.49  macallan static paddr_t	cg14_mmap(void *, void *, off_t, int);
    202  1.49  macallan static void	cg14_init_screen(void *, struct vcons_screen *, int, long *);
    203  1.49  macallan 
    204  1.49  macallan 
    205  1.49  macallan struct wsdisplay_accessops cg14_accessops = {
    206  1.49  macallan 	cg14_ioctl,
    207  1.49  macallan 	cg14_mmap,
    208  1.49  macallan 	NULL,	/* alloc_screen */
    209  1.49  macallan 	NULL,	/* free_screen */
    210  1.49  macallan 	NULL,	/* show_screen */
    211  1.49  macallan 	NULL, 	/* load_font */
    212  1.49  macallan 	NULL,	/* pollc */
    213  1.49  macallan 	NULL	/* scroll */
    214  1.49  macallan };
    215  1.49  macallan #endif
    216  1.49  macallan 
    217  1.42  christos /*
    218   1.1    abrown  * Attach a display.  We need to notice if it is the console, too.
    219   1.1    abrown  */
    220  1.42  christos void
    221  1.53  macallan cgfourteenattach(device_t parent, device_t self, void *aux)
    222   1.1    abrown {
    223  1.11        pk 	union obio_attach_args *uoba = aux;
    224  1.11        pk 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    225  1.53  macallan 	struct cgfourteen_softc *sc = device_private(self);
    226  1.11        pk 	struct fbdevice *fb = &sc->sc_fb;
    227  1.11        pk 	bus_space_handle_t bh;
    228  1.11        pk 	int node, ramsize;
    229  1.40       uwe 	volatile uint32_t *lut;
    230  1.56  macallan 	int i, isconsole, items;
    231  1.56  macallan 	uint32_t fbva[2] = {0, 0};
    232  1.56  macallan 	uint32_t *ptr = fbva;
    233  1.11        pk 
    234  1.53  macallan 	sc->sc_dev = self;
    235  1.55  macallan 	sc->sc_opens = 0;
    236  1.11        pk 	node = sa->sa_node;
    237  1.11        pk 
    238  1.11        pk 	/* Remember cookies for cgfourteenmmap() */
    239  1.11        pk 	sc->sc_bustag = sa->sa_bustag;
    240   1.1    abrown 
    241  1.11        pk 	fb->fb_driver = &cgfourteenfbdriver;
    242  1.53  macallan 	fb->fb_device = sc->sc_dev;
    243  1.11        pk 	/* Mask out invalid flags from the user. */
    244  1.53  macallan 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    245   1.1    abrown 
    246  1.63  macallan 	fb->fb_type.fb_type = FBTYPE_MDICOLOR;
    247  1.11        pk 	fb->fb_type.fb_depth = 32;
    248  1.63  macallan 
    249  1.11        pk 	fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
    250  1.42  christos 	ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
    251  1.53  macallan 
    252  1.11        pk 	fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
    253  1.42  christos 	fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
    254  1.11        pk 
    255  1.13        pk 	if (sa->sa_nreg < 2) {
    256  1.13        pk 		printf("%s: only %d register sets\n",
    257  1.13        pk 			self->dv_xname, sa->sa_nreg);
    258  1.11        pk 		return;
    259  1.11        pk 	}
    260  1.59    cegger 	memcpy(sc->sc_physadr, sa->sa_reg,
    261  1.42  christos 	      sa->sa_nreg * sizeof(struct sbus_reg));
    262   1.1    abrown 
    263  1.49  macallan 	sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
    264  1.49  macallan 
    265  1.49  macallan 	printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
    266   1.1    abrown 	/*
    267   1.1    abrown 	 * Now map in the 8 useful pages of registers
    268   1.1    abrown 	 */
    269  1.11        pk 	if (sa->sa_size < 0x10000) {
    270   1.1    abrown #ifdef DIAGNOSTIC
    271   1.5  christos 		printf("warning: can't find all cgfourteen registers...\n");
    272   1.1    abrown #endif
    273  1.11        pk 		sa->sa_size = 0x10000;
    274   1.1    abrown 	}
    275  1.42  christos 	if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    276  1.42  christos 			 sa->sa_offset,
    277  1.42  christos 			 sa->sa_size,
    278  1.55  macallan 			 0 /*BUS_SPACE_MAP_LINEAR*/,
    279  1.42  christos 			 &bh) != 0) {
    280  1.11        pk 		printf("%s: cannot map control registers\n", self->dv_xname);
    281  1.11        pk 		return;
    282  1.11        pk 	}
    283  1.49  macallan 	sc->sc_regh = bh;
    284  1.64  macallan 	sc->sc_regaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
    285  1.64  macallan 	sc->sc_fbaddr = BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    286  1.64  macallan 				sc->sc_physadr[CG14_PXL_IDX].sbr_offset);
    287  1.64  macallan 
    288  1.11        pk 	sc->sc_ctl   = (struct cg14ctl  *) (bh);
    289  1.11        pk 	sc->sc_hwc   = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
    290  1.11        pk 	sc->sc_dac   = (struct cg14dac  *) (bh + CG14_OFFSET_DAC);
    291  1.11        pk 	sc->sc_xlut  = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
    292  1.11        pk 	sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
    293  1.11        pk 	sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
    294  1.11        pk 	sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
    295  1.11        pk 	sc->sc_clutincr =        (u_int *) (bh + CG14_OFFSET_CLUTINCR);
    296   1.1    abrown 
    297   1.1    abrown 	/*
    298   1.1    abrown 	 * Let the user know that we're here
    299   1.1    abrown 	 */
    300  1.63  macallan 	printf(": %dx%d",
    301  1.11        pk 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    302  1.63  macallan 
    303   1.1    abrown 	/*
    304  1.42  christos 	 * Enable the video.
    305   1.1    abrown 	 */
    306   1.1    abrown 	cg14_set_video(sc, 1);
    307   1.1    abrown 
    308   1.1    abrown 	/*
    309   1.1    abrown 	 * Grab the initial colormap
    310   1.1    abrown 	 */
    311  1.39   tsutsui 	lut = sc->sc_clut1->clut_lut;
    312   1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++)
    313   1.1    abrown 		sc->sc_cmap.cm_chip[i] = lut[i];
    314   1.1    abrown 
    315   1.1    abrown 	/* See if we're the console */
    316  1.42  christos         isconsole = fb_is_console(node);
    317   1.1    abrown 
    318  1.49  macallan #if defined(RASTERCONSOLE)
    319   1.1    abrown 	if (isconsole) {
    320   1.5  christos 		printf(" (console)\n");
    321  1.42  christos 		/* *sbus*_bus_map?  but that's how we map the regs... */
    322  1.42  christos 		if (sbus_bus_map( sc->sc_bustag,
    323  1.42  christos 				  sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    324  1.49  macallan 				  sc->sc_physadr[CG14_PXL_IDX].sbr_offset +
    325  1.49  macallan 				    0x03800000,
    326  1.49  macallan 				  1152 * 900, BUS_SPACE_MAP_LINEAR,
    327  1.42  christos 				  &bh) != 0) {
    328  1.53  macallan 			printf("%s: cannot map pixels\n",
    329  1.53  macallan 			    device_xname(sc->sc_dev));
    330  1.49  macallan 			return;
    331  1.42  christos 		}
    332  1.49  macallan 		sc->sc_rcfb = sc->sc_fb;
    333  1.49  macallan 		sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
    334  1.49  macallan 		sc->sc_rcfb.fb_type.fb_depth = 8;
    335  1.49  macallan 		sc->sc_rcfb.fb_linebytes = 1152;
    336  1.49  macallan 		sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
    337  1.49  macallan 		sc->sc_rcfb.fb_pixels = (void *)bh;
    338  1.49  macallan 
    339  1.49  macallan 		printf("vram at %p\n",(void *)bh);
    340  1.49  macallan 		/* XXX should use actual screen size */
    341  1.49  macallan 
    342  1.55  macallan 		for (i = 0; i < ramsize; i++)
    343  1.49  macallan 		    ((unsigned char *)bh)[i] = 0;
    344  1.49  macallan 		fbrcons_init(&sc->sc_rcfb);
    345  1.49  macallan 		cg14_set_rcons_luts(sc);
    346  1.49  macallan 		sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID |
    347  1.49  macallan 		    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
    348   1.1    abrown 	} else
    349   1.5  christos 		printf("\n");
    350  1.49  macallan #endif
    351  1.49  macallan 
    352  1.49  macallan #if NWSDISPLAY > 0
    353  1.56  macallan 	prom_getprop(sa->sa_node, "address", 4, &items, &ptr);
    354  1.56  macallan 	if (fbva[1] == 0) {
    355  1.60  macallan 		if (sbus_bus_map( sc->sc_bustag,
    356  1.56  macallan 		    sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    357  1.56  macallan 		    sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
    358  1.60  macallan 		    ramsize, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    359  1.60  macallan 		    &bh) != 0) {
    360  1.58  macallan 			printf("%s: cannot map pixels\n",
    361  1.58  macallan 				device_xname(sc->sc_dev));
    362  1.56  macallan 			return;
    363  1.56  macallan 		}
    364  1.56  macallan 		sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
    365  1.56  macallan 	} else {
    366  1.56  macallan 		sc->sc_fb.fb_pixels = (void *)fbva[1];
    367  1.49  macallan 	}
    368  1.49  macallan 
    369  1.49  macallan 	if (isconsole)
    370  1.49  macallan 		printf(" (console)\n");
    371  1.51       jdc 	else
    372  1.51       jdc 		printf("\n");
    373  1.49  macallan 
    374  1.49  macallan 	sc->sc_depth = 8;
    375  1.49  macallan 	cg14_setup_wsdisplay(sc, isconsole);
    376  1.49  macallan #endif
    377   1.1    abrown 
    378   1.1    abrown 	/* Attach to /dev/fb */
    379  1.16        pk 	fb_attach(&sc->sc_fb, isconsole);
    380   1.1    abrown }
    381   1.1    abrown 
    382   1.1    abrown /*
    383  1.12        pk  * Keep track of the number of opens made. In the 24-bit driver, we need to
    384   1.1    abrown  * switch to 24-bit mode on the first open, and switch back to 8-bit on
    385   1.1    abrown  * the last close. This kind of nonsense is needed to give screenblank
    386   1.1    abrown  * a fighting chance of working.
    387   1.1    abrown  */
    388   1.1    abrown 
    389   1.1    abrown int
    390  1.41  christos cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
    391   1.1    abrown {
    392  1.42  christos 	struct cgfourteen_softc *sc;
    393  1.55  macallan 	int oldopens;
    394   1.1    abrown 
    395  1.54    cegger 	sc = device_lookup_private(&cgfourteen_cd, minor(dev));
    396  1.42  christos 	if (sc == NULL)
    397  1.42  christos 		return(ENXIO);
    398  1.55  macallan 	oldopens = sc->sc_opens++;
    399   1.1    abrown 
    400   1.1    abrown 	/* Setup the cg14 as we want it, and save the original PROM state */
    401   1.1    abrown 	if (oldopens == 0)	/* first open only, to make screenblank work */
    402   1.1    abrown 		cg14_init(sc);
    403   1.1    abrown 
    404   1.1    abrown 	return (0);
    405   1.1    abrown }
    406   1.1    abrown 
    407   1.1    abrown int
    408  1.41  christos cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
    409   1.1    abrown {
    410  1.53  macallan 	struct cgfourteen_softc *sc =
    411  1.54    cegger 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    412  1.55  macallan 	int opens;
    413   1.1    abrown 
    414  1.55  macallan 	opens = --sc->sc_opens;
    415  1.55  macallan 	if (sc->sc_opens < 0)
    416  1.55  macallan 		opens = sc->sc_opens = 0;
    417   1.1    abrown 
    418   1.1    abrown 	/*
    419   1.1    abrown 	 * Restore video state to make the PROM happy, on last close.
    420   1.1    abrown 	 */
    421   1.1    abrown 	if (opens == 0)
    422   1.1    abrown 		cg14_reset(sc);
    423   1.1    abrown 
    424   1.1    abrown 	return (0);
    425   1.1    abrown }
    426   1.1    abrown 
    427   1.1    abrown int
    428  1.48  christos cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    429   1.1    abrown {
    430  1.53  macallan 	struct cgfourteen_softc *sc =
    431  1.54    cegger 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    432  1.11        pk 	struct fbgattr *fba;
    433  1.42  christos 	int error;
    434   1.1    abrown 
    435   1.1    abrown 	switch (cmd) {
    436   1.1    abrown 
    437   1.1    abrown 	case FBIOGTYPE:
    438   1.1    abrown 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    439   1.1    abrown 		break;
    440   1.1    abrown 
    441   1.1    abrown 	case FBIOGATTR:
    442   1.1    abrown 		fba = (struct fbgattr *)data;
    443   1.1    abrown 		fba->real_type = FBTYPE_MDICOLOR;
    444   1.1    abrown 		fba->owner = 0;		/* XXX ??? */
    445   1.1    abrown 		fba->fbtype = sc->sc_fb.fb_type;
    446   1.1    abrown 		fba->sattr.flags = 0;
    447   1.1    abrown 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    448   1.1    abrown 		fba->sattr.dev_specific[0] = -1;
    449   1.1    abrown 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    450   1.1    abrown 		fba->emu_types[1] = -1;
    451   1.1    abrown 		break;
    452   1.1    abrown 
    453   1.1    abrown 	case FBIOGETCMAP:
    454  1.42  christos 		return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
    455   1.1    abrown 				     CG14_CLUT_SIZE));
    456   1.1    abrown 
    457   1.1    abrown 	case FBIOPUTCMAP:
    458   1.1    abrown 		/* copy to software map */
    459   1.1    abrown #define p ((struct fbcmap *)data)
    460   1.1    abrown 		error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
    461   1.1    abrown 		if (error)
    462   1.1    abrown 			return (error);
    463   1.1    abrown 		/* now blast them into the chip */
    464   1.1    abrown 		/* XXX should use retrace interrupt */
    465   1.1    abrown 		cg14_load_hwcmap(sc, p->index, p->count);
    466   1.1    abrown #undef p
    467   1.1    abrown 		break;
    468   1.1    abrown 
    469   1.1    abrown 	case FBIOGVIDEO:
    470   1.1    abrown 		*(int *)data = cg14_get_video(sc);
    471   1.1    abrown 		break;
    472   1.1    abrown 
    473   1.1    abrown 	case FBIOSVIDEO:
    474   1.1    abrown 		cg14_set_video(sc, *(int *)data);
    475   1.1    abrown 		break;
    476   1.1    abrown 
    477  1.64  macallan 	case CG14_SET_PIXELMODE: {
    478  1.64  macallan 		int depth = *(int *)data;
    479  1.64  macallan 
    480  1.64  macallan 		switch (depth) {
    481  1.64  macallan 		case 8:
    482  1.64  macallan 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
    483  1.64  macallan 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
    484  1.64  macallan 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
    485  1.64  macallan 			break;
    486  1.64  macallan 		case 32:
    487  1.64  macallan 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
    488  1.64  macallan 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
    489  1.64  macallan 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
    490  1.64  macallan 			break;
    491  1.64  macallan 		default:
    492  1.64  macallan 			return EINVAL;
    493  1.64  macallan 		}
    494  1.64  macallan 		}
    495  1.64  macallan 		break;
    496   1.1    abrown 	default:
    497   1.1    abrown 		return (ENOTTY);
    498   1.1    abrown 	}
    499   1.1    abrown 	return (0);
    500   1.1    abrown }
    501   1.1    abrown 
    502   1.1    abrown /*
    503   1.1    abrown  * Undo the effect of an FBIOSVIDEO that turns the video off.
    504   1.1    abrown  */
    505   1.1    abrown static void
    506  1.53  macallan cgfourteenunblank(device_t dev)
    507   1.1    abrown {
    508  1.53  macallan 	struct cgfourteen_softc *sc = device_private(dev);
    509   1.1    abrown 
    510  1.50  macallan 	cg14_set_video(sc, 1);
    511  1.50  macallan #if NWSDISPLAY > 0
    512  1.50  macallan 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
    513  1.50  macallan 		cg14_set_depth(sc, 8);
    514  1.50  macallan 		cg14_init_cmap(sc);
    515  1.50  macallan 		vcons_redraw_screen(sc->sc_vd.active);
    516  1.50  macallan 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    517  1.50  macallan 	}
    518  1.50  macallan #endif
    519   1.1    abrown }
    520   1.1    abrown 
    521   1.1    abrown /*
    522   1.1    abrown  * Return the address that would map the given device at the given
    523   1.1    abrown  * offset, allowing for the given protection, or return -1 for error.
    524  1.64  macallan   */
    525  1.17    simonb paddr_t
    526  1.40       uwe cgfourteenmmap(dev_t dev, off_t off, int prot)
    527   1.1    abrown {
    528  1.53  macallan 	struct cgfourteen_softc *sc =
    529  1.54    cegger 	    device_lookup_private(&cgfourteen_cd, minor(dev));
    530  1.64  macallan 	off_t offset = -1;
    531  1.12        pk 
    532   1.1    abrown 	if (off & PGOFSET)
    533   1.1    abrown 		panic("cgfourteenmmap");
    534  1.14       mrg 
    535  1.14       mrg 	if (off < 0)
    536  1.14       mrg 		return (-1);
    537   1.1    abrown 
    538  1.64  macallan 	if (off >= 0 && off < 0x10000) {
    539  1.64  macallan 		offset = sc->sc_regaddr;
    540  1.64  macallan 	} else if (off >= CG14_CURSOR_VOFF &&
    541  1.64  macallan 		   off < (CG14_CURSOR_VOFF + 0x1000)) {
    542  1.64  macallan 		offset = sc->sc_regaddr + CG14_OFFSET_CURS;
    543  1.64  macallan 		off -= CG14_CURSOR_VOFF;
    544  1.64  macallan 	} else if (off >= CG14_DIRECT_VOFF &&
    545  1.64  macallan 		   off < (CG14_DIRECT_VOFF + sc->sc_vramsize)) {
    546  1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_VRAM;
    547  1.64  macallan 		off -= CG14_DIRECT_VOFF;
    548  1.64  macallan 	} else if (off >= CG14_BGR_VOFF &&
    549  1.64  macallan 		   off < (CG14_BGR_VOFF + sc->sc_vramsize)) {
    550  1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_CBGR;
    551  1.64  macallan 		off -= CG14_BGR_VOFF;
    552  1.64  macallan 	} else if (off >= CG14_X32_VOFF &&
    553  1.64  macallan 		   off < (CG14_X32_VOFF + (sc->sc_vramsize >> 2))) {
    554  1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PX32;
    555  1.64  macallan 		off -= CG14_X32_VOFF;
    556  1.64  macallan 	} else if (off >= CG14_B32_VOFF &&
    557  1.64  macallan 		   off < (CG14_B32_VOFF + (sc->sc_vramsize >> 2))) {
    558  1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PB32;
    559  1.64  macallan 		off -= CG14_B32_VOFF;
    560  1.64  macallan 	} else if (off >= CG14_G32_VOFF &&
    561  1.64  macallan 		   off < (CG14_G32_VOFF + (sc->sc_vramsize >> 2))) {
    562  1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PG32;
    563  1.64  macallan 		off -= CG14_G32_VOFF;
    564  1.64  macallan 	} else if (off >= CG14_R32_VOFF &&
    565  1.64  macallan 		   off < CG14_R32_VOFF + (sc->sc_vramsize >> 2)) {
    566  1.64  macallan 		offset = sc->sc_fbaddr + CG14_FB_PR32;
    567  1.64  macallan 		off -= CG14_R32_VOFF;
    568  1.64  macallan 	} else
    569  1.64  macallan 		return -1;
    570  1.64  macallan 	return (bus_space_mmap(sc->sc_bustag, offset, off, prot,
    571  1.64  macallan 		    BUS_SPACE_MAP_LINEAR));
    572  1.12        pk }
    573   1.1    abrown 
    574  1.42  christos int
    575  1.42  christos cgfourteenpoll(dev_t dev, int events, struct lwp *l)
    576  1.42  christos {
    577  1.42  christos 
    578  1.42  christos 	return (seltrue(dev, events, l));
    579  1.42  christos }
    580  1.42  christos 
    581   1.1    abrown /*
    582  1.12        pk  * Miscellaneous helper functions
    583   1.1    abrown  */
    584   1.1    abrown 
    585   1.1    abrown /* Initialize the framebuffer, storing away useful state for later reset */
    586  1.12        pk static void
    587  1.40       uwe cg14_init(struct cgfourteen_softc *sc)
    588   1.1    abrown {
    589  1.45       uwe 	volatile uint32_t *clut;
    590  1.45       uwe 	volatile uint8_t  *xlut;
    591  1.11        pk 	int i;
    592   1.1    abrown 
    593   1.1    abrown 	/*
    594   1.1    abrown 	 * We stash away the following to restore on close:
    595   1.1    abrown 	 *
    596   1.1    abrown 	 * 	color look-up table 1 	(sc->sc_saveclut)
    597   1.1    abrown 	 *	x look-up table		(sc->sc_savexlut)
    598   1.1    abrown 	 *	control register	(sc->sc_savectl)
    599   1.1    abrown 	 *	cursor control register (sc->sc_savehwc)
    600   1.1    abrown 	 */
    601   1.1    abrown 	sc->sc_savectl = sc->sc_ctl->ctl_mctl;
    602   1.1    abrown 	sc->sc_savehwc = sc->sc_hwc->curs_ctl;
    603   1.1    abrown 
    604  1.45       uwe 	clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
    605  1.45       uwe 	xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
    606   1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    607   1.1    abrown 		sc->sc_saveclut.cm_chip[i] = clut[i];
    608   1.1    abrown 		sc->sc_savexlut[i] = xlut[i];
    609   1.1    abrown 	}
    610   1.1    abrown 
    611   1.1    abrown 	/*
    612   1.1    abrown 	 * Enable the video and put it in 8 bit mode
    613   1.1    abrown 	 */
    614   1.1    abrown 	sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
    615   1.1    abrown 		CG14_MCTL_POWERCTL;
    616   1.1    abrown }
    617   1.1    abrown 
    618  1.42  christos static void
    619  1.40       uwe /* Restore the state saved on cg14_init */
    620  1.40       uwe cg14_reset(struct cgfourteen_softc *sc)
    621   1.1    abrown {
    622  1.40       uwe 	volatile uint32_t *clut;
    623  1.40       uwe 	volatile uint8_t  *xlut;
    624  1.11        pk 	int i;
    625   1.1    abrown 
    626   1.1    abrown 	/*
    627   1.1    abrown 	 * We restore the following, saved in cg14_init:
    628   1.1    abrown 	 *
    629   1.1    abrown 	 * 	color look-up table 1 	(sc->sc_saveclut)
    630   1.1    abrown 	 *	x look-up table		(sc->sc_savexlut)
    631   1.1    abrown 	 *	control register	(sc->sc_savectl)
    632   1.1    abrown 	 *	cursor control register (sc->sc_savehwc)
    633   1.1    abrown 	 *
    634   1.1    abrown 	 * Note that we don't touch the video enable bits in the
    635   1.1    abrown 	 * control register; otherwise, screenblank wouldn't work.
    636   1.1    abrown 	 */
    637   1.1    abrown 	sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
    638   1.1    abrown 							CG14_MCTL_POWERCTL)) |
    639   1.1    abrown 				(sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
    640   1.1    abrown 						    CG14_MCTL_POWERCTL));
    641   1.1    abrown 	sc->sc_hwc->curs_ctl = sc->sc_savehwc;
    642   1.1    abrown 
    643  1.39   tsutsui 	clut = sc->sc_clut1->clut_lut;
    644  1.39   tsutsui 	xlut = sc->sc_xlut->xlut_lut;
    645   1.1    abrown 	for (i = 0; i < CG14_CLUT_SIZE; i++) {
    646   1.1    abrown 		clut[i] = sc->sc_saveclut.cm_chip[i];
    647   1.1    abrown 		xlut[i] = sc->sc_savexlut[i];
    648   1.1    abrown 	}
    649   1.1    abrown }
    650   1.1    abrown 
    651   1.1    abrown /* Enable/disable video display; power down monitor if DPMS-capable */
    652   1.1    abrown static void
    653  1.40       uwe cg14_set_video(struct cgfourteen_softc *sc, int enable)
    654   1.1    abrown {
    655  1.12        pk 	/*
    656   1.1    abrown 	 * We can only use DPMS to power down the display if the chip revision
    657   1.1    abrown 	 * is greater than 0.
    658   1.1    abrown 	 */
    659   1.1    abrown 	if (enable) {
    660   1.1    abrown 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    661   1.1    abrown 			sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
    662   1.1    abrown 						 CG14_MCTL_POWERCTL);
    663   1.1    abrown 		else
    664   1.1    abrown 			sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
    665   1.1    abrown 	} else {
    666   1.1    abrown 		if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
    667   1.1    abrown 			sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
    668   1.1    abrown 						  CG14_MCTL_POWERCTL);
    669   1.1    abrown 		else
    670   1.1    abrown 			sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
    671   1.1    abrown 	}
    672   1.1    abrown }
    673   1.1    abrown 
    674   1.1    abrown /* Get status of video display */
    675   1.1    abrown static int
    676  1.40       uwe cg14_get_video(struct cgfourteen_softc *sc)
    677   1.1    abrown {
    678   1.1    abrown 	return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
    679   1.1    abrown }
    680   1.1    abrown 
    681   1.1    abrown /* Read the software shadow colormap */
    682  1.12        pk static int
    683  1.40       uwe cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    684   1.1    abrown {
    685  1.42  christos         u_int i, start, count;
    686  1.42  christos         u_char *cp;
    687  1.42  christos         int error;
    688  1.42  christos 
    689  1.42  christos         start = p->index;
    690  1.42  christos         count = p->count;
    691  1.42  christos         if (start >= cmsize || count > cmsize - start)
    692  1.42  christos                 return (EINVAL);
    693  1.42  christos 
    694  1.42  christos         for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
    695  1.42  christos                 error = copyout(&cp[3], &p->red[i], 1);
    696  1.42  christos                 if (error)
    697  1.42  christos                         return error;
    698  1.42  christos                 error = copyout(&cp[2], &p->green[i], 1);
    699  1.42  christos                 if (error)
    700  1.42  christos                         return error;
    701  1.42  christos                 error = copyout(&cp[1], &p->blue[i], 1);
    702  1.42  christos                 if (error)
    703  1.42  christos                         return error;
    704  1.42  christos         }
    705  1.42  christos         return (0);
    706   1.1    abrown }
    707   1.1    abrown 
    708   1.1    abrown /* Write the software shadow colormap */
    709   1.1    abrown static int
    710  1.40       uwe cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
    711  1.24    itojun {
    712  1.42  christos         u_int i, start, count;
    713  1.42  christos         u_char *cp;
    714  1.42  christos         u_char cmap[256][4];
    715  1.42  christos         int error;
    716  1.42  christos 
    717  1.42  christos         start = p->index;
    718  1.42  christos         count = p->count;
    719  1.42  christos         if (start >= cmsize || count > cmsize - start)
    720  1.42  christos                 return (EINVAL);
    721  1.42  christos 
    722  1.42  christos         memcpy(&cmap, &cm->cm_map, sizeof cmap);
    723  1.42  christos         for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
    724  1.42  christos                 error = copyin(&p->red[i], &cp[3], 1);
    725  1.42  christos                 if (error)
    726  1.42  christos                         return error;
    727  1.42  christos                 error = copyin(&p->green[i], &cp[2], 1);
    728  1.42  christos                 if (error)
    729  1.42  christos                         return error;
    730  1.42  christos                 error = copyin(&p->blue[i], &cp[1], 1);
    731  1.42  christos                 if (error)
    732  1.42  christos                         return error;
    733  1.42  christos                 cp[0] = 0;      /* no alpha channel */
    734  1.42  christos         }
    735  1.42  christos         memcpy(&cm->cm_map, &cmap, sizeof cmap);
    736  1.42  christos         return (0);
    737   1.1    abrown }
    738   1.1    abrown 
    739   1.1    abrown static void
    740  1.40       uwe cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
    741   1.1    abrown {
    742   1.1    abrown 	/* XXX switch to auto-increment, and on retrace intr */
    743  1.40       uwe 
    744   1.1    abrown 	/* Setup pointers to source and dest */
    745  1.40       uwe 	uint32_t *colp = &sc->sc_cmap.cm_chip[start];
    746  1.40       uwe 	volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
    747   1.1    abrown 
    748   1.1    abrown 	/* Copy by words */
    749   1.1    abrown 	while (--ncolors >= 0)
    750   1.1    abrown 		*lutp++ = *colp++;
    751   1.1    abrown }
    752  1.49  macallan 
    753  1.49  macallan #if NWSDISPLAY > 0
    754  1.49  macallan static void
    755  1.49  macallan cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
    756  1.49  macallan {
    757  1.49  macallan 	struct wsemuldisplaydev_attach_args aa;
    758  1.49  macallan 	struct rasops_info *ri;
    759  1.49  macallan 	long defattr;
    760  1.49  macallan 
    761  1.49  macallan  	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    762  1.49  macallan 		"default",
    763  1.49  macallan 		0, 0,
    764  1.49  macallan 		NULL,
    765  1.49  macallan 		8, 16,
    766  1.49  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    767  1.49  macallan 		NULL
    768  1.49  macallan 	};
    769  1.49  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    770  1.49  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    771  1.49  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    772  1.49  macallan 	vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
    773  1.49  macallan 	    &cg14_accessops);
    774  1.49  macallan 	sc->sc_vd.init_screen = cg14_init_screen;
    775  1.49  macallan 
    776  1.49  macallan 	ri = &sc->sc_console_screen.scr_ri;
    777  1.49  macallan 
    778  1.49  macallan 	if (is_cons) {
    779  1.49  macallan 		vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
    780  1.49  macallan 		    &defattr);
    781  1.55  macallan 
    782  1.55  macallan 		/* clear the screen with the default background colour */
    783  1.55  macallan 		memset(sc->sc_fb.fb_pixels,
    784  1.55  macallan 		       (defattr >> 16) & 0xff,
    785  1.55  macallan 		       ri->ri_stride * ri->ri_height);
    786  1.49  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    787  1.49  macallan 
    788  1.49  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    789  1.49  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    790  1.49  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    791  1.49  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    792  1.49  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    793  1.49  macallan 		    defattr);
    794  1.62  macallan 		vcons_replay_msgbuf(&sc->sc_console_screen);
    795  1.49  macallan 	} else {
    796  1.49  macallan 		/*
    797  1.49  macallan 		 * since we're not the console we can postpone the rest
    798  1.49  macallan 		 * until someone actually allocates a screen for us
    799  1.49  macallan 		 */
    800  1.49  macallan 	}
    801  1.49  macallan 
    802  1.49  macallan 	cg14_init_cmap(sc);
    803  1.49  macallan 
    804  1.49  macallan 	aa.console = is_cons;
    805  1.49  macallan 	aa.scrdata = &sc->sc_screenlist;
    806  1.49  macallan 	aa.accessops = &cg14_accessops;
    807  1.49  macallan 	aa.accesscookie = &sc->sc_vd;
    808  1.49  macallan 
    809  1.53  macallan 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    810  1.49  macallan }
    811  1.49  macallan 
    812  1.49  macallan static void
    813  1.49  macallan cg14_init_cmap(struct cgfourteen_softc *sc)
    814  1.49  macallan {
    815  1.49  macallan 	int i, j = 0;
    816  1.49  macallan 
    817  1.49  macallan 	for (i = 0; i < 256; i++) {
    818  1.49  macallan 
    819  1.49  macallan 		sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
    820  1.49  macallan 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
    821  1.49  macallan 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
    822  1.49  macallan 		j += 3;
    823  1.49  macallan 	}
    824  1.49  macallan 	cg14_load_hwcmap(sc, 0, 256);
    825  1.49  macallan }
    826  1.49  macallan 
    827  1.49  macallan static int
    828  1.49  macallan cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    829  1.49  macallan {
    830  1.49  macallan 	u_int index = cm->index;
    831  1.49  macallan 	u_int count = cm->count;
    832  1.49  macallan 	int i, error;
    833  1.49  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    834  1.49  macallan 
    835  1.49  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    836  1.49  macallan 	    (cm->index + cm->count) > 256)
    837  1.49  macallan 		return EINVAL;
    838  1.49  macallan 	error = copyin(cm->red, &rbuf[index], count);
    839  1.49  macallan 	if (error)
    840  1.49  macallan 		return error;
    841  1.49  macallan 	error = copyin(cm->green, &gbuf[index], count);
    842  1.49  macallan 	if (error)
    843  1.49  macallan 		return error;
    844  1.49  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    845  1.49  macallan 	if (error)
    846  1.49  macallan 		return error;
    847  1.49  macallan 
    848  1.49  macallan 	for (i = 0; i < count; i++) {
    849  1.49  macallan 		sc->sc_cmap.cm_map[index][3] = rbuf[index];
    850  1.49  macallan 		sc->sc_cmap.cm_map[index][2] = gbuf[index];
    851  1.49  macallan 		sc->sc_cmap.cm_map[index][1] = bbuf[index];
    852  1.49  macallan 
    853  1.49  macallan 		index++;
    854  1.49  macallan 	}
    855  1.49  macallan 	cg14_load_hwcmap(sc, 0, 256);
    856  1.49  macallan 	return 0;
    857  1.49  macallan }
    858  1.49  macallan 
    859  1.49  macallan static int
    860  1.49  macallan cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
    861  1.49  macallan {
    862  1.49  macallan 	uint8_t rbuf[256], gbuf[256], bbuf[256];
    863  1.49  macallan 	u_int index = cm->index;
    864  1.49  macallan 	u_int count = cm->count;
    865  1.49  macallan 	int error, i;
    866  1.49  macallan 
    867  1.49  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    868  1.49  macallan 		return EINVAL;
    869  1.49  macallan 
    870  1.49  macallan 
    871  1.49  macallan 	for (i = 0; i < count; i++) {
    872  1.49  macallan 		rbuf[i] = sc->sc_cmap.cm_map[index][3];
    873  1.49  macallan 		gbuf[i] = sc->sc_cmap.cm_map[index][2];
    874  1.49  macallan 		bbuf[i] = sc->sc_cmap.cm_map[index][1];
    875  1.49  macallan 
    876  1.49  macallan 		index++;
    877  1.49  macallan 	}
    878  1.49  macallan 	error = copyout(rbuf,   cm->red,   count);
    879  1.49  macallan 	if (error)
    880  1.49  macallan 		return error;
    881  1.49  macallan 	error = copyout(gbuf, cm->green, count);
    882  1.49  macallan 	if (error)
    883  1.49  macallan 		return error;
    884  1.49  macallan 	error = copyout(bbuf,  cm->blue,  count);
    885  1.49  macallan 	if (error)
    886  1.49  macallan 		return error;
    887  1.49  macallan 
    888  1.49  macallan 	return 0;
    889  1.49  macallan }
    890  1.63  macallan 
    891  1.49  macallan static int
    892  1.49  macallan cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    893  1.49  macallan 	struct lwp *l)
    894  1.49  macallan {
    895  1.49  macallan 	struct vcons_data *vd = v;
    896  1.49  macallan 	struct cgfourteen_softc *sc = vd->cookie;
    897  1.49  macallan 	struct wsdisplay_fbinfo *wdf;
    898  1.49  macallan 	struct vcons_screen *ms = vd->active;
    899  1.49  macallan 
    900  1.49  macallan 	switch (cmd) {
    901  1.49  macallan 
    902  1.49  macallan 		case WSDISPLAYIO_GTYPE:
    903  1.49  macallan 			*(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
    904  1.49  macallan 			return 0;
    905  1.49  macallan 
    906  1.49  macallan 		case WSDISPLAYIO_GINFO:
    907  1.49  macallan 			wdf = (void *)data;
    908  1.49  macallan 			wdf->height = ms->scr_ri.ri_height;
    909  1.49  macallan 			wdf->width = ms->scr_ri.ri_width;
    910  1.49  macallan 			wdf->depth = 32;
    911  1.49  macallan 			wdf->cmsize = 256;
    912  1.49  macallan 			return 0;
    913  1.49  macallan 
    914  1.49  macallan 		case WSDISPLAYIO_GETCMAP:
    915  1.49  macallan 			return cg14_getcmap(sc,
    916  1.49  macallan 			    (struct wsdisplay_cmap *)data);
    917  1.49  macallan 
    918  1.49  macallan 		case WSDISPLAYIO_PUTCMAP:
    919  1.49  macallan 			return cg14_putcmap(sc,
    920  1.49  macallan 			    (struct wsdisplay_cmap *)data);
    921  1.49  macallan 
    922  1.49  macallan 		case WSDISPLAYIO_LINEBYTES:
    923  1.55  macallan 			*(u_int *)data = ms->scr_ri.ri_stride << 2;
    924  1.49  macallan 			return 0;
    925  1.49  macallan 
    926  1.49  macallan 		case WSDISPLAYIO_SMODE:
    927  1.49  macallan 			{
    928  1.49  macallan 				int new_mode = *(int*)data;
    929  1.49  macallan 				if (new_mode != sc->sc_mode) {
    930  1.49  macallan 					sc->sc_mode = new_mode;
    931  1.49  macallan 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    932  1.50  macallan 						bus_space_write_1(sc->sc_bustag,
    933  1.50  macallan 						    sc->sc_regh,
    934  1.50  macallan 						    CG14_CURSOR_CONTROL, 0);
    935  1.49  macallan 						cg14_set_depth(sc, 8);
    936  1.49  macallan 						cg14_init_cmap(sc);
    937  1.49  macallan 						vcons_redraw_screen(ms);
    938  1.55  macallan 					} else {
    939  1.49  macallan 						cg14_set_depth(sc, 32);
    940  1.55  macallan 					}
    941  1.49  macallan 				}
    942  1.49  macallan 			}
    943  1.49  macallan 			return 0;
    944  1.50  macallan 		case WSDISPLAYIO_SVIDEO:
    945  1.50  macallan 			cg14_set_video(sc, *(int *)data);
    946  1.50  macallan 			return 0;
    947  1.50  macallan 		case WSDISPLAYIO_GVIDEO:
    948  1.50  macallan 			return cg14_get_video(sc) ?
    949  1.50  macallan 			    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
    950  1.50  macallan 		case WSDISPLAYIO_GCURPOS:
    951  1.50  macallan 			{
    952  1.50  macallan 				struct wsdisplay_curpos *cp = (void *)data;
    953  1.50  macallan 
    954  1.50  macallan 				cp->x = sc->sc_cursor.cc_pos.x;
    955  1.50  macallan 				cp->y = sc->sc_cursor.cc_pos.y;
    956  1.50  macallan 			}
    957  1.50  macallan 			return 0;
    958  1.50  macallan 		case WSDISPLAYIO_SCURPOS:
    959  1.50  macallan 			{
    960  1.50  macallan 				struct wsdisplay_curpos *cp = (void *)data;
    961  1.50  macallan 
    962  1.50  macallan 				cg14_move_cursor(sc, cp->x, cp->y);
    963  1.50  macallan 			}
    964  1.50  macallan 			return 0;
    965  1.50  macallan 		case WSDISPLAYIO_GCURMAX:
    966  1.50  macallan 			{
    967  1.50  macallan 				struct wsdisplay_curpos *cp = (void *)data;
    968  1.50  macallan 
    969  1.50  macallan 				cp->x = 32;
    970  1.50  macallan 				cp->y = 32;
    971  1.50  macallan 			}
    972  1.50  macallan 			return 0;
    973  1.50  macallan 		case WSDISPLAYIO_SCURSOR:
    974  1.50  macallan 			{
    975  1.50  macallan 				struct wsdisplay_cursor *cursor = (void *)data;
    976  1.50  macallan 
    977  1.50  macallan 				return cg14_do_cursor(sc, cursor);
    978  1.50  macallan 			}
    979  1.55  macallan 		case PCI_IOC_CFGREAD:
    980  1.55  macallan 		case PCI_IOC_CFGWRITE:
    981  1.55  macallan 			return EINVAL;
    982  1.55  macallan 
    983  1.49  macallan 	}
    984  1.49  macallan 	return EPASSTHROUGH;
    985  1.49  macallan }
    986  1.49  macallan 
    987  1.49  macallan static paddr_t
    988  1.49  macallan cg14_mmap(void *v, void *vs, off_t offset, int prot)
    989  1.49  macallan {
    990  1.49  macallan 	struct vcons_data *vd = v;
    991  1.49  macallan 	struct cgfourteen_softc *sc = vd->cookie;
    992  1.49  macallan 
    993  1.49  macallan 	/* allow mmap()ing the full framebuffer, not just what we use */
    994  1.49  macallan 	if (offset < sc->sc_vramsize)
    995  1.49  macallan 		return bus_space_mmap(sc->sc_bustag,
    996  1.49  macallan 		    BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
    997  1.49  macallan 		      sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
    998  1.49  macallan 		    offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
    999  1.49  macallan 
   1000  1.49  macallan 	return -1;
   1001  1.49  macallan }
   1002  1.49  macallan 
   1003  1.49  macallan static void
   1004  1.49  macallan cg14_init_screen(void *cookie, struct vcons_screen *scr,
   1005  1.49  macallan     int existing, long *defattr)
   1006  1.49  macallan {
   1007  1.49  macallan 	struct cgfourteen_softc *sc = cookie;
   1008  1.49  macallan 	struct rasops_info *ri = &scr->scr_ri;
   1009  1.49  macallan 
   1010  1.49  macallan 	ri->ri_depth = 8;
   1011  1.49  macallan 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
   1012  1.49  macallan 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
   1013  1.49  macallan 	ri->ri_stride = ri->ri_width;
   1014  1.49  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
   1015  1.49  macallan 
   1016  1.65  macallan 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
   1017  1.65  macallan 	scr->scr_flags |= VCONS_DONT_READ;
   1018  1.49  macallan 
   1019  1.49  macallan 	if (existing) {
   1020  1.49  macallan 		ri->ri_flg |= RI_CLEAR;
   1021  1.49  macallan 	}
   1022  1.49  macallan 
   1023  1.49  macallan 	rasops_init(ri, sc->sc_fb.fb_type.fb_height / 8,
   1024  1.49  macallan 	     sc->sc_fb.fb_type.fb_width / 8);
   1025  1.49  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1026  1.49  macallan 
   1027  1.49  macallan 	rasops_reconfig(ri,
   1028  1.49  macallan 	    sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
   1029  1.49  macallan 	    sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
   1030  1.49  macallan 
   1031  1.49  macallan 	ri->ri_hw = scr;
   1032  1.49  macallan }
   1033  1.49  macallan 
   1034  1.49  macallan static void
   1035  1.49  macallan cg14_set_depth(struct cgfourteen_softc *sc, int depth)
   1036  1.49  macallan {
   1037  1.49  macallan 	int i;
   1038  1.49  macallan 
   1039  1.49  macallan 	if (sc->sc_depth == depth)
   1040  1.49  macallan 		return;
   1041  1.49  macallan 	switch (depth) {
   1042  1.49  macallan 		case 8:
   1043  1.49  macallan 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1044  1.49  macallan 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1045  1.49  macallan 			    CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
   1046  1.49  macallan 			sc->sc_depth = 8;
   1047  1.49  macallan 			/* everything is CLUT1 */
   1048  1.49  macallan 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1049  1.49  macallan 			     sc->sc_xlut->xlut_lut[i] = 0;
   1050  1.49  macallan 			break;
   1051  1.49  macallan 		case 32:
   1052  1.49  macallan 			bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1053  1.49  macallan 			    CG14_MCTL, CG14_MCTL_ENABLEVID |
   1054  1.49  macallan 			    CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
   1055  1.49  macallan 			sc->sc_depth = 32;
   1056  1.49  macallan 			for (i = 0; i < CG14_CLUT_SIZE; i++)
   1057  1.49  macallan 			     sc->sc_xlut->xlut_lut[i] = 0;
   1058  1.49  macallan 			break;
   1059  1.49  macallan 		default:
   1060  1.49  macallan 			printf("%s: can't change to depth %d\n",
   1061  1.53  macallan 			    device_xname(sc->sc_dev), depth);
   1062  1.49  macallan 	}
   1063  1.49  macallan }
   1064  1.50  macallan 
   1065  1.50  macallan static void
   1066  1.50  macallan cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
   1067  1.50  macallan {
   1068  1.50  macallan 	uint32_t pos;
   1069  1.50  macallan 
   1070  1.50  macallan 	sc->sc_cursor.cc_pos.x = x;
   1071  1.50  macallan 	sc->sc_cursor.cc_pos.y = y;
   1072  1.50  macallan 	pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
   1073  1.50  macallan 	      ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
   1074  1.50  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
   1075  1.50  macallan }
   1076  1.50  macallan 
   1077  1.50  macallan static int
   1078  1.50  macallan cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
   1079  1.50  macallan {
   1080  1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1081  1.50  macallan 
   1082  1.50  macallan 		bus_space_write_1(sc->sc_bustag, sc->sc_regh,
   1083  1.50  macallan 		    CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
   1084  1.50  macallan 	}
   1085  1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1086  1.50  macallan 
   1087  1.50  macallan 		sc->sc_cursor.cc_hot.x = cur->hot.x;
   1088  1.50  macallan 		sc->sc_cursor.cc_hot.y = cur->hot.y;
   1089  1.50  macallan 		cur->which |= WSDISPLAY_CURSOR_DOPOS;
   1090  1.50  macallan 	}
   1091  1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1092  1.50  macallan 
   1093  1.50  macallan 		cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
   1094  1.50  macallan 	}
   1095  1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1096  1.50  macallan 		int i;
   1097  1.50  macallan 		uint32_t val;
   1098  1.50  macallan 
   1099  1.55  macallan 		for (i = 0; i < min(cur->cmap.count, 3); i++) {
   1100  1.50  macallan 			val = (cur->cmap.red[i] ) |
   1101  1.50  macallan 			      (cur->cmap.green[i] << 8) |
   1102  1.50  macallan 			      (cur->cmap.blue[i] << 16);
   1103  1.50  macallan 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1104  1.50  macallan 			    CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
   1105  1.50  macallan 			    val);
   1106  1.50  macallan 		}
   1107  1.50  macallan 	}
   1108  1.50  macallan 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1109  1.50  macallan 		uint32_t buffer[32], latch, tmp;
   1110  1.50  macallan 		int i;
   1111  1.50  macallan 
   1112  1.55  macallan 		copyin(cur->mask, buffer, 128);
   1113  1.50  macallan 		for (i = 0; i < 32; i++) {
   1114  1.50  macallan 			latch = 0;
   1115  1.50  macallan 			tmp = buffer[i] & 0x80808080;
   1116  1.50  macallan 			latch |= tmp >> 7;
   1117  1.50  macallan 			tmp = buffer[i] & 0x40404040;
   1118  1.50  macallan 			latch |= tmp >> 5;
   1119  1.50  macallan 			tmp = buffer[i] & 0x20202020;
   1120  1.50  macallan 			latch |= tmp >> 3;
   1121  1.50  macallan 			tmp = buffer[i] & 0x10101010;
   1122  1.50  macallan 			latch |= tmp >> 1;
   1123  1.50  macallan 			tmp = buffer[i] & 0x08080808;
   1124  1.50  macallan 			latch |= tmp << 1;
   1125  1.50  macallan 			tmp = buffer[i] & 0x04040404;
   1126  1.50  macallan 			latch |= tmp << 3;
   1127  1.50  macallan 			tmp = buffer[i] & 0x02020202;
   1128  1.50  macallan 			latch |= tmp << 5;
   1129  1.50  macallan 			tmp = buffer[i] & 0x01010101;
   1130  1.50  macallan 			latch |= tmp << 7;
   1131  1.50  macallan 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1132  1.50  macallan 			    CG14_CURSOR_PLANE0 + (i << 2), latch);
   1133  1.50  macallan 		}
   1134  1.55  macallan 		copyin(cur->image, buffer, 128);
   1135  1.50  macallan 		for (i = 0; i < 32; i++) {
   1136  1.50  macallan 			latch = 0;
   1137  1.50  macallan 			tmp = buffer[i] & 0x80808080;
   1138  1.50  macallan 			latch |= tmp >> 7;
   1139  1.50  macallan 			tmp = buffer[i] & 0x40404040;
   1140  1.50  macallan 			latch |= tmp >> 5;
   1141  1.50  macallan 			tmp = buffer[i] & 0x20202020;
   1142  1.50  macallan 			latch |= tmp >> 3;
   1143  1.50  macallan 			tmp = buffer[i] & 0x10101010;
   1144  1.50  macallan 			latch |= tmp >> 1;
   1145  1.50  macallan 			tmp = buffer[i] & 0x08080808;
   1146  1.50  macallan 			latch |= tmp << 1;
   1147  1.50  macallan 			tmp = buffer[i] & 0x04040404;
   1148  1.50  macallan 			latch |= tmp << 3;
   1149  1.50  macallan 			tmp = buffer[i] & 0x02020202;
   1150  1.50  macallan 			latch |= tmp << 5;
   1151  1.50  macallan 			tmp = buffer[i] & 0x01010101;
   1152  1.50  macallan 			latch |= tmp << 7;
   1153  1.50  macallan 			bus_space_write_4(sc->sc_bustag, sc->sc_regh,
   1154  1.50  macallan 			    CG14_CURSOR_PLANE1 + (i << 2), latch);
   1155  1.50  macallan 		}
   1156  1.50  macallan 	}
   1157  1.50  macallan 	return 0;
   1158  1.50  macallan }
   1159  1.49  macallan #endif
   1160