Home | History | Annotate | Line # | Download | only in dev
cg4.c revision 1.11
      1  1.11       gwr /*	$NetBSD: cg4.c,v 1.11 1996/10/29 19:54:19 gwr Exp $	*/
      2   1.1       gwr 
      3   1.1       gwr /*
      4   1.1       gwr  * Copyright (c) 1992, 1993
      5   1.1       gwr  *	The Regents of the University of California.  All rights reserved.
      6   1.1       gwr  *
      7   1.1       gwr  * This software was developed by the Computer Systems Engineering group
      8   1.1       gwr  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9   1.1       gwr  * contributed to Berkeley.
     10   1.1       gwr  *
     11   1.1       gwr  * All advertising materials mentioning features or use of this software
     12   1.1       gwr  * must display the following acknowledgement:
     13   1.1       gwr  *	This product includes software developed by the University of
     14   1.1       gwr  *	California, Lawrence Berkeley Laboratory.
     15   1.1       gwr  *
     16   1.1       gwr  * Redistribution and use in source and binary forms, with or without
     17   1.1       gwr  * modification, are permitted provided that the following conditions
     18   1.1       gwr  * are met:
     19   1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     20   1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     21   1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     22   1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     23   1.1       gwr  *    documentation and/or other materials provided with the distribution.
     24   1.1       gwr  * 3. All advertising materials mentioning features or use of this software
     25   1.1       gwr  *    must display the following acknowledgement:
     26   1.1       gwr  *	This product includes software developed by the University of
     27   1.1       gwr  *	California, Berkeley and its contributors.
     28   1.1       gwr  * 4. Neither the name of the University nor the names of its contributors
     29   1.1       gwr  *    may be used to endorse or promote products derived from this software
     30   1.1       gwr  *    without specific prior written permission.
     31   1.1       gwr  *
     32   1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33   1.1       gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34   1.1       gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35   1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36   1.1       gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37   1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38   1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39   1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40   1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41   1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42   1.1       gwr  * SUCH DAMAGE.
     43   1.1       gwr  *
     44   1.1       gwr  *	from: @(#)cgthree.c	8.2 (Berkeley) 10/30/93
     45   1.1       gwr  */
     46   1.1       gwr 
     47   1.1       gwr /*
     48   1.1       gwr  * color display (cg4) driver.
     49   1.1       gwr  *
     50  1.11       gwr  * Credits, history:
     51  1.11       gwr  * Gordon Ross created this driver based on the cg3 driver from
     52  1.11       gwr  * the sparc port as distributed in BSD 4.4 Lite, but included
     53  1.11       gwr  * support for only the "type B" adapter (Brooktree DACs).
     54  1.11       gwr  * Ezra Story added support for the "type A" (AMD DACs).
     55   1.1       gwr  *
     56  1.11       gwr  * Todo:
     57  1.11       gwr  * Make this driver handle video interrupts.
     58  1.11       gwr  * Defer colormap updates to vertical retrace interrupts.
     59   1.1       gwr  */
     60   1.1       gwr 
     61   1.1       gwr #include <sys/param.h>
     62  1.11       gwr #include <sys/systm.h>
     63   1.1       gwr #include <sys/device.h>
     64   1.1       gwr #include <sys/ioctl.h>
     65   1.1       gwr #include <sys/malloc.h>
     66   1.1       gwr #include <sys/mman.h>
     67   1.1       gwr #include <sys/tty.h>
     68   1.1       gwr 
     69   1.1       gwr #include <vm/vm.h>
     70   1.1       gwr 
     71   1.2       gwr #include <machine/cpu.h>
     72   1.1       gwr #include <machine/fbio.h>
     73   1.1       gwr #include <machine/autoconf.h>
     74   1.1       gwr #include <machine/pmap.h>
     75   1.1       gwr 
     76   1.1       gwr #include "fbvar.h"
     77   1.1       gwr #include "btreg.h"
     78   1.1       gwr #include "btvar.h"
     79   1.1       gwr #include "cg4reg.h"
     80   1.1       gwr 
     81  1.11       gwr #define	CG4_MMAP_SIZE (CG4_OVERLAY_SIZE + CG4_ENABLE_SIZE + CG4_PIXMAP_SIZE)
     82  1.11       gwr 
     83   1.2       gwr extern unsigned char cpu_machine_id;
     84   1.2       gwr 
     85  1.11       gwr #define CMAP_SIZE 256
     86  1.11       gwr struct soft_cmap {
     87  1.11       gwr 	u_char r[CMAP_SIZE];
     88  1.11       gwr 	u_char g[CMAP_SIZE];
     89  1.11       gwr 	u_char b[CMAP_SIZE];
     90  1.11       gwr };
     91  1.11       gwr 
     92   1.1       gwr /* per-display variables */
     93   1.1       gwr struct cg4_softc {
     94   1.1       gwr 	struct	device sc_dev;		/* base device */
     95   1.1       gwr 	struct	fbdevice sc_fb;		/* frame buffer device */
     96  1.11       gwr 	int 	sc_cg4type;		/* A or B */
     97  1.11       gwr 	void	*sc_va_cmap;		/* Colormap h/w (mapped KVA) */
     98  1.11       gwr 	int 	sc_pa_overlay;		/* phys. addr. of overlay plane */
     99  1.11       gwr 	int 	sc_pa_enable;		/* phys. addr. of enable plane */
    100  1.11       gwr 	int 	sc_pa_pixmap;		/* phys. addr. of color plane */
    101   1.1       gwr 	int 	sc_blanked;		/* true if blanked */
    102  1.11       gwr 
    103  1.11       gwr 	union bt_cmap *sc_btcm;		/* Brooktree color map */
    104  1.11       gwr 	struct soft_cmap sc_cmap;	/* Generic soft colormap. */
    105   1.1       gwr };
    106   1.1       gwr 
    107   1.1       gwr /* autoconfiguration driver */
    108   1.1       gwr static void	cg4attach __P((struct device *, struct device *, void *));
    109   1.1       gwr static int	cg4match __P((struct device *, void *, void *));
    110   1.1       gwr 
    111   1.7   thorpej struct cfattach cgfour_ca = {
    112   1.7   thorpej 	sizeof(struct cg4_softc), cg4match, cg4attach
    113   1.7   thorpej };
    114   1.7   thorpej 
    115   1.7   thorpej struct cfdriver cgfour_cd = {
    116   1.7   thorpej 	NULL, "cgfour", DV_DULL
    117   1.7   thorpej };
    118   1.1       gwr 
    119   1.1       gwr /* frame buffer generic driver */
    120   1.4   mycroft int cg4open(), cg4close(), cg4mmap();
    121   1.1       gwr 
    122  1.11       gwr static int	cg4gattr   __P((struct fbdevice *, struct fbgattr *));
    123  1.11       gwr static int	cg4gvideo  __P((struct fbdevice *, int *));
    124  1.11       gwr static int	cg4svideo  __P((struct fbdevice *, int *));
    125   1.1       gwr static int	cg4getcmap __P((struct fbdevice *, struct fbcmap *));
    126   1.1       gwr static int	cg4putcmap __P((struct fbdevice *, struct fbcmap *));
    127   1.1       gwr 
    128  1.11       gwr static void	cg4a_init   __P((struct cg4_softc *));
    129  1.11       gwr static void	cg4a_svideo __P((struct cg4_softc *, int));
    130  1.11       gwr static void	cg4a_ldcmap __P((struct cg4_softc *));
    131  1.11       gwr 
    132  1.11       gwr static void	cg4b_init   __P((struct cg4_softc *));
    133  1.11       gwr static void	cg4b_svideo __P((struct cg4_softc *, int));
    134  1.11       gwr static void	cg4b_ldcmap __P((struct cg4_softc *));
    135  1.11       gwr 
    136  1.11       gwr static struct fbdriver cg4_fbdriver = {
    137   1.6       gwr 	cg4open, cg4close, cg4mmap, cg4gattr,
    138   1.1       gwr 	cg4gvideo, cg4svideo,
    139   1.1       gwr 	cg4getcmap, cg4putcmap };
    140   1.1       gwr 
    141   1.1       gwr /*
    142   1.1       gwr  * Match a cg4.
    143   1.1       gwr  */
    144   1.1       gwr static int
    145   1.1       gwr cg4match(parent, vcf, args)
    146   1.1       gwr 	struct device *parent;
    147   1.1       gwr 	void *vcf, *args;
    148   1.1       gwr {
    149   1.1       gwr 	struct confargs *ca = args;
    150  1.11       gwr 	int paddr;
    151   1.2       gwr 
    152  1.11       gwr 	/* XXX: Huge hack due to lack of probe info... */
    153  1.11       gwr 	/* XXX: Machines that might have a cg4 (gag). */
    154  1.11       gwr 	/* XXX: Need info on the "P4" register... */
    155   1.2       gwr 	switch (cpu_machine_id) {
    156  1.11       gwr 
    157  1.11       gwr 	case SUN3_MACH_110:
    158  1.11       gwr 		/* XXX: Assume type A. */
    159  1.11       gwr 		if (ca->ca_paddr == -1)
    160  1.11       gwr 			ca->ca_paddr = CG4A_DEF_BASE;
    161  1.11       gwr 		if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
    162  1.11       gwr 			return (0);
    163  1.11       gwr 		if (bus_peek(BUS_OBIO, CG4A_OBIO_CMAP, 1) == -1)
    164  1.11       gwr 			return (0);
    165  1.11       gwr 		break;
    166  1.11       gwr 
    167   1.2       gwr 	case SUN3_MACH_60:
    168  1.11       gwr 		/* XXX: Assume type A. */
    169  1.11       gwr 		if (ca->ca_paddr == -1)
    170  1.11       gwr 			ca->ca_paddr = CG4B_DEF_BASE;
    171  1.11       gwr 		paddr = ca->ca_paddr;
    172  1.11       gwr 		if (bus_peek(ca->ca_bustype, paddr, 1) == -1)
    173  1.11       gwr 			return (0);
    174  1.11       gwr         /* Make sure we're color */
    175  1.11       gwr 		paddr += CG4B_OFF_PIXMAP;
    176  1.11       gwr 		if (bus_peek(ca->ca_bustype, paddr, 1) == -1)
    177  1.11       gwr 			return (0);
    178   1.2       gwr 		break;
    179  1.11       gwr 
    180   1.2       gwr 	default:
    181   1.2       gwr 		return (0);
    182   1.2       gwr 	}
    183   1.1       gwr 
    184   1.3       gwr 	return (1);
    185   1.1       gwr }
    186   1.1       gwr 
    187   1.1       gwr /*
    188   1.1       gwr  * Attach a display.  We need to notice if it is the console, too.
    189   1.1       gwr  */
    190   1.1       gwr static void
    191   1.1       gwr cg4attach(parent, self, args)
    192   1.1       gwr 	struct device *parent, *self;
    193   1.1       gwr 	void *args;
    194   1.1       gwr {
    195   1.1       gwr 	struct cg4_softc *sc = (struct cg4_softc *)self;
    196   1.1       gwr 	struct fbdevice *fb = &sc->sc_fb;
    197   1.1       gwr 	struct confargs *ca = args;
    198   1.1       gwr 	struct fbtype *fbt;
    199   1.1       gwr 
    200  1.11       gwr 	/* XXX: should do better than this... */
    201  1.11       gwr 	switch (cpu_machine_id) {
    202  1.11       gwr 	case SUN3_MACH_110:
    203  1.11       gwr 		sc->sc_cg4type = CG4_TYPE_A;
    204  1.11       gwr 		break;
    205  1.11       gwr 	case SUN3_MACH_60:
    206  1.11       gwr 	default:
    207  1.11       gwr 		sc->sc_cg4type = CG4_TYPE_B;
    208  1.11       gwr 	}
    209  1.11       gwr 
    210  1.11       gwr 	fb->fb_driver = &cg4_fbdriver;
    211   1.1       gwr 	fb->fb_private = sc;
    212   1.1       gwr 	fb->fb_name = sc->sc_dev.dv_xname;
    213   1.1       gwr 
    214   1.1       gwr 	fbt = &fb->fb_fbtype;
    215   1.1       gwr 	fbt->fb_type = FBTYPE_SUN4COLOR;
    216   1.1       gwr 	fbt->fb_depth = 8;
    217   1.1       gwr 	fbt->fb_cmsize = 256;
    218   1.1       gwr 
    219   1.1       gwr 	fbt->fb_width = 1152;
    220   1.1       gwr 	fbt->fb_height = 900;
    221   1.2       gwr 	fbt->fb_size = CG4_MMAP_SIZE;
    222   1.1       gwr 
    223  1.11       gwr 	switch (sc->sc_cg4type) {
    224  1.11       gwr 	case CG4_TYPE_A:	/* Sun3/110 */
    225  1.11       gwr 		sc->sc_va_cmap = bus_mapin(BUS_OBIO, CG4A_OBIO_CMAP,
    226  1.11       gwr 		                           sizeof(struct amd_regs));
    227  1.11       gwr 		sc->sc_pa_overlay = ca->ca_paddr + CG4A_OFF_OVERLAY;
    228  1.11       gwr 		sc->sc_pa_enable  = ca->ca_paddr + CG4A_OFF_ENABLE;
    229  1.11       gwr 		sc->sc_pa_pixmap  = ca->ca_paddr + CG4A_OFF_PIXMAP;
    230  1.11       gwr 		sc->sc_btcm = NULL;
    231  1.11       gwr 		cg4a_init(sc);
    232  1.11       gwr 		break;
    233  1.11       gwr 
    234  1.11       gwr 	case CG4_TYPE_B:	/* Sun3/60 */
    235  1.11       gwr 	default:
    236  1.11       gwr 		sc->sc_va_cmap = (struct bt_regs *)
    237  1.11       gwr 			bus_mapin(ca->ca_bustype, ca->ca_paddr,
    238  1.11       gwr 					  sizeof(struct bt_regs *));
    239  1.11       gwr 		sc->sc_pa_overlay = ca->ca_paddr + CG4B_OFF_OVERLAY;
    240  1.11       gwr 		sc->sc_pa_enable  = ca->ca_paddr + CG4B_OFF_ENABLE;
    241  1.11       gwr 		sc->sc_pa_pixmap  = ca->ca_paddr + CG4B_OFF_PIXMAP;
    242  1.11       gwr 		sc->sc_btcm = malloc(sizeof(union bt_cmap), M_DEVBUF, M_WAITOK);
    243  1.11       gwr 		cg4b_init(sc);
    244  1.11       gwr 		break;
    245  1.11       gwr 	}
    246   1.1       gwr 
    247  1.10  christos 	printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
    248   1.2       gwr 	fb_attach(fb, 4);
    249   1.1       gwr }
    250   1.1       gwr 
    251   1.1       gwr int
    252   1.1       gwr cg4open(dev, flags, mode, p)
    253   1.1       gwr 	dev_t dev;
    254   1.1       gwr 	int flags, mode;
    255   1.1       gwr 	struct proc *p;
    256   1.1       gwr {
    257   1.1       gwr 	int unit = minor(dev);
    258   1.1       gwr 
    259   1.7   thorpej 	if (unit >= cgfour_cd.cd_ndevs || cgfour_cd.cd_devs[unit] == NULL)
    260   1.1       gwr 		return (ENXIO);
    261   1.1       gwr 	return (0);
    262   1.1       gwr }
    263   1.1       gwr 
    264   1.1       gwr int
    265   1.1       gwr cg4close(dev, flags, mode, p)
    266   1.1       gwr 	dev_t dev;
    267   1.1       gwr 	int flags, mode;
    268   1.1       gwr 	struct proc *p;
    269   1.1       gwr {
    270   1.1       gwr 
    271   1.1       gwr 	return (0);
    272   1.1       gwr }
    273   1.1       gwr 
    274   1.1       gwr int
    275   1.1       gwr cg4ioctl(dev, cmd, data, flags, p)
    276   1.1       gwr 	dev_t dev;
    277   1.1       gwr 	u_long cmd;
    278   1.1       gwr 	caddr_t data;
    279   1.1       gwr 	int flags;
    280   1.1       gwr 	struct proc *p;
    281   1.1       gwr {
    282   1.7   thorpej 	struct cg4_softc *sc = cgfour_cd.cd_devs[minor(dev)];
    283   1.1       gwr 
    284   1.1       gwr 	return (fbioctlfb(&sc->sc_fb, cmd, data));
    285   1.1       gwr }
    286   1.1       gwr 
    287   1.1       gwr /*
    288   1.1       gwr  * Return the address that would map the given device at the given
    289   1.1       gwr  * offset, allowing for the given protection, or return -1 for error.
    290   1.1       gwr  *
    291   1.1       gwr  * X11 expects its mmap'd region to look like this:
    292  1.11       gwr  * 	128k overlay data memory
    293  1.11       gwr  * 	128k overlay enable bitmap
    294   1.1       gwr  * 	1024k color memory
    295   1.1       gwr  *
    296   1.1       gwr  * The hardware really looks like this (starting at ca_paddr)
    297   1.1       gwr  *  4 bytes Brooktree DAC registers
    298   1.1       gwr  *  2MB-4 gap
    299   1.1       gwr  * 	128k overlay memory
    300   1.1       gwr  * 	1920k gap
    301   1.1       gwr  * 	128k overlay-enable bitmap
    302   1.1       gwr  * 	1920k gap
    303   1.1       gwr  * 	1024k color memory
    304   1.1       gwr  */
    305   1.1       gwr int
    306   1.4   mycroft cg4mmap(dev, off, prot)
    307   1.1       gwr 	dev_t dev;
    308   1.2       gwr 	register int off;
    309   1.2       gwr 	int prot;
    310   1.1       gwr {
    311   1.7   thorpej 	struct cg4_softc *sc = cgfour_cd.cd_devs[minor(dev)];
    312   1.2       gwr 	register int physbase;
    313   1.1       gwr 
    314   1.1       gwr 	if (off & PGOFSET)
    315   1.5   mycroft 		panic("cg4mmap");
    316   1.1       gwr 
    317   1.2       gwr 	if ((unsigned)off >= CG4_MMAP_SIZE)
    318   1.2       gwr 		return (-1);
    319   1.2       gwr 
    320   1.2       gwr 	if (off < 0x40000) {
    321   1.2       gwr 		if (off < 0x20000) {
    322  1.11       gwr 			physbase = sc->sc_pa_overlay;
    323   1.2       gwr 		} else {
    324   1.2       gwr 			/* enable plane */
    325   1.2       gwr 			off -= 0x20000;
    326  1.11       gwr 			physbase = sc->sc_pa_enable;
    327   1.2       gwr 		}
    328   1.2       gwr 	} else {
    329   1.2       gwr 		/* pixel map */
    330   1.2       gwr 		off -= 0x40000;
    331  1.11       gwr 		physbase = sc->sc_pa_pixmap;
    332   1.1       gwr 	}
    333   1.1       gwr 
    334   1.1       gwr 	/*
    335   1.1       gwr 	 * I turned on PMAP_NC here to disable the cache as I was
    336   1.1       gwr 	 * getting horribly broken behaviour with it on.
    337   1.1       gwr 	 */
    338   1.2       gwr 	return ((physbase + off) | PMAP_NC);
    339   1.1       gwr }
    340   1.1       gwr 
    341   1.1       gwr /*
    342   1.1       gwr  * Internal ioctl functions.
    343   1.1       gwr  */
    344   1.1       gwr 
    345   1.1       gwr /* FBIOGATTR: */
    346   1.1       gwr static int  cg4gattr(fb, fba)
    347   1.1       gwr 	struct fbdevice *fb;
    348   1.1       gwr 	struct fbgattr *fba;
    349   1.1       gwr {
    350   1.1       gwr 
    351   1.1       gwr 	fba->real_type = fb->fb_fbtype.fb_type;
    352   1.1       gwr 	fba->owner = 0;		/* XXX - TIOCCONS stuff? */
    353   1.1       gwr 	fba->fbtype = fb->fb_fbtype;
    354   1.1       gwr 	fba->sattr.flags = 0;
    355   1.1       gwr 	fba->sattr.emu_type = fb->fb_fbtype.fb_type;
    356   1.1       gwr 	fba->sattr.dev_specific[0] = -1;
    357   1.1       gwr 	fba->emu_types[0] = fb->fb_fbtype.fb_type;
    358   1.1       gwr 	fba->emu_types[1] = -1;
    359   1.1       gwr 	return (0);
    360   1.1       gwr }
    361   1.1       gwr 
    362   1.1       gwr /* FBIOGVIDEO: */
    363   1.1       gwr static int  cg4gvideo(fb, on)
    364   1.1       gwr 	struct fbdevice *fb;
    365   1.1       gwr 	int *on;
    366   1.1       gwr {
    367   1.1       gwr 	struct cg4_softc *sc = fb->fb_private;
    368   1.1       gwr 
    369   1.1       gwr 	*on = !sc->sc_blanked;
    370   1.1       gwr 	return (0);
    371   1.1       gwr }
    372   1.1       gwr 
    373   1.1       gwr /* FBIOSVIDEO: */
    374   1.1       gwr static int cg4svideo(fb, on)
    375   1.1       gwr 	struct fbdevice *fb;
    376   1.1       gwr 	int *on;
    377   1.1       gwr {
    378   1.1       gwr 	struct cg4_softc *sc = fb->fb_private;
    379  1.11       gwr 	int state;
    380  1.11       gwr 
    381  1.11       gwr 	state = *on;
    382  1.11       gwr 	if (sc->sc_cg4type == CG4_TYPE_A)
    383  1.11       gwr 		cg4a_svideo(sc, state);
    384  1.11       gwr 	else
    385  1.11       gwr 		cg4b_svideo(sc, state);
    386  1.11       gwr 	return (0);
    387  1.11       gwr }
    388  1.11       gwr 
    389  1.11       gwr /*
    390  1.11       gwr  * FBIOGETCMAP:
    391  1.11       gwr  * Copy current colormap out to user space.
    392  1.11       gwr  */
    393  1.11       gwr static int cg4getcmap(fb, fbcm)
    394  1.11       gwr 	struct fbdevice *fb;
    395  1.11       gwr 	struct fbcmap *fbcm;
    396  1.11       gwr {
    397  1.11       gwr 	struct cg4_softc *sc = fb->fb_private;
    398  1.11       gwr 	struct soft_cmap *cm = &sc->sc_cmap;
    399  1.11       gwr 	int error, start, count;
    400  1.11       gwr 
    401  1.11       gwr 	start = fbcm->index;
    402  1.11       gwr 	count = fbcm->count;
    403  1.11       gwr 	if ((start < 0) || (start >= CMAP_SIZE) ||
    404  1.11       gwr 	    (count < 0) || (start + count > CMAP_SIZE) )
    405  1.11       gwr 		return (EINVAL);
    406  1.11       gwr 
    407  1.11       gwr 	if ((error = copyout(&cm->r[start], fbcm->red, count)) != 0)
    408  1.11       gwr 		return (error);
    409  1.11       gwr 
    410  1.11       gwr 	if ((error = copyout(&cm->g[start], fbcm->green, count)) != 0)
    411  1.11       gwr 		return (error);
    412  1.11       gwr 
    413  1.11       gwr 	if ((error = copyout(&cm->b[start], fbcm->blue, count)) != 0)
    414  1.11       gwr 		return (error);
    415  1.11       gwr 
    416  1.11       gwr 	return (0);
    417  1.11       gwr }
    418  1.11       gwr 
    419  1.11       gwr /*
    420  1.11       gwr  * FBIOPUTCMAP:
    421  1.11       gwr  * Copy new colormap from user space and load.
    422  1.11       gwr  */
    423  1.11       gwr static int cg4putcmap(fb, fbcm)
    424  1.11       gwr 	struct fbdevice *fb;
    425  1.11       gwr 	struct fbcmap *fbcm;
    426  1.11       gwr {
    427  1.11       gwr 	struct cg4_softc *sc = fb->fb_private;
    428  1.11       gwr 	struct soft_cmap *cm = &sc->sc_cmap;
    429  1.11       gwr 	int error, start, count;
    430  1.11       gwr 
    431  1.11       gwr 	start = fbcm->index;
    432  1.11       gwr 	count = fbcm->count;
    433  1.11       gwr 	if ((start < 0) || (start >= CMAP_SIZE) ||
    434  1.11       gwr 	    (count < 0) || (start + count > CMAP_SIZE) )
    435  1.11       gwr 		return (EINVAL);
    436  1.11       gwr 
    437  1.11       gwr 	if ((error = copyin(fbcm->red, &cm->r[start], count)) != 0)
    438  1.11       gwr 		return (error);
    439  1.11       gwr 
    440  1.11       gwr 	if ((error = copyin(fbcm->green, &cm->g[start], count)) != 0)
    441  1.11       gwr 		return (error);
    442  1.11       gwr 
    443  1.11       gwr 	if ((error = copyin(fbcm->blue, &cm->b[start], count)) != 0)
    444  1.11       gwr 		return (error);
    445  1.11       gwr 
    446  1.11       gwr 	if (sc->sc_cg4type == CG4_TYPE_A)
    447  1.11       gwr 		cg4a_ldcmap(sc);
    448  1.11       gwr 	else
    449  1.11       gwr 		cg4b_ldcmap(sc);
    450   1.1       gwr 
    451  1.11       gwr 	return (0);
    452  1.11       gwr }
    453  1.11       gwr 
    454  1.11       gwr /****************************************************************
    455  1.11       gwr  * Routines for the "Type A" hardware
    456  1.11       gwr  ****************************************************************/
    457  1.11       gwr 
    458  1.11       gwr static void
    459  1.11       gwr cg4a_init(sc)
    460  1.11       gwr 	struct cg4_softc *sc;
    461  1.11       gwr {
    462  1.11       gwr 	volatile struct amd_regs *ar = sc->sc_va_cmap;
    463  1.11       gwr 	struct soft_cmap *cm = &sc->sc_cmap;
    464  1.11       gwr 	int i;
    465  1.11       gwr 
    466  1.11       gwr 	/* grab initial (current) color map */
    467  1.11       gwr 	for(i = 0; i < 256; i++) {
    468  1.11       gwr 		cm->r[i] = ar->r[i];
    469  1.11       gwr 		cm->g[i] = ar->g[i];
    470  1.11       gwr 		cm->b[i] = ar->b[i];
    471  1.11       gwr 	}
    472  1.11       gwr }
    473  1.11       gwr 
    474  1.11       gwr static void
    475  1.11       gwr cg4a_ldcmap(sc)
    476  1.11       gwr 	struct cg4_softc *sc;
    477  1.11       gwr {
    478  1.11       gwr 	volatile struct amd_regs *ar = sc->sc_va_cmap;
    479  1.11       gwr 	struct soft_cmap *cm = &sc->sc_cmap;
    480  1.11       gwr 	int i;
    481  1.11       gwr 
    482  1.11       gwr 	/*
    483  1.11       gwr 	 * Now blast them into the chip!
    484  1.11       gwr 	 * XXX Should use retrace interrupt!
    485  1.11       gwr 	 * Just set a "need load" bit and let the
    486  1.11       gwr 	 * retrace interrupt handler do the work.
    487  1.11       gwr 	 */
    488  1.11       gwr 	for(i = 0; i < 256; i++) {
    489  1.11       gwr 		ar->r[i] = cm->r[i];
    490  1.11       gwr 		ar->g[i] = cm->g[i];
    491  1.11       gwr 		ar->b[i] = cm->b[i];
    492  1.11       gwr 	}
    493  1.11       gwr }
    494  1.11       gwr 
    495  1.11       gwr static void
    496  1.11       gwr cg4a_svideo(sc, on)
    497  1.11       gwr 	struct cg4_softc *sc;
    498  1.11       gwr 	int on;
    499  1.11       gwr {
    500  1.11       gwr 	volatile struct amd_regs *ar = sc->sc_va_cmap;
    501  1.11       gwr 	int i;
    502   1.1       gwr 
    503  1.11       gwr 	if ((on == 0) && (sc->sc_blanked == 0)) {
    504  1.11       gwr 		/* Turn OFF video (make it blank). */
    505   1.1       gwr 		sc->sc_blanked = 1;
    506  1.11       gwr 		/* Load fake "all zero" colormap. */
    507  1.11       gwr 		for (i = 0; i < 256; i++) {
    508  1.11       gwr 			ar->r[i] = 0;
    509  1.11       gwr 			ar->g[i] = 0;
    510  1.11       gwr 			ar->b[i] = 0;
    511  1.11       gwr 		}
    512   1.1       gwr 	}
    513   1.1       gwr 
    514  1.11       gwr 	if ((on != 0) && (sc->sc_blanked != 0)) {
    515   1.1       gwr 		/* Turn video back ON (unblank). */
    516   1.1       gwr 		sc->sc_blanked = 0;
    517  1.11       gwr 		/* Restore normal colormap. */
    518  1.11       gwr 		cg4a_ldcmap(sc);
    519  1.11       gwr 	}
    520  1.11       gwr }
    521  1.11       gwr 
    522  1.11       gwr 
    523  1.11       gwr /****************************************************************
    524  1.11       gwr  * Routines for the "Type B" hardware
    525  1.11       gwr  ****************************************************************/
    526   1.1       gwr 
    527  1.11       gwr static void
    528  1.11       gwr cg4b_init(sc)
    529  1.11       gwr 	struct cg4_softc *sc;
    530  1.11       gwr {
    531  1.11       gwr 	volatile struct bt_regs *bt = sc->sc_va_cmap;
    532  1.11       gwr 	struct soft_cmap *cm = &sc->sc_cmap;
    533  1.11       gwr 	union bt_cmap *btcm = sc->sc_btcm;
    534  1.11       gwr 	int i;
    535  1.11       gwr 
    536  1.11       gwr 	/*
    537  1.11       gwr 	 * BT458 chip initialization as described in Brooktree's
    538  1.11       gwr 	 * 1993 Graphics and Imaging Product Databook (DB004-1/93).
    539  1.11       gwr 	 */
    540  1.11       gwr 	bt->bt_addr = 0x04;	/* select read mask register */
    541  1.11       gwr 	bt->bt_ctrl = 0xff;	/* all planes on */
    542  1.11       gwr 	bt->bt_addr = 0x05;	/* select blink mask register */
    543  1.11       gwr 	bt->bt_ctrl = 0x00;	/* all planes non-blinking */
    544  1.11       gwr 	bt->bt_addr = 0x06;	/* select command register */
    545  1.11       gwr 	bt->bt_ctrl = 0x43;	/* palette enabled, overlay planes enabled */
    546  1.11       gwr 	bt->bt_addr = 0x07;	/* select test register */
    547  1.11       gwr 	bt->bt_ctrl = 0x00;	/* set test mode */
    548  1.11       gwr 
    549  1.11       gwr 	/* grab initial (current) color map */
    550  1.11       gwr 	bt->bt_addr = 0;
    551  1.11       gwr 	for (i = 0; i < (256 * 3 / 4); i++) {
    552  1.11       gwr 		btcm->cm_chip[i] = bt->bt_cmap;
    553  1.11       gwr 	}
    554   1.1       gwr 
    555  1.11       gwr 	/* Transpose into S/W form. */
    556  1.11       gwr 	for (i = 0; i < 256; i++) {
    557  1.11       gwr 		cm->r[i] = btcm->cm_map[i][0];
    558  1.11       gwr 		cm->g[i] = btcm->cm_map[i][1];
    559  1.11       gwr 		cm->b[i] = btcm->cm_map[i][2];
    560   1.1       gwr 	}
    561   1.1       gwr }
    562   1.1       gwr 
    563  1.11       gwr static void
    564  1.11       gwr cg4b_ldcmap(sc)
    565  1.11       gwr 	struct cg4_softc *sc;
    566   1.1       gwr {
    567  1.11       gwr 	volatile struct bt_regs *bt = sc->sc_va_cmap;
    568  1.11       gwr 	struct soft_cmap *cm = &sc->sc_cmap;
    569  1.11       gwr 	union bt_cmap *btcm = sc->sc_btcm;
    570  1.11       gwr 	int i;
    571   1.1       gwr 
    572  1.11       gwr 	/*
    573  1.11       gwr 	 * Now blast them into the chip!
    574  1.11       gwr 	 * XXX Should use retrace interrupt!
    575  1.11       gwr 	 * Just set a "need load" bit and let the
    576  1.11       gwr 	 * retrace interrupt handler do the work.
    577  1.11       gwr 	 */
    578   1.1       gwr 
    579  1.11       gwr 	/* Transpose into H/W form. */
    580  1.11       gwr 	for (i = 0; i < 256; i++) {
    581  1.11       gwr 		btcm->cm_map[i][0] = cm->r[i];
    582  1.11       gwr 		btcm->cm_map[i][1] = cm->g[i];
    583  1.11       gwr 		btcm->cm_map[i][2] = cm->b[i];
    584  1.11       gwr 	}
    585   1.1       gwr 
    586  1.11       gwr 	bt->bt_addr = 0;
    587  1.11       gwr 	for (i = 0; i < (256 * 3 / 4); i++) {
    588  1.11       gwr 		bt->bt_cmap = btcm->cm_chip[i];
    589   1.1       gwr 	}
    590   1.1       gwr }
    591   1.1       gwr 
    592   1.1       gwr static void
    593  1.11       gwr cg4b_svideo(sc, on)
    594   1.1       gwr 	struct cg4_softc *sc;
    595  1.11       gwr 	int on;
    596   1.1       gwr {
    597  1.11       gwr 	volatile struct bt_regs *bt = sc->sc_va_cmap;
    598  1.11       gwr 	int i;
    599  1.11       gwr 
    600  1.11       gwr 	if ((on == 0) && (sc->sc_blanked == 0)) {
    601  1.11       gwr 		/* Turn OFF video (make it blank). */
    602  1.11       gwr 		sc->sc_blanked = 1;
    603  1.11       gwr 		/* Load fake "all zero" colormap. */
    604  1.11       gwr 		bt->bt_addr = 0;
    605  1.11       gwr 		for (i = 0; i < (256 * 3 / 4); i++)
    606  1.11       gwr 			bt->bt_cmap = 0;
    607  1.11       gwr 	}
    608  1.11       gwr 
    609  1.11       gwr 	if ((on != 0) && (sc->sc_blanked != 0)) {
    610  1.11       gwr 		/* Turn video back ON (unblank). */
    611  1.11       gwr 		sc->sc_blanked = 0;
    612  1.11       gwr 		/* Restore normal colormap. */
    613  1.11       gwr 		cg4b_ldcmap(sc);
    614  1.11       gwr 	}
    615   1.1       gwr }
    616  1.11       gwr 
    617