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