1 #include <console/console.h>
2 #include <device/pci.h>
3 #include <device/pci_ids.h>
4 #include <device/pci_ops.h>
8 /* vsmsetup.c derived from vgabios.c. Derived from: */
10 /*------------------------------------------------------------ -*- C -*-
11 * 2 Kernel Monte a.k.a. Linux loading Linux on x86
13 * Erik Arjan Hendriks <hendriks@lanl.gov>
15 * This version is a derivative of the original two kernel monte
16 * which is (C) 2000 Scyld.
18 * Copyright (C) 2000 Scyld Computing Corporation
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 * Portions related to the alpha architecture are:
36 * Copyright(C) 2001 University of California. LA-CC Number 01-67.
37 * This software has been authored by an employee or employees of the
38 * University of California, operator of the Los Alamos National
39 * Laboratory under Contract No. W-7405-ENG-36 with the U.S.
40 * Department of Energy. The U.S. Government has rights to use,
41 * reproduce, and distribute this software. If the software is
42 * modified to produce derivative works, such modified software should
43 * be clearly marked, so as not to confuse it with the version
44 * available from LANL.
46 * This software may be used and distributed according to the terms
47 * of the GNU General Public License, incorporated herein by
48 * reference to http://www.gnu.org/licenses/gpl.html.
50 * This software is provided by the author(s) "as is" and any express
51 * or implied warranties, including, but not limited to, the implied
52 * warranties of merchantability and fitness for a particular purpose
53 * are disclaimed. In no event shall the author(s) be liable for any
54 * direct, indirect, incidental, special, exemplary, or consequential
55 * damages (including, but not limited to, procurement of substitute
56 * goods or services; loss of use, data, or profits; or business
57 * interruption) however caused and on any theory of liability,
58 * whether in contract, strict liability, or tort (including
59 * negligence or otherwise) arising in any way out of the use of this
60 * software, even if advised of the possibility of such damage.
63 *--------------------------------------------------------------------*/
65 /* Modified to be a self sufficient plug in so that it can be used
66 without reliance on other parts of core Linuxbios
67 (C) 2005 Nick.Barker9@btinternet.com
69 Used initially for epia-m where there are problems getting the bios
70 emulator to successfully run this bios.
73 /* Declare a temporary global descriptor table - necessary because the
74 Core part of the bios no longer sets up any 16 bit segments */
76 /* pointer to original gdt */
81 /* compute the table limit */
82 "__mygdt_limit = __mygdt_end - __mygdt - 1 \n"
85 " .word __mygdt_limit \n"
89 /* selgdt 0, unused */
90 " .word 0x0000, 0x0000 \n"
91 " .byte 0x00, 0x00, 0x00, 0x00 \n"
93 /* selgdt 8, unused */
94 " .word 0x0000, 0x0000 \n"
95 " .byte 0x00, 0x00, 0x00, 0x00 \n"
97 /* selgdt 0x10, flat code segment */
98 " .word 0xffff, 0x0000 \n"
99 " .byte 0x00, 0x9b, 0xcf, 0x00 \n"
101 /* selgdt 0x18, flat data segment */
102 " .word 0xffff, 0x0000 \n"
103 " .byte 0x00, 0x93, 0xcf, 0x00 \n"
105 /* selgdt 0x20, unused */
106 " .word 0x0000, 0x0000 \n"
107 " .byte 0x00, 0x00, 0x00, 0x00 \n"
109 /* selgdt 0x28 16-bit 64k code at 0x00000000 */
110 " .word 0xffff, 0x0000 \n"
111 " .byte 0, 0x9a, 0, 0 \n"
113 /* selgdt 0x30 16-bit 64k data at 0x00000000 */
114 " .word 0xffff, 0x0000 \n"
115 " .byte 0, 0x92, 0, 0 \n"
120 /* Declare a pointer to where our idt is going to be i.e. at mem zero */
121 __asm__ ("__myidt: \n"
129 /* The address arguments to this function are PHYSICAL ADDRESSES */
130 static void real_mode_switch_call_vsm(unsigned long smm, unsigned long sysm)
132 __asm__ __volatile__ (
133 // paranoia -- does ecx get saved? not sure. This is
134 // the easiest safe thing to do.
137 " mov %esp, __stack \n"
139 "__stack: .long 0 \n"
141 /* get devfn into %ecx */
142 " movl %esp, %ebp \n"
144 /* I'm not happy about that pushal followed by esp-relative references.
145 * just do hard-codes for now
147 " movl 8(%ebp), %ecx \n"
148 " movl 12(%ebp), %edx \n"
150 " movl $0x10000026, %ecx \n"
151 " movl $0x10000028, %edx \n"
154 " lgdt %cs:__mygdtaddr \n"
156 /* This configures CS properly for real mode. */
157 " ljmp $0x28, $__rms_16bit\n"
160 /* 16 bit code from here on... */
162 /* Load the segment registers w/ properly configured segment
163 * descriptors. They will retain these configurations (limits,
164 * writability, etc.) once protected mode is turned off. */
172 /* Turn off protection (bit 0 in CR0) */
173 " movl %cr0, %eax \n"
174 " andl $0xFFFFFFFE, %eax \n"
175 " movl %eax, %cr0 \n"
177 /* Now really going into real mode */
178 " ljmp $0, $__rms_real\n"
181 /* put the stack at the end of page zero.
182 * that way we can easily share it between real and protected,
183 * since the 16-bit ESP at segment 0 will work for any case. */
187 " movl $0x1000, %eax \n"
188 " movl %eax, %esp \n"
190 /* Load our 16 it idt */
195 /* Dump zeros in the other segregs */
197 /* FixMe: Big real mode for gs, fs? */
204 /* run VGA BIOS at 0x6000:0020 */
205 " lcall $0x6000, $0x0020\n"
207 /* if we got here, just about done.
208 * Need to get back to protected mode */
209 " movl %cr0, %eax \n"
210 " orl $0x0000001, %eax\n" /* PE = 1 */
211 " movl %eax, %cr0 \n"
213 /* Now that we are in protected mode jump to a 32 bit code segment. */
214 " data32 ljmp $0x10, $vsmrestart\n"
217 " movw $0x18, %ax \n"
224 /* restore proper gdt and idt */
225 " lgdt %cs:gdtarg \n"
230 " mov __stack, %esp \n"
235 __asm__ (".text\n""real_mode_switch_end:\n");
236 extern char real_mode_switch_end[];
239 void do_vsmbios(void)
242 unsigned long busdevfn;
243 unsigned int rom = 0;
245 unsigned int size = 256*1024;
248 printk_err("do_vsmbios\n");
249 /* clear vsm bios data area */
250 for (i = 0x400; i < 0x500; i++) {
251 *(unsigned char *) i = 0;
254 /* declare rom address here - keep any config data out of the way
255 * of core LXB stuff */
257 /* this is the base of rom on the GX2 at present. At some point, this has to be
258 * much better parameterized
263 buf = (unsigned char *) rom;
264 printk_debug("buf %p *buf %d buf[256k] %d\n",
265 buf, buf[0], buf[256*1024]);
266 printk_debug("buf[0x20] signature is %x:%x:%x:%x\n",
267 buf[0x20] ,buf[0x21] ,buf[0x22],buf[0x23]);
268 /* check for post code at start of vsainit.bin. If you don't see it,
270 if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
271 (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
272 printk_err("do_vsmbios: no vsainit.bin signature, skipping!\n");
276 memcpy((void *) 0x60000, buf, size);
278 /* ecx gets smm, edx gets sysm */
279 printk_err("Call real_mode_switch_call_vsm\n");
280 real_mode_switch_call_vsm(0x10000026, 0x10000028);
284 // we had hoped to avoid this.
285 // this is a stub IDT only. It's main purpose is to ignore calls
287 // no longer. Dammit. We have to respond to these.
289 unsigned short offset, cs;
292 // from a handy writeup that andrey found.
295 // There are some assumptions we can make here.
296 // First, the Top Of Stack (TOS) is located on the top of page zero.
297 // we can share this stack between real and protected mode.
298 // that simplifies a lot of things ...
299 // we'll just push all the registers on the stack as longwords,
300 // and pop to protected mode.
301 // second, since this only ever runs as part of linuxbios,
302 // we know all the segment register values -- so we don't save any.
303 // keep the handler that calls things small. It can do a call to
304 // more complex code in linuxbios itself. This helps a lot as we don't
305 // have to do address fixup in this little stub, and calls are absolute
306 // so the handler is relocatable.
309 __asm__ __volatile__ (
314 " ljmp $0, $callbiosint16\n"
320 void debughandler(void)
322 __asm__ __volatile__ (
331 "end_debughandle: \n"
336 // Calling conventions. The first C function is called with this stuff
337 // on the stack. They look like value parameters, but note that if you
338 // modify them they will go back to the INTx function modified.
339 // the C function will call the biosint function with these as
340 // REFERENCE parameters. In this way, we can easily get
341 // returns back to the INTx caller (i.e. vgabios)
342 void callbiosint(void)
344 __asm__ __volatile__ (
351 // clean up the int #. To save space we put it in the lower
352 // byte. But the top 24 bits are junk.
353 " andl $0xff, %eax\n"
354 // this push does two things:
355 // - put the INT # on the stack as a parameter
356 // - provides us with a temp for the %cr0 mods.
361 " orl $0x00000001, %eax\n" /* PE = 1 */
363 /* Now that we are in protected mode jump to a 32 bit code segment. */
364 " data32 ljmp $0x10, $biosprotect\n"
367 " movw $0x18, %ax \n"
375 // back to real mode ...
376 " ljmp $0x28, $__rms_16bit2\n"
379 /* 16 bit code from here on... */
380 /* Load the segment registers w/ properly configured segment
381 * descriptors. They will retain these configurations (limits,
382 * writability, etc.) once protected mode is turned off. */
390 /* Turn off protection (bit 0 in CR0) */
391 " movl %cr0, %eax \n"
392 " andl $0xFFFFFFFE, %eax \n"
393 " movl %eax, %cr0 \n"
395 /* Now really going into real mode */
396 " ljmp $0, $__rms_real2 \n"
400 * FixME: where is esp? */
401 /* no need for a fix here. The esp is shared from 32-bit and 16-bit mode.
402 * you have to hack on the ss, but the esp remains the same across
408 /* debugging for RGM */
410 " outb %al, $0x80 \n"
412 /* Load our 16 bit idt */
417 /* Dump zeros in the other segregs */
424 /* pop the INT # that you pushed earlier */
441 int pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
442 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
443 unsigned long *pecx, unsigned long *peax, unsigned long *pflags);
445 int handleint21(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
446 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
447 unsigned long *pecx, unsigned long *peax, unsigned long *pflags
450 int biosint(unsigned long intnumber,
451 unsigned long gsfs, unsigned long dses,
452 unsigned long edi, unsigned long esi,
453 unsigned long ebp, unsigned long esp,
454 unsigned long ebx, unsigned long edx,
455 unsigned long ecx, unsigned long eax,
456 unsigned long cs_ip, unsigned short stackflags)
467 printk_debug("biosint: INT# 0x%lx\n", intnumber);
468 printk_debug("biosint: eax 0x%lx ebx 0x%lx ecx 0x%lx edx 0x%lx\n",
470 printk_debug("biosint: ebp 0x%lx esp 0x%lx edi 0x%lx esi 0x%lx\n",
472 printk_debug("biosint: ip 0x%x cs 0x%x flags 0x%x\n",
474 printk_debug("biosint: gs 0x%x fs 0x%x ds 0x%x es 0x%x\n",
475 gsfs >> 16, gsfs & 0xffff, dses >> 16, dses & 0xffff);
477 // cases in a good compiler are just as good as your own tables.
480 // These are not BIOS service, but the CPU-generated exceptions
481 printk_info("biosint: Oops, exception %u\n", intnumber);
483 printk_debug("Stack contents: ");
484 while (esp < 0x1000) {
485 printk_debug("0x%04x ", *(unsigned short *) esp);
490 printk_debug("biosint: Bailing out ... not now\n");
496 ret = pcibios( &edi, &esi, &ebp, &esp,
497 &ebx, &edx, &ecx, &eax, &flags);
505 ret=handleint21( &edi, &esi, &ebp, &esp,
506 &ebx, &edx, &ecx, &eax, &flags);
509 printk_info("BIOSINT: Unsupport int #0x%x\n",
514 flags |= 1; // carry flags
522 void setup_realmode_idt(void)
524 extern unsigned char idthandle, end_idthandle;
525 extern unsigned char debughandle, end_debughandle;
528 struct realidt *idts = (struct realidt *) 0;
529 int codesize = &end_idthandle - &idthandle;
530 unsigned char *intbyte, *codeptr;
532 // for each int, we create a customized little handler
533 // that just pushes %ax, puts the int # in %al,
534 // then calls the common interrupt handler.
535 // this necessitated because intel didn't know much about
536 // architecture when they did the 8086 (it shows)
537 // (hmm do they know anymore even now :-)
538 // obviously you can see I don't really care about memory
539 // efficiency. If I did I would probe back through the stack
540 // and get it that way. But that's really disgusting.
541 for (i = 0; i < 256; i++) {
543 codeptr = (char*) 4096 + i * codesize;
544 idts[i].offset = (unsigned) codeptr;
545 memcpy(codeptr, &idthandle, codesize);
546 intbyte = codeptr + 3;
550 // fixed entry points
552 // VGA BIOSes tend to hardcode f000:f065 as the previous handler of
554 // calling convention here is the same as INTs, we can reuse
555 // the int entry code.
556 codeptr = (char*) 0xff065;
557 memcpy(codeptr, &idthandle, codesize);
558 intbyte = codeptr + 3;
559 *intbyte = 0x42; /* int42 is the relocated int10 */
561 /* debug handler - useful to set a programmable delay between instructions if the
562 TF bit is set upon call to real mode */
564 idts[1].offset = 16384;
565 memcpy(16384, &debughandle, &end_debughandle - &debughandle);
575 READCONFBYTE = 0xb108,
576 READCONFWORD = 0xb109,
577 READCONFDWORD = 0xb10a,
578 WRITECONFBYTE = 0xb10b,
579 WRITECONFWORD = 0xb10c,
580 WRITECONFDWORD = 0xb10d
583 // errors go in AH. Just set these up so that word assigns
586 PCIBIOS_NODEV = 0x8600,
587 PCIBIOS_BADREG = 0x8700
591 pcibios(unsigned long *pedi, unsigned long *pesi, unsigned long *pebp,
592 unsigned long *pesp, unsigned long *pebx, unsigned long *pedx,
593 unsigned long *pecx, unsigned long *peax, unsigned long *pflags)
595 unsigned long edi = *pedi;
596 unsigned long esi = *pesi;
597 unsigned long ebp = *pebp;
598 unsigned long esp = *pesp;
599 unsigned long ebx = *pebx;
600 unsigned long edx = *pedx;
601 unsigned long ecx = *pecx;
602 unsigned long eax = *peax;
603 unsigned long flags = *pflags;
604 unsigned short func = (unsigned short) eax;
606 unsigned short devid, vendorid, devfn;
607 short devindex; /* Use short to get rid of gabage in upper half of 32-bit register */
623 while ((dev = dev_find_device(vendorid, devid, dev))) {
629 unsigned short busdevfn;
631 // busnum is an unsigned char;
632 // devfn is an int, so we mask it off.
633 busdevfn = (dev->bus->secondary << 8)
634 | (dev->path.u.pci.devfn & 0xff);
635 printk_debug("0x%x: return 0x%x\n", func, busdevfn);
639 *peax = PCIBIOS_NODEV;
656 devfn = *pebx & 0xff;
659 dev = dev_find_slot(bus, devfn);
661 printk_debug("0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
662 // idiots. the pcibios guys assumed you'd never pass a bad bus/devfn!
663 *peax = PCIBIOS_BADREG;
668 byte = pci_read_config8(dev, reg);
672 word = pci_read_config16(dev, reg);
676 dword = pci_read_config32(dev, reg);
681 pci_write_config8(dev, reg, byte);
685 pci_write_config16(dev, reg, word);
689 pci_write_config32(dev, reg, dword);
694 retval = PCIBIOS_BADREG;
695 printk_debug("0x%x: bus %d devfn 0x%x reg 0x%x val 0x%lx\n",
696 func, bus, devfn, reg, *pecx);
702 printk_err("UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
709 int handleint21(unsigned long *edi, unsigned long *esi, unsigned long *ebp,
710 unsigned long *esp, unsigned long *ebx, unsigned long *edx,
711 unsigned long *ecx, unsigned long *eax, unsigned long *flags)
714 printk_debug("handleint21, eax 0x%x\n", *eax);
721 *ebx=0x545; // MCLK = 133, 32M frame buffer, 256 M main memory
730 *ecx = (*ecx & 0xffffff00 ) | 2; // panel type = 2 = 1024 * 768
735 *ebx= (*ebx & 0xffff0000) | 2;
736 *ecx= (*ecx & 0xffff0000) | 0x401; // PAL + crt only
737 *edx= (*edx & 0xffff0000) | 0; // TV Layout - default