Following patch adds K8M890 support. It initializes the AGP and graphics UMA.
[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 v2 as published by
8  * the Free Software Foundation.
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 <bitops.h>
27 #include "k8t890.h"
28
29 static void dram_enable(struct device *dev)
30 {
31         msr_t msr;
32         u16 reg;
33
34         /*
35          * Enable Lowest Interrupt arbitration for APIC, enable NB APIC
36          * decoding, MSI support, no SMRAM, compatible SMM.
37          */
38         pci_write_config8(dev, 0x86, 0x39);
39
40         /*
41          * We want to use the 0xC0000-0xEFFFF as RAM mark area as RW, even if
42          * memory is doing K8 the DMA from SB will fail if we have it wrong,
43          * AND even we have it here, we must later copy it to SB to make it work :/
44          */
45
46         /* For CC000-CFFFF, bits 7:6 (10 = REn, 01 = WEn) bits 1:0 for
47          * C0000-C3FFF etc.
48          */
49         pci_write_config8(dev, 0x80, 0xff);
50         /* For page D0000-DFFFF */
51         pci_write_config8(dev, 0x81, 0xff);
52         /* For page E0000-EFFFF */
53         pci_write_config8(dev, 0x82, 0xff);
54         pci_write_config8(dev, 0x83, 0x30);
55
56         msr = rdmsr(TOP_MEM);
57         reg = pci_read_config16(dev, 0x84);
58         reg &= 0xf;
59         pci_write_config16(dev, 0x84, (msr.lo >> 16) | reg);
60
61         reg = pci_read_config16(dev, 0x88);
62         reg &= 0xf800;
63
64         /* The Address Next to the Last Valid DRAM Address */
65         pci_write_config16(dev, 0x88, (msr.lo >> 24) | reg);
66 }
67
68 static struct resource *resmax;
69
70 static void get_memres(void *gp, struct device *dev, struct resource *res)
71 {
72         unsigned int *fbsize = (unsigned int *) gp;
73         uint64_t proposed_base = res->base + res->size - *fbsize;
74
75         printk_debug("get_memres: res->base=%llx res->size=%llx %d %d %d\n",
76                         res->base, res->size, (res->size > *fbsize), 
77                         (!(proposed_base & (*fbsize - 1))),
78                         (proposed_base < ((uint64_t) 0xffffffff)));
79
80         /* if we fit and also align OK, and must be below 4GB */
81         if ((res->size > *fbsize) && (!(proposed_base & (*fbsize - 1))) && 
82                 (proposed_base < ((uint64_t) 0xffffffff) )) {
83                 resmax = res;
84         }
85 }
86
87
88 static void dram_init_fb(struct device *dev)
89 {
90         /* Important bits:
91          * Enable the internal GFX bit 7 of reg 0xa1 plus in same reg:
92          * bits 6:4 X fbuffer size will be  2^(X+2) or 100 = 64MB, 101 = 128MB 
93          * bits 3:0 BASE [31:28]
94          * reg 0xa0 bits 7:1 BASE [27:21] bit0 enable CPU access
95          */
96         u8 tmp;
97         uint64_t proposed_base;
98         unsigned int fbsize = (K8M890_FBSIZEMB * 1024 * 1024);
99
100         resmax = NULL;
101         search_global_resources(
102                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
103                 get_memres, (void *) &fbsize);
104
105         /* no space for FB */
106         if (!resmax) {
107                 printk_err("VIA FB: no space for framebuffer in RAM\n");
108                 return;
109         }
110
111         proposed_base = resmax->base + resmax->size - fbsize;
112         resmax->size -= fbsize;
113
114         printk_debug("VIA FB proposed base: %llx\n", proposed_base);
115
116         /* enable UMA but no FB */
117         pci_write_config8(dev, 0xa1, 0x80);
118
119         /* 27:21 goes to 7:1, 0 is enable CPU access */
120         tmp = (proposed_base >> 20) | 0x1;
121         pci_write_config8(dev, 0xa0, tmp);
122
123         /* 31:28 goes to 3:0 */
124         tmp = ((proposed_base >> 28) & 0xf);
125         tmp = ((log2(K8M890_FBSIZEMB) - 2) << 4);
126         tmp |= 0x80;
127         pci_write_config8(dev, 0xa1, tmp);
128
129         /* TODO K8 needs some UMA fine tuning too maybe call some generic routine here? */
130 }
131
132 static const struct device_operations dram_ops_t = {
133         .read_resources         = pci_dev_read_resources,
134         .set_resources          = pci_dev_set_resources,
135         .enable_resources       = pci_dev_enable_resources,
136         .enable                 = dram_enable,
137         .ops_pci                = 0,
138 };
139
140 static const struct device_operations dram_ops_m = {
141         .read_resources         = pci_dev_read_resources,
142         .set_resources          = pci_dev_set_resources,
143         .enable_resources       = pci_dev_enable_resources,
144         .enable                 = dram_enable,
145         .init                   = dram_init_fb,
146         .ops_pci                = 0,
147 };
148
149 static const struct pci_driver northbridge_driver_t __pci_driver = {
150         .ops    = &dram_ops_t,
151         .vendor = PCI_VENDOR_ID_VIA,
152         .device = PCI_DEVICE_ID_VIA_K8T890CE_3,
153 };
154
155 static const struct pci_driver northbridge_driver_m __pci_driver = {
156         .ops    = &dram_ops_m,
157         .vendor = PCI_VENDOR_ID_VIA,
158         .device = PCI_DEVICE_ID_VIA_K8M890CE_3,
159 };