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