This patch unifies the use of config options in v2 to all start with CONFIG_
[coreboot.git] / src / northbridge / via / cx700 / northbridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2009 coresystems GmbH
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 <console/console.h>
21 #include <arch/io.h>
22 #include <stdint.h>
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <device/hypertransport.h>
26 #include <device/pci_ids.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <bitops.h>
30 #include <cpu/cpu.h>
31 #include <cpu/x86/mtrr.h>
32 #include "chip.h"
33 #include "northbridge.h"
34
35 static void pci_domain_read_resources(device_t dev)
36 {
37         struct resource *resource;
38
39         /* Initialize the system wide io space constraints */
40         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
41         resource->limit = 0xffffUL;
42         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
43
44         /* Initialize the system wide memory resources constraints */
45         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
46         resource->limit = 0xffffffffULL;
47         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
48 }
49
50 static void ram_resource(device_t dev, unsigned long index,
51                          unsigned long basek, unsigned long sizek)
52 {
53         struct resource *resource;
54
55         if (!sizek) {
56                 return;
57         }
58         resource = new_resource(dev, index);
59         resource->base = ((resource_t) basek) << 10;
60         resource->size = ((resource_t) sizek) << 10;
61         resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE |
62             IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
63 }
64
65 static void tolm_test(void *gp, struct device *dev, struct resource *new)
66 {
67         struct resource **best_p = gp;
68         struct resource *best;
69         best = *best_p;
70         if (!best || (best->base > new->base)) {
71                 best = new;
72         }
73         *best_p = best;
74 }
75
76 static u32 find_pci_tolm(struct bus *bus)
77 {
78         struct resource *min = NULL;
79         u32 tolm;
80
81         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
82         tolm = 0xffffffffUL;
83         if (min && tolm > min->base) {
84                 tolm = min->base;
85         }
86
87         return tolm;
88 }
89
90 #if CONFIG_HAVE_HIGH_TABLES==1
91 /* maximum size of high tables in KB */
92 #define HIGH_TABLES_SIZE 64
93 extern uint64_t high_tables_base, high_tables_size;
94 #endif
95
96 static void pci_domain_set_resources(device_t dev)
97 {
98         device_t mc_dev;
99         u32 pci_tolm;
100         unsigned char reg;
101         unsigned long tomk, tolmk;
102         unsigned char rambits;
103         int idx;
104
105         pci_tolm = find_pci_tolm(&dev->link[0]);
106         mc_dev = dev_find_device(PCI_VENDOR_ID_VIA, 0x3324, 0);
107
108         rambits = pci_read_config8(mc_dev, 0x88);
109         rambits >>= 2;
110
111         /* Get memory size and frame buffer from northbridge's registers.
112          *
113          * If register contains an invalid value we set frame buffer size to a
114          * default of 32M, but that probably won't happen.
115          */
116         reg = pci_read_config8(mc_dev, 0xa1);
117         reg &= 0x70;
118         reg = reg >> 4;
119
120         /* TOP 1M SMM Memory */
121         if (reg == 0x0 || reg == 0x6 || reg == 0x7)
122                 tomk = (((rambits << 6) - 32 - 1) * 1024);      // Set frame buffer 32M for default
123         else
124                 tomk = (((rambits << 6) - (4 << reg) - 1) * 1024);
125
126         /* Compute the top of Low memory */
127         tolmk = pci_tolm >> 10;
128         if (tolmk >= tomk) {
129                 /* The PCI hole does does not overlap the memory. */
130                 tolmk = tomk;
131                 tolmk -= 1024;  // TOP 1M SM Memory
132         }
133
134 #if CONFIG_HAVE_HIGH_TABLES == 1
135         high_tables_base = (tolmk - HIGH_TABLES_SIZE) * 1024;
136         high_tables_size = HIGH_TABLES_SIZE* 1024;
137         printk_debug("tom: %lx, high_tables_base: %llx, high_tables_size: %llx\n", tomk*1024, high_tables_base, high_tables_size);
138 #endif
139
140         /* Report the memory regions */
141         idx = 10;
142
143         /* TODO: Hole needed? Should this go elsewhere? */
144         ram_resource(dev, idx++, 0, 640);       /* first 640k */
145         ram_resource(dev, idx++, 768, (tolmk - 768));   /* leave a hole for vga */
146         assign_resources(&dev->link[0]);
147 }
148
149 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
150 {
151         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
152         return max;
153 }
154
155 static struct device_operations pci_domain_ops = {
156         .read_resources   = pci_domain_read_resources,
157         .set_resources    = pci_domain_set_resources,
158         .enable_resources = enable_childrens_resources,
159         .init             = 0,
160         .scan_bus         = pci_domain_scan_bus,
161 };
162
163 static void cpu_bus_init(device_t dev)
164 {
165         initialize_cpus(&dev->link[0]);
166 }
167
168 static void cpu_bus_noop(device_t dev)
169 {
170 }
171
172 static struct device_operations cpu_bus_ops = {
173         .read_resources   = cpu_bus_noop,
174         .set_resources    = cpu_bus_noop,
175         .enable_resources = cpu_bus_noop,
176         .init             = cpu_bus_init,
177         .scan_bus         = 0,
178 };
179
180 static void enable_dev(struct device *dev)
181 {
182         /* Our wonderful device model */
183         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
184                 dev->ops = &pci_domain_ops;
185                 pci_set_method(dev);
186         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
187                 dev->ops = &cpu_bus_ops;
188         }
189 }
190
191 struct chip_operations northbridge_via_cx700_ops = {
192         CHIP_NAME("VIA CX700 Northbridge")
193         .enable_dev = enable_dev
194 };