1 /* $NetBSD: vreset.c,v 1.10 2008/03/26 15:09:50 kiyohara Exp $ */
4 * Copyright (C) 1995-1997 Gary Thomas (gdt@linuxppc.org)
7 * Initialize the VGA control registers to 80x25 text mode.
9 * Adapted from a program by:
11 * San Francisco Indigo Company
12 * sfindigo!sellgren@uunet.uu.net
13 * Adapted for Moto boxes by:
14 * Pat Kane & Mark Scott, 1996
15 * Fixed for IBM/PowerStack II Pat Kane 1997
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by Gary Thomas.
28 * 4. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include <lib/libsa/stand.h>
45 #include <sys/bswap.h>
50 static char rcsid
[] = "vreset.c 2.0 1997 kane PEK'97 Exp $";
64 * Default console text mode registers used to reset
68 #define ENDMK 0xFFFF /* End marker */
70 #define S3Vendor 0x5333
71 #define CirrusVendor 0x1013
72 #define DiamondVendor 0x100E
73 #define MatroxVendor 0x102B
75 struct VgaRegs GenVgaTextRegs
[NREGS
+1] = {
76 /* port index value */
99 { 0x3d4, 0x10, 0x9c },
100 { 0x3d4, 0x11, 0x8e },
101 { 0x3d4, 0x12, 0x8f },
102 { 0x3d4, 0x13, 0x28 },
103 { 0x3d4, 0x14, 0x1f },
104 { 0x3d4, 0x15, 0x96 },
105 { 0x3d4, 0x16, 0xb9 },
106 { 0x3d4, 0x17, 0xa3 },
113 { 0x3ce, 0x5, 0x10 },
116 { 0x3ce, 0x8, 0xff },
120 struct VgaRegs S3TextRegs
[NREGS
+1] = {
121 /* port index value */
128 { 0x3d4, 0x0, 0x5f },
129 { 0x3d4, 0x1, 0x4f },
130 { 0x3d4, 0x2, 0x50 },
131 { 0x3d4, 0x3, 0x82 },
132 { 0x3d4, 0x4, 0x55 },
133 { 0x3d4, 0x5, 0x81 },
134 { 0x3d4, 0x6, 0xbf },
135 { 0x3d4, 0x7, 0x1f },
136 { 0x3d4, 0x8, 0x00 },
137 { 0x3d4, 0x9, 0x4f },
138 { 0x3d4, 0xa, 0x0d },
139 { 0x3d4, 0xb, 0x0e },
140 { 0x3d4, 0xc, 0x00 },
141 { 0x3d4, 0xd, 0x00 },
142 { 0x3d4, 0xe, 0x00 },
143 { 0x3d4, 0xf, 0x00 },
144 { 0x3d4, 0x10, 0x9c },
145 { 0x3d4, 0x11, 0x8e },
146 { 0x3d4, 0x12, 0x8f },
147 { 0x3d4, 0x13, 0x28 },
148 { 0x3d4, 0x14, 0x1f },
149 { 0x3d4, 0x15, 0x96 },
150 { 0x3d4, 0x16, 0xb9 },
151 { 0x3d4, 0x17, 0xa3 },
158 { 0x3ce, 0x5, 0x10 },
161 { 0x3ce, 0x8, 0xff },
170 * Default console text mode color table.
171 * These values were obtained by booting Linux with
172 * text mode firmware & then dumping the registers.
174 struct RGBColors TextCLUT
[256] = {
183 { 0x2a, 0x2a, 0x2a },
190 { 0x2a, 0x2a, 0x15 },
191 { 0x2a, 0x2a, 0x3f },
197 { 0x2a, 0x15, 0x2a },
199 { 0x2a, 0x3f, 0x2a },
204 { 0x2a, 0x15, 0x15 },
205 { 0x2a, 0x15, 0x3f },
206 { 0x2a, 0x3f, 0x15 },
207 { 0x2a, 0x3f, 0x3f },
211 { 0x15, 0x2a, 0x2a },
215 { 0x3f, 0x2a, 0x2a },
218 { 0x15, 0x2a, 0x15 },
219 { 0x15, 0x2a, 0x3f },
222 { 0x3f, 0x2a, 0x15 },
223 { 0x3f, 0x2a, 0x3f },
225 { 0x15, 0x15, 0x2a },
227 { 0x15, 0x3f, 0x2a },
229 { 0x3f, 0x15, 0x2a },
231 { 0x3f, 0x3f, 0x2a },
232 { 0x15, 0x15, 0x15 },
233 { 0x15, 0x15, 0x3f },
234 { 0x15, 0x3f, 0x15 },
235 { 0x15, 0x3f, 0x3f },
236 { 0x3f, 0x15, 0x15 },
237 { 0x3f, 0x15, 0x3f },
238 { 0x3f, 0x3f, 0x15 },
239 { 0x3f, 0x3f, 0x3f },
242 { 0x26, 0x10, 0x3d },
243 { 0x29, 0x29, 0x38 },
246 { 0x3c, 0x25, 0x33 },
250 { 0x25, 0x2a, 0x35 },
257 { 0x3c, 0x35, 0x2f },
258 { 0x2d, 0x26, 0x3e },
260 { 0x25, 0x3c, 0x11 },
267 { 0x2a, 0x37, 0x1f },
270 { 0x12, 0x2f, 0x19 },
271 { 0x29, 0x2e, 0x31 },
272 { 0x25, 0x13, 0x3e },
273 { 0x33, 0x3e, 0x33 },
274 { 0x1d, 0x2c, 0x25 },
276 { 0x32, 0x25, 0x39 },
279 { 0x36, 0x17, 0x34 },
284 { 0x19, 0x21, 0x3e },
285 { 0x27, 0x11, 0x2f },
286 { 0x38, 0x3f, 0x3c },
287 { 0x36, 0x2d, 0x15 },
290 { 0x1b, 0x11, 0x3f },
292 { 0x1a, 0x39, 0x3d },
294 { 0x22, 0x21, 0x23 },
296 { 0x1f, 0x22, 0x3d },
328 { 0x14, 0x14, 0x1c },
329 { 0x16, 0x14, 0x1c },
330 { 0x18, 0x14, 0x1c },
331 { 0x1a, 0x14, 0x1c },
332 { 0x1c, 0x14, 0x1c },
333 { 0x1c, 0x14, 0x1a },
334 { 0x1c, 0x14, 0x18 },
335 { 0x1c, 0x14, 0x16 },
336 { 0x1c, 0x14, 0x14 },
337 { 0x1c, 0x16, 0x14 },
338 { 0x1c, 0x18, 0x14 },
339 { 0x1c, 0x1a, 0x14 },
340 { 0x1c, 0x1c, 0x14 },
341 { 0x1a, 0x1c, 0x14 },
342 { 0x18, 0x1c, 0x14 },
343 { 0x16, 0x1c, 0x14 },
344 { 0x14, 0x1c, 0x14 },
345 { 0x14, 0x1c, 0x16 },
346 { 0x14, 0x1c, 0x18 },
347 { 0x14, 0x1c, 0x1a },
348 { 0x14, 0x1c, 0x1c },
349 { 0x14, 0x1a, 0x1c },
350 { 0x14, 0x18, 0x1c },
351 { 0x14, 0x16, 0x1c },
434 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
435 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
436 0x0C, 0x00, 0x0F, 0x08, 0x00
439 void enablePCIvideo(int);
440 static int scanPCI(void);
441 static int PCIVendor(int);
443 void writeAttr(u_char
, u_char
, u_char
);
444 void setTextRegs(struct VgaRegs
*);
445 void setTextCLUT(void);
446 void loadFont(u_char
*);
449 static void printslots(void);
453 vga_reset(u_char
*ISA_mem
)
456 struct VgaRegs
*VgaTextRegs
;
458 /* See if VGA already in TEXT mode - exit if so! */
460 if ((inb(0x3CF) & 0x01) == 0)
463 /* If no VGA responding in text mode, then we have some work to do... */
464 slot
= scanPCI(); /* find video card in use */
465 enablePCIvideo(slot
); /* enable I/O to card */
468 * Note: the PCI scanning code does not yet work correctly
469 * for non-Moto boxes, so the switch below only
470 * defaults to using an S3 card if it does not
471 * find a Cirrus card.
473 * The only reason we need to scan the bus looking for
474 * a graphics card is so we could do the "enablePCIvideo(slot)"
475 * call above; it is needed because Moto's OpenFirmware
476 * disables I/O to the graphics adapter before it gives
480 switch (PCIVendor(slot
)) {
481 default: /* Assume S3 */
486 VgaTextRegs
= S3TextRegs
;
487 outw(0x3C4, 0x0120); /* disable video */
488 setTextRegs(VgaTextRegs
); /* initial register setup */
489 setTextCLUT(); /* load color lookup table */
490 loadFont(ISA_mem
); /* load font */
491 setTextRegs(VgaTextRegs
); /* reload registers */
492 outw(0x3C4, 0x0100); /* re-enable video */
493 outb(0x3c2, 0x63); /* MISC */
494 outb(0x3c2, 0x67); /* MISC */
498 VgaTextRegs
= GenVgaTextRegs
;
499 outw(0x3C4, 0x0612); /* unlock ext regs */
500 outw(0x3C4, 0x0700); /* reset ext sequence mode */
501 outw(0x3C4, 0x0120); /* disable video */
502 setTextRegs(VgaTextRegs
); /* initial register setup */
503 setTextCLUT(); /* load color lookup table */
504 loadFont(ISA_mem
); /* load font */
505 setTextRegs(VgaTextRegs
); /* reload registers */
506 outw(0x3C4, 0x0100); /* re-enable video */
507 outb(0x3c2, 0x63); /* MISC */
513 * The following code is almost enuf to get the Matrox
514 * working (on a Moto box) but the video is not stable.
515 * We probably need to tweak the TVP3026 Video PLL regs. PEK'97
517 VgaTextRegs
= GenVgaTextRegs
;
518 outw(0x3C4, 0x0120); /* disable video */
519 setTextRegs(VgaTextRegs
); /* initial register setup */
520 setTextCLUT(); /* load color lookup table */
521 loadFont(ISA_mem
); /* load font */
522 setTextRegs(VgaTextRegs
); /* reload registers */
523 outw(0x3C4, 0x0100); /* re-enable video */
524 outb(0x3c2, 0x63); /* MISC */
533 delayLoop(2); /* give time for the video monitor to come up */
537 * Write to VGA Attribute registers.
540 writeAttr(u_char index
, u_char data
, u_char videoOn
)
543 v
= inb(0x3da); /* reset attr. address toggle */
545 outb(0x3c0, (index
& 0x1F) | 0x20);
547 outb(0x3c0, (index
& 0x1F));
552 setTextRegs(struct VgaRegs
*svp
)
559 while (svp
->io_port
!= ENDMK
) {
560 outb(svp
->io_port
, svp
->io_index
);
561 outb(svp
->io_port
+1, svp
->io_value
);
565 outb(0x3c2, 0x67); /* MISC */
566 outb(0x3c6, 0xff); /* MASK */
568 for (i
= 0; i
< 0x10; i
++)
569 writeAttr(i
, AC
[i
], 0); /* pallete */
570 writeAttr(0x10, 0x0c, 0); /* text mode */
571 writeAttr(0x11, 0x00, 0); /* overscan color (border) */
572 writeAttr(0x12, 0x0f, 0); /* plane enable */
573 writeAttr(0x13, 0x08, 0); /* pixel panning */
574 writeAttr(0x14, 0x00, 1); /* color select; video on */
587 for (i
= 0; i
< 256; i
++) {
588 outb(0x3C9, TextCLUT
[i
].r
);
589 outb(0x3C9, TextCLUT
[i
].g
);
590 outb(0x3C9, TextCLUT
[i
].b
);
595 loadFont(u_char
*ISA_mem
)
598 u_char
*font_page
= (u_char
*)&ISA_mem
[0xA0000];
604 i
= inb(0x3DA); /* Reset Attr toggle */
607 outb(0x3C0, 0x01); /* graphics mode */
609 outw(0x3C4, 0x0001); /* reset sequencer */
610 outw(0x3C4, 0x0204); /* write to plane 2 */
611 outw(0x3C4, 0x0406); /* enable plane graphics */
612 outw(0x3C4, 0x0003); /* reset sequencer */
613 outw(0x3CE, 0x0402); /* read plane 2 */
614 outw(0x3CE, 0x0500); /* write mode 0, read mode 0 */
615 outw(0x3CE, 0x0605); /* set graphics mode */
617 for (i
= 0; i
< sizeof(font
); i
+= 16) {
618 for (j
= 0; j
< 16; j
++) {
619 __asm
volatile("eieio");
620 font_page
[(2*i
)+j
] = font
[i
+j
];
628 /* From the S3 manual */
629 outb(0x46E8, 0x10); /* Put into setup mode */
631 outb(0x102, 0x01); /* Enable registers */
632 outb(0x46E8, 0x08); /* Enable video */
636 outb(0x42E8, 0x80); /* Reset graphics engine? */
638 outb(0x3D4, 0x38); /* Unlock all registers */
643 outb(0x3D5, inb(0x3D5)|0x01);
645 outb(0x3D5, inb(0x3D5)&~0x52);
647 outb(0x3D5, inb(0x3D5)&~0x30);
653 outb(0x3D5, inb(0x3D5)&~0x4B);
666 outb(0x3D4, 0x69); /* High order bits for cursor address */
670 outb(0x3D5, inb(0x3D5)&~0x10);
680 * should use devfunc number/indirect method to be totally safe on
681 * all machines, this works for now on 3 slot Moto boxes
684 struct PCI_ConfigInfo
{
685 u_long
* config_addr
;
686 u_long regs
[NPCIREGS
];
687 } PCI_slots
[NSLOTS
] = {
688 { (u_long
*)0x80800800, { 0xDE, 0xAD, 0xBE, 0xEF } },
689 { (u_long
*)0x80801000, { 0xDE, 0xAD, 0xBE, 0xEF } },
690 { (u_long
*)0x80802000, { 0xDE, 0xAD, 0xBE, 0xEF } },
691 { (u_long
*)0x80804000, { 0xDE, 0xAD, 0xBE, 0xEF } },
692 { (u_long
*)0x80808000, { 0xDE, 0xAD, 0xBE, 0xEF } },
697 * The following code modifies the PCI Command register
698 * to enable memory and I/O accesses.
701 enablePCIvideo(int slot
)
703 volatile u_char
*ppci
;
705 ppci
= (u_char
*)PCI_slots
[slot
].config_addr
;
706 ppci
[4] = 0x0003; /* enable memory and I/O accesses */
707 __asm
volatile("eieio");
710 outb(0x3d5, 0x0e); /* unlock CR0-CR7 */
722 struct PCI_ConfigInfo
*pslot
;
724 int highVgaSlot
= -1;
726 for (slt
= 0; slt
< NSLOTS
; slt
++) {
727 pslot
= &PCI_slots
[slt
];
728 for (r
= 0; r
< NPCIREGS
; r
++) {
729 pslot
->regs
[r
] = bswap32(pslot
->config_addr
[r
]);
732 if (pslot
->regs
[DEVID
] != 0xFFFFFFFF) { /* card in slot ? */
733 if ((pslot
->regs
[CLASS
] & 0xFFFFFF00) == 0x03000000) { /* VGA ? */
735 if ((pslot
->regs
[CMD
] & 0x03)) { /* did firmware enable it ? */
743 theSlot
= highVgaSlot
;
755 for (i
= 0; i
< 500; i
++) {
757 for (j
= 0; j
< 200; j
++) {
765 /* return Vendor ID of card in the slot */
767 PCIVendor(int slotnum
)
769 struct PCI_ConfigInfo
*pslot
;
771 pslot
= &PCI_slots
[slotnum
];
773 return (pslot
->regs
[DEVID
] & 0xFFFF);
781 for (i
= 0; i
< NSLOTS
; i
++) {
782 printf("PCI Slot number: %d", i
);
783 printf(" Vendor ID: 0x%x\n", PCIVendor(i
));
787 #endif /* CONS_VGA */