de979d9d277643b5f4a53be3f076bcd56e48812c
[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.osgood@gmail.com>
5  * Copyright (C) 2010 Joseph Smith <joe@settoplinux.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <console/console.h>
23 #include <arch/io.h>
24 #include <stdint.h>
25 #include <device/device.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <bitops.h>
31 #include <cpu/cpu.h>
32 #include "chip.h"
33 #include <boot/tables.h>
34 #include "northbridge.h"
35 #include "i82810.h"
36
37 static void northbridge_init(device_t dev)
38 {
39         printk(BIOS_SPEW, "Northbridge init\n");
40 }
41
42 static struct device_operations northbridge_operations = {
43         .read_resources         = pci_dev_read_resources,
44         .set_resources          = pci_dev_set_resources,
45         .enable_resources       = pci_dev_enable_resources,
46         .init                   = northbridge_init,
47         .enable                 = 0,
48         .ops_pci                = 0,
49 };
50
51 /* Intel 82810/82810-DC100 */
52 static const struct pci_driver i810_northbridge_driver __pci_driver = {
53         .ops    = &northbridge_operations,
54         .vendor = PCI_VENDOR_ID_INTEL,
55         .device = 0x7120,
56 };
57
58 /* Intel 82810E */
59 static const struct pci_driver i810e_northbridge_driver __pci_driver = {
60         .ops    = &northbridge_operations,
61         .vendor = PCI_VENDOR_ID_INTEL,
62         .device = 0x7124,
63 };
64
65 static void ram_resource(device_t dev, unsigned long index,
66                          unsigned long basek, unsigned long sizek)
67 {
68         struct resource *resource;
69
70         if (!sizek) {
71                 return;
72         }
73         resource = new_resource(dev, index);
74         resource->base = ((resource_t) basek) << 10;
75         resource->size = ((resource_t) sizek) << 10;
76         resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE |
77             IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
78 }
79
80 static void tolm_test(void *gp, struct device *dev, struct resource *new)
81 {
82         struct resource **best_p = gp;
83         struct resource *best;
84         best = *best_p;
85         if (!best || (best->base > new->base)) {
86                 best = new;
87         }
88         *best_p = best;
89 }
90
91 static uint32_t find_pci_tolm(struct bus *bus)
92 {
93         struct resource *min;
94         uint32_t tolm;
95         min = 0;
96         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test,
97                              &min);
98         tolm = 0xffffffffUL;
99         if (min && tolm > min->base) {
100                 tolm = min->base;
101         }
102         return tolm;
103 }
104
105 /* IGD UMA memory */
106 uint64_t uma_memory_base=0, uma_memory_size=0;
107
108 int add_northbridge_resources(struct lb_memory *mem)
109 {
110         printk(BIOS_DEBUG, "Adding IGD UMA memory area\n");
111         lb_add_memory_range(mem, LB_MEM_RESERVED,
112                 uma_memory_base, uma_memory_size);
113
114         return 0;
115 }
116
117 /* Table which returns the RAM size in MB when fed the DRP[7:4] or [3:0] value.
118  * Note that 2 is a value which the DRP should never be programmed to.
119  * Some size values appear twice, due to single-sided vs dual-sided banks.
120  */
121 static int translate_i82810_to_mb[] = {
122 /* DRP  0  1 (2) 3   4   5   6   7   8   9   A   B   C    D    E    F */
123 /* MB */0, 8, 0, 16, 16, 24, 32, 32, 48, 64, 64, 96, 128, 128, 192, 256,
124 };
125
126 #if CONFIG_WRITE_HIGH_TABLES==1
127 #define HIGH_TABLES_SIZE 64     // maximum size of high tables in KB
128 extern uint64_t high_tables_base, high_tables_size;
129 #endif
130
131 static void pci_domain_set_resources(device_t dev)
132 {
133         device_t mc_dev;
134         uint32_t pci_tolm;
135         int igd_memory = 0;
136
137         pci_tolm = find_pci_tolm(dev->link_list);
138         mc_dev = dev->link_list->children;
139         if (!mc_dev)
140                 return;
141
142         unsigned long tomk, tolmk;
143         int idx, drp_value;
144         u8 reg8;
145
146         reg8 = pci_read_config8(mc_dev, SMRAM);
147         reg8 &= 0xc0;
148
149         switch (reg8) {
150                 case 0xc0:
151                         igd_memory = 1024;
152                         printk(BIOS_DEBUG, "%dKB IGD UMA\n", igd_memory);
153                         break;
154                 case 0x80:
155                         igd_memory = 512;
156                         printk(BIOS_DEBUG, "%dKB IGD UMA\n", igd_memory);
157                         break;
158                 default:
159                         igd_memory = 0;
160                         printk(BIOS_DEBUG, "No IGD UMA Memory\n");
161                         break;
162         }
163
164         /* Get the value for DIMM 0 and translate it to MB. */
165         drp_value = pci_read_config8(mc_dev, DRP);
166         tomk = (unsigned long)(translate_i82810_to_mb[drp_value & 0x0f]);
167         /* Get the value for DIMM 1 and translate it to MB. */
168         drp_value = drp_value >> 4;
169         tomk += (unsigned long)(translate_i82810_to_mb[drp_value]);
170         /* Convert tomk from MB to KB. */
171         tomk = tomk << 10;
172         tomk -= igd_memory;
173
174         /* For reserving UMA memory in the memory map */
175         uma_memory_base = tomk * 1024ULL;
176         uma_memory_size = igd_memory * 1024ULL;
177         printk(BIOS_DEBUG, "Available memory: %ldKB\n", tomk);
178
179         /* Compute the top of low memory. */
180         tolmk = pci_tolm >> 10;
181         if (tolmk >= tomk) {
182                 /* The PCI hole does does not overlap the memory. */
183                 tolmk = tomk;
184         }
185
186         /* Report the memory regions. */
187         idx = 10;
188         ram_resource(dev, idx++, 0, 640);
189         ram_resource(dev, idx++, 768, tolmk - 768);
190
191 #if CONFIG_WRITE_HIGH_TABLES==1
192         /* Leave some space for ACPI, PIRQ and MP tables */
193         high_tables_base = (tomk - HIGH_TABLES_SIZE) * 1024;
194         high_tables_size = HIGH_TABLES_SIZE * 1024;
195 #endif
196         assign_resources(dev->link_list);
197 }
198
199 static struct device_operations pci_domain_ops = {
200         .read_resources         = pci_domain_read_resources,
201         .set_resources          = pci_domain_set_resources,
202         .enable_resources       = NULL,
203         .init                   = NULL,
204         .scan_bus               = pci_domain_scan_bus,
205 };
206
207 static void cpu_bus_init(device_t dev)
208 {
209         initialize_cpus(dev->link_list);
210 }
211
212 static void cpu_bus_noop(device_t dev)
213 {
214 }
215
216 static struct device_operations cpu_bus_ops = {
217         .read_resources         = cpu_bus_noop,
218         .set_resources          = cpu_bus_noop,
219         .enable_resources       = cpu_bus_noop,
220         .init                   = cpu_bus_init,
221         .scan_bus               = 0,
222 };
223
224 static void enable_dev(struct device *dev)
225 {
226         /* Set the operations if it is a special bus type */
227         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
228                 dev->ops = &pci_domain_ops;
229                 pci_set_method(dev);
230         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
231                 dev->ops = &cpu_bus_ops;
232         }
233 }
234
235 struct chip_operations northbridge_intel_i82810_ops = {
236         CHIP_NAME("Intel 82810 Northbridge")
237         .enable_dev = enable_dev,
238 };