2fc67c925f8c86d161a43f5691328b1063a71e88
[coreboot.git] / src / northbridge / via / cn400 / northbridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 VIA Technologies, Inc.
5  * (Written by Aaron Lwe <aaron.lwe@gmail.com> for VIA)
6  * Copyright (C) 2007 Corey Osgood <corey.osgood@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include <console/console.h>
24 #include <arch/io.h>
25 #include <stdint.h>
26 #include <device/device.h>
27 #include <device/pci.h>
28 #include <device/pci_ids.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <bitops.h>
32 #include <cpu/cpu.h>
33 #include "chip.h"
34 #include "northbridge.h"
35 #include "cn400.h"
36
37 static void memctrl_init(device_t dev)
38 {
39         device_t vlink_dev;
40         u16 reg16;
41         u8 ranks, pagec, paged, pagee, pagef, shadowreg, reg8;
42         int i, j;
43
44         printk(BIOS_SPEW, "Entering cn400 memctrl_init.\n");
45         /* vlink mirror */
46         vlink_dev = dev_find_device(PCI_VENDOR_ID_VIA,
47                                     PCI_DEVICE_ID_VIA_CN400_VLINK, 0);
48
49         /* Setup Low Memory Top */
50         /* 0x47 == HA(32:25)    */
51         /* 0x84/85 == HA(31:20) << 4 | DRAM Granularity */
52         ranks = pci_read_config8(dev, 0x47);
53         reg16 = (((u16)(ranks - 1) << 9) & 0xFFF0) | 0x01F0;
54
55         pci_write_config16(dev, 0x84, reg16);
56         printk(BIOS_SPEW, "Low Top Address = 0x%04X\n", reg16);
57
58         /* Set up the VGA framebuffer size and Base Address   */
59         /* Note dependencies between agp.c and vga.c and here */
60         reg16 = (log2(CONFIG_VIDEO_MB) << 12) | (1 << 15) | 0xF00;
61         pci_write_config16(dev, 0xa0, reg16);
62
63
64         for (ranks = 0x4b; ranks >= 0x48; ranks--) {
65                 if (pci_read_config8(dev, ranks)) {
66                         ranks -= 0x48;
67                         break;
68                 }
69         }
70         if (ranks == 0x47)
71                 ranks = 0x00;
72         reg16 = 0xaaf0;
73         reg16 |= ranks;
74         /* GMINT Misc. FrameBuffer rank */
75         pci_write_config16(dev, 0xb0, reg16);
76         /* AGPCINT Misc. */
77         pci_write_config8(dev, 0xb8, 0x08);
78
79         /* Arbritation Counters */
80         pci_write_config8(dev, 0xb2, 0xaa);
81
82         /* Write FIFO Setup */
83         pci_write_config8(dev, 0xb3, 0x5a);
84
85         /* Graphics control optimisation */
86         pci_write_config8(dev, 0xb4, 0x0f);
87
88         /* Shadow RAM */
89         pagec = 0xff, paged = 0xff, pagee = 0xff, pagef = 0x30;
90         /* PAGE C, D, E are all read write enable */
91         pci_write_config8(dev, 0x80, pagec);
92         pci_write_config8(dev, 0x81, paged);
93         pci_write_config8(dev, 0x83, pagee);
94         /* PAGE F are read/writable */
95         shadowreg = pci_read_config8(dev, 0x82);
96         shadowreg |= pagef;
97         pci_write_config8(dev, 0x82, shadowreg);
98                 pci_write_config8(vlink_dev, 0x61, pagec);
99                 pci_write_config8(vlink_dev, 0x62, paged);
100                 pci_write_config8(vlink_dev, 0x64, pagee);
101
102                 shadowreg = pci_read_config8(vlink_dev, 0x63);
103                 shadowreg |= pagef;
104                 pci_write_config8(vlink_dev, 0x63, shadowreg);
105
106         /* Activate VGA Frame Buffer */
107
108         reg8 = pci_read_config8(dev, 0xA0);
109         reg8 |= 0x01;
110         pci_write_config8(dev, 0xA0, reg8);
111
112 #ifdef DEBUG_CN400
113         printk(BIOS_SPEW, "%s PCI Header Regs::\n", dev_path(dev));
114
115         for (i = 0 ; i < 16; i++)
116         {
117                 printk(BIOS_SPEW, "%02X: ", i*16);
118                 for (j = 0; j < 16; j++)
119                 {
120                         reg8 = pci_read_config8(dev, j+(i*16));
121                         printk(BIOS_SPEW, "%02X ", reg8);
122                 }
123                 printk(BIOS_SPEW, "\n");
124         }
125 #endif
126         printk(BIOS_SPEW, "Leaving cn400 %s.\n", __func__);
127 }
128
129 static const struct device_operations memctrl_operations = {
130         .read_resources = cn400_noop,
131         .set_resources    = cn400_noop,
132         .enable_resources = cn400_noop,
133         .init           = memctrl_init,
134         .ops_pci          = 0,
135 };
136
137 static const struct pci_driver memctrl_driver __pci_driver = {
138         .ops    = &memctrl_operations,
139         .vendor = PCI_VENDOR_ID_VIA,
140         .device = PCI_DEVICE_ID_VIA_CN400_MEMCTRL,
141 };
142
143 static void cn400_domain_read_resources(device_t dev)
144 {
145         struct resource *resource;
146
147         printk(BIOS_SPEW, "Entering %s.\n", __func__);
148
149         /* Initialize the system wide I/O space constraints. */
150         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
151         resource->limit = 0xffffUL;
152         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
153             IORESOURCE_ASSIGNED;
154
155         /* Initialize the system wide memory resources constraints. */
156         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
157         resource->limit = 0xffffffffULL;
158         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
159             IORESOURCE_ASSIGNED;
160
161         printk(BIOS_SPEW, "Leaving %s.\n", __func__);
162 }
163
164 static void ram_resource(device_t dev, unsigned long index,
165                          unsigned long basek, unsigned long sizek)
166 {
167         struct resource *resource;
168
169         if (!sizek)
170                 return;
171         resource = new_resource(dev, index);
172         resource->base = (resource_t) (basek << 10);
173         resource->size = (resource_t) (sizek << 10);
174         resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE |
175             IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
176 }
177
178 #ifdef UNUSED_CODE
179 static void ram_reservation(device_t dev, unsigned long index,
180                          unsigned long base, unsigned long size)
181 {
182         struct resource *res;
183
184         printk(BIOS_SPEW, "Configuring Via C3 LAPIC Fixed Resource\n");
185         /* Fixed LAPIC resource */
186         res = new_resource(dev, 1);
187         res->base = (resource_t) base;
188         res->size = size;
189         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
190                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
191 }
192 #endif
193
194 static void tolm_test(void *gp, struct device *dev, struct resource *new)
195 {
196         struct resource **best_p = gp;
197         struct resource *best;
198
199         best = *best_p;
200         if (!best || (best->base > new->base))
201                 best = new;
202         *best_p = best;
203 }
204
205 static u32 find_pci_tolm(struct bus *bus)
206 {
207         struct resource *min = NULL;
208         u32 tolm;
209
210         printk(BIOS_SPEW, "Entering CN400 find_pci_tolm\n");
211
212         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
213                              tolm_test, &min);
214         tolm = 0xffffffffUL;
215         if (min && tolm > min->base)
216                 tolm = min->base;
217
218         printk(BIOS_SPEW, "Leaving CN400 find_pci_tolm\n");
219
220         return tolm;
221 }
222
223 #if CONFIG_WRITE_HIGH_TABLES==1
224 /* maximum size of high tables in KB */
225 #define HIGH_TABLES_SIZE 64
226 extern uint64_t high_tables_base, high_tables_size;
227 #endif
228
229 static void cn400_domain_set_resources(device_t dev)
230 {
231         device_t mc_dev;
232         u32 pci_tolm;
233
234         printk(BIOS_SPEW, "Entering %s.\n", __func__);
235
236         pci_tolm = find_pci_tolm(dev->link_list);
237         mc_dev = dev_find_device(PCI_VENDOR_ID_VIA,
238                                  PCI_DEVICE_ID_VIA_CN400_MEMCTRL, 0);
239
240         if (mc_dev) {
241                 unsigned long tomk, tolmk;
242                 unsigned char rambits;
243                 int idx;
244
245                 rambits = pci_read_config8(mc_dev, 0x47);
246                 tomk = rambits * 32 * 1024;
247                 /* Compute the Top Of Low Memory (TOLM), in Kb. */
248                 tolmk = pci_tolm >> 10;
249                 printk(BIOS_SPEW, "tomk is 0x%lx, tolmk is 0x%08lX\n", tomk, tolmk);
250                 if (tolmk >= tomk) {
251                         /* The PCI hole does does not overlap the memory. */
252                         tolmk = tomk;
253                 }
254
255 #if CONFIG_WRITE_HIGH_TABLES == 1
256                 /* Locate the High Tables at the Top of Low Memory below the Video RAM */
257                 high_tables_base = (uint64_t) (tolmk - (CONFIG_VIDEO_MB *1024) - HIGH_TABLES_SIZE) * 1024;
258                 high_tables_size = (uint64_t) HIGH_TABLES_SIZE* 1024;
259                 printk(BIOS_SPEW, "tom: %lx, high_tables_base: %llx, high_tables_size: %llx\n", tomk*1024, high_tables_base, high_tables_size);
260 #endif
261
262                 /* Report the memory regions. */
263                 idx = 10;
264                 /* TODO: Hole needed? */
265                 ram_resource(dev, idx++, 0, 640);       /* First 640k */
266                 /* Leave a hole for VGA, 0xa0000 - 0xc0000 */
267                 ram_resource(dev, idx++, 768,
268                              (tolmk - 768 - CONFIG_VIDEO_MB * 1024));
269         }
270         assign_resources(dev->link_list);
271
272         printk(BIOS_SPEW, "Leaving %s.\n", __func__);
273 }
274
275 static unsigned int cn400_domain_scan_bus(device_t dev, unsigned int max)
276 {
277         printk(BIOS_DEBUG, "Entering %s.\n", __func__);
278
279         max = pci_scan_bus(dev->link_list, PCI_DEVFN(0, 0), 0xff, max);
280         return max;
281 }
282
283 static struct device_operations pci_domain_ops = {
284         .read_resources   = cn400_domain_read_resources,
285         .set_resources    = cn400_domain_set_resources,
286         .enable_resources = NULL,
287         .init             = NULL,
288         .scan_bus         = cn400_domain_scan_bus,
289 };
290
291 static void cpu_bus_init(device_t dev)
292 {
293         initialize_cpus(dev->link_list);
294 }
295
296 static void cpu_bus_noop(device_t dev)
297 {
298 }
299
300 static struct device_operations cpu_bus_ops = {
301         .read_resources   = cpu_bus_noop,
302         .set_resources    = cpu_bus_noop,
303         .enable_resources = cpu_bus_noop,
304         .init             = cpu_bus_init,
305         .scan_bus         = 0,
306 };
307
308 static void enable_dev(struct device *dev)
309 {
310         printk(BIOS_SPEW, "CN400: enable_dev for device %s.\n", dev_path(dev));
311
312         /* Set the operations if it is a special bus type. */
313         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
314                 dev->ops = &pci_domain_ops;
315                 pci_set_method(dev);
316         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
317                 dev->ops = &cpu_bus_ops;
318         }
319 }
320
321 struct chip_operations northbridge_via_cn400_ops = {
322         CHIP_NAME("VIA CN400 Northbridge")
323         .enable_dev = enable_dev,
324 };