Change license from GPLv3 to LGPLv3.
[seabios.git] / src / vgahooks.c
1 // Hooks for via vgabios calls into main bios.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "bregs.h" // set_code_fail
8 #include "biosvar.h" // GET_GLOBAL
9 #include "pci.h" // pci_find_device
10 #include "pci_ids.h" // PCI_VENDOR_ID_VIA
11 #include "util.h" // handle_155f
12 #include "config.h" // CONFIG_*
13
14 static void
15 handle_155f01(struct bregs *regs)
16 {
17     regs->eax = 0x5f;
18     regs->cl = 2; // panel type =  2 = 1024 * 768
19     set_success(regs);
20     dprintf(1, "Warning: VGA panel type is hardcoded\n");
21 }
22
23 static void
24 handle_155f02(struct bregs *regs)
25 {
26     regs->eax = 0x5f;
27     regs->bx = 2;
28     regs->cx = 0x401;  // PAL + crt only
29     regs->dx = 0;  // TV Layout - default
30     set_success(regs);
31     dprintf(1, "Warning: VGA TV/CRT output type is hardcoded\n");
32 }
33
34 static int
35 getFBSize()
36 {
37     /* Find K8M890 */
38     int bdf = pci_find_device(PCI_VENDOR_ID_VIA, 0x3336);
39     if (bdf < 0)
40         goto err;
41
42     /* FB config */
43     u8 reg = pci_config_readb(bdf, 0xa1);
44
45     /* GFX disabled ? */
46     if (!(reg & 0x80))
47         goto err;
48
49     static u8 mem_power[] VAR16 = {0, 3, 4, 5, 6, 7, 8, 9};
50     return GET_GLOBAL(mem_power[(reg >> 4) & 0x7]);
51 err:
52     dprintf(1, "Warning: VGA memory size is hardcoded\n");
53     return 5; // 32M frame buffer
54 }
55
56 static int
57 getRamSpeed()
58 {
59     int bdf = pci_find_device(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MEMCTL);
60     if (bdf < 0)
61         goto err;
62
63     /* mem clk 0 = DDR2 400 */
64     u8 reg = pci_config_readb(bdf, 0x94) & 0x7;
65     return reg + 6;
66 err:
67     dprintf(1, "Warning: VGA memory clock speed is hardcoded\n");
68     return 4; // MCLK = DDR266
69 }
70
71 /* int 0x15 - 5f18
72
73    ECX = unknown/dont care
74    EBX[3..0] Frame Buffer Size 2^N MiB
75    EBX[7..4] Memory speed:
76        0: SDR  66Mhz
77        1: SDR 100Mhz
78        2: SDR 133Mhz
79        3: DDR 100Mhz (PC1600 or DDR200)
80        4: DDR 133Mhz (PC2100 or DDR266)
81        5: DDR 166Mhz (PC2700 or DDR333)
82        6: DDR 200Mhz (PC3200 or DDR400)
83        7: DDR2 133Mhz (DDR2 533)
84        8: DDR2 166Mhz (DDR2 667)
85        9: DDR2 200Mhz (DDR2 800)
86        A: DDR2 233Mhz (DDR2 1066)
87        B: and above: Unknown
88    EBX[?..8] Total memory size?
89    EAX = 0x5f for success
90
91     K8M890 BIOS wants only this call (Desktop NoTv)
92 */
93
94 static void
95 handle_155f18(struct bregs *regs)
96 {
97     regs->eax = 0x5f;
98     u32 ramspeed = getRamSpeed();
99     u32 fbsize = getFBSize();
100     regs->ebx = 0x500 | (ramspeed << 4) | fbsize;
101     regs->ecx = 0x060;
102     set_success(regs);
103 }
104
105 static void
106 handle_155f19(struct bregs *regs)
107 {
108     set_fail_silent(regs);
109 }
110
111 static void
112 handle_155fXX(struct bregs *regs)
113 {
114     set_code_fail(regs, RET_EUNSUPPORTED);
115 }
116
117 void
118 handle_155f(struct bregs *regs)
119 {
120     if (! CONFIG_VGAHOOKS) {
121         handle_155fXX(regs);
122         return;
123     }
124
125     switch (regs->al) {
126     case 0x01: handle_155f01(regs); break;
127     case 0x02: handle_155f02(regs); break;
128     case 0x18: handle_155f18(regs); break;
129     case 0x19: handle_155f19(regs); break;
130     default:   handle_155fXX(regs); break;
131     }
132 }