Home | History | Annotate | Line # | Download | only in sbus
agten.c revision 1.6.2.2
      1 /*	$NetBSD: agten.c,v 1.6.2.2 2007/09/03 10:21:58 skrll 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.6.2.2 2007/09/03 10:21:58 skrll 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 	int sc_video;			/* video output enabled */
    117 
    118 	/* some /dev/fb* stuff */
    119 	int sc_fb_is_open;
    120 
    121 	union	bt_cmap sc_cmap;	/* Brooktree color map */
    122 
    123 	int sc_mode;
    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 static uint16_t util_interleave(uint8_t, uint8_t);
    153 static 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_video = -1;
    241 	sc->sc_bustag = sa->sa_bustag;
    242 #if 0
    243 	sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_WAITOK);
    244 
    245 	dict = device_properties(&sc->sc_dev);
    246 
    247 	prop_dictionary_get_bool(dict, "is_console", &console);
    248 #endif
    249 
    250 	reg = prom_getpropint(node, "i128_fb_physaddr", -1);
    251 	sc->sc_i128_fbsz = prom_getpropint(node, "i128_fb_size", -1);
    252 	if (sbus_bus_map(sc->sc_bustag,
    253 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg,
    254 	    sc->sc_i128_fbsz, BUS_SPACE_MAP_LINEAR, &sc->sc_i128_fbh) != 0) {
    255 
    256 		aprint_error("%s: unable to map the framebuffer\n",
    257 		    dev->dv_xname);
    258 		return;
    259 	}
    260 	fb->fb_pixels = bus_space_vaddr(sc->sc_bustag, sc->sc_i128_fbh);
    261 
    262 	reg = prom_getpropint(node, "i128_reg_physaddr", -1);
    263 	if (sbus_bus_map(sc->sc_bustag,
    264 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg,
    265 	    0x10000, 0, &sc->sc_i128_regh) != 0) {
    266 
    267 		aprint_error("%s: unable to map I128 registers\n",
    268 		    dev->dv_xname);
    269 		return;
    270 	}
    271 
    272 	reg = prom_getpropint(node, "p9100_reg_physaddr", -1);
    273 	if (sbus_bus_map(sc->sc_bustag,
    274 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg,
    275 	    0x8000, 0, &sc->sc_p9100_regh) != 0) {
    276 
    277 		aprint_error("%s: unable to map P9100 registers\n",
    278 		    dev->dv_xname);
    279 		return;
    280 	}
    281 
    282 	reg = prom_getpropint(node, "glint_fb0_physaddr", -1);
    283 	sc->sc_glint_fb = sbus_bus_addr(sc->sc_bustag,
    284 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg);
    285 	sc->sc_glint_fbsz = prom_getpropint(node, "glint_fb0_size", -1);
    286 	reg = prom_getpropint(node, "glint_reg_physaddr", -1);
    287 	sc->sc_glint_regs = sbus_bus_addr(sc->sc_bustag,
    288 	    sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base + reg);
    289 
    290 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
    291 #if 0
    292 	bus_intr_establish(sc->sc_bustag, sa->sa_pri, IPL_BIO,
    293 	    agten_intr, sc);
    294 #endif
    295 
    296 	sc->sc_width = prom_getpropint(node, "ffb_width", 800);
    297 	sc->sc_height = prom_getpropint(node, "ffb_height", 600);
    298 	sc->sc_depth = prom_getpropint(node, "ffb_depth", 8);
    299 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    300 
    301 	agten_init(sc);
    302 
    303 	console = fb_is_console(node);
    304 
    305 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    306 	    &agten_accessops);
    307 	sc->vd.init_screen = agten_init_screen;
    308 
    309 	ri = &sc->sc_console_screen.scr_ri;
    310 
    311 	if (console) {
    312 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    313 		    &defattr);
    314 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    315 
    316 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    317 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    318 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    319 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    320 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    321 		    defattr);
    322 		i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, 0, 0,
    323 		    sc->sc_width, sc->sc_height,
    324 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    325 	} else {
    326 		/*
    327 		 * since we're not the console we can postpone the rest
    328 		 * until someone actually allocates a screen for us
    329 		 */
    330 	}
    331 
    332 	/* Initialize the default color map. */
    333 
    334 	aa.console = console;
    335 	aa.scrdata = &sc->sc_screenlist;
    336 	aa.accessops = &agten_accessops;
    337 	aa.accesscookie = &sc->vd;
    338 
    339 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    340 
    341 	fb->fb_driver = &agtenfbdriver;
    342 	fb->fb_device = &sc->sc_dev;
    343 	fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
    344 	fb->fb_type.fb_type = FBTYPE_AG10E;
    345 	fb->fb_type.fb_cmsize = 256;	/* doesn't matter, we're always 24bit */
    346 	fb->fb_type.fb_size = sc->sc_glint_fbsz;
    347 	fb->fb_type.fb_width = sc->sc_width;
    348 	fb->fb_type.fb_height = sc->sc_height;
    349 	fb->fb_type.fb_depth = 32;
    350 	fb->fb_linebytes = sc->sc_stride;
    351 	fb_attach(fb, console);
    352 	agten_set_video(sc, 1);	/* make sure video's on */
    353 }
    354 
    355 static int
    356 agten_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    357 	struct lwp *l)
    358 {
    359 	struct vcons_data *vd = v;
    360 	struct agten_softc *sc = vd->cookie;
    361 	struct wsdisplay_fbinfo *wdf;
    362 	struct vcons_screen *ms = vd->active;
    363 
    364 	switch (cmd) {
    365 
    366 		case WSDISPLAYIO_GTYPE:
    367 			*(u_int *)data = WSDISPLAY_TYPE_AG10;
    368 			return 0;
    369 
    370 		case WSDISPLAYIO_GINFO:
    371 			if (ms == NULL)
    372 				return ENODEV;
    373 			wdf = (void *)data;
    374 			wdf->height = ms->scr_ri.ri_height;
    375 			wdf->width = ms->scr_ri.ri_width;
    376 			wdf->depth = 32;
    377 			wdf->cmsize = 256;
    378 			return 0;
    379 
    380 		case WSDISPLAYIO_GVIDEO:
    381 			*(int *)data = sc->sc_video;
    382 			return 0;
    383 
    384 		case WSDISPLAYIO_SVIDEO:
    385 			agten_set_video(sc, *(int *)data);
    386 			return 0;
    387 
    388 		case WSDISPLAYIO_GETCMAP:
    389 			return agten_getcmap(sc,
    390 			    (struct wsdisplay_cmap *)data);
    391 
    392 		case WSDISPLAYIO_PUTCMAP:
    393 			return agten_putcmap(sc,
    394 			    (struct wsdisplay_cmap *)data);
    395 
    396 		case WSDISPLAYIO_LINEBYTES:
    397 			*(u_int *)data = sc->sc_stride << 2;
    398 			return 0;
    399 
    400 		case WSDISPLAYIO_SMODE:
    401 			{
    402 				int new_mode = *(int*)data;
    403 				if (new_mode != sc->sc_mode) {
    404 					sc->sc_mode = new_mode;
    405 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    406 						agten_init(sc);
    407 						vcons_redraw_screen(ms);
    408 					} else {
    409 						agten_gfx(sc);
    410 					}
    411 				}
    412 			}
    413 			return 0;
    414 
    415 		case WSDISPLAYIO_GCURPOS:
    416 			{
    417 				struct wsdisplay_curpos *cp = (void *)data;
    418 
    419 				cp->x = sc->sc_cursor_x;
    420 				cp->y = sc->sc_cursor_y;
    421 			}
    422 			return 0;
    423 
    424 		case WSDISPLAYIO_SCURPOS:
    425 			{
    426 				struct wsdisplay_curpos *cp = (void *)data;
    427 
    428 				agten_move_cursor(sc, cp->x, cp->y);
    429 			}
    430 			return 0;
    431 
    432 		case WSDISPLAYIO_GCURMAX:
    433 			{
    434 				struct wsdisplay_curpos *cp = (void *)data;
    435 
    436 				cp->x = 64;
    437 				cp->y = 64;
    438 			}
    439 			return 0;
    440 
    441 		case WSDISPLAYIO_SCURSOR:
    442 			{
    443 				struct wsdisplay_cursor *cursor = (void *)data;
    444 
    445 				return agten_do_cursor(sc, cursor);
    446 			}
    447 	}
    448 	return EPASSTHROUGH;
    449 }
    450 
    451 static paddr_t
    452 agten_mmap(void *v, void *vs, off_t offset, int prot)
    453 {
    454 	struct vcons_data *vd = v;
    455 	struct agten_softc *sc = vd->cookie;
    456 
    457 	if (offset < sc->sc_glint_fbsz)
    458 		return bus_space_mmap(sc->sc_bustag, sc->sc_glint_fb, offset,
    459 		    prot, BUS_SPACE_MAP_LINEAR);
    460 	return -1;
    461 }
    462 
    463 static void
    464 agten_init_screen(void *cookie, struct vcons_screen *scr,
    465     int existing, long *defattr)
    466 {
    467 	struct agten_softc *sc = cookie;
    468 	struct rasops_info *ri = &scr->scr_ri;
    469 
    470 	ri->ri_depth = sc->sc_depth;
    471 	ri->ri_width = sc->sc_width;
    472 	ri->ri_height = sc->sc_height;
    473 	ri->ri_stride = sc->sc_stride;
    474 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    475 
    476 	ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
    477 
    478 	if (existing) {
    479 		ri->ri_flg |= RI_CLEAR;
    480 	}
    481 
    482 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    483 	ri->ri_caps = WSSCREEN_WSCOLORS;
    484 
    485 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    486 		    sc->sc_width / ri->ri_font->fontwidth);
    487 
    488 	ri->ri_hw = scr;
    489 	ri->ri_ops.copyrows  = agten_copyrows;
    490 	ri->ri_ops.eraserows = agten_eraserows;
    491 	ri->ri_ops.copycols  = agten_copycols;
    492 	ri->ri_ops.erasecols = agten_erasecols;
    493 
    494 }
    495 
    496 static int
    497 agten_putcmap(struct agten_softc *sc, struct wsdisplay_cmap *cm)
    498 {
    499 	u_int index = cm->index;
    500 	u_int count = cm->count;
    501 	int i, error;
    502 	u_char rbuf[256], gbuf[256], bbuf[256];
    503 	u_char *r, *g, *b;
    504 
    505 	if (cm->index >= 256 || cm->count > 256 ||
    506 	    (cm->index + cm->count) > 256)
    507 		return EINVAL;
    508 	error = copyin(cm->red, &rbuf[index], count);
    509 	if (error)
    510 		return error;
    511 	error = copyin(cm->green, &gbuf[index], count);
    512 	if (error)
    513 		return error;
    514 	error = copyin(cm->blue, &bbuf[index], count);
    515 	if (error)
    516 		return error;
    517 
    518 	r = &rbuf[index];
    519 	g = &gbuf[index];
    520 	b = &bbuf[index];
    521 
    522 	for (i = 0; i < count; i++) {
    523 		agten_putpalreg(sc, index, *r, *g, *b);
    524 		index++;
    525 		r++, g++, b++;
    526 	}
    527 	return 0;
    528 }
    529 
    530 static int
    531 agten_getcmap(struct agten_softc *sc, struct wsdisplay_cmap *cm)
    532 {
    533 	u_int index = cm->index;
    534 	u_int count = cm->count;
    535 	int error, i;
    536 	uint8_t red[256], green[256], blue[256];
    537 
    538 	if (index >= 255 || count > 256 || index + count > 256)
    539 		return EINVAL;
    540 
    541 	i = index;
    542 	while (i < (index + count)) {
    543 		red[i] = sc->sc_cmap.cm_map[i][0];
    544 		green[i] = sc->sc_cmap.cm_map[i][1];
    545 		blue[i] = sc->sc_cmap.cm_map[i][2];
    546 		i++;
    547 	}
    548 	error = copyout(&red[index],   cm->red,   count);
    549 	if (error)
    550 		return error;
    551 	error = copyout(&green[index], cm->green, count);
    552 	if (error)
    553 		return error;
    554 	error = copyout(&blue[index],  cm->blue,  count);
    555 	if (error)
    556 		return error;
    557 
    558 	return 0;
    559 }
    560 
    561 static int
    562 agten_putpalreg(struct agten_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    563     uint8_t b)
    564 {
    565 
    566 	sc->sc_cmap.cm_map[idx][0] = r;
    567 	sc->sc_cmap.cm_map[idx][1] = g;
    568 	sc->sc_cmap.cm_map[idx][2] = b;
    569 	agten_write_idx(sc, IBM561_CMAP_TABLE + idx);
    570 	agten_write_dac(sc, IBM561_CMD_CMAP, r);
    571 	agten_write_dac(sc, IBM561_CMD_CMAP, g);
    572 	agten_write_dac(sc, IBM561_CMD_CMAP, b);
    573 	return 0;
    574 }
    575 
    576 static void
    577 agten_init(struct agten_softc *sc)
    578 {
    579 	int i, j;
    580 	uint32_t src, srcw;
    581 	volatile uint32_t junk;
    582 
    583 	/* first we set up the colour map */
    584 	j = 0;
    585 	for (i = 0; i < 256; i++) {
    586 
    587 		agten_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    588 		    rasops_cmap[j + 2]);
    589 		j += 3;
    590 	}
    591 
    592 	/* then we set up a linear LUT for 24bit colour */
    593 	agten_write_idx(sc, IBM561_CMAP_TABLE + 256);
    594 	for (i = 0; i < 256; i++) {
    595 		agten_write_dac(sc, IBM561_CMD_CMAP, i);
    596 		agten_write_dac(sc, IBM561_CMD_CMAP, i);
    597 		agten_write_dac(sc, IBM561_CMD_CMAP, i);
    598 	}
    599 
    600 	/* and the linear gamma maps */
    601 	agten_write_idx(sc, IBM561_RED_GAMMA_TABLE);
    602 	for (i = 0; i < 0x3ff; i+= 4)
    603 		agten_write_dac_10(sc, IBM561_CMD_GAMMA, i);
    604 	agten_write_idx(sc, IBM561_GREEN_GAMMA_TABLE);
    605 	for (i = 0; i < 0x3ff; i+= 4)
    606 		agten_write_dac_10(sc, IBM561_CMD_GAMMA, i);
    607 	agten_write_idx(sc, IBM561_BLUE_GAMMA_TABLE);
    608 	for (i = 0; i < 0x3ff; i+= 4)
    609 		agten_write_dac_10(sc, IBM561_CMD_GAMMA, i);
    610 
    611 	/* enable outouts, RGB mode */
    612 	agten_write_idx(sc, IBM561_CONFIG_REG3);
    613 	agten_write_dac(sc, IBM561_CMD, 0x41);
    614 
    615 	/* MUX 4:1 basic, 8bit overlay, 8bit WIDs */
    616 	agten_write_idx(sc, IBM561_CONFIG_REG1);
    617 	agten_write_dac(sc, IBM561_CMD, 0x2c);
    618 
    619 	/* use internal PLL, enable video output */
    620 	agten_write_idx(sc, IBM561_CONFIG_REG2);
    621 	agten_write_dac(sc, IBM561_CMD, CR2_ENABLE_CLC | CR2_PLL_REF_SELECT |
    622 	    CR2_PIXEL_CLOCK_SELECT | CR2_ENABLE_RGB_OUTPUT);
    623 
    624 	/* now set up some window attributes */
    625 
    626 	/*
    627 	 * direct colour, 24 bit, transparency off, LUT from 0x100
    628 	 * we need to use direct colour and a linear LUT because for some
    629 	 * reason true color mode gives messed up colours
    630 	 */
    631 	agten_write_idx(sc, IBM561_FB_WINTYPE);
    632 	agten_write_dac_10(sc, IBM561_CMD_FB_WAT, 0x134);
    633 
    634 	/* use gamma LUTs, no crosshair, 0 is transparent */
    635 	agten_write_idx(sc, IBM561_AUXFB_WINTYPE);
    636 	agten_write_dac(sc, IBM561_CMD_FB_WAT, 0x0);
    637 
    638 	/* overlay is 8 bit, opaque */
    639 	agten_write_idx(sc, IBM561_OL_WINTYPE);
    640 	agten_write_dac_10(sc, IBM561_CMD_FB_WAT, 0x00);
    641 
    642 	/* now we fill the WID fb with zeroes */
    643 	src = 0;
    644 	srcw = sc->sc_width << 16 | sc->sc_height;
    645 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, FOREGROUND_COLOR,
    646 	    0x0);
    647 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, BACKGROUND_COLOR,
    648 	    0x0);
    649 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, RASTER_OP, ROP_PAT);
    650 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, COORD_INDEX, 0);
    651 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, RECT_RTW_XY, src);
    652 	bus_space_write_4(sc->sc_bustag, sc->sc_p9100_regh, RECT_RTW_XY, srcw);
    653 	junk = bus_space_read_4(sc->sc_bustag, sc->sc_p9100_regh, COMMAND_QUAD);
    654 
    655 	/* initialize the cursor registers */
    656 
    657 	/* initialize the Imagine 128 */
    658 	i128_init(sc->sc_bustag, sc->sc_i128_regh, sc->sc_stride, 8);
    659 }
    660 
    661 static void
    662 agten_gfx(struct agten_softc *sc)
    663 {
    664 	/* enable overlay transparency on colour 0x00 */
    665 	agten_write_idx(sc, IBM561_OL_WINTYPE);
    666 	agten_write_dac_10(sc, IBM561_CMD_FB_WAT, 0x01);
    667 
    668 	/* then blit the overlay full of 0x00 */
    669 	i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, 0, 0, sc->sc_width,
    670 	    sc->sc_height, 0);
    671 
    672 	/* ... so we can see the 24bit framebuffer */
    673 }
    674 
    675 static void
    676 agten_set_video(struct agten_softc *sc, int flag)
    677 {
    678 	uint8_t reg =
    679 	    CR2_ENABLE_CLC | CR2_PLL_REF_SELECT | CR2_PIXEL_CLOCK_SELECT;
    680 
    681 	if (flag == sc->sc_video)
    682 		return;
    683 
    684 	agten_write_idx(sc, IBM561_CONFIG_REG2);
    685 	agten_write_dac(sc, IBM561_CMD, flag ? reg | CR2_ENABLE_RGB_OUTPUT :
    686 	    reg);
    687 
    688 	sc->sc_video = flag;
    689 }
    690 
    691 static int
    692 agten_get_video(struct agten_softc *sc)
    693 {
    694 
    695 	return sc->sc_video;
    696 }
    697 
    698 static void
    699 agten_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    700 {
    701 	struct rasops_info *ri = cookie;
    702 	struct vcons_screen *scr = ri->ri_hw;
    703 	struct agten_softc *sc = scr->scr_cookie;
    704 	int32_t xs, xd, y, width, height;
    705 
    706 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    707 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    708 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    709 	width = ri->ri_font->fontwidth * ncols;
    710 	height = ri->ri_font->fontheight;
    711 	i128_bitblt(sc->sc_bustag, sc->sc_i128_regh, xs, y, xd, y, width,
    712 	    height, CR_COPY);
    713 }
    714 
    715 static void
    716 agten_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    717 {
    718 	struct rasops_info *ri = cookie;
    719 	struct vcons_screen *scr = ri->ri_hw;
    720 	struct agten_softc *sc = scr->scr_cookie;
    721 	int32_t x, y, width, height, bg;
    722 
    723 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    724 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    725 	width = ri->ri_font->fontwidth * ncols;
    726 	height = ri->ri_font->fontheight;
    727 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
    728 	i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, x, y, width, height, bg);
    729 }
    730 
    731 static void
    732 agten_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    733 {
    734 	struct rasops_info *ri = cookie;
    735 	struct vcons_screen *scr = ri->ri_hw;
    736 	struct agten_softc *sc = scr->scr_cookie;
    737 	int32_t x, ys, yd, width, height;
    738 
    739 	x = ri->ri_xorigin;
    740 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    741 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    742 	width = ri->ri_emuwidth;
    743 	height = ri->ri_font->fontheight * nrows;
    744 	i128_bitblt(sc->sc_bustag, sc->sc_i128_regh, x, ys, x, yd, width,
    745 	    height, CR_COPY);
    746 }
    747 
    748 static void
    749 agten_eraserows(void *cookie, int row, int nrows, long fillattr)
    750 {
    751 	struct rasops_info *ri = cookie;
    752 	struct vcons_screen *scr = ri->ri_hw;
    753 	struct agten_softc *sc = scr->scr_cookie;
    754 	int32_t x, y, width, height, bg;
    755 
    756 	if ((row == 0) && (nrows == ri->ri_rows)) {
    757 		x = y = 0;
    758 		width = ri->ri_width;
    759 		height = ri->ri_height;
    760 	} else {
    761 		x = ri->ri_xorigin;
    762 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    763 		width = ri->ri_emuwidth;
    764 		height = ri->ri_font->fontheight * nrows;
    765 	}
    766 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
    767 	i128_rectfill(sc->sc_bustag, sc->sc_i128_regh, x, y, width, height, bg);
    768 }
    769 
    770 static void
    771 agten_move_cursor(struct agten_softc *sc, int x, int y)
    772 {
    773 
    774 	sc->sc_cursor_x = x;
    775 	sc->sc_cursor_y = y;
    776 	agten_write_idx(sc, IBM561_CURSOR_X_REG);
    777 	agten_write_dac(sc, IBM561_CMD, x & 0xff);
    778 	agten_write_dac(sc, IBM561_CMD, (x >> 8) & 0xff);
    779 	agten_write_dac(sc, IBM561_CMD, y & 0xff);
    780 	agten_write_dac(sc, IBM561_CMD, (y >> 8) & 0xff);
    781 }
    782 
    783 static int
    784 agten_do_cursor(struct agten_softc *sc, struct wsdisplay_cursor *cur)
    785 {
    786 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    787 
    788 		agten_write_idx(sc, IBM561_CURS_CNTL_REG);
    789 		agten_write_dac(sc, IBM561_CMD, cur->enable ?
    790 		    CURS_ENABLE : 0);
    791 	}
    792 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    793 
    794 		agten_write_idx(sc, IBM561_HOTSPOT_X_REG);
    795 		agten_write_dac(sc, IBM561_CMD, cur->hot.x);
    796 		agten_write_dac(sc, IBM561_CMD, cur->hot.y);
    797 	}
    798 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    799 
    800 		agten_move_cursor(sc, cur->pos.x, cur->pos.y);
    801 	}
    802 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    803 		int i;
    804 
    805 		agten_write_idx(sc, IBM561_CURSOR_LUT + cur->cmap.index + 2);
    806 		for (i = 0; i < cur->cmap.count; i++) {
    807 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.red[i]);
    808 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.green[i]);
    809 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.blue[i]);
    810 		}
    811 	}
    812 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    813 		int i;
    814 		uint16_t tmp;
    815 
    816 		agten_write_idx(sc, IBM561_CURSOR_BITMAP);
    817 		for (i = 0; i < 512; i++) {
    818 			tmp = util_interleave(cur->mask[i], cur->image[i]);
    819 			agten_write_dac(sc, IBM561_CMD, (tmp >> 8) & 0xff);
    820 			agten_write_dac(sc, IBM561_CMD, tmp & 0xff);
    821 		}
    822 	}
    823 	return 0;
    824 }
    825 
    826 static int
    827 agten_do_sun_cursor(struct agten_softc *sc, struct fbcursor *cur)
    828 {
    829 	if (cur->set & FB_CUR_SETCUR) {
    830 
    831 		agten_write_idx(sc, IBM561_CURS_CNTL_REG);
    832 		agten_write_dac(sc, IBM561_CMD, cur->enable ?
    833 		    CURS_ENABLE : 0);
    834 	}
    835 	if (cur->set & FB_CUR_SETHOT) {
    836 
    837 		agten_write_idx(sc, IBM561_HOTSPOT_X_REG);
    838 		agten_write_dac(sc, IBM561_CMD, cur->hot.x);
    839 		agten_write_dac(sc, IBM561_CMD, cur->hot.y);
    840 	}
    841 	if (cur->set & FB_CUR_SETPOS) {
    842 
    843 		agten_move_cursor(sc, cur->pos.x, cur->pos.y);
    844 	}
    845 	if (cur->set & FB_CUR_SETCMAP) {
    846 		int i;
    847 
    848 		agten_write_idx(sc, IBM561_CURSOR_LUT + cur->cmap.index + 2);
    849 		for (i = 0; i < cur->cmap.count; i++) {
    850 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.red[i]);
    851 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.green[i]);
    852 			agten_write_dac(sc, IBM561_CMD_CMAP, cur->cmap.blue[i]);
    853 		}
    854 	}
    855 	if (cur->set & FB_CUR_SETSHAPE) {
    856 		int i;
    857 		uint16_t tmp;
    858 
    859 		agten_write_idx(sc, IBM561_CURSOR_BITMAP);
    860 		for (i = 0; i < 512; i++) {
    861 			tmp = util_interleave_lin(cur->mask[i], cur->image[i]);
    862 			agten_write_dac(sc, IBM561_CMD, (tmp >> 8) & 0xff);
    863 			agten_write_dac(sc, IBM561_CMD, tmp & 0xff);
    864 		}
    865 	}
    866 	return 0;
    867 }
    868 
    869 uint16_t
    870 util_interleave(uint8_t b1, uint8_t b2)
    871 {
    872 	int i;
    873 	uint16_t ret = 0;
    874 	uint16_t mask = 0x8000;
    875 	uint8_t mask8 = 0x01;
    876 
    877 	for (i = 0; i < 8; i++) {
    878 		if (b1 & mask8)
    879 			ret |= mask;
    880 		mask = mask >> 1;
    881 		if (b2 & mask8)
    882 			ret |= mask;
    883 		mask = mask >> 1;
    884 		mask8 = mask8 << 1;
    885 	}
    886 	return ret;
    887 }
    888 
    889 uint16_t
    890 util_interleave_lin(uint8_t b1, uint8_t b2)
    891 {
    892 	int i;
    893 	uint16_t ret = 0;
    894 	uint16_t mask = 0x8000;
    895 	uint8_t mask8 = 0x80;
    896 
    897 	for (i = 0; i < 8; i++) {
    898 		if (b1 & mask8)
    899 			ret |= mask;
    900 		mask = mask >> 1;
    901 		if (b2 & mask8)
    902 			ret |= mask;
    903 		mask = mask >> 1;
    904 		mask8 = mask8 >> 1;
    905 	}
    906 	return ret;
    907 }
    908 
    909 /* and now the /dev/fb* stuff */
    910 static void
    911 agten_fb_unblank(struct device *dev)
    912 {
    913 	struct agten_softc *sc = (void *)dev;
    914 
    915 	agten_init(sc);
    916 	agten_set_video(sc, 1);
    917 }
    918 
    919 static int
    920 agten_fb_open(dev_t dev, int flags, int mode, struct lwp *l)
    921 {
    922 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
    923 	int unit = minor(dev);
    924 
    925 	if (unit >= agten_cd.cd_ndevs || agten_cd.cd_devs[unit] == NULL)
    926 		return (ENXIO);
    927 	if (sc->sc_fb_is_open)
    928 		return 0;
    929 
    930 	sc->sc_fb_is_open++;
    931 	agten_gfx(sc);
    932 
    933 	return (0);
    934 }
    935 
    936 static int
    937 agten_fb_close(dev_t dev, int flags, int mode, struct lwp *l)
    938 {
    939 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
    940 
    941 	sc->sc_fb_is_open--;
    942 	if (sc->sc_fb_is_open < 0)
    943 		sc->sc_fb_is_open = 0;
    944 
    945 	if (sc->sc_fb_is_open == 0) {
    946 		agten_init(sc);
    947 		vcons_redraw_screen(sc->vd.active);
    948 	}
    949 
    950 	return (0);
    951 }
    952 
    953 static int
    954 agten_fb_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    955 {
    956 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
    957 	struct fbgattr *fba;
    958 	int error;
    959 
    960 	switch (cmd) {
    961 
    962 	case FBIOGTYPE:
    963 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    964 		break;
    965 
    966 	case FBIOGATTR:
    967 		fba = (struct fbgattr *)data;
    968 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    969 		fba->owner = 0;		/* XXX ??? */
    970 		fba->fbtype = sc->sc_fb.fb_type;
    971 		fba->sattr.flags = 0;
    972 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    973 		fba->sattr.dev_specific[0] = -1;
    974 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    975 		fba->emu_types[1] = -1;
    976 		break;
    977 
    978 	case FBIOGETCMAP:
    979 #define p ((struct fbcmap *)data)
    980 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    981 
    982 	case FBIOPUTCMAP:
    983 		/* copy to software map */
    984 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    985 		if (error)
    986 			return (error);
    987 		/* now blast them into the chip */
    988 		/* don't bother - we're 24bit */
    989 #undef p
    990 		break;
    991 
    992 	case FBIOGVIDEO:
    993 		*(int *)data = agten_get_video(sc);
    994 		break;
    995 
    996 	case FBIOSVIDEO:
    997 		agten_set_video(sc, *(int *)data);
    998 		break;
    999 
   1000 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
   1001 #define p ((struct fbcursor *)data)
   1002 #define pc (&sc->sc_cursor)
   1003 
   1004 	case FBIOGCURSOR:
   1005 		/* does anyone use this ioctl?! */
   1006 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
   1007 		p->enable = 1;
   1008 		p->pos.x = sc->sc_cursor_x;
   1009 		p->pos.y = sc->sc_cursor_y;
   1010 		p->size.x = 64;
   1011 		p->size.y = 64;
   1012 		break;
   1013 
   1014 	case FBIOSCURSOR:
   1015 		agten_do_sun_cursor(sc, p);
   1016 	break;
   1017 
   1018 #undef p
   1019 #undef cc
   1020 
   1021 	case FBIOGCURPOS:
   1022 	{
   1023 		struct fbcurpos *cp = (struct fbcurpos *)data;
   1024 		cp->x = sc->sc_cursor_x;
   1025 		cp->y = sc->sc_cursor_y;
   1026 	}
   1027 	break;
   1028 
   1029 	case FBIOSCURPOS:
   1030 	{
   1031 		struct fbcurpos *cp = (struct fbcurpos *)data;
   1032 		agten_move_cursor(sc, cp->x, cp->y);
   1033 	}
   1034 	break;
   1035 
   1036 	case FBIOGCURMAX:
   1037 		/* max cursor size is 64x64 */
   1038 		((struct fbcurpos *)data)->x = 64;
   1039 		((struct fbcurpos *)data)->y = 64;
   1040 		break;
   1041 
   1042 	default:
   1043 		return (ENOTTY);
   1044 	}
   1045 	return (0);
   1046 }
   1047 
   1048 static paddr_t
   1049 agten_fb_mmap(dev_t dev, off_t off, int prot)
   1050 {
   1051 	struct agten_softc *sc = agten_cd.cd_devs[minor(dev)];
   1052 
   1053 	/*
   1054 	 * mappings are subject to change
   1055 	 * for now we put the framebuffer at offset 0 and the GLint registers
   1056 	 * right after that. We may want to expose more register ranges and
   1057 	 * probably will want to map the 2nd framebuffer as well
   1058 	 */
   1059 
   1060 	if (off < 0)
   1061 		return EINVAL;
   1062 
   1063 	if (off >= sc->sc_glint_fbsz + 0x10000)
   1064 		return EINVAL;
   1065 
   1066 	if (off < sc->sc_glint_fbsz) {
   1067 		return (bus_space_mmap(sc->sc_bustag,
   1068 			sc->sc_glint_fb,
   1069 			off,
   1070 			prot,
   1071 			BUS_SPACE_MAP_LINEAR));
   1072 	}
   1073 
   1074 	off -= sc->sc_glint_fbsz;
   1075 	if (off < 0x10000) {
   1076 		return (bus_space_mmap(sc->sc_bustag,
   1077 			sc->sc_glint_regs,
   1078 			off,
   1079 			prot,
   1080 			BUS_SPACE_MAP_LINEAR));
   1081 	}
   1082 	return EINVAL;
   1083 }
   1084