voodoofb.c revision 1.21.2.2 1 /* $NetBSD: voodoofb.c,v 1.21.2.2 2010/10/22 07:22:12 uebayasi Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * A console driver for 3Dfx Voodoo3 graphics boards
30 * Thanks to Andreas Drewke (andreas_dr (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.21.2.2 2010/10/22 07:22:12 uebayasi 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 <uvm/uvm_extern.h>
46
47 #if defined(macppc) || defined (sparc64) || defined(ofppc)
48 #define HAVE_OPENFIRMWARE
49 #endif
50
51 #ifdef HAVE_OPENFIRMWARE
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_pci.h>
54 #endif
55
56 #include <dev/videomode/videomode.h>
57
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcidevs.h>
61 #include <dev/pci/pciio.h>
62 #include <dev/pci/voodoofbreg.h>
63
64 #include <dev/wscons/wsdisplayvar.h>
65 #include <dev/wscons/wsconsio.h>
66 #include <dev/wsfont/wsfont.h>
67 #include <dev/rasops/rasops.h>
68 #include <dev/wscons/wsdisplay_vconsvar.h>
69
70 #include "opt_wsemul.h"
71
72 struct voodoofb_softc {
73 device_t sc_dev;
74 pci_chipset_tag_t sc_pc;
75 pcitag_t sc_pcitag;
76 struct pci_attach_args sc_pa;
77
78 bus_space_tag_t sc_memt;
79 bus_space_tag_t sc_iot;
80 bus_space_handle_t sc_memh;
81
82 bus_space_tag_t sc_regt;
83 bus_space_tag_t sc_fbt;
84 bus_space_tag_t sc_ioregt;
85 bus_space_handle_t sc_regh;
86 bus_space_handle_t sc_fbh;
87 bus_space_handle_t sc_ioregh;
88 bus_addr_t sc_regs, sc_fb, sc_ioreg;
89 bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
90
91 void *sc_ih;
92
93 size_t memsize;
94 int memtype;
95
96 int bits_per_pixel;
97 int width, height, linebytes;
98 const struct videomode *sc_videomode;
99
100 int sc_mode;
101 uint32_t sc_bg;
102
103 u_char sc_cmap_red[256];
104 u_char sc_cmap_green[256];
105 u_char sc_cmap_blue[256];
106 int sc_dacw;
107
108 struct vcons_data vd;
109 };
110
111 struct voodoo_regs {
112 uint8_t vr_crtc[31];
113 uint8_t vr_graph[9];
114 uint8_t vr_attr[21];
115 uint8_t vr_seq[5];
116 };
117
118 static struct vcons_screen voodoofb_console_screen;
119
120 extern const u_char rasops_cmap[768];
121
122 static int voodoofb_match(device_t, cfdata_t, void *);
123 static void voodoofb_attach(device_t, device_t, void *);
124
125 static int voodoofb_drm_print(void *, const char *);
126 static int voodoofb_drm_unmap(struct voodoofb_softc *);
127 static int voodoofb_drm_map(struct voodoofb_softc *);
128
129 CFATTACH_DECL_NEW(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
130 voodoofb_attach, NULL, NULL);
131
132 static int voodoofb_is_console(struct pci_attach_args *);
133 static void voodoofb_init(struct voodoofb_softc *);
134
135 static void voodoofb_cursor(void *, int, int, int);
136 static void voodoofb_putchar(void *, int, int, u_int, long);
137 static void voodoofb_copycols(void *, int, int, int, int);
138 static void voodoofb_erasecols(void *, int, int, int, long);
139 static void voodoofb_copyrows(void *, int, int, int);
140 static void voodoofb_eraserows(void *, int, int, long);
141
142 #if 0
143 static int voodoofb_allocattr(void *, int, int, int, long *);
144 static void voodoofb_scroll(void *, void *, int);
145 static int voodoofb_load_font(void *, void *, struct wsdisplay_font *);
146 #endif
147
148 static int voodoofb_putcmap(struct voodoofb_softc *,
149 struct wsdisplay_cmap *);
150 static int voodoofb_getcmap(struct voodoofb_softc *,
151 struct wsdisplay_cmap *);
152 static int voodoofb_putpalreg(struct voodoofb_softc *, uint8_t, uint8_t,
153 uint8_t, uint8_t);
154 static void voodoofb_bitblt(struct voodoofb_softc *, int, int, int, int,
155 int, int);
156 static void voodoofb_rectfill(struct voodoofb_softc *, int, int, int, int,
157 int);
158 static void voodoofb_rectinvert(struct voodoofb_softc *, int, int, int,
159 int);
160 static void voodoofb_setup_mono(struct voodoofb_softc *, int, int, int,
161 int, uint32_t, uint32_t);
162 static void voodoofb_feed_line(struct voodoofb_softc *, int, uint8_t *);
163
164 #ifdef VOODOOFB_DEBUG
165 static void voodoofb_showpal(struct voodoofb_softc *);
166 #endif
167
168 static void voodoofb_wait_idle(struct voodoofb_softc *);
169
170 #ifdef VOODOOFB_ENABLE_INTR
171 static int voodoofb_intr(void *);
172 #endif
173
174 static void voodoofb_set_videomode(struct voodoofb_softc *,
175 const struct videomode *);
176
177 struct wsscreen_descr voodoofb_defaultscreen = {
178 "default",
179 0, 0,
180 NULL,
181 8, 16,
182 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
183 NULL,
184 };
185
186 const struct wsscreen_descr *_voodoofb_scrlist[] = {
187 &voodoofb_defaultscreen,
188 /* XXX other formats, graphics screen? */
189 };
190
191 struct wsscreen_list voodoofb_screenlist = {
192 sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
193 };
194
195 static int voodoofb_ioctl(void *, void *, u_long, void *, int,
196 struct lwp *);
197 static paddr_t voodoofb_mmap(void *, void *, off_t, int);
198
199 static void voodoofb_clearscreen(struct voodoofb_softc *);
200 static void voodoofb_init_screen(void *, struct vcons_screen *, int,
201 long *);
202
203
204 struct wsdisplay_accessops voodoofb_accessops = {
205 voodoofb_ioctl,
206 voodoofb_mmap,
207 NULL,
208 NULL,
209 NULL,
210 NULL, /* load_font */
211 NULL, /* polls */
212 NULL, /* scroll */
213 };
214
215 /*
216 * Inline functions for getting access to register aperture.
217 */
218 static inline void
219 voodoo3_write32(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
220 {
221 bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
222 }
223
224 static inline uint32_t
225 voodoo3_read32(struct voodoofb_softc *sc, uint32_t reg)
226 {
227 return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
228 }
229
230 static inline void
231 voodoo3_write_crtc(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
232 {
233 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_INDEX - 0x300, reg);
234 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_DATA - 0x300, val);
235 }
236
237 static inline void
238 voodoo3_write_seq(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
239 {
240 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_INDEX - 0x300, reg);
241 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_DATA - 0x300, val);
242 }
243
244 static inline void
245 voodoo3_write_gra(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
246 {
247 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_INDEX - 0x300, reg);
248 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_DATA - 0x300, val);
249 }
250
251 static inline void
252 voodoo3_write_attr(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
253 {
254 volatile uint8_t junk;
255 uint8_t index;
256
257 junk = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, IS1_R - 0x300);
258 index = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300);
259 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, reg);
260 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, val);
261 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, index);
262 }
263
264 static inline void
265 vga_outb(struct voodoofb_softc *sc, uint32_t reg, uint8_t val)
266 {
267 bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, reg - 0x300, val);
268 }
269
270 /* wait until there's room for len bytes in the FIFO */
271 static inline void
272 voodoo3_make_room(struct voodoofb_softc *sc, int len)
273 {
274 while ((voodoo3_read32(sc, STATUS) & 0x1f) < len);
275 }
276
277 static void
278 voodoofb_wait_idle(struct voodoofb_softc *sc)
279 {
280 int i = 0;
281
282 voodoo3_make_room(sc, 1);
283 voodoo3_write32(sc, COMMAND_3D, COMMAND_3D_NOP);
284
285 while (1) {
286 i = (voodoo3_read32(sc, STATUS) & STATUS_BUSY) ? 0 : i + 1;
287 if(i == 3) break;
288 }
289 }
290
291 static int
292 voodoofb_match(device_t parent, cfdata_t match, void *aux)
293 {
294 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
295
296 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
297 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
298 return 0;
299 if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
300 (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_VOODOO3))
301 return 100;
302 return 0;
303 }
304
305 static void
306 voodoofb_attach(device_t parent, device_t self, void *aux)
307 {
308 struct voodoofb_softc *sc = device_private(self);
309 struct pci_attach_args *pa = aux;
310 char devinfo[256];
311 struct wsemuldisplaydev_attach_args aa;
312 struct rasops_info *ri;
313 #ifdef VOODOOFB_ENABLE_INTR
314 pci_intr_handle_t ih;
315 const char *intrstr;
316 #endif
317 ulong defattr;
318 int console, width, height, i, j;
319 #ifdef HAVE_OPENFIRMWARE
320 int linebytes, depth, node;
321 #endif
322 uint32_t bg, fg, ul;
323
324 sc->sc_dev = self;
325
326 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
327 #ifdef HAVE_OPENFIRMWARE
328 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
329 #endif
330 sc->sc_pc = pa->pa_pc;
331 sc->sc_pcitag = pa->pa_tag;
332 sc->sc_dacw = -1;
333 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
334 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
335
336 sc->sc_memt = pa->pa_memt;
337 sc->sc_iot = pa->pa_iot;
338 sc->sc_pa = *pa;
339
340 /* the framebuffer */
341 if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM,
342 BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE |
343 BUS_SPACE_MAP_LINEAR,
344 &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
345 aprint_error_dev(self, "failed to map the frame buffer.\n");
346 }
347
348 /* memory-mapped registers */
349 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
350 &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
351 aprint_error_dev(self, "failed to map memory-mapped registers.\n");
352 }
353
354 /* IO-mapped registers */
355 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
356 &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
357 &sc->sc_ioregsize)) {
358 aprint_error_dev(self, "failed to map IO-mapped registers.\n");
359 }
360 voodoofb_init(sc);
361
362 /* we should read these from the chip instead of depending on OF */
363 width = height = -1;
364
365 #ifdef HAVE_OPENFIRMWARE
366 if (OF_getprop(node, "width", &width, 4) != 4)
367 OF_interpret("screen-width", 1, 1, &width);
368 if (OF_getprop(node, "height", &height, 4) != 4)
369 OF_interpret("screen-height", 1, 1, &height);
370 if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
371 linebytes = width; /* XXX */
372 if (OF_getprop(node, "depth", &depth, 4) != 4)
373 depth = 8; /* XXX */
374
375 if (width == -1 || height == -1)
376 return;
377
378 sc->width = width;
379 sc->height = height;
380 sc->bits_per_pixel = depth;
381 sc->linebytes = linebytes;
382 printf("%s: initial resolution %dx%d, %d bit\n", device_xname(self),
383 sc->width, sc->height, sc->bits_per_pixel);
384 #endif
385
386 /* XXX this should at least be configurable via kernel config */
387 if ((sc->sc_videomode = pick_mode_by_ref(1024, 768, 60)) != NULL)
388 voodoofb_set_videomode(sc, sc->sc_videomode);
389
390 vcons_init(&sc->vd, sc, &voodoofb_defaultscreen, &voodoofb_accessops);
391 sc->vd.init_screen = voodoofb_init_screen;
392
393 console = voodoofb_is_console(pa);
394
395 ri = &voodoofb_console_screen.scr_ri;
396 if (console) {
397 vcons_init_screen(&sc->vd, &voodoofb_console_screen, 1,
398 &defattr);
399 voodoofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
400
401 voodoofb_defaultscreen.textops = &ri->ri_ops;
402 voodoofb_defaultscreen.capabilities = ri->ri_caps;
403 voodoofb_defaultscreen.nrows = ri->ri_rows;
404 voodoofb_defaultscreen.ncols = ri->ri_cols;
405 wsdisplay_cnattach(&voodoofb_defaultscreen, ri, 0, 0, defattr);
406 } else {
407 /*
408 * since we're not the console we can postpone the rest
409 * until someone actually allocates a screen for us
410 */
411 voodoofb_set_videomode(sc, sc->sc_videomode);
412 }
413
414 printf("%s: %d MB aperture at 0x%08x, %d MB registers at 0x%08x\n",
415 device_xname(self), (u_int)(sc->sc_fbsize >> 20),
416 (u_int)sc->sc_fb, (u_int)(sc->sc_regsize >> 20),
417 (u_int)sc->sc_regs);
418 #ifdef VOODOOFB_DEBUG
419 printf("fb: %08lx\n", (ulong)ri->ri_bits);
420 #endif
421
422 j = 0;
423 for (i = 0; i < 256; i++) {
424 voodoofb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
425 rasops_cmap[j + 2]);
426 j += 3;
427 }
428
429 #ifdef VOODOOFB_ENABLE_INTR
430 /* Interrupt. We don't use it for anything yet */
431 if (pci_intr_map(pa, &ih)) {
432 aprint_error_dev(self, "failed to map interrupt\n");
433 return;
434 }
435
436 intrstr = pci_intr_string(sc->sc_pc, ih);
437 sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, voodoofb_intr,
438 sc);
439 if (sc->sc_ih == NULL) {
440 aprint_error_dev(self, "failed to establish interrupt");
441 if (intrstr != NULL)
442 aprint_error(" at %s", intrstr);
443 aprint_error("\n");
444 return;
445 }
446 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
447 #endif
448
449 rasops_unpack_attr(defattr, &fg, &bg, &ul);
450 sc->sc_bg = ri->ri_devcmap[bg];
451 voodoofb_clearscreen(sc);
452
453 if (console)
454 vcons_replay_msgbuf(&voodoofb_console_screen);
455 aa.console = console;
456 aa.scrdata = &voodoofb_screenlist;
457 aa.accessops = &voodoofb_accessops;
458 aa.accesscookie = &sc->vd;
459
460 config_found(self, &aa, wsemuldisplaydevprint);
461 config_found_ia(self, "drm", aux, voodoofb_drm_print);
462 }
463
464 static int
465 voodoofb_drm_print(void *opaque, const char *pnp)
466 {
467 if (pnp)
468 aprint_normal("drm at %s", pnp);
469
470 return UNCONF;
471 }
472
473 static int
474 voodoofb_drm_unmap(struct voodoofb_softc *sc)
475 {
476 printf("%s: releasing bus resources\n", device_xname(sc->sc_dev));
477
478 bus_space_unmap(sc->sc_ioregt, sc->sc_ioregh, sc->sc_ioregsize);
479 bus_space_unmap(sc->sc_regt, sc->sc_regh, sc->sc_regsize);
480 bus_space_unmap(sc->sc_fbt, sc->sc_fbh, sc->sc_fbsize);
481
482 return 0;
483 }
484
485 static int
486 voodoofb_drm_map(struct voodoofb_softc *sc)
487 {
488 if (pci_mapreg_map(&sc->sc_pa, 0x14, PCI_MAPREG_TYPE_MEM,
489 BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE |
490 BUS_SPACE_MAP_LINEAR,
491 &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
492 aprint_error_dev(sc->sc_dev, "failed to map the frame buffer.\n");
493 }
494
495 /* memory-mapped registers */
496 if (pci_mapreg_map(&sc->sc_pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
497 &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
498 aprint_error_dev(sc->sc_dev, "failed to map memory-mapped registers.\n");
499 }
500
501 /* IO-mapped registers */
502 if (pci_mapreg_map(&sc->sc_pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
503 &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
504 &sc->sc_ioregsize)) {
505 aprint_error_dev(sc->sc_dev, "failed to map IO-mapped registers.\n");
506 }
507
508 voodoofb_init(sc);
509 /* XXX this should at least be configurable via kernel config */
510 voodoofb_set_videomode(sc, sc->sc_videomode);
511
512 return 0;
513 }
514
515 static int
516 voodoofb_putpalreg(struct voodoofb_softc *sc, uint8_t index, uint8_t r,
517 uint8_t g, uint8_t b)
518 {
519 uint32_t color;
520
521 sc->sc_cmap_red[index] = r;
522 sc->sc_cmap_green[index] = g;
523 sc->sc_cmap_blue[index] = b;
524
525 color = (r << 16) | (g << 8) | b;
526 voodoo3_make_room(sc, 2);
527 voodoo3_write32(sc, DACADDR, index);
528 voodoo3_write32(sc, DACDATA, color);
529
530 return 0;
531 }
532
533 static int
534 voodoofb_putcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
535 {
536 u_char *r, *g, *b;
537 u_int index = cm->index;
538 u_int count = cm->count;
539 int i, error;
540 u_char rbuf[256], gbuf[256], bbuf[256];
541
542 #ifdef VOODOOFB_DEBUG
543 printf("putcmap: %d %d\n",index, count);
544 #endif
545 if (cm->index >= 256 || cm->count > 256 ||
546 (cm->index + cm->count) > 256)
547 return EINVAL;
548 error = copyin(cm->red, &rbuf[index], count);
549 if (error)
550 return error;
551 error = copyin(cm->green, &gbuf[index], count);
552 if (error)
553 return error;
554 error = copyin(cm->blue, &bbuf[index], count);
555 if (error)
556 return error;
557
558 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
559 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
560 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
561
562 r = &sc->sc_cmap_red[index];
563 g = &sc->sc_cmap_green[index];
564 b = &sc->sc_cmap_blue[index];
565
566 for (i = 0; i < count; i++) {
567 voodoofb_putpalreg(sc, index, *r, *g, *b);
568 index++;
569 r++, g++, b++;
570 }
571 return 0;
572 }
573
574 static int
575 voodoofb_getcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
576 {
577 u_int index = cm->index;
578 u_int count = cm->count;
579 int error;
580
581 if (index >= 255 || count > 256 || index + count > 256)
582 return EINVAL;
583
584 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
585 if (error)
586 return error;
587 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
588 if (error)
589 return error;
590 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
591 if (error)
592 return error;
593
594 return 0;
595 }
596
597 static int
598 voodoofb_is_console(struct pci_attach_args *pa)
599 {
600
601 #ifdef HAVE_OPENFIRMWARE
602 /* check if we're the /chosen console device */
603 int chosen, stdout, node, us;
604
605 us=pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
606 chosen = OF_finddevice("/chosen");
607 OF_getprop(chosen, "stdout", &stdout, 4);
608 node = OF_instance_to_package(stdout);
609 return(us == node);
610 #else
611 /* XXX how do we know we're console on i386? */
612 return 1;
613 #endif
614 }
615
616 static void
617 voodoofb_clearscreen(struct voodoofb_softc *sc)
618 {
619 voodoofb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
620 }
621
622 /*
623 * wsdisplay_emulops
624 */
625
626 static void
627 voodoofb_cursor(void *cookie, int on, int row, int col)
628 {
629 struct rasops_info *ri = cookie;
630 struct vcons_screen *scr = ri->ri_hw;
631 struct voodoofb_softc *sc = scr->scr_cookie;
632 int x, y, wi, he;
633
634 wi = ri->ri_font->fontwidth;
635 he = ri->ri_font->fontheight;
636
637 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
638 x = ri->ri_ccol * wi + ri->ri_xorigin;
639 y = ri->ri_crow * he + ri->ri_yorigin;
640 if (ri->ri_flg & RI_CURSOR) {
641 voodoofb_rectinvert(sc, x, y, wi, he);
642 ri->ri_flg &= ~RI_CURSOR;
643 }
644 ri->ri_crow = row;
645 ri->ri_ccol = col;
646 if (on)
647 {
648 x = ri->ri_ccol * wi + ri->ri_xorigin;
649 y = ri->ri_crow * he + ri->ri_yorigin;
650 voodoofb_rectinvert(sc, x, y, wi, he);
651 ri->ri_flg |= RI_CURSOR;
652 }
653 } else {
654 ri->ri_flg &= ~RI_CURSOR;
655 ri->ri_crow = row;
656 ri->ri_ccol = col;
657 }
658 }
659
660 #if 0
661 int
662 voodoofb_mapchar(void *cookie, int uni, u_int *index)
663 {
664 return 0;
665 }
666 #endif
667
668 static void
669 voodoofb_putchar(void *cookie, int row, int col, u_int c, long attr)
670 {
671 struct rasops_info *ri = cookie;
672 struct wsdisplay_font *font = PICK_FONT(ri, c);
673 struct vcons_screen *scr = ri->ri_hw;
674 struct voodoofb_softc *sc = scr->scr_cookie;
675
676 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
677 uint8_t *data;
678 int fg, bg, uc, i;
679 int x, y, wi, he;
680
681 wi = font->fontwidth;
682 he = font->fontheight;
683
684 if (!CHAR_IN_FONT(c, font))
685 return;
686 bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
687 fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
688 x = ri->ri_xorigin + col * wi;
689 y = ri->ri_yorigin + row * he;
690 if (c == 0x20) {
691 voodoofb_rectfill(sc, x, y, wi, he, bg);
692 } else {
693 uc = c - font->firstchar;
694 data = (uint8_t *)font->data + uc *
695 ri->ri_fontscale;
696 voodoofb_setup_mono(sc, x, y, wi, he, fg, bg);
697 for (i = 0; i < he; i++) {
698 voodoofb_feed_line(sc, font->stride, data);
699 data += font->stride;
700 }
701 }
702 }
703 }
704
705 static void
706 voodoofb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
707 {
708 struct rasops_info *ri = cookie;
709 struct vcons_screen *scr = ri->ri_hw;
710 struct voodoofb_softc *sc = scr->scr_cookie;
711 int32_t xs, xd, y, width, height;
712
713 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
714 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
715 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
716 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
717 width = ri->ri_font->fontwidth * ncols;
718 height = ri->ri_font->fontheight;
719 voodoofb_bitblt(sc, xs, y, xd, y, width, height);
720 }
721 }
722
723 static void
724 voodoofb_erasecols(void *cookie, int row, int startcol, int ncols,
725 long fillattr)
726 {
727 struct rasops_info *ri = cookie;
728 struct vcons_screen *scr = ri->ri_hw;
729 struct voodoofb_softc *sc = scr->scr_cookie;
730 int32_t x, y, width, height, fg, bg, ul;
731
732 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
733 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
734 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
735 width = ri->ri_font->fontwidth * ncols;
736 height = ri->ri_font->fontheight;
737 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
738
739 voodoofb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
740 }
741 }
742
743 static void
744 voodoofb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
745 {
746 struct rasops_info *ri = cookie;
747 struct vcons_screen *scr = ri->ri_hw;
748 struct voodoofb_softc *sc = scr->scr_cookie;
749 int32_t x, ys, yd, width, height;
750
751 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
752 x = ri->ri_xorigin;
753 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
754 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
755 width = ri->ri_emuwidth;
756 height = ri->ri_font->fontheight * nrows;
757 voodoofb_bitblt(sc, x, ys, x, yd, width, height);
758 }
759 }
760
761 static void
762 voodoofb_eraserows(void *cookie, int row, int nrows, long fillattr)
763 {
764 struct rasops_info *ri = cookie;
765 struct vcons_screen *scr = ri->ri_hw;
766 struct voodoofb_softc *sc = scr->scr_cookie;
767 int32_t x, y, width, height, fg, bg, ul;
768
769 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
770 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
771 if ((row == 0) && (nrows == ri->ri_rows)) {
772 /* clear the whole screen */
773 voodoofb_rectfill(sc, 0, 0, ri->ri_width,
774 ri->ri_height, ri->ri_devcmap[bg]);
775 } else {
776 x = ri->ri_xorigin;
777 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
778 width = ri->ri_emuwidth;
779 height = ri->ri_font->fontheight * nrows;
780 voodoofb_rectfill(sc, x, y, width, height,
781 ri->ri_devcmap[bg]);
782 }
783 }
784 }
785
786 static void
787 voodoofb_bitblt(struct voodoofb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
788 {
789 uint32_t fmt, blitcmd;
790
791 fmt = sc->linebytes | ((sc->bits_per_pixel +
792 ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
793 blitcmd = COMMAND_2D_S2S_BITBLT | (ROP_COPY << 24);
794
795 if (xs <= xd) {
796 blitcmd |= BIT(14);
797 xs += (width - 1);
798 xd += (width - 1);
799 }
800 if (ys <= yd) {
801 blitcmd |= BIT(15);
802 ys += (height - 1);
803 yd += (height - 1);
804 }
805 voodoo3_make_room(sc, 6);
806
807 voodoo3_write32(sc, SRCFORMAT, fmt);
808 voodoo3_write32(sc, DSTFORMAT, fmt);
809 voodoo3_write32(sc, DSTSIZE, width | (height << 16));
810 voodoo3_write32(sc, DSTXY, xd | (yd << 16));
811 voodoo3_write32(sc, SRCXY, xs | (ys << 16));
812 voodoo3_write32(sc, COMMAND_2D, blitcmd | SST_2D_GO);
813 }
814
815 static void
816 voodoofb_rectfill(struct voodoofb_softc *sc, int x, int y, int width,
817 int height, int colour)
818 {
819 uint32_t fmt, col;
820
821 col = (colour << 24) | (colour << 16) | (colour << 8) | colour;
822 fmt = sc->linebytes | ((sc->bits_per_pixel +
823 ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
824
825 voodoo3_make_room(sc, 6);
826 voodoo3_write32(sc, DSTFORMAT, fmt);
827 voodoo3_write32(sc, COLORFORE, colour);
828 voodoo3_write32(sc, COLORBACK, colour);
829 voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT | (ROP_COPY << 24));
830 voodoo3_write32(sc, DSTSIZE, width | (height << 16));
831 voodoo3_write32(sc, LAUNCH_2D, x | (y << 16));
832 }
833
834 static void
835 voodoofb_rectinvert(struct voodoofb_softc *sc, int x, int y, int width,
836 int height)
837 {
838 uint32_t fmt;
839
840 fmt = sc->linebytes | ((sc->bits_per_pixel +
841 ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
842
843 voodoo3_make_room(sc, 6);
844 voodoo3_write32(sc, DSTFORMAT, fmt);
845 voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT |
846 (ROP_INVERT << 24));
847 voodoo3_write32(sc, DSTSIZE, width | (height << 16));
848 voodoo3_write32(sc, DSTXY, x | (y << 16));
849 voodoo3_write32(sc, LAUNCH_2D, x | (y << 16));
850 }
851
852 static void
853 voodoofb_setup_mono(struct voodoofb_softc *sc, int xd, int yd, int width, int height, uint32_t fg,
854 uint32_t bg)
855 {
856 uint32_t dfmt, sfmt = sc->linebytes;
857
858 dfmt = sc->linebytes | ((sc->bits_per_pixel +
859 ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
860
861 voodoo3_make_room(sc, 9);
862 voodoo3_write32(sc, SRCFORMAT, sfmt);
863 voodoo3_write32(sc, DSTFORMAT, dfmt);
864 voodoo3_write32(sc, COLORFORE, fg);
865 voodoo3_write32(sc, COLORBACK, bg);
866 voodoo3_write32(sc, DSTSIZE, width | (height << 16));
867 voodoo3_write32(sc, DSTXY, xd | (yd << 16));
868 voodoo3_write32(sc, SRCXY, 0);
869 voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
870 (ROP_COPY << 24) | SST_2D_GO);
871
872 /* now feed the data into the chip */
873 }
874
875 static void
876 voodoofb_feed_line(struct voodoofb_softc *sc, int count, uint8_t *data)
877 {
878 int i;
879 uint32_t latch = 0, bork;
880 int shift = 0;
881
882 voodoo3_make_room(sc, count);
883 for (i = 0; i < count; i++) {
884 bork = data[i];
885 latch |= (bork << shift);
886 if (shift == 24) {
887 voodoo3_write32(sc, LAUNCH_2D, latch);
888 latch = 0;
889 shift = 0;
890 } else
891 shift += 8;
892 }
893 if (shift != 24)
894 voodoo3_write32(sc, LAUNCH_2D, latch);
895 }
896
897 #ifdef VOODOOFB_DEBUG
898 static void
899 voodoofb_showpal(struct voodoofb_softc *sc)
900 {
901 int i, x = 0;
902
903 for (i = 0; i < 16; i++) {
904 voodoofb_rectfill(sc, x, 0, 64, 64, i);
905 x += 64;
906 }
907 }
908 #endif
909
910 #if 0
911 static int
912 voodoofb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
913 {
914
915 return 0;
916 }
917 #endif
918
919 /*
920 * wsdisplay_accessops
921 */
922
923 static int
924 voodoofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
925 struct lwp *l)
926 {
927 struct vcons_data *vd = v;
928 struct voodoofb_softc *sc = vd->cookie;
929 struct wsdisplay_fbinfo *wdf;
930 struct vcons_screen *ms = vd->active;
931
932 switch (cmd) {
933 case WSDISPLAYIO_GTYPE:
934 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
935 return 0;
936
937 case WSDISPLAYIO_GINFO:
938 wdf = (void *)data;
939 wdf->height = ms->scr_ri.ri_height;
940 wdf->width = ms->scr_ri.ri_width;
941 wdf->depth = ms->scr_ri.ri_depth;
942 wdf->cmsize = 256;
943 return 0;
944
945 case WSDISPLAYIO_GETCMAP:
946 return voodoofb_getcmap(sc,
947 (struct wsdisplay_cmap *)data);
948
949 case WSDISPLAYIO_PUTCMAP:
950 return voodoofb_putcmap(sc,
951 (struct wsdisplay_cmap *)data);
952
953 /* PCI config read/write passthrough. */
954 case PCI_IOC_CFGREAD:
955 case PCI_IOC_CFGWRITE:
956 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
957 cmd, data, flag, l));
958
959 case WSDISPLAYIO_SMODE:
960 {
961 int new_mode = *(int*)data;
962 if (new_mode != sc->sc_mode)
963 {
964 sc->sc_mode = new_mode;
965 if(new_mode == WSDISPLAYIO_MODE_EMUL)
966 {
967 voodoofb_drm_map(sc);
968 int i;
969
970 /* restore the palette */
971 for (i = 0; i < 256; i++) {
972 voodoofb_putpalreg(sc,
973 i,
974 sc->sc_cmap_red[i],
975 sc->sc_cmap_green[i],
976 sc->sc_cmap_blue[i]);
977 }
978 vcons_redraw_screen(ms);
979 } else
980 voodoofb_drm_unmap(sc);
981 }
982 }
983 return 0;
984 }
985 return EPASSTHROUGH;
986 }
987
988 static paddr_t
989 voodoofb_mmap(void *v, void *vs, off_t offset, int prot)
990 {
991 struct vcons_data *vd = v;
992 struct voodoofb_softc *sc = vd->cookie;
993 paddr_t pa;
994
995 /* 'regular' framebuffer mmap()ing */
996 if (offset < sc->sc_fbsize) {
997 pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
998 BUS_SPACE_MAP_LINEAR);
999 return pa;
1000 }
1001
1002 /*
1003 * restrict all other mappings to processes with superuser privileges
1004 * or the kernel itself
1005 */
1006 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
1007 NULL) != 0) {
1008 aprint_error_dev(sc->sc_dev, "mmap() rejected.\n");
1009 return -1;
1010 }
1011
1012 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
1013 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1014 BUS_SPACE_MAP_LINEAR);
1015 return pa;
1016 }
1017
1018 if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
1019 sc->sc_regsize))) {
1020 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1021 BUS_SPACE_MAP_LINEAR);
1022 return pa;
1023 }
1024
1025 #ifdef PCI_MAGIC_IO_RANGE
1026 /* allow mapping of IO space */
1027 if ((offset >= PCI_MAGIC_IO_RANGE) &&\
1028 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
1029 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
1030 0, prot, BUS_SPACE_MAP_LINEAR);
1031 return pa;
1032 }
1033 #endif
1034
1035 #ifdef OFB_ALLOW_OTHERS
1036 if (offset >= 0x80000000) {
1037 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1038 BUS_SPACE_MAP_LINEAR);
1039 return pa;
1040 }
1041 #endif
1042 return -1;
1043 }
1044
1045 static void
1046 voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
1047 int existing, long *defattr)
1048 {
1049 struct voodoofb_softc *sc = cookie;
1050 struct rasops_info *ri = &scr->scr_ri;
1051
1052 ri->ri_depth = sc->bits_per_pixel;
1053 ri->ri_width = sc->width;
1054 ri->ri_height = sc->height;
1055 ri->ri_stride = sc->width;
1056 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
1057
1058 ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
1059
1060 #ifdef VOODOOFB_DEBUG
1061 printf("addr: %08lx\n", (ulong)ri->ri_bits);
1062 #endif
1063 if (existing) {
1064 ri->ri_flg |= RI_CLEAR;
1065 }
1066
1067 rasops_init(ri, sc->height/8, sc->width/8);
1068 ri->ri_caps = WSSCREEN_WSCOLORS;
1069
1070 rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
1071 sc->width / ri->ri_font->fontwidth);
1072
1073 ri->ri_hw = scr;
1074 ri->ri_ops.copyrows = voodoofb_copyrows;
1075 ri->ri_ops.copycols = voodoofb_copycols;
1076 ri->ri_ops.eraserows = voodoofb_eraserows;
1077 ri->ri_ops.erasecols = voodoofb_erasecols;
1078 ri->ri_ops.cursor = voodoofb_cursor;
1079 ri->ri_ops.putchar = voodoofb_putchar;
1080 }
1081
1082 #if 0
1083 int
1084 voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
1085 {
1086
1087 return 0;
1088 }
1089 #endif
1090
1091 #ifdef VOODOOFB_ENABLE_INTR
1092 static int
1093 voodoofb_intr(void *arg)
1094 {
1095 struct voodoofb_softc *sc = arg;
1096
1097 voodoo3_write32(sc, V3_STATUS, 0); /* clear interrupts */
1098 return 1;
1099 }
1100 #endif
1101
1102 /* video mode stuff */
1103
1104 #define REFFREQ 14318 /* .18 */
1105
1106 #define ABS(a) ((a < 0) ? -a : a)
1107
1108 static int
1109 voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
1110 {
1111 int m, n, k, best_m, best_n, best_k, f_cur, best_error;
1112 int minm, maxm;
1113
1114 best_error = freq;
1115 best_n = best_m = best_k = 0;
1116
1117 if (isBanshee) {
1118 minm = 24;
1119 maxm = 24;
1120 } else {
1121 minm = 1;
1122 maxm = 57;
1123 /* This used to be 64, alas it seems the last 8 (funny that ?)
1124 * values cause jittering at lower resolutions. I've not done
1125 * any calculations to what the adjustment affects clock ranges,
1126 * but I can still run at 1600x1200@75Hz */
1127 }
1128 for (n = 1; n < 256; n++) {
1129 f_cur = REFFREQ * (n + 2);
1130 if (f_cur < freq) {
1131 f_cur = f_cur / 3;
1132 if (freq - f_cur < best_error) {
1133 best_error = freq - f_cur;
1134 best_n = n;
1135 best_m = 1;
1136 best_k = 0;
1137 continue;
1138 }
1139 }
1140 for (m = minm; m < maxm; m++) {
1141 for (k = 0; k < 4; k++) {
1142 f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
1143 if (ABS(f_cur - freq) < best_error) {
1144 best_error = ABS(f_cur - freq);
1145 best_n = n;
1146 best_m = m;
1147 best_k = k;
1148 }
1149 }
1150 }
1151 }
1152 n = best_n;
1153 m = best_m;
1154 k = best_k;
1155 *f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
1156 return ( n << 8) | (m << 2) | k;
1157 }
1158
1159 static void
1160 voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
1161 {
1162 struct voodoo_regs mod;
1163 struct voodoo_regs *mode;
1164 uint32_t horizontal_display_end, horizontal_sync_start,
1165 horizontal_sync_end, horizontal_total,
1166 horizontal_blanking_start, horizontal_blanking_end;
1167
1168 uint32_t vertical_display_enable_end, vertical_sync_start,
1169 vertical_sync_end, vertical_total, vertical_blanking_start,
1170 vertical_blanking_end;
1171
1172 uint32_t wd; // CRTC offset
1173
1174 int i;
1175
1176 uint8_t misc;
1177
1178 memset(&mod, 0, sizeof(mode));
1179
1180 mode = &mod;
1181
1182 wd = (vm->hdisplay >> 3) - 1;
1183 horizontal_display_end = (vm->hdisplay >> 3) - 1;
1184 horizontal_sync_start = (vm->hsync_start >> 3) - 1;
1185 horizontal_sync_end = (vm->hsync_end >> 3) - 1;
1186 horizontal_total = (vm->htotal >> 3) - 1;
1187 horizontal_blanking_start = horizontal_display_end;
1188 horizontal_blanking_end = horizontal_total;
1189
1190 vertical_display_enable_end = vm->vdisplay - 1;
1191 vertical_sync_start = vm->vsync_start; // - 1;
1192 vertical_sync_end = vm->vsync_end; // - 1;
1193 vertical_total = vm->vtotal - 2;
1194 vertical_blanking_start = vertical_display_enable_end;
1195 vertical_blanking_end = vertical_total;
1196
1197 misc = 0x0f |
1198 (vm->hdisplay < 400 ? 0xa0 :
1199 vm->hdisplay < 480 ? 0x60 :
1200 vm->hdisplay < 768 ? 0xe0 : 0x20);
1201
1202 mode->vr_seq[0] = 3;
1203 mode->vr_seq[1] = 1;
1204 mode->vr_seq[2] = 8;
1205 mode->vr_seq[3] = 0;
1206 mode->vr_seq[4] = 6;
1207
1208 /* crtc regs start */
1209 mode->vr_crtc[0] = horizontal_total - 4;
1210 mode->vr_crtc[1] = horizontal_display_end;
1211 mode->vr_crtc[2] = horizontal_blanking_start;
1212 mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
1213 mode->vr_crtc[4] = horizontal_sync_start;
1214
1215 mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
1216 (horizontal_sync_end & 0x1f);
1217 mode->vr_crtc[6] = vertical_total;
1218 mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
1219 ((vertical_display_enable_end & 0x200) >> 3) |
1220 ((vertical_total & 0x200) >> 4) |
1221 0x10 |
1222 ((vertical_blanking_start & 0x100) >> 5) |
1223 ((vertical_sync_start & 0x100) >> 6) |
1224 ((vertical_display_enable_end & 0x100) >> 7) |
1225 ((vertical_total & 0x100) >> 8);
1226
1227 mode->vr_crtc[8] = 0;
1228 mode->vr_crtc[9] = 0x40 |
1229 ((vertical_blanking_start & 0x200) >> 4);
1230
1231 mode->vr_crtc[10] = 0;
1232 mode->vr_crtc[11] = 0;
1233 mode->vr_crtc[12] = 0;
1234 mode->vr_crtc[13] = 0;
1235 mode->vr_crtc[14] = 0;
1236 mode->vr_crtc[15] = 0;
1237
1238 mode->vr_crtc[16] = vertical_sync_start;
1239 mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
1240 mode->vr_crtc[18] = vertical_display_enable_end;
1241 mode->vr_crtc[19] = wd; // CRTC offset
1242 mode->vr_crtc[20] = 0;
1243 mode->vr_crtc[21] = vertical_blanking_start;
1244 mode->vr_crtc[22] = vertical_blanking_end + 1;
1245 mode->vr_crtc[23] = 128;
1246 mode->vr_crtc[24] = 255;
1247
1248 /* overflow registers */
1249 mode->vr_crtc[CRTC_HDISP_EXT] =
1250 (horizontal_total&0x100) >> 8 |
1251 (horizontal_display_end & 0x100) >> 6 |
1252 (horizontal_blanking_start & 0x100) >> 4 |
1253 (horizontal_blanking_end & 0x40) >> 1 |
1254 (horizontal_sync_start & 0x100) >> 2 |
1255 (horizontal_sync_end & 0x20) << 2;
1256
1257 mode->vr_crtc[CRTC_VDISP_EXT] =
1258 (vertical_total & 0x400) >> 10 |
1259 (vertical_display_enable_end & 0x400) >> 8 |
1260 (vertical_blanking_start & 0x400) >> 6 |
1261 (vertical_blanking_end & 0x400) >> 4;
1262
1263 /* attr regs start */
1264 mode->vr_attr[0] = 0;
1265 mode->vr_attr[1] = 0;
1266 mode->vr_attr[2] = 0;
1267 mode->vr_attr[3] = 0;
1268 mode->vr_attr[4] = 0;
1269 mode->vr_attr[5] = 0;
1270 mode->vr_attr[6] = 0;
1271 mode->vr_attr[7] = 0;
1272 mode->vr_attr[8] = 0;
1273 mode->vr_attr[9] = 0;
1274 mode->vr_attr[10] = 0;
1275 mode->vr_attr[11] = 0;
1276 mode->vr_attr[12] = 0;
1277 mode->vr_attr[13] = 0;
1278 mode->vr_attr[14] = 0;
1279 mode->vr_attr[15] = 0;
1280 mode->vr_attr[16] = 1;
1281 mode->vr_attr[17] = 0;
1282 mode->vr_attr[18] = 15;
1283 mode->vr_attr[19] = 0;
1284 /* attr regs end */
1285
1286 /* graph regs start */
1287 mode->vr_graph[0] = 159;
1288 mode->vr_graph[1] = 127;
1289 mode->vr_graph[2] = 127;
1290 mode->vr_graph[3] = 131;
1291 mode->vr_graph[4] = 130;
1292 mode->vr_graph[5] = 142;
1293 mode->vr_graph[6] = 30;
1294 mode->vr_graph[7] = 245;
1295 mode->vr_graph[8] = 0;
1296
1297 vga_outb(sc, MISC_W, misc | 0x01);
1298
1299 for(i = 0; i < 5; i++)
1300 voodoo3_write_seq(sc, i, mode->vr_seq[i]);
1301 for (i = 0; i < CRTC_PCI_READBACK; i ++)
1302 voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
1303 for (i = 0; i < 0x14; i ++)
1304 voodoo3_write_attr(sc, i, mode->vr_attr[i]);
1305 for (i = 0; i < 0x09; i ++)
1306 voodoo3_write_gra(sc, i, mode->vr_graph[i]);
1307 }
1308
1309 static void
1310 voodoofb_set_videomode(struct voodoofb_softc *sc,
1311 const struct videomode *vm)
1312 {
1313 uint32_t miscinit0 = 0;
1314 int vidpll, fout;
1315 uint32_t vp, vidproc = VIDPROCDEFAULT;
1316 uint32_t bpp = 1; /* for now */
1317 uint32_t bytes_per_row = vm->hdisplay * bpp;
1318
1319 sc->bits_per_pixel = bpp << 3;
1320 sc->width = vm->hdisplay;
1321 sc->height = vm->vdisplay;
1322 sc->linebytes = bytes_per_row;
1323
1324 voodoofb_setup_monitor(sc, vm);
1325 vp = voodoo3_read32(sc, VIDPROCCFG);
1326
1327 vidproc &= ~(0x1c0000); /* clear bits 18 to 20, bpp in vidproccfg */
1328 /* enable bits 18 to 20 to the required bpp */
1329 vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT);
1330
1331 vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
1332
1333 #ifdef VOODOOFB_DEBUG
1334 printf("old vidproc: %08x\n", vp);
1335 printf("pll: %08x %d\n", vidpll, fout);
1336 #endif
1337 /* bit 10 of vidproccfg, is enabled or disabled as needed */
1338 switch (bpp) {
1339 case 1:
1340 /*
1341 * bit 10 off for palettized modes only, off means
1342 * palette is used
1343 */
1344 vidproc &= ~(1 << 10);
1345 break;
1346 #if 0
1347 case 2:
1348 #if __POWERPC__
1349 miscinit0 = 0xc0000000;
1350 #endif
1351 /* bypass palette for 16bit modes */
1352 vidproc |= (1 << 10);
1353 break;
1354 case 4:
1355 #if __POWERPC__
1356 miscinit0 = 0x40000000;
1357 #endif
1358 vidproc |= (1 << 10); /* Same for 32bit modes */
1359 break;
1360 #endif
1361 default:
1362 printf("We support only 8 bit for now\n");
1363 return;
1364 }
1365
1366 voodoofb_wait_idle(sc);
1367
1368 voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
1369
1370 voodoo3_make_room(sc, 4);
1371 voodoo3_write32(sc, VGAINIT0, 4928);
1372 voodoo3_write32(sc, DACMODE, 0);
1373 voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
1374 voodoo3_write32(sc, PLLCTRL0, vidpll);
1375
1376 voodoo3_make_room(sc, 5);
1377 voodoo3_write32(sc, VIDSCREENSIZE, sc->width | (sc->height << 12));
1378 voodoo3_write32(sc, VIDDESKSTART, 0);
1379
1380 vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
1381 voodoo3_write32(sc, VIDPROCCFG, vidproc);
1382
1383 voodoo3_write32(sc, VGAINIT1, 0);
1384 voodoo3_write32(sc, MISCINIT0, miscinit0);
1385 #ifdef VOODOOFB_DEBUG
1386 printf("vidproc: %08x\n", vidproc);
1387 #endif
1388 voodoo3_make_room(sc, 8);
1389 voodoo3_write32(sc, SRCBASE, 0);
1390 voodoo3_write32(sc, DSTBASE, 0);
1391 voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
1392 voodoo3_write32(sc, CLIP0MIN, 0);
1393 voodoo3_write32(sc, CLIP0MAX, 0x0fff0fff);
1394 voodoo3_write32(sc, CLIP1MIN, 0);
1395 voodoo3_write32(sc, CLIP1MAX, 0x0fff0fff);
1396 voodoo3_write32(sc, SRCXY, 0);
1397 voodoofb_wait_idle(sc);
1398 printf("%s: switched to %dx%d, %d bit\n", device_xname(sc->sc_dev),
1399 sc->width, sc->height, sc->bits_per_pixel);
1400 }
1401
1402 static void
1403 voodoofb_init(struct voodoofb_softc *sc)
1404 {
1405 /* XXX */
1406 uint32_t vgainit0 = 0;
1407 uint32_t vidcfg = 0;
1408
1409 #ifdef VOODOOFB_DEBUG
1410 printf("initializing engine...");
1411 #endif
1412 vgainit0 = voodoo3_read32(sc, VGAINIT0);
1413 #ifdef VOODOOFB_DEBUG
1414 printf("vga: %08x", vgainit0);
1415 #endif
1416 vgainit0 |=
1417 VGAINIT0_8BIT_DAC |
1418 VGAINIT0_EXT_ENABLE |
1419 VGAINIT0_WAKEUP_3C3 |
1420 VGAINIT0_ALT_READBACK |
1421 VGAINIT0_EXTSHIFTOUT;
1422
1423 vidcfg = voodoo3_read32(sc, VIDPROCCFG);
1424 #ifdef VOODOOFB_DEBUG
1425 printf(" vidcfg: %08x\n", vidcfg);
1426 #endif
1427 vidcfg |=
1428 VIDCFG_VIDPROC_ENABLE |
1429 VIDCFG_DESK_ENABLE;
1430 vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
1431
1432 voodoo3_make_room(sc, 2);
1433
1434 voodoo3_write32(sc, VGAINIT0, vgainit0);
1435 voodoo3_write32(sc, VIDPROCCFG, vidcfg);
1436
1437 voodoo3_make_room(sc, 8);
1438 voodoo3_write32(sc, SRCBASE, 0);
1439 voodoo3_write32(sc, DSTBASE, 0);
1440 voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
1441 voodoo3_write32(sc, CLIP0MIN, 0);
1442 voodoo3_write32(sc, CLIP0MAX, 0x1fff1fff);
1443 voodoo3_write32(sc, CLIP1MIN, 0);
1444 voodoo3_write32(sc, CLIP1MAX, 0x1fff1fff);
1445 voodoo3_write32(sc, SRCXY, 0);
1446
1447 voodoofb_wait_idle(sc);
1448 }
1449