Convert the MSI MS-6178 board to CBFS.
[coreboot.git] / src / northbridge / intel / i82810 / northbridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Corey Osgood <corey@slightlyhackish.com>
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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <console/console.h>
22 #include <arch/io.h>
23 #include <stdint.h>
24 #include <device/device.h>
25 #include <device/pci.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 "chip.h"
32 #include "northbridge.h"
33 #include "i82810.h"
34
35 static void northbridge_init(device_t dev)
36 {
37         printk_spew("Northbridge init\n");
38 }
39
40 static struct device_operations northbridge_operations = {
41         .read_resources         = pci_dev_read_resources,
42         .set_resources          = pci_dev_set_resources,
43         .enable_resources       = pci_dev_enable_resources,
44         .init                   = northbridge_init,
45         .enable                 = 0,
46         .ops_pci                = 0,
47 };
48
49 static const struct pci_driver northbridge_driver __pci_driver = {
50         .ops    = &northbridge_operations,
51         .vendor = PCI_VENDOR_ID_INTEL,
52         .device = 0x7120,
53 };
54
55 #define BRIDGE_IO_MASK (IORESOURCE_IO | IORESOURCE_MEM)
56
57 static void pci_domain_read_resources(device_t dev)
58 {
59         struct resource *resource;
60         unsigned reg;
61
62         /* Initialize the system wide io space constraints */
63         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
64         resource->base = 0x400;
65         resource->limit = 0xffffUL;
66         resource->flags =
67             IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
68
69         /* Initialize the system wide memory resources constraints */
70         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
71         resource->limit = 0xffffffffULL;
72         resource->flags =
73             IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
74 }
75
76 static void ram_resource(device_t dev, unsigned long index,
77                          unsigned long basek, unsigned long sizek)
78 {
79         struct resource *resource;
80
81         if (!sizek) {
82                 return;
83         }
84         resource = new_resource(dev, index);
85         resource->base = ((resource_t) basek) << 10;
86         resource->size = ((resource_t) sizek) << 10;
87         resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE |
88             IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
89 }
90
91 static void tolm_test(void *gp, struct device *dev, struct resource *new)
92 {
93         struct resource **best_p = gp;
94         struct resource *best;
95         best = *best_p;
96         if (!best || (best->base > new->base)) {
97                 best = new;
98         }
99         *best_p = best;
100 }
101
102 static uint32_t find_pci_tolm(struct bus *bus)
103 {
104         struct resource *min;
105         uint32_t tolm;
106         min = 0;
107         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test,
108                              &min);
109         tolm = 0xffffffffUL;
110         if (min && tolm > min->base) {
111                 tolm = min->base;
112         }
113         return tolm;
114 }
115
116 /* Table which returns the RAM size in MB when fed the DRP[7:4] or [3:0] value.
117  * Note that 2 is a value which the DRP should never be programmed to.
118  * Some size values appear twice, due to single-sided vs dual-sided banks.
119  */
120 static int translate_i82810_to_mb[] = {
121 /* DRP  0  1 (2) 3   4   5   6   7   8   9   A   B   C    D    E    F */
122 /* MB */0, 8, 0, 16, 16, 24, 32, 32, 48, 64, 64, 96, 128, 128, 192, 256,
123 };
124
125 #if HAVE_HIGH_TABLES==1
126 #define HIGH_TABLES_SIZE 64     // maximum size of high tables in KB
127 extern uint64_t high_tables_base, high_tables_size;
128 #endif
129
130 static void pci_domain_set_resources(device_t dev)
131 {
132         device_t mc_dev;
133         uint32_t pci_tolm;
134
135         pci_tolm = find_pci_tolm(&dev->link[0]);
136         mc_dev = dev->link[0].children;
137
138         if (mc_dev) {
139                 /* Figure out which areas are/should be occupied by RAM.
140                  * This is all computed in kilobytes and converted to/from
141                  * the memory controller right at the edges.
142                  * Having different variables in different units is
143                  * too confusing to get right.  Kilobytes are good up to
144                  * 4 Terabytes of RAM...
145                  */
146                 unsigned long tomk, tolmk;
147                 int idx;
148                 int drp_value;
149
150                 /* First get the value for DIMM 0. */
151                 drp_value = pci_read_config8(mc_dev, DRP);
152                 /* Translate it to MB and add to tomk. */
153                 tomk = (unsigned long)(translate_i82810_to_mb[drp_value & 0xf]);
154                 /* Now do the same for DIMM 1. */
155                 drp_value = drp_value >> 4;     // >>= 4; //? mess with later
156                 tomk += (unsigned long)(translate_i82810_to_mb[drp_value]);
157
158                 printk_debug("Setting RAM size to %d MB\n", tomk);
159
160                 /* Convert tomk from MB to KB. */
161                 tomk = tomk << 10;
162
163                 /* Compute the top of Low memory. */
164                 tolmk = pci_tolm >> 10;
165                 if (tolmk >= tomk) {
166                         /* The PCI hole does does not overlap the memory. */
167                         tolmk = tomk;
168                 }
169
170                 /* Report the memory regions. */
171                 idx = 10;
172                 ram_resource(dev, idx++, 0, 640);
173                 ram_resource(dev, idx++, 768, tolmk - 768);
174
175 #if HAVE_HIGH_TABLES==1
176                 /* Leave some space for ACPI, PIRQ and MP tables */
177                 high_tables_base = (tomk - HIGH_TABLES_SIZE) * 1024;
178                 high_tables_size = HIGH_TABLES_SIZE * 1024;
179 #endif
180         }
181         assign_resources(&dev->link[0]);
182 }
183
184 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
185 {
186         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
187         return max;
188 }
189
190 static struct device_operations pci_domain_ops = {
191         .read_resources         = pci_domain_read_resources,
192         .set_resources          = pci_domain_set_resources,
193         .enable_resources       = enable_childrens_resources,
194         .init                   = 0,
195         .scan_bus               = pci_domain_scan_bus,
196 };
197
198 static void cpu_bus_init(device_t dev)
199 {
200         initialize_cpus(&dev->link[0]);
201 }
202
203 static void cpu_bus_noop(device_t dev)
204 {
205 }
206
207 static struct device_operations cpu_bus_ops = {
208         .read_resources         = cpu_bus_noop,
209         .set_resources          = cpu_bus_noop,
210         .enable_resources       = cpu_bus_noop,
211         .init                   = cpu_bus_init,
212         .scan_bus               = 0,
213 };
214
215 static void enable_dev(struct device *dev)
216 {
217         struct device_path path;
218
219         /* Set the operations if it is a special bus type */
220         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
221                 dev->ops = &pci_domain_ops;
222                 pci_set_method(dev);
223         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
224                 dev->ops = &cpu_bus_ops;
225         }
226 }
227
228 struct chip_operations northbridge_intel_i82810_ops = {
229         CHIP_NAME("Intel 82810 Northbridge")
230         .enable_dev = enable_dev,
231 };