Remove drivers/pci/onboard. The only purpose was for option ROMs, which are
[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 #include <cbfs.h>
11
12 /* vgabios.c. Derived from: */
13
14 /*------------------------------------------------------------ -*- C -*-
15  *  2 Kernel Monte a.k.a. Linux loading Linux on x86
16  *
17  *  Erik Arjan Hendriks <hendriks@lanl.gov>
18  *
19  *  This version is a derivative of the original two kernel monte
20  *  which is (C) 2000 Scyld.
21  *
22  *  Copyright (C) 2000 Scyld Computing Corporation
23  *
24  *  This program is free software; you can redistribute it and/or modify
25  *  it under the terms of the GNU General Public License as published by
26  *  the Free Software Foundation; either version 2 of the License, or
27  *  (at your option) any later version.
28  *
29  *  This program is distributed in the hope that it will be useful,
30  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
31  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  *  GNU General Public License for more details.
33  *
34  *  You should have received a copy of the GNU General Public License
35  *  along with this program; if not, write to the Free Software
36  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
37  *
38  * Portions related to the alpha architecture are:
39  *
40  *  Copyright(C) 2001 University of California.  LA-CC Number 01-67.
41  *  This software has been authored by an employee or employees of the
42  *  University of California, operator of the Los Alamos National
43  *  Laboratory under Contract No.  W-7405-ENG-36 with the U.S.
44  *  Department of Energy.  The U.S. Government has rights to use,
45  *  reproduce, and distribute this software. If the software is
46  *  modified to produce derivative works, such modified software should
47  *  be clearly marked, so as not to confuse it with the version
48  *  available from LANL.
49  *
50  *  This software may be used and distributed according to the terms
51  *  of the GNU General Public License, incorporated herein by
52  *  reference to http://www.gnu.org/licenses/gpl.html.
53  *
54  *  This software is provided by the author(s) "as is" and any express
55  *  or implied warranties, including, but not limited to, the implied
56  *  warranties of merchantability and fitness for a particular purpose
57  *  are disclaimed.  In no event shall the author(s) be liable for any
58  *  direct, indirect, incidental, special, exemplary, or consequential
59  *  damages (including, but not limited to, procurement of substitute
60  *  goods or services; loss of use, data, or profits; or business
61  *  interruption) however caused and on any theory of liability,
62  *  whether in contract, strict liability, or tort (including
63  *  negligence or otherwise) arising in any way out of the use of this
64  *  software, even if advised of the possibility of such damage.
65  *
66  *  $Id: vgabios.c,v 1.100 2006/09/12 15:31:49 indrek Exp $
67  *--------------------------------------------------------------------*/
68
69 /* Modified to be a self sufficient plug in so that it can be used 
70    without reliance on other parts of core coreboot 
71    (C) 2005 Nick.Barker9@btinternet.com
72
73   Used initially for epia-m where there are problems getting the bios
74   emulator to successfully run this bios.
75 */
76
77 /* Declare a temporary global descriptor table - necessary because the
78    Core part of the bios no longer sets up any 16 bit segments */
79 __asm__ (
80         /* pointer to original gdt */
81         "gdtarg:                        \n"
82         "       .word   gdt_limit       \n"
83         "       .long   gdt             \n"             
84
85         /* compute the table limit */
86         "__mygdt_limit = __mygdt_end - __mygdt - 1      \n"
87
88         "__mygdtaddr:                   \n"
89         "       .word   __mygdt_limit   \n"
90         "       .long   __mygdt         \n"
91
92         "__mygdt:                       \n"
93         /* selgdt 0, unused */
94         "       .word   0x0000, 0x0000  \n"
95         "       .byte   0x00, 0x00, 0x00, 0x00  \n"
96
97         /* selgdt 8, unused */
98         "       .word   0x0000, 0x0000          \n"
99         "       .byte   0x00, 0x00, 0x00, 0x00  \n"
100
101         /* selgdt 0x10, flat code segment */
102         "       .word   0xffff, 0x0000          \n"
103         "       .byte   0x00, 0x9b, 0xcf, 0x00  \n"     
104
105         /* selgdt 0x18, flat data segment */
106         "       .word   0xffff, 0x0000          \n"
107         "       .byte   0x00, 0x93, 0xcf, 0x00  \n"
108
109         /* selgdt 0x20, unused */
110         "       .word   0x0000, 0x0000          \n"
111         "       .byte   0x00, 0x00, 0x00, 0x00  \n"
112
113         /* selgdt 0x28 16-bit 64k code at 0x00000000 */
114         "       .word   0xffff, 0x0000          \n"
115         "       .byte   0, 0x9a, 0, 0           \n"
116
117         /* selgdt 0x30 16-bit 64k data at 0x00000000 */
118         "       .word   0xffff, 0x0000          \n"
119         "       .byte   0, 0x92, 0, 0           \n"
120
121         "__mygdt_end:                           \n"
122 );
123
124 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
125 __asm__ ("__myidt:              \n"
126          /* 16-bit limit */
127          "      .word 1023      \n"
128          /* 24-bit base */
129          "      .long 0         \n"
130          "      .word 0         \n"
131 );
132
133 /* The address arguments to this function are PHYSICAL ADDRESSES */ 
134 static void real_mode_switch_call_vga(unsigned long devfn)
135 {
136         __asm__ __volatile__ (
137                 // paranoia -- does ecx get saved? not sure. This is 
138                 // the easiest safe thing to do.
139                 "       pushal                  \n"
140                 /* save the stack */
141                 "       mov     %esp, __stack   \n"
142                 "       jmp     1f              \n"
143                 "__stack: .long 0               \n"
144                 "1:\n"
145                 /* get devfn into %ecx */
146                 "       movl    %esp, %ebp      \n"
147                 "       movl    8(%ebp), %ecx   \n"
148                 /* load 'our' gdt */
149                 "       lgdt    %cs:__mygdtaddr \n"
150
151                 /*  This configures CS properly for real mode. */
152                 "       ljmp    $0x28, $__rms_16bit\n"
153                 "__rms_16bit:                   \n"
154                 "       .code16                 \n"
155                 /* 16 bit code from here on... */
156
157                 /* Load the segment registers w/ properly configured segment
158                  * descriptors.  They will retain these configurations (limits,
159                  * writability, etc.) once protected mode is turned off. */
160                 "       mov     $0x30, %ax      \n"
161                 "       mov     %ax, %ds        \n"
162                 "       mov     %ax, %es        \n"
163                 "       mov     %ax, %fs        \n"
164                 "       mov     %ax, %gs        \n"
165                 "       mov     %ax, %ss        \n"
166
167                 /* Turn off protection (bit 0 in CR0) */
168                 "       movl    %cr0, %eax      \n"
169                 "       andl    $0xFFFFFFFE, %eax \n"
170                 "       movl    %eax, %cr0      \n"
171
172                 /* Now really going into real mode */
173                 "       ljmp    $0,  $__rms_real\n"
174                 "__rms_real:                    \n"
175
176                 /* put the stack at the end of page zero. 
177                  * that way we can easily share it between real and protected, 
178                  * since the 16-bit ESP at segment 0 will work for any case. */
179                 /* Setup a stack */
180                 "       mov     $0x0, %ax       \n"
181                 "       mov     %ax, %ss        \n"
182                 "       movl    $0x1000, %eax   \n"
183                 "       movl    %eax, %esp      \n"
184
185                 /* Load our 16 it idt */
186                 "       xor     %ax, %ax        \n"
187                 "       mov     %ax, %ds        \n"
188                 "       lidt    __myidt         \n"
189
190                 /* Dump zeros in the other segregs */
191                 "       mov     %ax, %es        \n"
192                 "       mov     %ax, %fs        \n"
193                 "       mov     %ax, %gs        \n"
194                 "       mov     $0x40, %ax      \n"
195                 "       mov     %ax, %ds        \n"
196                 "       mov     %cx, %ax        \n"
197
198                 /* run VGA BIOS at 0xc000:0003 */
199                 "       lcall   $0xc000, $0x0003\n"
200
201                 /* if we got here, just about done. 
202                  * Need to get back to protected mode */
203                 "       movl    %cr0, %eax      \n"
204                 "       orl     $0x0000001, %eax\n" /* PE = 1 */
205                 "       movl    %eax, %cr0      \n"
206
207                 /* Now that we are in protected mode jump to a 32 bit code segment. */
208                 "       data32  ljmp    $0x10, $vgarestart\n"
209                 "vgarestart:\n"
210                 "       .code32\n"
211                 "       movw    $0x18, %ax      \n"
212                 "       mov     %ax, %ds        \n"
213                 "       mov     %ax, %es        \n"
214                 "       mov     %ax, %fs        \n"
215                 "       mov     %ax, %gs        \n"
216                 "       mov     %ax, %ss        \n"
217
218                 /* restore proper gdt and idt */
219                 "       lgdt    %cs:gdtarg      \n"
220                 "       lidt    idtarg          \n"
221
222                 ".globl vga_exit                \n"
223                 "vga_exit:                      \n"
224                 "       mov     __stack, %esp   \n"
225                 "       popal                   \n"
226                 );
227 }
228
229 __asm__ (".text\n""real_mode_switch_end:\n");
230 extern char real_mode_switch_end[];
231
232 /* ============================================================================================
233  * run VGA ROM
234  * */
235
236 void do_vgabios(device_t dev)
237 {
238         
239         unsigned long busdevfn;
240         unsigned int rom = cbfs_load_optionrom(dev->vendor, dev->device, 0);
241         unsigned char *buf;
242         unsigned int size = 64*1024;
243         int i;
244         
245         /* clear vga bios data area */
246         for (i = 0x400; i < 0x500; i++) {
247                 *(unsigned char *) i = 0;
248         }
249
250         if (!dev) {
251                 printk_debug("NO VGA FOUND\n");
252                 return;
253         }
254         printk_debug("found VGA: vid=%x, did=%x\n", dev->vendor, dev->device);
255
256         /* declare rom address here - keep any config data out of the way
257          * of core LXB stuff */
258
259         //pci_write_config32(dev, PCI_ROM_ADDRESS, rom|1);
260         printk_debug("rom base, size: %x\n", rom);
261
262         buf = (unsigned char *) rom;
263         if ((buf[0] == 0x55) && (buf[1] == 0xaa)) {
264                 memcpy((void *) 0xc0000, buf, size);
265
266 //              write_protect_vgabios();  // in northbridge, not universal
267
268                 // check signature again
269                 buf = (unsigned char *) 0xc0000;
270                 if (buf[0]==0x55 && buf[1]==0xAA) {
271                         busdevfn = (dev->bus->secondary << 8) | dev->path.pci.devfn;
272                         printk_debug("bus/devfn = %#x\n", busdevfn);
273
274                         real_mode_switch_call_vga(busdevfn);
275                 } else
276                         printk_debug("Failed to copy VGA BIOS to 0xc0000\n");
277         } else 
278                 printk_debug("BAD SIGNATURE 0x%x 0x%x\n", buf[0], buf[1]);
279
280         pci_write_config32(dev, PCI_ROM_ADDRESS, 0);
281 }
282
283 /* ============================================================================================
284 /* call vga bios int 10 function 0x4f14 to enable main console 
285  * 
286  * vga bios does not always autosence the main console so forcing it on is good !!
287  * */ 
288 static void vga_enable_console(void)
289 {
290         __asm__ __volatile__ (
291                 /* paranoia -- does ecx get saved? not sure. This is 
292                  * the easiest safe thing to do. */
293                 "       pushal                  \n"
294                 /* save the stack */
295                 "       mov     %esp, __stack   \n"
296
297                 /* load 'our' gdt */
298                 "       lgdt    %cs:__mygdtaddr \n"
299
300                 /*  This configures CS properly for real mode. */
301                 "       ljmp    $0x28, $__vga_ec_16bit\n"
302                 "__vga_ec_16bit:                \n"
303                 "       .code16                 \n"
304                 /* 16 bit code from here on... */
305
306                 /* Load the segment registers w/ properly configured segment
307                  * descriptors.  They will retain these configurations (limits,
308                  * writability, etc.) once protected mode is turned off. */
309                 "       mov     $0x30, %ax      \n"
310                 "       mov     %ax, %ds        \n"
311                 "       mov     %ax, %es        \n"
312                 "       mov     %ax, %fs        \n"
313                 "       mov     %ax, %gs        \n"
314                 "       mov     %ax, %ss        \n"
315
316                 /* Turn off protection (bit 0 in CR0) */
317                 "       movl    %cr0, %eax      \n"
318                 "       andl    $0xFFFFFFFE, %eax\n"
319                 "       movl    %eax, %cr0      \n"
320
321                 /* Now really going into real mode */
322                 "       ljmp    $0, $__vga_ec_real \n"
323                 "__vga_ec_real:                  \n"
324
325                 /* put the stack at the end of page zero. 
326                  * that way we can easily share it between real and protected, 
327                  * since the 16-bit ESP at segment 0 will work for any case. */
328                 /* Setup a stack */
329                 "       mov     $0x0, %ax       \n"
330                 "       mov     %ax, %ss        \n"
331                 "       movl    $0x1000, %eax   \n"
332                 "       movl    %eax, %esp      \n"
333
334                 /* debugging for RGM */
335                 "       mov     $0x11, %al      \n"
336                 "       outb    %al, $0x80      \n"
337
338                 /* Load our 16 it idt */
339                 "       xor     %ax, %ax        \n"
340                 "       mov     %ax, %ds        \n"
341                 "       lidt    __myidt         \n"
342
343                 /* Dump zeros in the other segregs */
344                 "       mov     %ax, %ds        \n"
345                 "       mov     %ax, %es        \n"
346                 "       mov     %ax, %fs        \n"
347                 "       mov     %ax, %gs        \n"
348
349                 /* ask bios to enable main console */
350                 /* set up for int 10 call - values found from X server
351                  * bios call routines */
352                 "       movw    $0x4f14,%ax     \n"
353                 "       movw    $0x8003,%bx     \n"
354                 "       movw    $1, %cx         \n"
355                 "       movw    $0, %dx         \n"
356                 "       movw    $0, %di         \n"
357                 "       int     $0x10           \n"
358
359                 "       movb    $0x55, %al      \n"
360                 "       outb    %al, $0x80      \n"
361
362                 /* if we got here, just about done. 
363                  * Need to get back to protected mode */
364                 "       movl    %cr0, %eax      \n"
365                 "       orl     $0x0000001, %eax\n" /* PE = 1 */
366                 "       movl    %eax, %cr0      \n"
367
368                 /* Now that we are in protected mode jump to a 32 bit code segment. */
369                 "       data32  ljmp    $0x10, $vga_ec_restart\n"
370                 "vga_ec_restart:\n"
371                 "       .code32\n"
372                 "       movw    $0x18, %ax      \n"
373                 "       mov     %ax, %ds        \n"
374                 "       mov     %ax, %es        \n"
375                 "       mov     %ax, %fs        \n"
376                 "       mov     %ax, %gs        \n"
377                 "       mov     %ax, %ss        \n"
378
379                 /* restore proper gdt and idt */
380                 "       lgdt    %cs:gdtarg      \n"
381                 "       lidt    idtarg          \n"
382                 "       .globl  vga__ec_exit    \n"
383                 "vga_ec_exit:\n"
384                 "       mov     __stack, %esp   \n"
385                 "       popal\n"
386                 );
387 }
388
389
390
391
392
393 /* ============================================================================================
394  * real-mode interrupt handlers. 
395  * These are handled because VGA ROMs tend to call bios ints in real mode
396  * */
397
398
399 // we had hoped to avoid this. 
400 // this is a stub IDT only. It's main purpose is to ignore calls 
401 // to the BIOS. 
402 // no longer. Dammit. We have to respond to these.
403 struct realidt {
404         unsigned short offset, cs;
405 }; 
406
407 // from a handy writeup that andrey found.
408
409 // handler. 
410 // There are some assumptions we can make here. 
411 // First, the Top Of Stack (TOS) is located on the top of page zero. 
412 // we can share this stack between real and protected mode. 
413 // that simplifies a lot of things ...
414 // we'll just push all the registers on the stack as longwords, 
415 // and pop to protected mode. 
416 // second, since this only ever runs as part of coreboot, 
417 // we know all the segment register values -- so we don't save any.
418 // keep the handler that calls things small. It can do a call to 
419 // more complex code in coreboot itself. This helps a lot as we don't
420 // have to do address fixup in this little stub, and calls are absolute
421 // so the handler is relocatable.
422 void handler_vga(void)
423 {
424         __asm__ __volatile__ ( 
425                 "       .code16         \n"
426                 "idthandle:             \n"
427                 "       pushal          \n"
428                 "       movb    $0, %al \n"
429                 "       ljmp    $0, $callbiosint16\n"
430                 "end_idthandle:         \n"
431                 "       .code32         \n"
432                 );
433 }
434
435 void debughandler_vga(void)
436 {
437         __asm__ __volatile__ ( 
438                 "       .code16         \n"
439                 "debughandle:           \n"
440                 "       pushw   %cx     \n"
441                 "       movw    $250, %cx \n"
442                 "dbh1:                  \n"
443                 "       loop    dbh1    \n"
444                 "       popw    %cx     \n"
445                 "       iret            \n"
446                 "end_debughandle:       \n"
447                 ".code32                \n"
448                 );
449 }
450
451 // Calling conventions. The first C function is called with this stuff
452 // on the stack. They look like value parameters, but note that if you
453 // modify them they will go back to the INTx function modified. 
454 // the C function will call the biosint function with these as
455 // REFERENCE parameters. In this way, we can easily get 
456 // returns back to the INTx caller (i.e. vgabios)
457 void callbiosint_vga(void)
458 {
459         __asm__ __volatile__ (
460                 "       .code16         \n"
461                 "callbiosint16:         \n"
462                 "       push    %ds     \n"
463                 "       push    %es     \n"
464                 "       push    %fs     \n"
465                 "       push    %gs     \n"
466                 // clean up the int #. To save space we put it in the lower
467                 // byte. But the top 24 bits are junk. 
468                 "       andl    $0xff, %eax\n"
469                 // this push does two things:
470                 // - put the INT # on the stack as a parameter
471                 // - provides us with a temp for the %cr0 mods.
472                 "       pushl   %eax    \n"
473                 "       movl    %cr0, %eax\n"
474                 "       orl     $0x00000001, %eax\n" /* PE = 1 */
475                 "       movl    %eax, %cr0\n"
476                 /* Now that we are in protected mode jump to a 32 bit code segment. */
477                 "       data32  ljmp    $0x10, $biosprotect\n"
478                 "biosprotect:           \n"
479                 "       .code32         \n"
480                 "       movw    $0x18, %ax          \n"
481                 "       mov     %ax, %ds          \n"
482                 "       mov     %ax, %es          \n"
483                 "       mov     %ax, %fs          \n"
484                 "       mov     %ax, %gs          \n"
485                 "       mov     %ax, %ss          \n"
486                 "       lidt    idtarg         \n"
487                 "       call    biosint_vga     \n"
488                 // back to real mode ...
489                 "       ljmp    $0x28, $__rms_16bit2\n"
490                 "__rms_16bit2:                  \n"
491                 "       .code16                 \n"
492                 /* 16 bit code from here on... */
493                 /* Load the segment registers w/ properly configured segment
494                  * descriptors.  They will retain these configurations (limits,
495                  * writability, etc.) once protected mode is turned off. */
496                 "       mov     $0x30, %ax      \n"
497                 "       mov     %ax, %ds        \n"
498                 "       mov     %ax, %es        \n"
499                 "       mov     %ax, %fs        \n"
500                 "       mov     %ax, %gs        \n"
501                 "       mov     %ax, %ss        \n"
502                 
503                 /* Turn off protection (bit 0 in CR0) */
504                 "       movl    %cr0, %eax              \n"
505                 "       andl    $0xFFFFFFFE, %eax       \n"
506                 "       movl    %eax, %cr0              \n"
507
508                 /* Now really going into real mode */
509                 "       ljmp $0,  $__rms_real2  \n"
510                 "__rms_real2:                   \n"
511
512                 /* Setup a stack
513                  * FixME: where is esp? */
514                 "       mov     $0x0, %ax       \n"
515                 "       mov     %ax, %ss        \n"
516
517                 /* ebugging for RGM */
518                 "       mov     $0x11, %al      \n"
519                 "       outb    %al, $0x80      \n"
520
521                 /* Load our 16 it idt */
522                 "       xor     %ax, %ax        \n"
523                 "       mov     %ax, %ds        \n"
524                 "       lidt    __myidt         \n"
525
526                 /* Dump zeros in the other segregs */
527                 "       mov     %ax, %es        \n"
528                 "       mov     %ax, %fs        \n"
529                 "       mov     %ax, %gs        \n"
530                 "       mov     $0x40, %ax      \n"
531                 "       mov     %ax, %ds        \n"
532
533                 /* pop the INT # that you pushed earlier */
534                 "       popl    %eax            \n"
535                 "       pop     %gs             \n"
536                 "       pop     %fs             \n"
537                 "       pop     %es             \n"
538                 "       pop     %ds             \n"
539                 "       popal                   \n"
540                 "       iret                    \n"
541                 "       .code32                 \n"
542                 );
543 }
544
545
546
547
548
549 enum {
550         PCIBIOS = 0x1a, 
551         MEMSIZE = 0x12
552 };
553
554 int pcibios_vga(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
555             unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
556             unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
557
558 int handleint21_vga(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
559                 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
560                 unsigned long *pecx, unsigned long *peax, unsigned long *pflags
561         );
562
563 extern void vga_exit(void);
564
565 int biosint_vga(unsigned long intnumber,
566             unsigned long gsfs, unsigned long dses,
567             unsigned long edi, unsigned long esi,
568             unsigned long ebp, unsigned long esp, 
569             unsigned long ebx, unsigned long edx, 
570             unsigned long ecx, unsigned long eax, 
571             unsigned long cs_ip, unsigned short stackflags)
572 {
573         unsigned long ip; 
574         unsigned long cs; 
575         unsigned long flags;
576         int ret = -1;
577         
578         ip = cs_ip & 0xffff;
579         cs = cs_ip >> 16;
580         flags = stackflags;
581         
582         printk_debug("biosint: INT# 0x%lx\n", intnumber);
583         printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n", 
584                       eax, ebx, ecx, edx);
585         printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
586                      ebp, esp, edi, esi);
587         printk_debug("biosint:  ip 0x%x   cs 0x%x  flags 0x%x\n",
588                      ip, cs, flags);
589
590         // cases in a good compiler are just as good as your own tables. 
591         switch (intnumber) {
592         case 0 ... 15:
593                 // These are not BIOS service, but the CPU-generated exceptions
594                 printk_info("biosint: Oops, exception %u\n", intnumber);
595                 if (esp < 0x1000) {
596                         printk_debug("Stack contents: ");
597                         while (esp < 0x1000) {
598                                 printk_debug("0x%04x ", *(unsigned short *) esp);
599                                 esp += 2;
600                         }
601                         printk_debug("\n");
602                 }
603                 printk_debug("biosint: Bailing out\n");
604                 // "longjmp"
605                 vga_exit();
606                 break;
607                 
608         case PCIBIOS:
609                 ret = pcibios_vga( &edi, &esi, &ebp, &esp, 
610                                &ebx, &edx, &ecx, &eax, &flags);
611                 break;
612         case MEMSIZE: 
613                 // who cares. 
614                 eax = 64 * 1024;
615                 ret = 0;
616                 break;
617         case 0x15:
618                 ret=handleint21_vga( &edi, &esi, &ebp, &esp, 
619                                 &ebx, &edx, &ecx, &eax, &flags);
620                 break;
621         default:
622                 printk_info("BIOSINT: Unsupport int #0x%x\n", 
623                             intnumber);
624                 break;
625         }
626         if (ret)
627                 flags |= 1; // carry flags
628         else
629                 flags &= ~1;
630         stackflags = flags;
631         return ret;
632
633
634
635
636
637 enum {
638         CHECK = 0xb001,
639         FINDDEV = 0xb102,
640         READCONFBYTE = 0xb108,
641         READCONFWORD = 0xb109,
642         READCONFDWORD = 0xb10a,
643         WRITECONFBYTE = 0xb10b,
644         WRITECONFWORD = 0xb10c,
645         WRITECONFDWORD = 0xb10d
646 };
647
648 // errors go in AH. Just set these up so that word assigns
649 // will work. KISS. 
650 enum {
651         PCIBIOS_NODEV = 0x8600,
652         PCIBIOS_BADREG = 0x8700
653 };
654
655 int
656 pcibios_vga(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp, 
657         unsigned long *pesp, unsigned long *pebx, unsigned long *pedx, 
658         unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
659 {
660         unsigned long edi = *pedi;
661         unsigned long esi = *pesi;
662         unsigned long ebp = *pebp;
663         unsigned long esp = *pesp;
664         unsigned long ebx = *pebx;
665         unsigned long edx = *pedx;
666         unsigned long ecx = *pecx;
667         unsigned long eax = *peax;
668         unsigned long flags = *pflags;
669         unsigned short func = (unsigned short) eax;
670         int retval = 0;
671         unsigned short devid, vendorid, devfn;
672         short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
673         unsigned char bus;
674         device_t dev;
675         
676         switch(func) {
677         case  CHECK:
678                 *pedx = 0x4350;
679                 *pecx = 0x2049;
680                 retval = 0;
681                 break;
682         case FINDDEV:
683         {
684                 devid = *pecx;
685                 vendorid = *pedx;
686                 devindex = *pesi;
687                 dev = 0;
688                 while ((dev = dev_find_device(vendorid, devid, dev))) {
689                         if (devindex <= 0)
690                                 break;
691                         devindex--;
692                 }
693                 if (dev) {
694                         unsigned short busdevfn;
695                         *peax = 0;
696                         // busnum is an unsigned char;
697                         // devfn is an int, so we mask it off. 
698                         busdevfn = (dev->bus->secondary << 8)
699                                 | (dev->path.pci.devfn & 0xff);
700                         printk_debug("0x%x: return 0x%x\n", func, busdevfn);
701                         *pebx = busdevfn;
702                         retval = 0;
703                 } else {
704                         *peax = PCIBIOS_NODEV;
705                         retval = -1;
706                 }
707         }
708         break;
709         case READCONFDWORD:
710         case READCONFWORD:
711         case READCONFBYTE:
712         case WRITECONFDWORD:
713         case WRITECONFWORD:
714         case WRITECONFBYTE:
715         {
716                 unsigned long dword;
717                 unsigned short word;
718                 unsigned char byte;
719                 unsigned char reg;
720                 
721                 devfn = *pebx & 0xff;
722                 bus = *pebx >> 8;
723                 reg = *pedi;
724                 dev = dev_find_slot(bus, devfn);
725                 if (! dev) {
726                         printk_debug("0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
727                         // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
728                         *peax = PCIBIOS_BADREG;
729                         retval = -1;
730                 }
731                 switch(func) {
732                 case READCONFBYTE:
733                         byte = pci_read_config8(dev, reg);
734                         *pecx = byte;
735                         break;
736                 case READCONFWORD:
737                         word = pci_read_config16(dev, reg);
738                         *pecx = word;
739                         break;
740                 case READCONFDWORD:
741                         dword = pci_read_config32(dev, reg);
742                         *pecx = dword;
743                         break;
744                 case WRITECONFBYTE:
745                         byte = *pecx;
746                         pci_write_config8(dev, reg, byte);
747                         break;
748                 case WRITECONFWORD:
749                         word = *pecx;
750                         pci_write_config16(dev, reg, word);
751                         break;
752                 case WRITECONFDWORD:
753                         dword = *pecx;
754                         pci_write_config32(dev, reg, dword);
755                         break;
756                 }
757                 
758                 if (retval) 
759                         retval = PCIBIOS_BADREG;
760                 printk_debug("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
761                              func, bus, devfn, reg, *pecx);
762                 *peax = 0;
763                 retval = 0;
764         }
765         break;
766         default:
767                 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n",  func);
768                 break;
769         }
770         
771         return retval;
772
773
774 int handleint21_vga(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
775                 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
776                 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
777 {
778         int res=-1;
779         switch(*eax&0xffff)
780         {
781         case 0x5f19:
782                 break;
783         case 0x5f18:
784                 *eax=0x5f;
785                 *ebx=0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
786                 *ecx=0x060;
787                 res=0;
788                 break;
789         case 0x5f00:
790                 *eax = 0x8600;
791                 break;
792         case 0x5f01:
793                 *eax = 0x5f;
794                 *ecx = (*ecx & 0xffffff00 ) | 2; // panel type =  2 = 1024 * 768
795                 res = 0;
796                 break;
797         case 0x5f02:
798                 *eax=0x5f;
799                 *ebx= (*ebx & 0xffff0000) | 2;
800                 *ecx= (*ecx & 0xffff0000) | 0x401;  // PAL + crt only 
801                 *edx= (*edx & 0xffff0000) | 0;  // TV Layout - default
802                 res=0;
803                 break;
804         case 0x5f0f:
805                 *eax=0x860f;
806                 break;
807         }
808         return res;
809 }
810
811
812
813
814 /* ============================================================================================
815  * This function sets up bios and pci interrupt handlers for real mode
816  * This is needed when running VGA BIOS in real mode
817  * */
818  
819
820 void setup_realmode_idt_vga(void) 
821 {
822         extern unsigned char idthandle, end_idthandle;
823         extern unsigned char debughandle, end_debughandle;
824
825         int i;
826         struct realidt *idts = (struct realidt *) 0;
827         int codesize = &end_idthandle - &idthandle;
828         unsigned char *intbyte, *codeptr;
829         
830         // for each int, we create a customized little handler
831         // that just pushes %ax, puts the int # in %al, 
832         // then calls the common interrupt handler. 
833         // this necessitated because intel didn't know much about 
834         // architecture when they did the 8086 (it shows)
835         // (hmm do they know anymore even now :-)
836         // obviously you can see I don't really care about memory 
837         // efficiency. If I did I would probe back through the stack
838         // and get it that way. But that's really disgusting.
839         for (i = 0; i < 256; i++) {
840                 idts[i].cs = 0;
841                 codeptr = (char*) 4096 + i * codesize;
842                 idts[i].offset = (unsigned) codeptr;
843                 memcpy(codeptr, &idthandle, codesize);
844                 intbyte = codeptr + 3;
845                 *intbyte = i;
846         }
847         
848         // fixed entry points
849         
850         // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
851         // int10. 
852         // calling convention here is the same as INTs, we can reuse
853         // the int entry code.
854         codeptr = (char*) 0xff065;
855         memcpy(codeptr, &idthandle, codesize);
856         intbyte = codeptr + 3;
857         *intbyte = 0x42; /* int42 is the relocated int10 */
858
859         /* debug handler - useful to set a programmable delay between instructions if the
860            TF bit is set upon call to real mode */
861         idts[1].cs = 0;
862         idts[1].offset = 16384;
863         memcpy(16384, &debughandle, &end_debughandle - &debughandle);
864
865         
866 }
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882 /* ============================================================================================
883  *  initializes vga bios during device initialization
884  * */
885 static void vga_init(device_t dev)
886 {
887         struct drivers_pci_realmode_config *cfg = (struct drivers_pci_realmode_config *)dev->chip_info;
888
889         // code to make vga init go through the emulator - as of yet this does not workfor the epia-m
890         dev->on_mainboard=1;
891         pci_dev_init(dev);
892
893         // code to make vga init run in real mode - does work but against the current coreboot philosophy 
894     printk_debug("INSTALL REAL-MODE IDT\n");
895     setup_realmode_idt();
896     printk_debug("DO THE VGA BIOS\n");
897     do_vgabios(dev);
898
899     vga_enable_console();
900
901         pci_write_config32(dev,0x30,0);
902 }
903
904
905 // device operations : on vga device init, call vga_init
906 static struct device_operations drivers_pci_realmode_dev_ops = {
907         .init   = vga_init,
908         .read_resources   = pci_dev_read_resources,
909         .set_resources    = pci_dev_set_resources,
910                 .enable_resources = pci_dev_enable_resources,        
911                 .scan_bus         = 0,
912 };
913
914
915 /* ============================================================================================
916  * FIRST LOOP : traversing static.c list and enabling all chips found in there
917  * set given device operations descriptor to call vga_initialize 
918  * */
919
920 void vga_configure_pci(device_t dev){
921         printk_debug("Setting VGA callback\n");
922         dev->ops = &drivers_pci_realmode_dev_ops;
923 }
924
925 // PCI VGA device operations
926 struct chip_operations drivers_pci_realmode_ops = {
927         .enable_dev     = vga_configure_pci,
928 };