xref: /netbsd-src/sys/dev/sbus/p9100.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: p9100.c,v 1.33 2006/05/28 13:17:02 blymn Exp $ */
2 
3 /*-
4  * Copyright (c) 1998, 2005, 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matt Thomas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * color display (p9100) driver.
41  *
42  * Does not handle interrupts, even though they can occur.
43  *
44  * XXX should defer colormap updates to vertical retrace interrupts
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: p9100.c,v 1.33 2006/05/28 13:17:02 blymn Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/buf.h>
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #include <sys/malloc.h>
56 #include <sys/mman.h>
57 #include <sys/tty.h>
58 #include <sys/conf.h>
59 
60 #include <machine/bus.h>
61 #include <machine/autoconf.h>
62 
63 #include <dev/sun/fbio.h>
64 #include <dev/sun/fbvar.h>
65 #include <dev/sun/btreg.h>
66 #include <dev/sun/btvar.h>
67 
68 #include <dev/sbus/p9100reg.h>
69 
70 #include <dev/sbus/sbusvar.h>
71 
72 /*#include <dev/wscons/wsdisplayvar.h>*/
73 #include <dev/wscons/wsconsio.h>
74 #include <dev/wsfont/wsfont.h>
75 #include <dev/rasops/rasops.h>
76 
77 #include <dev/wscons/wsdisplay_vconsvar.h>
78 
79 #include "opt_wsemul.h"
80 #include "rasops_glue.h"
81 
82 #include "tctrl.h"
83 #if NTCTRL > 0
84 #include <machine/tctrl.h>
85 #include <sparc/dev/tctrlvar.h>/*XXX*/
86 #endif
87 
88 struct pnozz_cursor {
89 	short	pc_enable;		/* cursor is enabled */
90 	struct	fbcurpos pc_pos;	/* position */
91 	struct	fbcurpos pc_hot;	/* hot-spot */
92 	struct	fbcurpos pc_size;	/* size of mask & image fields */
93 	uint32_t pc_bits[0x100];	/* space for mask & image bits */
94 	unsigned char red[3], green[3];
95 	unsigned char blue[3];		/* cursor palette */
96 };
97 
98 /* per-display variables */
99 struct p9100_softc {
100 	struct device	sc_dev;		/* base device */
101 	struct sbusdev	sc_sd;		/* sbus device */
102 	struct fbdevice	sc_fb;		/* frame buffer device */
103 
104 	bus_space_tag_t	sc_bustag;
105 
106 	bus_addr_t	sc_ctl_paddr;	/* phys address description */
107 	bus_size_t	sc_ctl_psize;	/*   for device mmap() */
108 	bus_space_handle_t sc_ctl_memh;	/*   bus space handle */
109 
110 	bus_addr_t	sc_cmd_paddr;	/* phys address description */
111 	bus_size_t	sc_cmd_psize;	/*   for device mmap() */
112 	bus_space_handle_t sc_cmd_memh;	/*   bus space handle */
113 
114 	bus_addr_t	sc_fb_paddr;	/* phys address description */
115 	bus_size_t	sc_fb_psize;	/*   for device mmap() */
116 	bus_space_handle_t sc_fb_memh;	/*   bus space handle */
117 
118 	volatile uint32_t sc_junk;
119 	uint32_t sc_mono_width;	/* for setup_mono */
120 
121 	uint32_t sc_width;
122 	uint32_t sc_height;	/* panel width / height */
123 	uint32_t sc_stride;
124 	uint32_t sc_depth;
125 	union	bt_cmap sc_cmap;	/* Brooktree color map */
126 
127 	struct pnozz_cursor sc_cursor;
128 
129 	int sc_mode;
130 	int sc_video, sc_powerstate;
131 	uint32_t sc_bg;
132 	volatile uint32_t sc_last_offset;
133 	struct vcons_data vd;
134 };
135 
136 
137 static struct vcons_screen p9100_console_screen;
138 
139 extern const u_char rasops_cmap[768];
140 
141 struct wsscreen_descr p9100_defscreendesc = {
142 	"default",
143 	0, 0,
144 	NULL,
145 	8, 16,
146 	WSSCREEN_WSCOLORS,
147 };
148 
149 const struct wsscreen_descr *_p9100_scrlist[] = {
150 	&p9100_defscreendesc,
151 	/* XXX other formats, graphics screen? */
152 };
153 
154 struct wsscreen_list p9100_screenlist = {
155 	sizeof(_p9100_scrlist) / sizeof(struct wsscreen_descr *), _p9100_scrlist
156 };
157 
158 /* autoconfiguration driver */
159 static int	p9100_sbus_match(struct device *, struct cfdata *, void *);
160 static void	p9100_sbus_attach(struct device *, struct device *, void *);
161 
162 static void	p9100unblank(struct device *);
163 static void	p9100_shutdown(void *);
164 
165 CFATTACH_DECL(pnozz, sizeof(struct p9100_softc),
166     p9100_sbus_match, p9100_sbus_attach, NULL, NULL);
167 
168 extern struct cfdriver pnozz_cd;
169 
170 static dev_type_open(p9100open);
171 static dev_type_ioctl(p9100ioctl);
172 static dev_type_mmap(p9100mmap);
173 
174 const struct cdevsw pnozz_cdevsw = {
175 	p9100open, nullclose, noread, nowrite, p9100ioctl,
176 	nostop, notty, nopoll, p9100mmap, nokqfilter,
177 };
178 
179 /* frame buffer generic driver */
180 static struct fbdriver p9100fbdriver = {
181 	p9100unblank, p9100open, nullclose, p9100ioctl, nopoll,
182 	p9100mmap, nokqfilter
183 };
184 
185 static void	p9100loadcmap(struct p9100_softc *, int, int);
186 static void	p9100_set_video(struct p9100_softc *, int);
187 static int	p9100_get_video(struct p9100_softc *);
188 static uint32_t p9100_ctl_read_4(struct p9100_softc *, bus_size_t);
189 static void	p9100_ctl_write_4(struct p9100_softc *, bus_size_t, uint32_t);
190 static uint8_t	p9100_ramdac_read(struct p9100_softc *, bus_size_t);
191 static void	p9100_ramdac_write(struct p9100_softc *, bus_size_t, uint8_t);
192 
193 static uint8_t	p9100_ramdac_read_ctl(struct p9100_softc *, int);
194 static void	p9100_ramdac_write_ctl(struct p9100_softc *, int, uint8_t);
195 
196 static void 	p9100_init_engine(struct p9100_softc *);
197 
198 #if NWSDISPLAY > 0
199 static void	p9100_sync(struct p9100_softc *);
200 static void	p9100_bitblt(void *, int, int, int, int, int, int, uint32_t);
201 static void 	p9100_rectfill(void *, int, int, int, int, uint32_t);
202 static void	p9100_clearscreen(struct p9100_softc *);
203 
204 static void	p9100_setup_mono(struct p9100_softc *, int, int, int, int,
205 		    uint32_t, uint32_t);
206 static void	p9100_feed_line(struct p9100_softc *, int, uint8_t *);
207 static void	p9100_set_color_reg(struct p9100_softc *, int, int32_t);
208 
209 static void	p9100_copycols(void *, int, int, int, int);
210 static void	p9100_erasecols(void *, int, int, int, long);
211 static void	p9100_copyrows(void *, int, int, int);
212 static void	p9100_eraserows(void *, int, int, long);
213 /*static int	p9100_mapchar(void *, int, u_int *);*/
214 static void	p9100_putchar(void *, int, int, u_int, long);
215 static void	p9100_cursor(void *, int, int, int);
216 static int	p9100_allocattr(void *, int, int, int, long *);
217 
218 /*static void	p9100_scroll(void *, void *, int);*/
219 
220 static int	p9100_putcmap(struct p9100_softc *, struct wsdisplay_cmap *);
221 static int 	p9100_getcmap(struct p9100_softc *, struct wsdisplay_cmap *);
222 static int	p9100_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
223 static paddr_t	p9100_mmap(void *, void *, off_t, int);
224 
225 /*static int	p9100_load_font(void *, void *, struct wsdisplay_font *);*/
226 
227 static void	p9100_init_screen(void *, struct vcons_screen *, int,
228 	    long *);
229 #endif
230 
231 static void	p9100_init_cursor(struct p9100_softc *);
232 
233 static void	p9100_set_fbcursor(struct p9100_softc *);
234 static void	p9100_setcursorcmap(struct p9100_softc *);
235 static void	p9100_loadcursor(struct p9100_softc *);
236 
237 static int	p9100_intr(void *);
238 
239 /* power management stuff */
240 static void p9100_power_hook(int, void *);
241 
242 static void p9100_set_extvga(void *, int);
243 
244 #if NWSDISPLAY > 0
245 struct wsdisplay_accessops p9100_accessops = {
246 	p9100_ioctl,
247 	p9100_mmap,
248 	NULL,	/* vcons_alloc_screen */
249 	NULL,	/* vcons_free_screen */
250 	NULL,	/* vcons_show_screen */
251 	NULL,	/* load_font */
252 	NULL,	/* polls */
253 	NULL,	/* scroll */
254 };
255 #endif
256 
257 #define PNOZZ_LATCH(sc, off) if(sc->sc_last_offset == (off & 0xffffff80)) { \
258 		sc->sc_junk = bus_space_read_4(sc->sc_bustag, sc->sc_fb_memh, \
259 		    off); \
260 		sc->sc_last_offset = off & 0xffffff80; }
261 
262 /*
263  * Match a p9100.
264  */
265 static int
266 p9100_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
267 {
268 	struct sbus_attach_args *sa = aux;
269 
270 	return (strcmp("p9100", sa->sa_name) == 0);
271 }
272 
273 
274 /*
275  * Attach a display.  We need to notice if it is the console, too.
276  */
277 static void
278 p9100_sbus_attach(struct device *parent, struct device *self, void *args)
279 {
280 	struct p9100_softc *sc = (struct p9100_softc *)self;
281 	struct sbus_attach_args *sa = args;
282 	struct fbdevice *fb = &sc->sc_fb;
283 	int isconsole;
284 	int node;
285 	int i, j;
286 	uint8_t ver;
287 
288 #if NWSDISPLAY > 0
289 	struct wsemuldisplaydev_attach_args aa;
290 	struct rasops_info *ri;
291 	unsigned long defattr;
292 #endif
293 
294 	sc->sc_last_offset = 0xffffffff;
295 
296 	/* Remember cookies for p9100_mmap() */
297 	sc->sc_bustag = sa->sa_bustag;
298 	sc->sc_ctl_paddr = sbus_bus_addr(sa->sa_bustag,
299 		sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base);
300 	sc->sc_ctl_psize = 0x8000;/*(bus_size_t)sa->sa_reg[0].oa_size;*/
301 
302 	sc->sc_cmd_paddr = sbus_bus_addr(sa->sa_bustag,
303 		sa->sa_reg[1].oa_space, sa->sa_reg[1].oa_base);
304 	sc->sc_cmd_psize = (bus_size_t)sa->sa_reg[1].oa_size;
305 
306 	sc->sc_fb_paddr = sbus_bus_addr(sa->sa_bustag,
307 		sa->sa_reg[2].oa_space, sa->sa_reg[2].oa_base);
308 	sc->sc_fb_psize = (bus_size_t)sa->sa_reg[2].oa_size;
309 
310 	fb->fb_driver = &p9100fbdriver;
311 	fb->fb_device = &sc->sc_dev;
312 	fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
313 #ifdef PNOZZ_EMUL_CG3
314 	fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
315 #else
316 	fb->fb_type.fb_type = FBTYPE_P9100;
317 #endif
318 	fb->fb_pixels = NULL;
319 
320 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
321 
322 	node = sa->sa_node;
323 	isconsole = fb_is_console(node);
324 	if (!isconsole) {
325 		printf("\n%s: fatal error: PROM didn't configure device\n",
326 		    self->dv_xname);
327 		return;
328 	}
329 
330 	/*
331 	 * When the ROM has mapped in a p9100 display, the address
332 	 * maps only the video RAM, so in any case we have to map the
333 	 * registers ourselves.  We only need the video RAM if we are
334 	 * going to print characters via rconsole.
335 	 */
336 	if (sbus_bus_map(sc->sc_bustag,
337 			 sa->sa_reg[0].oa_space,
338 			 sa->sa_reg[0].oa_base,
339 			 /*
340 			  * XXX for some reason the SBus resources don't cover
341 			  * all registers, so we just map what we need
342 			  */
343 			 /*sc->sc_ctl_psize*/ 0x8000,
344 			 /*BUS_SPACE_MAP_LINEAR*/0, &sc->sc_ctl_memh) != 0) {
345 		printf("%s: cannot map control registers\n", self->dv_xname);
346 		return;
347 	}
348 
349 	if (sa->sa_npromvaddrs != 0)
350 		fb->fb_pixels = (caddr_t)sa->sa_promvaddrs[0];
351 
352 	if (fb->fb_pixels == NULL) {
353 		if (sbus_bus_map(sc->sc_bustag,
354 				sa->sa_reg[2].oa_space,
355 				sa->sa_reg[2].oa_base,
356 				sc->sc_fb_psize,
357 				BUS_SPACE_MAP_LINEAR, &sc->sc_fb_memh) != 0) {
358 			printf("%s: cannot map framebuffer\n", self->dv_xname);
359 			return;
360 		}
361 		fb->fb_pixels = (char *)sc->sc_fb_memh;
362 	} else {
363 		sc->sc_fb_memh = (bus_space_handle_t) fb->fb_pixels;
364 	}
365 
366 	i = p9100_ctl_read_4(sc, 0x0004);
367 	switch ((i >> 26) & 7) {
368 	    case 5: fb->fb_type.fb_depth = 32; break;
369 	    case 7: fb->fb_type.fb_depth = 24; break;
370 	    case 3: fb->fb_type.fb_depth = 16; break;
371 	    case 2: fb->fb_type.fb_depth = 8; break;
372 	    default: {
373 		panic("pnozz: can't determine screen depth (0x%02x)", i);
374 	    }
375 	}
376 	sc->sc_depth = (fb->fb_type.fb_depth >> 3);
377 
378 	/* XXX for some reason I get a kernel trap with this */
379 	sc->sc_width = prom_getpropint(node, "width", 800);
380 	sc->sc_height = prom_getpropint(node, "height", 600);
381 
382 	sc->sc_stride = prom_getpropint(node, "linebytes", sc->sc_width *
383 	    (fb->fb_type.fb_depth >> 3));
384 
385 	/* check the RAMDAC */
386 	ver = p9100_ramdac_read_ctl(sc, DAC_VERSION);
387 
388 	p9100_init_engine(sc);
389 
390 	fb_setsize_obp(fb, fb->fb_type.fb_depth, sc->sc_width, sc->sc_height,
391 	    node);
392 
393 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
394 	bus_intr_establish(sc->sc_bustag, sa->sa_pri, IPL_BIO,
395 	    p9100_intr, sc);
396 
397 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
398 	printf(": rev %d / %x, %dx%d, depth %d mem %x",
399 	       (i & 7), ver, fb->fb_type.fb_width, fb->fb_type.fb_height,
400 	       fb->fb_type.fb_depth, (unsigned int)sc->sc_fb_psize);
401 
402 	fb->fb_type.fb_cmsize = prom_getpropint(node, "cmsize", 256);
403 	if ((1 << fb->fb_type.fb_depth) != fb->fb_type.fb_cmsize)
404 		printf(", %d entry colormap", fb->fb_type.fb_cmsize);
405 
406 	/* Initialize the default color map. */
407 	/*bt_initcmap(&sc->sc_cmap, 256);*/
408 	j = 0;
409 	for (i = 0; i < 256; i++) {
410 		sc->sc_cmap.cm_map[i][0] = rasops_cmap[j];
411 		j++;
412 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j];
413 		j++;
414 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j];
415 		j++;
416 	}
417 	p9100loadcmap(sc, 0, 256);
418 
419 	/* make sure we are not blanked */
420 	if (isconsole)
421 		p9100_set_video(sc, 1);
422 
423 	if (shutdownhook_establish(p9100_shutdown, sc) == NULL) {
424 		panic("%s: could not establish shutdown hook",
425 		      sc->sc_dev.dv_xname);
426 	}
427 
428 	if (isconsole) {
429 		printf(" (console)\n");
430 #ifdef RASTERCONSOLE
431 		/*p9100loadcmap(sc, 255, 1);*/
432 		fbrcons_init(fb);
433 #endif
434 	} else
435 		printf("\n");
436 
437 #if NWSDISPLAY > 0
438 	wsfont_init();
439 
440 	vcons_init(&sc->vd, sc, &p9100_defscreendesc, &p9100_accessops);
441 	sc->vd.init_screen = p9100_init_screen;
442 
443 	vcons_init_screen(&sc->vd, &p9100_console_screen, 1, &defattr);
444 	p9100_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
445 
446 	sc->sc_bg = (defattr >> 16) & 0xff;
447 	p9100_clearscreen(sc);
448 
449 	ri = &p9100_console_screen.scr_ri;
450 
451 	p9100_defscreendesc.nrows = ri->ri_rows;
452 	p9100_defscreendesc.ncols = ri->ri_cols;
453 	p9100_defscreendesc.textops = &ri->ri_ops;
454 	p9100_defscreendesc.capabilities = ri->ri_caps;
455 
456 	if(isconsole) {
457 		wsdisplay_cnattach(&p9100_defscreendesc, ri, 0, 0, defattr);
458 	}
459 
460 	aa.console = isconsole;
461 	aa.scrdata = &p9100_screenlist;
462 	aa.accessops = &p9100_accessops;
463 	aa.accesscookie = &sc->vd;
464 
465 	config_found(self, &aa, wsemuldisplaydevprint);
466 #endif
467 	/* cursor sprite handling */
468 	p9100_init_cursor(sc);
469 
470 	/* attach the fb */
471 	fb_attach(fb, isconsole);
472 
473 	/* register with power management */
474 	sc->sc_video = 1;
475 	sc->sc_powerstate = PWR_RESUME;
476 	powerhook_establish(p9100_power_hook, sc);
477 
478 #if NTCTRL > 0
479 	/* register callback for external monitor status change */
480 	tadpole_register_callback(p9100_set_extvga, sc);
481 #endif
482 }
483 
484 static void
485 p9100_shutdown(arg)
486 	void *arg;
487 {
488 	struct p9100_softc *sc = arg;
489 
490 #ifdef RASTERCONSOLE
491 	sc->sc_cmap.cm_map[0][0] = 0xff;
492 	sc->sc_cmap.cm_map[0][1] = 0xff;
493 	sc->sc_cmap.cm_map[0][2] = 0xff;
494 	sc->sc_cmap.cm_map[1][0] = 0;
495 	sc->sc_cmap.cm_map[1][1] = 0;
496 	sc->sc_cmap.cm_map[1][2] = 0x00;
497 	p9100loadcmap(sc, 0, 2);
498 	sc->sc_cmap.cm_map[255][0] = 0;
499 	sc->sc_cmap.cm_map[255][1] = 0;
500 	sc->sc_cmap.cm_map[255][2] = 0;
501 	p9100loadcmap(sc, 255, 1);
502 #endif
503 	p9100_set_video(sc, 1);
504 }
505 
506 int
507 p9100open(dev_t dev, int flags, int mode, struct lwp *l)
508 {
509 	int unit = minor(dev);
510 
511 	if (unit >= pnozz_cd.cd_ndevs || pnozz_cd.cd_devs[unit] == NULL)
512 		return (ENXIO);
513 	return (0);
514 }
515 
516 int
517 p9100ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
518 {
519 	struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
520 	struct fbgattr *fba;
521 	int error, v;
522 
523 	switch (cmd) {
524 
525 	case FBIOGTYPE:
526 		*(struct fbtype *)data = sc->sc_fb.fb_type;
527 		break;
528 
529 	case FBIOGATTR:
530 		fba = (struct fbgattr *)data;
531 		fba->real_type = sc->sc_fb.fb_type.fb_type;
532 		fba->owner = 0;		/* XXX ??? */
533 		fba->fbtype = sc->sc_fb.fb_type;
534 		fba->sattr.flags = 0;
535 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
536 		fba->sattr.dev_specific[0] = -1;
537 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
538 		fba->emu_types[1] = -1;
539 		break;
540 
541 	case FBIOGETCMAP:
542 #define p ((struct fbcmap *)data)
543 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
544 
545 	case FBIOPUTCMAP:
546 		/* copy to software map */
547 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
548 		if (error)
549 			return (error);
550 		/* now blast them into the chip */
551 		/* XXX should use retrace interrupt */
552 		p9100loadcmap(sc, p->index, p->count);
553 #undef p
554 		break;
555 
556 	case FBIOGVIDEO:
557 		*(int *)data = p9100_get_video(sc);
558 		break;
559 
560 	case FBIOSVIDEO:
561 		p9100_set_video(sc, *(int *)data);
562 		break;
563 
564 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
565 #define p ((struct fbcursor *)data)
566 #define pc (&sc->sc_cursor)
567 
568 	case FBIOGCURSOR:
569 		p->set = FB_CUR_SETALL;	/* close enough, anyway */
570 		p->enable = pc->pc_enable;
571 		p->pos = pc->pc_pos;
572 		p->hot = pc->pc_hot;
573 		p->size = pc->pc_size;
574 
575 		if (p->image != NULL) {
576 			error = copyout(pc->pc_bits, p->image, 0x200);
577 			if (error)
578 				return error;
579 			error = copyout(&pc->pc_bits[0x80], p->mask, 0x200);
580 			if (error)
581 				return error;
582 		}
583 
584 		p->cmap.index = 0;
585 		p->cmap.count = 3;
586 		if (p->cmap.red != NULL) {
587 			copyout(pc->red, p->cmap.red, 3);
588 			copyout(pc->green, p->cmap.green, 3);
589 			copyout(pc->blue, p->cmap.blue, 3);
590 		}
591 		break;
592 
593 	case FBIOSCURSOR:
594 	{
595 		int count;
596 		uint32_t image[0x80], mask[0x80];
597 		uint8_t red[3], green[3], blue[3];
598 
599 		v = p->set;
600 		if (v & FB_CUR_SETCMAP) {
601 			error = copyin(p->cmap.red, red, 3);
602 			error |= copyin(p->cmap.green, green, 3);
603 			error |= copyin(p->cmap.blue, blue, 3);
604 			if (error)
605 				return error;
606 		}
607 		if (v & FB_CUR_SETSHAPE) {
608 			if (p->size.x > 64 || p->size.y > 64)
609 				return EINVAL;
610 			memset(&mask, 0, 0x200);
611 			memset(&image, 0, 0x200);
612 			count = p->size.y * 8;
613 			error = copyin(p->image, image, count);
614 			if (error)
615 				return error;
616 			error = copyin(p->mask, mask, count);
617 			if (error)
618 				return error;
619 		}
620 
621 		/* parameters are OK; do it */
622 		if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
623 			if (v & FB_CUR_SETCUR)
624 				pc->pc_enable = p->enable;
625 			if (v & FB_CUR_SETPOS)
626 				pc->pc_pos = p->pos;
627 			if (v & FB_CUR_SETHOT)
628 				pc->pc_hot = p->hot;
629 			p9100_set_fbcursor(sc);
630 		}
631 
632 		if (v & FB_CUR_SETCMAP) {
633 			memcpy(pc->red, red, 3);
634 			memcpy(pc->green, green, 3);
635 			memcpy(pc->blue, blue, 3);
636 			p9100_setcursorcmap(sc);
637 		}
638 
639 		if (v & FB_CUR_SETSHAPE) {
640 			memcpy(pc->pc_bits, image, 0x200);
641 			memcpy(&pc->pc_bits[0x80], mask, 0x200);
642 			p9100_loadcursor(sc);
643 		}
644 	}
645 	break;
646 
647 #undef p
648 #undef cc
649 
650 	case FBIOGCURPOS:
651 		*(struct fbcurpos *)data = sc->sc_cursor.pc_pos;
652 		break;
653 
654 	case FBIOSCURPOS:
655 		sc->sc_cursor.pc_pos = *(struct fbcurpos *)data;
656 		p9100_set_fbcursor(sc);
657 		break;
658 
659 	case FBIOGCURMAX:
660 		/* max cursor size is 64x64 */
661 		((struct fbcurpos *)data)->x = 64;
662 		((struct fbcurpos *)data)->y = 64;
663 		break;
664 
665 	default:
666 		return (ENOTTY);
667 	}
668 	return (0);
669 }
670 
671 static uint32_t
672 p9100_ctl_read_4(struct p9100_softc *sc, bus_size_t off)
673 {
674 
675 	PNOZZ_LATCH(sc, off);
676 	return bus_space_read_4(sc->sc_bustag, sc->sc_ctl_memh, off);
677 }
678 
679 static void
680 p9100_ctl_write_4(struct p9100_softc *sc, bus_size_t off, uint32_t v)
681 {
682 
683 	PNOZZ_LATCH(sc, off);
684 	bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off, v);
685 }
686 
687 /* initialize the drawing engine */
688 static void
689 p9100_init_engine(struct p9100_softc *sc)
690 {
691 	/* reset clipping rectangles */
692 	uint32_t rmax = ((sc->sc_width & 0x3fff) << 16) |
693 	    (sc->sc_height & 0x3fff);
694 
695 	sc->sc_last_offset = 0xffffffff;
696 
697 	p9100_ctl_write_4(sc, WINDOW_OFFSET, 0);
698 	p9100_ctl_write_4(sc, WINDOW_MIN, 0);
699 	p9100_ctl_write_4(sc, WINDOW_MAX, rmax);
700 	p9100_ctl_write_4(sc, BYTE_CLIP_MIN, 0);
701 	p9100_ctl_write_4(sc, BYTE_CLIP_MAX, rmax);
702 	p9100_ctl_write_4(sc, DRAW_MODE, 0);
703 	p9100_ctl_write_4(sc, PLANE_MASK, 0xffffffff);
704 	p9100_ctl_write_4(sc, PATTERN0, 0xffffffff);
705 	p9100_ctl_write_4(sc, PATTERN1, 0xffffffff);
706 	p9100_ctl_write_4(sc, PATTERN2, 0xffffffff);
707 	p9100_ctl_write_4(sc, PATTERN3, 0xffffffff);
708 }
709 
710 /* we only need these in the wsdisplay case */
711 #if NWSDISPLAY > 0
712 
713 /* wait until the engine is idle */
714 static void
715 p9100_sync(struct p9100_softc *sc)
716 {
717 	while((p9100_ctl_read_4(sc, ENGINE_STATUS) &
718 	    (ENGINE_BUSY | BLITTER_BUSY)) != 0);
719 }
720 
721 static void
722 p9100_set_color_reg(struct p9100_softc *sc, int reg, int32_t col)
723 {
724 	uint32_t out;
725 
726 	switch(sc->sc_depth)
727 	{
728 		case 1:	/* 8 bit */
729 			out = (col << 8) | col;
730 			out |= out << 16;
731 			break;
732 		case 2: /* 16 bit */
733 			out = col | (col << 16);
734 			break;
735 		default:
736 			out = col;
737 	}
738 	p9100_ctl_write_4(sc, reg, out);
739 }
740 
741 /* screen-to-screen blit */
742 static void
743 p9100_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
744     int he, uint32_t rop)
745 {
746 	struct p9100_softc *sc = cookie;
747 	uint32_t src, dst, srcw, dstw;
748 
749 	sc->sc_last_offset = 0xffffffff;
750 
751 	src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
752 	dst = ((xd & 0x3fff) << 16) | (yd & 0x3fff);
753 	srcw = (((xs + wi - 1) & 0x3fff) << 16) | ((ys + he - 1) & 0x3fff);
754 	dstw = (((xd + wi - 1) & 0x3fff) << 16) | ((yd + he - 1) & 0x3fff);
755 	p9100_sync(sc);
756 	p9100_ctl_write_4(sc, RASTER_OP, rop);
757 
758 	p9100_ctl_write_4(sc, ABS_XY0, src);
759 
760 	p9100_ctl_write_4(sc, ABS_XY1, srcw);
761 	p9100_ctl_write_4(sc, ABS_XY2, dst);
762 	p9100_ctl_write_4(sc, ABS_XY3, dstw);
763 	sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_BLIT);
764 }
765 
766 /* solid rectangle fill */
767 static void
768 p9100_rectfill(void *cookie, int xs, int ys, int wi, int he, uint32_t col)
769 {
770 	struct p9100_softc *sc = cookie;
771 	uint32_t src, srcw;
772 
773 	sc->sc_last_offset = 0xffffffff;
774 
775 	src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
776 	srcw = (((xs + wi) & 0x3fff) << 16) | ((ys + he) & 0x3fff);
777 	p9100_sync(sc);
778 	p9100_set_color_reg(sc, FOREGROUND_COLOR, col);
779 	p9100_set_color_reg(sc, BACKGROUND_COLOR, col);
780 	p9100_ctl_write_4(sc, RASTER_OP, ROP_PAT);
781 	p9100_ctl_write_4(sc, COORD_INDEX, 0);
782 	p9100_ctl_write_4(sc, RECT_RTW_XY, src);
783 	p9100_ctl_write_4(sc, RECT_RTW_XY, srcw);
784 	sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_QUAD);
785 }
786 
787 /* setup for mono->colour expansion */
788 static void
789 p9100_setup_mono(struct p9100_softc *sc, int x, int y, int wi, int he,
790     uint32_t fg, uint32_t bg)
791 {
792 
793 	sc->sc_last_offset = 0xffffffff;
794 
795 	p9100_sync(sc);
796 	/*
797 	 * this doesn't make any sense to me either, but for some reason the
798 	 * chip applies the foreground colour to 0 pixels
799 	 */
800 
801 	p9100_set_color_reg(sc,FOREGROUND_COLOR,bg);
802 	p9100_set_color_reg(sc,BACKGROUND_COLOR,fg);
803 
804 	p9100_ctl_write_4(sc, RASTER_OP, ROP_SRC);
805 	p9100_ctl_write_4(sc, ABS_X0, x);
806 	p9100_ctl_write_4(sc, ABS_XY1, (x << 16) | (y & 0xFFFFL));
807 	p9100_ctl_write_4(sc, ABS_X2, (x + wi));
808 	p9100_ctl_write_4(sc, ABS_Y3, he);
809 	/* now feed the data into the chip */
810 	sc->sc_mono_width = wi;
811 }
812 
813 /* write monochrome data to the screen through the blitter */
814 static void
815 p9100_feed_line(struct p9100_softc *sc, int count, uint8_t *data)
816 {
817 	int i;
818 	uint32_t latch = 0, bork;
819 	int shift = 24;
820 	int to_go = sc->sc_mono_width;
821 
822 	PNOZZ_LATCH(sc, PIXEL_1);
823 
824 	for (i = 0; i < count; i++) {
825 		bork = data[i];
826 		latch |= (bork << shift);
827 		if (shift == 0) {
828 			/* check how many bits are significant */
829 			if (to_go > 31) {
830 				bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
831 				    (PIXEL_1 + (31 << 2)), latch);
832 				/*p9100_ctl_write_4(sc, (PIXEL_1 +
833 				    (31 << 2)), latch);*/
834 				to_go -= 32;
835 			} else
836 			{
837 				bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
838 				    (PIXEL_1 + ((to_go - 1) << 2)), latch);
839 				/*p9100_ctl_write_4(sc, (PIXEL_1 +
840 				    ((to_go - 1) << 2)), latch);*/
841 				to_go = 0;
842 			}
843 			latch = 0;
844 			shift = 24;
845 		} else
846 			shift -= 8;
847 		}
848 	if (shift != 24)
849 		p9100_ctl_write_4(sc, (PIXEL_1 + ((to_go - 1) << 2)), latch);
850 }
851 
852 static void
853 p9100_clearscreen(struct p9100_softc *sc)
854 {
855 
856 	p9100_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
857 }
858 #endif /* NWSDISPLAY > 0 */
859 
860 static uint8_t
861 p9100_ramdac_read(struct p9100_softc *sc, bus_size_t off)
862 {
863 
864 	sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
865 	return ((bus_space_read_4(sc->sc_bustag,
866 	    sc->sc_ctl_memh, off) >> 16) & 0xff);
867 }
868 
869 static void
870 p9100_ramdac_write(struct p9100_softc *sc, bus_size_t off, uint8_t v)
871 {
872 
873 	sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
874 	bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off,
875 	    ((uint32_t)v) << 16);
876 }
877 
878 static uint8_t
879 p9100_ramdac_read_ctl(struct p9100_softc *sc, int off)
880 {
881 	p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
882 	p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
883 	return p9100_ramdac_read(sc, DAC_INDX_DATA);
884 }
885 
886 static void
887 p9100_ramdac_write_ctl(struct p9100_softc *sc, int off, uint8_t val)
888 {
889 	p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
890 	p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
891 	p9100_ramdac_write(sc, DAC_INDX_DATA, val);
892 }
893 
894 /*
895  * Undo the effect of an FBIOSVIDEO that turns the video off.
896  */
897 static void
898 p9100unblank(struct device *dev)
899 {
900 	struct p9100_softc *sc = (struct p9100_softc *)dev;
901 
902 	p9100_set_video((struct p9100_softc *)dev, 1);
903 
904 	/*
905 	 * Check if we're in terminal mode. If not force the console screen
906 	 * to front so we can see ddb, panic messages and so on
907 	 */
908 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
909 		sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
910 		if (sc->vd.active != &p9100_console_screen) {
911 			SCREEN_INVISIBLE(sc->vd.active);
912 			sc->vd.active = &p9100_console_screen;
913 			SCREEN_VISIBLE(&p9100_console_screen);
914 		}
915 		vcons_redraw_screen(&p9100_console_screen);
916 	}
917 }
918 
919 static void
920 p9100_set_video(struct p9100_softc *sc, int enable)
921 {
922 	u_int32_t v = p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1);
923 
924 	if (enable)
925 		v |= VIDEO_ENABLED;
926 	else
927 		v &= ~VIDEO_ENABLED;
928 	p9100_ctl_write_4(sc, SCRN_RPNT_CTL_1, v);
929 #if NTCTRL > 0
930 	/* Turn On/Off the TFT if we know how.
931 	 */
932 	tadpole_set_video(enable);
933 #endif
934 }
935 
936 static int
937 p9100_get_video(struct p9100_softc *sc)
938 {
939 	return (p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1) & VIDEO_ENABLED) != 0;
940 }
941 
942 static void
943 p9100_power_hook(int why, void *cookie)
944 {
945 	struct p9100_softc *sc = cookie;
946 
947 	if (why == sc->sc_powerstate)
948 		return;
949 
950 	switch(why)
951 	{
952 		case PWR_SUSPEND:
953 		case PWR_STANDBY:
954 			sc->sc_video = p9100_get_video(sc);
955 			p9100_set_video(sc, 0);
956 			sc->sc_powerstate = why;
957 			break;
958 		case PWR_RESUME:
959 			p9100_set_video(sc, sc->sc_video);
960 			sc->sc_powerstate = why;
961 			break;
962 	}
963 }
964 
965 /*
966  * Load a subset of the current (new) colormap into the IBM RAMDAC.
967  */
968 static void
969 p9100loadcmap(struct p9100_softc *sc, int start, int ncolors)
970 {
971 	int i;
972 
973 	sc->sc_last_offset = 0xffffffff;
974 
975 	p9100_ramdac_write(sc, DAC_CMAP_WRIDX, start);
976 
977 	for (i=0;i<ncolors;i++) {
978 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
979 		    sc->sc_cmap.cm_map[i + start][0]);
980 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
981 		    sc->sc_cmap.cm_map[i + start][1]);
982 		p9100_ramdac_write(sc, DAC_CMAP_DATA,
983 		    sc->sc_cmap.cm_map[i + start][2]);
984 	}
985 }
986 
987 /*
988  * Return the address that would map the given device at the given
989  * offset, allowing for the given protection, or return -1 for error.
990  */
991 static paddr_t
992 p9100mmap(dev_t dev, off_t off, int prot)
993 {
994 	struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
995 
996 	if (off & PGOFSET)
997 		panic("p9100mmap");
998 	if (off < 0)
999 		return (-1);
1000 
1001 #ifdef PNOZZ_EMUL_CG3
1002 #define CG3_MMAP_OFFSET	0x04000000
1003 	/* Make Xsun think we are a CG3 (SUN3COLOR)
1004 	 */
1005 	if (off >= CG3_MMAP_OFFSET && off < CG3_MMAP_OFFSET + sc->sc_fb_psize) {
1006 		off -= CG3_MMAP_OFFSET;
1007 		return (bus_space_mmap(sc->sc_bustag,
1008 			sc->sc_fb_paddr,
1009 			off,
1010 			prot,
1011 			BUS_SPACE_MAP_LINEAR));
1012 	}
1013 #endif
1014 
1015 	if (off >= sc->sc_fb_psize + sc->sc_ctl_psize + sc->sc_cmd_psize)
1016 		return (-1);
1017 
1018 	if (off < sc->sc_fb_psize) {
1019 		return (bus_space_mmap(sc->sc_bustag,
1020 			sc->sc_fb_paddr,
1021 			off,
1022 			prot,
1023 			BUS_SPACE_MAP_LINEAR));
1024 	}
1025 	off -= sc->sc_fb_psize;
1026 	if (off < sc->sc_ctl_psize) {
1027 		return (bus_space_mmap(sc->sc_bustag,
1028 			sc->sc_ctl_paddr,
1029 			off,
1030 			prot,
1031 			BUS_SPACE_MAP_LINEAR));
1032 	}
1033 	off -= sc->sc_ctl_psize;
1034 
1035 	return (bus_space_mmap(sc->sc_bustag,
1036 		sc->sc_cmd_paddr,
1037 		off,
1038 		prot,
1039 		BUS_SPACE_MAP_LINEAR));
1040 }
1041 
1042 /* wscons stuff */
1043 #if NWSDISPLAY > 0
1044 
1045 static void
1046 p9100_cursor(void *cookie, int on, int row, int col)
1047 {
1048 	struct rasops_info *ri = cookie;
1049 	struct vcons_screen *scr = ri->ri_hw;
1050 	struct p9100_softc *sc = scr->scr_cookie;
1051 	int x, y, wi,he;
1052 
1053 	wi = ri->ri_font->fontwidth;
1054 	he = ri->ri_font->fontheight;
1055 
1056 	if (ri->ri_flg & RI_CURSOR) {
1057 		x = ri->ri_ccol * wi + ri->ri_xorigin;
1058 		y = ri->ri_crow * he + ri->ri_yorigin;
1059 		p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
1060 		ri->ri_flg &= ~RI_CURSOR;
1061 	}
1062 	ri->ri_crow = row;
1063 	ri->ri_ccol = col;
1064 	if (on)
1065 	{
1066 		x = ri->ri_ccol * wi + ri->ri_xorigin;
1067 		y = ri->ri_crow * he + ri->ri_yorigin;
1068 		p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
1069 		ri->ri_flg |= RI_CURSOR;
1070 	}
1071 }
1072 
1073 #if 0
1074 static int
1075 p9100_mapchar(void *cookie, int uni, u_int *index)
1076 {
1077 	return 0;
1078 }
1079 #endif
1080 
1081 static void
1082 p9100_putchar(void *cookie, int row, int col, u_int c, long attr)
1083 {
1084 	struct rasops_info *ri = cookie;
1085 	struct vcons_screen *scr = ri->ri_hw;
1086 	struct p9100_softc *sc = scr->scr_cookie;
1087 
1088 	int fg, bg, uc, i;
1089 	uint8_t *data;
1090 	int x, y, wi,he;
1091 
1092 	wi = ri->ri_font->fontwidth;
1093 	he = ri->ri_font->fontheight;
1094 
1095 	if (!CHAR_IN_FONT(c, ri->ri_font))
1096 		return;
1097 	bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
1098 	fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xff];
1099 	x = ri->ri_xorigin + col * wi;
1100 	y = ri->ri_yorigin + row * he;
1101 	if (c == 0x20) {
1102 		p9100_rectfill(sc, x, y, wi, he, bg);
1103 	} else {
1104 		uc = c-ri->ri_font->firstchar;
1105 		data = (uint8_t *)ri->ri_font->data + uc *
1106 		    ri->ri_fontscale;
1107 
1108 		p9100_setup_mono(sc, x, y, wi, 1, fg, bg);
1109 		for (i = 0; i < he; i++) {
1110 			p9100_feed_line(sc, ri->ri_font->stride,
1111 			    data);
1112 			data += ri->ri_font->stride;
1113 		}
1114 		/*p9100_sync(sc);*/
1115 	}
1116 }
1117 
1118 /*
1119  * wsdisplay_accessops
1120  */
1121 
1122 int
1123 p9100_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
1124 	struct lwp *l)
1125 {
1126 	struct vcons_data *vd = v;
1127 	struct p9100_softc *sc = vd->cookie;
1128 	struct wsdisplay_fbinfo *wdf;
1129 	struct vcons_screen *ms = vd->active;
1130 
1131 	switch (cmd) {
1132 		case WSDISPLAYIO_GTYPE:
1133 			*(u_int *)data = WSDISPLAY_TYPE_SB_P9100;
1134 			return 0;
1135 
1136 		case FBIOGVIDEO:
1137 		case WSDISPLAYIO_GVIDEO:
1138 			*(int *)data = p9100_get_video(sc);
1139 			return 0;
1140 
1141 		case WSDISPLAYIO_SVIDEO:
1142 		case FBIOSVIDEO:
1143 			p9100_set_video(sc, *(int *)data);
1144 			return 0;
1145 
1146 		case WSDISPLAYIO_GINFO:
1147 			wdf = (void *)data;
1148 			wdf->height = ms->scr_ri.ri_height;
1149 			wdf->width = ms->scr_ri.ri_width;
1150 			wdf->depth = ms->scr_ri.ri_depth;
1151 			wdf->cmsize = 256;
1152 			return 0;
1153 
1154 		case WSDISPLAYIO_GETCMAP:
1155 			return p9100_getcmap(sc, (struct wsdisplay_cmap *)data);
1156 
1157 		case WSDISPLAYIO_PUTCMAP:
1158 			return p9100_putcmap(sc, (struct wsdisplay_cmap *)data);
1159 
1160 		case WSDISPLAYIO_SMODE:
1161 			{
1162 				int new_mode = *(int*)data;
1163 				if (new_mode != sc->sc_mode)
1164 				{
1165 					sc->sc_mode = new_mode;
1166 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
1167 					{
1168 						p9100_init_engine(sc);
1169 						p9100loadcmap(sc, 0, 256);
1170 						p9100_clearscreen(sc);
1171 						vcons_redraw_screen(ms);
1172 					}
1173 				}
1174 			}
1175 	}
1176 	return EPASSTHROUGH;
1177 }
1178 
1179 static paddr_t
1180 p9100_mmap(void *v, void *vs, off_t offset, int prot)
1181 {
1182 	struct vcons_data *vd = v;
1183 	struct p9100_softc *sc = vd->cookie;
1184 	paddr_t pa;
1185 
1186 	/* 'regular' framebuffer mmap()ing */
1187 	if (offset < sc->sc_fb_psize) {
1188 		pa = bus_space_mmap(sc->sc_bustag, sc->sc_fb_paddr + offset, 0,
1189 		    prot, BUS_SPACE_MAP_LINEAR);
1190 		return pa;
1191 	}
1192 
1193 	if ((offset >= sc->sc_fb_paddr) && (offset < (sc->sc_fb_paddr +
1194 	    sc->sc_fb_psize))) {
1195 		pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
1196 		    BUS_SPACE_MAP_LINEAR);
1197 		return pa;
1198 	}
1199 
1200 	if ((offset >= sc->sc_ctl_paddr) && (offset < (sc->sc_ctl_paddr +
1201 	    sc->sc_ctl_psize))) {
1202 		pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
1203 		    BUS_SPACE_MAP_LINEAR);
1204 		return pa;
1205 	}
1206 
1207 	return -1;
1208 }
1209 
1210 static void
1211 p9100_init_screen(void *cookie, struct vcons_screen *scr,
1212     int existing, long *defattr)
1213 {
1214 	struct p9100_softc *sc = cookie;
1215 	struct rasops_info *ri = &scr->scr_ri;
1216 
1217 	ri->ri_depth = sc->sc_depth << 3;
1218 	ri->ri_width = sc->sc_width;
1219 	ri->ri_height = sc->sc_height;
1220 	ri->ri_stride = sc->sc_stride;
1221 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
1222 
1223 	ri->ri_bits = bus_space_vaddr(sc->sc_bustag, sc->sc_fb_memh);
1224 
1225 #ifdef DEBUG_P9100
1226 	printf("addr: %08lx\n",(ulong)ri->ri_bits);
1227 #endif
1228 	rasops_init(ri, sc->sc_height/8, sc->sc_width/8);
1229 	ri->ri_caps = WSSCREEN_WSCOLORS;
1230 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
1231 		    sc->sc_width / ri->ri_font->fontwidth);
1232 
1233 	/* enable acceleration */
1234 	ri->ri_ops.cursor    = p9100_cursor;
1235 	ri->ri_ops.copyrows  = p9100_copyrows;
1236 	ri->ri_ops.eraserows = p9100_eraserows;
1237 	ri->ri_ops.copycols  = p9100_copycols;
1238 	ri->ri_ops.erasecols = p9100_erasecols;
1239 	ri->ri_ops.putchar   = p9100_putchar;
1240 	ri->ri_ops.allocattr = p9100_allocattr;
1241 }
1242 
1243 static int
1244 p9100_putcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
1245 {
1246 	u_int index = cm->index;
1247 	u_int count = cm->count;
1248 	int i, error;
1249 	u_char rbuf[256], gbuf[256], bbuf[256];
1250 	u_char *r, *g, *b;
1251 
1252 	if (cm->index >= 256 || cm->count > 256 ||
1253 	    (cm->index + cm->count) > 256)
1254 		return EINVAL;
1255 	error = copyin(cm->red, &rbuf[index], count);
1256 	if (error)
1257 		return error;
1258 	error = copyin(cm->green, &gbuf[index], count);
1259 	if (error)
1260 		return error;
1261 	error = copyin(cm->blue, &bbuf[index], count);
1262 	if (error)
1263 		return error;
1264 
1265 	r = &rbuf[index];
1266 	g = &gbuf[index];
1267 	b = &bbuf[index];
1268 
1269 	for (i = 0; i < count; i++) {
1270 		sc->sc_cmap.cm_map[index][0] = *r;
1271 		sc->sc_cmap.cm_map[index][1] = *g;
1272 		sc->sc_cmap.cm_map[index][2] = *b;
1273 		index++;
1274 		r++, g++, b++;
1275 	}
1276 	p9100loadcmap(sc, 0, 256);
1277 	return 0;
1278 }
1279 
1280 static int
1281 p9100_getcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
1282 {
1283 	u_int index = cm->index;
1284 	u_int count = cm->count;
1285 	int error, i;
1286 	uint8_t red[256], green[256], blue[256];
1287 
1288 	if (index >= 255 || count > 256 || index + count > 256)
1289 		return EINVAL;
1290 
1291 	i = index;
1292 	while (i < (index + count)) {
1293 		red[i] = sc->sc_cmap.cm_map[i][0];
1294 		green[i] = sc->sc_cmap.cm_map[i][1];
1295 		blue[i] = sc->sc_cmap.cm_map[i][2];
1296 		i++;
1297 	}
1298 	error = copyout(&red[index],   cm->red,   count);
1299 	if (error)
1300 		return error;
1301 	error = copyout(&green[index], cm->green, count);
1302 	if (error)
1303 		return error;
1304 	error = copyout(&blue[index],  cm->blue,  count);
1305 	if (error)
1306 		return error;
1307 
1308 	return 0;
1309 }
1310 
1311 static void
1312 p9100_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1313 {
1314 	struct rasops_info *ri = cookie;
1315 	struct vcons_screen *scr = ri->ri_hw;
1316 	int32_t xs, xd, y, width, height;
1317 
1318 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1319 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1320 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1321 	width = ri->ri_font->fontwidth * ncols;
1322 	height = ri->ri_font->fontheight;
1323 	p9100_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, ROP_SRC);
1324 }
1325 
1326 static void
1327 p9100_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1328 {
1329 	struct rasops_info *ri = cookie;
1330 	struct vcons_screen *scr = ri->ri_hw;
1331 	int32_t x, y, width, height, bg;
1332 
1333 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1334 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1335 	width = ri->ri_font->fontwidth * ncols;
1336 	height = ri->ri_font->fontheight;
1337 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1338 	p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
1339 }
1340 
1341 static void
1342 p9100_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1343 {
1344 	struct rasops_info *ri = cookie;
1345 	struct vcons_screen *scr = ri->ri_hw;
1346 	int32_t x, ys, yd, width, height;
1347 
1348 	x = ri->ri_xorigin;
1349 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1350 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1351 	width = ri->ri_emuwidth;
1352 	height = ri->ri_font->fontheight * nrows;
1353 	p9100_bitblt(scr->scr_cookie, x, ys, x, yd, width, height, ROP_SRC);
1354 }
1355 
1356 static void
1357 p9100_eraserows(void *cookie, int row, int nrows, long fillattr)
1358 {
1359 	struct rasops_info *ri = cookie;
1360 	struct vcons_screen *scr = ri->ri_hw;
1361 	int32_t x, y, width, height, bg;
1362 
1363 	if ((row == 0) && (nrows == ri->ri_rows)) {
1364 		x = y = 0;
1365 		width = ri->ri_width;
1366 		height = ri->ri_height;
1367 	} else {
1368 		x = ri->ri_xorigin;
1369 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1370 		width = ri->ri_emuwidth;
1371 		height = ri->ri_font->fontheight * nrows;
1372 	}
1373 	bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1374 	p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
1375 }
1376 
1377 
1378 static int
1379 p9100_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
1380 {
1381 	if ((fg == 0) && (bg == 0))
1382 	{
1383 		fg = WS_DEFAULT_FG;
1384 		bg = WS_DEFAULT_BG;
1385 	}
1386 	if (flags & WSATTR_REVERSE) {
1387 		*attrp = (bg & 0xff) << 24 | (fg & 0xff) << 16 |
1388 		    (flags & 0xff) << 8;
1389 	} else
1390 		*attrp = (fg & 0xff) << 24 | (bg & 0xff) << 16 |
1391 		    (flags & 0xff) << 8;
1392 	return 0;
1393 }
1394 
1395 #if 0
1396 static int
1397 p9100_load_font(void *v, void *cookie, struct wsdisplay_font *data)
1398 {
1399 
1400 	return 0;
1401 }
1402 #endif
1403 
1404 #endif /* NWSDISPLAY > 0 */
1405 
1406 static int
1407 p9100_intr(void *arg)
1408 {
1409 	/*p9100_softc *sc=arg;
1410 	printf(".");*/
1411 	return 1;
1412 }
1413 
1414 static void
1415 p9100_init_cursor(struct p9100_softc *sc)
1416 {
1417 
1418 	memset(&sc->sc_cursor, 0, sizeof(struct pnozz_cursor));
1419 	sc->sc_cursor.pc_size.x = 64;
1420 	sc->sc_cursor.pc_size.y = 64;
1421 
1422 }
1423 
1424 static void
1425 p9100_set_fbcursor(struct p9100_softc *sc)
1426 {
1427 #ifdef PNOZZ_PARANOID
1428 	int s;
1429 
1430 	s = splhigh();	/* just in case... */
1431 #endif
1432 	sc->sc_last_offset = 0xffffffff;
1433 
1434 	/* set position and hotspot */
1435 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
1436 	p9100_ramdac_write(sc, DAC_INDX_HI, 0);
1437 	p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_CTL);
1438 	if (sc->sc_cursor.pc_enable) {
1439 		p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_X11 |
1440 		    DAC_CURSOR_64);
1441 	} else
1442 		p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_OFF);
1443 	/* next two registers - x low, high, y low, high */
1444 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.x & 0xff);
1445 	p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.x >> 8) &
1446 	    0xff);
1447 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.y & 0xff);
1448 	p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.y >> 8) &
1449 	    0xff);
1450 	/* hotspot */
1451 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.x & 0xff);
1452 	p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.y & 0xff);
1453 
1454 #ifdef PNOZZ_PARANOID
1455 	splx(s);
1456 #endif
1457 
1458 }
1459 
1460 static void
1461 p9100_setcursorcmap(struct p9100_softc *sc)
1462 {
1463 	int i;
1464 
1465 #ifdef PNOZZ_PARANOID
1466 	int s;
1467 	s = splhigh();	/* just in case... */
1468 #endif
1469 	sc->sc_last_offset = 0xffffffff;
1470 
1471 	/* set cursor colours */
1472 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
1473 	p9100_ramdac_write(sc, DAC_INDX_HI, 0);
1474 	p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_COL_1);
1475 
1476 	for (i = 0; i < 3; i++) {
1477 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.red[i]);
1478 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.green[i]);
1479 		p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.blue[i]);
1480 	}
1481 
1482 #ifdef PNOZZ_PARANOID
1483 	splx(s);
1484 #endif
1485 }
1486 
1487 static void
1488 p9100_loadcursor(struct p9100_softc *sc)
1489 {
1490 	uint32_t *image, *mask;
1491 	uint32_t bit, bbit, im, ma;
1492 	int i, j, k;
1493 	uint8_t latch1, latch2;
1494 
1495 #ifdef PNOZZ_PARANOID
1496 	int s;
1497 	s = splhigh();	/* just in case... */
1498 #endif
1499 	sc->sc_last_offset = 0xffffffff;
1500 
1501 	/* set cursor shape */
1502 	p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
1503 	p9100_ramdac_write(sc, DAC_INDX_HI, 1);
1504 	p9100_ramdac_write(sc, DAC_INDX_LO, 0);
1505 
1506 	image = sc->sc_cursor.pc_bits;
1507 	mask = &sc->sc_cursor.pc_bits[0x80];
1508 
1509 	for (i = 0; i < 0x80; i++) {
1510 		bit = 0x80000000;
1511 		im = image[i];
1512 		ma = mask[i];
1513 		for (k = 0; k < 4; k++) {
1514 			bbit = 0x1;
1515 			latch1 = 0;
1516 			for (j = 0; j < 4; j++) {
1517 				if (im & bit)
1518 					latch1 |= bbit;
1519 				bbit <<= 1;
1520 				if (ma & bit)
1521 					latch1 |= bbit;
1522 				bbit <<= 1;
1523 				bit >>= 1;
1524 			}
1525 			bbit = 0x1;
1526 			latch2 = 0;
1527 			for (j = 0; j < 4; j++) {
1528 				if (im & bit)
1529 					latch2 |= bbit;
1530 				bbit <<= 1;
1531 				if (ma & bit)
1532 					latch2 |= bbit;
1533 				bbit <<= 1;
1534 				bit >>= 1;
1535 			}
1536 			p9100_ramdac_write(sc, DAC_INDX_DATA, latch1);
1537 			p9100_ramdac_write(sc, DAC_INDX_DATA, latch2);
1538 		}
1539 	}
1540 #ifdef DEBUG_CURSOR
1541 	printf("image:\n");
1542 	for (i=0;i<0x80;i+=2)
1543 		printf("%08x %08x\n", image[i], image[i+1]);
1544 	printf("mask:\n");
1545 	for (i=0;i<0x80;i+=2)
1546 		printf("%08x %08x\n", mask[i], mask[i+1]);
1547 #endif
1548 #ifdef PNOZZ_PARANOID
1549 	splx(s);
1550 #endif
1551 }
1552 
1553 static void
1554 p9100_set_extvga(void *cookie, int status)
1555 {
1556 	struct p9100_softc *sc = cookie;
1557 #ifdef PNOZZ_PARANOID
1558 	int s;
1559 
1560 	s = splhigh();
1561 #endif
1562 #ifdef DEBUG
1563 	printf("%s: external VGA %s\n", sc->sc_dev.dv_xname,
1564 	    status ? "on" : "off");
1565 #endif
1566 	sc->sc_last_offset = 0xffffffff;
1567 
1568 	if (status) {
1569 		p9100_ramdac_write_ctl(sc, DAC_POWER,
1570 		    p9100_ramdac_read_ctl(sc, DAC_POWER) &
1571 		    ~DAC_POWER_IPWR_DISABLE);
1572 	} else {
1573 		p9100_ramdac_write_ctl(sc, DAC_POWER,
1574 		    p9100_ramdac_read_ctl(sc, DAC_POWER) |
1575 		    DAC_POWER_IPWR_DISABLE);
1576 	}
1577 #ifdef PNOZZ_PARANOID
1578 	splx(s);
1579 #endif
1580 }
1581