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