printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / southbridge / via / k8t890 / k8t890_dram.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <device/device.h>
21 #include <device/pci.h>
22 #include <device/pci_ids.h>
23 #include <console/console.h>
24 #include <cpu/x86/msr.h>
25 #include <cpu/amd/mtrr.h>
26 #include <pc80/mc146818rtc.h>
27 #include <bitops.h>
28 #include "k8t890.h"
29
30 static void dram_enable(struct device *dev)
31 {
32         msr_t msr;
33         u16 reg;
34
35         /*
36          * Enable Lowest Interrupt arbitration for APIC, enable NB APIC
37          * decoding, MSI support, no SMRAM, compatible SMM.
38          */
39         pci_write_config8(dev, 0x86, 0x39);
40
41         /*
42          * We want to use the 0xC0000-0xEFFFF as RAM mark area as RW, even if
43          * memory is doing K8 the DMA from SB will fail if we have it wrong,
44          * AND even we have it here, we must later copy it to SB to make it work :/
45          */
46
47         /* For CC000-CFFFF, bits 7:6 (10 = REn, 01 = WEn) bits 1:0 for
48          * C0000-C3FFF etc.
49          */
50         pci_write_config8(dev, 0x80, 0xff);
51         /* For page D0000-DFFFF */
52         pci_write_config8(dev, 0x81, 0xff);
53         /* For page E0000-EFFFF */
54         pci_write_config8(dev, 0x82, 0xff);
55         pci_write_config8(dev, 0x83, 0x30);
56
57         msr = rdmsr(TOP_MEM);
58         reg = pci_read_config16(dev, 0x84);
59         reg &= 0xf;
60         pci_write_config16(dev, 0x84, (msr.lo >> 16) | reg);
61
62         reg = pci_read_config16(dev, 0x88);
63         reg &= 0xf800;
64
65         /* The Address Next to the Last Valid DRAM Address */
66         pci_write_config16(dev, 0x88, (msr.lo >> 24) | reg);
67
68 }
69
70 static void dram_enable_k8m890(struct device *dev)
71 {
72         dram_enable(dev);
73
74         /* enable VGA, so the bridges gets VGA_EN and resources are set */
75         pci_write_config8(dev, 0xa1, 0x80);
76 }
77
78 static struct resource *resmax;
79
80 static void get_memres(void *gp, struct device *dev, struct resource *res)
81 {
82         unsigned int *fbsize = (unsigned int *) gp;
83         uint64_t proposed_base = res->base + res->size - *fbsize;
84
85         printk(BIOS_DEBUG, "get_memres: res->base=%llx res->size=%llx %d %d %d\n",
86                         res->base, res->size, (res->size > *fbsize), 
87                         (!(proposed_base & (*fbsize - 1))),
88                         (proposed_base < ((uint64_t) 0xffffffff)));
89
90         /* if we fit and also align OK, and must be below 4GB */
91         if ((res->size > *fbsize) && (!(proposed_base & (*fbsize - 1))) && 
92                 (proposed_base < ((uint64_t) 0xffffffff) )) {
93                 resmax = res;
94         }
95 #if CONFIG_WRITE_HIGH_TABLES==1
96 /* in arch/i386/boot/tables.c */
97 extern uint64_t high_tables_base, high_tables_size;
98
99         if ((high_tables_base) && ((high_tables_base > proposed_base) &&
100                         (high_tables_base < (res->base + res->size)))) {
101                 high_tables_base = proposed_base - high_tables_size;
102                 printk(BIOS_DEBUG, "Moving the high_tables_base pointer to "
103                                 "new base %llx\n", high_tables_base);
104         }
105 #endif
106 }
107
108 /*
109  *
110  */
111 int
112 k8m890_host_fb_size_get(void)
113 {
114         struct device *dev = dev_find_device(PCI_VENDOR_ID_VIA,
115                                              PCI_DEVICE_ID_VIA_K8M890CE_3, 0);
116         unsigned char tmp;
117
118         tmp = pci_read_config8(dev, 0xA1);
119         tmp >>= 4;
120         if (tmp & 0x08)
121                 return 4 << (tmp & 7);
122         else
123                 return 0;
124 }
125
126 static void dram_init_fb(struct device *dev)
127 {
128         /* Important bits:
129          * Enable the internal GFX bit 7 of reg 0xa1 plus in same reg:
130          * bits 6:4 X fbuffer size will be  2^(X+2) or 100 = 64MB, 101 = 128MB
131          * bits 3:0 BASE [31:28]
132          * reg 0xa0 bits 7:1 BASE [27:21] bit0 enable CPU access
133          */
134         u8 tmp;
135         uint64_t proposed_base;
136         unsigned int fbbits = 0;
137         unsigned int fbsize;
138         int ret;
139
140
141         ret = get_option(&fbbits, "videoram_size");
142         if (ret) {
143                 printk(BIOS_WARNING, "Failed to get videoram size (error %d), using default.\n", ret);
144                 fbbits = 5;
145         }
146
147         if ((fbbits < 1) || (fbbits > 7)) {
148                 printk(BIOS_WARNING, "Invalid videoram size (%d), using default.\n",
149                                4 << fbbits);
150                 fbbits = 5;
151         }
152
153         fbsize = 4 << (fbbits + 20);
154
155         resmax = NULL;
156         search_global_resources(
157                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
158                 get_memres, (void *) &fbsize);
159
160         /* no space for FB */
161         if (!resmax) {
162                 printk(BIOS_ERR, "VIA FB: no space for framebuffer in RAM\n");
163                 return;
164         }
165
166         proposed_base = resmax->base + resmax->size - fbsize;
167         resmax->size -= fbsize;
168
169         printk(BIOS_INFO, "K8M890: Using a %dMB framebuffer.\n", 4 << fbbits);
170
171         /* Step 1: enable UMA but no FB */
172         pci_write_config8(dev, 0xa1, 0x80);
173
174         /* Step 2: enough is just the FB size, the CPU accessible address is not needed */
175         tmp = (fbbits << 4) | 0x80;
176         pci_write_config8(dev, 0xa1, tmp);
177
178         /* TODO K8 needs some UMA fine tuning too maybe call some generic routine here? */
179 }
180
181 static const struct device_operations dram_ops_t = {
182         .read_resources         = pci_dev_read_resources,
183         .set_resources          = pci_dev_set_resources,
184         .enable_resources       = pci_dev_enable_resources,
185         .enable                 = dram_enable,
186         .ops_pci                = 0,
187 };
188
189 static const struct device_operations dram_ops_m = {
190         .read_resources         = pci_dev_read_resources,
191         .set_resources          = pci_dev_set_resources,
192         .enable_resources       = pci_dev_enable_resources,
193         .enable                 = dram_enable_k8m890,
194         .init                   = dram_init_fb,
195         .ops_pci                = 0,
196 };
197
198 static const struct pci_driver northbridge_driver_t __pci_driver = {
199         .ops    = &dram_ops_t,
200         .vendor = PCI_VENDOR_ID_VIA,
201         .device = PCI_DEVICE_ID_VIA_K8T890CE_3,
202 };
203
204 static const struct pci_driver northbridge_driver_m __pci_driver = {
205         .ops    = &dram_ops_m,
206         .vendor = PCI_VENDOR_ID_VIA,
207         .device = PCI_DEVICE_ID_VIA_K8M890CE_3,
208 };