stupid svn failed.
[coreboot.git] / src / northbridge / amd / gx2 / 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 #include "northbridge.h"
12 #include <cpu/amd/gx2def.h>
13 #include <cpu/x86/msr.h>
14 #include <cpu/x86/cache.h>
15
16 #define NORTHBRIDGE_FILE "northbridge.c"
17 /*
18 */
19
20 /* todo: add a resource record. We don't do this here because this may be called when 
21   * very little of the platform is actually working.
22   */
23 int
24 sizeram(void)
25 {
26         msr_t msr;
27         int sizem;
28         unsigned short dimm;
29
30         msr = rdmsr(0x20000018);
31         printk_debug("sizeram: %08x:%08x\n", msr.hi, msr.lo);
32
33         /* dimm 0 */
34         dimm = msr.hi;
35         /* installed? */
36         if ((dimm & 7) != 7)
37                 sizem = (1 << ((dimm >> 12)-1)) * 8;
38
39
40         /* dimm 1*/
41         dimm = msr.hi >> 16;
42         /* installed? */
43         if ((dimm & 7) != 7)
44                 sizem += (1 << ((dimm >> 12)-1)) * 8;
45
46         printk_debug("sizeram: sizem 0x%x\n", sizem);
47         return sizem;
48 }
49
50 #define CACHE_DISABLE (1<<0)
51 #define WRITE_ALLOCATE (1<<1)
52 #define WRITE_PROTECT (1<<2)
53 #define WRITE_THROUGH (1<<3)
54 #define WRITE_COMBINE (1<<4)
55 #define WRITE_SERIALIZE (1<<5)
56
57 /* ram has none of this stuff */
58 #define RAM_PROPERTIES (0)
59 #define DEVICE_PROPERTIES (WRITE_SERIALIZE|CACHE_DISABLE)
60 #define ROM_PROPERTIES (WRITE_SERIALIZE|WRITE_THROUGH|CACHE_DISABLE)
61
62 static void
63 setup_gx2_cache(int sizem)
64 {
65         msr_t msr;
66         unsigned long long val;
67         printk_debug("enable_cache: enable for %dm bytes\n", sizem);
68         /* build up the rconf word. */
69         /* the SYSTOP bits 27:8 are actually the top bits from 31:12. Book fails to say that */
70         /* set romrp */
71         val = ((unsigned long long) ROM_PROPERTIES) << 56;
72         /* make rom base useful for 1M roms */
73         /* fuctory sets this to a weird value, just go with it. */
74         val |= ((unsigned long long) 0xff800)<<36;
75         /* set the devrp properties */
76         val |= ((unsigned long long) DEVICE_PROPERTIES) << 28;
77         /* sigh. Take our TOM, RIGHT shift 12, since it page-aligned, then LEFT-shift 8 for reg. */
78         /* yank off 8M for frame buffer and 1M for VSA */
79         sizem -= 9;
80         sizem *= 0x100000;
81         sizem >>= 12;
82         sizem <<= 8;
83         val |= sizem;
84         val |= RAM_PROPERTIES;
85         msr.lo = val;
86         msr.hi = (val >> 32);
87         printk_debug("msr will be set to %x:%x\n", msr.hi, msr.lo);
88         wrmsr(0x1808, msr);
89         
90         enable_cache();
91         wbinvd();
92 }
93 /* we have to do this here. We have not found a nicer way to do it */
94 void
95 setup_gx2(void)
96 {
97         int sizem;
98         sizem = sizeram();
99         
100         setup_gx2_cache(sizem);
101 }
102
103
104 static void optimize_xbus(device_t dev)
105 {
106         /* Optimise X-Bus performance */
107         pci_write_config8(dev, 0x40, 0x1e);
108         pci_write_config8(dev, 0x41, 0x52);
109         pci_write_config8(dev, 0x43, 0xc1);
110         pci_write_config8(dev, 0x44, 0x00);
111 }
112
113 static void enable_shadow(device_t dev)
114 {
115         
116 }
117
118 static void northbridge_init(device_t dev) 
119 {
120         printk_debug("northbridge: %s()\n", __FUNCTION__);
121         
122         optimize_xbus(dev);
123         enable_shadow(dev);
124 }
125
126
127 static struct device_operations northbridge_operations = {
128         .read_resources   = pci_dev_read_resources,
129         .set_resources    = pci_dev_set_resources,
130         .enable_resources = pci_dev_enable_resources,
131         .init             = northbridge_init,
132         .enable           = 0,
133         .ops_pci          = 0,
134 };
135
136 static struct pci_driver northbridge_driver __pci_driver = {
137         .ops = &northbridge_operations,
138         .vendor = PCI_VENDOR_ID_CYRIX,
139         .device = PCI_DEVICE_ID_CYRIX_PCI_MASTER, 
140 };
141
142
143
144 #define BRIDGE_IO_MASK (IORESOURCE_IO | IORESOURCE_MEM)
145
146 static void pci_domain_read_resources(device_t dev)
147 {
148         struct resource *resource;
149
150         printk_spew("%s:%s()\n", NORTHBRIDGE_FILE, __FUNCTION__);
151
152         /* Initialize the system wide io space constraints */
153         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0,0));
154         resource->limit = 0xffffUL;
155         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
156
157         /* Initialize the system wide memory resources constraints */
158         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1,0));
159         resource->limit = 0xffffffffULL;
160         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
161 }
162
163 static void ram_resource(device_t dev, unsigned long index,
164         unsigned long basek, unsigned long sizek)
165 {
166         struct resource *resource;
167
168         if (!sizek) {
169                 return;
170         }
171         resource = new_resource(dev, index);
172         resource->base  = ((resource_t)basek) << 10;
173         resource->size  = ((resource_t)sizek) << 10;
174         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
175                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
176 }
177
178 static void tolm_test(void *gp, struct device *dev, struct resource *new)
179 {
180         struct resource **best_p = gp;
181         struct resource *best;
182         best = *best_p;
183         if (!best || (best->base > new->base)) {
184                 best = new;
185         }
186         *best_p = best;
187 }
188
189 static uint32_t find_pci_tolm(struct bus *bus)
190 {
191         struct resource *min;
192         uint32_t tolm;
193         min = 0;
194         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
195         tolm = 0xffffffffUL;
196         if (min && tolm > min->base) {
197                 tolm = min->base;
198         }
199         return tolm;
200 }
201
202 #define FRAMEBUFFERK 4096
203
204 static void pci_domain_set_resources(device_t dev)
205 {
206         device_t mc_dev;
207         uint32_t pci_tolm;
208 #if 0
209         pci_tolm = find_pci_tolm(&dev->link[0]);
210         mc_dev = dev->link[0].children;
211         if (mc_dev) {
212                 unsigned int tomk, tolmk;
213                 unsigned int ramreg = 0;
214                 int i, idx;
215                 unsigned int *bcdramtop = (unsigned int *)(GX_BASE + BC_DRAM_TOP);
216                 unsigned int *mcgbaseadd = (unsigned int *)(GX_BASE + MC_GBASE_ADD);
217
218                 for(i=0; i<0x20; i+= 0x10) {
219                         unsigned int *mcreg = (unsigned int *)(GX_BASE + MC_BANK_CFG);
220                         unsigned int mem_config = *mcreg;
221
222                         if (((mem_config & (DIMM_PG_SZ << i)) >> (4 + i)) == 7)
223                                 continue;
224                         ramreg += 1 << (((mem_config & (DIMM_SZ << i)) >> (i + 8)) + 2);
225                 }
226                         
227                 tomk = ramreg << 10;
228
229                 /* Sort out the framebuffer size */
230                 tomk -= FRAMEBUFFERK;
231                 *bcdramtop = ((tomk << 10) - 1);
232                 *mcgbaseadd = (tomk >> 9);
233
234                 printk_debug("BC_DRAM_TOP = 0x%08x\n", *bcdramtop);
235                 printk_debug("MC_GBASE_ADD = 0x%08x\n", *mcgbaseadd);
236
237                 printk_debug("I would set ram size to %d Mbytes\n", (tomk >> 10));
238
239                 /* Compute the top of Low memory */
240                 tolmk = pci_tolm >> 10;
241                 if (tolmk >= tomk) {
242                         /* The PCI hole does does not overlap the memory.
243                          */
244                         tolmk = tomk;
245                 }
246                 /* Report the memory regions */
247                 idx = 10;
248                 ram_resource(dev, idx++, 0, tolmk);
249         }
250         assign_resources(&dev->link[0]);
251 #endif
252 }
253
254 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
255 {
256         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
257         return max;
258 }
259
260 static struct device_operations pci_domain_ops = {
261         .read_resources   = pci_domain_read_resources,
262         .set_resources    = pci_domain_set_resources,
263         .enable_resources = enable_childrens_resources,
264         .init             = 0,
265         .scan_bus         = pci_domain_scan_bus,
266 };  
267
268 static void cpu_bus_init(device_t dev)
269 {
270         initialize_cpus(&dev->link[0]);
271 }
272
273 static void cpu_bus_noop(device_t dev)
274 {
275 }
276
277 static struct device_operations cpu_bus_ops = {
278         .read_resources   = cpu_bus_noop,
279         .set_resources    = cpu_bus_noop,
280         .enable_resources = cpu_bus_noop,
281         .init             = cpu_bus_init,
282         .scan_bus         = 0,
283 };
284
285 static void enable_dev(struct device *dev)
286 {
287         printk_debug("gx2 north: enable_dev\n");
288         /* Set the operations if it is a special bus type */
289         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
290                 printk_debug("DEVICE_PATH_PCI_DOMAIN\n");
291                 setup_gx2();
292                 dev->ops = &pci_domain_ops;
293                 pci_set_method(dev);
294         }
295         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
296                 printk_debug("DEVICE_PATH_APIC_CLUSTER\n");
297                 dev->ops = &cpu_bus_ops;
298         }
299 }
300
301 struct chip_operations northbridge_amd_gx2_ops = {
302         CHIP_NAME("AMD GX2 Northbridge")
303         .enable_dev = enable_dev, 
304 };