This, ladies and gentlement, is commit #4000.
[coreboot.git] / src / northbridge / intel / i82830 / northbridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 Joseph Smith <joe@smittys.pointclark.net>
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 <cpu/cpu.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <bitops.h>
31 #include "chip.h"
32 #include "i82830.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 = 0x3575,
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
60         /* Initialize the system wide I/O space constraints. */
61         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
62         resource->limit = 0xffffUL;
63         resource->flags =
64             IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
65
66         /* Initialize the system wide memory resources constraints. */
67         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
68         resource->limit = 0xffffffffULL;
69         resource->flags =
70             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         resource = new_resource(dev, index);
81         resource->base = ((resource_t) basek) << 10;
82         resource->size = ((resource_t) sizek) << 10;
83         resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE |
84             IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
85 }
86
87 static void tolm_test(void *gp, struct device *dev, struct resource *new)
88 {
89         struct resource **best_p = gp;
90         struct resource *best;
91         best = *best_p;
92         if (!best || (best->base > new->base))
93                 best = new;
94         *best_p = best;
95 }
96
97 static uint32_t find_pci_tolm(struct bus *bus)
98 {
99         struct resource *min;
100         uint32_t tolm;
101         min = 0;
102         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test,
103                              &min);
104         tolm = 0xffffffffUL;
105         if (min && tolm > min->base)
106                 tolm = min->base;
107         return tolm;
108 }
109
110 static void pci_domain_set_resources(device_t dev)
111 {
112         device_t mc_dev;
113         uint32_t pci_tolm;
114         int igd_memory = 0;
115
116         pci_tolm = find_pci_tolm(&dev->link[0]);
117         mc_dev = dev->link[0].children;
118         if (mc_dev) {
119                 unsigned long tomk, tolmk;
120                 int idx;
121
122                 if (CONFIG_VIDEO_MB == 512) {
123                         igd_memory = (CONFIG_VIDEO_MB);
124                 } else {
125                         igd_memory = (CONFIG_VIDEO_MB * 1024);
126                 }
127
128                 /* Get the value of the highest DRB. This tells the end of
129                  * the physical memory. The units are ticks of 32MB
130                  * i.e. 1 means 32MB.
131                  */
132                 tomk = ((unsigned long)pci_read_config8(mc_dev, DRB + 3)) << 15;
133                 tomk -= igd_memory;
134                 printk_debug("Setting RAM size to %d\n", tomk);
135
136                 /* Compute the top of low memory. */
137                 tolmk = pci_tolm >> 10;
138                 if (tolmk >= tomk) {
139                         /* The PCI hole does does not overlap the memory. */
140                         tolmk = tomk;
141                 }
142
143                 /* Report the memory regions. */
144                 idx = 10;
145                 ram_resource(dev, idx++, 0, 640);
146                 ram_resource(dev, idx++, 1024, tolmk - 1024);
147         }
148         assign_resources(&dev->link[0]);
149 }
150
151 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
152 {
153         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
154         return max;
155 }
156
157 static struct device_operations pci_domain_ops = {
158         .read_resources         = pci_domain_read_resources,
159         .set_resources          = pci_domain_set_resources,
160         .enable_resources       = enable_childrens_resources,
161         .init                   = 0,
162         .scan_bus               = pci_domain_scan_bus,
163 };
164
165 static void cpu_bus_init(device_t dev)
166 {
167         initialize_cpus(&dev->link[0]);
168 }
169
170 static void cpu_bus_noop(device_t dev)
171 {
172 }
173
174 static struct device_operations cpu_bus_ops = {
175         .read_resources         = cpu_bus_noop,
176         .set_resources          = cpu_bus_noop,
177         .enable_resources       = cpu_bus_noop,
178         .init                   = cpu_bus_init,
179         .scan_bus               = 0,
180 };
181
182 static void enable_dev(struct device *dev)
183 {
184         struct device_path;
185
186         /* Set the operations if it is a special bus type. */
187         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
188                 dev->ops = &pci_domain_ops;
189                 pci_set_method(dev);
190         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
191                 dev->ops = &cpu_bus_ops;
192         }
193 }
194
195 struct chip_operations northbridge_intel_i82830_ops = {
196         CHIP_NAME("Intel 82830 Northbridge")
197         .enable_dev = enable_dev,
198 };