Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: cgfour.c,v 1.52 2026/05/25 13:47:08 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1992, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * This software was developed by the Computer Systems Engineering group
     37  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     38  * contributed to Berkeley.
     39  *
     40  * All advertising materials mentioning features or use of this software
     41  * must display the following acknowledgement:
     42  *	This product includes software developed by the University of
     43  *	California, Lawrence Berkeley Laboratory.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  *
     69  *	from @(#)cgthree.c	8.2 (Berkeley) 10/30/93
     70  */
     71 
     72 /*
     73  * Copyright (c) 1995 Theo de Raadt.  All rights reserved.
     74  *
     75  * Redistribution and use in source and binary forms, with or without
     76  * modification, are permitted provided that the following conditions
     77  * are met:
     78  * 1. Redistributions of source code must retain the above copyright
     79  *    notice, this list of conditions and the following disclaimer.
     80  * 2. Redistributions in binary form must reproduce the above copyright
     81  *    notice, this list of conditions and the following disclaimer in the
     82  *    documentation and/or other materials provided with the distribution.
     83  *
     84  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     85  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     86  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     87  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     88  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     89  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     90  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     91  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     92  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     93  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     94  */
     95 
     96 /*
     97  * color display (cgfour) driver.
     98  *
     99  * Does not handle interrupts, even though they can occur.
    100  *
    101  * XXX should defer colormap updates to vertical retrace interrupts
    102  */
    103 
    104 #include "wsdisplay.h"
    105 
    106 #include <sys/cdefs.h>
    107 __KERNEL_RCSID(0, "$NetBSD: cgfour.c,v 1.52 2026/05/25 13:47:08 christos Exp $");
    108 
    109 #include <sys/param.h>
    110 #include <sys/systm.h>
    111 #include <sys/buf.h>
    112 #include <sys/device.h>
    113 #include <sys/ioctl.h>
    114 #include <sys/mman.h>
    115 #include <sys/tty.h>
    116 #include <sys/conf.h>
    117 
    118 #include <machine/autoconf.h>
    119 #include <machine/eeprom.h>
    120 
    121 #include <dev/sun/fbio.h>
    122 #include <dev/sun/fbvar.h>
    123 #include <dev/sun/btreg.h>
    124 #include <dev/sun/btvar.h>
    125 #include <dev/sun/pfourreg.h>
    126 
    127 #if NWSDISPLAY > 0
    128 #include <dev/wscons/wsconsio.h>
    129 #include <dev/wscons/wsdisplay_vconsvar.h>
    130 #include <dev/wsfont/wsfont.h>
    131 #include <dev/rasops/rasops.h>
    132 
    133 #include "opt_wsemul.h"
    134 #endif
    135 
    136 #define CGFOUR_OVERLAY_SIZE    0x20000
    137 #define CGFOUR_RAM_SIZE                0x100000        /* All our cgfour's are 1MB */
    138 
    139 /* per-display variables */
    140 struct cgfour_softc {
    141 	struct fbdevice	sc_fb;		/* frame buffer device */
    142 	bus_space_tag_t	sc_bustag;
    143 	bus_addr_t	sc_paddr;	/* phys address for device mmap() */
    144 
    145 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
    146 #if NWSDISPLAY > 0
    147        uint32_t sc_width;
    148        uint32_t sc_height;             /* display width / height */
    149        uint32_t sc_stride;
    150        int sc_mode;
    151        struct vcons_data sc_vd;
    152        char *sc_overlay;
    153        char *sc_enable;
    154 #endif
    155 	union bt_cmap	sc_cmap;	/* Brooktree color map */
    156 };
    157 
    158 /* autoconfiguration driver */
    159 static int	cgfourmatch(device_t, cfdata_t, void *);
    160 static void	cgfourattach(device_t, device_t, void *);
    161 
    162 #if defined(SUN4)
    163 static void	cgfourunblank(device_t);
    164 #endif
    165 
    166 static int	cg4_pfour_probe(void *, void *);
    167 
    168 CFATTACH_DECL_NEW(cgfour, sizeof(struct cgfour_softc),
    169     cgfourmatch, cgfourattach, NULL, NULL);
    170 
    171 extern struct cfdriver cgfour_cd;
    172 
    173 dev_type_open(cgfouropen);
    174 dev_type_ioctl(cgfourioctl);
    175 dev_type_mmap(cgfourmmap);
    176 
    177 const struct cdevsw cgfour_cdevsw = {
    178 	.d_open = cgfouropen,
    179 	.d_close = nullclose,
    180 	.d_read = noread,
    181 	.d_write = nowrite,
    182 	.d_ioctl = cgfourioctl,
    183 	.d_stop = nostop,
    184 	.d_tty = notty,
    185 	.d_poll = nopoll,
    186 	.d_mmap = cgfourmmap,
    187 	.d_kqfilter = nokqfilter,
    188 	.d_discard = nodiscard,
    189 	.d_flag = 0
    190 };
    191 
    192 #if defined(SUN4)
    193 /* frame buffer generic driver */
    194 static struct fbdriver cgfourfbdriver = {
    195 	cgfourunblank, cgfouropen, nullclose, cgfourioctl, nopoll,
    196 	cgfourmmap, nokqfilter
    197 };
    198 
    199 static void cgfourloadcmap(struct cgfour_softc *, int, int);
    200 static int cgfour_get_video(struct cgfour_softc *);
    201 static void cgfour_set_video(struct cgfour_softc *, int);
    202 
    203 #if NWSDISPLAY > 0
    204 static void cgfour_setup_palette(struct cgfour_softc *);
    205 
    206 struct wsscreen_descr cgfour_defaultscreen = {
    207        "std",
    208        0, 0,   /* will be filled in -- XXX shouldn't, it's global */
    209        NULL,   /* textops */
    210        8, 16,  /* font width/height */
    211        WSSCREEN_WSCOLORS,      /* capabilities */
    212        NULL    /* modecookie */
    213 };
    214 
    215 static int cgfour_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    216 static paddr_t cgfour_mmap(void *, void *, off_t, int);
    217 static void cgfour_init_screen(void *, struct vcons_screen *, int, long *);
    218 static void cgfour_clear_screen(struct cgfour_softc *, int);
    219 
    220 static int cgfour_putcmap(struct cgfour_softc *, struct wsdisplay_cmap *);
    221 static int cgfour_getcmap(struct cgfour_softc *, struct wsdisplay_cmap *);
    222 
    223 struct wsdisplay_accessops cgfour_accessops = {
    224        .ioctl = cgfour_ioctl,
    225        .mmap = cgfour_mmap,
    226        .alloc_screen = NULL,
    227        .free_screen = NULL,
    228        .show_screen = NULL,
    229        .load_font = NULL,
    230        .pollc = NULL,
    231        .scroll = NULL
    232 };
    233 
    234 const struct wsscreen_descr *_cgfour_scrlist[] = {
    235        &cgfour_defaultscreen
    236 };
    237 
    238 struct wsscreen_list cgfour_screenlist = {
    239        .nscreens = sizeof(_cgfour_scrlist) / sizeof(struct wsscreen_descr *),
    240        .screens = _cgfour_scrlist
    241 };
    242 
    243 static struct vcons_screen cgfour_console_screen;
    244 #endif /* NWSDISPLAY > 0 */
    245 #endif /* SUN4 */
    246 /*
    247  * Match a cgfour.
    248  */
    249 static int
    250 cgfourmatch(device_t parent, cfdata_t cf, void *aux)
    251 {
    252 	union obio_attach_args *uoba = aux;
    253 	struct obio4_attach_args *oba;
    254 
    255 	if (uoba->uoba_isobio4 == 0)
    256 		return (0);
    257 
    258 	oba = &uoba->uoba_oba4;
    259 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    260 				4,	/* probe size */
    261 				0,	/* offset */
    262 				0,	/* flags */
    263 				cg4_pfour_probe, NULL));
    264 }
    265 
    266 static int
    267 cg4_pfour_probe(void *vaddr, void *arg)
    268 {
    269 
    270 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR8P1);
    271 }
    272 
    273 /*
    274  * Attach a display.  We need to notice if it is the console, too.
    275  */
    276 static void
    277 cgfourattach(device_t parent, device_t self, void *aux)
    278 {
    279 #if defined(SUN4)
    280 	struct cgfour_softc *sc = device_private(self);
    281 	union obio_attach_args *uoba = aux;
    282 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    283 	bus_space_handle_t bh;
    284 	volatile struct bt_regs *bt;
    285 	struct fbdevice *fb = &sc->sc_fb;
    286 	int i, isconsole;
    287 #if NWSDISPLAY > 0
    288        struct wsemuldisplaydev_attach_args wsemul_aa;
    289        struct rasops_info *ri = &cgfour_console_screen.scr_ri;
    290        unsigned long defattr = 0;
    291 #endif
    292 	sc->sc_bustag = oba->oba_bustag;
    293 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
    294 
    295 	/* Map the pfour register. */
    296 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
    297 			  sizeof(uint32_t),
    298 			  BUS_SPACE_MAP_LINEAR,
    299 			  &bh) != 0) {
    300 		printf("%s: cannot map control registers\n",
    301 			device_xname(self));
    302 		return;
    303 	}
    304 	fb->fb_pfour = (volatile uint32_t *)bh;
    305 
    306 	fb->fb_driver = &cgfourfbdriver;
    307 	fb->fb_device = self;
    308 	fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
    309 	fb->fb_flags = device_cfdata(self)->cf_flags & FB_USERMASK;
    310 	fb->fb_flags |= FB_PFOUR;
    311 
    312 
    313 	fb->fb_type.fb_depth = 8;
    314 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
    315 
    316 	fb->fb_type.fb_cmsize = 256;
    317 	fb->fb_type.fb_size = CGFOUR_RAM_SIZE;
    318 	printf(": cgfour/p4, %d x %d",
    319 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    320 
    321 	isconsole = 0;
    322 
    323 	if (CPU_ISSUN4) {
    324 		struct eeprom *eep = (struct eeprom *)eeprom_va;
    325 
    326 		/*
    327 		 * Assume this is the console if there's no eeprom info
    328 		 * to be found.
    329 		 */
    330 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
    331 			isconsole = fb_is_console(0);
    332 	}
    333 
    334 	/*
    335 	 * When the ROM has mapped in a cgfour display, the address
    336 	 * maps only the video RAM, so in any case we have to map the
    337 	 * registers ourselves.
    338 	 */
    339 
    340 	if (isconsole) {
    341                /* Map the framebuffers. */
    342                if (bus_space_map(oba->oba_bustag,
    343                            oba->oba_paddr + PFOUR_COLOR_OFF_OVERLAY,
    344                            CGFOUR_OVERLAY_SIZE,
    345                            BUS_SPACE_MAP_LINEAR,
    346                            &bh) != 0) {
    347                        printf("%s: cannot map overlay plane\n",
    348                            device_xname(self));
    349                return;
    350                }
    351                sc->sc_overlay = (char *)bh;
    352 
    353                if (bus_space_map(oba->oba_bustag,
    354                            oba->oba_paddr + PFOUR_COLOR_OFF_ENABLE,
    355                            CGFOUR_OVERLAY_SIZE,
    356                            BUS_SPACE_MAP_LINEAR,
    357                            &bh) != 0) {
    358                        printf("%s: cannot map enable plane\n",
    359                            device_xname(self));
    360                        return;
    361                }
    362                sc->sc_enable  = (char *)bh;
    363 
    364                if (bus_space_map(oba->oba_bustag,
    365                            oba->oba_paddr + PFOUR_COLOR_OFF_COLOR,
    366                            CGFOUR_RAM_SIZE,
    367                            BUS_SPACE_MAP_LINEAR,
    368                            &bh) != 0) {
    369                        printf("%s: cannot map pixels\n",
    370                            device_xname(self));
    371                        return;
    372                }
    373                fb->fb_pixels = (char *)bh;
    374         }
    375 
    376 	/* Map the Brooktree. */
    377 	if (bus_space_map(oba->oba_bustag,
    378 			  oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
    379 			  sizeof(struct fbcontrol),
    380 			  BUS_SPACE_MAP_LINEAR,
    381 			  &bh) != 0) {
    382 		printf("%s: cannot map control registers\n",
    383 			device_xname(self));
    384 		return;
    385 	}
    386 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
    387 
    388 	/* grab initial (current) color map */
    389 	bt = &sc->sc_fbc->fbc_dac;
    390 	bt->bt_addr = 0;
    391 	for (i = 0; i < 256 * 3 / 4; i++)
    392 		((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
    393 
    394 	BT_INIT(bt, 24);
    395 
    396 	if (isconsole) {
    397 		printf(" (console)\n");
    398 	} else
    399 		printf("\n");
    400 
    401 	/*
    402 	 * Even though we're not using rconsole, we'd still like
    403 	 * to notice if we're the console framebuffer.
    404 	 */
    405 	fb_attach(fb, isconsole);
    406 
    407 #if NWSDISPLAY > 0
    408        sc->sc_width = fb->fb_type.fb_width;
    409        sc->sc_stride = fb->fb_type.fb_width;
    410        sc->sc_height = fb->fb_type.fb_height;
    411 
    412        /* setup rasops and so on for wsdisplay */
    413        sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    414 
    415        vcons_init(&sc->sc_vd, sc, &cgfour_defaultscreen, &cgfour_accessops);
    416        sc->sc_vd.init_screen = cgfour_init_screen;
    417 
    418        if (isconsole) {
    419                /* we mess with cgfour_console_screen only once */
    420                vcons_init_screen(&sc->sc_vd, &cgfour_console_screen, 1,
    421                    &defattr);
    422                cgfour_clear_screen(sc, (defattr >> 16) & 0xff);
    423                cgfour_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    424 
    425                cgfour_defaultscreen.textops = &ri->ri_ops;
    426                cgfour_defaultscreen.capabilities = ri->ri_caps;
    427                cgfour_defaultscreen.nrows = ri->ri_rows;
    428                cgfour_defaultscreen.ncols = ri->ri_cols;
    429                SCREEN_VISIBLE(&cgfour_console_screen);
    430                sc->sc_vd.active = &cgfour_console_screen;
    431                wsdisplay_cnattach(&cgfour_defaultscreen, ri, 0, 0, defattr);
    432                vcons_replay_msgbuf(&cgfour_console_screen);
    433        } else {
    434                /*
    435                 * we're not the console so we just clear the screen and don't
    436                 * set up any sort of text display
    437                 */
    438        }
    439 
    440        /* Initialize the default color map. */
    441        cgfour_setup_palette(sc);
    442 
    443        wsemul_aa.scrdata = &cgfour_screenlist;
    444        wsemul_aa.console = isconsole;
    445        wsemul_aa.accessops = &cgfour_accessops;
    446        wsemul_aa.accesscookie = &sc->sc_vd;
    447        config_found(self, &wsemul_aa, wsemuldisplaydevprint,
    448            CFARGS_NONE);
    449 #endif /* NWSDISPLAY > 0 */
    450 #endif /* SUN4 */
    451 }
    452 
    453 int
    454 cgfouropen(dev_t dev, int flags, int mode, struct lwp *l)
    455 {
    456 	int unit = minor(dev);
    457 
    458 	if (device_lookup(&cgfour_cd, unit) == NULL)
    459 		return (ENXIO);
    460 	return (0);
    461 }
    462 
    463 int
    464 cgfourioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    465 {
    466 #if defined(SUN4)
    467 	struct cgfour_softc *sc = device_lookup_private(&cgfour_cd, minor(dev));
    468 	struct fbgattr *fba;
    469 	int error;
    470 
    471 	switch (cmd) {
    472 
    473 	case FBIOGTYPE:
    474 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    475 		break;
    476 
    477 	case FBIOGATTR:
    478 		fba = (struct fbgattr *)data;
    479 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    480 		fba->owner = 0;		/* XXX ??? */
    481 		fba->fbtype = sc->sc_fb.fb_type;
    482 		fba->sattr.flags = 0;
    483 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    484 		fba->sattr.dev_specific[0] = -1;
    485 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    486 		fba->emu_types[1] = -1;
    487 		break;
    488 
    489 	case FBIOGETCMAP:
    490 #define p ((struct fbcmap *)data)
    491 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    492 
    493 	case FBIOPUTCMAP:
    494 		/* copy to software map */
    495 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    496 		if (error)
    497 			return (error);
    498 		/* now blast them into the chip */
    499 		/* XXX should use retrace interrupt */
    500 		cgfourloadcmap(sc, p->index, p->count);
    501 #undef p
    502 		break;
    503 
    504 	case FBIOGVIDEO:
    505 		*(int *)data = cgfour_get_video(sc);
    506 		break;
    507 
    508 	case FBIOSVIDEO:
    509 		cgfour_set_video(sc, *(int *)data);
    510 		break;
    511 
    512 	default:
    513 		return (ENOTTY);
    514 	}
    515 #endif /* SUN4 */
    516 
    517 	return (0);
    518 }
    519 
    520 /*
    521  * Return the address that would map the given device at the given
    522  * offset, allowing for the given protection, or return -1 for error.
    523  *
    524  * the cg4 maps its overlay plane for 128K, followed by the enable
    525  * plane for 128K, followed by the colour plane (for as much colour
    526  * as their is.)
    527  *
    528  * As well, mapping at an offset of 0x04000000 causes the cg4 to map
    529  * only its colour plane, at 0.
    530  */
    531 paddr_t
    532 cgfourmmap(dev_t dev, off_t off, int prot)
    533 {
    534 	struct cgfour_softc *sc = device_lookup_private(&cgfour_cd, minor(dev));
    535 	off_t poff;
    536 
    537 #define START_ENABLE	(128*1024)
    538 #define START_COLOR	((128*1024) + (128*1024))
    539 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
    540 			    sc->sc_fb.fb_type.fb_height)
    541 #define END_COLOR	(START_COLOR + COLOR_SIZE)
    542 #define NOOVERLAY	(0x04000000)
    543 
    544 	if (off & PGOFSET)
    545 		panic("cgfourmap");
    546 
    547 	if (off < 0)
    548 		return (-1);
    549 	else if ((u_int)off >= NOOVERLAY) {
    550 		off -= NOOVERLAY;
    551 
    552 		/*
    553 		 * X11 maps a huge chunk of the frame buffer; far more than
    554 		 * there really is. We compensate by double-mapping the
    555 		 * first page for as many other pages as it wants
    556 		 */
    557 		while ((u_int)off >= COLOR_SIZE)
    558 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
    559 
    560 		poff = off + PFOUR_COLOR_OFF_COLOR;
    561 	} else if ((u_int)off < START_ENABLE) {
    562 		/*
    563 		 * in overlay plane
    564 		 */
    565 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
    566 	} else if ((u_int)off < START_COLOR) {
    567 		/*
    568 		 * in enable plane
    569 		 */
    570 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
    571 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
    572 		/*
    573 		 * in colour plane
    574 		 */
    575 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
    576 	} else
    577 		return (-1);
    578 
    579 	return (bus_space_mmap(sc->sc_bustag,
    580 		sc->sc_paddr, poff,
    581 		prot, BUS_SPACE_MAP_LINEAR));
    582 }
    583 
    584 #if defined(SUN4)
    585 /*
    586  * Undo the effect of an FBIOSVIDEO that turns the video off.
    587  */
    588 static void
    589 cgfourunblank(device_t dev)
    590 {
    591 
    592 	cgfour_set_video(device_private(dev), 1);
    593 }
    594 
    595 static int
    596 cgfour_get_video(struct cgfour_softc *sc)
    597 {
    598 
    599 	return (fb_pfour_get_video(&sc->sc_fb));
    600 }
    601 
    602 static void
    603 cgfour_set_video(struct cgfour_softc *sc, int enable)
    604 {
    605 
    606 	fb_pfour_set_video(&sc->sc_fb, enable);
    607 }
    608 
    609 /*
    610  * Load a subset of the current (new) colormap into the Brooktree DAC.
    611  */
    612 static void
    613 cgfourloadcmap(struct cgfour_softc *sc, int start, int ncolors)
    614 {
    615 	volatile struct bt_regs *bt;
    616 	u_int *ip, i;
    617 	int count;
    618 
    619 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    620 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    621 	bt = &sc->sc_fbc->fbc_dac;
    622 	bt->bt_addr = BT_D4M4(start) << 24;
    623 	while (--count >= 0) {
    624 		i = *ip++;
    625 		/* hardware that makes one want to pound boards with hammers */
    626 		bt->bt_cmap = i;
    627 		bt->bt_cmap = i << 8;
    628 		bt->bt_cmap = i << 16;
    629 		bt->bt_cmap = i << 24;
    630 	}
    631 }
    632 
    633 #if NWSDISPLAY > 0
    634 static void
    635 cgfour_setup_palette(struct cgfour_softc *sc)
    636 {
    637        int i, j;
    638 
    639        j = 0;
    640        for (i = 0; i < 256; i++) {
    641                sc->sc_cmap.cm_map[i][0] = rasops_cmap[j];
    642                j++;
    643                sc->sc_cmap.cm_map[i][1] = rasops_cmap[j];
    644                j++;
    645                sc->sc_cmap.cm_map[i][2] = rasops_cmap[j];
    646                j++;
    647        }
    648        cgfourloadcmap(sc, 0, 256);
    649 }
    650 
    651 static int
    652 cgfour_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    653     struct lwp *l)
    654 {
    655        /* we'll probably need to add more stuff here */
    656        struct vcons_data *vd = v;
    657        struct cgfour_softc *sc = vd->cookie;
    658        struct wsdisplay_fbinfo *wdf;
    659        struct vcons_screen *ms = sc->sc_vd.active;
    660        struct rasops_info *ri = &ms->scr_ri;
    661        switch (cmd) {
    662                case WSDISPLAYIO_GTYPE:
    663                        *(u_int *)data = WSDISPLAY_TYPE_SUNTCX; /* XXX */
    664                        return 0;
    665                case WSDISPLAYIO_GINFO:
    666                        wdf = (void *)data;
    667                        wdf->height = ri->ri_height;
    668                        wdf->width = ri->ri_width;
    669                        wdf->depth = 8;
    670                        wdf->cmsize = 256;
    671                        return 0;
    672 
    673                case WSDISPLAYIO_GETCMAP:
    674                        return cgfour_getcmap(sc,
    675                            (struct wsdisplay_cmap *)data);
    676                case WSDISPLAYIO_PUTCMAP:
    677                        return cgfour_putcmap(sc,
    678                            (struct wsdisplay_cmap *)data);
    679 
    680                case WSDISPLAYIO_SMODE:
    681                        {
    682                                int new_mode = *(int *)data;
    683 
    684                                if (new_mode != sc->sc_mode) {
    685                                        sc->sc_mode = new_mode;
    686                                        if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    687                                                cgfour_setup_palette(sc);
    688                                                cgfour_clear_screen(sc, 0xff);
    689                                                vcons_redraw_screen(ms);
    690                                        }
    691                                }
    692                        }
    693                        return 0;
    694                case WSDISPLAYIO_GET_FBINFO:
    695                        {
    696                                struct wsdisplayio_fbinfo *fbi = data;
    697 
    698                                return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    699                        }
    700        }
    701        return EPASSTHROUGH;
    702 }
    703 
    704 static paddr_t
    705 cgfour_mmap(void *v, void *vs, off_t offset, int prot)
    706 {
    707        struct vcons_data *vd = v;
    708        struct cgfour_softc *sc = vd->cookie;
    709        paddr_t cookie = -1;
    710 
    711        switch (sc->sc_mode) {
    712        case WSDISPLAYIO_MODE_DUMBFB:
    713                if (offset >= 0 && offset <= sc->sc_fb.fb_type.fb_size) {
    714                        cookie = bus_space_mmap(sc->sc_bustag,
    715                            sc->sc_paddr, PFOUR_COLOR_OFF_COLOR + offset,
    716                            prot, BUS_SPACE_MAP_LINEAR);
    717                }
    718                break;
    719        default:
    720                break;
    721        }
    722 
    723        return cookie;
    724 }
    725 
    726 static int
    727 cgfour_putcmap(struct cgfour_softc *sc, struct wsdisplay_cmap *cm)
    728 {
    729        u_int index = cm->index;
    730        u_int count = cm->count;
    731        int error,i;
    732        if (index >= 256 || count > 256 || index + count > 256)
    733                return EINVAL;
    734 
    735        for (i = 0; i < count; i++)
    736        {
    737                error = copyin(&cm->red[i],
    738                    &sc->sc_cmap.cm_map[index + i][0], 1);
    739                if (error)
    740                        return error;
    741                error = copyin(&cm->green[i],
    742                    &sc->sc_cmap.cm_map[index + i][1],
    743                    1);
    744                if (error)
    745                        return error;
    746                error = copyin(&cm->blue[i],
    747                    &sc->sc_cmap.cm_map[index + i][2], 1);
    748                if (error)
    749                        return error;
    750        }
    751        cgfourloadcmap(sc, index, count);
    752 
    753        return 0;
    754 }
    755 
    756 static int
    757 cgfour_getcmap(struct cgfour_softc *sc, struct wsdisplay_cmap *cm)
    758 {
    759        u_int index = cm->index;
    760        u_int count = cm->count;
    761        int error,i;
    762 
    763        if (index >= 256 || count > 256 || index + count > 256)
    764                return EINVAL;
    765 
    766        for (i = 0; i < count; i++)
    767        {
    768                error = copyout(&sc->sc_cmap.cm_map[index + i][0],
    769                    &cm->red[i], 1);
    770                if (error)
    771                        return error;
    772                error = copyout(&sc->sc_cmap.cm_map[index + i][1],
    773                    &cm->green[i], 1);
    774                if (error)
    775                        return error;
    776                error = copyout(&sc->sc_cmap.cm_map[index + i][2],
    777                    &cm->blue[i], 1);
    778                if (error)
    779                        return error;
    780        }
    781 
    782        return 0;
    783 }
    784 
    785 static void
    786 cgfour_init_screen(void *cookie, struct vcons_screen *scr,
    787     int existing, long *defattr)
    788 {
    789        struct cgfour_softc *sc = cookie;
    790        struct rasops_info *ri = &scr->scr_ri;
    791 
    792        scr->scr_flags |= VCONS_DONT_READ;
    793 
    794        ri->ri_depth = 8;
    795        ri->ri_width = sc->sc_width;
    796        ri->ri_height = sc->sc_height;
    797        ri->ri_stride = sc->sc_stride;
    798        ri->ri_flg = RI_CENTER;
    799 
    800        ri->ri_bits = sc->sc_fb.fb_pixels;
    801 
    802        rasops_init(ri, 0, 0);
    803        ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
    804        rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    805            sc->sc_width / ri->ri_font->fontwidth);
    806 
    807        ri->ri_hw = scr;
    808 }
    809 
    810 static void
    811 cgfour_clear_screen(struct cgfour_softc *sc, int bg)
    812 {
    813        char *bits;
    814 
    815        /* We can write a maximum of 4 bytes at a time to the framebuffer. */
    816        for (bits = (char *)sc->sc_enable;
    817            bits < (char *)sc->sc_enable + CGFOUR_OVERLAY_SIZE;
    818            bits += 4)
    819                memset(bits,  0x00, 4);
    820 
    821        for (bits = (char *)sc->sc_fb.fb_pixels;
    822            bits < (char *)sc->sc_fb.fb_pixels + sc->sc_stride * sc->sc_height;
    823            bits += 4)
    824                memset(bits, bg, 4);
    825 }
    826 #endif /* NWSDISPLAY > 0 */
    827 #endif /* SUN4 */
    828