blob: ddeb1d31d7bf210a23a6d348c19fd12cae0771be [file] [log] [blame]
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -04001// Hooks for via vgabios calls into main bios.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -04006
Kevin O'Connor47358772008-12-26 13:01:23 -05007#include "bregs.h" // set_code_fail
8#include "biosvar.h" // GET_GLOBAL
9#include "pci.h" // pci_find_device
Kevin O'Connor22e1b912009-07-19 18:52:46 -040010#include "pci_regs.h" // PCI_VENDOR_ID
Kevin O'Connor47358772008-12-26 13:01:23 -050011#include "pci_ids.h" // PCI_VENDOR_ID_VIA
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -040012#include "util.h" // handle_155f
13#include "config.h" // CONFIG_*
14
Kevin O'Connor22e1b912009-07-19 18:52:46 -040015// The Bus/Dev/Fn of the primary VGA device.
16int VGAbdf VAR16_32;
17// Coreboot board detected.
18int CBmainboard VAR16_32;
19
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -040020static void
Kevin O'Connor22e1b912009-07-19 18:52:46 -040021handle_155fXX(struct bregs *regs)
22{
23 set_code_fail(regs, RET_EUNSUPPORTED);
24}
25
26
27/****************************************************************
28 * Via hooks
29 ****************************************************************/
30
31static void
32via_155f01(struct bregs *regs)
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -040033{
34 regs->eax = 0x5f;
35 regs->cl = 2; // panel type = 2 = 1024 * 768
36 set_success(regs);
Kevin O'Connor47358772008-12-26 13:01:23 -050037 dprintf(1, "Warning: VGA panel type is hardcoded\n");
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -040038}
39
40static void
Kevin O'Connor22e1b912009-07-19 18:52:46 -040041via_155f02(struct bregs *regs)
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -040042{
43 regs->eax = 0x5f;
44 regs->bx = 2;
45 regs->cx = 0x401; // PAL + crt only
46 regs->dx = 0; // TV Layout - default
47 set_success(regs);
Kevin O'Connor47358772008-12-26 13:01:23 -050048 dprintf(1, "Warning: VGA TV/CRT output type is hardcoded\n");
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -040049}
50
Kevin O'Connor47358772008-12-26 13:01:23 -050051static int
Kevin O'Connor22e1b912009-07-19 18:52:46 -040052getFBSize(u16 bdf)
Kevin O'Connor47358772008-12-26 13:01:23 -050053{
Kevin O'Connor47358772008-12-26 13:01:23 -050054 /* FB config */
55 u8 reg = pci_config_readb(bdf, 0xa1);
56
57 /* GFX disabled ? */
58 if (!(reg & 0x80))
Kevin O'Connor22e1b912009-07-19 18:52:46 -040059 return -1;
Kevin O'Connor47358772008-12-26 13:01:23 -050060
Kevin O'Connor92f95b02008-12-29 20:42:40 -050061 static u8 mem_power[] VAR16 = {0, 3, 4, 5, 6, 7, 8, 9};
Kevin O'Connor47358772008-12-26 13:01:23 -050062 return GET_GLOBAL(mem_power[(reg >> 4) & 0x7]);
Kevin O'Connor47358772008-12-26 13:01:23 -050063}
64
65static int
Kevin O'Connor22e1b912009-07-19 18:52:46 -040066getViaRamSpeed(u16 bdf)
67{
68 return (pci_config_readb(bdf, 0x90) & 0x07) + 3;
69}
70
71static int
72getAMDRamSpeed()
Kevin O'Connor47358772008-12-26 13:01:23 -050073{
74 int bdf = pci_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MEMCTL);
75 if (bdf < 0)
Kevin O'Connor22e1b912009-07-19 18:52:46 -040076 return -1;
Kevin O'Connor47358772008-12-26 13:01:23 -050077
78 /* mem clk 0 = DDR2 400 */
Kevin O'Connor22e1b912009-07-19 18:52:46 -040079 return (pci_config_readb(bdf, 0x94) & 0x7) + 6;
Kevin O'Connor47358772008-12-26 13:01:23 -050080}
81
82/* int 0x15 - 5f18
83
84 ECX = unknown/dont care
85 EBX[3..0] Frame Buffer Size 2^N MiB
86 EBX[7..4] Memory speed:
87 0: SDR 66Mhz
88 1: SDR 100Mhz
89 2: SDR 133Mhz
90 3: DDR 100Mhz (PC1600 or DDR200)
91 4: DDR 133Mhz (PC2100 or DDR266)
92 5: DDR 166Mhz (PC2700 or DDR333)
93 6: DDR 200Mhz (PC3200 or DDR400)
94 7: DDR2 133Mhz (DDR2 533)
95 8: DDR2 166Mhz (DDR2 667)
96 9: DDR2 200Mhz (DDR2 800)
97 A: DDR2 233Mhz (DDR2 1066)
98 B: and above: Unknown
99 EBX[?..8] Total memory size?
100 EAX = 0x5f for success
Kevin O'Connor47358772008-12-26 13:01:23 -0500101*/
102
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400103#define PCI_DEVICE_ID_VIA_K8M890CE_3 0x3336
104#define PCI_DEVICE_ID_VIA_VX855_MEMCTRL 0x3409
105
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400106static void
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400107via_155f18(struct bregs *regs)
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400108{
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400109 u32 ramspeed, fbsize;
110
111 int bdf = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_K8M890CE_3);
112 if (bdf >= 0) {
113 fbsize = getFBSize(bdf);
114 ramspeed = getAMDRamSpeed();
115 goto done;
116 }
117 bdf = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_MEMCTRL);
118 if (bdf >= 0) {
119 fbsize = getFBSize(bdf);
120 ramspeed = getViaRamSpeed(bdf);
121 goto done;
122 }
123
124 dprintf(1, "Warning: VGA memory size and speed is hardcoded\n");
125 fbsize = 5; // 32M frame buffer
126 ramspeed = 4; // MCLK = DDR266
127
128done:
129 if (fbsize < 0 || ramspeed < 0) {
130 set_code_fail(regs, RET_EUNSUPPORTED);
131 return;
132 }
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400133 regs->eax = 0x5f;
Kevin O'Connor47358772008-12-26 13:01:23 -0500134 regs->ebx = 0x500 | (ramspeed << 4) | fbsize;
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400135 regs->ecx = 0x060;
136 set_success(regs);
137}
138
139static void
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400140via_155f19(struct bregs *regs)
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400141{
142 set_fail_silent(regs);
143}
144
145static void
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400146via_155f(struct bregs *regs)
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400147{
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400148 switch (regs->al) {
149 case 0x01: via_155f01(regs); break;
150 case 0x02: via_155f02(regs); break;
151 case 0x18: via_155f18(regs); break;
152 case 0x19: via_155f19(regs); break;
153 default: handle_155fXX(regs); break;
154 }
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400155}
156
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400157
158/****************************************************************
159 * Entry and setup
160 ****************************************************************/
161
162// Main 16bit entry point
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400163void
164handle_155f(struct bregs *regs)
165{
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400166 if (! CONFIG_VGAHOOKS)
167 goto fail;
168
169 // XXX - Use this value later.
170 //int cbmb = GET_GLOBAL(CBmainboard);
171
172 int bdf = GET_GLOBAL(VGAbdf);
173 if (bdf < 0)
174 goto fail;
175 u16 vendor = pci_config_readw(bdf, PCI_VENDOR_ID);
176 if (vendor == PCI_VENDOR_ID_VIA) {
177 via_155f(regs);
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400178 return;
179 }
180
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400181fail:
182 handle_155fXX(regs);
183}
184
185// Setup
186void
187vgahook_setup(const char *vendor, const char *part)
188{
189 if (! CONFIG_VGAHOOKS)
190 return;
191 // XXX - add support later.
192 CBmainboard = 0;
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400193}