Home | History | Annotate | Line # | Download | only in pci
voodoofb.c revision 1.1
      1 /*	$NetBSD: voodoofb.c,v 1.1 2006/04/11 16:11:07 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * A console driver for 3Dfx Voodoo3 graphics boards
     32  * Thanks to Andreas Drewke (andreas_dr (at) gmx.de) for his Voodoo3 driver for BeOS
     33  * which I used as reference / documentation
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: voodoofb.c,v 1.1 2006/04/11 16:11:07 macallan Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/callout.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 
     48 #if defined(macppc) || defined (sparc64) || defined(ofppc)
     49 #define HAVE_OPENFIRMWARE
     50 #endif
     51 
     52 #ifdef HAVE_OPENFIRMWARE
     53 #include <dev/ofw/openfirm.h>
     54 #include <dev/ofw/ofw_pci.h>
     55 #endif
     56 
     57 #include <dev/videomode/videomode.h>
     58 
     59 #include <dev/pci/pcivar.h>
     60 #include <dev/pci/pcireg.h>
     61 #include <dev/pci/pcidevs.h>
     62 #include <dev/pci/pciio.h>
     63 #include <dev/pci/voodoofbreg.h>
     64 
     65 #include <dev/wscons/wsdisplayvar.h>
     66 #include <dev/wscons/wsconsio.h>
     67 #include <dev/wsfont/wsfont.h>
     68 #include <dev/rasops/rasops.h>
     69 #include <dev/wscons/wsdisplay_vconsvar.h>
     70 
     71 #include "opt_wsemul.h"
     72 
     73 struct voodoofb_softc {
     74 	struct device sc_dev;
     75 	pci_chipset_tag_t sc_pc;
     76 	pcitag_t sc_pcitag;
     77 
     78 	bus_space_tag_t sc_memt;
     79 	bus_space_tag_t sc_iot;
     80 	bus_space_handle_t sc_memh;
     81 
     82 	bus_space_tag_t sc_regt;
     83 	bus_space_tag_t sc_fbt;
     84 	bus_space_tag_t sc_ioregt;
     85 	bus_space_handle_t sc_regh;
     86 	bus_space_handle_t sc_fbh;
     87 	bus_space_handle_t sc_ioregh;
     88 	bus_addr_t sc_regs, sc_fb, sc_ioreg;
     89 	bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
     90 
     91 /*	void (*putchar)(void *c, int row, int col, u_int uc, long attr);*/
     92 
     93 	void *sc_ih;
     94 
     95 	size_t memsize;
     96 	int memtype;
     97 
     98 	int bits_per_pixel;
     99 	int width, height, linebytes;
    100 
    101 	int sc_mode;
    102 	uint32_t sc_bg;
    103 
    104 	u_char sc_cmap_red[256];
    105 	u_char sc_cmap_green[256];
    106 	u_char sc_cmap_blue[256];
    107 	int sc_dacw;
    108 
    109 	struct vcons_data vd;
    110 };
    111 
    112 struct voodoo_regs {
    113 	uint8_t vr_crtc[31];
    114 	uint8_t vr_graph[9];
    115 	uint8_t vr_attr[21];
    116 	uint8_t vr_seq[5];
    117 };
    118 
    119 static struct vcons_screen voodoofb_console_screen;
    120 
    121 extern const u_char rasops_cmap[768];
    122 
    123 static int	voodoofb_match(struct device *, struct cfdata *, void *);
    124 static void	voodoofb_attach(struct device *, struct device *, void *);
    125 
    126 CFATTACH_DECL(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
    127     voodoofb_attach, NULL, NULL);
    128 
    129 static int	voodoofb_is_console(struct pci_attach_args *);
    130 static void 	voodoofb_init(struct voodoofb_softc *);
    131 
    132 static void	voodoofb_cursor(void *, int, int, int);
    133 static void	voodoofb_putchar(void *, int, int, u_int, long);
    134 static void	voodoofb_copycols(void *, int, int, int, int);
    135 static void	voodoofb_erasecols(void *, int, int, int, long);
    136 static void	voodoofb_copyrows(void *, int, int, int);
    137 static void	voodoofb_eraserows(void *, int, int, long);
    138 
    139 #if 0
    140 static int	voodoofb_allocattr(void *, int, int, int, long *);
    141 static void	voodoofb_scroll(void *, void *, int);
    142 static int	voodoofb_load_font(void *, void *, struct wsdisplay_font *);
    143 #endif
    144 
    145 static int	voodoofb_putcmap(struct voodoofb_softc *,
    146 			    struct wsdisplay_cmap *);
    147 static int 	voodoofb_getcmap(struct voodoofb_softc *,
    148 			    struct wsdisplay_cmap *);
    149 static int 	voodoofb_putpalreg(struct voodoofb_softc *, uint8_t, uint8_t,
    150 			    uint8_t, uint8_t);
    151 static void	voodoofb_bitblt(struct voodoofb_softc *, int, int, int, int,
    152 			    int, int);
    153 static void	voodoofb_rectfill(struct voodoofb_softc *, int, int, int, int,
    154 			    int);
    155 static void	voodoofb_rectinvert(struct voodoofb_softc *, int, int, int,
    156 			    int);
    157 static void	voodoofb_setup_mono(struct voodoofb_softc *, int, int, int,
    158 			    int, uint32_t, uint32_t);
    159 static void	voodoofb_feed_line(struct voodoofb_softc *, int, uint8_t *);
    160 
    161 #ifdef VOODOOFB_DEBUG
    162 static void	voodoofb_showpal(struct voodoofb_softc *);
    163 #endif
    164 
    165 static void	voodoofb_wait_idle(struct voodoofb_softc *);
    166 
    167 static int	voodoofb_intr(void *);
    168 
    169 static void	voodoofb_set_videomode(struct voodoofb_softc *,
    170 			    const struct videomode *);
    171 
    172 struct wsscreen_descr voodoofb_defaultscreen = {
    173 	"default",
    174 	0, 0,
    175 	NULL,
    176 	8, 16,
    177 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    178 };
    179 
    180 const struct wsscreen_descr *_voodoofb_scrlist[] = {
    181 	&voodoofb_defaultscreen,
    182 	/* XXX other formats, graphics screen? */
    183 };
    184 
    185 struct wsscreen_list voodoofb_screenlist = {
    186 	sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
    187 };
    188 
    189 static int	voodoofb_ioctl(void *, u_long, caddr_t, int, struct lwp *);
    190 static paddr_t	voodoofb_mmap(void *, off_t, int);
    191 
    192 static void	voodoofb_clearscreen(struct voodoofb_softc *);
    193 static void	voodoofb_init_screen(void *, struct vcons_screen *, int,
    194 			    long *);
    195 
    196 
    197 struct wsdisplay_accessops voodoofb_accessops = {
    198 	voodoofb_ioctl,
    199 	voodoofb_mmap,
    200 	NULL,	/* load_font */
    201 	NULL,	/* polls */
    202 	NULL,	/* getwschar */
    203 	NULL,	/* putwschar */
    204 	NULL,	/* scroll */
    205 };
    206 
    207 /*
    208  * Inline functions for getting access to register aperture.
    209  */
    210 static inline void
    211 voodoo3_write32(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
    212 {
    213 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
    214 }
    215 
    216 static inline uint32_t
    217 voodoo3_read32(struct voodoofb_softc *sc, uint32_t reg)
    218 {
    219 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
    220 }
    221 
    222 static inline void
    223 voodoo3_write_crtc(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    224 {
    225 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_INDEX - 0x300, reg);
    226 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_DATA - 0x300, val);
    227 }
    228 
    229 static inline void
    230 voodoo3_write_seq(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    231 {
    232 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_INDEX - 0x300, reg);
    233 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_DATA - 0x300, val);
    234 }
    235 
    236 static inline void
    237 voodoo3_write_gra(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    238 {
    239 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_INDEX - 0x300, reg);
    240 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_DATA - 0x300, val);
    241 }
    242 
    243 static inline void
    244 voodoo3_write_attr(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    245 {
    246 	volatile uint8_t junk;
    247 	uint8_t index;
    248 
    249 	junk = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, IS1_R - 0x300);
    250 	index = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300);
    251 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, reg);
    252 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, val);
    253 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, index);
    254 }
    255 
    256 static inline void
    257 vga_outb(struct voodoofb_softc *sc, uint32_t reg,  uint8_t val)
    258 {
    259 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, reg - 0x300, val);
    260 }
    261 
    262 /* wait until there's room for len bytes in the FIFO */
    263 static inline void
    264 voodoo3_make_room(struct voodoofb_softc *sc, int len)
    265 {
    266 	while ((voodoo3_read32(sc, STATUS) & 0x1f) < len);
    267 }
    268 
    269 static void
    270 voodoofb_wait_idle(struct voodoofb_softc *sc)
    271 {
    272 	int i = 0;
    273 
    274 	voodoo3_make_room(sc, 1);
    275 	voodoo3_write32(sc, COMMAND_3D, COMMAND_3D_NOP);
    276 
    277 	while (1) {
    278 		i = (voodoo3_read32(sc, STATUS) & STATUS_BUSY) ? 0 : i + 1;
    279 		if(i == 3) break;
    280 	}
    281 }
    282 
    283 static int
    284 voodoofb_match(struct device *parent, struct cfdata *match, void *aux)
    285 {
    286 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    287 
    288 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    289 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    290 		return 0;
    291 	if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
    292 	    (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_VOODOO3))
    293 		return 100;
    294 	return 0;
    295 }
    296 
    297 static void
    298 voodoofb_attach(struct device *parent, struct device *self, void *aux)
    299 {
    300 	struct voodoofb_softc *sc = (void *)self;
    301 	struct pci_attach_args *pa = aux;
    302 	char devinfo[256];
    303 	struct wsemuldisplaydev_attach_args aa;
    304 	struct rasops_info *ri;
    305 	pci_intr_handle_t ih;
    306 	ulong defattr;
    307 	const char *intrstr;
    308 	int console, width, height, node, i, j;
    309 #ifdef HAVE_OPENFIRMWARE
    310 	int linebytes, depth;
    311 #endif
    312 	uint32_t bg, fg, ul;
    313 
    314 	sc->sc_mode=WSDISPLAYIO_MODE_EMUL;
    315 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    316 	sc->sc_pc = pa->pa_pc;
    317 	sc->sc_pcitag = pa->pa_tag;
    318 	sc->sc_dacw = -1;
    319 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    320 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    321 
    322 	sc->sc_memt = pa->pa_memt;
    323 	sc->sc_iot = pa->pa_iot;
    324 
    325 	/* the framebuffer */
    326 	if(pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM,
    327 	    BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE |
    328 	    BUS_SPACE_MAP_LINEAR,
    329 	    &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    330 		printf("%s: Couldn't map the frame buffer!\n",
    331 		    sc->sc_dev.dv_xname);
    332 	}
    333 
    334 	/* memory-mapped registers */
    335 	if(pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    336 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
    337 		printf("%s: Couldn't map memory-mapped registers!\n",
    338 		    sc->sc_dev.dv_xname);
    339 	}
    340 
    341 	/* IO-mapped registers */
    342 	if(pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
    343 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
    344 	    &sc->sc_ioregsize)) {
    345 		printf("%s: Couldn't map IO-mapped registers!\n",
    346 		    sc->sc_dev.dv_xname);
    347 	}
    348 	voodoofb_init(sc);
    349 
    350 	/* we should read these from the chip instead of depending on OF */
    351 	width = height = -1;
    352 
    353 #ifdef HAVE_OPENFIRMWARE
    354 	if (OF_getprop(node, "width", &width, 4) != 4)
    355 		OF_interpret("screen-width", 1, &width);
    356 	if (OF_getprop(node, "height", &height, 4) != 4)
    357 		OF_interpret("screen-height", 1, &height);
    358 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    359 		linebytes = width;			/* XXX */
    360 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    361 		depth = 8;				/* XXX */
    362 
    363 	if (width == -1 || height == -1)
    364 		return;
    365 
    366 	sc->width = width;
    367 	sc->height = height;
    368 	sc->bits_per_pixel = depth;
    369 	sc->linebytes = linebytes;
    370 	printf("%s: initial resolution %dx%d, %d bit\n", sc->sc_dev.dv_xname,
    371 	    sc->width, sc->height, sc->bits_per_pixel);
    372 #endif
    373 
    374 	/* XXX this should at least be configurable via kernel config */
    375 	voodoofb_set_videomode(sc, &videomode_list[16]);
    376 
    377 	vcons_init(&sc->vd, sc, &voodoofb_defaultscreen, &voodoofb_accessops);
    378 	sc->vd.init_screen = voodoofb_init_screen;
    379 
    380 	console = voodoofb_is_console(pa);
    381 
    382 	ri = &voodoofb_console_screen.scr_ri;
    383 	if (console) {
    384 		vcons_init_screen(&sc->vd, &voodoofb_console_screen, 1,
    385 		    &defattr);
    386 		voodoofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    387 
    388 		voodoofb_defaultscreen.textops = &ri->ri_ops;
    389 		voodoofb_defaultscreen.capabilities = ri->ri_caps;
    390 		voodoofb_defaultscreen.nrows = ri->ri_rows;
    391 		voodoofb_defaultscreen.ncols = ri->ri_cols;
    392 		wsdisplay_cnattach(&voodoofb_defaultscreen, ri, 0, 0, defattr);
    393 	} else {
    394 		/*
    395 		 * since we're not the console we can postpone the rest
    396 		 * until someone actually allocates a screen for us
    397 		 */
    398 		voodoofb_set_videomode(sc, &videomode_list[0]);
    399 	}
    400 
    401 	printf("%s: %d MB aperture at 0x%08x, %d MB registers at 0x%08x\n",
    402 	    sc->sc_dev.dv_xname, (u_int)(sc->sc_fbsize >> 20),
    403 	    (u_int)sc->sc_fb, (u_int)(sc->sc_regsize >> 20),
    404 	    (u_int)sc->sc_regs);
    405 #ifdef VOODOOFB_DEBUG
    406 	printf("fb: %08lx\n", (ulong)ri->ri_bits);
    407 #endif
    408 
    409 	j = 0;
    410 	for (i = 0; i < 256; i++) {
    411 		voodoofb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    412 		    rasops_cmap[j + 2]);
    413 		j += 3;
    414 	}
    415 
    416 	/* Interrupt. We don't use it for anything yet */
    417 	if (pci_intr_map(pa, &ih)) {
    418 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    419 		return;
    420 	}
    421 
    422 	intrstr = pci_intr_string(sc->sc_pc, ih);
    423 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, voodoofb_intr,
    424 	    sc);
    425 	if (sc->sc_ih == NULL) {
    426 		printf("%s: couldn't establish interrupt",
    427 		    sc->sc_dev.dv_xname);
    428 		if (intrstr != NULL)
    429 			printf(" at %s", intrstr);
    430 		printf("\n");
    431 		return;
    432 	}
    433 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    434 
    435 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    436 	sc->sc_bg = ri->ri_devcmap[bg];
    437 	voodoofb_clearscreen(sc);
    438 
    439 	aa.console = console;
    440 	aa.scrdata = &voodoofb_screenlist;
    441 	aa.accessops = &voodoofb_accessops;
    442 	aa.accesscookie = &sc->vd;
    443 
    444 	config_found(self, &aa, wsemuldisplaydevprint);
    445 }
    446 
    447 static int
    448 voodoofb_putpalreg(struct voodoofb_softc *sc, uint8_t index, uint8_t r,
    449     uint8_t g, uint8_t b)
    450 {
    451 	uint32_t color;
    452 
    453 	sc->sc_cmap_red[index] = r;
    454 	sc->sc_cmap_green[index] = g;
    455 	sc->sc_cmap_blue[index] = b;
    456 
    457 	color=(r << 16) | (g << 8) | b;
    458 	voodoo3_make_room(sc, 2);
    459 	voodoo3_write32(sc, DACADDR, index);
    460 	voodoo3_write32(sc, DACDATA, color);
    461 
    462 	return 0;
    463 }
    464 
    465 static int
    466 voodoofb_putcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    467 {
    468 	u_char *r, *g, *b;
    469 	u_int index = cm->index;
    470 	u_int count = cm->count;
    471 	int i, error;
    472 	u_char rbuf[256], gbuf[256], bbuf[256];
    473 
    474 #ifdef VOODOOFB_DEBUG
    475 	printf("putcmap: %d %d\n",index, count);
    476 #endif
    477 	if (cm->index >= 256 || cm->count > 256 ||
    478 	    (cm->index + cm->count) > 256)
    479 		return EINVAL;
    480 	error = copyin(cm->red, &rbuf[index], count);
    481 	if (error)
    482 		return error;
    483 	error = copyin(cm->green, &gbuf[index], count);
    484 	if (error)
    485 		return error;
    486 	error = copyin(cm->blue, &bbuf[index], count);
    487 	if (error)
    488 		return error;
    489 
    490 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    491 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    492 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    493 
    494 	r = &sc->sc_cmap_red[index];
    495 	g = &sc->sc_cmap_green[index];
    496 	b = &sc->sc_cmap_blue[index];
    497 
    498 	for (i = 0; i < count; i++) {
    499 		voodoofb_putpalreg(sc,index, *r, *g, *b);
    500 		index++;
    501 		r++, g++, b++;
    502 	}
    503 	return 0;
    504 }
    505 
    506 static int
    507 voodoofb_getcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    508 {
    509 	u_int index = cm->index;
    510 	u_int count = cm->count;
    511 	int error;
    512 
    513 	if (index >= 255 || count > 256 || index + count > 256)
    514 		return EINVAL;
    515 
    516 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    517 	if (error)
    518 		return error;
    519 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    520 	if (error)
    521 		return error;
    522 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    523 	if (error)
    524 		return error;
    525 
    526 	return 0;
    527 }
    528 
    529 static int
    530 voodoofb_is_console(struct pci_attach_args *pa)
    531 {
    532 
    533 #ifdef HAVE_OPENFIRMWARE
    534 	/* check if we're the /chosen console device */
    535 	int chosen, stdout, node, us;
    536 
    537 	us=pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    538 	chosen = OF_finddevice("/chosen");
    539 	OF_getprop(chosen, "stdout", &stdout, 4);
    540 	node = OF_instance_to_package(stdout);
    541 	return(us == node);
    542 #else
    543 	/* XXX how do we know we're console on i386? */
    544 	return 1;
    545 #endif
    546 }
    547 
    548 static void
    549 voodoofb_clearscreen(struct voodoofb_softc *sc)
    550 {
    551 	voodoofb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
    552 }
    553 
    554 /*
    555  * wsdisplay_emulops
    556  */
    557 
    558 static void
    559 voodoofb_cursor(void *cookie, int on, int row, int col)
    560 {
    561 	struct rasops_info *ri = cookie;
    562 	struct vcons_screen *scr = ri->ri_hw;
    563 	struct voodoofb_softc *sc = scr->scr_cookie;
    564 	int x, y, wi,he;
    565 
    566 	wi = ri->ri_font->fontwidth;
    567 	he = ri->ri_font->fontheight;
    568 
    569 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    570 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    571 		y = ri->ri_crow * he + ri->ri_yorigin;
    572 		if (ri->ri_flg & RI_CURSOR) {
    573 			voodoofb_rectinvert(sc, x, y, wi, he);
    574 			ri->ri_flg &= ~RI_CURSOR;
    575 		}
    576 		ri->ri_crow = row;
    577 		ri->ri_ccol = col;
    578 		if (on)
    579 		{
    580 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    581 			y = ri->ri_crow * he + ri->ri_yorigin;
    582 			voodoofb_rectinvert(sc, x, y, wi, he);
    583 			ri->ri_flg |= RI_CURSOR;
    584 		}
    585 	} else {
    586 		ri->ri_flg &= ~RI_CURSOR;
    587 		ri->ri_crow = row;
    588 		ri->ri_ccol = col;
    589 	}
    590 }
    591 
    592 #if 0
    593 int
    594 voodoofb_mapchar(void *cookie, int uni, u_int *index)
    595 {
    596 	return 0;
    597 }
    598 #endif
    599 
    600 static void
    601 voodoofb_putchar(void *cookie, int row, int col, u_int c, long attr)
    602 {
    603 	struct rasops_info *ri = cookie;
    604 	struct vcons_screen *scr = ri->ri_hw;
    605 	struct voodoofb_softc *sc = scr->scr_cookie;
    606 
    607 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    608 		uint8_t *data;
    609 		int fg, bg, uc, i;
    610 		int x, y, wi, he;
    611 
    612 		wi = ri->ri_font->fontwidth;
    613 		he = ri->ri_font->fontheight;
    614 
    615 		if (!CHAR_IN_FONT(c, ri->ri_font))
    616 			return;
    617 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    618 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    619 		x = ri->ri_xorigin + col * wi;
    620 		y = ri->ri_yorigin + row * he;
    621 		if (c == 0x20) {
    622 			voodoofb_rectfill(sc, x, y, wi, he, bg);
    623 		} else {
    624 			uc = c-ri->ri_font->firstchar;
    625 			data = (uint8_t *)ri->ri_font->data + uc *
    626 			    ri->ri_fontscale;
    627 				voodoofb_setup_mono(sc, x, y, wi, he, fg, bg);
    628 			for (i = 0; i < he; i++) {
    629 				voodoofb_feed_line(sc,
    630 				    ri->ri_font->stride, data);
    631 				data += ri->ri_font->stride;
    632 			}
    633 		}
    634 	}
    635 }
    636 
    637 static void
    638 voodoofb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    639 {
    640 	struct rasops_info *ri = cookie;
    641 	struct vcons_screen *scr = ri->ri_hw;
    642 	struct voodoofb_softc *sc = scr->scr_cookie;
    643 	int32_t xs, xd, y, width, height;
    644 
    645 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    646 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    647 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    648 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    649 		width = ri->ri_font->fontwidth * ncols;
    650 		height = ri->ri_font->fontheight;
    651 		voodoofb_bitblt(sc, xs, y, xd, y, width, height);
    652 	}
    653 }
    654 
    655 static void
    656 voodoofb_erasecols(void *cookie, int row, int startcol, int ncols,
    657     long fillattr)
    658 {
    659 	struct rasops_info *ri = cookie;
    660 	struct vcons_screen *scr = ri->ri_hw;
    661 	struct voodoofb_softc *sc = scr->scr_cookie;
    662 	int32_t x, y, width, height, fg, bg, ul;
    663 
    664 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    665 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    666 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    667 		width = ri->ri_font->fontwidth * ncols;
    668 		height = ri->ri_font->fontheight;
    669 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    670 
    671 		voodoofb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    672 	}
    673 }
    674 
    675 static void
    676 voodoofb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    677 {
    678 	struct rasops_info *ri = cookie;
    679 	struct vcons_screen *scr = ri->ri_hw;
    680 	struct voodoofb_softc *sc = scr->scr_cookie;
    681 	int32_t x, ys, yd, width, height;
    682 
    683 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    684 		x = ri->ri_xorigin;
    685 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    686 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    687 		width = ri->ri_emuwidth;
    688 		height = ri->ri_font->fontheight * nrows;
    689 		voodoofb_bitblt(sc, x, ys, x, yd, width, height);
    690 	}
    691 }
    692 
    693 static void
    694 voodoofb_eraserows(void *cookie, int row, int nrows, long fillattr)
    695 {
    696 	struct rasops_info *ri = cookie;
    697 	struct vcons_screen *scr = ri->ri_hw;
    698 	struct voodoofb_softc *sc = scr->scr_cookie;
    699 	int32_t x, y, width, height, fg, bg, ul;
    700 
    701 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    702 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    703 		if ((row == 0) && (nrows == ri->ri_rows)) {
    704 			/* clear the whole screen */
    705 			voodoofb_rectfill(sc, 0, 0, ri->ri_width,
    706 			    ri->ri_height, ri->ri_devcmap[bg]);
    707 		} else {
    708 			x = ri->ri_xorigin;
    709 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    710 			width = ri->ri_emuwidth;
    711 			height = ri->ri_font->fontheight * nrows;
    712 			voodoofb_rectfill(sc, x, y, width, height,
    713 			    ri->ri_devcmap[bg]);
    714 		}
    715 	}
    716 }
    717 
    718 static void
    719 voodoofb_bitblt(struct voodoofb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
    720 {
    721 	uint32_t fmt, blitcmd;
    722 
    723 	fmt = sc->linebytes | ((sc->bits_per_pixel +
    724 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    725 	blitcmd = COMMAND_2D_S2S_BITBLT | (ROP_COPY << 24);
    726 
    727 	if (xs <= xd) {
    728 	        blitcmd |= BIT(14);
    729 		xs += (width - 1);
    730 		xd += (width - 1);
    731 	}
    732 	if (ys <= yd) {
    733 		blitcmd |= BIT(15);
    734 		ys += (height - 1);
    735 		yd += (height - 1);
    736 	}
    737 	voodoo3_make_room(sc, 6);
    738 
    739 	voodoo3_write32(sc, SRCFORMAT, fmt);
    740 	voodoo3_write32(sc, DSTFORMAT, fmt);
    741 	voodoo3_write32(sc, DSTSIZE,   width | (height << 16));
    742 	voodoo3_write32(sc, DSTXY,     xd | (yd << 16));
    743 	voodoo3_write32(sc, SRCXY, xs | (ys << 16));
    744 	voodoo3_write32(sc, COMMAND_2D, blitcmd | SST_2D_GO);
    745 }
    746 
    747 static void
    748 voodoofb_rectfill(struct voodoofb_softc *sc, int x, int y, int width,
    749     int height, int colour)
    750 {
    751 	uint32_t fmt,col;
    752 
    753 	col = (colour << 24) | (colour << 16) | (colour << 8) | colour;
    754 	fmt = sc->linebytes | ((sc->bits_per_pixel +
    755 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    756 
    757 	voodoo3_make_room(sc, 6);
    758 	voodoo3_write32(sc, DSTFORMAT, fmt);
    759 	voodoo3_write32(sc, COLORFORE, colour);
    760 	voodoo3_write32(sc, COLORBACK, colour);
    761 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT | (ROP_COPY << 24));
    762 	voodoo3_write32(sc, DSTSIZE,    width | (height << 16));
    763 	voodoo3_write32(sc, LAUNCH_2D,  x | (y << 16));
    764 }
    765 
    766 static void
    767 voodoofb_rectinvert(struct voodoofb_softc *sc, int x, int y, int width,
    768     int height)
    769 {
    770 	uint32_t fmt;
    771 
    772 	fmt = sc->linebytes | ((sc->bits_per_pixel +
    773 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    774 
    775 	voodoo3_make_room(sc, 6);
    776 	voodoo3_write32(sc, DSTFORMAT,	fmt);
    777 	voodoo3_write32(sc, COMMAND_2D,	COMMAND_2D_FILLRECT |
    778 	    (ROP_INVERT << 24));
    779 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
    780 	voodoo3_write32(sc, DSTXY,	x | (y << 16));
    781 	voodoo3_write32(sc, LAUNCH_2D,	x | (y << 16));
    782 }
    783 
    784 static void
    785 voodoofb_setup_mono(struct voodoofb_softc *sc, int xd, int yd, int width, int height, uint32_t fg,
    786 					uint32_t bg)
    787 {
    788 	uint32_t dfmt, sfmt = sc->linebytes;
    789 
    790 	dfmt = sc->linebytes | ((sc->bits_per_pixel +
    791 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    792 
    793 	voodoo3_make_room(sc, 9);
    794 	voodoo3_write32(sc, SRCFORMAT,	sfmt);
    795 	voodoo3_write32(sc, DSTFORMAT,	dfmt);
    796 	voodoo3_write32(sc, COLORFORE,	fg);
    797 	voodoo3_write32(sc, COLORBACK,	bg);
    798 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
    799 	voodoo3_write32(sc, DSTXY,	xd | (yd << 16));
    800 	voodoo3_write32(sc, SRCXY,	0);
    801 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
    802 	    (ROP_COPY << 24) | SST_2D_GO);
    803 
    804 	/* now feed the data into the chip */
    805 }
    806 
    807 static void
    808 voodoofb_feed_line(struct voodoofb_softc *sc, int count, uint8_t *data)
    809 {
    810 	int i;
    811 	uint32_t latch = 0, bork;
    812 	int shift = 0;
    813 
    814 	voodoo3_make_room(sc, count);
    815 	for (i = 0; i < count; i++) {
    816 		bork = data[i];
    817 		latch |= (bork << shift);
    818 		if (shift == 24) {
    819 			voodoo3_write32(sc, LAUNCH_2D, latch);
    820 			latch = 0;
    821 			shift = 0;
    822 		} else
    823 			shift += 8;
    824 		}
    825 	if (shift != 24)
    826 		voodoo3_write32(sc, LAUNCH_2D, latch);
    827 }
    828 
    829 #ifdef VOODOOFB_DEBUG
    830 static void
    831 voodoofb_showpal(struct voodoofb_softc *sc)
    832 {
    833 	int i, x = 0;
    834 
    835 	for (i = 0; i < 16; i++) {
    836 		voodoofb_rectfill(sc, x, 0, 64, 64, i);
    837 		x += 64;
    838 	}
    839 }
    840 #endif
    841 
    842 #if 0
    843 static int
    844 voodoofb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    845 {
    846 
    847 	return 0;
    848 }
    849 #endif
    850 
    851 /*
    852  * wsdisplay_accessops
    853  */
    854 
    855 static int
    856 voodoofb_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
    857 {
    858 	struct vcons_data *vd = v;
    859 	struct voodoofb_softc *sc = vd->cookie;
    860 	struct wsdisplay_fbinfo *wdf;
    861 	struct vcons_screen *ms = vd->active;
    862 
    863 	switch (cmd) {
    864 		case WSDISPLAYIO_GTYPE:
    865 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    866 			return 0;
    867 
    868 		case WSDISPLAYIO_GINFO:
    869 			wdf = (void *)data;
    870 			wdf->height = ms->scr_ri.ri_height;
    871 			wdf->width = ms->scr_ri.ri_width;
    872 			wdf->depth = ms->scr_ri.ri_depth;
    873 			wdf->cmsize = 256;
    874 			return 0;
    875 
    876 		case WSDISPLAYIO_GETCMAP:
    877 			return voodoofb_getcmap(sc,
    878 			    (struct wsdisplay_cmap *)data);
    879 
    880 		case WSDISPLAYIO_PUTCMAP:
    881 			return voodoofb_putcmap(sc,
    882 			    (struct wsdisplay_cmap *)data);
    883 
    884 		/* PCI config read/write passthrough. */
    885 		case PCI_IOC_CFGREAD:
    886 		case PCI_IOC_CFGWRITE:
    887 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    888 			    cmd, data, flag, l));
    889 
    890 		case WSDISPLAYIO_SMODE:
    891 			{
    892 				int new_mode = *(int*)data;
    893 				if (new_mode != sc->sc_mode)
    894 				{
    895 					sc->sc_mode = new_mode;
    896 					if(new_mode == WSDISPLAYIO_MODE_EMUL)
    897 					{
    898 						int i;
    899 
    900 						/* restore the palette */
    901 						for (i = 0; i < 256; i++) {
    902 							voodoofb_putpalreg(sc,
    903 							   i,
    904 							   sc->sc_cmap_red[i],
    905 							   sc->sc_cmap_green[i],
    906 							   sc->sc_cmap_blue[i]);
    907 						}
    908 						vcons_redraw_screen(ms);
    909 					}
    910 				}
    911 			}
    912 	}
    913 	return EPASSTHROUGH;
    914 }
    915 
    916 static paddr_t
    917 voodoofb_mmap(void *v, off_t offset, int prot)
    918 {
    919 	struct vcons_data *vd = v;
    920 	struct voodoofb_softc *sc = vd->cookie;
    921 	paddr_t pa;
    922 
    923 	/* 'regular' framebuffer mmap()ing */
    924 	if (offset < sc->sc_fbsize) {
    925 		pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
    926 		    BUS_SPACE_MAP_LINEAR);
    927 		return pa;
    928 	}
    929 
    930 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    931 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    932 		    BUS_SPACE_MAP_LINEAR);
    933 		return pa;
    934 	}
    935 
    936 	if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
    937 	    sc->sc_regsize))) {
    938 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    939 		    BUS_SPACE_MAP_LINEAR);
    940 		return pa;
    941 	}
    942 
    943 	/* allow mapping of IO space */
    944 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    945 		pa = bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    946 		    BUS_SPACE_MAP_LINEAR);
    947 		return pa;
    948 	}
    949 #ifdef OFB_ALLOW_OTHERS
    950 	if (offset >= 0x80000000) {
    951 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    952 		    BUS_SPACE_MAP_LINEAR);
    953 		return pa;
    954 	}
    955 #endif
    956 	return -1;
    957 }
    958 
    959 static void
    960 voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
    961     int existing, long *defattr)
    962 {
    963 	struct voodoofb_softc *sc = cookie;
    964 	struct rasops_info *ri = &scr->scr_ri;
    965 
    966 	ri->ri_depth = sc->bits_per_pixel;
    967 	ri->ri_width = sc->width;
    968 	ri->ri_height = sc->height;
    969 	ri->ri_stride = sc->width;
    970 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    971 
    972 	ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
    973 
    974 #ifdef VOODOOFB_DEBUG
    975 	printf("addr: %08lx\n", (ulong)ri->ri_bits);
    976 #endif
    977 	if (existing) {
    978 		ri->ri_flg |= RI_CLEAR;
    979 	}
    980 
    981 	rasops_init(ri, sc->height/8, sc->width/8);
    982 	ri->ri_caps = WSSCREEN_WSCOLORS;
    983 
    984 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
    985 		    sc->width / ri->ri_font->fontwidth);
    986 
    987 	ri->ri_hw = scr;
    988 	ri->ri_ops.copyrows = voodoofb_copyrows;
    989 	ri->ri_ops.copycols = voodoofb_copycols;
    990 	ri->ri_ops.eraserows = voodoofb_eraserows;
    991 	ri->ri_ops.erasecols = voodoofb_erasecols;
    992 	ri->ri_ops.cursor = voodoofb_cursor;
    993 	ri->ri_ops.putchar = voodoofb_putchar;
    994 }
    995 
    996 #if 0
    997 int
    998 voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    999 {
   1000 
   1001 	return 0;
   1002 }
   1003 #endif
   1004 
   1005 static int
   1006 voodoofb_intr(void *arg)
   1007 {
   1008 	struct voodoofb_softc *sc = arg;
   1009 
   1010 	voodoo3_write32(sc, V3_STATUS, 0);	/* clear interrupts */
   1011 	return 1;
   1012 }
   1013 
   1014 /* video mode stuff */
   1015 
   1016 #define REFFREQ 14318	/* .18 */
   1017 
   1018 #define ABS(a) ((a < 0) ? -a : a)
   1019 
   1020 static int
   1021 voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
   1022 {
   1023 	int m, n, k, best_m, best_n, best_k, f_cur, best_error;
   1024 	int minm, maxm;
   1025 
   1026 	best_error = freq;
   1027 	best_n = best_m = best_k = 0;
   1028 
   1029 	if (isBanshee) {
   1030 		minm = 24;
   1031 		maxm = 24;
   1032 	} else {
   1033 		minm = 1;
   1034 		maxm = 57;
   1035 		/* This used to be 64, alas it seems the last 8 (funny that ?)
   1036 		 * values cause jittering at lower resolutions. I've not done
   1037 		 * any calculations to what the adjustment affects clock ranges,
   1038 		 * but I can still run at 1600x1200@75Hz */
   1039 	}
   1040 	for (n = 1; n < 256; n++) {
   1041 		f_cur = REFFREQ * (n + 2);
   1042 		if (f_cur < freq) {
   1043 			f_cur = f_cur / 3;
   1044 			if (freq - f_cur < best_error) {
   1045 				best_error = freq - f_cur;
   1046 				best_n = n;
   1047 				best_m = 1;
   1048 				best_k = 0;
   1049 				continue;
   1050       			}
   1051 		}
   1052 		for (m = minm; m < maxm; m++) {
   1053 			for (k = 0; k < 4; k++) {
   1054 				f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1055 				if (ABS(f_cur - freq) < best_error) {
   1056 					best_error = ABS(f_cur - freq);
   1057 					best_n = n;
   1058 					best_m = m;
   1059 					best_k = k;
   1060 				}
   1061 			}
   1062 		}
   1063 	}
   1064 	n = best_n;
   1065 	m = best_m;
   1066 	k = best_k;
   1067 	*f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1068 	return ( n << 8) | (m << 2) | k;
   1069 }
   1070 
   1071 static void
   1072 voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
   1073 {
   1074 	struct voodoo_regs mod;
   1075 	struct voodoo_regs *mode;
   1076 	uint32_t horizontal_display_end, horizontal_sync_start,
   1077 		horizontal_sync_end, horizontal_total,
   1078 		horizontal_blanking_start, horizontal_blanking_end;
   1079 
   1080 	uint32_t vertical_display_enable_end, vertical_sync_start,
   1081 		vertical_sync_end, vertical_total, vertical_blanking_start,
   1082 		vertical_blanking_end;
   1083 
   1084 	uint32_t wd; // CRTC offset
   1085 
   1086 	int i;
   1087 
   1088 	uint8_t misc;
   1089 
   1090 	memset(&mod, 0, sizeof(mode));
   1091 
   1092 	mode = &mod;
   1093 
   1094 	wd = (vm->hdisplay >> 3) - 1;
   1095 	horizontal_display_end	= (vm->hdisplay >> 3) - 1;
   1096 	horizontal_sync_start	= (vm->hsync_start >> 3) - 1;
   1097 	horizontal_sync_end	= (vm->hsync_end >> 3) - 1;
   1098 	horizontal_total  	= (vm->htotal   >> 3) - 1;
   1099 	horizontal_blanking_start = horizontal_display_end;
   1100 	horizontal_blanking_end = horizontal_total;
   1101 
   1102 	vertical_display_enable_end  = vm->vdisplay - 1;
   1103 	vertical_sync_start  	= vm->vsync_start;	// - 1;
   1104 	vertical_sync_end	= vm->vsync_end;	// - 1;
   1105 	vertical_total		= vm->vtotal - 2;
   1106 	vertical_blanking_start	= vertical_display_enable_end;
   1107 	vertical_blanking_end	= vertical_total;
   1108 
   1109 	misc = 0x0f |
   1110 	    (vm->hdisplay < 400 ? 0xa0 :
   1111 		vm->hdisplay < 480 ? 0x60 :
   1112 		vm->hdisplay < 768 ? 0xe0 : 0x20);
   1113 
   1114 	mode->vr_seq[0] = 3;
   1115 	mode->vr_seq[1] = 1;
   1116 	mode->vr_seq[2] = 8;
   1117 	mode->vr_seq[3] = 0;
   1118 	mode->vr_seq[4] = 6;
   1119 
   1120 	/* crtc regs start */
   1121 	mode->vr_crtc[0] = horizontal_total - 4;
   1122 	mode->vr_crtc[1] = horizontal_display_end;
   1123 	mode->vr_crtc[2] = horizontal_blanking_start;
   1124 	mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
   1125 	mode->vr_crtc[4] = horizontal_sync_start;
   1126 
   1127 	mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
   1128 	    (horizontal_sync_end & 0x1f);
   1129 	mode->vr_crtc[6] = vertical_total;
   1130 	mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
   1131 	    ((vertical_display_enable_end & 0x200) >> 3) |
   1132 	    ((vertical_total & 0x200) >> 4) |
   1133 	    0x10 |
   1134 	    ((vertical_blanking_start & 0x100) >> 5) |
   1135 	    ((vertical_sync_start  & 0x100) >> 6) |
   1136 	    ((vertical_display_enable_end  & 0x100) >> 7) |
   1137 	    ((vertical_total  & 0x100) >> 8);
   1138 
   1139 	mode->vr_crtc[8] = 0;
   1140 	mode->vr_crtc[9] = 0x40 |
   1141 	    ((vertical_blanking_start & 0x200) >> 4);
   1142 
   1143 	mode->vr_crtc[10] = 0;
   1144 	mode->vr_crtc[11] = 0;
   1145 	mode->vr_crtc[12] = 0;
   1146 	mode->vr_crtc[13] = 0;
   1147 	mode->vr_crtc[14] = 0;
   1148 	mode->vr_crtc[15] = 0;
   1149 
   1150 	mode->vr_crtc[16] = vertical_sync_start;
   1151 	mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
   1152 	mode->vr_crtc[18] = vertical_display_enable_end;
   1153 	mode->vr_crtc[19] = wd; // CRTC offset
   1154 	mode->vr_crtc[20] = 0;
   1155 	mode->vr_crtc[21] = vertical_blanking_start;
   1156 	mode->vr_crtc[22] = vertical_blanking_end + 1;
   1157 	mode->vr_crtc[23] = 128;
   1158 	mode->vr_crtc[24] = 255;
   1159 
   1160 	/* attr regs start */
   1161 	mode->vr_attr[0] = 0;
   1162 	mode->vr_attr[1] = 0;
   1163 	mode->vr_attr[2] = 0;
   1164 	mode->vr_attr[3] = 0;
   1165 	mode->vr_attr[4] = 0;
   1166 	mode->vr_attr[5] = 0;
   1167 	mode->vr_attr[6] = 0;
   1168 	mode->vr_attr[7] = 0;
   1169 	mode->vr_attr[8] = 0;
   1170 	mode->vr_attr[9] = 0;
   1171 	mode->vr_attr[10] = 0;
   1172 	mode->vr_attr[11] = 0;
   1173 	mode->vr_attr[12] = 0;
   1174 	mode->vr_attr[13] = 0;
   1175 	mode->vr_attr[14] = 0;
   1176 	mode->vr_attr[15] = 0;
   1177 	mode->vr_attr[16] = 1;
   1178 	mode->vr_attr[17] = 0;
   1179 	mode->vr_attr[18] = 15;
   1180 	mode->vr_attr[19] = 0;
   1181 	/* attr regs end */
   1182 
   1183 	/* graph regs start */
   1184 	mode->vr_graph[0] = 159;
   1185 	mode->vr_graph[1] = 127;
   1186 	mode->vr_graph[2] = 127;
   1187 	mode->vr_graph[3] = 131;
   1188 	mode->vr_graph[4] = 130;
   1189 	mode->vr_graph[5] = 142;
   1190 	mode->vr_graph[6] = 30;
   1191 	mode->vr_graph[7] = 245;
   1192 	mode->vr_graph[8] = 0;
   1193 
   1194 	vga_outb(sc, MISC_W, misc | 0x01);
   1195 
   1196 	for(i = 0; i < 5; i++)
   1197 		voodoo3_write_seq(sc, i, mode->vr_seq[i]);
   1198 	for (i = 0; i < 0x19; i ++)
   1199         	voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
   1200 	for (i = 0; i < 0x14; i ++)
   1201         	voodoo3_write_attr(sc, i, mode->vr_attr[i]);
   1202 	for (i = 0; i < 0x09; i ++)
   1203         	voodoo3_write_gra(sc, i, mode->vr_graph[i]);
   1204 }
   1205 
   1206 static void
   1207 voodoofb_set_videomode(struct voodoofb_softc *sc,
   1208     const struct videomode *vm)
   1209 {
   1210 	uint32_t miscinit0 = 0;
   1211 	int vidpll, fout;
   1212 	uint32_t vp, vidproc = VIDPROCDEFAULT;
   1213 	uint32_t bpp = 1;	/* for now */
   1214 	uint32_t bytes_per_row = vm->hdisplay * bpp;
   1215 
   1216 	sc->bits_per_pixel = bpp << 3;
   1217 	sc->width = vm->hdisplay;
   1218 	sc->height = vm->vdisplay;
   1219 	sc->linebytes = bytes_per_row;
   1220 
   1221 	voodoofb_setup_monitor(sc, vm);
   1222 	vp = voodoo3_read32(sc, VIDPROCCFG);
   1223 
   1224 	vidproc &= ~(0x1c0000); // clear bits 18 to 20, bpp in vidproccfg
   1225 	vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT); // enable bits 18 to 20 to the required bpp
   1226 
   1227 	vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
   1228 
   1229 #ifdef VOODOOFB_DEBUG
   1230 	printf("old vidproc: %08x\n", vp);
   1231 	printf("pll: %08x %d\n", vidpll, fout);
   1232 #endif
   1233 
   1234 	switch (bpp) { // bit 10 of vidproccfg, is enabled or disabled as needed for the Desktop
   1235 		case 1:
   1236 			vidproc &= ~(1 << 10); // bit 10 off for palettized modes only, off means palette is used
   1237 			break;
   1238 #if 0
   1239 		case 2:
   1240 			#if __POWERPC__
   1241 				miscinit0 = 0xc0000000;
   1242 			#endif
   1243 			vidproc |= (1 << 10); // bypass palette for 16bit modes
   1244 			break;
   1245 		case 4:
   1246 			#if __POWERPC__
   1247 				miscinit0 = 0x40000000;
   1248 			#endif
   1249 			vidproc |= (1 << 10); // Same for 32bit modes
   1250 			break;
   1251 #endif
   1252 		default:
   1253 			printf("We support only 8 bit for now\n");
   1254 			return;
   1255 	}
   1256 
   1257 	voodoofb_wait_idle(sc);
   1258 
   1259 	voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
   1260 
   1261 	voodoo3_make_room(sc, 4);
   1262 	voodoo3_write32(sc, VGAINIT0, 4928);
   1263 	voodoo3_write32(sc, DACMODE, 0);
   1264 	voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
   1265 	voodoo3_write32(sc, PLLCTRL0, vidpll);
   1266 
   1267 	voodoo3_make_room(sc, 5);
   1268 	voodoo3_write32(sc, VIDSCREENSIZE, sc->width | (sc->height << 12));
   1269 	voodoo3_write32(sc, VIDDESKSTART,  1024);
   1270 
   1271 	vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
   1272 	voodoo3_write32(sc, VIDPROCCFG, vidproc);
   1273 
   1274 	voodoo3_write32(sc, VGAINIT1, 0);
   1275 	voodoo3_write32(sc, MISCINIT0, miscinit0);
   1276 #ifdef VOODOOFB_DEBUG
   1277 	printf("vidproc: %08x\n", vidproc);
   1278 #endif
   1279 	voodoo3_make_room(sc, 8);
   1280 	voodoo3_write32(sc, SRCBASE, 0);
   1281 	voodoo3_write32(sc, DSTBASE, 0);
   1282 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1283   	voodoo3_write32(sc, CLIP0MIN,        0);
   1284   	voodoo3_write32(sc, CLIP0MAX,        0x0fff0fff);
   1285   	voodoo3_write32(sc, CLIP1MIN,        0);
   1286   	voodoo3_write32(sc, CLIP1MAX,        0x0fff0fff);
   1287 	voodoo3_write32(sc, SRCXY, 0);
   1288 	voodoofb_wait_idle(sc);
   1289 	printf("%s: switched to %dx%d, %d bit\n", sc->sc_dev.dv_xname,
   1290 	    sc->width, sc->height, sc->bits_per_pixel);
   1291 }
   1292 
   1293 static void
   1294 voodoofb_init(struct voodoofb_softc *sc)
   1295 {
   1296 	/* XXX */
   1297 	uint32_t vgainit0 = 0;
   1298 	uint32_t vidcfg = 0;
   1299 /*	uint32_t cpp = 16;*/
   1300 
   1301 #ifdef VOODOOFB_DEBUG
   1302 	printf("initializing engine...");
   1303 #endif
   1304 	vgainit0 = voodoo3_read32(sc, VGAINIT0);
   1305 #ifdef VOODOOFB_DEBUG
   1306 	printf("vga: %08x", vgainit0);
   1307 #endif
   1308 	vgainit0 |=
   1309 	    VGAINIT0_8BIT_DAC     |
   1310 	    VGAINIT0_EXT_ENABLE   |
   1311 	    VGAINIT0_WAKEUP_3C3   |
   1312 	    VGAINIT0_ALT_READBACK |
   1313 	    VGAINIT0_EXTSHIFTOUT;
   1314 
   1315 	vidcfg = voodoo3_read32(sc, VIDPROCCFG);
   1316 #ifdef VOODOOFB_DEBUG
   1317 	printf(" vidcfg: %08x\n", vidcfg);
   1318 #endif
   1319 	vidcfg |=
   1320 	    VIDCFG_VIDPROC_ENABLE |
   1321 	    VIDCFG_DESK_ENABLE;
   1322 	vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
   1323 
   1324 	voodoo3_make_room(sc, 2);
   1325 
   1326 	voodoo3_write32(sc, VGAINIT0, vgainit0);
   1327 	voodoo3_write32(sc, VIDPROCCFG, vidcfg);
   1328 
   1329 	voodoo3_make_room(sc, 8);
   1330 	voodoo3_write32(sc, SRCBASE, 0);
   1331 	voodoo3_write32(sc, DSTBASE, 0);
   1332 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1333   	voodoo3_write32(sc, CLIP0MIN,        0);
   1334   	voodoo3_write32(sc, CLIP0MAX,        0x1fff1fff);
   1335   	voodoo3_write32(sc, CLIP1MIN,        0);
   1336   	voodoo3_write32(sc, CLIP1MAX,        0x1fff1fff);
   1337 	voodoo3_write32(sc, SRCXY, 0);
   1338 
   1339 	voodoofb_wait_idle(sc);
   1340 }
   1341