1 #include <console/console.h>
2 #include <device/pci.h>
3 #include <device/pci_ids.h>
4 #include <device/pci_ops.h>
8 #include <cpu/amd/gx2def.h>
10 /* what a mess this uncompress thing is. I am not at all happy about how this
11 * was done, but can't fix it yet. RGM
13 #warning "Fix the uncompress once coreboot knows how to do it"
14 #include "../lib/nrv2b.c"
16 /* vsmsetup.c derived from vgabios.c. Derived from: */
18 /*------------------------------------------------------------ -*- C -*-
19 * 2 Kernel Monte a.k.a. Linux loading Linux on x86
21 * Erik Arjan Hendriks <hendriks@lanl.gov>
23 * This version is a derivative of the original two kernel monte
24 * which is (C) 2000 Scyld.
26 * Copyright (C) 2000 Scyld Computing Corporation
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 * Portions related to the alpha architecture are:
44 * Copyright(C) 2001 University of California. LA-CC Number 01-67.
45 * This software has been authored by an employee or employees of the
46 * University of California, operator of the Los Alamos National
47 * Laboratory under Contract No. W-7405-ENG-36 with the U.S.
48 * Department of Energy. The U.S. Government has rights to use,
49 * reproduce, and distribute this software. If the software is
50 * modified to produce derivative works, such modified software should
51 * be clearly marked, so as not to confuse it with the version
52 * available from LANL.
54 * This software may be used and distributed according to the terms
55 * of the GNU General Public License, incorporated herein by
56 * reference to http://www.gnu.org/licenses/gpl.html.
58 * This software is provided by the author(s) "as is" and any express
59 * or implied warranties, including, but not limited to, the implied
60 * warranties of merchantability and fitness for a particular purpose
61 * are disclaimed. In no event shall the author(s) be liable for any
62 * direct, indirect, incidental, special, exemplary, or consequential
63 * damages (including, but not limited to, procurement of substitute
64 * goods or services; loss of use, data, or profits; or business
65 * interruption) however caused and on any theory of liability,
66 * whether in contract, strict liability, or tort (including
67 * negligence or otherwise) arising in any way out of the use of this
68 * software, even if advised of the possibility of such damage.
71 *--------------------------------------------------------------------*/
73 /* Modified to be a self sufficient plug in so that it can be used
74 without reliance on other parts of core coreboot
75 (C) 2005 Nick.Barker9@btinternet.com
77 Used initially for epia-m where there are problems getting the bios
78 emulator to successfully run this bios.
81 /* Declare a temporary global descriptor table - necessary because the
82 Core part of the bios no longer sets up any 16 bit segments */
84 /* pointer to original gdt */
89 /* compute the table limit */
90 "__mygdt_limit = __mygdt_end - __mygdt - 1 \n"
93 " .word __mygdt_limit \n"
97 /* selgdt 0, unused */
98 " .word 0x0000, 0x0000 \n"
99 " .byte 0x00, 0x00, 0x00, 0x00 \n"
101 /* selgdt 8, unused */
102 " .word 0x0000, 0x0000 \n"
103 " .byte 0x00, 0x00, 0x00, 0x00 \n"
105 /* selgdt 0x10, flat code segment */
106 " .word 0xffff, 0x0000 \n"
107 " .byte 0x00, 0x9b, 0xcf, 0x00 \n"
109 /* selgdt 0x18, flat data segment */
110 " .word 0xffff, 0x0000 \n"
111 " .byte 0x00, 0x93, 0xcf, 0x00 \n"
113 /* selgdt 0x20, unused */
114 " .word 0x0000, 0x0000 \n"
115 " .byte 0x00, 0x00, 0x00, 0x00 \n"
117 /* selgdt 0x28 16-bit 64k code at 0x00000000 */
118 " .word 0xffff, 0x0000 \n"
119 " .byte 0, 0x9a, 0, 0 \n"
121 /* selgdt 0x30 16-bit 64k data at 0x00000000 */
122 " .word 0xffff, 0x0000 \n"
123 " .byte 0, 0x92, 0, 0 \n"
128 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
129 __asm__ ("__myidt: \n"
137 /* The address arguments to this function are PHYSICAL ADDRESSES */
138 static void real_mode_switch_call_vsm(unsigned long smm, unsigned long sysm)
140 __asm__ __volatile__ (
141 // paranoia -- does ecx get saved? not sure. This is
142 // the easiest safe thing to do.
145 " mov %esp, __stack \n"
147 "__stack: .long 0 \n"
149 /* get devfn into %ecx */
150 " movl %esp, %ebp \n"
152 /* I'm not happy about that pushal followed by esp-relative references.
153 * just do hard-codes for now
155 " movl 8(%ebp), %ecx \n"
156 " movl 12(%ebp), %edx \n"
158 " movl $0x10000026, %ecx \n"
159 " movl $0x10000028, %edx \n"
162 " lgdt %cs:__mygdtaddr \n"
164 /* This configures CS properly for real mode. */
165 " ljmp $0x28, $__rms_16bit\n"
168 /* 16 bit code from here on... */
170 /* Load the segment registers w/ properly configured segment
171 * descriptors. They will retain these configurations (limits,
172 * writability, etc.) once protected mode is turned off. */
180 /* Turn off protection (bit 0 in CR0) */
181 " movl %cr0, %eax \n"
182 " andl $0xFFFFFFFE, %eax \n"
183 " movl %eax, %cr0 \n"
185 /* Now really going into real mode */
186 " ljmp $0, $__rms_real\n"
189 /* put the stack at the end of page zero.
190 * that way we can easily share it between real and protected,
191 * since the 16-bit ESP at segment 0 will work for any case. */
195 " movl $0x1000, %eax \n"
196 " movl %eax, %esp \n"
198 /* Load our 16 it idt */
203 /* Dump zeros in the other segregs */
205 /* FixMe: Big real mode for gs, fs? */
211 " movl $0x10000026, %ecx \n"
212 " movl $0x10000028, %edx \n"
214 /* run VGA BIOS at 0x6000:0020 */
215 " lcall $0x6000, $0x0020\n"
217 /* if we got here, just about done.
218 * Need to get back to protected mode */
219 " movl %cr0, %eax \n"
220 " orl $0x0000001, %eax\n" /* PE = 1 */
221 " movl %eax, %cr0 \n"
223 /* Now that we are in protected mode jump to a 32 bit code segment. */
224 " data32 ljmp $0x10, $vsmrestart\n"
227 " movw $0x18, %ax \n"
234 /* restore proper gdt and idt */
235 " lgdt %cs:gdtarg \n"
240 " mov __stack, %esp \n"
245 __asm__ (".text\n""real_mode_switch_end:\n");
246 extern char real_mode_switch_end[];
249 void do_vsmbios(void)
252 unsigned long busdevfn;
253 unsigned int rom = 0;
255 unsigned int size = SMM_SIZE*1024;
257 unsigned long ilen, olen;
259 printk_err("do_vsmbios\n");
260 /* clear vsm bios data area */
261 for (i = 0x400; i < 0x500; i++) {
262 *(unsigned char *) i = 0;
265 /* declare rom address here - keep any config data out of the way
266 * of core LXB stuff */
268 /* this is the base of rom on the GX2 at present. At some point, this has to be
269 * much better parameterized
273 /* the VSA starts at the base of rom - 64 */
274 rom = ((unsigned long) 0) - (ROM_SIZE + 64*1024);
276 buf = (unsigned char *) 0x60000;
277 olen = unrv2b((uint8_t *)rom, buf, &ilen);
278 printk_debug("buf ilen %d olen%d\n", ilen, olen);
279 printk_debug("buf %p *buf %d buf[256k] %d\n",
280 buf, buf[0], buf[SMM_SIZE*1024]);
281 printk_debug("buf[0x20] signature is %x:%x:%x:%x\n",
282 buf[0x20] ,buf[0x21] ,buf[0x22],buf[0x23]);
283 /* check for post code at start of vsainit.bin. If you don't see it,
285 if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
286 (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
287 printk_err("do_vsmbios: no vsainit.bin signature, skipping!\n");
291 //memcpy((void *) 0x60000, buf, size);
293 //for (i = 0; i < 0x800000; i++)
296 /* ecx gets smm, edx gets sysm */
297 printk_err("Call real_mode_switch_call_vsm\n");
298 real_mode_switch_call_vsm(0x10000026, 0x10000028);
300 /* restart timer 1 */
306 // we had hoped to avoid this.
307 // this is a stub IDT only. It's main purpose is to ignore calls
309 // no longer. Dammit. We have to respond to these.
311 unsigned short offset, cs;
314 // from a handy writeup that andrey found.
317 // There are some assumptions we can make here.
318 // First, the Top Of Stack (TOS) is located on the top of page zero.
319 // we can share this stack between real and protected mode.
320 // that simplifies a lot of things ...
321 // we'll just push all the registers on the stack as longwords,
322 // and pop to protected mode.
323 // second, since this only ever runs as part of coreboot,
324 // we know all the segment register values -- so we don't save any.
325 // keep the handler that calls things small. It can do a call to
326 // more complex code in coreboot itself. This helps a lot as we don't
327 // have to do address fixup in this little stub, and calls are absolute
328 // so the handler is relocatable.
331 __asm__ __volatile__ (
336 " ljmp $0, $callbiosint16\n"
342 void debughandler(void)
344 __asm__ __volatile__ (
353 "end_debughandle: \n"
358 // Calling conventions. The first C function is called with this stuff
359 // on the stack. They look like value parameters, but note that if you
360 // modify them they will go back to the INTx function modified.
361 // the C function will call the biosint function with these as
362 // REFERENCE parameters. In this way, we can easily get
363 // returns back to the INTx caller (i.e. vgabios)
364 void callbiosint(void)
366 __asm__ __volatile__ (
373 // clean up the int #. To save space we put it in the lower
374 // byte. But the top 24 bits are junk.
375 " andl $0xff, %eax\n"
376 // this push does two things:
377 // - put the INT # on the stack as a parameter
378 // - provides us with a temp for the %cr0 mods.
383 " orl $0x00000001, %eax\n" /* PE = 1 */
385 /* Now that we are in protected mode jump to a 32 bit code segment. */
386 " data32 ljmp $0x10, $biosprotect\n"
389 " movw $0x18, %ax \n"
397 // back to real mode ...
398 " ljmp $0x28, $__rms_16bit2\n"
401 /* 16 bit code from here on... */
402 /* Load the segment registers w/ properly configured segment
403 * descriptors. They will retain these configurations (limits,
404 * writability, etc.) once protected mode is turned off. */
412 /* Turn off protection (bit 0 in CR0) */
413 " movl %cr0, %eax \n"
414 " andl $0xFFFFFFFE, %eax \n"
415 " movl %eax, %cr0 \n"
417 /* Now really going into real mode */
418 " ljmp $0, $__rms_real2 \n"
422 * FixME: where is esp? */
423 /* no need for a fix here. The esp is shared from 32-bit and 16-bit mode.
424 * you have to hack on the ss, but the esp remains the same across
430 /* debugging for RGM */
432 " outb %al, $0x80 \n"
434 /* Load our 16 bit idt */
439 /* Dump zeros in the other segregs */
446 /* pop the INT # that you pushed earlier */
463 int pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
464 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
465 unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
467 int handleint21(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
468 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
469 unsigned long *pecx, unsigned long *peax, unsigned long *pflags
472 int biosint(unsigned long intnumber,
473 unsigned long gsfs, unsigned long dses,
474 unsigned long edi, unsigned long esi,
475 unsigned long ebp, unsigned long esp,
476 unsigned long ebx, unsigned long edx,
477 unsigned long ecx, unsigned long eax,
478 unsigned long cs_ip, unsigned short stackflags)
489 printk_debug("biosint: INT# 0x%lx\n", intnumber);
490 printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n",
492 printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
494 printk_debug("biosint: ip 0x%x cs 0x%x flags 0x%x\n",
496 printk_debug("biosint: gs 0x%x fs 0x%x ds 0x%x es 0x%x\n",
497 gsfs >> 16, gsfs & 0xffff, dses >> 16, dses & 0xffff);
499 // cases in a good compiler are just as good as your own tables.
502 // These are not BIOS service, but the CPU-generated exceptions
503 printk_info("biosint: Oops, exception %u\n", intnumber);
505 printk_debug("Stack contents: ");
506 while (esp < 0x1000) {
507 printk_debug("0x%04x ", *(unsigned short *) esp);
512 printk_debug("biosint: Bailing out ... not now\n");
518 ret = pcibios( &edi, &esi, &ebp, &esp,
519 &ebx, &edx, &ecx, &eax, &flags);
527 ret=handleint21( &edi, &esi, &ebp, &esp,
528 &ebx, &edx, &ecx, &eax, &flags);
531 printk_info("BIOSINT: Unsupport int #0x%x\n",
536 flags |= 1; // carry flags
544 void setup_realmode_idt(void)
546 extern unsigned char idthandle, end_idthandle;
547 extern unsigned char debughandle, end_debughandle;
550 struct realidt *idts = (struct realidt *) 0;
551 int codesize = &end_idthandle - &idthandle;
552 unsigned char *intbyte, *codeptr;
554 // for each int, we create a customized little handler
555 // that just pushes %ax, puts the int # in %al,
556 // then calls the common interrupt handler.
557 // this necessitated because intel didn't know much about
558 // architecture when they did the 8086 (it shows)
559 // (hmm do they know anymore even now :-)
560 // obviously you can see I don't really care about memory
561 // efficiency. If I did I would probe back through the stack
562 // and get it that way. But that's really disgusting.
563 for (i = 0; i < 256; i++) {
565 codeptr = (char*) 4096 + i * codesize;
566 idts[i].offset = (unsigned) codeptr;
567 memcpy(codeptr, &idthandle, codesize);
568 intbyte = codeptr + 3;
572 // fixed entry points
574 // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
576 // calling convention here is the same as INTs, we can reuse
577 // the int entry code.
578 codeptr = (char*) 0xff065;
579 memcpy(codeptr, &idthandle, codesize);
580 intbyte = codeptr + 3;
581 *intbyte = 0x42; /* int42 is the relocated int10 */
583 /* debug handler - useful to set a programmable delay between instructions if the
584 TF bit is set upon call to real mode */
586 idts[1].offset = 16384;
587 memcpy(16384, &debughandle, &end_debughandle - &debughandle);
595 READCONFBYTE = 0xb108,
596 READCONFWORD = 0xb109,
597 READCONFDWORD = 0xb10a,
598 WRITECONFBYTE = 0xb10b,
599 WRITECONFWORD = 0xb10c,
600 WRITECONFDWORD = 0xb10d
603 // errors go in AH. Just set these up so that word assigns
606 PCIBIOS_NODEV = 0x8600,
607 PCIBIOS_BADREG = 0x8700
611 pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
612 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
613 unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
615 unsigned long edi = *pedi;
616 unsigned long esi = *pesi;
617 unsigned long ebp = *pebp;
618 unsigned long esp = *pesp;
619 unsigned long ebx = *pebx;
620 unsigned long edx = *pedx;
621 unsigned long ecx = *pecx;
622 unsigned long eax = *peax;
623 unsigned long flags = *pflags;
624 unsigned short func = (unsigned short) eax;
626 unsigned short devid, vendorid, devfn;
627 short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
643 while ((dev = dev_find_device(vendorid, devid, dev))) {
649 unsigned short busdevfn;
651 // busnum is an unsigned char;
652 // devfn is an int, so we mask it off.
653 busdevfn = (dev->bus->secondary << 8)
654 | (dev->path.pci.devfn & 0xff);
655 printk_debug("0x%x: return 0x%x\n", func, busdevfn);
659 *peax = PCIBIOS_NODEV;
676 devfn = *pebx & 0xff;
679 dev = dev_find_slot(bus, devfn);
681 printk_debug("0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
682 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
683 *peax = PCIBIOS_BADREG;
688 byte = pci_read_config8(dev, reg);
692 word = pci_read_config16(dev, reg);
696 dword = pci_read_config32(dev, reg);
701 pci_write_config8(dev, reg, byte);
705 pci_write_config16(dev, reg, word);
709 pci_write_config32(dev, reg, dword);
714 retval = PCIBIOS_BADREG;
715 printk_debug("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
716 func, bus, devfn, reg, *pecx);
722 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
729 int handleint21(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
730 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
731 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
734 printk_debug("handleint21, eax 0x%x\n", *eax);
741 *ebx=0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
750 *ecx = (*ecx & 0xffffff00 ) | 2; // panel type = 2 = 1024 * 768
755 *ebx= (*ebx & 0xffff0000) | 2;
756 *ecx= (*ecx & 0xffff0000) | 0x401; // PAL + crt only
757 *edx= (*edx & 0xffff0000) | 0; // TV Layout - default