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 linuxbios 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 Linuxbios
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;
258 printk_err("do_vsmbios\n");
259 /* clear vsm bios data area */
260 for (i = 0x400; i < 0x500; i++) {
261 *(unsigned char *) i = 0;
264 /* declare rom address here - keep any config data out of the way
265 * of core LXB stuff */
267 /* this is the base of rom on the GX2 at present. At some point, this has to be
268 * much better parameterized
272 /* the VSA starts at the base of rom - 64 */
273 rom = ((unsigned long) 0) - (ROM_SIZE + 64*1024);
275 buf = (unsigned char *) 0x60000;
276 unrv2b((uint8_t *)rom, buf);
277 printk_debug("buf %p *buf %d buf[256k] %d\n",
278 buf, buf[0], buf[SMM_SIZE*1024]);
279 printk_debug("buf[0x20] signature is %x:%x:%x:%x\n",
280 buf[0x20] ,buf[0x21] ,buf[0x22],buf[0x23]);
281 /* check for post code at start of vsainit.bin. If you don't see it,
283 if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
284 (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
285 printk_err("do_vsmbios: no vsainit.bin signature, skipping!\n");
289 //memcpy((void *) 0x60000, buf, size);
291 //for (i = 0; i < 0x800000; i++)
294 /* ecx gets smm, edx gets sysm */
295 printk_err("Call real_mode_switch_call_vsm\n");
296 real_mode_switch_call_vsm(0x10000026, 0x10000028);
298 /* restart timer 1 */
304 // we had hoped to avoid this.
305 // this is a stub IDT only. It's main purpose is to ignore calls
307 // no longer. Dammit. We have to respond to these.
309 unsigned short offset, cs;
312 // from a handy writeup that andrey found.
315 // There are some assumptions we can make here.
316 // First, the Top Of Stack (TOS) is located on the top of page zero.
317 // we can share this stack between real and protected mode.
318 // that simplifies a lot of things ...
319 // we'll just push all the registers on the stack as longwords,
320 // and pop to protected mode.
321 // second, since this only ever runs as part of linuxbios,
322 // we know all the segment register values -- so we don't save any.
323 // keep the handler that calls things small. It can do a call to
324 // more complex code in linuxbios itself. This helps a lot as we don't
325 // have to do address fixup in this little stub, and calls are absolute
326 // so the handler is relocatable.
329 __asm__ __volatile__ (
334 " ljmp $0, $callbiosint16\n"
340 void debughandler(void)
342 __asm__ __volatile__ (
351 "end_debughandle: \n"
356 // Calling conventions. The first C function is called with this stuff
357 // on the stack. They look like value parameters, but note that if you
358 // modify them they will go back to the INTx function modified.
359 // the C function will call the biosint function with these as
360 // REFERENCE parameters. In this way, we can easily get
361 // returns back to the INTx caller (i.e. vgabios)
362 void callbiosint(void)
364 __asm__ __volatile__ (
371 // clean up the int #. To save space we put it in the lower
372 // byte. But the top 24 bits are junk.
373 " andl $0xff, %eax\n"
374 // this push does two things:
375 // - put the INT # on the stack as a parameter
376 // - provides us with a temp for the %cr0 mods.
381 " orl $0x00000001, %eax\n" /* PE = 1 */
383 /* Now that we are in protected mode jump to a 32 bit code segment. */
384 " data32 ljmp $0x10, $biosprotect\n"
387 " movw $0x18, %ax \n"
395 // back to real mode ...
396 " ljmp $0x28, $__rms_16bit2\n"
399 /* 16 bit code from here on... */
400 /* Load the segment registers w/ properly configured segment
401 * descriptors. They will retain these configurations (limits,
402 * writability, etc.) once protected mode is turned off. */
410 /* Turn off protection (bit 0 in CR0) */
411 " movl %cr0, %eax \n"
412 " andl $0xFFFFFFFE, %eax \n"
413 " movl %eax, %cr0 \n"
415 /* Now really going into real mode */
416 " ljmp $0, $__rms_real2 \n"
420 * FixME: where is esp? */
421 /* no need for a fix here. The esp is shared from 32-bit and 16-bit mode.
422 * you have to hack on the ss, but the esp remains the same across
428 /* debugging for RGM */
430 " outb %al, $0x80 \n"
432 /* Load our 16 bit idt */
437 /* Dump zeros in the other segregs */
444 /* pop the INT # that you pushed earlier */
461 int pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
462 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
463 unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
465 int handleint21(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
466 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
467 unsigned long *pecx, unsigned long *peax, unsigned long *pflags
470 int biosint(unsigned long intnumber,
471 unsigned long gsfs, unsigned long dses,
472 unsigned long edi, unsigned long esi,
473 unsigned long ebp, unsigned long esp,
474 unsigned long ebx, unsigned long edx,
475 unsigned long ecx, unsigned long eax,
476 unsigned long cs_ip, unsigned short stackflags)
487 printk_debug("biosint: INT# 0x%lx\n", intnumber);
488 printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n",
490 printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
492 printk_debug("biosint: ip 0x%x cs 0x%x flags 0x%x\n",
494 printk_debug("biosint: gs 0x%x fs 0x%x ds 0x%x es 0x%x\n",
495 gsfs >> 16, gsfs & 0xffff, dses >> 16, dses & 0xffff);
497 // cases in a good compiler are just as good as your own tables.
500 // These are not BIOS service, but the CPU-generated exceptions
501 printk_info("biosint: Oops, exception %u\n", intnumber);
503 printk_debug("Stack contents: ");
504 while (esp < 0x1000) {
505 printk_debug("0x%04x ", *(unsigned short *) esp);
510 printk_debug("biosint: Bailing out ... not now\n");
516 ret = pcibios( &edi, &esi, &ebp, &esp,
517 &ebx, &edx, &ecx, &eax, &flags);
525 ret=handleint21( &edi, &esi, &ebp, &esp,
526 &ebx, &edx, &ecx, &eax, &flags);
529 printk_info("BIOSINT: Unsupport int #0x%x\n",
534 flags |= 1; // carry flags
542 void setup_realmode_idt(void)
544 extern unsigned char idthandle, end_idthandle;
545 extern unsigned char debughandle, end_debughandle;
548 struct realidt *idts = (struct realidt *) 0;
549 int codesize = &end_idthandle - &idthandle;
550 unsigned char *intbyte, *codeptr;
552 // for each int, we create a customized little handler
553 // that just pushes %ax, puts the int # in %al,
554 // then calls the common interrupt handler.
555 // this necessitated because intel didn't know much about
556 // architecture when they did the 8086 (it shows)
557 // (hmm do they know anymore even now :-)
558 // obviously you can see I don't really care about memory
559 // efficiency. If I did I would probe back through the stack
560 // and get it that way. But that's really disgusting.
561 for (i = 0; i < 256; i++) {
563 codeptr = (char*) 4096 + i * codesize;
564 idts[i].offset = (unsigned) codeptr;
565 memcpy(codeptr, &idthandle, codesize);
566 intbyte = codeptr + 3;
570 // fixed entry points
572 // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
574 // calling convention here is the same as INTs, we can reuse
575 // the int entry code.
576 codeptr = (char*) 0xff065;
577 memcpy(codeptr, &idthandle, codesize);
578 intbyte = codeptr + 3;
579 *intbyte = 0x42; /* int42 is the relocated int10 */
581 /* debug handler - useful to set a programmable delay between instructions if the
582 TF bit is set upon call to real mode */
584 idts[1].offset = 16384;
585 memcpy(16384, &debughandle, &end_debughandle - &debughandle);
593 READCONFBYTE = 0xb108,
594 READCONFWORD = 0xb109,
595 READCONFDWORD = 0xb10a,
596 WRITECONFBYTE = 0xb10b,
597 WRITECONFWORD = 0xb10c,
598 WRITECONFDWORD = 0xb10d
601 // errors go in AH. Just set these up so that word assigns
604 PCIBIOS_NODEV = 0x8600,
605 PCIBIOS_BADREG = 0x8700
609 pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
610 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
611 unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
613 unsigned long edi = *pedi;
614 unsigned long esi = *pesi;
615 unsigned long ebp = *pebp;
616 unsigned long esp = *pesp;
617 unsigned long ebx = *pebx;
618 unsigned long edx = *pedx;
619 unsigned long ecx = *pecx;
620 unsigned long eax = *peax;
621 unsigned long flags = *pflags;
622 unsigned short func = (unsigned short) eax;
624 unsigned short devid, vendorid, devfn;
625 short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
641 while ((dev = dev_find_device(vendorid, devid, dev))) {
647 unsigned short busdevfn;
649 // busnum is an unsigned char;
650 // devfn is an int, so we mask it off.
651 busdevfn = (dev->bus->secondary << 8)
652 | (dev->path.u.pci.devfn & 0xff);
653 printk_debug("0x%x: return 0x%x\n", func, busdevfn);
657 *peax = PCIBIOS_NODEV;
674 devfn = *pebx & 0xff;
677 dev = dev_find_slot(bus, devfn);
679 printk_debug("0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
680 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
681 *peax = PCIBIOS_BADREG;
686 byte = pci_read_config8(dev, reg);
690 word = pci_read_config16(dev, reg);
694 dword = pci_read_config32(dev, reg);
699 pci_write_config8(dev, reg, byte);
703 pci_write_config16(dev, reg, word);
707 pci_write_config32(dev, reg, dword);
712 retval = PCIBIOS_BADREG;
713 printk_debug("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
714 func, bus, devfn, reg, *pecx);
720 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
727 int handleint21(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
728 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
729 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
732 printk_debug("handleint21, eax 0x%x\n", *eax);
739 *ebx=0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
748 *ecx = (*ecx & 0xffffff00 ) | 2; // panel type = 2 = 1024 * 768
753 *ebx= (*ebx & 0xffff0000) | 2;
754 *ecx= (*ecx & 0xffff0000) | 0x401; // PAL + crt only
755 *edx= (*edx & 0xffff0000) | 0; // TV Layout - default