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