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