Add initial support for the Intel 82810 northbridge.
[coreboot.git] / src / northbridge / intel / i82810 / northbridge.c
1 /*
2  * This file is part of the LinuxBIOS 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 "chip.h"
31 #include "northbridge.h"
32 #include "i82810.h"
33
34 static void northbridge_init(device_t dev) 
35 {
36         printk_spew("Northbridge init\n");
37 }
38
39 static struct device_operations northbridge_operations = {
40         .read_resources   = pci_dev_read_resources,
41         .set_resources    = pci_dev_set_resources,
42         .enable_resources = pci_dev_enable_resources,
43         .init             = northbridge_init,
44         .enable           = 0,
45         .ops_pci          = 0,
46 };
47
48 static struct pci_driver northbridge_driver __pci_driver = {
49         .ops = &northbridge_operations,
50         .vendor = PCI_VENDOR_ID_INTEL,
51         .device = 0x7120,
52 };
53
54 #define BRIDGE_IO_MASK (IORESOURCE_IO | IORESOURCE_MEM)
55
56 static void pci_domain_read_resources(device_t dev)
57 {
58         struct resource *resource;
59         unsigned reg;
60
61         /* Initialize the system wide io space constraints */
62         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
63         resource->base = 0x400;
64         resource->limit = 0xffffUL;
65         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
66
67         /* Initialize the system wide memory resources constraints */
68         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
69         resource->limit = 0xffffffffULL;
70         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
71 }
72
73 static void ram_resource(device_t dev, unsigned long index,
74         unsigned long basek, unsigned long sizek)
75 {
76         struct resource *resource;
77
78         if (!sizek) {
79                 return;
80         }
81         resource = new_resource(dev, index);
82         resource->base  = ((resource_t)basek) << 10;
83         resource->size  = ((resource_t)sizek) << 10;
84         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
85                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
86 }
87
88 static void tolm_test(void *gp, struct device *dev, struct resource *new)
89 {
90         struct resource **best_p = gp;
91         struct resource *best;
92         best = *best_p;
93         if (!best || (best->base > new->base)) {
94                 best = new;
95         }
96         *best_p = best;
97 }
98
99 static uint32_t find_pci_tolm(struct bus *bus)
100 {
101         struct resource *min;
102         uint32_t tolm;
103         min = 0;
104         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
105         tolm = 0xffffffffUL;
106         if (min && tolm > min->base) {
107                 tolm = min->base;
108         }
109         return tolm;
110 }
111
112 /* Table which returns the RAM size in MB when fed the DRP[7:4] or [3:0] value.
113  * Note that 2 is a value which the DRP should never be programmed to.
114  * Some size values appear twice, due to single-sided vs dual-sided banks.
115  */
116 static int translate_i82810_to_mb[] = {
117 /* DRP  0  1 (2) 3   4   5   6   7   8   9   A   B   C    D    E    F */
118 /* MB */0, 8, 0, 16, 16, 24, 32, 32, 48, 64, 64, 96, 128, 128, 192, 256,
119 };
120
121 static void pci_domain_set_resources(device_t dev)
122 {
123         device_t mc_dev;
124         uint32_t pci_tolm;
125
126         pci_tolm = find_pci_tolm(&dev->link[0]);
127         mc_dev = dev->link[0].children;
128
129         if (mc_dev) {
130                 /* Figure out which areas are/should be occupied by RAM.
131                  * This is all computed in kilobytes and converted to/from
132                  * the memory controller right at the edges.
133                  * Having different variables in different units is
134                  * too confusing to get right.  Kilobytes are good up to
135                  * 4 Terabytes of RAM...
136                  */
137                 unsigned long tomk, tolmk;
138                 int idx;
139                 int drp_value;
140
141                 /* First get the value for DIMM 0. */
142                 drp_value = pci_read_config8(mc_dev, DRP);
143                 /* Translate it to MB and add to tomk. */
144                 tomk = (unsigned long)(translate_i82810_to_mb[drp_value & 0xf]);
145                 /* Now do the same for DIMM 1. */
146                 drp_value = drp_value >> 4; // >>= 4; //? mess with later
147                 tomk += (unsigned long)(translate_i82810_to_mb[drp_value]);
148
149                 printk_debug("Setting RAM size to %d MB\n", tomk);
150
151                 /* Convert tomk from MB to KB. */
152                 tomk = tomk << 10;
153
154                 /* Compute the top of Low memory. */
155                 tolmk = pci_tolm >> 10;
156                 if (tolmk >= tomk) {
157                         /* The PCI hole does does not overlap the memory. */
158                         tolmk = tomk;
159                 }
160
161                 /* Report the memory regions. */
162                 idx = 10;
163                 ram_resource(dev, idx++, 0, 640);
164                 ram_resource(dev, idx++, 1024, tolmk - 1024);
165         }
166         assign_resources(&dev->link[0]);
167 }
168
169 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
170 {
171         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
172         return max;
173 }
174
175 static struct device_operations pci_domain_ops = {
176         .read_resources   = pci_domain_read_resources,
177         .set_resources    = pci_domain_set_resources,
178         .enable_resources = enable_childrens_resources,
179         .init             = 0,
180         .scan_bus         = pci_domain_scan_bus,
181 };
182
183 static void cpu_bus_init(device_t dev)
184 {
185         initialize_cpus(&dev->link[0]);
186 }
187
188 static void cpu_bus_noop(device_t dev)
189 {
190 }
191
192 static struct device_operations cpu_bus_ops = {
193         .read_resources   = cpu_bus_noop,
194         .set_resources    = cpu_bus_noop,
195         .enable_resources = cpu_bus_noop,
196         .init             = cpu_bus_init,
197         .scan_bus         = 0,
198 };
199
200 static void enable_dev(struct device *dev)
201 {
202         struct device_path path;
203
204         /* Set the operations if it is a special bus type */
205         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
206                 dev->ops = &pci_domain_ops;
207                 pci_set_method(dev);
208         }
209         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
210                 dev->ops = &cpu_bus_ops;
211         }
212 }
213
214 struct chip_operations northbridge_intel_i82810_ops = {
215         CHIP_NAME("Intel 82810 Northbridge")
216         .enable_dev = enable_dev,
217 };