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