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