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