b20d3d78291967dca639864fffff25631dd5b726
[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 #include <cpu/amd/vr.h>
16 #include <cpu/cpu.h>
17 #include "../../../southbridge/amd/cs5536/cs5536.h"
18 #define VIDEO_MB 8
19
20 extern void graphics_init(void);
21
22 #define NORTHBRIDGE_FILE "northbridge.c"
23
24 /* todo: add a resource record. We don't do this here because this may be called when 
25   * very little of the platform is actually working.
26   */
27 int
28 sizeram(void)
29 {
30         msr_t msr;
31         int sizem = 0;
32         unsigned short dimm;
33
34         msr = rdmsr(0x20000018);
35         printk_debug("sizeram: %08x:%08x\n", msr.hi, msr.lo);
36
37         /* dimm 0 */
38         dimm = msr.hi;
39         /* installed? */
40         if ((dimm & 7) != 7)
41                 sizem = (1 << ((dimm >> 12)-1)) * 8;
42
43
44         /* dimm 1*/
45         dimm = msr.hi >> 16;
46         /* installed? */
47         if ((dimm & 7) != 7)
48                 sizem += (1 << ((dimm >> 12)-1)) * 8;
49
50         printk_debug("sizeram: sizem 0x%x\n", sizem);
51         return sizem;
52 }
53
54
55 /* here is programming for the various MSRs.*/
56 #define IM_QWAIT 0x100000
57
58 #define DMCF_WRITE_SERIALIZE_REQUEST (2<<12) /* 2 outstanding */ /* in high */
59 #define DMCF_SERIAL_LOAD_MISSES  (2) /* enabled */
60
61 /* these are the 8-bit attributes for controlling RCONF registers */
62 #define CACHE_DISABLE (1<<0)
63 #define WRITE_ALLOCATE (1<<1)
64 #define WRITE_PROTECT (1<<2)
65 #define WRITE_THROUGH (1<<3)
66 #define WRITE_COMBINE (1<<4)
67 #define WRITE_SERIALIZE (1<<5)
68
69 /* ram has none of this stuff */
70 #define RAM_PROPERTIES (0)
71 #define DEVICE_PROPERTIES (WRITE_SERIALIZE|CACHE_DISABLE)
72 #define ROM_PROPERTIES (WRITE_SERIALIZE|WRITE_PROTECT|CACHE_DISABLE)
73 #define MSR_WS_CD_DEFAULT (0x21212121)
74
75 /* 1810-1817 give you 8 registers with which to program protection regions */
76 /* the are region configuration range registers, or RRCF */
77 /* in msr terms, the are a straight base, top address assign, since they are 4k aligned. */
78 /* so no left-shift needed for top or base */
79 #define RRCF_LOW(base,properties) (base|(1<<8)|properties)
80 #define RRCF_LOW_CD(base)       RRCF_LOW(base, CACHE_DISABLE)
81
82 /* build initializer for P2D MSR */
83 #define P2D_BM(msr, pdid1, bizarro, pbase, pmask) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(pbase>>24), .lo=(pbase<<8)|pmask}}
84 #define P2D_BMO(msr, pdid1, bizarro, poffset, pbase, pmask) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(poffset<<8)|(pbase>>24), .lo=(pbase<<8)|pmask}}
85 #define P2D_R(msr, pdid1, bizarro, pmax, pmin) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(pmax>>12), .lo=(pmax<<20)|pmin}}
86 #define P2D_RO(msr, pdid1, bizarro, poffset, pmax, pmin) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(poffset<<8)|(pmax>>12), .lo=(pmax<<20)|pmin}}
87 #define P2D_SC(msr, pdid1, bizarro, wen, ren,pscbase) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(wen), .lo=(ren<<16)|(pscbase>>18)}}
88 #define IOD_BM(msr, pdid1, bizarro, ibase, imask) {msr, {.hi=(pdid1<<29)|(bizarro<<28)|(ibase>>12), .lo=(ibase<<20)|imask}}
89 #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)}}
90
91
92
93 struct msr_defaults {
94         int msr_no;
95         msr_t msr;
96 } msr_defaults [] = {
97         {0x1700, {.hi = 0, .lo = IM_QWAIT}},
98         {0x1800, {.hi = DMCF_WRITE_SERIALIZE_REQUEST, .lo = DMCF_SERIAL_LOAD_MISSES}},
99         /* 1808 will be done down below, so we have to do 180a->1817 (well, 1813 really) */
100         /* for 180a, for now, we assume VSM will configure it */
101         /* 180b is left at reset value,a0000-bffff is non-cacheable */
102         /* 180c, c0000-dffff is set to write serialize and non-cachable */
103         /* oops, 180c will be set by cpu bug handling in cpubug.c */
104         //{0x180c, {.hi = MSR_WS_CD_DEFAULT, .lo = MSR_WS_CD_DEFAULT}},
105         /* 180d is left at default, e0000-fffff is non-cached */
106
107         /* we will assume 180e, the ssm region configuration, is left at default or set by VSM */
108         /* we will not set 0x180f, the DMM,yet */
109         //{0x1810, {.hi=0xee7ff000, .lo=RRCF_LOW(0xee000000, WRITE_COMBINE|CACHE_DISABLE)}},
110         //{0x1811, {.hi = 0xefffb000, .lo = RRCF_LOW_CD(0xefff8000)}},
111         //{0x1812, {.hi = 0xefff7000, .lo = RRCF_LOW_CD(0xefff4000)}},
112         //{0x1813, {.hi = 0xefff3000, .lo = RRCF_LOW_CD(0xefff0000)}},
113         /* now for GLPCI routing */
114         /* GLIU0 */
115         P2D_BM(0x10000020, 0x1, 0x0, 0x0, 0xfff80),
116         P2D_BM(0x10000021, 0x1, 0x0, 0x80000, 0xfffe0),
117         P2D_SC(0x1000002c, 0x1, 0x0, 0x0,  0xff03, 0xC0000),
118         /* GLIU1 */
119         P2D_BM(0x40000020, 0x1, 0x0, 0x0, 0xfff80),
120         P2D_BM(0x40000021, 0x1, 0x0, 0x80000, 0xfffe0),
121         P2D_SC(0x4000002d, 0x1, 0x0, 0x0,  0xff03, 0xC0000),
122         {0}
123 };
124
125 /* note that dev is NOT used -- yet */
126 static void irq_init_steering(struct device *dev, uint16_t irq_map) {
127         /* Set up IRQ steering */
128         uint32_t pciAddr = 0x80000000 | (CHIPSET_DEV_NUM << 11) | 0x5C;
129
130         printk_debug("%s(%08X [%08X], %04X)\n", __func__, dev, pciAddr, irq_map);
131
132         /* The IRQ steering values (in hex) are effectively dcba, where:
133          *    <a> represents the IRQ for INTA, 
134          *    <b> represents the IRQ for INTB,
135          *    <c> represents the IRQ for INTC, and
136          *    <d> represents the IRQ for INTD.
137          * Thus, a value of irq_map = 0xAA5B translates to:
138          *    INTA = IRQB (IRQ 11)
139          *    INTB = IRQ5 (IRQ 5)
140          *    INTC = IRQA (IRQ 10)
141          *    INTD = IRQA (IRQ 10)
142          */
143         outl(pciAddr & ~3, 0xCF8);
144         outl(irq_map,      0xCFC);
145 }
146
147
148 /*
149  * setup_gx2_cache
150  *
151  * Returns the amount of memory (in KB) available to the system.  This is the 
152  * total amount of memory less the amount of memory reserved for SMM use.
153  *
154  */ 
155 static int
156 setup_gx2_cache(void)
157 {
158         msr_t msr;
159         unsigned long long val;
160         int sizekbytes, sizereg;
161
162         sizekbytes = sizeram() * 1024;
163         printk_debug("setup_gx2_cache: enable for %d KB\n", sizekbytes);
164         /* build up the rconf word. */
165         /* the SYSTOP bits 27:8 are actually the top bits from 31:12. Book fails to say that */
166         /* set romrp */
167         val = ((unsigned long long) ROM_PROPERTIES) << 56;
168         /* make rom base useful for 1M roms */
169         /* Flash base address -- sized for 1M for now*/
170         val |= ((unsigned long long) 0xfff00)<<36;
171         /* set the devrp properties */
172         val |= ((unsigned long long) DEVICE_PROPERTIES) << 28;
173         /* Take our TOM, RIGHT shift 12, since it page-aligned, then LEFT-shift 8 for reg. */
174         /* yank off memory for the SMM handler */
175         sizekbytes -= SMM_SIZE;
176         sizereg = sizekbytes;
177         sizereg *= 1024;        // convert to bytes
178         sizereg >>= 12;
179         sizereg <<= 8;
180         val |= sizereg;
181         val |= RAM_PROPERTIES;
182         msr.lo = val;
183         msr.hi = (val >> 32);
184         printk_debug("msr 0x%08X will be set to %08x:%08x\n", CPU_RCONF_DEFAULT, msr.hi, msr.lo);
185         wrmsr(CPU_RCONF_DEFAULT, msr);
186
187         enable_cache();
188         wbinvd();
189         return sizekbytes;
190 }
191
192 /* we have to do this here. We have not found a nicer way to do it */
193 void
194 setup_gx2(void)
195 {
196
197         unsigned long tmp, tmp2;
198         msr_t msr;
199         unsigned long size_kb, membytes;
200
201         size_kb = setup_gx2_cache();
202
203         membytes = size_kb * 1024;
204         /* NOTE! setup_gx2_cache returns the SIZE OF RAM - RAMADJUST!
205           * so it is safe to use. You should NOT at this point call     
206           * sizeram() directly. 
207           */
208
209         /* we need to set 0x10000028 and 0x40000029 */
210         /*
211          * These two descriptors cover the range from 1 MB (0x100000) to 
212          * SYSTOP (a.k.a. TOM, or Top of Memory)
213          */
214
215 #if 0
216         /* This has already been done elsewhere */
217         printk_debug("size_kb 0x%x, membytes 0x%x\n", size_kb, membytes);
218         msr.hi = 0x20000000 | membytes>>24;
219         msr.lo = 0x100 | ( ((membytes >>12) & 0xfff) << 20);
220         wrmsr(0x10000028, msr);
221         msr.hi = 0x20000000 | membytes>>24;
222         msr.lo = 0x100 | ( ((membytes >>12) & 0xfff) << 20);
223         wrmsr(0x40000029, msr);
224 #endif
225 #if 0
226         msr = rdmsr(0x10000028);
227         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x10000028, msr.hi,msr.lo);
228         msr = rdmsr(0x40000029);
229         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x40000029, msr.hi,msr.lo);
230 #endif
231 #if 1
232         /* fixme: SMM MSR 0x10000026 and 0x400000023 */
233         /* calculate the OFFSET field */
234         tmp = membytes - SMM_OFFSET;
235         tmp >>= 12;
236         tmp <<= 8;
237         tmp |= 0x20000000;
238         tmp |= (SMM_OFFSET >> 24);
239
240         /* calculate the PBASE and PMASK fields */
241         tmp2 = (SMM_OFFSET << 8) & 0xFFF00000; /* shift right 12 then left 20  == left 8 */
242         tmp2 |= (((~(SMM_SIZE * 1024) + 1) >> 12) & 0xfffff);
243         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x10000026, tmp, tmp2);
244         msr.hi = tmp;
245         msr.lo = tmp2;
246         wrmsr(0x10000026, msr);
247 #endif
248 #if 0
249
250         msr.hi = 0x2cfbc040;
251         msr.lo = 0x400fffc0;
252         wrmsr(0x10000026, msr);
253         msr = rdmsr(0x10000026);
254         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x10000026, msr.hi, msr.lo);
255 #endif
256 #if 0
257         msr.hi = 0x22fffc02;
258         msr.lo = 0x10ffbf00;
259         wrmsr(0x1808, msr);
260         msr = rdmsr(0x1808);
261         printk_debug("MSR 0x%x is now 0x%x:0x%x\n", 0x1808, msr.hi, msr.lo);
262 #endif
263 #if 0   // SDG - don't do this
264         /* now do the default MSR values */
265         for(i = 0; msr_defaults[i].msr_no; i++) {
266                 msr_t msr;
267                 wrmsr(msr_defaults[i].msr_no, msr_defaults[i].msr);     // MSR - see table above
268                 msr = rdmsr(msr_defaults[i].msr_no);
269                 printk_debug("MSR 0x%08X is now 0x%08X:0x%08X\n", msr_defaults[i].msr_no, msr.hi,msr.lo);
270         }
271 #endif
272 }
273
274 static void enable_shadow(device_t dev)
275 {
276         
277 }
278
279 static void northbridge_init(device_t dev) 
280 {
281         unsigned long m;
282
283         struct northbridge_amd_gx2_config *nb = (struct northbridge_amd_gx2_config *)dev->chip_info;
284         printk_debug("northbridge: %s()\n", __func__);
285         
286         enable_shadow(dev);
287         irq_init_steering(dev, nb->irqmap);
288
289         /* HACK HACK HACK HACK */
290         /* 0x1000 is where GPIO is being assigned */
291         m = inl(0x1038);
292         m &= ~GPIOL_12_SET;
293         m |= GPIOL_12_CLEAR;
294         outl(m, 0x1038);
295 }
296
297 /* due to vsa interactions, we need not not touch the nb settings ... */
298 /* this is a test -- we are not sure it will work -- but it ought to */
299 static void set_resources(struct device *dev)
300 {
301         struct resource *resource, *last;
302         unsigned link;
303         uint8_t line;
304
305 #if 0
306         last = &dev->resource[dev->resources];
307
308         for(resource = &dev->resource[0]; resource < last; resource++) {
309                 pci_set_resource(dev, resource);
310         }
311 #endif
312         for(link = 0; link < dev->links; link++) {
313                 struct bus *bus;
314                 bus = &dev->link[link];
315                 if (bus->children) {
316                         assign_resources(bus);
317                 }
318         }
319
320 #if 0
321         /* set a default latency timer */
322         pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
323
324         /* set a default secondary latency timer */
325         if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
326                 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
327         }
328
329         /* zero the irq settings */
330         line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
331         if (line) {
332                 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
333         }
334         /* set the cache line size, so far 64 bytes is good for everyone */
335         pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
336 #endif
337 }
338
339
340
341 static struct device_operations northbridge_operations = {
342         .read_resources   = pci_dev_read_resources,
343 #if 0
344         .set_resources    = pci_dev_set_resources,
345 #endif
346         .set_resources    = set_resources,
347         .enable_resources = pci_dev_enable_resources,
348         .init             = northbridge_init,
349         .enable           = 0,
350         .ops_pci          = 0,
351 };
352
353 static const struct pci_driver northbridge_driver __pci_driver = {
354         .ops = &northbridge_operations,
355         .vendor = PCI_VENDOR_ID_NS,
356         .device = PCI_DEVICE_ID_NS_GX2,
357 };
358
359 static void ram_resource(device_t dev, unsigned long index,
360         unsigned long basek, unsigned long sizek)
361 {
362         struct resource *resource;
363
364         if (!sizek) {
365                 return;
366         }
367         resource = new_resource(dev, index);
368         resource->base  = ((resource_t)basek) << 10;
369         resource->size  = ((resource_t)sizek) << 10;
370         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
371                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
372 }
373
374 static void tolm_test(void *gp, struct device *dev, struct resource *new)
375 {
376         struct resource **best_p = gp;
377         struct resource *best;
378         best = *best_p;
379         if (!best || (best->base > new->base)) {
380                 best = new;
381         }
382         *best_p = best;
383 }
384
385 #if 0
386 static uint32_t find_pci_tolm(struct bus *bus)
387 {
388         struct resource *min;
389         uint32_t tolm;
390         min = 0;
391         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
392         tolm = 0xffffffffUL;
393         if (min && tolm > min->base) {
394                 tolm = min->base;
395         }
396         return tolm;
397 }
398 #endif
399 #define FRAMEBUFFERK 4096
400
401 static void pci_domain_set_resources(device_t dev)
402 {
403 #if 0
404         device_t mc_dev;
405         uint32_t pci_tolm;
406
407         pci_tolm = find_pci_tolm(&dev->link[0]);
408         mc_dev = dev->link[0].children;
409         if (mc_dev) {
410                 unsigned int tomk, tolmk;
411                 unsigned int ramreg = 0;
412                 int i, idx;
413                 unsigned int *bcdramtop = (unsigned int *)(GX_BASE + BC_DRAM_TOP);
414                 unsigned int *mcgbaseadd = (unsigned int *)(GX_BASE + MC_GBASE_ADD);
415
416                 for(i=0; i<0x20; i+= 0x10) {
417                         unsigned int *mcreg = (unsigned int *)(GX_BASE + MC_BANK_CFG);
418                         unsigned int mem_config = *mcreg;
419
420                         if (((mem_config & (DIMM_PG_SZ << i)) >> (4 + i)) == 7)
421                                 continue;
422                         ramreg += 1 << (((mem_config & (DIMM_SZ << i)) >> (i + 8)) + 2);
423                 }
424                         
425                 tomk = ramreg << 10;
426
427                 /* Sort out the framebuffer size */
428                 tomk -= FRAMEBUFFERK;
429                 *bcdramtop = ((tomk << 10) - 1);
430                 *mcgbaseadd = (tomk >> 9);
431
432                 printk_debug("BC_DRAM_TOP = 0x%08x\n", *bcdramtop);
433                 printk_debug("MC_GBASE_ADD = 0x%08x\n", *mcgbaseadd);
434
435                 printk_debug("I would set ram size to %d Mbytes\n", (tomk >> 10));
436
437                 /* Compute the top of Low memory */
438                 tolmk = pci_tolm >> 10;
439                 if (tolmk >= tomk) {
440                         /* The PCI hole does does not overlap the memory.
441                          */
442                         tolmk = tomk;
443                 }
444                 /* Report the memory regions */
445                 idx = 10;
446                 ram_resource(dev, idx++, 0, tolmk);
447         }
448 #endif
449         assign_resources(&dev->link[0]);
450 }
451
452 static struct device_operations pci_domain_ops = {
453         .read_resources   = pci_domain_read_resources,
454         .set_resources    = pci_domain_set_resources,
455         .enable_resources = enable_childrens_resources,
456         .init             = 0,
457         .scan_bus         = pci_domain_scan_bus,
458 };  
459
460 static void cpu_bus_init(device_t dev)
461 {
462         initialize_cpus(&dev->link[0]);
463 }
464
465 static void cpu_bus_noop(device_t dev)
466 {
467 }
468
469 static struct device_operations cpu_bus_ops = {
470         .read_resources   = cpu_bus_noop,
471         .set_resources    = cpu_bus_noop,
472         .enable_resources = cpu_bus_noop,
473         .init             = cpu_bus_init,
474         .scan_bus         = 0,
475 };
476
477 void chipsetInit (void);
478
479 #if CONFIG_WRITE_HIGH_TABLES==1
480 #define HIGH_TABLES_SIZE 64     // maximum size of high tables in KB
481 extern uint64_t high_tables_base, high_tables_size;
482 #endif
483
484 static void enable_dev(struct device *dev)
485 {
486         printk_debug("gx2 north: enable_dev\n");
487         void northbridgeinit(void);
488         void chipsetinit(struct northbridge_amd_gx2_config *nb);
489         void do_vsmbios(void);
490         /* Set the operations if it is a special bus type */
491         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
492                 struct northbridge_amd_gx2_config *nb = (struct northbridge_amd_gx2_config *)dev->chip_info;
493                 extern void cpubug(void);
494                 u32 tomk;
495                 printk_debug("DEVICE_PATH_PCI_DOMAIN\n");
496                 /* cpubug MUST be called before setup_gx2(), so we force the issue here */
497                 northbridgeinit();
498                 cpubug();       
499                 chipsetinit(nb);
500                 setup_gx2();
501                 do_vsmbios();
502                 graphics_init();
503                 dev->ops = &pci_domain_ops;
504                 pci_set_method(dev);
505                 tomk = ((sizeram() - VIDEO_MB) * 1024) - SMM_SIZE;
506 #if CONFIG_WRITE_HIGH_TABLES==1
507                 /* Leave some space for ACPI, PIRQ and MP tables */
508                 high_tables_base = (tomk - HIGH_TABLES_SIZE) * 1024;
509                 high_tables_size = HIGH_TABLES_SIZE * 1024;
510 #endif
511                 ram_resource(dev, 0, 0, tomk);
512         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
513                 printk_debug("DEVICE_PATH_APIC_CLUSTER\n");
514                 dev->ops = &cpu_bus_ops;
515         }
516         printk_debug("gx2 north: end enable_dev\n");
517 }
518
519 struct chip_operations northbridge_amd_gx2_ops = {
520         CHIP_NAME("AMD GX (previously GX2) Northbridge")
521         .enable_dev = enable_dev, 
522 };