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