1 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7 #include <device/hypertransport.h>
13 #include "northbridge.h"
17 static unsigned int max_bus;
19 static void ram_resource(device_t dev, unsigned long index,
20 unsigned long basek, unsigned long sizek)
22 struct resource *resource;
24 resource = new_resource(dev, index);
25 resource->base = ((resource_t)basek) << 10;
26 resource->size = ((resource_t)sizek) << 10;
27 resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
28 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
32 static void pci_domain_read_resources(device_t dev)
34 struct resource *resource;
36 /* Initialize the system wide io space constraints */
37 resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0,0));
42 resource->limit = 0xffffUL;
43 resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
45 /* Initialize the system wide memory resources constraints */
46 resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1,0));
51 resource->limit = 0xffffffffUL;
52 resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
55 static void tolm_test(void *gp, struct device *dev, struct resource *new)
57 struct resource **best_p = gp;
58 struct resource *best;
60 if (!best || (best->base > new->base)) {
66 static uint32_t find_pci_tolm(struct bus *bus)
71 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
73 if (min && tolm > min->base) {
79 #if CONFIG_HAVE_HIGH_TABLES==1
80 #define HIGH_TABLES_SIZE 64 // maximum size of high tables in KB
81 extern uint64_t high_tables_base, high_tables_size;
84 static void pci_domain_set_resources(device_t dev)
89 pci_tolm = find_pci_tolm(&dev->link[0]);
92 printk_debug("PCI mem marker = %x\n", pci_tolm);
94 /* FIXME Me temporary hack */
95 if(pci_tolm > 0xe0000000)
96 pci_tolm = 0xe0000000;
97 /* Ensure pci_tolm is 128M aligned */
98 pci_tolm &= 0xf8000000;
99 mc_dev = dev->link[0].children;
101 /* Figure out which areas are/should be occupied by RAM.
102 * This is all computed in kilobytes and converted to/from
103 * the memory controller right at the edges.
104 * Having different variables in different units is
105 * too confusing to get right. Kilobytes are good up to
106 * 4 Terabytes of RAM...
108 uint16_t tolm_r, remapbase_r, remaplimit_r, remapoffset_r;
109 unsigned long tomk, tolmk;
110 unsigned long remapbasek, remaplimitk, remapoffsetk;
112 /* Get the Top of Memory address, units are 128M */
113 tomk = ((unsigned long)pci_read_config16(mc_dev, TOM)) << 17;
114 /* Compute the Top of Low Memory */
115 tolmk = (pci_tolm & 0xf8000000) >> 10;
118 /* The PCI hole does not overlap memory
119 * we won't use the remap window.
122 remapbasek = 0x3ff << 16;
123 remaplimitk = 0 << 16;
124 remapoffsetk = 0 << 16;
127 /* The PCI memory hole overlaps memory
128 * setup the remap window.
130 /* Find the bottom of the remap window
133 remapbasek = 4*1024*1024;
134 if (tomk > remapbasek) {
137 /* Find the limit of the remap window */
138 remaplimitk = (remapbasek + (4*1024*1024 - tolmk) - (1 << 16));
139 /* Find the offset of the remap window from tolm */
140 remapoffsetk = remapbasek - tolmk;
142 /* Write the ram configruation registers,
143 * preserving the reserved bits.
145 tolm_r = pci_read_config16(mc_dev, 0xc4);
146 tolm_r = ((tolmk >> 17) << 11) | (tolm_r & 0x7ff);
147 pci_write_config16(mc_dev, 0xc4, tolm_r);
149 remapbase_r = pci_read_config16(mc_dev, 0xc6);
150 remapbase_r = (remapbasek >> 16) | (remapbase_r & 0xfc00);
151 pci_write_config16(mc_dev, 0xc6, remapbase_r);
153 remaplimit_r = pci_read_config16(mc_dev, 0xc8);
154 remaplimit_r = (remaplimitk >> 16) | (remaplimit_r & 0xfc00);
155 pci_write_config16(mc_dev, 0xc8, remaplimit_r);
157 remapoffset_r = pci_read_config16(mc_dev, 0xca);
158 remapoffset_r = (remapoffsetk >> 16) | (remapoffset_r & 0xfc00);
159 pci_write_config16(mc_dev, 0xca, remapoffset_r);
161 /* Report the memory regions */
162 ram_resource(dev, 3, 0, 640);
163 ram_resource(dev, 4, 768, (tolmk - 768));
164 if (tomk > 4*1024*1024) {
165 ram_resource(dev, 5, 4096*1024, tomk - 4*1024*1024);
167 if (remaplimitk >= remapbasek) {
168 ram_resource(dev, 6, remapbasek,
169 (remaplimitk + 64*1024) - remapbasek);
172 #if CONFIG_HAVE_HIGH_TABLES==1
173 /* Leave some space for ACPI, PIRQ and MP tables */
174 high_tables_base = (tolmk - HIGH_TABLES_SIZE) * 1024;
175 high_tables_size = HIGH_TABLES_SIZE * 1024;
178 assign_resources(&dev->link[0]);
181 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
183 max = pci_scan_bus(&dev->link[0], 0, 0xff, max);
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,
195 .scan_bus = pci_domain_scan_bus,
196 .ops_pci_bus = &pci_cf8_conf1, /* Do we want to use the memory mapped space here? */
199 static void mc_read_resources(device_t dev)
201 struct resource *resource;
203 pci_dev_read_resources(dev);
205 resource = new_resource(dev, 0xcf);
206 resource->base = 0xe0000000;
207 resource->size = max_bus * 4096*256;
208 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
211 static void mc_set_resources(device_t dev)
213 struct resource *resource, *last;
215 last = &dev->resource[dev->resources];
216 resource = find_resource(dev, 0xcf);
218 report_resource_stored(dev, resource, "<mmconfig>");
220 pci_dev_set_resources(dev);
223 static void intel_set_subsystem(device_t dev, unsigned vendor, unsigned device)
225 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
226 ((device & 0xffff) << 16) | (vendor & 0xffff));
229 static struct pci_operations intel_pci_ops = {
230 .set_subsystem = intel_set_subsystem,
233 static struct device_operations mc_ops = {
234 .read_resources = mc_read_resources,
235 .set_resources = mc_set_resources,
236 .enable_resources = pci_dev_enable_resources,
239 .ops_pci = &intel_pci_ops,
242 static const struct pci_driver mc_driver __pci_driver = {
244 .vendor = PCI_VENDOR_ID_INTEL,
248 static void cpu_bus_init(device_t dev)
250 initialize_cpus(&dev->link[0]);
253 static void cpu_bus_noop(device_t dev)
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,
266 static void enable_dev(device_t dev)
268 /* Set the operations if it is a special bus type */
269 if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
270 dev->ops = &pci_domain_ops;
272 else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
273 dev->ops = &cpu_bus_ops;
277 struct chip_operations northbridge_intel_e7520_ops = {
278 CHIP_NAME("Intel E7520 Northbridge")
279 .enable_dev = enable_dev,