Move C labels to start-of-line
[coreboot.git] / src / devices / oprom / x86_interrupts.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2001 Ronald G. Minnich
5  * Copyright (C) 2005 Nick.Barker9@btinternet.com
6  * Copyright (C) 2007-2009 coresystems GmbH
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #include <device/pci.h>
23 #include <device/pci_ids.h>
24 #include <device/pci_ops.h>
25 #include <string.h>
26 #include <console/console.h>
27 #include <arch/io.h>
28 #include <arch/registers.h>
29 #include "x86.h"
30
31 // errors go in AH. Just set these up so that word assigns
32 // will work. KISS.
33 enum {
34         PCIBIOS_SUCCESSFUL = 0x0000,
35         PCIBIOS_UNSUPPORTED = 0x8100,
36         PCIBIOS_BADVENDOR = 0x8300,
37         PCIBIOS_NODEV = 0x8600,
38         PCIBIOS_BADREG = 0x8700
39 };
40
41 int int10_handler(struct eregs *regs)
42 {
43         int res=-1;
44         static u8 cursor_row=0, cursor_col=0;
45         switch((regs->eax & 0xff00)>>8) {
46         case 0x01: // Set cursor shape
47                 res = 0;
48                 break;
49         case 0x02: // Set cursor position
50                 if (cursor_row != ((regs->edx >> 8) & 0xff) ||
51                     cursor_col >= (regs->edx & 0xff)) {
52                         printk(BIOS_INFO, "\n");
53                 }
54                 cursor_row = (regs->edx >> 8) & 0xff;
55                 cursor_col = regs->edx & 0xff;
56                 res = 0;
57                 break;
58         case 0x03: // Get cursor position
59                 regs->eax &= 0x00ff;
60                 regs->ecx = 0x0607;
61                 regs->edx = (cursor_row << 8) | cursor_col;
62                 res = 0;
63                 break;
64         case 0x06: // Scroll up
65                 printk(BIOS_INFO, "\n");
66                 res = 0;
67                 break;
68         case 0x08: // Get Character and Mode at Cursor Position
69                 regs->eax = 0x0f00 | 'A'; // White on black 'A'
70                 res = 0;
71                 break;
72         case 0x09: // Write Character and attribute
73         case 0x0e: // Write Character
74                 printk(BIOS_INFO, "%c", regs->eax & 0xff);
75                 res = 0;
76                 break;
77         case 0x0f: // Get video mode
78                 regs->eax = 0x5002; //80x25
79                 regs->ebx &= 0x00ff;
80                 res = 0;
81                 break;
82         default:
83                 printk(BIOS_WARNING, "Unknown INT10 function %04x!\n",
84                                 regs->eax & 0xffff);
85                 break;
86         }
87         return res;
88 }
89
90 int int12_handler(struct eregs *regs)
91 {
92         regs->eax = 64 * 1024;
93         return 0;
94 }
95
96 int int16_handler(struct eregs *regs)
97 {
98         int res=-1;
99         switch((regs->eax & 0xff00)>>8) {
100         case 0x00: // Check for Keystroke
101                 regs->eax = 0x6120; // Space Bar, Space
102                 res = 0;
103                 break;
104         case 0x01: // Check for Keystroke
105                 regs->eflags |= 1<<6; // Zero Flag set (no key available)
106                 res = 0;
107                 break;
108         default:
109                 printk(BIOS_WARNING, "Unknown INT16 function %04x!\n",
110                                 regs->eax & 0xffff);
111                 break;
112         }
113         return res;
114 }
115
116 #define PCI_CONFIG_SPACE_TYPE1  (1 << 0)
117 #define PCI_CONFIG_SPACE_TYPE2  (1 << 1)
118 #define PCI_SPECIAL_CYCLE_TYPE1 (1 << 4)
119 #define PCI_SPECIAL_CYCLE_TYPE2 (1 << 5)
120
121 int int1a_handler(struct eregs *regs)
122 {
123         unsigned short func = (unsigned short)regs->eax;
124         int retval = 0;
125         unsigned short devid, vendorid, devfn;
126         /* Use short to get rid of gabage in upper half of 32-bit register */
127         short devindex;
128         unsigned char bus;
129         struct device *dev;
130         u32 dword;
131         u16 word;
132         u8 byte, reg;
133
134         switch (func) {
135         case 0xb101: /* PCIBIOS Check */
136                 regs->edx = 0x20494350; /* ' ICP' */
137                 regs->eax &= 0xffff0000; /* Clear AH / AL */
138                 regs->eax |= PCI_CONFIG_SPACE_TYPE1 | PCI_SPECIAL_CYCLE_TYPE1;
139                 // last bus in the system. Hard code to 255 for now.
140                 // dev_enumerate() does not seem to tell us (publically)
141                 regs->ecx = 0xff;
142                 regs->edi = 0x00000000; /* protected mode entry */
143                 retval = 0;
144                 break;
145         case 0xb102: /* Find Device */
146                 devid = regs->ecx;
147                 vendorid = regs->edx;
148                 devindex = regs->esi;
149                 dev = 0;
150                 while ((dev = dev_find_device(vendorid, devid, dev))) {
151                         if (devindex <= 0)
152                                 break;
153                         devindex--;
154                 }
155                 if (dev) {
156                         unsigned short busdevfn;
157                         regs->eax &= 0xffff00ff; /* Clear AH */
158                         regs->eax |= PCIBIOS_SUCCESSFUL;
159                         // busnum is an unsigned char;
160                         // devfn is an int, so we mask it off.
161                         busdevfn = (dev->bus->secondary << 8)
162                             | (dev->path.pci.devfn & 0xff);
163                         printk(BIOS_DEBUG, "0x%x: return 0x%x\n", func, busdevfn);
164                         regs->ebx = busdevfn;
165                         retval = 0;
166                 } else {
167                         regs->eax &= 0xffff00ff; /* Clear AH */
168                         regs->eax |= PCIBIOS_NODEV;
169                         retval = -1;
170                 }
171                 break;
172         case 0xb10a: /* Read Config Dword */
173         case 0xb109: /* Read Config Word */
174         case 0xb108: /* Read Config Byte */
175         case 0xb10d: /* Write Config Dword */
176         case 0xb10c: /* Write Config Word */
177         case 0xb10b: /* Write Config Byte */
178                 devfn = regs->ebx & 0xff;
179                 bus = regs->ebx >> 8;
180                 reg = regs->edi;
181                 dev = dev_find_slot(bus, devfn);
182                 if (!dev) {
183                         printk(BIOS_DEBUG, "0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
184                         // Or are we supposed to return PCIBIOS_NODEV?
185                         regs->eax &= 0xffff00ff; /* Clear AH */
186                         regs->eax |= PCIBIOS_BADREG;
187                         retval = -1;
188                         return retval;
189                 }
190                 switch (func) {
191                 case 0xb108: /* Read Config Byte */
192                         byte = pci_read_config8(dev, reg);
193                         regs->ecx = byte;
194                         break;
195                 case 0xb109: /* Read Config Word */
196                         word = pci_read_config16(dev, reg);
197                         regs->ecx = word;
198                         break;
199                 case 0xb10a: /* Read Config Dword */
200                         dword = pci_read_config32(dev, reg);
201                         regs->ecx = dword;
202                         break;
203                 case 0xb10b: /* Write Config Byte */
204                         byte = regs->ecx;
205                         pci_write_config8(dev, reg, byte);
206                         break;
207                 case 0xb10c: /* Write Config Word */
208                         word = regs->ecx;
209                         pci_write_config16(dev, reg, word);
210                         break;
211                 case 0xb10d: /* Write Config Dword */
212                         dword = regs->ecx;
213                         pci_write_config32(dev, reg, dword);
214                         break;
215                 }
216
217 #if CONFIG_REALMODE_DEBUG
218                 printk(BIOS_DEBUG, "0x%x: bus %d devfn 0x%x reg 0x%x val 0x%x\n",
219                              func, bus, devfn, reg, regs->ecx);
220 #endif
221                 regs->eax &= 0xffff00ff; /* Clear AH */
222                 regs->eax |= PCIBIOS_SUCCESSFUL;
223                 retval = 0;
224                 break;
225         default:
226                 printk(BIOS_ERR, "UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
227                 regs->eax &= 0xffff00ff; /* Clear AH */
228                 regs->eax |= PCIBIOS_UNSUPPORTED;
229                 retval = -1;
230                 break;
231         }
232
233         return retval;
234 }
235