1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/pci.h>
4 #include <device/pci_ids.h>
5 #include <device/pci_ops.h>
11 /* vgabios.c. Derived from: */
13 /*------------------------------------------------------------ -*- C -*-
14 * 2 Kernel Monte a.k.a. Linux loading Linux on x86
16 * Erik Arjan Hendriks <hendriks@lanl.gov>
18 * This version is a derivative of the original two kernel monte
19 * which is (C) 2000 Scyld.
21 * Copyright (C) 2000 Scyld Computing Corporation
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 * Portions related to the alpha architecture are:
39 * Copyright(C) 2001 University of California. LA-CC Number 01-67.
40 * This software has been authored by an employee or employees of the
41 * University of California, operator of the Los Alamos National
42 * Laboratory under Contract No. W-7405-ENG-36 with the U.S.
43 * Department of Energy. The U.S. Government has rights to use,
44 * reproduce, and distribute this software. If the software is
45 * modified to produce derivative works, such modified software should
46 * be clearly marked, so as not to confuse it with the version
47 * available from LANL.
49 * This software may be used and distributed according to the terms
50 * of the GNU General Public License, incorporated herein by
51 * reference to http://www.gnu.org/licenses/gpl.html.
53 * This software is provided by the author(s) "as is" and any express
54 * or implied warranties, including, but not limited to, the implied
55 * warranties of merchantability and fitness for a particular purpose
56 * are disclaimed. In no event shall the author(s) be liable for any
57 * direct, indirect, incidental, special, exemplary, or consequential
58 * damages (including, but not limited to, procurement of substitute
59 * goods or services; loss of use, data, or profits; or business
60 * interruption) however caused and on any theory of liability,
61 * whether in contract, strict liability, or tort (including
62 * negligence or otherwise) arising in any way out of the use of this
63 * software, even if advised of the possibility of such damage.
65 * $Id: vgabios.c,v 1.100 2006/09/12 15:31:49 indrek Exp $
66 *--------------------------------------------------------------------*/
68 /* Modified to be a self sufficient plug in so that it can be used
69 without reliance on other parts of core Linuxbios
70 (C) 2005 Nick.Barker9@btinternet.com
72 Used initially for epia-m where there are problems getting the bios
73 emulator to successfully run this bios.
80 Modified to be an universal driver for loading VGA ROMs.
81 Aug 2006, anti.sullin@artecdesign.ee, Artec Design
84 define in your motherboard Config.lb file in device hierarchy
85 around the VGA pci device realmode chip and define its rom address.
86 Rom address is read from Config.lb, this rom is then copied to 0xC000 and then excecuted
88 chip drivers/pci/realmode
89 device pci 1.1 on end # VGA
90 register "rom_address" = "0xfffc0000" # at the beginning of 256k
93 then, chip enable is called at this list first traversal, and this sets
94 up device's init callback. Device init is called during last list traversal and
95 so, other hw should be already initialized to run vga bios successfully.
106 /* Declare a temporary global descriptor table - necessary because the
107 Core part of the bios no longer sets up any 16 bit segments */
109 /* pointer to original gdt */
111 " .word gdt_limit \n"
114 /* compute the table limit */
115 "__mygdt_limit = __mygdt_end - __mygdt - 1 \n"
118 " .word __mygdt_limit \n"
122 /* selgdt 0, unused */
123 " .word 0x0000, 0x0000 \n"
124 " .byte 0x00, 0x00, 0x00, 0x00 \n"
126 /* selgdt 8, unused */
127 " .word 0x0000, 0x0000 \n"
128 " .byte 0x00, 0x00, 0x00, 0x00 \n"
130 /* selgdt 0x10, flat code segment */
131 " .word 0xffff, 0x0000 \n"
132 " .byte 0x00, 0x9b, 0xcf, 0x00 \n"
134 /* selgdt 0x18, flat data segment */
135 " .word 0xffff, 0x0000 \n"
136 " .byte 0x00, 0x93, 0xcf, 0x00 \n"
138 /* selgdt 0x20, unused */
139 " .word 0x0000, 0x0000 \n"
140 " .byte 0x00, 0x00, 0x00, 0x00 \n"
142 /* selgdt 0x28 16-bit 64k code at 0x00000000 */
143 " .word 0xffff, 0x0000 \n"
144 " .byte 0, 0x9a, 0, 0 \n"
146 /* selgdt 0x30 16-bit 64k data at 0x00000000 */
147 " .word 0xffff, 0x0000 \n"
148 " .byte 0, 0x92, 0, 0 \n"
153 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
154 __asm__ ("__myidt: \n"
162 /* The address arguments to this function are PHYSICAL ADDRESSES */
163 static void real_mode_switch_call_vga(unsigned long devfn)
165 __asm__ __volatile__ (
166 // paranoia -- does ecx get saved? not sure. This is
167 // the easiest safe thing to do.
170 " mov %esp, __stack \n"
172 "__stack: .long 0 \n"
174 /* get devfn into %ecx */
175 " movl %esp, %ebp \n"
176 " movl 8(%ebp), %ecx \n"
178 " lgdt %cs:__mygdtaddr \n"
180 /* This configures CS properly for real mode. */
181 " ljmp $0x28, $__rms_16bit\n"
184 /* 16 bit code from here on... */
186 /* Load the segment registers w/ properly configured segment
187 * descriptors. They will retain these configurations (limits,
188 * writability, etc.) once protected mode is turned off. */
196 /* Turn off protection (bit 0 in CR0) */
197 " movl %cr0, %eax \n"
198 " andl $0xFFFFFFFE, %eax \n"
199 " movl %eax, %cr0 \n"
201 /* Now really going into real mode */
202 " ljmp $0, $__rms_real\n"
205 /* put the stack at the end of page zero.
206 * that way we can easily share it between real and protected,
207 * since the 16-bit ESP at segment 0 will work for any case. */
211 " movl $0x1000, %eax \n"
212 " movl %eax, %esp \n"
214 /* Load our 16 it idt */
219 /* Dump zeros in the other segregs */
227 /* run VGA BIOS at 0xc000:0003 */
228 " lcall $0xc000, $0x0003\n"
230 /* if we got here, just about done.
231 * Need to get back to protected mode */
232 " movl %cr0, %eax \n"
233 " orl $0x0000001, %eax\n" /* PE = 1 */
234 " movl %eax, %cr0 \n"
236 /* Now that we are in protected mode jump to a 32 bit code segment. */
237 " data32 ljmp $0x10, $vgarestart\n"
240 " movw $0x18, %ax \n"
247 /* restore proper gdt and idt */
248 " lgdt %cs:gdtarg \n"
253 " mov __stack, %esp \n"
258 __asm__ (".text\n""real_mode_switch_end:\n");
259 extern char real_mode_switch_end[];
261 /* ============================================================================================
265 void do_vgabios(device_t dev)
268 unsigned long busdevfn;
269 unsigned int rom = dev->rom_address;
271 unsigned int size = 64*1024;
274 /* clear vga bios data area */
275 for (i = 0x400; i < 0x500; i++) {
276 *(unsigned char *) i = 0;
280 printk_debug("NO VGA FOUND\n");
283 printk_debug("found VGA: vid=%x, did=%x\n", dev->vendor, dev->device);
285 /* declare rom address here - keep any config data out of the way
286 * of core LXB stuff */
288 //pci_write_config32(dev, PCI_ROM_ADDRESS, rom|1);
289 printk_debug("rom base, size: %x\n", rom);
291 buf = (unsigned char *) rom;
292 if ((buf[0] == 0x55) && (buf[1] == 0xaa)) {
293 memcpy((void *) 0xc0000, buf, size);
295 // write_protect_vgabios(); // in northbridge, not universal
297 // check signature again
298 buf = (unsigned char *) 0xc0000;
299 if (buf[0]==0x55 && buf[1]==0xAA) {
300 busdevfn = (dev->bus->secondary << 8) | dev->path.u.pci.devfn;
301 printk_debug("bus/devfn = %#x\n", busdevfn);
303 real_mode_switch_call_vga(busdevfn);
305 printk_debug("Failed to copy VGA BIOS to 0xc0000\n");
307 printk_debug("BAD SIGNATURE 0x%x 0x%x\n", buf[0], buf[1]);
309 pci_write_config32(dev, PCI_ROM_ADDRESS, 0);
312 /* ============================================================================================
313 /* call vga bios int 10 function 0x4f14 to enable main console
315 * vga bios does not always autosence the main console so forcing it on is good !!
317 static void vga_enable_console(void)
319 __asm__ __volatile__ (
320 /* paranoia -- does ecx get saved? not sure. This is
321 * the easiest safe thing to do. */
324 " mov %esp, __stack \n"
327 " lgdt %cs:__mygdtaddr \n"
329 /* This configures CS properly for real mode. */
330 " ljmp $0x28, $__vga_ec_16bit\n"
333 /* 16 bit code from here on... */
335 /* Load the segment registers w/ properly configured segment
336 * descriptors. They will retain these configurations (limits,
337 * writability, etc.) once protected mode is turned off. */
345 /* Turn off protection (bit 0 in CR0) */
346 " movl %cr0, %eax \n"
347 " andl $0xFFFFFFFE, %eax\n"
348 " movl %eax, %cr0 \n"
350 /* Now really going into real mode */
351 " ljmp $0, $__vga_ec_real \n"
354 /* put the stack at the end of page zero.
355 * that way we can easily share it between real and protected,
356 * since the 16-bit ESP at segment 0 will work for any case. */
360 " movl $0x1000, %eax \n"
361 " movl %eax, %esp \n"
363 /* debugging for RGM */
365 " outb %al, $0x80 \n"
367 /* Load our 16 it idt */
372 /* Dump zeros in the other segregs */
378 /* ask bios to enable main console */
379 /* set up for int 10 call - values found from X server
380 * bios call routines */
381 " movw $0x4f14,%ax \n"
382 " movw $0x8003,%bx \n"
388 " movb $0x55, %al \n"
389 " outb %al, $0x80 \n"
391 /* if we got here, just about done.
392 * Need to get back to protected mode */
393 " movl %cr0, %eax \n"
394 " orl $0x0000001, %eax\n" /* PE = 1 */
395 " movl %eax, %cr0 \n"
397 /* Now that we are in protected mode jump to a 32 bit code segment. */
398 " data32 ljmp $0x10, $vga_ec_restart\n"
401 " movw $0x18, %ax \n"
408 /* restore proper gdt and idt */
409 " lgdt %cs:gdtarg \n"
411 " .globl vga__ec_exit \n"
413 " mov __stack, %esp \n"
422 /* ============================================================================================
423 * real-mode interrupt handlers.
424 * These are handled because VGA ROMs tend to call bios ints in real mode
428 // we had hoped to avoid this.
429 // this is a stub IDT only. It's main purpose is to ignore calls
431 // no longer. Dammit. We have to respond to these.
433 unsigned short offset, cs;
436 // from a handy writeup that andrey found.
439 // There are some assumptions we can make here.
440 // First, the Top Of Stack (TOS) is located on the top of page zero.
441 // we can share this stack between real and protected mode.
442 // that simplifies a lot of things ...
443 // we'll just push all the registers on the stack as longwords,
444 // and pop to protected mode.
445 // second, since this only ever runs as part of linuxbios,
446 // we know all the segment register values -- so we don't save any.
447 // keep the handler that calls things small. It can do a call to
448 // more complex code in linuxbios itself. This helps a lot as we don't
449 // have to do address fixup in this little stub, and calls are absolute
450 // so the handler is relocatable.
451 void handler_vga(void)
453 __asm__ __volatile__ (
458 " ljmp $0, $callbiosint16\n"
464 void debughandler_vga(void)
466 __asm__ __volatile__ (
475 "end_debughandle: \n"
480 // Calling conventions. The first C function is called with this stuff
481 // on the stack. They look like value parameters, but note that if you
482 // modify them they will go back to the INTx function modified.
483 // the C function will call the biosint function with these as
484 // REFERENCE parameters. In this way, we can easily get
485 // returns back to the INTx caller (i.e. vgabios)
486 void callbiosint_vga(void)
488 __asm__ __volatile__ (
495 // clean up the int #. To save space we put it in the lower
496 // byte. But the top 24 bits are junk.
497 " andl $0xff, %eax\n"
498 // this push does two things:
499 // - put the INT # on the stack as a parameter
500 // - provides us with a temp for the %cr0 mods.
503 " orl $0x00000001, %eax\n" /* PE = 1 */
505 /* Now that we are in protected mode jump to a 32 bit code segment. */
506 " data32 ljmp $0x10, $biosprotect\n"
509 " movw $0x18, %ax \n"
516 " call biosint_vga \n"
517 // back to real mode ...
518 " ljmp $0x28, $__rms_16bit2\n"
521 /* 16 bit code from here on... */
522 /* Load the segment registers w/ properly configured segment
523 * descriptors. They will retain these configurations (limits,
524 * writability, etc.) once protected mode is turned off. */
532 /* Turn off protection (bit 0 in CR0) */
533 " movl %cr0, %eax \n"
534 " andl $0xFFFFFFFE, %eax \n"
535 " movl %eax, %cr0 \n"
537 /* Now really going into real mode */
538 " ljmp $0, $__rms_real2 \n"
542 * FixME: where is esp? */
546 /* ebugging for RGM */
548 " outb %al, $0x80 \n"
550 /* Load our 16 it idt */
555 /* Dump zeros in the other segregs */
562 /* pop the INT # that you pushed earlier */
583 int pcibios_vga(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
584 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
585 unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
587 int handleint21_vga(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
588 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
589 unsigned long *pecx, unsigned long *peax, unsigned long *pflags
592 extern void vga_exit(void);
594 int biosint_vga(unsigned long intnumber,
595 unsigned long gsfs, unsigned long dses,
596 unsigned long edi, unsigned long esi,
597 unsigned long ebp, unsigned long esp,
598 unsigned long ebx, unsigned long edx,
599 unsigned long ecx, unsigned long eax,
600 unsigned long cs_ip, unsigned short stackflags)
611 printk_debug("biosint: INT# 0x%lx\n", intnumber);
612 printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n",
614 printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
616 printk_debug("biosint: ip 0x%x cs 0x%x flags 0x%x\n",
619 // cases in a good compiler are just as good as your own tables.
622 // These are not BIOS service, but the CPU-generated exceptions
623 printk_info("biosint: Oops, exception %u\n", intnumber);
625 printk_debug("Stack contents: ");
626 while (esp < 0x1000) {
627 printk_debug("0x%04x ", *(unsigned short *) esp);
632 printk_debug("biosint: Bailing out\n");
638 ret = pcibios_vga( &edi, &esi, &ebp, &esp,
639 &ebx, &edx, &ecx, &eax, &flags);
647 ret=handleint21_vga( &edi, &esi, &ebp, &esp,
648 &ebx, &edx, &ecx, &eax, &flags);
651 printk_info("BIOSINT: Unsupport int #0x%x\n",
656 flags |= 1; // carry flags
669 READCONFBYTE = 0xb108,
670 READCONFWORD = 0xb109,
671 READCONFDWORD = 0xb10a,
672 WRITECONFBYTE = 0xb10b,
673 WRITECONFWORD = 0xb10c,
674 WRITECONFDWORD = 0xb10d
677 // errors go in AH. Just set these up so that word assigns
680 PCIBIOS_NODEV = 0x8600,
681 PCIBIOS_BADREG = 0x8700
685 pcibios_vga(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
686 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
687 unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
689 unsigned long edi = *pedi;
690 unsigned long esi = *pesi;
691 unsigned long ebp = *pebp;
692 unsigned long esp = *pesp;
693 unsigned long ebx = *pebx;
694 unsigned long edx = *pedx;
695 unsigned long ecx = *pecx;
696 unsigned long eax = *peax;
697 unsigned long flags = *pflags;
698 unsigned short func = (unsigned short) eax;
700 unsigned short devid, vendorid, devfn;
701 short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
717 while ((dev = dev_find_device(vendorid, devid, dev))) {
723 unsigned short busdevfn;
725 // busnum is an unsigned char;
726 // devfn is an int, so we mask it off.
727 busdevfn = (dev->bus->secondary << 8)
728 | (dev->path.u.pci.devfn & 0xff);
729 printk_debug("0x%x: return 0x%x\n", func, busdevfn);
733 *peax = PCIBIOS_NODEV;
750 devfn = *pebx & 0xff;
753 dev = dev_find_slot(bus, devfn);
755 printk_debug("0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
756 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
757 *peax = PCIBIOS_BADREG;
762 byte = pci_read_config8(dev, reg);
766 word = pci_read_config16(dev, reg);
770 dword = pci_read_config32(dev, reg);
775 pci_write_config8(dev, reg, byte);
779 pci_write_config16(dev, reg, word);
783 pci_write_config32(dev, reg, dword);
788 retval = PCIBIOS_BADREG;
789 printk_debug("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
790 func, bus, devfn, reg, *pecx);
796 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
803 int handleint21_vga(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
804 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
805 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
814 *ebx=0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
823 *ecx = (*ecx & 0xffffff00 ) | 2; // panel type = 2 = 1024 * 768
828 *ebx= (*ebx & 0xffff0000) | 2;
829 *ecx= (*ecx & 0xffff0000) | 0x401; // PAL + crt only
830 *edx= (*edx & 0xffff0000) | 0; // TV Layout - default
843 /* ============================================================================================
844 * This function sets up bios and pci interrupt handlers for real mode
845 * This is needed when running VGA BIOS in real mode
849 void setup_realmode_idt_vga(void)
851 extern unsigned char idthandle, end_idthandle;
852 extern unsigned char debughandle, end_debughandle;
855 struct realidt *idts = (struct realidt *) 0;
856 int codesize = &end_idthandle - &idthandle;
857 unsigned char *intbyte, *codeptr;
859 // for each int, we create a customized little handler
860 // that just pushes %ax, puts the int # in %al,
861 // then calls the common interrupt handler.
862 // this necessitated because intel didn't know much about
863 // architecture when they did the 8086 (it shows)
864 // (hmm do they know anymore even now :-)
865 // obviously you can see I don't really care about memory
866 // efficiency. If I did I would probe back through the stack
867 // and get it that way. But that's really disgusting.
868 for (i = 0; i < 256; i++) {
870 codeptr = (char*) 4096 + i * codesize;
871 idts[i].offset = (unsigned) codeptr;
872 memcpy(codeptr, &idthandle, codesize);
873 intbyte = codeptr + 3;
877 // fixed entry points
879 // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
881 // calling convention here is the same as INTs, we can reuse
882 // the int entry code.
883 codeptr = (char*) 0xff065;
884 memcpy(codeptr, &idthandle, codesize);
885 intbyte = codeptr + 3;
886 *intbyte = 0x42; /* int42 is the relocated int10 */
888 /* debug handler - useful to set a programmable delay between instructions if the
889 TF bit is set upon call to real mode */
891 idts[1].offset = 16384;
892 memcpy(16384, &debughandle, &end_debughandle - &debughandle);
911 /* ============================================================================================
912 * initializes vga bios during device initialization
914 static void vga_init(device_t dev)
916 struct drivers_pci_realmode_config *cfg = (struct drivers_pci_realmode_config *)dev->chip_info;
918 // code to make vga init go through the emulator - as of yet this does not workfor the epia-m
920 dev->rom_address = (void *)cfg->rom_address;
924 // code to make vga init run in real mode - does work but against the current Linuxbios philosophy
925 printk_debug("INSTALL REAL-MODE IDT\n");
926 setup_realmode_idt();
927 printk_debug("DO THE VGA BIOS\n");
930 vga_enable_console();
932 pci_write_config32(dev,0x30,0);
936 // device operations : on vga device init, call vga_init
937 static struct device_operations drivers_pci_realmode_dev_ops = {
939 .read_resources = pci_dev_read_resources,
940 .set_resources = pci_dev_set_resources,
941 .enable_resources = pci_dev_enable_resources,
946 /* ============================================================================================
947 * FIRST LOOP : traversing static.c list and enabling all chips found in there
948 * set given device operations descriptor to call vga_initialize
951 void vga_configure_pci(device_t dev){
952 printk_debug("Setting VGA callback\n");
953 dev->ops = &drivers_pci_realmode_dev_ops;
956 // PCI VGA device operations
957 struct chip_operations drivers_pci_realmode_ops = {
958 .enable_dev = vga_configure_pci,