more ifdef -> if fixes
[coreboot.git] / src / devices / oprom / x86.c
index 7df46283a93797dd5f46522b0916bdf5902e734d..37b45e6376a8e5b15784c9afcab20fee1b09a428 100644 (file)
@@ -34,28 +34,66 @@ struct realmode_idt {
 
 void x86_exception(struct eregs *info);
 
+/* From x86_asm.S */
 extern unsigned char __idt_handler, __idt_handler_size;
 extern unsigned char __realmode_code, __realmode_code_size;
-extern unsigned char __run_optionrom, __run_interrupt;
+extern unsigned char __realmode_call, __realmode_interrupt;
 
-void (*run_optionrom)(u32 devfn) __attribute__((regparm(0))) = (void *)&__run_optionrom;
-void (*vga_enable_console)(void) __attribute__((regparm(0))) = (void *)&__run_interrupt;
+void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
+               u32 esi, u32 edi) __attribute__((regparm(0))) = (void *)&__realmode_call;
+
+void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx, u32 edx, 
+               u32 esi, u32 edi) __attribute__((regparm(0))) = (void *)&__realmode_interrupt;
+
+#define FAKE_MEMORY_SIZE (1024*1024) // only 1MB
+#define INITIAL_EBDA_SEGMENT 0xF600
+#define INITIAL_EBDA_SIZE 0x400
+
+static void setup_bda(void)
+{
+       /* clear BIOS DATA AREA */
+       memset((void *)0x400, 0, 0x200);
+
+       write16(0x413, FAKE_MEMORY_SIZE / 1024);
+       write16(0x40e, INITIAL_EBDA_SEGMENT);
+
+       /* Set up EBDA */
+       memset((void *)(INITIAL_EBDA_SEGMENT << 4), 0, INITIAL_EBDA_SIZE);
+       write16((INITIAL_EBDA_SEGMENT << 4) + 0x0, INITIAL_EBDA_SIZE / 1024);
+}
+
+static void setup_rombios(void)
+{
+       const char date[] = "06/11/99";
+       memcpy((void *)0xffff5, &date, 8);
+
+       const char ident[] = "PCI_ISA";
+       memcpy((void *)0xfffd9, &ident, 7);
+
+       /* system model: IBM-AT */
+       write8(0xffffe, 0xfc);
+}
 
 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
 
 static int intXX_exception_handler(struct eregs *regs)
 {
-       printk(BIOS_INFO, "Oops, exception %d while executing option rom\n", 
+       printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
                        regs->vector);
-       x86_exception(regs);    // Call coreboot exception handler 
+#if 0
+       // Odd: The i945GM VGA oprom chokes on a pushl %eax and will
+       // die with an exception #6 if we run the coreboot exception 
+       // handler. Just continue, as it executes fine.
+       x86_exception(regs);    // Call coreboot exception handler
+#endif
 
        return 0;               // Never returns?
 }
 
 static int intXX_unknown_handler(struct eregs *regs)
 {
-       printk(BIOS_INFO, "Unsupported software interrupt #0x%x\n", 
-                       regs->vector);
+       printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
+                       regs->vector, regs->eax);
 
        return -1;
 }
@@ -66,6 +104,75 @@ void mainboard_interrupt_handlers(int intXX, void *intXX_func)
        intXX_handler[intXX] = intXX_func;
 }
 
+static int int10_handler(struct eregs *regs)
+{
+       int res=-1;
+       static u8 cursor_row=0, cursor_col=0;
+       switch((regs->eax & 0xff00)>>8) {
+       case 0x01: // Set cursor shape
+               res = 0;
+               break;
+       case 0x02: // Set cursor position
+               if (cursor_row != ((regs->edx >> 8) & 0xff) ||
+                   cursor_col >= (regs->edx & 0xff)) {
+                       printk(BIOS_INFO, "\n");
+               }
+               cursor_row = (regs->edx >> 8) & 0xff;
+               cursor_col = regs->edx & 0xff;
+               res = 0;
+               break;
+       case 0x03: // Get cursor position
+               regs->eax &= 0x00ff;
+               regs->ecx = 0x0607;
+               regs->edx = (cursor_row << 8) | cursor_col;
+               res = 0;
+               break;
+       case 0x06: // Scroll up
+               printk(BIOS_INFO, "\n");
+               res = 0;
+               break;
+       case 0x08: // Get Character and Mode at Cursor Position
+               regs->eax = 0x0f00 | 'A'; // White on black 'A'
+               res = 0;
+               break;
+       case 0x09: // Write Character and attribute
+       case 0x10: // Write Character
+               printk(BIOS_INFO, "%c", regs->eax & 0xff);
+               res = 0;
+               break;
+       case 0x0f: // Get video mode
+               regs->eax = 0x5002; //80x25
+               regs->ebx &= 0x00ff;
+               res = 0;
+               break;
+        default:
+               printk(BIOS_WARNING, "Unknown INT10 function %04x!\n",
+                               regs->eax & 0xffff);
+               break;
+       }
+       return res;
+}
+
+static int int16_handler(struct eregs *regs)
+{
+       int res=-1;
+       switch((regs->eax & 0xff00)>>8) {
+       case 0x00: // Check for Keystroke
+               regs->eax = 0x6120; // Space Bar, Space
+               res = 0;
+               break;
+       case 0x01: // Check for Keystroke
+               regs->eflags |= 1<<6; // Zero Flag set (no key available)
+               res = 0;
+               break;
+        default:
+               printk(BIOS_WARNING, "Unknown INT16 function %04x!\n",
+                               regs->eax & 0xffff);
+               break;
+       }
+       return res;
+}
+
 int int12_handler(struct eregs *regs);
 int int15_handler(struct eregs *regs);
 int int1a_handler(struct eregs *regs);
@@ -74,12 +181,12 @@ static void setup_interrupt_handlers(void)
 {
        int i;
 
-       /* The first 16 intXX functions are not BIOS services, 
+       /* The first 16 intXX functions are not BIOS services,
         * but the CPU-generated exceptions ("hardware interrupts")
         */
        for (i = 0; i < 0x10; i++)
                intXX_handler[i] = &intXX_exception_handler;
-       
+
        /* Mark all other intXX calls as unknown first */
        for (i = 0x10; i < 0x100; i++)
        {
@@ -93,12 +200,18 @@ static void setup_interrupt_handlers(void)
                         * interrupt handlers, such as the int15
                         */
                        switch (i) {
+                       case 0x10:
+                               intXX_handler[0x10] = &int10_handler;
+                               break;
                        case 0x12:
                                intXX_handler[0x12] = &int12_handler;
                                break;
                        case 0x15:
                                intXX_handler[0x15] = &int15_handler;
                                break;
+                       case 0x16:
+                               intXX_handler[0x16] = &int16_handler;
+                               break;
                        case 0x1a:
                                intXX_handler[0x1a] = &int1a_handler;
                                break;
@@ -133,23 +246,43 @@ static void setup_realmode_idt(void)
        }
 
        /* Many option ROMs use the hard coded interrupt entry points in the
-        * system bios. So install them at the known locations. 
-        * Only need int10 so far.
+        * system bios. So install them at the known locations.
         */
-       
+
        /* int42 is the relocated int10 */
-       write_idt_stub((void *)0xff065, 0x42); 
+       write_idt_stub((void *)0xff065, 0x42);
+       /* BIOS Int 11 Handler F000:F84D */
+       write_idt_stub((void *)0xff84d, 0x11);
+       /* BIOS Int 12 Handler F000:F841 */
+       write_idt_stub((void *)0xff841, 0x12);
+       /* BIOS Int 13 Handler F000:EC59 */
+       write_idt_stub((void *)0xfec59, 0x13);
+       /* BIOS Int 14 Handler F000:E739 */
+       write_idt_stub((void *)0xfe739, 0x14);
+       /* BIOS Int 15 Handler F000:F859 */
+       write_idt_stub((void *)0xff859, 0x15);
+       /* BIOS Int 16 Handler F000:E82E */
+       write_idt_stub((void *)0xfe82e, 0x16);
+       /* BIOS Int 17 Handler F000:EFD2 */
+       write_idt_stub((void *)0xfefd2, 0x17);
+       /* ROM BIOS Int 1A Handler F000:FE6E */
+       write_idt_stub((void *)0xffe6e, 0x1a);
 }
 
 void run_bios(struct device *dev, unsigned long addr)
 {
-       /* clear vga bios data area */
-       memset((void *)0x400, 0, 0x200);
+       u32 num_dev = (dev->bus->secondary << 8) | dev->path.pci.devfn;
+
+       /* Set up BIOS Data Area */
+       setup_bda();
+
+       /* Set up some legacy information in the F segment */
+       setup_rombios();
 
        /* Set up C interrupt handlers */
        setup_interrupt_handlers();
 
-       /* Setting up realmode IDT */
+       /* Set up real-mode IDT */
        setup_realmode_idt();
 
        memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
@@ -157,18 +290,17 @@ void run_bios(struct device *dev, unsigned long addr)
                        (u32)&__realmode_code_size);
 
        printk(BIOS_DEBUG, "Calling Option ROM...\n");
-       run_optionrom((dev->bus->secondary << 8) | dev->path.pci.devfn);
+       /* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
+       /* Option ROM entry point is at OPROM start + 3 */
+       realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
        printk(BIOS_DEBUG, "... Option ROM returned.\n");
 }
 
-#if defined(CONFIG_GEODE_VSA) && CONFIG_GEODE_VSA
+#if CONFIG_GEODE_VSA
 #include <cpu/amd/lxdef.h>
 #include <cpu/amd/vr.h>
 #include <cbfs.h>
 
-extern unsigned char __run_vsa;
-void (*run_vsa)(u32 smm, u32 sysmem) __attribute__((regparm(0))) = (void *)&__run_vsa;
-
 #define VSA2_BUFFER            0x60000
 #define VSA2_ENTRY_POINT       0x60020
 
@@ -185,7 +317,7 @@ static u32 VSA_vrRead(u16 classIndex)
                "outl   %%eax, %%dx\n"
                "addb   $2, %%dl\n"
                "inw    %%dx, %%ax\n"
-               : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx) 
+               : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
                : "a"(classIndex)
        );
 
@@ -196,9 +328,6 @@ void do_vsmbios(void)
 {
        printk(BIOS_DEBUG, "Preparing for VSA...\n");
 
-       /* clear bios data area */
-       memset((void *)0x400, 0, 0x200);
-
        /* Set up C interrupt handlers */
        setup_interrupt_handlers();
 
@@ -227,8 +356,11 @@ void do_vsmbios(void)
        }
 
        printk(BIOS_DEBUG, "Calling VSA module...\n");
+
        /* ECX gets SMM, EDX gets SYSMEM */
-       run_vsa(MSR_GLIU0_SMM, MSR_GLIU0_SYSMEM);
+       realmode_call(VSA2_ENTRY_POINT, 0x0, 0x0, MSR_GLIU0_SMM, 
+                       MSR_GLIU0_SYSMEM, 0x0, 0x0);
+
        printk(BIOS_DEBUG, "... VSA module returned.\n");
 
        /* Restart timer 1 */
@@ -243,7 +375,7 @@ void do_vsmbios(void)
 }
 #endif
 
-/* interrupt_handler() is called from assembler code only, 
+/* interrupt_handler() is called from assembler code only,
  * so there is no use in putting the prototype into a header file.
  */
 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
@@ -272,6 +404,7 @@ int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
        cs = cs_ip >> 16;
        flags = stackflags;
 
+#if CONFIG_REALMODE_DEBUG
        printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
        printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
                      eax, ebx, ecx, edx);
@@ -279,6 +412,7 @@ int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
                     ebp, esp, edi, esi);
        printk(BIOS_DEBUG, "oprom:  ip: %04x      cs: %04x   flags: %08x\n",
                     ip, cs, flags);
+#endif
 
        // Fetch arguments from the stack and put them into
        // a structure that we want to pass on to our sub interrupt
@@ -306,7 +440,7 @@ int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
        // will later pop them.
        // What happens here is that we force (volatile!) changing
        // the values of the parameters of this function. We do this
-       // because we know that they stay alive on the stack after 
+       // because we know that they stay alive on the stack after
        // we leave this function. Don't say this is bollocks.
        *(volatile u32 *)&eax = reg_info.eax;
        *(volatile u32 *)&ecx = reg_info.ecx;