Home | History | Annotate | Line # | Download | only in pci
voodoofb.c revision 1.36
      1 /*	$NetBSD: voodoofb.c,v 1.36 2012/01/25 02:04:35 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     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.36 2012/01/25 02:04:35 macallan 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 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/pcireg.h>
     47 #include <dev/pci/pcidevs.h>
     48 #include <dev/pci/pciio.h>
     49 #include <dev/pci/voodoofbreg.h>
     50 
     51 #include <dev/wscons/wsdisplayvar.h>
     52 #include <dev/wscons/wsconsio.h>
     53 #include <dev/wsfont/wsfont.h>
     54 #include <dev/rasops/rasops.h>
     55 #include <dev/wscons/wsdisplay_vconsvar.h>
     56 #include <dev/pci/wsdisplay_pci.h>
     57 
     58 #include <dev/i2c/i2cvar.h>
     59 #include <dev/i2c/i2c_bitbang.h>
     60 #include <dev/i2c/ddcvar.h>
     61 #include <dev/videomode/videomode.h>
     62 #include <dev/videomode/edidvar.h>
     63 #include <dev/videomode/edidreg.h>
     64 
     65 #include "opt_wsemul.h"
     66 
     67 struct voodoofb_softc {
     68 	device_t sc_dev;
     69 	pci_chipset_tag_t sc_pc;
     70 	pcitag_t sc_pcitag;
     71 	struct pci_attach_args sc_pa;
     72 
     73 	bus_space_tag_t sc_memt;
     74 	bus_space_tag_t sc_iot;
     75 	bus_space_handle_t sc_memh;
     76 
     77 	bus_space_tag_t sc_regt;
     78 	bus_space_tag_t sc_ioregt;
     79 	bus_space_handle_t sc_regh;
     80 	bus_space_handle_t sc_ioregh;
     81 	bus_addr_t sc_regs, sc_fb, sc_ioreg;
     82 	bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
     83 
     84 	void *sc_ih;
     85 
     86 	size_t sc_memsize;
     87 	int sc_memtype;
     88 
     89 	int sc_bits_per_pixel;
     90 	int sc_width, sc_height, sc_linebytes;
     91 	const struct videomode *sc_videomode;
     92 
     93 	/* i2c stuff */
     94 	struct i2c_controller sc_i2c;
     95 	uint8_t sc_edid_data[128];
     96 	struct edid_info sc_edid_info;
     97 	uint32_t sc_i2creg;
     98 
     99 	int sc_mode;
    100 	uint32_t sc_bg;
    101 
    102 	u_char sc_cmap_red[256];
    103 	u_char sc_cmap_green[256];
    104 	u_char sc_cmap_blue[256];
    105 	int sc_dacw;
    106 
    107 	struct vcons_data vd;
    108 };
    109 
    110 struct voodoo_regs {
    111 	uint8_t vr_crtc[31];
    112 	uint8_t vr_graph[9];
    113 	uint8_t vr_attr[21];
    114 	uint8_t vr_seq[5];
    115 };
    116 
    117 static struct vcons_screen voodoofb_console_screen;
    118 
    119 static int	voodoofb_match(device_t, cfdata_t, void *);
    120 static void	voodoofb_attach(device_t, device_t, void *);
    121 
    122 static int	voodoofb_drm_print(void *, const char *);
    123 static int	voodoofb_drm_unmap(struct voodoofb_softc *);
    124 static int	voodoofb_drm_map(struct voodoofb_softc *);
    125 
    126 CFATTACH_DECL_NEW(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
    127     voodoofb_attach, NULL, NULL);
    128 
    129 static bool	voodoofb_is_console(struct voodoofb_softc *);
    130 static void 	voodoofb_init(struct voodoofb_softc *);
    131 
    132 static void	voodoofb_cursor(void *, int, int, int);
    133 static void	voodoofb_putchar(void *, int, int, u_int, long);
    134 static void	voodoofb_putchar_aa(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 static void	voodoofb_wait_idle(struct voodoofb_softc *);
    163 
    164 #ifdef VOODOOFB_ENABLE_INTR
    165 static int	voodoofb_intr(void *);
    166 #endif
    167 
    168 static void	voodoofb_set_videomode(struct voodoofb_softc *,
    169 			    const struct videomode *);
    170 
    171 struct wsscreen_descr voodoofb_defaultscreen = {
    172 	"default",
    173 	0, 0,
    174 	NULL,
    175 	8, 16,
    176 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    177 	NULL,
    178 };
    179 
    180 const struct wsscreen_descr *_voodoofb_scrlist[] = {
    181 	&voodoofb_defaultscreen,
    182 	/* XXX other formats, graphics screen? */
    183 };
    184 
    185 struct wsscreen_list voodoofb_screenlist = {
    186 	sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
    187 };
    188 
    189 static int	voodoofb_ioctl(void *, void *, u_long, void *, int,
    190 		    struct lwp *);
    191 static paddr_t	voodoofb_mmap(void *, void *, off_t, int);
    192 
    193 static void	voodoofb_clearscreen(struct voodoofb_softc *);
    194 static void	voodoofb_init_screen(void *, struct vcons_screen *, int,
    195 			    long *);
    196 
    197 
    198 struct wsdisplay_accessops voodoofb_accessops = {
    199 	voodoofb_ioctl,
    200 	voodoofb_mmap,
    201 	NULL,
    202 	NULL,
    203 	NULL,
    204 	NULL,	/* load_font */
    205 	NULL,	/* polls */
    206 	NULL,	/* scroll */
    207 };
    208 
    209 /* I2C glue */
    210 static int voodoofb_i2c_acquire_bus(void *, int);
    211 static void voodoofb_i2c_release_bus(void *, int);
    212 static int voodoofb_i2c_send_start(void *, int);
    213 static int voodoofb_i2c_send_stop(void *, int);
    214 static int voodoofb_i2c_initiate_xfer(void *, i2c_addr_t, int);
    215 static int voodoofb_i2c_read_byte(void *, uint8_t *, int);
    216 static int voodoofb_i2c_write_byte(void *, uint8_t, int);
    217 
    218 /* I2C bitbang glue */
    219 static void voodoofb_i2cbb_set_bits(void *, uint32_t);
    220 static void voodoofb_i2cbb_set_dir(void *, uint32_t);
    221 static uint32_t voodoofb_i2cbb_read(void *);
    222 
    223 static void voodoofb_setup_i2c(struct voodoofb_softc *);
    224 
    225 static const struct i2c_bitbang_ops voodoofb_i2cbb_ops = {
    226 	voodoofb_i2cbb_set_bits,
    227 	voodoofb_i2cbb_set_dir,
    228 	voodoofb_i2cbb_read,
    229 	{
    230 		VSP_SDA0_IN,
    231 		VSP_SCL0_IN,
    232 		0,
    233 		0
    234 	}
    235 };
    236 
    237 extern const u_char rasops_cmap[768];
    238 
    239 /*
    240  * Inline functions for getting access to register aperture.
    241  */
    242 static inline void
    243 voodoo3_write32(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
    244 {
    245 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
    246 }
    247 
    248 static inline void
    249 voodoo3_write32s(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
    250 {
    251 	bus_space_write_stream_4(sc->sc_regt, sc->sc_regh, reg, val);
    252 }
    253 static inline uint32_t
    254 voodoo3_read32(struct voodoofb_softc *sc, uint32_t reg)
    255 {
    256 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
    257 }
    258 
    259 static inline void
    260 voodoo3_write_crtc(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    261 {
    262 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_INDEX - 0x300, reg);
    263 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_DATA - 0x300, val);
    264 }
    265 
    266 static inline void
    267 voodoo3_write_seq(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    268 {
    269 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_INDEX - 0x300, reg);
    270 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_DATA - 0x300, val);
    271 }
    272 
    273 static inline void
    274 voodoo3_write_gra(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    275 {
    276 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_INDEX - 0x300, reg);
    277 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_DATA - 0x300, val);
    278 }
    279 
    280 static inline void
    281 voodoo3_write_attr(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    282 {
    283 	volatile uint8_t junk;
    284 	uint8_t index;
    285 
    286 	junk = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, IS1_R - 0x300);
    287 	index = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300);
    288 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, reg);
    289 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, val);
    290 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, index);
    291 }
    292 
    293 static inline void
    294 vga_outb(struct voodoofb_softc *sc, uint32_t reg,  uint8_t val)
    295 {
    296 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, reg - 0x300, val);
    297 }
    298 
    299 /* wait until there's room for len bytes in the FIFO */
    300 static inline void
    301 voodoo3_make_room(struct voodoofb_softc *sc, int len)
    302 {
    303 	while ((voodoo3_read32(sc, STATUS) & 0x1f) < len);
    304 }
    305 
    306 static void
    307 voodoofb_wait_idle(struct voodoofb_softc *sc)
    308 {
    309 	int i = 0;
    310 
    311 	voodoo3_make_room(sc, 1);
    312 	voodoo3_write32(sc, COMMAND_3D, COMMAND_3D_NOP);
    313 
    314 	while (1) {
    315 		i = (voodoo3_read32(sc, STATUS) & STATUS_BUSY) ? 0 : i + 1;
    316 		if(i == 3) break;
    317 	}
    318 }
    319 
    320 static int
    321 voodoofb_match(device_t parent, cfdata_t match, void *aux)
    322 {
    323 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    324 
    325 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    326 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    327 		return 0;
    328 	if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
    329 	    (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_VOODOO3))
    330 		return 100;
    331 	return 0;
    332 }
    333 
    334 static void
    335 voodoofb_attach(device_t parent, device_t self, void *aux)
    336 {
    337 	struct voodoofb_softc *sc = device_private(self);
    338 	struct pci_attach_args *pa = aux;
    339 	char devinfo[256];
    340 	struct wsemuldisplaydev_attach_args aa;
    341 	struct rasops_info *ri;
    342 #ifdef VOODOOFB_ENABLE_INTR
    343 	pci_intr_handle_t ih;
    344 	const char *intrstr;
    345 #endif
    346 	ulong defattr;
    347 	int console, width, height, i, j;
    348 	prop_dictionary_t dict;
    349 	int linebytes, depth, flags;
    350 	uint32_t bg, fg, ul;
    351 
    352 	sc->sc_dev = self;
    353 
    354 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    355 	sc->sc_pc = pa->pa_pc;
    356 	sc->sc_pcitag = pa->pa_tag;
    357 	sc->sc_dacw = -1;
    358 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    359 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    360 
    361 	sc->sc_memt = pa->pa_memt;
    362 	sc->sc_iot = pa->pa_iot;
    363 	sc->sc_pa = *pa;
    364 
    365 	/* the framebuffer */
    366 	if (pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, 0x14, PCI_MAPREG_TYPE_MEM,
    367 	    &sc->sc_fb, &sc->sc_fbsize, &flags)) {
    368 		aprint_error_dev(self, "failed to map the frame buffer.\n");
    369 	}
    370 	sc->sc_memsize = sc->sc_fbsize >> 1;	/* VRAM aperture is 2x VRAM */
    371 
    372 	/* memory-mapped registers */
    373 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    374 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
    375 		aprint_error_dev(self, "failed to map memory-mapped registers.\n");
    376 	}
    377 
    378 	/* IO-mapped registers */
    379 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
    380 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
    381 	    &sc->sc_ioregsize)) {
    382 		aprint_error_dev(self, "failed to map IO-mapped registers.\n");
    383 	}
    384 	voodoofb_init(sc);
    385 
    386 	/* we should read these from the chip instead of depending on OF */
    387 	width = height = -1;
    388 
    389 	dict = device_properties(self);
    390 	if (!prop_dictionary_get_uint32(dict, "width", &width)) {
    391 		aprint_error_dev(self, "no width property\n");
    392 		return;
    393 	}
    394 	if (!prop_dictionary_get_uint32(dict, "height", &height)) {
    395 		aprint_error_dev(self, "no height property\n");
    396 		return;
    397 	}
    398 	if (!prop_dictionary_get_uint32(dict, "depth", &depth)) {
    399 		aprint_error_dev(self, "no depth property\n");
    400 		return;
    401 	}
    402 	linebytes = width;			/* XXX */
    403 
    404 	if (width == -1 || height == -1)
    405 		return;
    406 
    407 	sc->sc_width = width;
    408 	sc->sc_height = height;
    409 	sc->sc_bits_per_pixel = depth;
    410 	sc->sc_linebytes = linebytes;
    411 	printf("%s: initial resolution %dx%d, %d bit\n", device_xname(self),
    412 	    sc->sc_width, sc->sc_height, sc->sc_bits_per_pixel);
    413 
    414 	sc->sc_videomode = NULL;
    415 	voodoofb_setup_i2c(sc);
    416 
    417 	/* XXX this should at least be configurable via kernel config */
    418 	if (sc->sc_videomode == NULL) {
    419 		sc->sc_videomode = pick_mode_by_ref(width, height, 60);
    420 	}
    421 
    422 	voodoofb_set_videomode(sc, sc->sc_videomode);
    423 
    424 	vcons_init(&sc->vd, sc, &voodoofb_defaultscreen, &voodoofb_accessops);
    425 	sc->vd.init_screen = voodoofb_init_screen;
    426 
    427 	console = voodoofb_is_console(sc);
    428 
    429 	ri = &voodoofb_console_screen.scr_ri;
    430 	if (console) {
    431 		vcons_init_screen(&sc->vd, &voodoofb_console_screen, 1,
    432 		    &defattr);
    433 		voodoofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    434 
    435 		voodoofb_defaultscreen.textops = &ri->ri_ops;
    436 		voodoofb_defaultscreen.capabilities = ri->ri_caps;
    437 		voodoofb_defaultscreen.nrows = ri->ri_rows;
    438 		voodoofb_defaultscreen.ncols = ri->ri_cols;
    439 		wsdisplay_cnattach(&voodoofb_defaultscreen, ri, 0, 0, defattr);
    440 	} else {
    441 		/*
    442 		 * since we're not the console we can postpone the rest
    443 		 * until someone actually allocates a screen for us
    444 		 */
    445 		voodoofb_set_videomode(sc, sc->sc_videomode);
    446 	}
    447 
    448 	printf("%s: %d MB aperture at 0x%08x, %d MB registers at 0x%08x\n",
    449 	    device_xname(self), (u_int)(sc->sc_fbsize >> 20),
    450 	    (u_int)sc->sc_fb, (u_int)(sc->sc_regsize >> 20),
    451 	    (u_int)sc->sc_regs);
    452 #ifdef VOODOOFB_DEBUG
    453 	printf("fb: %08lx\n", (ulong)ri->ri_bits);
    454 #endif
    455 
    456 	j = 0;
    457 	if (sc->sc_bits_per_pixel == 8) {
    458 		uint8_t tmp;
    459 		for (i = 0; i < 256; i++) {
    460 			tmp = i & 0xe0;
    461 			/*
    462 			 * replicate bits so 0xe0 maps to a red value of 0xff
    463 			 * in order to make white look actually white
    464 			 */
    465 			tmp |= (tmp >> 3) | (tmp >> 6);
    466 			sc->sc_cmap_red[i] = tmp;
    467 
    468 			tmp = (i & 0x1c) << 3;
    469 			tmp |= (tmp >> 3) | (tmp >> 6);
    470 			sc->sc_cmap_green[i] = tmp;
    471 
    472 			tmp = (i & 0x03) << 6;
    473 			tmp |= tmp >> 2;
    474 			tmp |= tmp >> 4;
    475 			sc->sc_cmap_blue[i] = tmp;
    476 
    477 			voodoofb_putpalreg(sc, i, sc->sc_cmap_red[i],
    478 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    479 		}
    480 	} else {
    481 		/* linear ramp */
    482 		for (i = 0; i < 256; i++) {
    483 			sc->sc_cmap_red[i] = i;
    484 			sc->sc_cmap_green[i] = i;
    485 			sc->sc_cmap_blue[i] = i;
    486 
    487 			voodoofb_putpalreg(sc, i, sc->sc_cmap_red[i],
    488 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    489 		}
    490 	}
    491 
    492 #ifdef VOODOOFB_ENABLE_INTR
    493 	/* Interrupt. We don't use it for anything yet */
    494 	if (pci_intr_map(pa, &ih)) {
    495 		aprint_error_dev(self, "failed to map interrupt\n");
    496 		return;
    497 	}
    498 
    499 	intrstr = pci_intr_string(sc->sc_pc, ih);
    500 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, voodoofb_intr,
    501 	    sc);
    502 	if (sc->sc_ih == NULL) {
    503 		aprint_error_dev(self, "failed to establish interrupt");
    504 		if (intrstr != NULL)
    505 			aprint_error(" at %s", intrstr);
    506 		aprint_error("\n");
    507 		return;
    508 	}
    509 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    510 #endif
    511 
    512 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    513 	sc->sc_bg = ri->ri_devcmap[bg];
    514 	voodoofb_clearscreen(sc);
    515 
    516 	if (console)
    517 		vcons_replay_msgbuf(&voodoofb_console_screen);
    518 	aa.console = console;
    519 	aa.scrdata = &voodoofb_screenlist;
    520 	aa.accessops = &voodoofb_accessops;
    521 	aa.accesscookie = &sc->vd;
    522 
    523 	config_found(self, &aa, wsemuldisplaydevprint);
    524 	config_found_ia(self, "drm", aux, voodoofb_drm_print);
    525 }
    526 
    527 static int
    528 voodoofb_drm_print(void *opaque, const char *pnp)
    529 {
    530 	if (pnp)
    531 		aprint_normal("drm at %s", pnp);
    532 
    533 	return UNCONF;
    534 }
    535 
    536 static int
    537 voodoofb_drm_unmap(struct voodoofb_softc *sc)
    538 {
    539 	printf("%s: releasing bus resources\n", device_xname(sc->sc_dev));
    540 
    541 	bus_space_unmap(sc->sc_ioregt, sc->sc_ioregh, sc->sc_ioregsize);
    542 	bus_space_unmap(sc->sc_regt, sc->sc_regh, sc->sc_regsize);
    543 
    544 	return 0;
    545 }
    546 
    547 static int
    548 voodoofb_drm_map(struct voodoofb_softc *sc)
    549 {
    550 
    551 	/* memory-mapped registers */
    552 	if (pci_mapreg_map(&sc->sc_pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    553 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
    554 		aprint_error_dev(sc->sc_dev, "failed to map memory-mapped registers.\n");
    555 	}
    556 
    557 	/* IO-mapped registers */
    558 	if (pci_mapreg_map(&sc->sc_pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
    559 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
    560 	    &sc->sc_ioregsize)) {
    561 		aprint_error_dev(sc->sc_dev, "failed to map IO-mapped registers.\n");
    562 	}
    563 
    564 	voodoofb_init(sc);
    565 	/* XXX this should at least be configurable via kernel config */
    566 	voodoofb_set_videomode(sc, sc->sc_videomode);
    567 
    568 	return 0;
    569 }
    570 
    571 static int
    572 voodoofb_putpalreg(struct voodoofb_softc *sc, uint8_t index, uint8_t r,
    573     uint8_t g, uint8_t b)
    574 {
    575 	uint32_t color;
    576 
    577 	sc->sc_cmap_red[index] = r;
    578 	sc->sc_cmap_green[index] = g;
    579 	sc->sc_cmap_blue[index] = b;
    580 
    581 	color = (r << 16) | (g << 8) | b;
    582 	voodoo3_make_room(sc, 2);
    583 	voodoo3_write32(sc, DACADDR, index);
    584 	voodoo3_write32(sc, DACDATA, color);
    585 
    586 	return 0;
    587 }
    588 
    589 static int
    590 voodoofb_putcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    591 {
    592 	u_char *r, *g, *b;
    593 	u_int index = cm->index;
    594 	u_int count = cm->count;
    595 	int i, error;
    596 	u_char rbuf[256], gbuf[256], bbuf[256];
    597 
    598 #ifdef VOODOOFB_DEBUG
    599 	printf("putcmap: %d %d\n",index, count);
    600 #endif
    601 	if (cm->index >= 256 || cm->count > 256 ||
    602 	    (cm->index + cm->count) > 256)
    603 		return EINVAL;
    604 	error = copyin(cm->red, &rbuf[index], count);
    605 	if (error)
    606 		return error;
    607 	error = copyin(cm->green, &gbuf[index], count);
    608 	if (error)
    609 		return error;
    610 	error = copyin(cm->blue, &bbuf[index], count);
    611 	if (error)
    612 		return error;
    613 
    614 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    615 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    616 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    617 
    618 	r = &sc->sc_cmap_red[index];
    619 	g = &sc->sc_cmap_green[index];
    620 	b = &sc->sc_cmap_blue[index];
    621 
    622 	for (i = 0; i < count; i++) {
    623 		voodoofb_putpalreg(sc, index, *r, *g, *b);
    624 		index++;
    625 		r++, g++, b++;
    626 	}
    627 	return 0;
    628 }
    629 
    630 static int
    631 voodoofb_getcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    632 {
    633 	u_int index = cm->index;
    634 	u_int count = cm->count;
    635 	int error;
    636 
    637 	if (index >= 255 || count > 256 || index + count > 256)
    638 		return EINVAL;
    639 
    640 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    641 	if (error)
    642 		return error;
    643 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    644 	if (error)
    645 		return error;
    646 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    647 	if (error)
    648 		return error;
    649 
    650 	return 0;
    651 }
    652 
    653 static bool
    654 voodoofb_is_console(struct voodoofb_softc *sc)
    655 {
    656 	prop_dictionary_t dict;
    657 	bool console;
    658 
    659 	dict = device_properties(sc->sc_dev);
    660 	prop_dictionary_get_bool(dict, "is_console", &console);
    661 	return console;
    662 }
    663 
    664 static void
    665 voodoofb_clearscreen(struct voodoofb_softc *sc)
    666 {
    667 	voodoofb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
    668 }
    669 
    670 /*
    671  * wsdisplay_emulops
    672  */
    673 
    674 static void
    675 voodoofb_cursor(void *cookie, int on, int row, int col)
    676 {
    677 	struct rasops_info *ri = cookie;
    678 	struct vcons_screen *scr = ri->ri_hw;
    679 	struct voodoofb_softc *sc = scr->scr_cookie;
    680 	int x, y, wi, he;
    681 
    682 	wi = ri->ri_font->fontwidth;
    683 	he = ri->ri_font->fontheight;
    684 
    685 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    686 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    687 		y = ri->ri_crow * he + ri->ri_yorigin;
    688 		if (ri->ri_flg & RI_CURSOR) {
    689 			voodoofb_rectinvert(sc, x, y, wi, he);
    690 			ri->ri_flg &= ~RI_CURSOR;
    691 		}
    692 		ri->ri_crow = row;
    693 		ri->ri_ccol = col;
    694 		if (on)
    695 		{
    696 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    697 			y = ri->ri_crow * he + ri->ri_yorigin;
    698 			voodoofb_rectinvert(sc, x, y, wi, he);
    699 			ri->ri_flg |= RI_CURSOR;
    700 		}
    701 	} else {
    702 		ri->ri_flg &= ~RI_CURSOR;
    703 		ri->ri_crow = row;
    704 		ri->ri_ccol = col;
    705 	}
    706 }
    707 
    708 #if 0
    709 int
    710 voodoofb_mapchar(void *cookie, int uni, u_int *index)
    711 {
    712 	return 0;
    713 }
    714 #endif
    715 
    716 static void
    717 voodoofb_putchar(void *cookie, int row, int col, u_int c, long attr)
    718 {
    719 	struct rasops_info *ri = cookie;
    720 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    721 	struct vcons_screen *scr = ri->ri_hw;
    722 	struct voodoofb_softc *sc = scr->scr_cookie;
    723 
    724 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    725 		uint8_t *data;
    726 		int fg, bg, uc, i;
    727 		int x, y, wi, he;
    728 
    729 		wi = font->fontwidth;
    730 		he = font->fontheight;
    731 
    732 		if (!CHAR_IN_FONT(c, font))
    733 			return;
    734 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    735 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    736 		x = ri->ri_xorigin + col * wi;
    737 		y = ri->ri_yorigin + row * he;
    738 		if (c == 0x20) {
    739 			voodoofb_rectfill(sc, x, y, wi, he, bg);
    740 		} else {
    741 			uc = c - font->firstchar;
    742 			data = (uint8_t *)font->data + uc *
    743 			    ri->ri_fontscale;
    744 				voodoofb_setup_mono(sc, x, y, wi, he, fg, bg);
    745 			for (i = 0; i < he; i++) {
    746 				voodoofb_feed_line(sc, font->stride, data);
    747 				data += font->stride;
    748 			}
    749 		}
    750 	}
    751 }
    752 
    753 static void
    754 voodoofb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
    755 {
    756 	struct rasops_info *ri = cookie;
    757 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    758 	struct vcons_screen *scr = ri->ri_hw;
    759 	struct voodoofb_softc *sc = scr->scr_cookie;
    760 	uint8_t *data;
    761 	uint32_t bg, latch = 0, bg8, fg8, pixel;
    762 	int i, x, y, wi, he, r, g, b, aval;
    763 	int r1, g1, b1, r0, g0, b0, fgo, bgo, j;
    764 
    765 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    766 		return;
    767 
    768 	if (!CHAR_IN_FONT(c, font))
    769 		return;
    770 
    771 	wi = font->fontwidth;
    772 	he = font->fontheight;
    773 
    774 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    775 	x = ri->ri_xorigin + col * wi;
    776 	y = ri->ri_yorigin + row * he;
    777 	if (c == 0x20) {
    778 		voodoofb_rectfill(sc, x, y, wi, he, bg);
    779 		return;
    780 	}
    781 	data = WSFONT_GLYPH(c, font);
    782 
    783 	voodoo3_make_room(sc, 6);
    784 	voodoo3_write32(sc, SRCFORMAT,	FMT_8BIT | FMT_PAD_BYTE);
    785 	voodoo3_write32(sc, DSTFORMAT,	sc->sc_linebytes | FMT_8BIT);
    786 	voodoo3_write32(sc, DSTSIZE,	wi | (he << 16));
    787 	voodoo3_write32(sc, DSTXY,	x | (y << 16));
    788 	voodoo3_write32(sc, SRCXY,	0);
    789 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
    790 	    (ROP_COPY << 24) | SST_2D_GO);
    791 
    792 	/*
    793 	 * we need the RGB colours here, so get offsets into rasops_cmap
    794 	 */
    795 	fgo = ((attr >> 24) & 0xf) * 3;
    796 	bgo = ((attr >> 16) & 0xf) * 3;
    797 
    798 	r0 = rasops_cmap[bgo];
    799 	r1 = rasops_cmap[fgo];
    800 	g0 = rasops_cmap[bgo + 1];
    801 	g1 = rasops_cmap[fgo + 1];
    802 	b0 = rasops_cmap[bgo + 2];
    803 	b1 = rasops_cmap[fgo + 2];
    804 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
    805 	bg8 = R3G3B2(r0, g0, b0);
    806 	fg8 = R3G3B2(r1, g1, b1);
    807 	voodoo3_make_room(sc, 15);
    808 	j = 15;
    809 	for (i = 0; i < ri->ri_fontscale; i++) {
    810 		aval = *data;
    811 		if (aval == 0) {
    812 			pixel = bg8;
    813 		} else if (aval == 255) {
    814 			pixel = fg8;
    815 		} else {
    816 			r = aval * r1 + (255 - aval) * r0;
    817 			g = aval * g1 + (255 - aval) * g0;
    818 			b = aval * b1 + (255 - aval) * b0;
    819 			pixel = ((r & 0xe000) >> 8) |
    820 				((g & 0xe000) >> 11) |
    821 				((b & 0xc000) >> 14);
    822 		}
    823 		latch = (latch << 8) | pixel;
    824 		/* write in 32bit chunks */
    825 		if ((i & 3) == 3) {
    826 			voodoo3_write32s(sc, LAUNCH_2D, latch);
    827 			/*
    828 			 * not strictly necessary, old data should be shifted
    829 			 * out
    830 			 */
    831 			latch = 0;
    832 			/*
    833 			 * check for pipeline space every now and then
    834 			 * I don't think we can feed a Voodoo3 faster than it
    835 			 * can process simple pixel data but I have been wrong
    836 			 * about that before
    837 			 */
    838 			j--;
    839 			if (j == 0) {
    840 				voodoo3_make_room(sc, 15);
    841 				j = 15;
    842 			}
    843 		}
    844 		data++;
    845 	}
    846 	/* if we have pixels left in latch write them out */
    847 	if ((i & 3) != 0) {
    848 		latch = latch << ((4 - (i & 3)) << 3);
    849 		voodoo3_write32s(sc, LAUNCH_2D, latch);
    850 	}
    851 }
    852 
    853 static void
    854 voodoofb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    855 {
    856 	struct rasops_info *ri = cookie;
    857 	struct vcons_screen *scr = ri->ri_hw;
    858 	struct voodoofb_softc *sc = scr->scr_cookie;
    859 	int32_t xs, xd, y, width, height;
    860 
    861 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    862 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    863 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    864 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    865 		width = ri->ri_font->fontwidth * ncols;
    866 		height = ri->ri_font->fontheight;
    867 		voodoofb_bitblt(sc, xs, y, xd, y, width, height);
    868 	}
    869 }
    870 
    871 static void
    872 voodoofb_erasecols(void *cookie, int row, int startcol, int ncols,
    873     long fillattr)
    874 {
    875 	struct rasops_info *ri = cookie;
    876 	struct vcons_screen *scr = ri->ri_hw;
    877 	struct voodoofb_softc *sc = scr->scr_cookie;
    878 	int32_t x, y, width, height, fg, bg, ul;
    879 
    880 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    881 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    882 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    883 		width = ri->ri_font->fontwidth * ncols;
    884 		height = ri->ri_font->fontheight;
    885 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    886 
    887 		voodoofb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    888 	}
    889 }
    890 
    891 static void
    892 voodoofb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    893 {
    894 	struct rasops_info *ri = cookie;
    895 	struct vcons_screen *scr = ri->ri_hw;
    896 	struct voodoofb_softc *sc = scr->scr_cookie;
    897 	int32_t x, ys, yd, width, height;
    898 
    899 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    900 		x = ri->ri_xorigin;
    901 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    902 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    903 		width = ri->ri_emuwidth;
    904 		height = ri->ri_font->fontheight * nrows;
    905 		voodoofb_bitblt(sc, x, ys, x, yd, width, height);
    906 	}
    907 }
    908 
    909 static void
    910 voodoofb_eraserows(void *cookie, int row, int nrows, long fillattr)
    911 {
    912 	struct rasops_info *ri = cookie;
    913 	struct vcons_screen *scr = ri->ri_hw;
    914 	struct voodoofb_softc *sc = scr->scr_cookie;
    915 	int32_t x, y, width, height, fg, bg, ul;
    916 
    917 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    918 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    919 		if ((row == 0) && (nrows == ri->ri_rows)) {
    920 			/* clear the whole screen */
    921 			voodoofb_rectfill(sc, 0, 0, ri->ri_width,
    922 			    ri->ri_height, ri->ri_devcmap[bg]);
    923 		} else {
    924 			x = ri->ri_xorigin;
    925 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    926 			width = ri->ri_emuwidth;
    927 			height = ri->ri_font->fontheight * nrows;
    928 			voodoofb_rectfill(sc, x, y, width, height,
    929 			    ri->ri_devcmap[bg]);
    930 		}
    931 	}
    932 }
    933 
    934 static void
    935 voodoofb_bitblt(struct voodoofb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
    936 {
    937 	uint32_t fmt, blitcmd;
    938 
    939 	fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
    940 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
    941 	blitcmd = COMMAND_2D_S2S_BITBLT | (ROP_COPY << 24);
    942 
    943 	if (xs <= xd) {
    944 	        blitcmd |= BIT(14);
    945 		xs += (width - 1);
    946 		xd += (width - 1);
    947 	}
    948 	if (ys <= yd) {
    949 		blitcmd |= BIT(15);
    950 		ys += (height - 1);
    951 		yd += (height - 1);
    952 	}
    953 	voodoo3_make_room(sc, 6);
    954 
    955 	voodoo3_write32(sc, SRCFORMAT, fmt);
    956 	voodoo3_write32(sc, DSTFORMAT, fmt);
    957 	voodoo3_write32(sc, DSTSIZE,   width | (height << 16));
    958 	voodoo3_write32(sc, DSTXY,     xd | (yd << 16));
    959 	voodoo3_write32(sc, SRCXY, xs | (ys << 16));
    960 	voodoo3_write32(sc, COMMAND_2D, blitcmd | SST_2D_GO);
    961 }
    962 
    963 static void
    964 voodoofb_rectfill(struct voodoofb_softc *sc, int x, int y, int width,
    965     int height, int colour)
    966 {
    967 	uint32_t fmt, col;
    968 
    969 	col = (colour << 24) | (colour << 16) | (colour << 8) | colour;
    970 	fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
    971 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
    972 
    973 	voodoo3_make_room(sc, 6);
    974 	voodoo3_write32(sc, DSTFORMAT, fmt);
    975 	voodoo3_write32(sc, COLORFORE, colour);
    976 	voodoo3_write32(sc, COLORBACK, colour);
    977 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT | (ROP_COPY << 24));
    978 	voodoo3_write32(sc, DSTSIZE,    width | (height << 16));
    979 	voodoo3_write32(sc, LAUNCH_2D,  x | (y << 16));
    980 }
    981 
    982 static void
    983 voodoofb_rectinvert(struct voodoofb_softc *sc, int x, int y, int width,
    984     int height)
    985 {
    986 	uint32_t fmt;
    987 
    988 	fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
    989 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
    990 
    991 	voodoo3_make_room(sc, 6);
    992 	voodoo3_write32(sc, DSTFORMAT,	fmt);
    993 	voodoo3_write32(sc, COMMAND_2D,	COMMAND_2D_FILLRECT |
    994 	    (ROP_INVERT << 24));
    995 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
    996 	voodoo3_write32(sc, DSTXY,	x | (y << 16));
    997 	voodoo3_write32(sc, LAUNCH_2D,	x | (y << 16));
    998 }
    999 
   1000 static void
   1001 voodoofb_setup_mono(struct voodoofb_softc *sc, int xd, int yd, int width, int height, uint32_t fg,
   1002 					uint32_t bg)
   1003 {
   1004 	uint32_t dfmt, sfmt = sc->sc_linebytes;
   1005 
   1006 	dfmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
   1007 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
   1008 
   1009 	voodoo3_make_room(sc, 9);
   1010 	voodoo3_write32(sc, SRCFORMAT,	sfmt);
   1011 	voodoo3_write32(sc, DSTFORMAT,	dfmt);
   1012 	voodoo3_write32(sc, COLORFORE,	fg);
   1013 	voodoo3_write32(sc, COLORBACK,	bg);
   1014 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
   1015 	voodoo3_write32(sc, DSTXY,	xd | (yd << 16));
   1016 	voodoo3_write32(sc, SRCXY,	0);
   1017 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
   1018 	    (ROP_COPY << 24) | SST_2D_GO);
   1019 
   1020 	/* now feed the data into the chip */
   1021 }
   1022 
   1023 static void
   1024 voodoofb_feed_line(struct voodoofb_softc *sc, int count, uint8_t *data)
   1025 {
   1026 	int i;
   1027 	uint32_t latch = 0, bork;
   1028 	int shift = 0;
   1029 
   1030 	voodoo3_make_room(sc, count);
   1031 	for (i = 0; i < count; i++) {
   1032 		bork = data[i];
   1033 		latch |= (bork << shift);
   1034 		if (shift == 24) {
   1035 			voodoo3_write32(sc, LAUNCH_2D, latch);
   1036 			latch = 0;
   1037 			shift = 0;
   1038 		} else
   1039 			shift += 8;
   1040 		}
   1041 	if (shift != 24)
   1042 		voodoo3_write32(sc, LAUNCH_2D, latch);
   1043 }
   1044 
   1045 #if 0
   1046 static int
   1047 voodoofb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
   1048 {
   1049 
   1050 	return 0;
   1051 }
   1052 #endif
   1053 
   1054 /*
   1055  * wsdisplay_accessops
   1056  */
   1057 
   1058 static int
   1059 voodoofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
   1060 	struct lwp *l)
   1061 {
   1062 	struct vcons_data *vd = v;
   1063 	struct voodoofb_softc *sc = vd->cookie;
   1064 	struct wsdisplay_fbinfo *wdf;
   1065 	struct vcons_screen *ms = vd->active;
   1066 
   1067 	switch (cmd) {
   1068 	case WSDISPLAYIO_GTYPE:
   1069 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
   1070 		return 0;
   1071 
   1072 	case WSDISPLAYIO_GINFO:
   1073 		wdf = (void *)data;
   1074 		wdf->height = ms->scr_ri.ri_height;
   1075 		wdf->width = ms->scr_ri.ri_width;
   1076 		wdf->depth = ms->scr_ri.ri_depth;
   1077 		wdf->cmsize = 256;
   1078 		return 0;
   1079 
   1080 	case WSDISPLAYIO_GETCMAP:
   1081 		return voodoofb_getcmap(sc,
   1082 		    (struct wsdisplay_cmap *)data);
   1083 
   1084 	case WSDISPLAYIO_PUTCMAP:
   1085 		return voodoofb_putcmap(sc,
   1086 		    (struct wsdisplay_cmap *)data);
   1087 
   1088 	/* PCI config read/write passthrough. */
   1089 	case PCI_IOC_CFGREAD:
   1090 	case PCI_IOC_CFGWRITE:
   1091 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
   1092 		    cmd, data, flag, l);
   1093 
   1094 	case WSDISPLAYIO_GET_BUSID:
   1095 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
   1096 		    sc->sc_pcitag, data);
   1097 
   1098 	case WSDISPLAYIO_SMODE: {
   1099 		int new_mode = *(int*)data;
   1100 		if (new_mode != sc->sc_mode) {
   1101 			sc->sc_mode = new_mode;
   1102 			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
   1103 				voodoofb_drm_map(sc);
   1104 				int i;
   1105 
   1106 				/* restore the palette */
   1107 				for (i = 0; i < 256; i++) {
   1108 					voodoofb_putpalreg(sc,
   1109 					   i,
   1110 					   sc->sc_cmap_red[i],
   1111 					   sc->sc_cmap_green[i],
   1112 					   sc->sc_cmap_blue[i]);
   1113 				}
   1114 				voodoofb_clearscreen(sc);
   1115 				vcons_redraw_screen(ms);
   1116 			} else {
   1117 				voodoofb_drm_unmap(sc);
   1118 			}
   1119 		}
   1120 		}
   1121 		return 0;
   1122 	}
   1123 	return EPASSTHROUGH;
   1124 }
   1125 
   1126 static paddr_t
   1127 voodoofb_mmap(void *v, void *vs, off_t offset, int prot)
   1128 {
   1129 	struct vcons_data *vd = v;
   1130 	struct voodoofb_softc *sc = vd->cookie;
   1131 	paddr_t pa;
   1132 
   1133 	/* 'regular' framebuffer mmap()ing */
   1134 	if (offset < sc->sc_fbsize) {
   1135 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1136 		    BUS_SPACE_MAP_LINEAR);
   1137 		return pa;
   1138 	}
   1139 
   1140 	/*
   1141 	 * restrict all other mappings to processes with superuser privileges
   1142 	 * or the kernel itself
   1143 	 */
   1144 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
   1145 	    NULL) != 0) {
   1146 		aprint_error_dev(sc->sc_dev, "mmap() rejected.\n");
   1147 		return -1;
   1148 	}
   1149 
   1150 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
   1151 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1152 		    BUS_SPACE_MAP_LINEAR);
   1153 		return pa;
   1154 	}
   1155 
   1156 	if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
   1157 	    sc->sc_regsize))) {
   1158 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1159 		    BUS_SPACE_MAP_LINEAR);
   1160 		return pa;
   1161 	}
   1162 
   1163 #ifdef PCI_MAGIC_IO_RANGE
   1164 	/* allow mapping of IO space */
   1165 	if ((offset >= PCI_MAGIC_IO_RANGE) &&\
   1166 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
   1167 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
   1168 		    0, prot, BUS_SPACE_MAP_LINEAR);
   1169 		return pa;
   1170 	}
   1171 #endif
   1172 
   1173 #ifdef OFB_ALLOW_OTHERS
   1174 	if (offset >= 0x80000000) {
   1175 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1176 		    BUS_SPACE_MAP_LINEAR);
   1177 		return pa;
   1178 	}
   1179 #endif
   1180 	return -1;
   1181 }
   1182 
   1183 static void
   1184 voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
   1185     int existing, long *defattr)
   1186 {
   1187 	struct voodoofb_softc *sc = cookie;
   1188 	struct rasops_info *ri = &scr->scr_ri;
   1189 
   1190 	ri->ri_depth = sc->sc_bits_per_pixel;
   1191 	ri->ri_width = sc->sc_width;
   1192 	ri->ri_height = sc->sc_height;
   1193 	ri->ri_stride = sc->sc_linebytes;
   1194 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
   1195 
   1196 	rasops_init(ri, 0, 0);
   1197 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1198 
   1199 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
   1200 		    sc->sc_width / ri->ri_font->fontwidth);
   1201 
   1202 	ri->ri_hw = scr;
   1203 	ri->ri_ops.copyrows = voodoofb_copyrows;
   1204 	ri->ri_ops.copycols = voodoofb_copycols;
   1205 	ri->ri_ops.eraserows = voodoofb_eraserows;
   1206 	ri->ri_ops.erasecols = voodoofb_erasecols;
   1207 	ri->ri_ops.cursor = voodoofb_cursor;
   1208 	if (FONT_IS_ALPHA(ri->ri_font)) {
   1209 		ri->ri_ops.putchar = voodoofb_putchar_aa;
   1210 	} else {
   1211 		ri->ri_ops.putchar = voodoofb_putchar;
   1212 	}
   1213 }
   1214 
   1215 #if 0
   1216 int
   1217 voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1218 {
   1219 
   1220 	return 0;
   1221 }
   1222 #endif
   1223 
   1224 #ifdef VOODOOFB_ENABLE_INTR
   1225 static int
   1226 voodoofb_intr(void *arg)
   1227 {
   1228 	struct voodoofb_softc *sc = arg;
   1229 
   1230 	voodoo3_write32(sc, V3_STATUS, 0);	/* clear interrupts */
   1231 	return 1;
   1232 }
   1233 #endif
   1234 
   1235 /* video mode stuff */
   1236 
   1237 #define REFFREQ 14318	/* .18 */
   1238 
   1239 #define ABS(a) ((a < 0) ? -a : a)
   1240 
   1241 static int
   1242 voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
   1243 {
   1244 	int m, n, k, best_m, best_n, best_k, f_cur, best_error;
   1245 	int minm, maxm;
   1246 
   1247 	best_error = freq;
   1248 	best_n = best_m = best_k = 0;
   1249 
   1250 	if (isBanshee) {
   1251 		minm = 24;
   1252 		maxm = 24;
   1253 	} else {
   1254 		minm = 1;
   1255 		maxm = 57;
   1256 		/* This used to be 64, alas it seems the last 8 (funny that ?)
   1257 		 * values cause jittering at lower resolutions. I've not done
   1258 		 * any calculations to what the adjustment affects clock ranges,
   1259 		 * but I can still run at 1600x1200@75Hz */
   1260 	}
   1261 	for (n = 1; n < 256; n++) {
   1262 		f_cur = REFFREQ * (n + 2);
   1263 		if (f_cur < freq) {
   1264 			f_cur = f_cur / 3;
   1265 			if (freq - f_cur < best_error) {
   1266 				best_error = freq - f_cur;
   1267 				best_n = n;
   1268 				best_m = 1;
   1269 				best_k = 0;
   1270 				continue;
   1271       			}
   1272 		}
   1273 		for (m = minm; m < maxm; m++) {
   1274 			for (k = 0; k < 4; k++) {
   1275 				f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1276 				if (ABS(f_cur - freq) < best_error) {
   1277 					best_error = ABS(f_cur - freq);
   1278 					best_n = n;
   1279 					best_m = m;
   1280 					best_k = k;
   1281 				}
   1282 			}
   1283 		}
   1284 	}
   1285 	n = best_n;
   1286 	m = best_m;
   1287 	k = best_k;
   1288 	*f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1289 	return ( n << 8) | (m << 2) | k;
   1290 }
   1291 
   1292 static void
   1293 voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
   1294 {
   1295 	struct voodoo_regs mod;
   1296 	struct voodoo_regs *mode;
   1297 	uint32_t horizontal_display_end, horizontal_sync_start,
   1298 		horizontal_sync_end, horizontal_total,
   1299 		horizontal_blanking_start, horizontal_blanking_end;
   1300 
   1301 	uint32_t vertical_display_enable_end, vertical_sync_start,
   1302 		vertical_sync_end, vertical_total, vertical_blanking_start,
   1303 		vertical_blanking_end;
   1304 
   1305 	uint32_t wd; // CRTC offset
   1306 
   1307 	int i;
   1308 
   1309 	uint8_t misc;
   1310 
   1311 	memset(&mod, 0, sizeof(mode));
   1312 
   1313 	mode = &mod;
   1314 
   1315 	wd = (vm->hdisplay >> 3) - 1;
   1316 	horizontal_display_end	= (vm->hdisplay >> 3) - 1;
   1317 	horizontal_sync_start	= (vm->hsync_start >> 3) - 1;
   1318 	horizontal_sync_end	= (vm->hsync_end >> 3) - 1;
   1319 	horizontal_total  	= (vm->htotal   >> 3) - 1;
   1320 	horizontal_blanking_start = horizontal_display_end;
   1321 	horizontal_blanking_end = horizontal_total;
   1322 
   1323 	vertical_display_enable_end  = vm->vdisplay - 1;
   1324 	vertical_sync_start  	= vm->vsync_start;	// - 1;
   1325 	vertical_sync_end	= vm->vsync_end;	// - 1;
   1326 	vertical_total		= vm->vtotal - 2;
   1327 	vertical_blanking_start	= vertical_display_enable_end;
   1328 	vertical_blanking_end	= vertical_total;
   1329 
   1330 #if 0
   1331 	misc = 0x0f |
   1332 	    (vm->hdisplay < 400 ? 0xa0 :
   1333 		vm->hdisplay < 480 ? 0x60 :
   1334 		vm->hdisplay < 768 ? 0xe0 : 0x20);
   1335 #else
   1336 	misc = 0x2f;
   1337 	if (vm->flags & VID_NHSYNC)
   1338 		misc |= HSYNC_NEG;
   1339 	if (vm->flags & VID_NVSYNC)
   1340 		misc |= VSYNC_NEG;
   1341 #ifdef VOODOOFB_DEBUG
   1342 	printf("misc: %02x\n", misc);
   1343 #endif
   1344 #endif
   1345 	mode->vr_seq[0] = 3;
   1346 	mode->vr_seq[1] = 1;
   1347 	mode->vr_seq[2] = 8;
   1348 	mode->vr_seq[3] = 0;
   1349 	mode->vr_seq[4] = 6;
   1350 
   1351 	/* crtc regs start */
   1352 	mode->vr_crtc[0] = horizontal_total - 4;
   1353 	mode->vr_crtc[1] = horizontal_display_end;
   1354 	mode->vr_crtc[2] = horizontal_blanking_start;
   1355 	mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
   1356 	mode->vr_crtc[4] = horizontal_sync_start;
   1357 
   1358 	mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
   1359 	    (horizontal_sync_end & 0x1f);
   1360 	mode->vr_crtc[6] = vertical_total;
   1361 	mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
   1362 	    ((vertical_display_enable_end & 0x200) >> 3) |
   1363 	    ((vertical_total & 0x200) >> 4) |
   1364 	    0x10 |
   1365 	    ((vertical_blanking_start & 0x100) >> 5) |
   1366 	    ((vertical_sync_start  & 0x100) >> 6) |
   1367 	    ((vertical_display_enable_end  & 0x100) >> 7) |
   1368 	    ((vertical_total  & 0x100) >> 8);
   1369 
   1370 	mode->vr_crtc[8] = 0;
   1371 	mode->vr_crtc[9] = 0x40 |
   1372 	    ((vertical_blanking_start & 0x200) >> 4);
   1373 
   1374 	mode->vr_crtc[10] = 0;
   1375 	mode->vr_crtc[11] = 0;
   1376 	mode->vr_crtc[12] = 0;
   1377 	mode->vr_crtc[13] = 0;
   1378 	mode->vr_crtc[14] = 0;
   1379 	mode->vr_crtc[15] = 0;
   1380 
   1381 	mode->vr_crtc[16] = vertical_sync_start;
   1382 	mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
   1383 	mode->vr_crtc[18] = vertical_display_enable_end;
   1384 	mode->vr_crtc[19] = wd; // CRTC offset
   1385 	mode->vr_crtc[20] = 0;
   1386 	mode->vr_crtc[21] = vertical_blanking_start;
   1387 	mode->vr_crtc[22] = vertical_blanking_end + 1;
   1388 	mode->vr_crtc[23] = 128;
   1389 	mode->vr_crtc[24] = 255;
   1390 
   1391 	/* overflow registers */
   1392 	mode->vr_crtc[CRTC_HDISP_EXT] =
   1393 	    (horizontal_total&0x100) >> 8 |
   1394 	    (horizontal_display_end & 0x100) >> 6 |
   1395 	    (horizontal_blanking_start & 0x100) >> 4 |
   1396 	    (horizontal_blanking_end & 0x40) >> 1 |
   1397 	    (horizontal_sync_start & 0x100) >> 2 |
   1398 	    (horizontal_sync_end & 0x20) << 2;
   1399 
   1400 	mode->vr_crtc[CRTC_VDISP_EXT] =
   1401 	    (vertical_total & 0x400) >> 10 |
   1402 	    (vertical_display_enable_end & 0x400) >> 8 | /* the manual is contradictory here */
   1403 	    (vertical_blanking_start & 0x400) >> 6 |
   1404 	    (vertical_blanking_end & 0x400) >> 4;
   1405 
   1406 	/* attr regs start */
   1407 	mode->vr_attr[0] = 0;
   1408 	mode->vr_attr[1] = 0;
   1409 	mode->vr_attr[2] = 0;
   1410 	mode->vr_attr[3] = 0;
   1411 	mode->vr_attr[4] = 0;
   1412 	mode->vr_attr[5] = 0;
   1413 	mode->vr_attr[6] = 0;
   1414 	mode->vr_attr[7] = 0;
   1415 	mode->vr_attr[8] = 0;
   1416 	mode->vr_attr[9] = 0;
   1417 	mode->vr_attr[10] = 0;
   1418 	mode->vr_attr[11] = 0;
   1419 	mode->vr_attr[12] = 0;
   1420 	mode->vr_attr[13] = 0;
   1421 	mode->vr_attr[14] = 0;
   1422 	mode->vr_attr[15] = 0;
   1423 	mode->vr_attr[16] = 1;
   1424 	mode->vr_attr[17] = 0;
   1425 	mode->vr_attr[18] = 15;
   1426 	mode->vr_attr[19] = 0;
   1427 	/* attr regs end */
   1428 
   1429 	/* graph regs start */
   1430 	mode->vr_graph[0] = 159;
   1431 	mode->vr_graph[1] = 127;
   1432 	mode->vr_graph[2] = 127;
   1433 	mode->vr_graph[3] = 131;
   1434 	mode->vr_graph[4] = 130;
   1435 	mode->vr_graph[5] = 142;
   1436 	mode->vr_graph[6] = 30;
   1437 	mode->vr_graph[7] = 245;
   1438 	mode->vr_graph[8] = 0;
   1439 
   1440 	vga_outb(sc, MISC_W, misc | 0x01);
   1441 
   1442 	for(i = 0; i < 5; i++)
   1443 		voodoo3_write_seq(sc, i, mode->vr_seq[i]);
   1444 	for (i = 0; i < CRTC_PCI_READBACK; i ++)
   1445         	voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
   1446 	for (i = 0; i < 0x14; i ++)
   1447         	voodoo3_write_attr(sc, i, mode->vr_attr[i]);
   1448 	for (i = 0; i < 0x09; i ++)
   1449         	voodoo3_write_gra(sc, i, mode->vr_graph[i]);
   1450 }
   1451 
   1452 static void
   1453 voodoofb_set_videomode(struct voodoofb_softc *sc,
   1454     const struct videomode *vm)
   1455 {
   1456 	uint32_t miscinit0 = 0;
   1457 	int vidpll, fout;
   1458 	uint32_t vp, vidproc = VIDPROCDEFAULT;
   1459 	uint32_t bpp = 1;	/* for now */
   1460 	uint32_t bytes_per_row = vm->hdisplay * bpp;
   1461 
   1462 	sc->sc_bits_per_pixel = bpp << 3;
   1463 	sc->sc_width = vm->hdisplay;
   1464 	sc->sc_height = vm->vdisplay;
   1465 	sc->sc_linebytes = bytes_per_row;
   1466 
   1467 	voodoofb_setup_monitor(sc, vm);
   1468 	vp = voodoo3_read32(sc, VIDPROCCFG);
   1469 
   1470 	vidproc &= ~(0x1c0000); /* clear bits 18 to 20, bpp in vidproccfg */
   1471 	/* enable bits 18 to 20 to the required bpp */
   1472 	vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT);
   1473 
   1474 	vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
   1475 
   1476 #ifdef VOODOOFB_DEBUG
   1477 	printf("old vidproc: %08x\n", vp);
   1478 	printf("pll: %08x %d\n", vidpll, fout);
   1479 #endif
   1480 	/* bit 10 of vidproccfg, is enabled or disabled as needed */
   1481 	switch (bpp) {
   1482 		case 1:
   1483 			/*
   1484 			 * bit 10 off for palettized modes only, off means
   1485 			 * palette is used
   1486 			 */
   1487 			vidproc &= ~(1 << 10);
   1488 			break;
   1489 #if 0
   1490 		case 2:
   1491 			#if __POWERPC__
   1492 				miscinit0 = 0xc0000000;
   1493 			#endif
   1494 			/* bypass palette for 16bit modes */
   1495 			vidproc |= (1 << 10);
   1496 			break;
   1497 		case 4:
   1498 			#if __POWERPC__
   1499 				miscinit0 = 0x40000000;
   1500 			#endif
   1501 			vidproc |= (1 << 10); /* Same for 32bit modes */
   1502 			break;
   1503 #endif
   1504 		default:
   1505 			printf("We support only 8 bit for now\n");
   1506 			return;
   1507 	}
   1508 
   1509 	voodoofb_wait_idle(sc);
   1510 
   1511 	voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
   1512 
   1513 	voodoo3_make_room(sc, 4);
   1514 	voodoo3_write32(sc, VGAINIT0, 4928);
   1515 	voodoo3_write32(sc, DACMODE, 0);
   1516 	voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
   1517 	voodoo3_write32(sc, PLLCTRL0, vidpll);
   1518 
   1519 	voodoo3_make_room(sc, 5);
   1520 	voodoo3_write32(sc, VIDSCREENSIZE, sc->sc_width |
   1521 	    (sc->sc_height << 12));
   1522 	voodoo3_write32(sc, VIDDESKSTART,  0);
   1523 
   1524 	vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
   1525 	voodoo3_write32(sc, VIDPROCCFG, vidproc);
   1526 
   1527 	voodoo3_write32(sc, VGAINIT1, 0);
   1528 	voodoo3_write32(sc, MISCINIT0, miscinit0);
   1529 #ifdef VOODOOFB_DEBUG
   1530 	printf("vidproc: %08x\n", vidproc);
   1531 #endif
   1532 	voodoo3_make_room(sc, 8);
   1533 	voodoo3_write32(sc, SRCBASE, 0);
   1534 	voodoo3_write32(sc, DSTBASE, 0);
   1535 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1536   	voodoo3_write32(sc, CLIP0MIN,        0);
   1537   	voodoo3_write32(sc, CLIP0MAX,        0x0fff0fff);
   1538   	voodoo3_write32(sc, CLIP1MIN,        0);
   1539   	voodoo3_write32(sc, CLIP1MAX,        0x0fff0fff);
   1540 	voodoo3_write32(sc, SRCXY, 0);
   1541 	voodoofb_wait_idle(sc);
   1542 	printf("%s: switched to %dx%d, %d bit\n", device_xname(sc->sc_dev),
   1543 	    sc->sc_width, sc->sc_height, sc->sc_bits_per_pixel);
   1544 }
   1545 
   1546 static void
   1547 voodoofb_init(struct voodoofb_softc *sc)
   1548 {
   1549 	/* XXX */
   1550 	uint32_t vgainit0 = 0;
   1551 	uint32_t vidcfg = 0;
   1552 
   1553 #ifdef VOODOOFB_DEBUG
   1554 	printf("initializing engine...");
   1555 #endif
   1556 	vgainit0 = voodoo3_read32(sc, VGAINIT0);
   1557 #ifdef VOODOOFB_DEBUG
   1558 	printf("vga: %08x", vgainit0);
   1559 #endif
   1560 	vgainit0 |=
   1561 	    VGAINIT0_8BIT_DAC     |
   1562 	    VGAINIT0_EXT_ENABLE   |
   1563 	    VGAINIT0_WAKEUP_3C3   |
   1564 	    VGAINIT0_ALT_READBACK |
   1565 	    VGAINIT0_EXTSHIFTOUT;
   1566 
   1567 	vidcfg = voodoo3_read32(sc, VIDPROCCFG);
   1568 #ifdef VOODOOFB_DEBUG
   1569 	printf(" vidcfg: %08x\n", vidcfg);
   1570 #endif
   1571 	vidcfg |=
   1572 	    VIDCFG_VIDPROC_ENABLE |
   1573 	    VIDCFG_DESK_ENABLE;
   1574 	vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
   1575 
   1576 	voodoo3_make_room(sc, 2);
   1577 
   1578 	voodoo3_write32(sc, VGAINIT0, vgainit0);
   1579 	voodoo3_write32(sc, VIDPROCCFG, vidcfg);
   1580 
   1581 	voodoo3_make_room(sc, 8);
   1582 	voodoo3_write32(sc, SRCBASE, 0);
   1583 	voodoo3_write32(sc, DSTBASE, 0);
   1584 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1585   	voodoo3_write32(sc, CLIP0MIN,        0);
   1586   	voodoo3_write32(sc, CLIP0MAX,        0x1fff1fff);
   1587   	voodoo3_write32(sc, CLIP1MIN,        0);
   1588   	voodoo3_write32(sc, CLIP1MAX,        0x1fff1fff);
   1589 	voodoo3_write32(sc, SRCXY, 0);
   1590 
   1591 	voodoofb_wait_idle(sc);
   1592 }
   1593 
   1594 #define MAX_CLOCK 250000	/* all Voodoo3 should support that */
   1595 #define MAX_HRES  1700		/*
   1596 				 * XXX in theory we can go higher but I
   1597 				 * couldn't get anything above 1680 x 1200
   1598 				 * to work, so until I find out why, it's
   1599 				 * disabled so people won't end up with a
   1600 				 * blank screen
   1601 				 */
   1602 #define MODE_IS_VALID(m) (((m)->dot_clock <= MAX_CLOCK) && \
   1603 					    ((m)->hdisplay < MAX_HRES))
   1604 static void
   1605 voodoofb_setup_i2c(struct voodoofb_softc *sc)
   1606 {
   1607 	int i;
   1608 
   1609 	/* Fill in the i2c tag */
   1610 	sc->sc_i2c.ic_cookie = sc;
   1611 	sc->sc_i2c.ic_acquire_bus = voodoofb_i2c_acquire_bus;
   1612 	sc->sc_i2c.ic_release_bus = voodoofb_i2c_release_bus;
   1613 	sc->sc_i2c.ic_send_start = voodoofb_i2c_send_start;
   1614 	sc->sc_i2c.ic_send_stop = voodoofb_i2c_send_stop;
   1615 	sc->sc_i2c.ic_initiate_xfer = voodoofb_i2c_initiate_xfer;
   1616 	sc->sc_i2c.ic_read_byte = voodoofb_i2c_read_byte;
   1617 	sc->sc_i2c.ic_write_byte = voodoofb_i2c_write_byte;
   1618 	sc->sc_i2c.ic_exec = NULL;
   1619 
   1620 	sc->sc_i2creg = voodoo3_read32(sc, VIDSERPARPORT);
   1621 #ifdef VOODOOFB_DEBUG
   1622 	printf("data: %08x\n", sc->sc_i2creg);
   1623 #endif
   1624 	sc->sc_i2creg |= VSP_ENABLE_IIC0;
   1625 	sc->sc_i2creg &= ~(VSP_SDA0_OUT | VSP_SCL0_OUT);
   1626 	voodoo3_write32(sc, VIDSERPARPORT, sc->sc_i2creg);
   1627 
   1628 	/* zero out the EDID buffer */
   1629 	memset(sc->sc_edid_data, 0, 128);
   1630 
   1631 	/* Some monitors don't respond first time */
   1632 	i = 0;
   1633 	while (sc->sc_edid_data[1] == 0 && i++ < 3)
   1634 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
   1635 	if (i < 3) {
   1636 		if (edid_parse(sc->sc_edid_data, &sc->sc_edid_info) != -1) {
   1637 #ifdef VOODOOFB_DEBUG
   1638 			edid_print(&sc->sc_edid_info);
   1639 #endif
   1640 			/*
   1641 			 * Now pick a mode.
   1642 			 * How do we know our max. pixel clock?
   1643 			 * All Voodoo3 should support at least 250MHz.
   1644 			 * All Voodoo3 I've seen so far have at least 8MB
   1645 			 * which we're not going to exhaust either in 8bit.
   1646 			 */
   1647 			if ((sc->sc_edid_info.edid_preferred_mode != NULL)) {
   1648 				struct videomode *m =
   1649 				    sc->sc_edid_info.edid_preferred_mode;
   1650 				if (MODE_IS_VALID(m)) {
   1651 					sc->sc_videomode = m;
   1652 				} else {
   1653 					aprint_error_dev(sc->sc_dev,
   1654 					    "unable to use preferred mode\n");
   1655 				}
   1656 			}
   1657 			/*
   1658 			 * if we can't use the preferred mode go look for the
   1659 			 * best one we can support
   1660 			 */
   1661 			if (sc->sc_videomode == NULL) {
   1662 				int n;
   1663 				struct videomode *m =
   1664 				     sc->sc_edid_info.edid_modes;
   1665 
   1666 				sort_modes(sc->sc_edid_info.edid_modes,
   1667 			   	    &sc->sc_edid_info.edid_preferred_mode,
   1668 				    sc->sc_edid_info.edid_nmodes);
   1669 				while ((sc->sc_videomode == NULL) &&
   1670 				       (n < sc->sc_edid_info.edid_nmodes)) {
   1671 					if (MODE_IS_VALID(&m[n])) {
   1672 						sc->sc_videomode = &m[n];
   1673 					}
   1674 					n++;
   1675 				}
   1676 			}
   1677 		}
   1678 	}
   1679 }
   1680 
   1681 /* I2C bitbanging */
   1682 static void voodoofb_i2cbb_set_bits(void *cookie, uint32_t bits)
   1683 {
   1684 	struct voodoofb_softc *sc = cookie;
   1685 	uint32_t out;
   1686 
   1687 	out = bits >> 2;	/* bitmasks match the IN bits */
   1688 
   1689 	voodoo3_write32(sc, VIDSERPARPORT, sc->sc_i2creg | out);
   1690 }
   1691 
   1692 static void voodoofb_i2cbb_set_dir(void *cookie, uint32_t dir)
   1693 {
   1694 	/* Nothing to do */
   1695 }
   1696 
   1697 static uint32_t voodoofb_i2cbb_read(void *cookie)
   1698 {
   1699 	struct voodoofb_softc *sc = cookie;
   1700 	uint32_t bits;
   1701 
   1702 	bits = voodoo3_read32(sc, VIDSERPARPORT);
   1703 
   1704 	return bits;
   1705 }
   1706 
   1707 /* higher level I2C stuff */
   1708 static int
   1709 voodoofb_i2c_acquire_bus(void *cookie, int flags)
   1710 {
   1711 	/* private bus */
   1712 	return (0);
   1713 }
   1714 
   1715 static void
   1716 voodoofb_i2c_release_bus(void *cookie, int flags)
   1717 {
   1718 	/* private bus */
   1719 }
   1720 
   1721 static int
   1722 voodoofb_i2c_send_start(void *cookie, int flags)
   1723 {
   1724 	return (i2c_bitbang_send_start(cookie, flags, &voodoofb_i2cbb_ops));
   1725 }
   1726 
   1727 static int
   1728 voodoofb_i2c_send_stop(void *cookie, int flags)
   1729 {
   1730 
   1731 	return (i2c_bitbang_send_stop(cookie, flags, &voodoofb_i2cbb_ops));
   1732 }
   1733 
   1734 static int
   1735 voodoofb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
   1736 {
   1737 
   1738 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
   1739 	    &voodoofb_i2cbb_ops));
   1740 }
   1741 
   1742 static int
   1743 voodoofb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
   1744 {
   1745 	return (i2c_bitbang_read_byte(cookie, valp, flags, &voodoofb_i2cbb_ops));
   1746 }
   1747 
   1748 static int
   1749 voodoofb_i2c_write_byte(void *cookie, uint8_t val, int flags)
   1750 {
   1751 	return (i2c_bitbang_write_byte(cookie, val, flags, &voodoofb_i2cbb_ops));
   1752 }
   1753