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