1 #include <console/console.h>
2 #include <device/pci.h>
3 #include <device/pci_ids.h>
4 #include <device/pci_ops.h>
10 /* vgabios.c. Derived from: */
12 /*------------------------------------------------------------ -*- C -*-
13 * 2 Kernel Monte a.k.a. Linux loading Linux on x86
15 * Erik Arjan Hendriks <hendriks@lanl.gov>
17 * This version is a derivative of the original two kernel monte
18 * which is (C) 2000 Scyld.
20 * Copyright (C) 2000 Scyld Computing Corporation
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; version 2 of the License.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
35 * Portions related to the alpha architecture are:
37 * Copyright(C) 2001 University of California. LA-CC Number 01-67.
38 * This software has been authored by an employee or employees of the
39 * University of California, operator of the Los Alamos National
40 * Laboratory under Contract No. W-7405-ENG-36 with the U.S.
41 * Department of Energy. The U.S. Government has rights to use,
42 * reproduce, and distribute this software. If the software is
43 * modified to produce derivative works, such modified software should
44 * be clearly marked, so as not to confuse it with the version
45 * available from LANL.
47 * This software may be used and distributed according to the terms
48 * of the GNU General Public License, incorporated herein by
49 * reference to http://www.gnu.org/licenses/gpl.html.
51 * This software is provided by the author(s) "as is" and any express
52 * or implied warranties, including, but not limited to, the implied
53 * warranties of merchantability and fitness for a particular purpose
54 * are disclaimed. In no event shall the author(s) be liable for any
55 * direct, indirect, incidental, special, exemplary, or consequential
56 * damages (including, but not limited to, procurement of substitute
57 * goods or services; loss of use, data, or profits; or business
58 * interruption) however caused and on any theory of liability,
59 * whether in contract, strict liability, or tort (including
60 * negligence or otherwise) arising in any way out of the use of this
61 * software, even if advised of the possibility of such damage.
63 * $Id: vgabios.c,v 1.5 2004/10/06 17:33:52 rminnich Exp $
64 *--------------------------------------------------------------------*/
66 /* Modified to be a self sufficient plug in so that it can be used
67 without reliance on other parts of coreboot's core
68 (C) 2005 Nick.Barker9@btinternet.com
70 Used initially for epia-m where there are problems getting the bios
71 emulator to successfully run this bios.
73 /* Declare a temporary global descriptor table - necessary because the
74 Core part of the bios no longer sets up any 16 bit segments */
76 /* pointer to original gdt */
80 /* compute the table limit */
81 "__mygdt_limit = __mygdt_end - __mygdt - 1 \n"
83 " .word __mygdt_limit \n"
86 /* selgdt 0, unused */
87 " .word 0x0000, 0x0000 \n"
88 " .byte 0x00, 0x00, 0x00, 0x00 \n"
89 /* selgdt 8, unused */
90 " .word 0x0000, 0x0000 \n"
91 " .byte 0x00, 0x00, 0x00, 0x00 \n"
92 /* selgdt 0x10, flat code segment */
93 " .word 0xffff, 0x0000 \n"
94 " .byte 0x00, 0x9b, 0xcf, 0x00 \n"
95 /* selgdt 0x18, flat data segment */
96 " .word 0xffff, 0x0000 \n"
97 " .byte 0x00, 0x93, 0xcf, 0x00 \n"
98 /* selgdt 0x20, unused */
99 " .word 0x0000, 0x0000 \n"
100 " .byte 0x00, 0x00, 0x00, 0x00 \n"
101 /* selgdt 0x28 16-bit 64k code at 0x00000000 */
102 " .word 0xffff, 0x0000 \n"
103 " .byte 0, 0x9a, 0, 0 \n"
104 /* selgdt 0x30 16-bit 64k data at 0x00000000 */
105 " .word 0xffff, 0x0000 \n"
106 " .byte 0, 0x92, 0, 0 \n"
109 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
110 __asm__("__myidt: \n"
114 " .long 0 \n" " .word 0 \n");
116 /* The address arguments to this function are PHYSICAL ADDRESSES */
117 extern u8 acpi_sleep_type;
119 static void real_mode_switch_call_vga(unsigned long devfn)
121 if ((acpi_sleep_type == 0)/* && (PAYLOAD_IS_SEABIOS == 1)*/)
123 __asm__ __volatile__(
124 // paranoia -- does ecx get saved? not sure. This is
125 // the easiest safe thing to do.
128 " mov %esp, __stack \n"
130 "__stack: .long 0 \n" "1:\n"
131 /* get devfn into %ecx */
132 " movl %esp, %ebp \n"
133 " movl 8(%ebp), %ecx \n"
135 " lgdt %cs:__mygdtaddr \n"
136 /* This configures CS properly for real mode. */
137 " ljmp $0x28, $__rms_16bit\n"
140 /* 16 bit code from here on... */
141 /* Load the segment registers w/ properly configured segment
142 * descriptors. They will retain these configurations (limits,
143 * writability, etc.) once protected mode is turned off. */
150 /* Turn off protection (bit 0 in CR0) */
151 " movl %cr0, %eax \n"
152 " andl $0xFFFFFFFE, %eax \n"
153 " movl %eax, %cr0 \n"
154 /* Now really going into real mode */
155 " ljmp $0, $__rms_real\n"
157 /* put the stack at the end of page zero.
158 * that way we can easily share it between real and protected,
159 * since the 16-bit ESP at segment 0 will work for any case.
163 " movl $0x1000, %eax \n"
164 " movl %eax, %esp \n"
165 /* Load our 16 it idt */
169 /* Dump zeros in the other segregs */
176 /* run VGA BIOS at 0xc000:0003 */
177 " lcall $0xc000, $0x0003\n"
178 /* if we got here, just about done.
179 * Need to get back to protected mode */
180 " movl %cr0, %eax \n" " orl $0x0000001, %eax\n" /* PE = 1 */
181 " movl %eax, %cr0 \n"
182 /* Now that we are in protected mode jump to a 32 bit code segment. */
183 " data32 ljmp $0x10, $vgarestart\n"
186 " movw $0x18, %ax \n"
192 /* restore proper gdt and idt */
193 " lgdt %cs:gdtarg \n"
197 " mov __stack, %esp \n"
201 __asm__(".text\n" "real_mode_switch_end:\n");
202 extern char real_mode_switch_end[];
204 /* call vga bios int 10 function 0x4f14 to enable main console
205 epia-m does not always autosence the main console so forcing it on is good !! */
206 void vga_enable_console()
208 if ((acpi_sleep_type == 0)/* && (PAYLOAD_IS_SEABIOS == 1)*/)
210 __asm__ __volatile__(
211 /* paranoia -- does ecx get saved? not sure. This is
212 * the easiest safe thing to do. */
215 " mov %esp, __stack \n"
217 " lgdt %cs:__mygdtaddr \n"
218 /* This configures CS properly for real mode. */
219 " ljmp $0x28, $__vga_ec_16bit\n"
222 /* 16 bit code from here on... */
223 /* Load the segment registers w/ properly configured segment
224 * descriptors. They will retain these configurations (limits,
225 * writability, etc.) once protected mode is turned off. */
232 /* Turn off protection (bit 0 in CR0) */
233 " movl %cr0, %eax \n"
234 " andl $0xFFFFFFFE, %eax\n"
235 " movl %eax, %cr0 \n"
236 /* Now really going into real mode */
237 " ljmp $0, $__vga_ec_real \n"
239 /* put the stack at the end of page zero.
240 * that way we can easily share it between real and protected,
241 * since the 16-bit ESP at segment 0 will work for any case.
245 " movl $0x1000, %eax \n"
246 " movl %eax, %esp \n"
247 /* debugging for RGM */
249 " outb %al, $0x80 \n"
250 /* Load our 16 it idt */
254 /* Dump zeros in the other segregs */
259 /* ask bios to enable main console */
260 /* set up for int 10 call - values found from X server
261 * bios call routines */
262 " movw $0x4f14,%ax \n"
263 " movw $0x8003,%bx \n"
268 " movb $0x55, %al \n"
269 " outb %al, $0x80 \n"
270 /* if we got here, just about done.
271 * Need to get back to protected mode */
272 " movl %cr0, %eax \n" " orl $0x0000001, %eax\n" /* PE = 1 */
273 " movl %eax, %cr0 \n"
274 /* Now that we are in protected mode jump to a 32 bit code segment. */
275 " data32 ljmp $0x10, $vga_ec_restart\n"
278 " movw $0x18, %ax \n"
284 /* restore proper gdt and idt */
285 " lgdt %cs:gdtarg \n"
287 " .globl vga__ec_exit \n"
289 " mov __stack, %esp \n"
293 void do_vgabios(void)
296 unsigned long busdevfn;
297 unsigned int rom = 0;
299 unsigned int size = 64 * 1024;
304 printk_emerg("file '%s', line %d\n\n", __FILE__, __LINE__);
306 /* clear vga bios data area */
307 for (i = 0x400; i < 0x500; i++) {
308 *(unsigned char *)i = 0;
311 dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, 0);
314 printk_debug("NO VGA FOUND\n");
317 printk_debug("found VGA: vid=%x, did=%x\n", dev->vendor, dev->device);
319 /* declare rom address here - keep any config data out of the way
320 * of core LXB stuff */
322 rom = 0xffffffff - CONFIG_ROM_SIZE + 1;
323 pci_write_config32(dev, PCI_ROM_ADDRESS, rom | 1);
324 printk_debug("rom base: %x\n", rom);
325 buf = (unsigned char *)rom;
326 printk_emerg("file '%s', line %d\n\n", __FILE__, __LINE__);
328 if ((buf[0] == 0x55) && (buf[1] == 0xaa)) {
329 memcpy((void *)0xc0000, buf, size);
331 printk_emerg("file '%s', line %d\n\n", __FILE__, __LINE__);
333 write_protect_vgabios(); // in northbridge
335 // check signature again
336 buf = (unsigned char *)0xc0000;
337 if (buf[0] == 0x55 && buf[1] == 0xAA) {
339 (dev->bus->secondary << 8) | dev->path.pci.devfn;
340 printk_debug("bus/devfn = %#x\n", busdevfn);
341 real_mode_switch_call_vga(busdevfn);
343 printk_debug("Failed to copy VGA BIOS to 0xc0000\n");
345 printk_debug("BAD SIGNATURE 0x%x 0x%x\n", buf[0], buf[1]);
347 printk_emerg("file '%s', line %d\n\n", __FILE__, __LINE__);
349 pci_write_config32(dev, PCI_ROM_ADDRESS, 0);
352 // we had hoped to avoid this.
353 // this is a stub IDT only. It's main purpose is to ignore calls
355 // no longer. Dammit. We have to respond to these.
357 unsigned short offset, cs;
360 // from a handy writeup that andrey found.
363 // There are some assumptions we can make here.
364 // First, the Top Of Stack (TOS) is located on the top of page zero.
365 // we can share this stack between real and protected mode.
366 // that simplifies a lot of things ...
367 // we'll just push all the registers on the stack as longwords,
368 // and pop to protected mode.
369 // second, since this only ever runs as part of coreboot,
370 // we know all the segment register values -- so we don't save any.
371 // keep the handler that calls things small. It can do a call to
372 // more complex code in coreboot itself. This helps a lot as we don't
373 // have to do address fixup in this little stub, and calls are absolute
374 // so the handler is relocatable.
377 __asm__ __volatile__(" .code16 \n"
381 " ljmp $0, $callbiosint16\n"
386 void debughandler(void)
388 __asm__ __volatile__(" .code16 \n"
396 "end_debughandle: \n" ".code32 \n");
399 // Calling conventions. The first C function is called with this stuff
400 // on the stack. They look like value parameters, but note that if you
401 // modify them they will go back to the INTx function modified.
402 // the C function will call the biosint function with these as
403 // REFERENCE parameters. In this way, we can easily get
404 // returns back to the INTx caller (i.e. vgabios)
405 void callbiosint(void)
407 __asm__ __volatile__(" .code16 \n"
411 " push %fs \n" " push %gs \n"
412 // clean up the int #. To save space we put it in the lower
413 // byte. But the top 24 bits are junk.
414 " andl $0xff, %eax\n"
415 // this push does two things:
416 // - put the INT # on the stack as a parameter
417 // - provides us with a temp for the %cr0 mods.
418 " pushl %eax \n" " movl %cr0, %eax\n" " orl $0x00000001, %eax\n" /* PE = 1 */
420 /* Now that we are in protected mode jump to a 32 bit code segment. */
421 " data32 ljmp $0x10, $biosprotect\n"
424 " movw $0x18, %ax \n"
432 // back to real mode ...
433 " ljmp $0x28, $__rms_16bit2\n"
436 /* 16 bit code from here on... */
437 /* Load the segment registers w/ properly configured segment
438 * descriptors. They will retain these configurations (limits,
439 * writability, etc.) once protected mode is turned off. */
446 /* Turn off protection (bit 0 in CR0) */
447 " movl %cr0, %eax \n"
448 " andl $0xFFFFFFFE, %eax \n"
449 " movl %eax, %cr0 \n"
450 /* Now really going into real mode */
451 " ljmp $0, $__rms_real2 \n"
454 * FixME: where is esp? */
457 /* ebugging for RGM */
459 " outb %al, $0x80 \n"
460 /* Load our 16 it idt */
464 /* Dump zeros in the other segregs */
470 /* pop the INT # that you pushed earlier */
486 int pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
487 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
488 unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
490 int handleint21(unsigned long *pedi, unsigned long *pesi,
491 unsigned long *pebp, unsigned long *pesp,
492 unsigned long *pebx, unsigned long *pedx,
493 unsigned long *pecx, unsigned long *peax,
494 unsigned long *pflags);
496 extern void vga_exit(void);
498 int biosint(unsigned long intnumber,
499 unsigned long gsfs, unsigned long dses,
500 unsigned long edi, unsigned long esi,
501 unsigned long ebp, unsigned long esp,
502 unsigned long ebx, unsigned long edx,
503 unsigned long ecx, unsigned long eax,
504 unsigned long cs_ip, unsigned short stackflags)
515 printk_debug("biosint: INT# 0x%lx\n", intnumber);
516 printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n",
518 printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
520 printk_debug("biosint: ip 0x%x cs 0x%x flags 0x%x\n",
523 // cases in a good compiler are just as good as your own tables.
525 case 0: case 1: case 2: case 3: case 4: case 5:
526 case 6: case 7: case 8: case 9: case 10:
527 case 11: case 12: case 13: case 14: case 15:
528 // These are not BIOS service, but the CPU-generated exceptions
529 printk_info("biosint: Oops, exception %u\n", intnumber);
531 printk_debug("Stack contents: ");
532 while (esp < 0x1000) {
533 printk_debug("0x%04x ", *(unsigned short *)esp);
538 printk_debug("biosint: Bailing out\n");
540 if ((acpi_sleep_type == 3)/* || (PAYLOAD_IS_SEABIOS == 0)*/) // add this to keep same with kevin's seabios patch in 2008-9-8
545 ret = pcibios(&edi, &esi, &ebp, &esp,
546 &ebx, &edx, &ecx, &eax, &flags);
554 ret = handleint21(&edi, &esi, &ebp, &esp,
555 &ebx, &edx, &ecx, &eax, &flags);
558 printk_info("BIOSINT: Unsupport int #0x%x\n", intnumber);
562 flags |= 1; // carry flags
569 void setup_realmode_idt(void)
571 extern unsigned char idthandle, end_idthandle;
572 extern unsigned char debughandle, end_debughandle;
575 struct realidt *idts = (struct realidt *)0;
576 int codesize = &end_idthandle - &idthandle;
577 unsigned char *intbyte, *codeptr;
579 // for each int, we create a customized little handler
580 // that just pushes %ax, puts the int # in %al,
581 // then calls the common interrupt handler.
582 // this necessitated because intel didn't know much about
583 // architecture when they did the 8086 (it shows)
584 // (hmm do they know anymore even now :-)
585 // obviously you can see I don't really care about memory
586 // efficiency. If I did I would probe back through the stack
587 // and get it that way. But that's really disgusting.
588 for (i = 0; i < 256; i++) {
590 codeptr = (char *)4096 + i * codesize;
591 idts[i].offset = (unsigned)codeptr;
592 memcpy(codeptr, &idthandle, codesize);
593 intbyte = codeptr + 3;
597 // fixed entry points
599 // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
601 // calling convention here is the same as INTs, we can reuse
602 // the int entry code.
603 codeptr = (char *)0xff065;
604 memcpy(codeptr, &idthandle, codesize);
605 intbyte = codeptr + 3;
606 *intbyte = 0x42; /* int42 is the relocated int10 */
609 VBIOS will call f000:f859 instead of sending int15.
610 calling convertion here is the same as INTs, we can reuse the int entry code.
612 codeptr = (char *)0xff859;
613 memcpy(codeptr, &idthandle, codesize);
614 intbyte = codeptr + 3;
617 /* debug handler - useful to set a programmable delay between instructions if the
618 TF bit is set upon call to real mode */
620 idts[1].offset = 16384;
621 memcpy(16384, &debughandle, &end_debughandle - &debughandle);
628 READCONFBYTE = 0xb108,
629 READCONFWORD = 0xb109,
630 READCONFDWORD = 0xb10a,
631 WRITECONFBYTE = 0xb10b,
632 WRITECONFWORD = 0xb10c,
633 WRITECONFDWORD = 0xb10d
636 // errors go in AH. Just set these up so that word assigns
639 PCIBIOS_NODEV = 0x8600,
640 PCIBIOS_BADREG = 0x8700
644 pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
645 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
646 unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
648 unsigned long edi = *pedi;
649 unsigned long esi = *pesi;
650 unsigned long ebp = *pebp;
651 unsigned long esp = *pesp;
652 unsigned long ebx = *pebx;
653 unsigned long edx = *pedx;
654 unsigned long ecx = *pecx;
655 unsigned long eax = *peax;
656 unsigned long flags = *pflags;
657 unsigned short func = (unsigned short)eax;
659 unsigned short devid, vendorid, devfn;
660 short devindex; /* Use short to get rid of garbage in upper half of 32-bit register */
676 while ((dev = dev_find_device(vendorid, devid, dev))) {
682 unsigned short busdevfn;
684 // busnum is an unsigned char;
685 // devfn is an int, so we mask it off.
686 busdevfn = (dev->bus->secondary << 8)
687 | (dev->path.pci.devfn & 0xff);
688 printk_debug("0x%x: return 0x%x\n", func,
693 *peax = PCIBIOS_NODEV;
710 devfn = *pebx & 0xff;
713 dev = dev_find_slot(bus, devfn);
716 ("0x%x: BAD DEVICE bus %d devfn 0x%x\n",
718 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
719 *peax = PCIBIOS_BADREG;
724 byte = pci_read_config8(dev, reg);
728 word = pci_read_config16(dev, reg);
732 dword = pci_read_config32(dev, reg);
737 pci_write_config8(dev, reg, byte);
741 pci_write_config16(dev, reg, word);
745 pci_write_config32(dev, reg, dword);
750 retval = PCIBIOS_BADREG;
752 ("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
753 func, bus, devfn, reg, *pecx);
759 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
766 /* return value of int0x15(int21)
767 AH AL Completion status
768 ?? 5Fh Function call supported
769 ?? !=5Fh Function not supported
770 00 5Fh Function call successful
771 01 5Fh Function call failed
773 int handleint21(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
774 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
775 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
778 switch (*eax & 0xffff) {
793 0011: 200MHz ( DDR200 )
794 0100: 266MHz ( DDR266 )
795 0101: 333MHz ( DDR333 )
796 0110: 400MHz ( DDR400 )
797 0111: 533MHz ( DDR I/II 533
798 1000: 667MHz ( DDR I/II 667)
800 N: Frame Buffer Size 2^N MB
804 dev = dev_find_slot(0, PCI_DEVFN(0, 3));
805 i = pci_read_config8(dev, 0xa1);
809 *eax = 0x00; //not support 5f18
814 i = pci_read_config8(dev, 0x90);
818 *ebx = (*ebx) + ((u32) i);
829 *ecx = (*ecx & 0xffffff00) | 2; // panel type = 2 = 1024 * 768
834 *ebx = (*ebx & 0xffff0000) | 2;
835 *ecx = (*ecx & 0xffff0000) | 0x401; // PAL + crt only
836 *edx = (*edx & 0xffff0000) | 0; // TV Layout - default