removing redundant and unneeded calls to functions.
[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 /* number of MB to take off the top of ram for VSM and display memory. 
19  * FIXME -- make this configurable
20  */
21 #define RAMADJUSTMB     9
22 /*
23 */
24
25 /* todo: add a resource record. We don't do this here because this may be called when 
26   * very little of the platform is actually working.
27   */
28 int
29 sizeram(void)
30 {
31         msr_t msr;
32         int sizem = 0;
33         unsigned short dimm;
34
35         msr = rdmsr(0x20000018);
36         printk_debug("sizeram: %08x:%08x\n", msr.hi, msr.lo);
37
38         /* dimm 0 */
39         dimm = msr.hi;
40         /* installed? */
41         if ((dimm & 7) != 7)
42                 sizem = (1 << ((dimm >> 12)-1)) * 8;
43
44
45         /* dimm 1*/
46         dimm = msr.hi >> 16;
47         /* installed? */
48         if ((dimm & 7) != 7)
49                 sizem += (1 << ((dimm >> 12)-1)) * 8;
50
51         printk_debug("sizeram: sizem 0x%x\n", sizem);
52         return sizem;
53 }
54
55
56 /* here is programming for the various MSRs.*/
57 #define IM_QWAIT 0x100000
58
59 #define DMCF_WRITE_SERIALIZE_REQUEST (2<<12) /* 2 outstanding */ /* in high */
60 #define DMCF_SERIAL_LOAD_MISSES  (2) /* enabled */
61
62 /* these are the 8-bit attributes for controlling RCONF registers */
63 #define CACHE_DISABLE (1<<0)
64 #define WRITE_ALLOCATE (1<<1)
65 #define WRITE_PROTECT (1<<2)
66 #define WRITE_THROUGH (1<<3)
67 #define WRITE_COMBINE (1<<4)
68 #define WRITE_SERIALIZE (1<<5)
69
70 /* ram has none of this stuff */
71 #define RAM_PROPERTIES (0)
72 #define DEVICE_PROPERTIES (WRITE_SERIALIZE|CACHE_DISABLE)
73 #define ROM_PROPERTIES (WRITE_SERIALIZE|WRITE_THROUGH|CACHE_DISABLE)
74 #define MSR_WS_CD_DEFAULT (0x21212121)
75
76 /* 1810-1817 give you 8 registers with which to program protection regions */
77 /* the are region configuration range registers, or RRCF */
78 /* in msr terms, the are a straight base, top address assign, since they are 4k aligned. */
79 /* so no left-shift needed for top or base */
80 #define RRCF_LOW(base,properties) (base|(1<<8)|properties)
81 #define RRCF_LOW_CD(base)       RRCF_LOW(base, CACHE_DISABLE)
82
83 /* build initializer for P2D MSR */
84 #define P2D_BM(msr, pdid1, bizarro, pbase, pmask) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(pbase>>24), .lo=(pbase<<8)|pmask}}
85 #define P2D_BMO(msr, pdid1, bizarro, poffset, pbase, pmask) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(poffset<<8)|(pbase>>24), .lo=(pbase<<8)|pmask}}
86 #define P2D_R(msr, pdid1, bizarro, pmax, pmin) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(pmax>>12), .lo=(pmax<<20)|pmin}}
87 #define P2D_RO(msr, pdid1, bizarro, poffset, pmax, pmin) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(poffset<<8)|(pmax>>12), .lo=(pmax<<20)|pmin}}
88 #define P2D_SC(msr, pdid1, bizarro, wen, ren,pscbase) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(wen), .lo=(ren<<16)|(pscbase>>18)}}
89 #define IOD_BM(msr, pdid1, bizarro, ibase, imask) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(ibase>>12), .lo=(ibase<<20)|imask}}
90 #define IOD_SC(msr, pdid1, bizarro, en, wen, ren, ibase) {msr, {.hi=(pdid1<<29)|(bizarro<<28), .lo=(en<<24)|(wen<<21)|(ren<<20)|(ibase<<3)}}
91
92
93
94 struct msr_defaults {
95         int msr_no;
96         msr_t msr;
97 } msr_defaults [] = {
98         {0x1700, {.hi = 0, .lo = IM_QWAIT}},
99         {0x1800, {.hi = DMCF_WRITE_SERIALIZE_REQUEST, .lo = DMCF_SERIAL_LOAD_MISSES}},
100         /* 1808 will be done down below, so we have to do 180a->1817 (well, 1813 really) */
101         /* for 180a, for now, we assume VSM will configure it */
102         /* 180b is left at reset value,a0000-bffff is non-cacheable */
103         /* 180c, c0000-dffff is set to write serialize and non-cachable */
104         /* oops, 180c will be set by cpu bug handling in cpubug.c */
105         //{0x180c, {.hi = MSR_WS_CD_DEFAULT, .lo = MSR_WS_CD_DEFAULT}},
106         /* 180d is left at default, e0000-fffff is non-cached */
107
108         /* we will assume 180e, the ssm region configuration, is left at default or set by VSM */
109         /* we will not set 0x180f, the DMM,yet */
110         //{0x1810, {.hi=0xee7ff000, .lo=RRCF_LOW(0xee000000, WRITE_COMBINE|CACHE_DISABLE)}},
111         //{0x1811, {.hi = 0xefffb000, .lo = RRCF_LOW_CD(0xefff8000)}},
112         //{0x1812, {.hi = 0xefff7000, .lo = RRCF_LOW_CD(0xefff4000)}},
113         //{0x1813, {.hi = 0xefff3000, .lo = RRCF_LOW_CD(0xefff0000)}},
114         /* now for GLPCI routing */
115         /* GLIU0 */
116         P2D_BM(0x10000020, 0x1, 0x0, 0x0, 0xfff80),
117         P2D_BM(0x10000021, 0x1, 0x0, 0x80000, 0xfffe0),
118         P2D_SC(0x1000002c, 0x1, 0x0, 0x0,  0xff03, 0x3),
119         /* GLIU1 */
120         P2D_BM(0x40000020, 0x1, 0x0, 0x0, 0xfff80),
121         P2D_BM(0x40000021, 0x1, 0x0, 0x80000, 0xfffe0),
122         P2D_SC(0x4000002d, 0x1, 0x0, 0x0,  0xff03, 0x3),
123         {0}
124 };
125
126
127 static int
128 setup_gx2_cache(void)
129 {
130         msr_t msr;
131         unsigned long long val;
132         int sizembytes, sizereg;
133
134         sizembytes = sizeram();
135         printk_debug("enable_cache: enable for %dm bytes\n", sizembytes);
136         /* build up the rconf word. */
137         /* the SYSTOP bits 27:8 are actually the top bits from 31:12. Book fails to say that */
138         /* set romrp */
139         val = ((unsigned long long) ROM_PROPERTIES) << 56;
140         /* make rom base useful for 1M roms */
141         /* Flash base address -- sized for 1/2M for now*/
142         val |= ((unsigned long long) 0xfff800)<<36;
143         /* set the devrp properties */
144         val |= ((unsigned long long) DEVICE_PROPERTIES) << 28;
145         /* sigh. Take our TOM, RIGHT shift 12, since it page-aligned, then LEFT-shift 8 for reg. */
146         /* yank off 8M for frame buffer and 1M for VSA */
147         sizembytes -= RAMADJUSTMB;
148         sizereg = sizembytes;
149         sizereg *= 0x100000;
150         sizereg >>= 12;
151         sizereg <<= 8;
152         val |= sizereg;
153         val |= RAM_PROPERTIES;
154         msr.lo = val;
155         msr.hi = (val >> 32);
156         printk_debug("msr will be set to %x:%x\n", msr.hi, msr.lo);
157         wrmsr(CPU_RCONF_DEFAULT, msr);
158
159         enable_cache();
160         wbinvd();
161         return sizembytes;
162 }
163
164 #define SMM_OFFSET 0x40400000
165 #define SMM_SIZE   256
166
167 /* we have to do this here. We have not found a nicer way to do it */
168 void
169 setup_gx2(void)
170 {
171         int i;
172         unsigned long tmp, tmp2, tmp3;
173         msr_t msr;
174         unsigned long sizem, membytes;
175
176         sizem = setup_gx2_cache();
177
178         membytes = sizem * 1048576;
179 #if 0
180         /* we need to set 0x10000028 and 0x40000029 */
181         printk_debug("sizem 0x%x, membytes 0x%x\n", sizem, membytes);
182         msr.hi = 0x20000000 | membytes>>24;
183         msr.lo = 0x100 | ( ((membytes >>12) & 0xfff) << 20);
184         wrmsr(0x10000028, msr);
185         msr.hi = 0x20000000 | membytes>>24;
186         msr.lo = 0x100 | ( ((membytes >>12) & 0xfff) << 20);
187         wrmsr(0x40000029, msr);
188         msr = rdmsr(0x10000028);
189         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x10000028, msr.hi,msr.lo);
190         msr = rdmsr(0x40000029);
191         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x40000029, msr.hi,msr.lo);
192
193
194         /* fixme: SMM MSR 0x10000026 and 0x400000023 */
195         /* calculate the OFFSET field */
196         tmp = membytes - SMM_OFFSET;
197         tmp >>= 12;
198         tmp <<= 8;
199         tmp |= 0x20000000;
200         tmp |= (SMM_OFFSET >> 24);
201
202         /* calculate the PBASE and PMASK fields */
203         tmp2 = (SMM_OFFSET << 8) & 0xFFF00000; /* shift right 12 then left 20  == left 8 */
204         tmp2 |= (((~(SMM_SIZE * 1024) + 1) >> 12) & 0xfffff);
205         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x10000026, tmp, tmp2);
206         msr.hi = tmp;
207         msr.lo = tmp2;
208         wrmsr(0x10000026, msr);
209 #else
210         msr.hi = 0x2000000f;
211         msr.lo = 0xfbf00100;
212         wrmsr(0x10000028, msr);
213         msr = rdmsr(0x10000028);
214         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x10000028, msr.hi, msr.lo);
215         wrmsr(0x40000029, msr);
216         msr = rdmsr(0x40000029);
217         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x40000029, msr.hi, msr.lo);
218
219         msr.hi = 0x2cfbc040;
220         msr.lo = 0x400fffc0;
221         wrmsr(0x10000026, msr);
222         msr = rdmsr(0x10000026);
223         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x10000026, msr.hi, msr.lo);
224
225         msr.hi = 0x22fffc02;
226         msr.lo = 0x10ffbf00;
227         wrmsr(0x1808, msr);
228         msr = rdmsr(0x1808);
229         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x1808, msr.hi, msr.lo);
230 #endif
231         /* now do the default MSR values */
232         for(i = 0; msr_defaults[i].msr_no; i++) {
233                 msr_t msr;
234                 wrmsr(msr_defaults[i].msr_no, msr_defaults[i].msr);
235                 msr = rdmsr(msr_defaults[i].msr_no);
236                 printk_debug("MSR 0x%x is now 0x%x:0x%x\n", msr_defaults[i].msr_no, msr.hi,msr.lo);
237         }
238 }
239
240 static void enable_shadow(device_t dev)
241 {
242         
243 }
244
245 static void northbridge_init(device_t dev) 
246 {
247         printk_debug("northbridge: %s()\n", __FUNCTION__);
248         
249         enable_shadow(dev);
250 }
251
252 static struct device_operations northbridge_operations = {
253         .read_resources   = pci_dev_read_resources,
254         .set_resources    = pci_dev_set_resources,
255         .enable_resources = pci_dev_enable_resources,
256         .init             = northbridge_init,
257         .enable           = 0,
258         .ops_pci          = 0,
259 };
260
261 static struct pci_driver northbridge_driver __pci_driver = {
262         .ops = &northbridge_operations,
263         .vendor = PCI_VENDOR_ID_NS,
264         .device = PCI_DEVICE_ID_NS_GX2,
265 };
266
267 #define BRIDGE_IO_MASK (IORESOURCE_IO | IORESOURCE_MEM)
268
269 static void pci_domain_read_resources(device_t dev)
270 {
271         struct resource *resource;
272
273         printk_spew("%s:%s()\n", NORTHBRIDGE_FILE, __FUNCTION__);
274
275         /* Initialize the system wide io space constraints */
276         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0,0));
277         resource->limit = 0xffffUL;
278         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
279
280         /* Initialize the system wide memory resources constraints */
281         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1,0));
282         resource->limit = 0xffffffffULL;
283         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED;
284 }
285
286 static void ram_resource(device_t dev, unsigned long index,
287         unsigned long basek, unsigned long sizek)
288 {
289         struct resource *resource;
290
291         if (!sizek) {
292                 return;
293         }
294         resource = new_resource(dev, index);
295         resource->base  = ((resource_t)basek) << 10;
296         resource->size  = ((resource_t)sizek) << 10;
297         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
298                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
299 }
300
301 static void tolm_test(void *gp, struct device *dev, struct resource *new)
302 {
303         struct resource **best_p = gp;
304         struct resource *best;
305         best = *best_p;
306         if (!best || (best->base > new->base)) {
307                 best = new;
308         }
309         *best_p = best;
310 }
311
312 static uint32_t find_pci_tolm(struct bus *bus)
313 {
314         struct resource *min;
315         uint32_t tolm;
316         min = 0;
317         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
318         tolm = 0xffffffffUL;
319         if (min && tolm > min->base) {
320                 tolm = min->base;
321         }
322         return tolm;
323 }
324
325 #define FRAMEBUFFERK 4096
326
327 static void pci_domain_set_resources(device_t dev)
328 {
329         device_t mc_dev;
330         uint32_t pci_tolm;
331 #if 0
332         pci_tolm = find_pci_tolm(&dev->link[0]);
333         mc_dev = dev->link[0].children;
334         if (mc_dev) {
335                 unsigned int tomk, tolmk;
336                 unsigned int ramreg = 0;
337                 int i, idx;
338                 unsigned int *bcdramtop = (unsigned int *)(GX_BASE + BC_DRAM_TOP);
339                 unsigned int *mcgbaseadd = (unsigned int *)(GX_BASE + MC_GBASE_ADD);
340
341                 for(i=0; i<0x20; i+= 0x10) {
342                         unsigned int *mcreg = (unsigned int *)(GX_BASE + MC_BANK_CFG);
343                         unsigned int mem_config = *mcreg;
344
345                         if (((mem_config & (DIMM_PG_SZ << i)) >> (4 + i)) == 7)
346                                 continue;
347                         ramreg += 1 << (((mem_config & (DIMM_SZ << i)) >> (i + 8)) + 2);
348                 }
349                         
350                 tomk = ramreg << 10;
351
352                 /* Sort out the framebuffer size */
353                 tomk -= FRAMEBUFFERK;
354                 *bcdramtop = ((tomk << 10) - 1);
355                 *mcgbaseadd = (tomk >> 9);
356
357                 printk_debug("BC_DRAM_TOP = 0x%08x\n", *bcdramtop);
358                 printk_debug("MC_GBASE_ADD = 0x%08x\n", *mcgbaseadd);
359
360                 printk_debug("I would set ram size to %d Mbytes\n", (tomk >> 10));
361
362                 /* Compute the top of Low memory */
363                 tolmk = pci_tolm >> 10;
364                 if (tolmk >= tomk) {
365                         /* The PCI hole does does not overlap the memory.
366                          */
367                         tolmk = tomk;
368                 }
369                 /* Report the memory regions */
370                 idx = 10;
371                 ram_resource(dev, idx++, 0, tolmk);
372         }
373         assign_resources(&dev->link[0]);
374 #endif
375 }
376
377 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
378 {
379         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
380         return max;
381 }
382
383 static struct device_operations pci_domain_ops = {
384         .read_resources   = pci_domain_read_resources,
385         .set_resources    = pci_domain_set_resources,
386         .enable_resources = enable_childrens_resources,
387         .init             = 0,
388         .scan_bus         = pci_domain_scan_bus,
389 };  
390
391 static void cpu_bus_init(device_t dev)
392 {
393         initialize_cpus(&dev->link[0]);
394 }
395
396 static void cpu_bus_noop(device_t dev)
397 {
398 }
399
400 static struct device_operations cpu_bus_ops = {
401         .read_resources   = cpu_bus_noop,
402         .set_resources    = cpu_bus_noop,
403         .enable_resources = cpu_bus_noop,
404         .init             = cpu_bus_init,
405         .scan_bus         = 0,
406 };
407
408 void chipsetInit (void);
409
410 static void enable_dev(struct device *dev)
411 {
412         printk_debug("gx2 north: enable_dev\n");
413         /* Set the operations if it is a special bus type */
414         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
415                 extern void cpubug(void);
416                 printk_debug("DEVICE_PATH_PCI_DOMAIN\n");
417                 /* cpubug MUST be called before setup_gx2(), so we force the issue here */
418                 northbridgeinit();
419                 cpubug();       
420                 chipsetinit();
421                 //setup_gx2();
422                 /* do this here for now -- this chip really breaks our device model */
423                 setup_realmode_idt();
424                 do_vsmbios();
425                 dev->ops = &pci_domain_ops;
426                 pci_set_method(dev);
427                 ram_resource(dev, 0, 0, (sizeram() - RAMADJUSTMB)*1024);
428         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
429                 printk_debug("DEVICE_PATH_APIC_CLUSTER\n");
430                 dev->ops = &cpu_bus_ops;
431         }
432 }
433
434 struct chip_operations northbridge_amd_gx2_ops = {
435         CHIP_NAME("AMD GX2 Northbridge")
436         .enable_dev = enable_dev, 
437 };