To make use of HAVE_HIGH_TABLES following patch is needed. Also, it moves
[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
69 static void dram_enable_k8m890(struct device *dev)
70 {
71         dram_enable(dev);
72
73         /* enable VGA, so the bridges gets VGA_EN and resources are set */
74         pci_write_config8(dev, 0xa1, 0x80);
75 }
76
77 static struct resource *resmax;
78
79 static void get_memres(void *gp, struct device *dev, struct resource *res)
80 {
81         unsigned int *fbsize = (unsigned int *) gp;
82         uint64_t proposed_base = res->base + res->size - *fbsize;
83
84         printk_debug("get_memres: res->base=%llx res->size=%llx %d %d %d\n",
85                         res->base, res->size, (res->size > *fbsize), 
86                         (!(proposed_base & (*fbsize - 1))),
87                         (proposed_base < ((uint64_t) 0xffffffff)));
88
89         /* if we fit and also align OK, and must be below 4GB */
90         if ((res->size > *fbsize) && (!(proposed_base & (*fbsize - 1))) && 
91                 (proposed_base < ((uint64_t) 0xffffffff) )) {
92                 resmax = res;
93         }
94 #if HAVE_HIGH_TABLES==1
95 /* in arch/i386/boot/tables.c */
96 extern uint64_t high_tables_base, high_tables_size;
97
98         if ((high_tables_base) && ((high_tables_base > proposed_base) &&
99                         (high_tables_base < (res->base + res->size)))) {
100                 high_tables_base = proposed_base - high_tables_size;
101                 printk_debug("Moving the high_tables_base pointer to "
102                                 "new base %llx\n", high_tables_base);
103         }
104 #endif
105 }
106
107
108 static void dram_init_fb(struct device *dev)
109 {
110         /* Important bits:
111          * Enable the internal GFX bit 7 of reg 0xa1 plus in same reg:
112          * bits 6:4 X fbuffer size will be  2^(X+2) or 100 = 64MB, 101 = 128MB 
113          * bits 3:0 BASE [31:28]
114          * reg 0xa0 bits 7:1 BASE [27:21] bit0 enable CPU access
115          */
116         u8 tmp;
117         uint64_t proposed_base;
118         unsigned int fbsize = (K8M890_FBSIZEMB * 1024 * 1024);
119
120         resmax = NULL;
121         search_global_resources(
122                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
123                 get_memres, (void *) &fbsize);
124
125         /* no space for FB */
126         if (!resmax) {
127                 printk_err("VIA FB: no space for framebuffer in RAM\n");
128                 return;
129         }
130
131         proposed_base = resmax->base + resmax->size - fbsize;
132         resmax->size -= fbsize;
133
134         printk_debug("VIA FB proposed base: %llx\n", proposed_base);
135
136         /* Step 1: enable UMA but no FB */
137         pci_write_config8(dev, 0xa1, 0x80);
138
139         /* Step 2: enough is just the FB size, the CPU accessible address is not needed */
140         tmp = ((log2(K8M890_FBSIZEMB) - 2) << 4) | 0x80;
141         pci_write_config8(dev, 0xa1, tmp);
142
143         /* TODO K8 needs some UMA fine tuning too maybe call some generic routine here? */
144 }
145
146 static const struct device_operations dram_ops_t = {
147         .read_resources         = pci_dev_read_resources,
148         .set_resources          = pci_dev_set_resources,
149         .enable_resources       = pci_dev_enable_resources,
150         .enable                 = dram_enable,
151         .ops_pci                = 0,
152 };
153
154 static const struct device_operations dram_ops_m = {
155         .read_resources         = pci_dev_read_resources,
156         .set_resources          = pci_dev_set_resources,
157         .enable_resources       = pci_dev_enable_resources,
158         .enable                 = dram_enable_k8m890,
159         .init                   = dram_init_fb,
160         .ops_pci                = 0,
161 };
162
163 static const struct pci_driver northbridge_driver_t __pci_driver = {
164         .ops    = &dram_ops_t,
165         .vendor = PCI_VENDOR_ID_VIA,
166         .device = PCI_DEVICE_ID_VIA_K8T890CE_3,
167 };
168
169 static const struct pci_driver northbridge_driver_m __pci_driver = {
170         .ops    = &dram_ops_m,
171         .vendor = PCI_VENDOR_ID_VIA,
172         .device = PCI_DEVICE_ID_VIA_K8M890CE_3,
173 };