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