85adf4f417836b88051ce128fb147d68c6fc73a2
[coreboot.git] / src / northbridge / intel / i855pm / northbridge.c
1 #include <console/console.h>
2 #include <arch/io.h>
3 #include <stdint.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <bitops.h>
10 #include "chip.h"
11
12 #define BRIDGE_IO_MASK (IORESOURCE_IO | IORESOURCE_MEM)
13
14 static void pci_domain_read_resources(device_t dev)
15 {
16         struct resource *resource;
17
18         /* Initialize the system wide io space constraints */
19         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
20         resource->limit = 0xffffUL;
21         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
22
23         /* Initialize the system wide memory resources constraints */
24         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
25         resource->limit = 0xffffffffULL;
26         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
27 }
28
29 static void ram_resource(device_t dev, unsigned long index,
30         unsigned long basek, unsigned long sizek)
31 {
32         struct resource *resource;
33
34         if (!sizek) {
35                 return;
36         }
37         resource = new_resource(dev, index);
38         resource->base  = ((resource_t)basek) << 10;
39         resource->size  = ((resource_t)sizek) << 10;
40         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
41                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
42 }
43
44 static void tolm_test(void *gp, struct device *dev, struct resource *new)
45 {
46         struct resource **best_p = gp;
47         struct resource *best;
48         best = *best_p;
49         if (!best || (best->base > new->base)) {
50                 best = new;
51         }
52         *best_p = best;
53 }
54
55 static uint32_t find_pci_tolm(struct bus *bus)
56 {
57         struct resource *min;
58         uint32_t tolm;
59         min = 0;
60         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
61         tolm = 0xffffffffUL;
62         if (min && tolm > min->base) {
63                 tolm = min->base;
64         }
65         return tolm;
66 }
67
68 static void pci_domain_set_resources(device_t dev)
69 {
70         device_t mc_dev;
71         uint32_t pci_tolm;
72
73         pci_tolm = find_pci_tolm(&dev->link[0]);
74         mc_dev = dev->link[0].children;
75         if (mc_dev) {
76                 /* Figure out which areas are/should be occupied by RAM.
77                  * This is all computed in kilobytes and converted to/from
78                  * the memory controller right at the edges.
79                  * Having different variables in different units is
80                  * too confusing to get right.  Kilobytes are good up to
81                  * 4 Terabytes of RAM...
82                  */
83                 uint16_t tolm_r;
84                 unsigned long tomk, tolmk;
85                 int idx;
86
87                 /* Get the value of the highest DRB. This tells the end of
88                  * the physical memory.  The units are ticks of 32MB
89                  * i.e. 1 means 32MB.
90                  */
91                 tomk = ((unsigned long)pci_read_config8(mc_dev, 0x63)) << 15;
92                 /* Compute the top of Low memory */
93                 tolmk = pci_tolm >> 10;
94                 if (tolmk >= tomk) {
95                         /* The PCI hole does does not overlap the memory.
96                          */
97                         tolmk = tomk;
98                 }
99                 /* Write the ram configuration registers,
100                  * preserving the reserved bits.
101                  */
102                 tolm_r = pci_read_config16(mc_dev, 0xc4);
103                 tolm_r = ((tolmk >> 10) << 3) | (tolm_r & 0xf);
104                 pci_write_config16(mc_dev, 0xc4, tolm_r);
105
106                 /* Report the memory regions */
107                 idx = 10;
108                 ram_resource(dev, idx++, 0, 640);
109                 ram_resource(dev, idx++, 768, tolmk - 768);
110         }
111         assign_resources(&dev->link[0]);
112 }
113
114 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
115 {
116         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
117         return max;
118 }
119
120 static struct device_operations pci_domain_ops = {
121         .read_resources   = pci_domain_read_resources,
122         .set_resources    = pci_domain_set_resources,
123         .enable_resources = enable_childrens_resources,
124         .init             = 0,
125         .scan_bus         = pci_domain_scan_bus,
126 };  
127
128 static void cpu_bus_init(device_t dev)
129 {
130         initialize_cpus(&dev->link[0]);
131 }
132
133 static void cpu_bus_noop(device_t dev)
134 {
135 }
136
137 static struct device_operations cpu_bus_ops = {
138         .read_resources   = cpu_bus_noop,
139         .set_resources    = cpu_bus_noop,
140         .enable_resources = cpu_bus_noop,
141         .init             = cpu_bus_init,
142         .scan_bus         = 0,
143 };
144
145 static void enable_dev(struct device *dev)
146 {
147         struct device_path path;
148
149         /* Set the operations if it is a special bus type */
150         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
151                 dev->ops = &pci_domain_ops;
152                 pci_set_method(dev);
153         }
154         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
155                 dev->ops = &cpu_bus_ops;
156         }
157 }
158
159 struct chip_operations northbridge_intel_i855pm_ops = {
160         CHIP_NAME("Intel 855PM Northbridge")
161         .enable_dev = enable_dev,
162 };