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