Home | History | Annotate | Line # | Download | only in sbus
agten.c revision 1.3.2.1
      1  1.3.2.1      matt /*	$NetBSD: agten.c,v 1.3.2.1 2007/11/06 23:30:04 matt Exp $ */
      2      1.1  macallan 
      3      1.1  macallan /*-
      4      1.1  macallan  * Copyright (c) 2007 Michael Lorenz
      5      1.1  macallan  * All rights reserved.
      6      1.1  macallan  *
      7      1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8      1.1  macallan  * modification, are permitted provided that the following conditions
      9      1.1  macallan  * are met:
     10      1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11      1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12      1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15      1.1  macallan  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16      1.1  macallan  *    contributors may be used to endorse or promote products derived
     17      1.1  macallan  *    from this software without specific prior written permission.
     18      1.1  macallan  *
     19      1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  macallan  */
     31      1.1  macallan 
     32      1.1  macallan #include <sys/cdefs.h>
     33  1.3.2.1      matt __KERNEL_RCSID(0, "$NetBSD: agten.c,v 1.3.2.1 2007/11/06 23:30:04 matt Exp $");
     34      1.1  macallan 
     35      1.1  macallan /*
     36      1.1  macallan  * a driver for the Fujitsu AG-10e SBus framebuffer
     37      1.1  macallan  *
     38      1.1  macallan  * this thing is Frankenstein's Monster among graphics boards.
     39      1.1  macallan  * it contains three graphics chips:
     40      1.1  macallan  * a GLint - 24bit stuff, double-buffered
     41      1.1  macallan  * an Imagine 128 which provides an 8bit overlay
     42      1.1  macallan  * a Weitek P9100 which provides WIDs
     43      1.1  macallan  * so here we need to mess only with the P9100 and the I128 - for X we just
     44      1.1  macallan  * hide the overlay and let the Xserver mess with the GLint
     45      1.1  macallan  */
     46      1.1  macallan 
     47      1.1  macallan #include <sys/param.h>
     48      1.1  macallan #include <sys/systm.h>
     49      1.1  macallan #include <sys/kernel.h>
     50      1.1  macallan #include <sys/device.h>
     51      1.1  macallan #include <sys/proc.h>
     52      1.1  macallan #include <sys/mutex.h>
     53      1.1  macallan #include <sys/ioctl.h>
     54      1.1  macallan #include <sys/kernel.h>
     55      1.1  macallan #include <sys/systm.h>
     56  1.3.2.1      matt #include <sys/conf.h>
     57      1.1  macallan 
     58      1.1  macallan #include <dev/sun/fbio.h>
     59      1.1  macallan #include <dev/sun/fbvar.h>
     60      1.1  macallan #include <dev/sun/btreg.h>
     61      1.1  macallan #include <dev/sun/btvar.h>
     62      1.1  macallan 
     63  1.3.2.1      matt #include <sys/bus.h>
     64      1.1  macallan #include <machine/autoconf.h>
     65      1.1  macallan 
     66      1.1  macallan #include <dev/sbus/sbusvar.h>
     67      1.1  macallan 
     68      1.1  macallan #include <dev/wscons/wsconsio.h>
     69      1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     70      1.1  macallan #include <dev/rasops/rasops.h>
     71      1.1  macallan #include <dev/wsfont/wsfont.h>
     72      1.1  macallan 
     73      1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     74      1.1  macallan 
     75      1.1  macallan #include <dev/sbus/p9100reg.h>
     76      1.1  macallan #include <dev/ic/ibm561reg.h>
     77      1.1  macallan #include <dev/ic/i128reg.h>
     78      1.1  macallan #include <dev/ic/i128var.h>
     79      1.1  macallan 
     80      1.1  macallan #include "opt_agten.h"
     81      1.1  macallan 
     82      1.1  macallan static int	agten_match(struct device *, struct cfdata *, void *);
     83      1.1  macallan static void	agten_attach(struct device *, struct device *, void *);
     84      1.1  macallan 
     85      1.1  macallan static int	agten_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     86      1.1  macallan static paddr_t	agten_mmap(void *, void *, off_t, int);
     87      1.1  macallan static void	agten_init_screen(void *, struct vcons_screen *, int, long *);
     88      1.1  macallan 
     89      1.1  macallan struct agten_softc {
     90      1.1  macallan 	struct device	sc_dev;		/* base device */
     91      1.1  macallan 	struct sbusdev	sc_sd;		/* sbus device */
     92      1.1  macallan 	struct fbdevice	sc_fb;		/* frame buffer device */
     93      1.1  macallan 
     94      1.1  macallan 	struct vcons_screen sc_console_screen;
     95      1.1  macallan 	struct wsscreen_descr sc_defaultscreen_descr;
     96      1.1  macallan 	const struct wsscreen_descr *sc_screens[1];
     97      1.1  macallan 	struct wsscreen_list sc_screenlist;
     98      1.1  macallan 
     99      1.1  macallan 	bus_space_tag_t	sc_bustag;
    100      1.1  macallan 
    101      1.1  macallan 	bus_space_handle_t 	sc_i128_fbh;
    102      1.1  macallan 	bus_size_t		sc_i128_fbsz;
    103      1.1  macallan 	bus_space_handle_t 	sc_i128_regh;
    104      1.1  macallan 	bus_space_handle_t 	sc_p9100_regh;
    105      1.3  macallan 	bus_addr_t		sc_glint_fb;
    106      1.3  macallan 	bus_addr_t		sc_glint_regs;
    107      1.3  macallan 	uint32_t		sc_glint_fbsz;
    108      1.1  macallan 
    109      1.1  macallan 	uint32_t	sc_width;
    110      1.1  macallan 	uint32_t	sc_height;	/* panel width / height */
    111      1.1  macallan 	uint32_t	sc_stride;
    112      1.1  macallan 	uint32_t	sc_depth;
    113  1.3.2.1      matt 
    114  1.3.2.1      matt 	int sc_cursor_x;
    115  1.3.2.1      matt 	int sc_cursor_y;
    116  1.3.2.1      matt 	int sc_video;			/* video output enabled */
    117  1.3.2.1      matt 
    118  1.3.2.1      matt 	/* some /dev/fb* stuff */
    119  1.3.2.1      matt 	int sc_fb_is_open;
    120  1.3.2.1      matt 
    121      1.1  macallan 	union	bt_cmap sc_cmap;	/* Brooktree color map */
    122      1.1  macallan 
    123      1.1  macallan 	int sc_mode;
    124      1.1  macallan 	uint32_t sc_bg;
    125      1.1  macallan 	struct vcons_data vd;
    126      1.1  macallan };
    127      1.1  macallan 
    128      1.1  macallan CFATTACH_DECL(agten, sizeof(struct agten_softc),
    129      1.1  macallan     agten_match, agten_attach, NULL, NULL);
    130      1.1  macallan 
    131      1.1  macallan 
    132      1.1  macallan static int	agten_putcmap(struct agten_softc *, struct wsdisplay_cmap *);
    133      1.1  macallan static int 	agten_getcmap(struct agten_softc *, struct wsdisplay_cmap *);
    134      1.1  macallan static int 	agten_putpalreg(struct agten_softc *, uint8_t, uint8_t,
    135      1.1  macallan 			    uint8_t, uint8_t);
    136      1.1  macallan static void	agten_init(struct agten_softc *);
    137      1.3  macallan static void	agten_gfx(struct agten_softc *);
    138  1.3.2.1      matt static void	agten_set_video(struct agten_softc *, int);
    139  1.3.2.1      matt static int	agten_get_video(struct agten_softc *);
    140      1.1  macallan 
    141      1.1  macallan static void	agten_copycols(void *, int, int, int, int);
    142      1.1  macallan static void	agten_erasecols(void *, int, int, int, long);
    143      1.1  macallan static void	agten_copyrows(void *, int, int, int);
    144      1.1  macallan static void	agten_eraserows(void *, int, int, long);
    145      1.1  macallan 
    146  1.3.2.1      matt static void	agten_move_cursor(struct agten_softc *, int, int);
    147  1.3.2.1      matt static int	agten_do_cursor(struct agten_softc *sc,
    148  1.3.2.1      matt 				struct wsdisplay_cursor *);
    149  1.3.2.1      matt static int	agten_do_sun_cursor(struct agten_softc *sc,
    150  1.3.2.1      matt 				struct fbcursor *);
    151  1.3.2.1      matt 
    152  1.3.2.1      matt static uint16_t util_interleave(uint8_t, uint8_t);
    153  1.3.2.1      matt static uint16_t util_interleave_lin(uint8_t, uint8_t);
    154  1.3.2.1      matt 
    155      1.1  macallan extern const u_char rasops_cmap[768];
    156      1.1  macallan 
    157      1.1  macallan struct wsdisplay_accessops agten_accessops = {
    158      1.1  macallan 	agten_ioctl,
    159      1.1  macallan 	agten_mmap,
    160      1.1  macallan 	NULL,	/* alloc_screen */
    161      1.1  macallan 	NULL,	/* free_screen */
    162      1.1  macallan 	NULL,	/* show_screen */
    163      1.1  macallan 	NULL, 	/* load_font */
    164      1.1  macallan 	NULL,	/* pollc */
    165      1.1  macallan 	NULL	/* scroll */
    166      1.1  macallan };
    167      1.1  macallan 
    168  1.3.2.1      matt /* /dev/fb* stuff */
    169  1.3.2.1      matt extern struct cfdriver agten_cd;
    170  1.3.2.1      matt 
    171  1.3.2.1      matt static int agten_fb_open(dev_t, int, int, struct lwp *);
    172  1.3.2.1      matt static int agten_fb_close(dev_t, int, int, struct lwp *);
    173  1.3.2.1      matt static int agten_fb_ioctl(dev_t, u_long, void *, int, struct lwp *);
    174  1.3.2.1      matt static paddr_t agten_fb_mmap(dev_t, off_t, int);
    175  1.3.2.1      matt static void agten_fb_unblank(struct device *);
    176  1.3.2.1      matt 
    177  1.3.2.1      matt static struct fbdriver agtenfbdriver = {
    178  1.3.2.1      matt 	agten_fb_unblank, agten_fb_open, agten_fb_close, agten_fb_ioctl,
    179  1.3.2.1      matt 	nopoll, agten_fb_mmap, nokqfilter
    180  1.3.2.1      matt };
    181  1.3.2.1      matt 
    182      1.1  macallan static inline void
    183      1.1  macallan agten_write_dac(struct agten_softc *sc, int reg, uint8_t val)
    184      1.1  macallan {
    185      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh,
    186      1.1  macallan 	    0x200 + (reg << 2), (uint32_t)val << 16);
    187      1.1  macallan }
    188      1.1  macallan 
    189      1.1  macallan static inline void
    190      1.1  macallan agten_write_idx(struct agten_softc *sc, int offset)
    191      1.1  macallan {
    192      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh,
    193      1.1  macallan 	    0x200 + (IBM561_ADDR_LOW << 2), (offset & 0xff) << 16);
    194      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh,
    195      1.1  macallan 	    0x200 + (IBM561_ADDR_HIGH << 2), ((offset >> 8) & 0xff) << 16);
    196      1.1  macallan }
    197      1.1  macallan 
    198      1.1  macallan static inline void
    199      1.1  macallan agten_write_dac_10(struct agten_softc *sc, int reg, uint16_t val)
    200      1.1  macallan {
    201      1.1  macallan 	agten_write_dac(sc, reg, (val >> 2) & 0xff);
    202      1.1  macallan 	agten_write_dac(sc, reg, (val & 0x3) << 6);
    203      1.1  macallan }
    204      1.1  macallan 
    205      1.1  macallan static int
    206      1.1  macallan agten_match(struct device *dev, struct cfdata *cf, void *aux)
    207      1.1  macallan {
    208      1.1  macallan 	struct sbus_attach_args *sa = aux;
    209      1.1  macallan 
    210      1.1  macallan 	if (strcmp("PFU,aga", sa->sa_name) == 0)
    211      1.1  macallan 		return 100;
    212      1.1  macallan 	return 0;
    213      1.1  macallan }
    214      1.1  macallan 
    215      1.1  macallan static void
    216      1.1  macallan agten_attach(struct device *parent, struct device *dev, void *aux)
    217      1.1  macallan {
    218      1.1  macallan 	struct agten_softc *sc = (struct agten_softc *)dev;
    219      1.1  macallan 	struct sbus_attach_args *sa = aux;
    220      1.1  macallan 	struct fbdevice *fb = &sc->sc_fb;
    221      1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    222      1.1  macallan 	struct rasops_info *ri;
    223      1.1  macallan 	long defattr;
    224      1.1  macallan 	uint32_t reg;
    225      1.1  macallan 	int node = sa->sa_node;
    226      1.1  macallan 	int console;
    227      1.1  macallan 
    228      1.1  macallan 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    229      1.1  macallan 		"default",
    230      1.1  macallan 		0, 0,
    231      1.1  macallan 		NULL,
    232      1.1  macallan 		8, 16,
    233      1.1  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    234      1.1  macallan 		NULL
    235      1.1  macallan 	};
    236      1.1  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    237      1.1  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    238      1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    239  1.3.2.1      matt 	sc->sc_fb_is_open = 0;
    240  1.3.2.1      matt 	sc->sc_video = -1;
    241      1.1  macallan 	sc->sc_bustag = sa->sa_bustag;
    242      1.1  macallan 
    243      1.1  macallan 	reg = prom_getpropint(node, "i128_fb_physaddr", -1);
    244      1.1  macallan 	sc->sc_i128_fbsz = prom_getpropint(node, "i128_fb_size", -1);
    245      1.1  macallan 	if (sbus_bus_map(sc->sc_bustag,
    246      1.1  macallan 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg,
    247      1.1  macallan 	    sc->sc_i128_fbsz, BUS_SPACE_MAP_LINEAR, &sc->sc_i128_fbh) != 0) {
    248      1.1  macallan 
    249      1.1  macallan 		aprint_error("%s: unable to map the framebuffer\n",
    250      1.1  macallan 		    dev->dv_xname);
    251      1.1  macallan 		return;
    252      1.1  macallan 	}
    253      1.1  macallan 	fb->fb_pixels = bus_space_vaddr(sc->sc_bustag, sc->sc_i128_fbh);
    254      1.1  macallan 
    255      1.3  macallan 	reg = prom_getpropint(node, "i128_reg_physaddr", -1);
    256      1.1  macallan 	if (sbus_bus_map(sc->sc_bustag,
    257      1.1  macallan 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg,
    258      1.3  macallan 	    0x10000, 0, &sc->sc_i128_regh) != 0) {
    259      1.1  macallan 
    260      1.3  macallan 		aprint_error("%s: unable to map I128 registers\n",
    261      1.1  macallan 		    dev->dv_xname);
    262      1.1  macallan 		return;
    263      1.1  macallan 	}
    264      1.1  macallan 
    265      1.3  macallan 	reg = prom_getpropint(node, "p9100_reg_physaddr", -1);
    266      1.1  macallan 	if (sbus_bus_map(sc->sc_bustag,
    267      1.1  macallan 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg,
    268      1.3  macallan 	    0x8000, 0, &sc->sc_p9100_regh) != 0) {
    269      1.1  macallan 
    270      1.3  macallan 		aprint_error("%s: unable to map P9100 registers\n",
    271      1.1  macallan 		    dev->dv_xname);
    272      1.1  macallan 		return;
    273      1.1  macallan 	}
    274      1.2  macallan 
    275      1.3  macallan 	reg = prom_getpropint(node, "glint_fb0_physaddr", -1);
    276      1.3  macallan 	sc->sc_glint_fb = sbus_bus_addr(sc->sc_bustag,
    277      1.3  macallan 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg);
    278  1.3.2.1      matt 	sc->sc_glint_fbsz = prom_getpropint(node, "glint_lb_size", -1);
    279      1.3  macallan 	reg = prom_getpropint(node, "glint_reg_physaddr", -1);
    280      1.3  macallan 	sc->sc_glint_regs = sbus_bus_addr(sc->sc_bustag,
    281      1.3  macallan 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg);
    282      1.3  macallan 
    283      1.1  macallan 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
    284  1.3.2.1      matt 
    285      1.1  macallan #if 0
    286      1.1  macallan 	bus_intr_establish(sc->sc_bustag, sa->sa_pri, IPL_BIO,
    287      1.1  macallan 	    agten_intr, sc);
    288      1.1  macallan #endif
    289      1.1  macallan 
    290  1.3.2.1      matt 	sc->sc_width = prom_getpropint(node, "ffb_width", 1152);
    291  1.3.2.1      matt 	sc->sc_height = prom_getpropint(node, "ffb_height", 900);
    292      1.1  macallan 	sc->sc_depth = prom_getpropint(node, "ffb_depth", 8);
    293      1.1  macallan 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    294      1.1  macallan 
    295  1.3.2.1      matt 	printf(": %dx%d\n", sc->sc_width, sc->sc_height);
    296      1.1  macallan 	agten_init(sc);
    297      1.1  macallan 
    298      1.1  macallan 	console = fb_is_console(node);
    299      1.1  macallan 
    300      1.1  macallan 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    301      1.1  macallan 	    &agten_accessops);
    302      1.1  macallan 	sc->vd.init_screen = agten_init_screen;
    303      1.1  macallan 
    304      1.1  macallan 	ri = &sc->sc_console_screen.scr_ri;
    305      1.1  macallan 
    306      1.1  macallan 	if (console) {
    307      1.1  macallan 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    308      1.1  macallan 		    &defattr);
    309      1.1  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    310      1.1  macallan 
    311      1.1  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    312      1.1  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    313      1.1  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    314      1.1  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    315      1.1  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    316      1.1  macallan 		    defattr);
    317      1.2  macallan 		i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, 0, 0,
    318      1.2  macallan 		    sc->sc_width, sc->sc_height,
    319      1.2  macallan 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    320      1.1  macallan 	} else {
    321      1.1  macallan 		/*
    322      1.1  macallan 		 * since we're not the console we can postpone the rest
    323      1.1  macallan 		 * until someone actually allocates a screen for us
    324      1.1  macallan 		 */
    325      1.1  macallan 	}
    326      1.1  macallan 
    327      1.1  macallan 	/* Initialize the default color map. */
    328      1.1  macallan 
    329      1.1  macallan 	aa.console = console;
    330      1.1  macallan 	aa.scrdata = &sc->sc_screenlist;
    331      1.1  macallan 	aa.accessops = &agten_accessops;
    332      1.1  macallan 	aa.accesscookie = &sc->vd;
    333      1.1  macallan 
    334      1.1  macallan 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    335  1.3.2.1      matt 
    336  1.3.2.1      matt 	fb->fb_driver = &agtenfbdriver;
    337  1.3.2.1      matt 	fb->fb_device = &sc->sc_dev;
    338  1.3.2.1      matt 	fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
    339  1.3.2.1      matt 	fb->fb_type.fb_type = FBTYPE_AG10E;
    340  1.3.2.1      matt 	fb->fb_type.fb_cmsize = 256;	/* doesn't matter, we're always 24bit */
    341  1.3.2.1      matt 	fb->fb_type.fb_size = sc->sc_glint_fbsz;
    342  1.3.2.1      matt 	fb->fb_type.fb_width = sc->sc_width;
    343  1.3.2.1      matt 	fb->fb_type.fb_height = sc->sc_height;
    344  1.3.2.1      matt 	fb->fb_type.fb_depth = 32;
    345  1.3.2.1      matt 	fb->fb_linebytes = sc->sc_stride;
    346  1.3.2.1      matt 	fb_attach(fb, console);
    347  1.3.2.1      matt 	agten_set_video(sc, 1);	/* make sure video's on */
    348      1.1  macallan }
    349      1.1  macallan 
    350      1.1  macallan static int
    351      1.1  macallan agten_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    352      1.1  macallan 	struct lwp *l)
    353      1.1  macallan {
    354      1.1  macallan 	struct vcons_data *vd = v;
    355      1.1  macallan 	struct agten_softc *sc = vd->cookie;
    356      1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    357      1.1  macallan 	struct vcons_screen *ms = vd->active;
    358      1.1  macallan 
    359      1.1  macallan 	switch (cmd) {
    360      1.1  macallan 
    361      1.3  macallan 		case WSDISPLAYIO_GTYPE:
    362      1.3  macallan 			*(u_int *)data = WSDISPLAY_TYPE_AG10;
    363      1.3  macallan 			return 0;
    364      1.3  macallan 
    365      1.1  macallan 		case WSDISPLAYIO_GINFO:
    366      1.1  macallan 			if (ms == NULL)
    367      1.1  macallan 				return ENODEV;
    368      1.1  macallan 			wdf = (void *)data;
    369      1.1  macallan 			wdf->height = ms->scr_ri.ri_height;
    370      1.1  macallan 			wdf->width = ms->scr_ri.ri_width;
    371      1.3  macallan 			wdf->depth = 32;
    372      1.1  macallan 			wdf->cmsize = 256;
    373      1.1  macallan 			return 0;
    374      1.1  macallan 
    375  1.3.2.1      matt 		case WSDISPLAYIO_GVIDEO:
    376  1.3.2.1      matt 			*(int *)data = sc->sc_video;
    377  1.3.2.1      matt 			return 0;
    378  1.3.2.1      matt 
    379  1.3.2.1      matt 		case WSDISPLAYIO_SVIDEO:
    380  1.3.2.1      matt 			agten_set_video(sc, *(int *)data);
    381  1.3.2.1      matt 			return 0;
    382  1.3.2.1      matt 
    383      1.1  macallan 		case WSDISPLAYIO_GETCMAP:
    384      1.1  macallan 			return agten_getcmap(sc,
    385      1.1  macallan 			    (struct wsdisplay_cmap *)data);
    386      1.1  macallan 
    387      1.1  macallan 		case WSDISPLAYIO_PUTCMAP:
    388      1.1  macallan 			return agten_putcmap(sc,
    389      1.1  macallan 			    (struct wsdisplay_cmap *)data);
    390      1.1  macallan 
    391      1.1  macallan 		case WSDISPLAYIO_LINEBYTES:
    392      1.3  macallan 			*(u_int *)data = sc->sc_stride << 2;
    393      1.1  macallan 			return 0;
    394      1.1  macallan 
    395      1.1  macallan 		case WSDISPLAYIO_SMODE:
    396      1.1  macallan 			{
    397      1.1  macallan 				int new_mode = *(int*)data;
    398      1.1  macallan 				if (new_mode != sc->sc_mode) {
    399      1.1  macallan 					sc->sc_mode = new_mode;
    400      1.1  macallan 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    401      1.2  macallan 						agten_init(sc);
    402      1.1  macallan 						vcons_redraw_screen(ms);
    403      1.3  macallan 					} else {
    404      1.3  macallan 						agten_gfx(sc);
    405      1.1  macallan 					}
    406      1.1  macallan 				}
    407      1.1  macallan 			}
    408      1.1  macallan 			return 0;
    409  1.3.2.1      matt 
    410  1.3.2.1      matt 		case WSDISPLAYIO_GCURPOS:
    411  1.3.2.1      matt 			{
    412  1.3.2.1      matt 				struct wsdisplay_curpos *cp = (void *)data;
    413  1.3.2.1      matt 
    414  1.3.2.1      matt 				cp->x = sc->sc_cursor_x;
    415  1.3.2.1      matt 				cp->y = sc->sc_cursor_y;
    416  1.3.2.1      matt 			}
    417  1.3.2.1      matt 			return 0;
    418  1.3.2.1      matt 
    419  1.3.2.1      matt 		case WSDISPLAYIO_SCURPOS:
    420  1.3.2.1      matt 			{
    421  1.3.2.1      matt 				struct wsdisplay_curpos *cp = (void *)data;
    422  1.3.2.1      matt 
    423  1.3.2.1      matt 				agten_move_cursor(sc, cp->x, cp->y);
    424  1.3.2.1      matt 			}
    425  1.3.2.1      matt 			return 0;
    426  1.3.2.1      matt 
    427  1.3.2.1      matt 		case WSDISPLAYIO_GCURMAX:
    428  1.3.2.1      matt 			{
    429  1.3.2.1      matt 				struct wsdisplay_curpos *cp = (void *)data;
    430  1.3.2.1      matt 
    431  1.3.2.1      matt 				cp->x = 64;
    432  1.3.2.1      matt 				cp->y = 64;
    433  1.3.2.1      matt 			}
    434  1.3.2.1      matt 			return 0;
    435  1.3.2.1      matt 
    436  1.3.2.1      matt 		case WSDISPLAYIO_SCURSOR:
    437  1.3.2.1      matt 			{
    438  1.3.2.1      matt 				struct wsdisplay_cursor *cursor = (void *)data;
    439  1.3.2.1      matt 
    440  1.3.2.1      matt 				return agten_do_cursor(sc, cursor);
    441  1.3.2.1      matt 			}
    442      1.1  macallan 	}
    443      1.1  macallan 	return EPASSTHROUGH;
    444      1.1  macallan }
    445      1.1  macallan 
    446      1.1  macallan static paddr_t
    447      1.1  macallan agten_mmap(void *v, void *vs, off_t offset, int prot)
    448      1.1  macallan {
    449      1.1  macallan 	struct vcons_data *vd = v;
    450      1.1  macallan 	struct agten_softc *sc = vd->cookie;
    451      1.1  macallan 
    452      1.3  macallan 	if (offset < sc->sc_glint_fbsz)
    453      1.3  macallan 		return bus_space_mmap(sc->sc_bustag, sc->sc_glint_fb, offset,
    454      1.1  macallan 		    prot, BUS_SPACE_MAP_LINEAR);
    455      1.1  macallan 	return -1;
    456      1.1  macallan }
    457      1.1  macallan 
    458      1.1  macallan static void
    459      1.1  macallan agten_init_screen(void *cookie, struct vcons_screen *scr,
    460      1.1  macallan     int existing, long *defattr)
    461      1.1  macallan {
    462      1.1  macallan 	struct agten_softc *sc = cookie;
    463      1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    464      1.1  macallan 
    465      1.1  macallan 	ri->ri_depth = sc->sc_depth;
    466      1.1  macallan 	ri->ri_width = sc->sc_width;
    467      1.1  macallan 	ri->ri_height = sc->sc_height;
    468      1.1  macallan 	ri->ri_stride = sc->sc_stride;
    469      1.1  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    470      1.1  macallan 
    471      1.1  macallan 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
    472      1.1  macallan 
    473      1.1  macallan 	if (existing) {
    474      1.1  macallan 		ri->ri_flg |= RI_CLEAR;
    475      1.1  macallan 	}
    476      1.1  macallan 
    477      1.1  macallan 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    478      1.1  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
    479      1.1  macallan 
    480      1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    481      1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    482      1.1  macallan 
    483      1.1  macallan 	ri->ri_hw = scr;
    484      1.1  macallan 	ri->ri_ops.copyrows  = agten_copyrows;
    485      1.1  macallan 	ri->ri_ops.eraserows = agten_eraserows;
    486      1.1  macallan 	ri->ri_ops.copycols  = agten_copycols;
    487      1.1  macallan 	ri->ri_ops.erasecols = agten_erasecols;
    488      1.1  macallan 
    489      1.1  macallan }
    490      1.1  macallan 
    491      1.1  macallan static int
    492      1.1  macallan agten_putcmap(struct agten_softc *sc, struct wsdisplay_cmap *cm)
    493      1.1  macallan {
    494      1.1  macallan 	u_int index = cm->index;
    495      1.1  macallan 	u_int count = cm->count;
    496      1.1  macallan 	int i, error;
    497      1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    498      1.1  macallan 	u_char *r, *g, *b;
    499      1.1  macallan 
    500      1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    501      1.1  macallan 	    (cm->index + cm->count) > 256)
    502      1.1  macallan 		return EINVAL;
    503      1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    504      1.1  macallan 	if (error)
    505      1.1  macallan 		return error;
    506      1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    507      1.1  macallan 	if (error)
    508      1.1  macallan 		return error;
    509      1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    510      1.1  macallan 	if (error)
    511      1.1  macallan 		return error;
    512      1.1  macallan 
    513      1.1  macallan 	r = &rbuf[index];
    514      1.1  macallan 	g = &gbuf[index];
    515      1.1  macallan 	b = &bbuf[index];
    516      1.1  macallan 
    517      1.1  macallan 	for (i = 0; i < count; i++) {
    518      1.1  macallan 		agten_putpalreg(sc, index, *r, *g, *b);
    519      1.1  macallan 		index++;
    520      1.1  macallan 		r++, g++, b++;
    521      1.1  macallan 	}
    522      1.1  macallan 	return 0;
    523      1.1  macallan }
    524      1.1  macallan 
    525      1.1  macallan static int
    526      1.1  macallan agten_getcmap(struct agten_softc *sc, struct wsdisplay_cmap *cm)
    527      1.1  macallan {
    528      1.1  macallan 	u_int index = cm->index;
    529      1.1  macallan 	u_int count = cm->count;
    530      1.1  macallan 	int error, i;
    531      1.1  macallan 	uint8_t red[256], green[256], blue[256];
    532      1.1  macallan 
    533      1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    534      1.1  macallan 		return EINVAL;
    535      1.1  macallan 
    536      1.1  macallan 	i = index;
    537      1.1  macallan 	while (i < (index + count)) {
    538      1.1  macallan 		red[i] = sc->sc_cmap.cm_map[i][0];
    539      1.1  macallan 		green[i] = sc->sc_cmap.cm_map[i][1];
    540      1.1  macallan 		blue[i] = sc->sc_cmap.cm_map[i][2];
    541      1.1  macallan 		i++;
    542      1.1  macallan 	}
    543      1.1  macallan 	error = copyout(&red[index],   cm->red,   count);
    544      1.1  macallan 	if (error)
    545      1.1  macallan 		return error;
    546      1.1  macallan 	error = copyout(&green[index], cm->green, count);
    547      1.1  macallan 	if (error)
    548      1.1  macallan 		return error;
    549      1.1  macallan 	error = copyout(&blue[index],  cm->blue,  count);
    550      1.1  macallan 	if (error)
    551      1.1  macallan 		return error;
    552      1.1  macallan 
    553      1.1  macallan 	return 0;
    554      1.1  macallan }
    555      1.1  macallan 
    556      1.1  macallan static int
    557      1.1  macallan agten_putpalreg(struct agten_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    558      1.1  macallan     uint8_t b)
    559      1.1  macallan {
    560      1.1  macallan 
    561      1.1  macallan 	sc->sc_cmap.cm_map[idx][0] = r;
    562      1.1  macallan 	sc->sc_cmap.cm_map[idx][1] = g;
    563      1.1  macallan 	sc->sc_cmap.cm_map[idx][2] = b;
    564      1.1  macallan 	agten_write_idx(sc, IBM561_CMAP_TABLE + idx);
    565      1.1  macallan 	agten_write_dac(sc, IBM561_CMD_CMAP, r);
    566      1.1  macallan 	agten_write_dac(sc, IBM561_CMD_CMAP, g);
    567      1.1  macallan 	agten_write_dac(sc, IBM561_CMD_CMAP, b);
    568      1.1  macallan 	return 0;
    569      1.1  macallan }
    570      1.1  macallan 
    571      1.1  macallan static void
    572      1.1  macallan agten_init(struct agten_softc *sc)
    573      1.1  macallan {
    574      1.1  macallan 	int i, j;
    575      1.1  macallan 	uint32_t src, srcw;
    576      1.1  macallan 	volatile uint32_t junk;
    577      1.1  macallan 
    578      1.1  macallan 	/* first we set up the colour map */
    579      1.1  macallan 	j = 0;
    580      1.1  macallan 	for (i = 0; i < 256; i++) {
    581      1.1  macallan 
    582      1.1  macallan 		agten_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    583      1.1  macallan 		    rasops_cmap[j + 2]);
    584      1.1  macallan 		j += 3;
    585      1.1  macallan 	}
    586      1.1  macallan 
    587      1.3  macallan 	/* then we set up a linear LUT for 24bit colour */
    588      1.3  macallan 	agten_write_idx(sc, IBM561_CMAP_TABLE + 256);
    589      1.3  macallan 	for (i = 0; i < 256; i++) {
    590      1.3  macallan 		agten_write_dac(sc, IBM561_CMD_CMAP, i);
    591      1.3  macallan 		agten_write_dac(sc, IBM561_CMD_CMAP, i);
    592      1.3  macallan 		agten_write_dac(sc, IBM561_CMD_CMAP, i);
    593      1.3  macallan 	}
    594      1.3  macallan 
    595      1.3  macallan 	/* and the linear gamma maps */
    596      1.3  macallan 	agten_write_idx(sc, IBM561_RED_GAMMA_TABLE);
    597      1.3  macallan 	for (i = 0; i < 0x3ff; i+= 4)
    598      1.3  macallan 		agten_write_dac_10(sc, IBM561_CMD_GAMMA, i);
    599      1.3  macallan 	agten_write_idx(sc, IBM561_GREEN_GAMMA_TABLE);
    600      1.3  macallan 	for (i = 0; i < 0x3ff; i+= 4)
    601      1.3  macallan 		agten_write_dac_10(sc, IBM561_CMD_GAMMA, i);
    602      1.3  macallan 	agten_write_idx(sc, IBM561_BLUE_GAMMA_TABLE);
    603      1.3  macallan 	for (i = 0; i < 0x3ff; i+= 4)
    604      1.3  macallan 		agten_write_dac_10(sc, IBM561_CMD_GAMMA, i);
    605      1.3  macallan 
    606      1.3  macallan 	/* enable outouts, RGB mode */
    607      1.3  macallan 	agten_write_idx(sc, IBM561_CONFIG_REG3);
    608      1.3  macallan 	agten_write_dac(sc, IBM561_CMD, 0x41);
    609      1.3  macallan 
    610      1.3  macallan 	/* MUX 4:1 basic, 8bit overlay, 8bit WIDs */
    611      1.3  macallan 	agten_write_idx(sc, IBM561_CONFIG_REG1);
    612      1.3  macallan 	agten_write_dac(sc, IBM561_CMD, 0x2c);
    613      1.3  macallan 
    614  1.3.2.1      matt 	/* use internal PLL, enable video output */
    615  1.3.2.1      matt 	agten_write_idx(sc, IBM561_CONFIG_REG2);
    616  1.3.2.1      matt 	agten_write_dac(sc, IBM561_CMD, CR2_ENABLE_CLC | CR2_PLL_REF_SELECT |
    617  1.3.2.1      matt 	    CR2_PIXEL_CLOCK_SELECT | CR2_ENABLE_RGB_OUTPUT);
    618  1.3.2.1      matt 
    619      1.1  macallan 	/* now set up some window attributes */
    620      1.3  macallan 
    621      1.3  macallan 	/*
    622      1.3  macallan 	 * direct colour, 24 bit, transparency off, LUT from 0x100
    623      1.3  macallan 	 * we need to use direct colour and a linear LUT because for some
    624      1.3  macallan 	 * reason true color mode gives messed up colours
    625      1.3  macallan 	 */
    626      1.3  macallan 	agten_write_idx(sc, IBM561_FB_WINTYPE);
    627      1.3  macallan 	agten_write_dac_10(sc, IBM561_CMD_FB_WAT, 0x134);
    628      1.3  macallan 
    629      1.3  macallan 	/* use gamma LUTs, no crosshair, 0 is transparent */
    630      1.3  macallan 	agten_write_idx(sc, IBM561_AUXFB_WINTYPE);
    631      1.3  macallan 	agten_write_dac(sc, IBM561_CMD_FB_WAT, 0x0);
    632      1.3  macallan 
    633      1.3  macallan 	/* overlay is 8 bit, opaque */
    634      1.2  macallan 	agten_write_idx(sc, IBM561_OL_WINTYPE);
    635      1.3  macallan 	agten_write_dac_10(sc, IBM561_CMD_FB_WAT, 0x00);
    636      1.1  macallan 
    637      1.3  macallan 	/* now we fill the WID fb with zeroes */
    638      1.1  macallan 	src = 0;
    639      1.1  macallan 	srcw = sc->sc_width << 16 | sc->sc_height;
    640      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, FOREGROUND_COLOR,
    641      1.1  macallan 	    0x0);
    642      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, BACKGROUND_COLOR,
    643      1.1  macallan 	    0x0);
    644      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, RASTER_OP, ROP_PAT);
    645      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, COORD_INDEX, 0);
    646      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, RECT_RTW_XY, src);
    647      1.1  macallan 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, RECT_RTW_XY, srcw);
    648      1.1  macallan 	junk = bus_space_read_4(sc->sc_bustag, sc->sc_p9100_regh, COMMAND_QUAD);
    649      1.1  macallan 
    650  1.3.2.1      matt 	/* initialize the cursor registers */
    651  1.3.2.1      matt 
    652  1.3.2.1      matt 	/* initialize the Imagine 128 */
    653      1.2  macallan 	i128_init(sc->sc_bustag, sc->sc_i128_regh, sc->sc_stride, 8);
    654      1.1  macallan }
    655      1.1  macallan 
    656      1.1  macallan static void
    657      1.3  macallan agten_gfx(struct agten_softc *sc)
    658      1.3  macallan {
    659      1.3  macallan 	/* enable overlay transparency on colour 0x00 */
    660      1.3  macallan 	agten_write_idx(sc, IBM561_OL_WINTYPE);
    661      1.3  macallan 	agten_write_dac_10(sc, IBM561_CMD_FB_WAT, 0x01);
    662      1.3  macallan 
    663      1.3  macallan 	/* then blit the overlay full of 0x00 */
    664      1.3  macallan 	i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, 0, 0, sc->sc_width,
    665      1.3  macallan 	    sc->sc_height, 0);
    666      1.3  macallan 
    667      1.3  macallan 	/* ... so we can see the 24bit framebuffer */
    668      1.3  macallan }
    669      1.3  macallan 
    670      1.3  macallan static void
    671  1.3.2.1      matt agten_set_video(struct agten_softc *sc, int flag)
    672  1.3.2.1      matt {
    673  1.3.2.1      matt 	uint8_t reg =
    674  1.3.2.1      matt 	    CR2_ENABLE_CLC | CR2_PLL_REF_SELECT | CR2_PIXEL_CLOCK_SELECT;
    675  1.3.2.1      matt 
    676  1.3.2.1      matt 	if (flag == sc->sc_video)
    677  1.3.2.1      matt 		return;
    678  1.3.2.1      matt 
    679  1.3.2.1      matt 	agten_write_idx(sc, IBM561_CONFIG_REG2);
    680  1.3.2.1      matt 	agten_write_dac(sc, IBM561_CMD, flag ? reg | CR2_ENABLE_RGB_OUTPUT :
    681  1.3.2.1      matt 	    reg);
    682  1.3.2.1      matt 
    683  1.3.2.1      matt 	sc->sc_video = flag;
    684  1.3.2.1      matt }
    685  1.3.2.1      matt 
    686  1.3.2.1      matt static int
    687  1.3.2.1      matt agten_get_video(struct agten_softc *sc)
    688  1.3.2.1      matt {
    689  1.3.2.1      matt 
    690  1.3.2.1      matt 	return sc->sc_video;
    691  1.3.2.1      matt }
    692  1.3.2.1      matt 
    693  1.3.2.1      matt static void
    694      1.1  macallan agten_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    695      1.1  macallan {
    696      1.1  macallan 	struct rasops_info *ri = cookie;
    697      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    698      1.1  macallan 	struct agten_softc *sc = scr->scr_cookie;
    699      1.1  macallan 	int32_t xs, xd, y, width, height;
    700      1.1  macallan 
    701      1.1  macallan 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    702      1.1  macallan 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    703      1.1  macallan 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    704      1.1  macallan 	width = ri->ri_font->fontwidth * ncols;
    705      1.1  macallan 	height = ri->ri_font->fontheight;
    706      1.1  macallan 	i128_bitblt(sc->sc_bustag, sc->sc_i128_regh, xs, y, xd, y, width,
    707      1.2  macallan 	    height, CR_COPY);
    708      1.1  macallan }
    709      1.1  macallan 
    710      1.1  macallan static void
    711      1.1  macallan agten_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    712      1.1  macallan {
    713      1.1  macallan 	struct rasops_info *ri = cookie;
    714      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    715      1.1  macallan 	struct agten_softc *sc = scr->scr_cookie;
    716      1.1  macallan 	int32_t x, y, width, height, bg;
    717      1.1  macallan 
    718      1.1  macallan 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    719      1.1  macallan 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    720      1.1  macallan 	width = ri->ri_font->fontwidth * ncols;
    721      1.1  macallan 	height = ri->ri_font->fontheight;
    722      1.1  macallan 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
    723      1.1  macallan 	i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, x, y, width, height, bg);
    724      1.1  macallan }
    725      1.1  macallan 
    726      1.1  macallan static void
    727      1.1  macallan agten_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    728      1.1  macallan {
    729      1.1  macallan 	struct rasops_info *ri = cookie;
    730      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    731      1.1  macallan 	struct agten_softc *sc = scr->scr_cookie;
    732      1.1  macallan 	int32_t x, ys, yd, width, height;
    733      1.1  macallan 
    734      1.1  macallan 	x = ri->ri_xorigin;
    735      1.1  macallan 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    736      1.1  macallan 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    737      1.1  macallan 	width = ri->ri_emuwidth;
    738      1.1  macallan 	height = ri->ri_font->fontheight * nrows;
    739      1.1  macallan 	i128_bitblt(sc->sc_bustag, sc->sc_i128_regh, x, ys, x, yd, width,
    740      1.2  macallan 	    height, CR_COPY);
    741      1.1  macallan }
    742      1.1  macallan 
    743      1.1  macallan static void
    744      1.1  macallan agten_eraserows(void *cookie, int row, int nrows, long fillattr)
    745      1.1  macallan {
    746      1.1  macallan 	struct rasops_info *ri = cookie;
    747      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    748      1.1  macallan 	struct agten_softc *sc = scr->scr_cookie;
    749      1.1  macallan 	int32_t x, y, width, height, bg;
    750      1.1  macallan 
    751      1.1  macallan 	if ((row == 0) && (nrows == ri->ri_rows)) {
    752      1.1  macallan 		x = y = 0;
    753      1.1  macallan 		width = ri->ri_width;
    754      1.1  macallan 		height = ri->ri_height;
    755      1.1  macallan 	} else {
    756      1.1  macallan 		x = ri->ri_xorigin;
    757      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    758      1.1  macallan 		width = ri->ri_emuwidth;
    759      1.1  macallan 		height = ri->ri_font->fontheight * nrows;
    760      1.1  macallan 	}
    761      1.1  macallan 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
    762      1.1  macallan 	i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, x, y, width, height, bg);
    763      1.1  macallan }
    764  1.3.2.1      matt 
    765  1.3.2.1      matt static void
    766  1.3.2.1      matt agten_move_cursor(struct agten_softc *sc, int x, int y)
    767  1.3.2.1      matt {
    768  1.3.2.1      matt 
    769  1.3.2.1      matt 	sc->sc_cursor_x = x;
    770  1.3.2.1      matt 	sc->sc_cursor_y = y;
    771  1.3.2.1      matt 	agten_write_idx(sc, IBM561_CURSOR_X_REG);
    772  1.3.2.1      matt 	agten_write_dac(sc, IBM561_CMD, x & 0xff);
    773  1.3.2.1      matt 	agten_write_dac(sc, IBM561_CMD, (x >> 8) & 0xff);
    774  1.3.2.1      matt 	agten_write_dac(sc, IBM561_CMD, y & 0xff);
    775  1.3.2.1      matt 	agten_write_dac(sc, IBM561_CMD, (y >> 8) & 0xff);
    776  1.3.2.1      matt }
    777  1.3.2.1      matt 
    778  1.3.2.1      matt static int
    779  1.3.2.1      matt agten_do_cursor(struct agten_softc *sc, struct wsdisplay_cursor *cur)
    780  1.3.2.1      matt {
    781  1.3.2.1      matt 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    782  1.3.2.1      matt 
    783  1.3.2.1      matt 		agten_write_idx(sc, IBM561_CURS_CNTL_REG);
    784  1.3.2.1      matt 		agten_write_dac(sc, IBM561_CMD, cur->enable ?
    785  1.3.2.1      matt 		    CURS_ENABLE : 0);
    786  1.3.2.1      matt 	}
    787  1.3.2.1      matt 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    788  1.3.2.1      matt 
    789  1.3.2.1      matt 		agten_write_idx(sc, IBM561_HOTSPOT_X_REG);
    790  1.3.2.1      matt 		agten_write_dac(sc, IBM561_CMD, cur->hot.x);
    791  1.3.2.1      matt 		agten_write_dac(sc, IBM561_CMD, cur->hot.y);
    792  1.3.2.1      matt 	}
    793  1.3.2.1      matt 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    794  1.3.2.1      matt 
    795  1.3.2.1      matt 		agten_move_cursor(sc, cur->pos.x, cur->pos.y);
    796  1.3.2.1      matt 	}
    797  1.3.2.1      matt 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    798  1.3.2.1      matt 		int i;
    799  1.3.2.1      matt 
    800  1.3.2.1      matt 		agten_write_idx(sc, IBM561_CURSOR_LUT + cur->cmap.index + 2);
    801  1.3.2.1      matt 		for (i = 0; i < cur->cmap.count; i++) {
    802  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.red[i]);
    803  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.green[i]);
    804  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.blue[i]);
    805  1.3.2.1      matt 		}
    806  1.3.2.1      matt 	}
    807  1.3.2.1      matt 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    808  1.3.2.1      matt 		int i;
    809  1.3.2.1      matt 		uint16_t tmp;
    810  1.3.2.1      matt 
    811  1.3.2.1      matt 		agten_write_idx(sc, IBM561_CURSOR_BITMAP);
    812  1.3.2.1      matt 		for (i = 0; i < 512; i++) {
    813  1.3.2.1      matt 			tmp = util_interleave(cur->mask[i], cur->image[i]);
    814  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD, (tmp >> 8) & 0xff);
    815  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD, tmp & 0xff);
    816  1.3.2.1      matt 		}
    817  1.3.2.1      matt 	}
    818  1.3.2.1      matt 	return 0;
    819  1.3.2.1      matt }
    820  1.3.2.1      matt 
    821  1.3.2.1      matt static int
    822  1.3.2.1      matt agten_do_sun_cursor(struct agten_softc *sc, struct fbcursor *cur)
    823  1.3.2.1      matt {
    824  1.3.2.1      matt 	if (cur->set & FB_CUR_SETCUR) {
    825  1.3.2.1      matt 
    826  1.3.2.1      matt 		agten_write_idx(sc, IBM561_CURS_CNTL_REG);
    827  1.3.2.1      matt 		agten_write_dac(sc, IBM561_CMD, cur->enable ?
    828  1.3.2.1      matt 		    CURS_ENABLE : 0);
    829  1.3.2.1      matt 	}
    830  1.3.2.1      matt 	if (cur->set & FB_CUR_SETHOT) {
    831  1.3.2.1      matt 
    832  1.3.2.1      matt 		agten_write_idx(sc, IBM561_HOTSPOT_X_REG);
    833  1.3.2.1      matt 		agten_write_dac(sc, IBM561_CMD, cur->hot.x);
    834  1.3.2.1      matt 		agten_write_dac(sc, IBM561_CMD, cur->hot.y);
    835  1.3.2.1      matt 	}
    836  1.3.2.1      matt 	if (cur->set & FB_CUR_SETPOS) {
    837  1.3.2.1      matt 
    838  1.3.2.1      matt 		agten_move_cursor(sc, cur->pos.x, cur->pos.y);
    839  1.3.2.1      matt 	}
    840  1.3.2.1      matt 	if (cur->set & FB_CUR_SETCMAP) {
    841  1.3.2.1      matt 		int i;
    842  1.3.2.1      matt 
    843  1.3.2.1      matt 		agten_write_idx(sc, IBM561_CURSOR_LUT + cur->cmap.index + 2);
    844  1.3.2.1      matt 		for (i = 0; i < cur->cmap.count; i++) {
    845  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.red[i]);
    846  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.green[i]);
    847  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.blue[i]);
    848  1.3.2.1      matt 		}
    849  1.3.2.1      matt 	}
    850  1.3.2.1      matt 	if (cur->set & FB_CUR_SETSHAPE) {
    851  1.3.2.1      matt 		int i;
    852  1.3.2.1      matt 		uint16_t tmp;
    853  1.3.2.1      matt 
    854  1.3.2.1      matt 		agten_write_idx(sc, IBM561_CURSOR_BITMAP);
    855  1.3.2.1      matt 		for (i = 0; i < 512; i++) {
    856  1.3.2.1      matt 			tmp = util_interleave_lin(cur->mask[i], cur->image[i]);
    857  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD, (tmp >> 8) & 0xff);
    858  1.3.2.1      matt 			agten_write_dac(sc, IBM561_CMD, tmp & 0xff);
    859  1.3.2.1      matt 		}
    860  1.3.2.1      matt 	}
    861  1.3.2.1      matt 	return 0;
    862  1.3.2.1      matt }
    863  1.3.2.1      matt 
    864  1.3.2.1      matt uint16_t
    865  1.3.2.1      matt util_interleave(uint8_t b1, uint8_t b2)
    866  1.3.2.1      matt {
    867  1.3.2.1      matt 	int i;
    868  1.3.2.1      matt 	uint16_t ret = 0;
    869  1.3.2.1      matt 	uint16_t mask = 0x8000;
    870  1.3.2.1      matt 	uint8_t mask8 = 0x01;
    871  1.3.2.1      matt 
    872  1.3.2.1      matt 	for (i = 0; i < 8; i++) {
    873  1.3.2.1      matt 		if (b1 & mask8)
    874  1.3.2.1      matt 			ret |= mask;
    875  1.3.2.1      matt 		mask = mask >> 1;
    876  1.3.2.1      matt 		if (b2 & mask8)
    877  1.3.2.1      matt 			ret |= mask;
    878  1.3.2.1      matt 		mask = mask >> 1;
    879  1.3.2.1      matt 		mask8 = mask8 << 1;
    880  1.3.2.1      matt 	}
    881  1.3.2.1      matt 	return ret;
    882  1.3.2.1      matt }
    883  1.3.2.1      matt 
    884  1.3.2.1      matt uint16_t
    885  1.3.2.1      matt util_interleave_lin(uint8_t b1, uint8_t b2)
    886  1.3.2.1      matt {
    887  1.3.2.1      matt 	int i;
    888  1.3.2.1      matt 	uint16_t ret = 0;
    889  1.3.2.1      matt 	uint16_t mask = 0x8000;
    890  1.3.2.1      matt 	uint8_t mask8 = 0x80;
    891  1.3.2.1      matt 
    892  1.3.2.1      matt 	for (i = 0; i < 8; i++) {
    893  1.3.2.1      matt 		if (b1 & mask8)
    894  1.3.2.1      matt 			ret |= mask;
    895  1.3.2.1      matt 		mask = mask >> 1;
    896  1.3.2.1      matt 		if (b2 & mask8)
    897  1.3.2.1      matt 			ret |= mask;
    898  1.3.2.1      matt 		mask = mask >> 1;
    899  1.3.2.1      matt 		mask8 = mask8 >> 1;
    900  1.3.2.1      matt 	}
    901  1.3.2.1      matt 	return ret;
    902  1.3.2.1      matt }
    903  1.3.2.1      matt 
    904  1.3.2.1      matt /* and now the /dev/fb* stuff */
    905  1.3.2.1      matt static void
    906  1.3.2.1      matt agten_fb_unblank(struct device *dev)
    907  1.3.2.1      matt {
    908  1.3.2.1      matt 	struct agten_softc *sc = (void *)dev;
    909  1.3.2.1      matt 
    910  1.3.2.1      matt 	agten_init(sc);
    911  1.3.2.1      matt 	agten_set_video(sc, 1);
    912  1.3.2.1      matt }
    913  1.3.2.1      matt 
    914  1.3.2.1      matt static int
    915  1.3.2.1      matt agten_fb_open(dev_t dev, int flags, int mode, struct lwp *l)
    916  1.3.2.1      matt {
    917  1.3.2.1      matt 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
    918  1.3.2.1      matt 	int unit = minor(dev);
    919  1.3.2.1      matt 
    920  1.3.2.1      matt 	if (unit >= agten_cd.cd_ndevs || agten_cd.cd_devs[unit] == NULL)
    921  1.3.2.1      matt 		return (ENXIO);
    922  1.3.2.1      matt 	if (sc->sc_fb_is_open)
    923  1.3.2.1      matt 		return 0;
    924  1.3.2.1      matt 
    925  1.3.2.1      matt 	sc->sc_fb_is_open++;
    926  1.3.2.1      matt 	agten_gfx(sc);
    927  1.3.2.1      matt 
    928  1.3.2.1      matt 	return (0);
    929  1.3.2.1      matt }
    930  1.3.2.1      matt 
    931  1.3.2.1      matt static int
    932  1.3.2.1      matt agten_fb_close(dev_t dev, int flags, int mode, struct lwp *l)
    933  1.3.2.1      matt {
    934  1.3.2.1      matt 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
    935  1.3.2.1      matt 
    936  1.3.2.1      matt 	sc->sc_fb_is_open--;
    937  1.3.2.1      matt 	if (sc->sc_fb_is_open < 0)
    938  1.3.2.1      matt 		sc->sc_fb_is_open = 0;
    939  1.3.2.1      matt 
    940  1.3.2.1      matt 	if (sc->sc_fb_is_open == 0) {
    941  1.3.2.1      matt 		agten_init(sc);
    942  1.3.2.1      matt 		vcons_redraw_screen(sc->vd.active);
    943  1.3.2.1      matt 	}
    944  1.3.2.1      matt 
    945  1.3.2.1      matt 	return (0);
    946  1.3.2.1      matt }
    947  1.3.2.1      matt 
    948  1.3.2.1      matt static int
    949  1.3.2.1      matt agten_fb_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    950  1.3.2.1      matt {
    951  1.3.2.1      matt 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
    952  1.3.2.1      matt 	struct fbgattr *fba;
    953  1.3.2.1      matt 	int error;
    954  1.3.2.1      matt 
    955  1.3.2.1      matt 	switch (cmd) {
    956  1.3.2.1      matt 
    957  1.3.2.1      matt 	case FBIOGTYPE:
    958  1.3.2.1      matt 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    959  1.3.2.1      matt 		break;
    960  1.3.2.1      matt 
    961  1.3.2.1      matt 	case FBIOGATTR:
    962  1.3.2.1      matt 		fba = (struct fbgattr *)data;
    963  1.3.2.1      matt 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    964  1.3.2.1      matt 		fba->owner = 0;		/* XXX ??? */
    965  1.3.2.1      matt 		fba->fbtype = sc->sc_fb.fb_type;
    966  1.3.2.1      matt 		fba->sattr.flags = 0;
    967  1.3.2.1      matt 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    968  1.3.2.1      matt 		fba->sattr.dev_specific[0] = -1;
    969  1.3.2.1      matt 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    970  1.3.2.1      matt 		fba->emu_types[1] = -1;
    971  1.3.2.1      matt 		break;
    972  1.3.2.1      matt 
    973  1.3.2.1      matt 	case FBIOGETCMAP:
    974  1.3.2.1      matt #define p ((struct fbcmap *)data)
    975  1.3.2.1      matt 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    976  1.3.2.1      matt 
    977  1.3.2.1      matt 	case FBIOPUTCMAP:
    978  1.3.2.1      matt 		/* copy to software map */
    979  1.3.2.1      matt 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    980  1.3.2.1      matt 		if (error)
    981  1.3.2.1      matt 			return (error);
    982  1.3.2.1      matt 		/* now blast them into the chip */
    983  1.3.2.1      matt 		/* don't bother - we're 24bit */
    984  1.3.2.1      matt #undef p
    985  1.3.2.1      matt 		break;
    986  1.3.2.1      matt 
    987  1.3.2.1      matt 	case FBIOGVIDEO:
    988  1.3.2.1      matt 		*(int *)data = agten_get_video(sc);
    989  1.3.2.1      matt 		break;
    990  1.3.2.1      matt 
    991  1.3.2.1      matt 	case FBIOSVIDEO:
    992  1.3.2.1      matt 		agten_set_video(sc, *(int *)data);
    993  1.3.2.1      matt 		break;
    994  1.3.2.1      matt 
    995  1.3.2.1      matt /* these are for both FBIOSCURSOR and FBIOGCURSOR */
    996  1.3.2.1      matt #define p ((struct fbcursor *)data)
    997  1.3.2.1      matt #define pc (&sc->sc_cursor)
    998  1.3.2.1      matt 
    999  1.3.2.1      matt 	case FBIOGCURSOR:
   1000  1.3.2.1      matt 		/* does anyone use this ioctl?! */
   1001  1.3.2.1      matt 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
   1002  1.3.2.1      matt 		p->enable = 1;
   1003  1.3.2.1      matt 		p->pos.x = sc->sc_cursor_x;
   1004  1.3.2.1      matt 		p->pos.y = sc->sc_cursor_y;
   1005  1.3.2.1      matt 		p->size.x = 64;
   1006  1.3.2.1      matt 		p->size.y = 64;
   1007  1.3.2.1      matt 		break;
   1008  1.3.2.1      matt 
   1009  1.3.2.1      matt 	case FBIOSCURSOR:
   1010  1.3.2.1      matt 		agten_do_sun_cursor(sc, p);
   1011  1.3.2.1      matt 	break;
   1012  1.3.2.1      matt 
   1013  1.3.2.1      matt #undef p
   1014  1.3.2.1      matt #undef cc
   1015  1.3.2.1      matt 
   1016  1.3.2.1      matt 	case FBIOGCURPOS:
   1017  1.3.2.1      matt 	{
   1018  1.3.2.1      matt 		struct fbcurpos *cp = (struct fbcurpos *)data;
   1019  1.3.2.1      matt 		cp->x = sc->sc_cursor_x;
   1020  1.3.2.1      matt 		cp->y = sc->sc_cursor_y;
   1021  1.3.2.1      matt 	}
   1022  1.3.2.1      matt 	break;
   1023  1.3.2.1      matt 
   1024  1.3.2.1      matt 	case FBIOSCURPOS:
   1025  1.3.2.1      matt 	{
   1026  1.3.2.1      matt 		struct fbcurpos *cp = (struct fbcurpos *)data;
   1027  1.3.2.1      matt 		agten_move_cursor(sc, cp->x, cp->y);
   1028  1.3.2.1      matt 	}
   1029  1.3.2.1      matt 	break;
   1030  1.3.2.1      matt 
   1031  1.3.2.1      matt 	case FBIOGCURMAX:
   1032  1.3.2.1      matt 		/* max cursor size is 64x64 */
   1033  1.3.2.1      matt 		((struct fbcurpos *)data)->x = 64;
   1034  1.3.2.1      matt 		((struct fbcurpos *)data)->y = 64;
   1035  1.3.2.1      matt 		break;
   1036  1.3.2.1      matt 
   1037  1.3.2.1      matt 	default:
   1038  1.3.2.1      matt 		return (ENOTTY);
   1039  1.3.2.1      matt 	}
   1040  1.3.2.1      matt 	return (0);
   1041  1.3.2.1      matt }
   1042  1.3.2.1      matt 
   1043  1.3.2.1      matt static paddr_t
   1044  1.3.2.1      matt agten_fb_mmap(dev_t dev, off_t off, int prot)
   1045  1.3.2.1      matt {
   1046  1.3.2.1      matt 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
   1047  1.3.2.1      matt 
   1048  1.3.2.1      matt 	/*
   1049  1.3.2.1      matt 	 * mappings are subject to change
   1050  1.3.2.1      matt 	 * for now we put the framebuffer at offset 0 and the GLint registers
   1051  1.3.2.1      matt 	 * right after that. We may want to expose more register ranges and
   1052  1.3.2.1      matt 	 * probably will want to map the 2nd framebuffer as well
   1053  1.3.2.1      matt 	 */
   1054  1.3.2.1      matt 
   1055  1.3.2.1      matt 	if (off < 0)
   1056  1.3.2.1      matt 		return EINVAL;
   1057  1.3.2.1      matt 
   1058  1.3.2.1      matt 	if (off >= sc->sc_glint_fbsz + 0x10000)
   1059  1.3.2.1      matt 		return EINVAL;
   1060  1.3.2.1      matt 
   1061  1.3.2.1      matt 	if (off < sc->sc_glint_fbsz) {
   1062  1.3.2.1      matt 		return (bus_space_mmap(sc->sc_bustag,
   1063  1.3.2.1      matt 			sc->sc_glint_fb,
   1064  1.3.2.1      matt 			off,
   1065  1.3.2.1      matt 			prot,
   1066  1.3.2.1      matt 			BUS_SPACE_MAP_LINEAR));
   1067  1.3.2.1      matt 	}
   1068  1.3.2.1      matt 
   1069  1.3.2.1      matt 	off -= sc->sc_glint_fbsz;
   1070  1.3.2.1      matt 	if (off < 0x10000) {
   1071  1.3.2.1      matt 		return (bus_space_mmap(sc->sc_bustag,
   1072  1.3.2.1      matt 			sc->sc_glint_regs,
   1073  1.3.2.1      matt 			off,
   1074  1.3.2.1      matt 			prot,
   1075  1.3.2.1      matt 			BUS_SPACE_MAP_LINEAR));
   1076  1.3.2.1      matt 	}
   1077  1.3.2.1      matt 	return EINVAL;
   1078  1.3.2.1      matt }
   1079