coreboot-v2: drop this ugly historic union name in v2 that was dropped in v3
[coreboot.git] / src / mainboard / artecgroup / dbe61 / realmode / vgabios.c
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>
6 #undef __KERNEL__
7 #include <arch/io.h>
8 #include <string.h>
9 #include "chip.h"
10
11 /* vgabios.c. Derived from: */
12
13 /*------------------------------------------------------------ -*- C -*-
14  *  2 Kernel Monte a.k.a. Linux loading Linux on x86
15  *
16  *  Erik Arjan Hendriks <hendriks@lanl.gov>
17  *
18  *  This version is a derivative of the original two kernel monte
19  *  which is (C) 2000 Scyld.
20  *
21  *  Copyright (C) 2000 Scyld Computing Corporation
22  *
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.
27  *
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.
32  *
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.
36  *
37  * Portions related to the alpha architecture are:
38  *
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.
48  *
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.
52  *
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.
64  *
65  *  $Id: vgabios.c,v 1.100 2006/09/12 15:31:49 indrek Exp $
66  *--------------------------------------------------------------------*/
67
68 /* Modified to be a self sufficient plug in so that it can be used 
69    without reliance on other parts of core coreboot 
70    (C) 2005 Nick.Barker9@btinternet.com
71
72   Used initially for epia-m where there are problems getting the bios
73   emulator to successfully run this bios.
74 */
75
76
77
78
79 /*
80   Modified to be an universal driver for loading VGA ROMs.
81   Aug 2006, anti.sullin@artecdesign.ee, Artec Design
82   
83   USAGE:
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
87         
88                 chip drivers/pci/realmode
89                                 device pci 1.1 on end                                   # VGA
90                                 register "rom_address" = "0xfffc0000"   # at the beginning of 256k
91                 end
92         
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.
96 */
97
98
99
100
101
102
103
104
105
106 /* Declare a temporary global descriptor table - necessary because the
107    Core part of the bios no longer sets up any 16 bit segments */
108 __asm__ (
109         /* pointer to original gdt */
110         "gdtarg:                        \n"
111         "       .word   gdt_limit       \n"
112         "       .long   gdt             \n"             
113
114         /* compute the table limit */
115         "__mygdt_limit = __mygdt_end - __mygdt - 1      \n"
116
117         "__mygdtaddr:                   \n"
118         "       .word   __mygdt_limit   \n"
119         "       .long   __mygdt         \n"
120
121         "__mygdt:                       \n"
122         /* selgdt 0, unused */
123         "       .word   0x0000, 0x0000  \n"
124         "       .byte   0x00, 0x00, 0x00, 0x00  \n"
125
126         /* selgdt 8, unused */
127         "       .word   0x0000, 0x0000          \n"
128         "       .byte   0x00, 0x00, 0x00, 0x00  \n"
129
130         /* selgdt 0x10, flat code segment */
131         "       .word   0xffff, 0x0000          \n"
132         "       .byte   0x00, 0x9b, 0xcf, 0x00  \n"     
133
134         /* selgdt 0x18, flat data segment */
135         "       .word   0xffff, 0x0000          \n"
136         "       .byte   0x00, 0x93, 0xcf, 0x00  \n"
137
138         /* selgdt 0x20, unused */
139         "       .word   0x0000, 0x0000          \n"
140         "       .byte   0x00, 0x00, 0x00, 0x00  \n"
141
142         /* selgdt 0x28 16-bit 64k code at 0x00000000 */
143         "       .word   0xffff, 0x0000          \n"
144         "       .byte   0, 0x9a, 0, 0           \n"
145
146         /* selgdt 0x30 16-bit 64k data at 0x00000000 */
147         "       .word   0xffff, 0x0000          \n"
148         "       .byte   0, 0x92, 0, 0           \n"
149
150         "__mygdt_end:                           \n"
151 );
152
153 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
154 __asm__ ("__myidt:              \n"
155          /* 16-bit limit */
156          "      .word 1023      \n"
157          /* 24-bit base */
158          "      .long 0         \n"
159          "      .word 0         \n"
160 );
161
162 /* The address arguments to this function are PHYSICAL ADDRESSES */ 
163 static void real_mode_switch_call_vga(unsigned long devfn)
164 {
165         __asm__ __volatile__ (
166                 // paranoia -- does ecx get saved? not sure. This is 
167                 // the easiest safe thing to do.
168                 "       pushal                  \n"
169                 /* save the stack */
170                 "       mov     %esp, __stack   \n"
171                 "       jmp     1f              \n"
172                 "__stack: .long 0               \n"
173                 "1:\n"
174                 /* get devfn into %ecx */
175                 "       movl    %esp, %ebp      \n"
176                 "       movl    8(%ebp), %ecx   \n"
177                 /* load 'our' gdt */
178                 "       lgdt    %cs:__mygdtaddr \n"
179
180                 /*  This configures CS properly for real mode. */
181                 "       ljmp    $0x28, $__rms_16bit\n"
182                 "__rms_16bit:                   \n"
183                 "       .code16                 \n"
184                 /* 16 bit code from here on... */
185
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. */
189                 "       mov     $0x30, %ax      \n"
190                 "       mov     %ax, %ds        \n"
191                 "       mov     %ax, %es        \n"
192                 "       mov     %ax, %fs        \n"
193                 "       mov     %ax, %gs        \n"
194                 "       mov     %ax, %ss        \n"
195
196                 /* Turn off protection (bit 0 in CR0) */
197                 "       movl    %cr0, %eax      \n"
198                 "       andl    $0xFFFFFFFE, %eax \n"
199                 "       movl    %eax, %cr0      \n"
200
201                 /* Now really going into real mode */
202                 "       ljmp    $0,  $__rms_real\n"
203                 "__rms_real:                    \n"
204
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. */
208                 /* Setup a stack */
209                 "       mov     $0x0, %ax       \n"
210                 "       mov     %ax, %ss        \n"
211                 "       movl    $0x1000, %eax   \n"
212                 "       movl    %eax, %esp      \n"
213
214                 /* Load our 16 it idt */
215                 "       xor     %ax, %ax        \n"
216                 "       mov     %ax, %ds        \n"
217                 "       lidt    __myidt         \n"
218
219                 /* Dump zeros in the other segregs */
220                 "       mov     %ax, %es        \n"
221                 "       mov     %ax, %fs        \n"
222                 "       mov     %ax, %gs        \n"
223                 "       mov     $0x40, %ax      \n"
224                 "       mov     %ax, %ds        \n"
225                 "       mov     %cx, %ax        \n"
226
227                 /* run VGA BIOS at 0xc000:0003 */
228                 "       lcall   $0xc000, $0x0003\n"
229
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"
235
236                 /* Now that we are in protected mode jump to a 32 bit code segment. */
237                 "       data32  ljmp    $0x10, $vgarestart\n"
238                 "vgarestart:\n"
239                 "       .code32\n"
240                 "       movw    $0x18, %ax      \n"
241                 "       mov     %ax, %ds        \n"
242                 "       mov     %ax, %es        \n"
243                 "       mov     %ax, %fs        \n"
244                 "       mov     %ax, %gs        \n"
245                 "       mov     %ax, %ss        \n"
246
247                 /* restore proper gdt and idt */
248                 "       lgdt    %cs:gdtarg      \n"
249                 "       lidt    idtarg          \n"
250
251                 ".globl vga_exit                \n"
252                 "vga_exit:                      \n"
253                 "       mov     __stack, %esp   \n"
254                 "       popal                   \n"
255                 );
256 }
257
258 __asm__ (".text\n""real_mode_switch_end:\n");
259 extern char real_mode_switch_end[];
260
261 /* ============================================================================================
262  * run VGA ROM
263  * */
264
265 void do_vgabios(device_t dev)
266 {
267         
268         unsigned long busdevfn;
269         unsigned int rom = dev->rom_address;
270         unsigned char *buf;
271         unsigned int size = 64*1024;
272         int i;
273         
274         /* clear vga bios data area */
275         for (i = 0x400; i < 0x500; i++) {
276                 *(unsigned char *) i = 0;
277         }
278
279         if (!dev) {
280                 printk_debug("NO VGA FOUND\n");
281                 return;
282         }
283         printk_debug("found VGA: vid=%x, did=%x\n", dev->vendor, dev->device);
284
285         /* declare rom address here - keep any config data out of the way
286          * of core LXB stuff */
287
288         //pci_write_config32(dev, PCI_ROM_ADDRESS, rom|1);
289         printk_debug("rom base, size: %x\n", rom);
290
291         buf = (unsigned char *) rom;
292         if ((buf[0] == 0x55) && (buf[1] == 0xaa)) {
293                 memcpy((void *) 0xc0000, buf, size);
294
295 //              write_protect_vgabios();  // in northbridge, not universal
296
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.pci.devfn;
301                         printk_debug("bus/devfn = %#x\n", busdevfn);
302
303                         real_mode_switch_call_vga(busdevfn);
304                 } else
305                         printk_debug("Failed to copy VGA BIOS to 0xc0000\n");
306         } else 
307                 printk_debug("BAD SIGNATURE 0x%x 0x%x\n", buf[0], buf[1]);
308
309         pci_write_config32(dev, PCI_ROM_ADDRESS, 0);
310 }
311
312 /* ============================================================================================
313 /* call vga bios int 10 function 0x4f14 to enable main console 
314  * 
315  * vga bios does not always autosence the main console so forcing it on is good !!
316  * */ 
317 static void vga_enable_console(void)
318 {
319         __asm__ __volatile__ (
320                 /* paranoia -- does ecx get saved? not sure. This is 
321                  * the easiest safe thing to do. */
322                 "       pushal                  \n"
323                 /* save the stack */
324                 "       mov     %esp, __stack   \n"
325
326                 /* load 'our' gdt */
327                 "       lgdt    %cs:__mygdtaddr \n"
328
329                 /*  This configures CS properly for real mode. */
330                 "       ljmp    $0x28, $__vga_ec_16bit\n"
331                 "__vga_ec_16bit:                \n"
332                 "       .code16                 \n"
333                 /* 16 bit code from here on... */
334
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. */
338                 "       mov     $0x30, %ax      \n"
339                 "       mov     %ax, %ds        \n"
340                 "       mov     %ax, %es        \n"
341                 "       mov     %ax, %fs        \n"
342                 "       mov     %ax, %gs        \n"
343                 "       mov     %ax, %ss        \n"
344
345                 /* Turn off protection (bit 0 in CR0) */
346                 "       movl    %cr0, %eax      \n"
347                 "       andl    $0xFFFFFFFE, %eax\n"
348                 "       movl    %eax, %cr0      \n"
349
350                 /* Now really going into real mode */
351                 "       ljmp    $0, $__vga_ec_real \n"
352                 "__vga_ec_real:                  \n"
353
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. */
357                 /* Setup a stack */
358                 "       mov     $0x0, %ax       \n"
359                 "       mov     %ax, %ss        \n"
360                 "       movl    $0x1000, %eax   \n"
361                 "       movl    %eax, %esp      \n"
362
363                 /* debugging for RGM */
364                 "       mov     $0x11, %al      \n"
365                 "       outb    %al, $0x80      \n"
366
367                 /* Load our 16 it idt */
368                 "       xor     %ax, %ax        \n"
369                 "       mov     %ax, %ds        \n"
370                 "       lidt    __myidt         \n"
371
372                 /* Dump zeros in the other segregs */
373                 "       mov     %ax, %ds        \n"
374                 "       mov     %ax, %es        \n"
375                 "       mov     %ax, %fs        \n"
376                 "       mov     %ax, %gs        \n"
377
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"
383                 "       movw    $1, %cx         \n"
384                 "       movw    $0, %dx         \n"
385                 "       movw    $0, %di         \n"
386                 "       int     $0x10           \n"
387
388                 "       movb    $0x55, %al      \n"
389                 "       outb    %al, $0x80      \n"
390
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"
396
397                 /* Now that we are in protected mode jump to a 32 bit code segment. */
398                 "       data32  ljmp    $0x10, $vga_ec_restart\n"
399                 "vga_ec_restart:\n"
400                 "       .code32\n"
401                 "       movw    $0x18, %ax      \n"
402                 "       mov     %ax, %ds        \n"
403                 "       mov     %ax, %es        \n"
404                 "       mov     %ax, %fs        \n"
405                 "       mov     %ax, %gs        \n"
406                 "       mov     %ax, %ss        \n"
407
408                 /* restore proper gdt and idt */
409                 "       lgdt    %cs:gdtarg      \n"
410                 "       lidt    idtarg          \n"
411                 "       .globl  vga__ec_exit    \n"
412                 "vga_ec_exit:\n"
413                 "       mov     __stack, %esp   \n"
414                 "       popal\n"
415                 );
416 }
417
418
419
420
421
422 /* ============================================================================================
423  * real-mode interrupt handlers. 
424  * These are handled because VGA ROMs tend to call bios ints in real mode
425  * */
426
427
428 // we had hoped to avoid this. 
429 // this is a stub IDT only. It's main purpose is to ignore calls 
430 // to the BIOS. 
431 // no longer. Dammit. We have to respond to these.
432 struct realidt {
433         unsigned short offset, cs;
434 }; 
435
436 // from a handy writeup that andrey found.
437
438 // handler. 
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 coreboot, 
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 coreboot 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)
452 {
453         __asm__ __volatile__ ( 
454                 "       .code16         \n"
455                 "idthandle:             \n"
456                 "       pushal          \n"
457                 "       movb    $0, %al \n"
458                 "       ljmp    $0, $callbiosint16\n"
459                 "end_idthandle:         \n"
460                 "       .code32         \n"
461                 );
462 }
463
464 void debughandler_vga(void)
465 {
466         __asm__ __volatile__ ( 
467                 "       .code16         \n"
468                 "debughandle:           \n"
469                 "       pushw   %cx     \n"
470                 "       movw    $250, %cx \n"
471                 "dbh1:                  \n"
472                 "       loop    dbh1    \n"
473                 "       popw    %cx     \n"
474                 "       iret            \n"
475                 "end_debughandle:       \n"
476                 ".code32                \n"
477                 );
478 }
479
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)
487 {
488         __asm__ __volatile__ (
489                 "       .code16         \n"
490                 "callbiosint16:         \n"
491                 "       push    %ds     \n"
492                 "       push    %es     \n"
493                 "       push    %fs     \n"
494                 "       push    %gs     \n"
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.
501                 "       pushl   %eax    \n"
502                 "       movl    %cr0, %eax\n"
503                 "       orl     $0x00000001, %eax\n" /* PE = 1 */
504                 "       movl    %eax, %cr0\n"
505                 /* Now that we are in protected mode jump to a 32 bit code segment. */
506                 "       data32  ljmp    $0x10, $biosprotect\n"
507                 "biosprotect:           \n"
508                 "       .code32         \n"
509                 "       movw    $0x18, %ax          \n"
510                 "       mov     %ax, %ds          \n"
511                 "       mov     %ax, %es          \n"
512                 "       mov     %ax, %fs          \n"
513                 "       mov     %ax, %gs          \n"
514                 "       mov     %ax, %ss          \n"
515                 "       lidt    idtarg         \n"
516                 "       call    biosint_vga     \n"
517                 // back to real mode ...
518                 "       ljmp    $0x28, $__rms_16bit2\n"
519                 "__rms_16bit2:                  \n"
520                 "       .code16                 \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. */
525                 "       mov     $0x30, %ax      \n"
526                 "       mov     %ax, %ds        \n"
527                 "       mov     %ax, %es        \n"
528                 "       mov     %ax, %fs        \n"
529                 "       mov     %ax, %gs        \n"
530                 "       mov     %ax, %ss        \n"
531                 
532                 /* Turn off protection (bit 0 in CR0) */
533                 "       movl    %cr0, %eax              \n"
534                 "       andl    $0xFFFFFFFE, %eax       \n"
535                 "       movl    %eax, %cr0              \n"
536
537                 /* Now really going into real mode */
538                 "       ljmp $0,  $__rms_real2  \n"
539                 "__rms_real2:                   \n"
540
541                 /* Setup a stack
542                  * FixME: where is esp? */
543                 "       mov     $0x0, %ax       \n"
544                 "       mov     %ax, %ss        \n"
545
546                 /* ebugging for RGM */
547                 "       mov     $0x11, %al      \n"
548                 "       outb    %al, $0x80      \n"
549
550                 /* Load our 16 it idt */
551                 "       xor     %ax, %ax        \n"
552                 "       mov     %ax, %ds        \n"
553                 "       lidt    __myidt         \n"
554
555                 /* Dump zeros in the other segregs */
556                 "       mov     %ax, %es        \n"
557                 "       mov     %ax, %fs        \n"
558                 "       mov     %ax, %gs        \n"
559                 "       mov     $0x40, %ax      \n"
560                 "       mov     %ax, %ds        \n"
561
562                 /* pop the INT # that you pushed earlier */
563                 "       popl    %eax            \n"
564                 "       pop     %gs             \n"
565                 "       pop     %fs             \n"
566                 "       pop     %es             \n"
567                 "       pop     %ds             \n"
568                 "       popal                   \n"
569                 "       iret                    \n"
570                 "       .code32                 \n"
571                 );
572 }
573
574
575
576
577
578 enum {
579         PCIBIOS = 0x1a, 
580         MEMSIZE = 0x12
581 };
582
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);
586
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
590         );
591
592 extern void vga_exit(void);
593
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)
601 {
602         unsigned long ip; 
603         unsigned long cs; 
604         unsigned long flags;
605         int ret = -1;
606         
607         ip = cs_ip & 0xffff;
608         cs = cs_ip >> 16;
609         flags = stackflags;
610         
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", 
613                       eax, ebx, ecx, edx);
614         printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
615                      ebp, esp, edi, esi);
616         printk_debug("biosint:  ip 0x%x   cs 0x%x  flags 0x%x\n",
617                      ip, cs, flags);
618
619         // cases in a good compiler are just as good as your own tables. 
620         switch (intnumber) {
621         case 0 ... 15:
622                 // These are not BIOS service, but the CPU-generated exceptions
623                 printk_info("biosint: Oops, exception %u\n", intnumber);
624                 if (esp < 0x1000) {
625                         printk_debug("Stack contents: ");
626                         while (esp < 0x1000) {
627                                 printk_debug("0x%04x ", *(unsigned short *) esp);
628                                 esp += 2;
629                         }
630                         printk_debug("\n");
631                 }
632                 printk_debug("biosint: Bailing out\n");
633                 // "longjmp"
634                 vga_exit();
635                 break;
636                 
637         case PCIBIOS:
638                 ret = pcibios_vga( &edi, &esi, &ebp, &esp, 
639                                &ebx, &edx, &ecx, &eax, &flags);
640                 break;
641         case MEMSIZE: 
642                 // who cares. 
643                 eax = 64 * 1024;
644                 ret = 0;
645                 break;
646         case 0x15:
647                 ret=handleint21_vga( &edi, &esi, &ebp, &esp, 
648                                 &ebx, &edx, &ecx, &eax, &flags);
649                 break;
650         default:
651                 printk_info("BIOSINT: Unsupport int #0x%x\n", 
652                             intnumber);
653                 break;
654         }
655         if (ret)
656                 flags |= 1; // carry flags
657         else
658                 flags &= ~1;
659         stackflags = flags;
660         return ret;
661
662
663
664
665
666 enum {
667         CHECK = 0xb001,
668         FINDDEV = 0xb102,
669         READCONFBYTE = 0xb108,
670         READCONFWORD = 0xb109,
671         READCONFDWORD = 0xb10a,
672         WRITECONFBYTE = 0xb10b,
673         WRITECONFWORD = 0xb10c,
674         WRITECONFDWORD = 0xb10d
675 };
676
677 // errors go in AH. Just set these up so that word assigns
678 // will work. KISS. 
679 enum {
680         PCIBIOS_NODEV = 0x8600,
681         PCIBIOS_BADREG = 0x8700
682 };
683
684 int
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)
688 {
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;
699         int retval = 0;
700         unsigned short devid, vendorid, devfn;
701         short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
702         unsigned char bus;
703         device_t dev;
704         
705         switch(func) {
706         case  CHECK:
707                 *pedx = 0x4350;
708                 *pecx = 0x2049;
709                 retval = 0;
710                 break;
711         case FINDDEV:
712         {
713                 devid = *pecx;
714                 vendorid = *pedx;
715                 devindex = *pesi;
716                 dev = 0;
717                 while ((dev = dev_find_device(vendorid, devid, dev))) {
718                         if (devindex <= 0)
719                                 break;
720                         devindex--;
721                 }
722                 if (dev) {
723                         unsigned short busdevfn;
724                         *peax = 0;
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.pci.devfn & 0xff);
729                         printk_debug("0x%x: return 0x%x\n", func, busdevfn);
730                         *pebx = busdevfn;
731                         retval = 0;
732                 } else {
733                         *peax = PCIBIOS_NODEV;
734                         retval = -1;
735                 }
736         }
737         break;
738         case READCONFDWORD:
739         case READCONFWORD:
740         case READCONFBYTE:
741         case WRITECONFDWORD:
742         case WRITECONFWORD:
743         case WRITECONFBYTE:
744         {
745                 unsigned long dword;
746                 unsigned short word;
747                 unsigned char byte;
748                 unsigned char reg;
749                 
750                 devfn = *pebx & 0xff;
751                 bus = *pebx >> 8;
752                 reg = *pedi;
753                 dev = dev_find_slot(bus, devfn);
754                 if (! dev) {
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;
758                         retval = -1;
759                 }
760                 switch(func) {
761                 case READCONFBYTE:
762                         byte = pci_read_config8(dev, reg);
763                         *pecx = byte;
764                         break;
765                 case READCONFWORD:
766                         word = pci_read_config16(dev, reg);
767                         *pecx = word;
768                         break;
769                 case READCONFDWORD:
770                         dword = pci_read_config32(dev, reg);
771                         *pecx = dword;
772                         break;
773                 case WRITECONFBYTE:
774                         byte = *pecx;
775                         pci_write_config8(dev, reg, byte);
776                         break;
777                 case WRITECONFWORD:
778                         word = *pecx;
779                         pci_write_config16(dev, reg, word);
780                         break;
781                 case WRITECONFDWORD:
782                         dword = *pecx;
783                         pci_write_config32(dev, reg, dword);
784                         break;
785                 }
786                 
787                 if (retval) 
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);
791                 *peax = 0;
792                 retval = 0;
793         }
794         break;
795         default:
796                 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n",  func);
797                 break;
798         }
799         
800         return retval;
801
802
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)
806 {
807         int res=-1;
808         switch(*eax&0xffff)
809         {
810         case 0x5f19:
811                 break;
812         case 0x5f18:
813                 *eax=0x5f;
814                 *ebx=0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
815                 *ecx=0x060;
816                 res=0;
817                 break;
818         case 0x5f00:
819                 *eax = 0x8600;
820                 break;
821         case 0x5f01:
822                 *eax = 0x5f;
823                 *ecx = (*ecx & 0xffffff00 ) | 2; // panel type =  2 = 1024 * 768
824                 res = 0;
825                 break;
826         case 0x5f02:
827                 *eax=0x5f;
828                 *ebx= (*ebx & 0xffff0000) | 2;
829                 *ecx= (*ecx & 0xffff0000) | 0x401;  // PAL + crt only 
830                 *edx= (*edx & 0xffff0000) | 0;  // TV Layout - default
831                 res=0;
832                 break;
833         case 0x5f0f:
834                 *eax=0x860f;
835                 break;
836         }
837         return res;
838 }
839
840
841
842
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
846  * */
847  
848
849 void setup_realmode_idt_vga(void) 
850 {
851         extern unsigned char idthandle, end_idthandle;
852         extern unsigned char debughandle, end_debughandle;
853
854         int i;
855         struct realidt *idts = (struct realidt *) 0;
856         int codesize = &end_idthandle - &idthandle;
857         unsigned char *intbyte, *codeptr;
858         
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++) {
869                 idts[i].cs = 0;
870                 codeptr = (char*) 4096 + i * codesize;
871                 idts[i].offset = (unsigned) codeptr;
872                 memcpy(codeptr, &idthandle, codesize);
873                 intbyte = codeptr + 3;
874                 *intbyte = i;
875         }
876         
877         // fixed entry points
878         
879         // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
880         // int10. 
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 */
887
888         /* debug handler - useful to set a programmable delay between instructions if the
889            TF bit is set upon call to real mode */
890         idts[1].cs = 0;
891         idts[1].offset = 16384;
892         memcpy(16384, &debughandle, &end_debughandle - &debughandle);
893
894         
895 }
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911 /* ============================================================================================
912  *  initializes vga bios during device initialization
913  * */
914 static void vga_init(device_t dev)
915 {
916         struct drivers_pci_realmode_config *cfg = (struct drivers_pci_realmode_config *)dev->chip_info;
917
918         // code to make vga init go through the emulator - as of yet this does not workfor the epia-m
919         dev->on_mainboard=1;
920         dev->rom_address = (void *)cfg->rom_address;
921
922         pci_dev_init(dev);
923
924         // code to make vga init run in real mode - does work but against the current coreboot philosophy 
925     printk_debug("INSTALL REAL-MODE IDT\n");
926     setup_realmode_idt();
927     printk_debug("DO THE VGA BIOS\n");
928     do_vgabios(dev);
929
930     vga_enable_console();
931
932         pci_write_config32(dev,0x30,0);
933 }
934
935
936 // device operations : on vga device init, call vga_init
937 static struct device_operations drivers_pci_realmode_dev_ops = {
938         .init   = vga_init,
939         .read_resources   = pci_dev_read_resources,
940         .set_resources    = pci_dev_set_resources,
941                 .enable_resources = pci_dev_enable_resources,        
942                 .scan_bus         = 0,
943 };
944
945
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 
949  * */
950
951 void vga_configure_pci(device_t dev){
952         printk_debug("Setting VGA callback\n");
953         dev->ops = &drivers_pci_realmode_dev_ops;
954 }
955
956 // PCI VGA device operations
957 struct chip_operations drivers_pci_realmode_ops = {
958         .enable_dev     = vga_configure_pci,
959 };