Factor out a few commonly duplicated functions from northbridge.c.
[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 #ifdef UNUSED_CODE
165 static void ram_reservation(device_t dev, unsigned long index,
166                          unsigned long base, unsigned long size)
167 {
168         struct resource *res;
169
170         printk(BIOS_SPEW, "Configuring Via C3 LAPIC Fixed Resource\n");
171         /* Fixed LAPIC resource */
172         res = new_resource(dev, 1);
173         res->base = (resource_t) base;
174         res->size = size;
175         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
176                      IORESOURCE_STORED | IORESOURCE_ASSIGNED;
177 }
178 #endif
179
180 #if CONFIG_WRITE_HIGH_TABLES==1
181 /* maximum size of high tables in KB */
182 #define HIGH_TABLES_SIZE 64
183 extern uint64_t high_tables_base, high_tables_size;
184 #endif
185
186 static void cn400_domain_set_resources(device_t dev)
187 {
188         device_t mc_dev;
189         u32 pci_tolm;
190
191         printk(BIOS_SPEW, "Entering %s.\n", __func__);
192
193         pci_tolm = find_pci_tolm(dev->link_list);
194         mc_dev = dev_find_device(PCI_VENDOR_ID_VIA,
195                                  PCI_DEVICE_ID_VIA_CN400_MEMCTRL, 0);
196
197         if (mc_dev) {
198                 unsigned long tomk, tolmk;
199                 unsigned char rambits;
200                 int idx;
201
202                 rambits = pci_read_config8(mc_dev, 0x47);
203                 tomk = rambits * 32 * 1024;
204                 /* Compute the Top Of Low Memory (TOLM), in Kb. */
205                 tolmk = pci_tolm >> 10;
206                 printk(BIOS_SPEW, "tomk is 0x%lx, tolmk is 0x%08lX\n", tomk, tolmk);
207                 if (tolmk >= tomk) {
208                         /* The PCI hole does does not overlap the memory. */
209                         tolmk = tomk;
210                 }
211
212 #if CONFIG_WRITE_HIGH_TABLES == 1
213                 /* Locate the High Tables at the Top of Low Memory below the Video RAM */
214                 high_tables_base = (uint64_t) (tolmk - (CONFIG_VIDEO_MB *1024) - HIGH_TABLES_SIZE) * 1024;
215                 high_tables_size = (uint64_t) HIGH_TABLES_SIZE* 1024;
216                 printk(BIOS_SPEW, "tom: %lx, high_tables_base: %llx, high_tables_size: %llx\n", tomk*1024, high_tables_base, high_tables_size);
217 #endif
218
219                 /* Report the memory regions. */
220                 idx = 10;
221                 /* TODO: Hole needed? */
222                 ram_resource(dev, idx++, 0, 640);       /* First 640k */
223                 /* Leave a hole for VGA, 0xa0000 - 0xc0000 */
224                 ram_resource(dev, idx++, 768,
225                              (tolmk - 768 - CONFIG_VIDEO_MB * 1024));
226         }
227         assign_resources(dev->link_list);
228
229         printk(BIOS_SPEW, "Leaving %s.\n", __func__);
230 }
231
232 static unsigned int cn400_domain_scan_bus(device_t dev, unsigned int max)
233 {
234         printk(BIOS_DEBUG, "Entering %s.\n", __func__);
235
236         max = pci_scan_bus(dev->link_list, PCI_DEVFN(0, 0), 0xff, max);
237         return max;
238 }
239
240 static struct device_operations pci_domain_ops = {
241         .read_resources   = cn400_domain_read_resources,
242         .set_resources    = cn400_domain_set_resources,
243         .enable_resources = NULL,
244         .init             = NULL,
245         .scan_bus         = cn400_domain_scan_bus,
246 };
247
248 static void cpu_bus_init(device_t dev)
249 {
250         initialize_cpus(dev->link_list);
251 }
252
253 static void cpu_bus_noop(device_t dev)
254 {
255 }
256
257 static struct device_operations cpu_bus_ops = {
258         .read_resources   = cpu_bus_noop,
259         .set_resources    = cpu_bus_noop,
260         .enable_resources = cpu_bus_noop,
261         .init             = cpu_bus_init,
262         .scan_bus         = 0,
263 };
264
265 static void enable_dev(struct device *dev)
266 {
267         printk(BIOS_SPEW, "CN400: enable_dev for device %s.\n", dev_path(dev));
268
269         /* Set the operations if it is a special bus type. */
270         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
271                 dev->ops = &pci_domain_ops;
272                 pci_set_method(dev);
273         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
274                 dev->ops = &cpu_bus_ops;
275         }
276 }
277
278 struct chip_operations northbridge_via_cn400_ops = {
279         CHIP_NAME("VIA CN400 Northbridge")
280         .enable_dev = enable_dev,
281 };