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