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