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