changed dev->enable to dev->enabled. Sorry, I am the only one who can't speak
[coreboot.git] / src / northbridge / amd / amdk8 / northbridge.c
1 #include <console/console.h>
2 #include <arch/io.h>
3 #include <stdint.h>
4 #include <mem.h>
5 #include <part/sizeram.h>
6 #include <device/device.h>
7 #include <device/pci.h>
8 #include <device/pci_ids.h>
9 #include <device/hypertransport.h>
10 #include <device/chip.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <bitops.h>
14 #include "chip.h"
15 #include "northbridge.h"
16 #include "amdk8.h"
17
18 struct mem_range *sizeram(void)
19 {
20         unsigned long mmio_basek;
21         static struct mem_range mem[10];
22         device_t dev;
23         int i, idx;
24
25 #warning "FIXME handle interleaved nodes"
26         dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
27         if (!dev) {
28                 printk_err("Cannot find PCI: 0:18.1\n");
29                 return 0;
30         }
31         mmio_basek = (dev_root.resource[1].base >> 10);
32         /* Round mmio_basek to something the processor can support */
33         mmio_basek &= ~((1 << 6) -1);
34
35 #if 1
36 #warning "FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M MMIO hole"
37         /* Round the mmio hold to 256M */
38         mmio_basek &= ~((256*1024) - 1);
39 #endif
40
41 #if 0
42         printk_debug("mmio_base: %dKB\n", mmio_basek);
43 #endif
44
45         for (idx = i = 0; i < 8; i++) {
46                 uint32_t base, limit;
47                 unsigned basek, limitk, sizek;
48                 base  = pci_read_config32(dev, 0x40 + (i<<3));
49                 limit = pci_read_config32(dev, 0x44 + (i<<3));
50                 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
51                         continue;
52                 }
53                 basek = (base & 0xffff0000) >> 2;
54                 limitk = ((limit + 0x00010000) & 0xffff0000) >> 2;
55                 sizek = limitk - basek;
56                 if ((idx > 0) &&
57                     ((mem[idx - 1].basek + mem[idx - 1].sizek) == basek)) {
58                         mem[idx -1].sizek += sizek;
59                 } else {
60                         mem[idx].basek = basek;
61                         mem[idx].sizek = sizek;
62                         idx++;
63                 }
64         
65                 /* see if we need a hole from 0xa0000 to 0xbffff */
66                 if ((mem[idx-1].basek < ((8*64)+(8*16))) /* 640 */ && 
67                     (mem[idx-1].sizek > ((8*64)+(16*16))) /* 768 */ ) {
68 #warning "FIXME: this left 0xA0000 to 0xBFFFF undefined"
69                         mem[idx].basek = (8*64)+(16*16);
70                         mem[idx].sizek = mem[idx-1].sizek - ((8*64)+(16*16));
71                         mem[idx-1].sizek = ((8*64)+(8*16)) - mem[idx-1].basek;
72                         idx++;
73                 }       
74                 
75                 /* See if I need to split the region to accomodate pci memory space */
76                 if ((mem[idx - 1].basek <= mmio_basek) &&
77                     ((mem[idx - 1].basek + mem[idx - 1].sizek) >  mmio_basek)) {
78                         if (mem[idx - 1].basek < mmio_basek) {
79                                 unsigned pre_sizek;
80                                 pre_sizek = mmio_basek - mem[idx - 1].basek;
81                                 mem[idx].basek = mmio_basek;
82                                 mem[idx].sizek = mem[idx - 1].sizek - pre_sizek;
83                                 mem[idx - 1].sizek = pre_sizek;
84                                 idx++;
85                         }
86                         if ((mem[idx - 1].basek + mem[idx - 1].sizek) <= 4*1024*1024) {
87                                 idx -= 1;
88                         } else {
89                                 mem[idx - 1].basek = 4*1024*1024;
90                                 mem[idx - 1].sizek -= (4*1024*1024 - mmio_basek);
91                         }
92                 }
93         }
94 #if 1
95         for (i = 0; i < idx; i++) {
96                 printk_debug("mem[%d].basek = %08x mem[%d].sizek = %08x\n",
97                              i, mem[i].basek, i, mem[i].sizek);
98         }
99 #endif
100         while (idx < sizeof(mem)/sizeof(mem[0])) {
101                 mem[idx].basek = 0;
102                 mem[idx].sizek = 0;
103                 idx++;
104         }
105         return mem;
106 }
107
108 #define F1_DEVS 8
109 static device_t __f1_dev[F1_DEVS];
110
111 #if 0
112 static void debug_f1_devs(void)
113 {
114         int i;
115         for (i = 0; i < F1_DEVS; i++) {
116                 device_t dev;
117                 dev = __f1_dev[i];
118                 if (dev) {
119                         printk_debug("__f1_dev[%d]: %s bus: %p\n",
120                                      i, dev_path(dev), dev->bus);
121                 }
122         }
123 }
124 #endif
125
126 static void get_f1_devs(void)
127 {
128         int i;
129         if (__f1_dev[0]) {
130                 return;
131         }
132         for (i = 0; i < F1_DEVS; i++) {
133                 __f1_dev[i] = dev_find_slot(0, PCI_DEVFN(0x18 + i, 1));
134         }
135         if (!__f1_dev[0]) {
136                 die("Cannot find 0:0x18.1\n");
137         }
138 }
139
140 static uint32_t f1_read_config32(unsigned reg)
141 {
142         get_f1_devs();
143         return pci_read_config32(__f1_dev[0], reg);
144 }
145
146 static void f1_write_config32(unsigned reg, uint32_t value)
147 {
148         int i;
149         get_f1_devs();
150         for (i = 0; i < F1_DEVS; i++) {
151                 device_t dev;
152                 dev = __f1_dev[i];
153                 if (dev) {
154                         pci_write_config32(dev, reg, value);
155                 }
156         }
157 }
158
159 static unsigned int amdk8_nodeid(device_t dev)
160 {
161         return (dev->path.u.pci.devfn >> 3) - 0x18;
162 }
163
164 static unsigned int amdk8_scan_chains(device_t dev, unsigned int max)
165 {
166         unsigned nodeid;
167         unsigned link;
168
169         nodeid = amdk8_nodeid(dev);
170
171 #if 1
172         printk_debug("amdk8_scan_chains max: %d starting...\n", max);
173 #endif
174
175         for (link = 0; link < dev->links; link++) {
176                 uint32_t link_type;
177                 uint32_t busses, config_busses;
178                 unsigned free_reg, config_reg;
179                 dev->link[link].cap = 0x80 + (link *0x20);
180                 do {
181                         link_type = pci_read_config32(dev, dev->link[link].cap + 0x18);
182                 } while(link_type & ConnectionPending);
183                 if (!(link_type & LinkConnected)) {
184                         continue;
185                 }
186                 do {
187                         link_type = pci_read_config32(dev, dev->link[link].cap + 0x18);
188                 } while(!(link_type & InitComplete));
189                 if (!(link_type & NonCoherent)) {
190                         continue;
191                 }
192                 /* See if there is an available configuration space mapping register
193                  * in function 1. */
194                 free_reg = 0;
195                 for (config_reg = 0xe0; config_reg <= 0xec; config_reg += 4) {
196                         uint32_t config;
197                         config = f1_read_config32(config_reg);
198                         if (!free_reg && ((config & 3) == 0)) {
199                                 free_reg = config_reg;
200                                 continue;
201                         }
202                         if (((config & 3) == 3) && 
203                             (((config >> 4) & 7) == nodeid) &&
204                             (((config >> 8) & 3) == link)) {
205                                 break;
206                         }
207                 }
208                 if (free_reg && (config_reg > 0xec)) {
209                         config_reg = free_reg;
210                 }
211                 /* If we can't find an available configuration space mapping
212                  * register skip this bus */
213                 if (config_reg > 0xec) {
214                         continue;
215                 }
216
217                 /* Set up the primary, secondary and subordinate bus numbers.
218                  * We have no idea how many busses are behind this bridge yet,
219                  * so we set the subordinate bus number to 0xff for the moment.
220                  */
221                 dev->link[link].secondary = ++max;
222                 dev->link[link].subordinate = 0xff;
223
224                 /* Read the existing primary/secondary/subordinate bus
225                  * number configuration.
226                  */
227                 busses = pci_read_config32(dev, dev->link[link].cap + 0x14);
228                 config_busses = f1_read_config32(config_reg);
229                 
230                 /* Configure the bus numbers for this bridge: the configuration
231                  * transactions will not be propagates by the bridge if it is
232                  * not correctly configured
233                  */
234                 busses &= 0xff000000;
235                 busses |= (((unsigned int)(dev->bus->secondary) << 0) |
236                         ((unsigned int)(dev->link[link].secondary) << 8) |
237                         ((unsigned int)(dev->link[link].subordinate) << 16));
238                 pci_write_config32(dev, dev->link[link].cap + 0x14, busses);
239
240                 config_busses &= 0x000fc88;
241                 config_busses |= 
242                         (3 << 0) |  /* rw enable, no device compare */
243                         (( nodeid & 7) << 4) | 
244                         (( link & 3 ) << 8) |  
245                         ((dev->link[link].secondary) << 16) |
246                         ((dev->link[link].subordinate) << 24);
247                 f1_write_config32(config_reg, config_busses);
248
249 #if 1
250                 printk_debug("Hyper transport scan link: %d max: %d\n",
251                              link, max);
252 #endif
253
254                 /* Now we can scan all of the subordinate busses i.e. the
255                  * chain on the hypertranport link */
256                 max = hypertransport_scan_chain(&dev->link[link], max);
257
258 #if 1
259                 printk_debug("Hyper transport scan link: %d new max: %d\n",
260                              link, max);
261 #endif          
262
263                 /* We know the number of busses behind this bridge.  Set the
264                  * subordinate bus number to it's real value
265                  */
266                 dev->link[link].subordinate = max;
267                 busses = (busses & 0xff00ffff) |
268                         ((unsigned int) (dev->link[link].subordinate) << 16);
269                 pci_write_config32(dev, dev->link[link].cap + 0x14, busses);
270
271                 config_busses = (config_busses & 0x00ffffff) |
272                         (dev->link[link].subordinate << 24);
273                 f1_write_config32(config_reg, config_busses);
274 #if 1
275                 printk_debug("Hypertransport scan link done\n");
276 #endif          
277         }
278 #if 1
279         printk_debug("amdk8_scan_chains max: %d done\n", max);
280 #endif
281         return max;
282 }
283
284 static unsigned amdk8_find_iopair(unsigned nodeid, unsigned link)
285 {
286         unsigned free_reg, reg;
287
288         free_reg = 0;
289         for (reg = 0xc0; reg <= 0xd8; reg += 0x8) {
290                 uint32_t base, limit;
291                 base  = f1_read_config32(reg);
292                 limit = f1_read_config32(reg + 0x4);
293                 /* Do I have a free register */
294                 if (!free_reg && ((base & 3) == 0)) {
295                         free_reg = reg;
296                 }
297                 /* Do I have a match for this node and link? */
298                 if (((base & 3) == 3) &&
299                     ((limit & 7) == nodeid) &&
300                     (((limit >> 4) & 3) == link)) {
301                         break;
302                 }
303         }
304         /* If I didn't find an exact match return a free register */
305         if (reg > 0xd8) {
306                 reg = free_reg;
307         }
308         /* Return an available I/O pair or 0 on failure */
309         return reg;
310 }
311
312 static unsigned amdk8_find_mempair(unsigned nodeid, unsigned link)
313 {
314         unsigned free_reg, reg;
315
316         free_reg = 0;
317         for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
318                 uint32_t base, limit;
319                 base  = f1_read_config32(reg);
320                 limit = f1_read_config32(reg + 0x4);
321                 /* Do I have a free register */
322                 if (!free_reg && ((base & 3) == 0)) {
323                         free_reg = reg;
324                 }
325                 /* Do I have a match for this node and link? */
326                 if (((base & 3) == 3) &&
327                         ((limit & 7) == nodeid) &&
328                         (((limit >> 4) & 3) == link)) {
329                         break;
330                 }
331         }
332         /* If I didn't find an exact match return a free register */
333         if (reg > 0xb8) {
334                 reg = free_reg;
335         }
336         /* Return an available I/O pair or 0 on failure */
337         return reg;
338 }
339
340 static void amdk8_link_read_bases(device_t dev, unsigned nodeid, unsigned link)
341 {
342         unsigned int reg = dev->resources;
343         unsigned index;
344         
345         /* Initialize the io space constraints on the current bus */
346         index = amdk8_find_iopair(nodeid, link);
347         if (index) {
348                 dev->resource[reg].base  = 0;
349                 dev->resource[reg].size  = 0;
350                 dev->resource[reg].align = log2(HT_IO_HOST_ALIGN);
351                 dev->resource[reg].gran  = log2(HT_IO_HOST_ALIGN);
352                 dev->resource[reg].limit = 0xffffUL;
353                 dev->resource[reg].flags = IORESOURCE_IO;
354                 dev->resource[reg].index = index | (link & 0x3);
355                 compute_allocate_resource(&dev->link[link], &dev->resource[reg], 
356                                           IORESOURCE_IO, IORESOURCE_IO);
357                 reg++;
358         }
359
360         /* Initialize the memory constraints on the current bus */
361         index = amdk8_find_mempair(nodeid, link);
362         if (index) {
363                 dev->resource[reg].base  = 0;
364                 dev->resource[reg].size  = 0;
365                 dev->resource[reg].align = log2(HT_MEM_HOST_ALIGN);
366                 dev->resource[reg].gran  = log2(HT_MEM_HOST_ALIGN);
367                 dev->resource[reg].limit = 0xffffffffUL;
368                 dev->resource[reg].flags = IORESOURCE_MEM;
369                 dev->resource[reg].index = index | (link & 0x3);
370                 compute_allocate_resource(&dev->link[link], &dev->resource[reg], 
371                                           IORESOURCE_MEM, IORESOURCE_MEM);
372                 reg++;
373         }
374         dev->resources = reg;
375 }
376
377 static void amdk8_read_resources(device_t dev)
378 {
379         unsigned nodeid, link;
380         nodeid = amdk8_nodeid(dev);
381         dev->resources = 0;
382         memset(&dev->resource, 0, sizeof(dev->resource));
383         for (link = 0; link < dev->links; link++) {
384                 if (dev->link[link].children) {
385                         amdk8_link_read_bases(dev, nodeid, link);
386                 }
387         }
388 }
389
390 static void amdk8_set_resource(device_t dev, struct resource *resource,
391                                unsigned nodeid)
392 {
393         unsigned long rbase, rlimit;
394         unsigned reg, link;
395
396         /* Make certain the resource has actually been set */
397         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
398                 return;
399         }
400
401         /* If I have already stored this resource don't worry about it */
402         if (resource->flags & IORESOURCE_STORED) {
403                 return;
404         }
405         
406         /* Only handle PCI memory and IO resources */
407         if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
408                 return;
409
410         /* Get the base address */
411         rbase = resource->base;
412         
413         /* Get the limit (rounded up) */
414         rlimit = rbase + ((resource->size + resource->align - 1UL) &
415                           ~(resource->align -1)) - 1UL;
416
417         /* Get the register and link */
418         reg  = resource->index & ~3;
419         link = resource->index & 3;
420
421         if (resource->flags & IORESOURCE_IO) {
422                 uint32_t base, limit;
423                 compute_allocate_resource(&dev->link[link], resource,
424                                           IORESOURCE_IO, IORESOURCE_IO);
425                 base  = f1_read_config32(reg);
426                 limit = f1_read_config32(reg + 0x4);
427                 base  &= 0xfe000fcc;
428                 base  |= rbase  & 0x01fff000;
429                 base  |= 3;
430                 limit &= 0xfe000fc8;
431                 limit |= rlimit & 0x01fff000;
432                 limit |= (link & 3) << 4;
433                 limit |= (nodeid & 7);
434
435                 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
436                         base |= PCI_IO_BASE_VGA_EN;
437                 }
438                 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
439                         base |= PCI_IO_BASE_NO_ISA;
440                 }
441                 
442                 f1_write_config32(reg + 0x4, limit);
443                 f1_write_config32(reg, base);
444         } else if (resource->flags & IORESOURCE_MEM) {
445                 uint32_t base, limit;
446                 compute_allocate_resource(&dev->link[link], resource,
447                                           IORESOURCE_MEM, IORESOURCE_MEM);
448                 base  = f1_read_config32(reg);
449                 limit = f1_read_config32(reg + 0x4);
450                 base  &= 0x000000f0;
451                 base  |= (rbase & 0xffff0000) >> 8;
452                 base  |= 3;
453                 limit &= 0x00000048;
454                 limit |= (rlimit & 0xffff0000) >> 8;
455                 limit |= (link & 3) << 4;
456                 limit |= (nodeid & 7);
457                 f1_write_config32(reg + 0x4, limit);
458                 f1_write_config32(reg, base);
459         }
460         resource->flags |= IORESOURCE_STORED;
461         printk_debug("%s %02x <- [0x%08lx - 0x%08lx] node %d link %d %s\n",
462                      dev_path(dev), reg, rbase, rlimit, nodeid, link,
463                      (resource->flags & IORESOURCE_IO)? "io": "mem");
464 }
465
466 static void amdk8_set_resources(device_t dev)
467 {
468         unsigned nodeid, link;
469         int i;
470
471         /* Find the nodeid */
472         nodeid = amdk8_nodeid(dev);     
473
474         /* Set each resource we have found */
475         for (i = 0; i < dev->resources; i++) {
476                 amdk8_set_resource(dev, &dev->resource[i], nodeid);
477         }
478
479         for (link = 0; link < dev->links; link++) {
480                 struct bus *bus;
481                 bus = &dev->link[link];
482                 if (bus->children) {
483                         assign_resources(bus);
484                 }
485         }
486 }
487
488 unsigned int amdk8_scan_root_bus(device_t root, unsigned int max)
489 {
490         unsigned reg;
491
492         printk_debug("amdk8_scan_root_bus\n");
493
494         /* Unmap all of the HT chains */
495         printk_debug("amdk8_scan_root_bus: clearing HT registers\n");
496         for (reg = 0xe0; reg <= 0xec; reg += 4) {
497                 f1_write_config32(reg, 0);
498         }
499
500         printk_debug("amdk8_scan_root_bus: start scaning pci bus\n");
501         max = pci_scan_bus(&root->link[0], PCI_DEVFN(0x18, 0), 0xff, max);
502
503         printk_debug("amdk8_scan_root_bus: done\n");
504         return max;
505 }
506
507 static void mcf0_control_init(struct device *dev)
508 {
509         uint32_t cmd;
510
511 #if 1   
512         printk_debug("NB: Function 0 Misc Control.. ");
513         /* improve latency and bandwith on HT */
514         cmd = pci_read_config32(dev, 0x68);
515         cmd &= 0xffff80ff;
516         cmd |= 0x00004800;
517         pci_write_config32(dev, 0x68, cmd );
518 #endif
519
520 #if 0   
521         /* over drive the ht port to 1000 Mhz */
522         cmd = pci_read_config32(dev, 0xa8);
523         cmd &= 0xfffff0ff;
524         cmd |= 0x00000600;
525         pci_write_config32(dev, 0xdc, cmd );
526 #endif  
527         printk_debug("done.\n");
528 }
529
530
531 static void amdk8_enable_resources(struct device *dev)
532 {
533         uint16_t ctrl;
534         unsigned link;
535         unsigned int vgalink = -1;
536
537         ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
538         ctrl |= dev->link[0].bridge_ctrl;
539         printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
540         pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
541
542 #if 0
543         /* let's see what link VGA is on */
544         for (link = 0; link < dev->links; link++) {
545                 device_t child;
546                 printk_err("Kid %d of k8: bridge ctrl says: 0x%x\n",
547                            link, dev->link[link].bridge_ctrl);
548                 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
549                         vgalink = link;
550         }
551
552         if (vgalink != 1) {
553                 /* now find the IOPAIR that goes to vgalink and set the vga
554                  * enable in the base part (0x30) */
555                 /* now allocate an MMIOPAIR and point it to the CPU0,
556                  * LINK=vgalink */
557                 /* now set IORR1 so it has a hole for the 0xa0000-0xcffff
558                  * region */
559         }
560 #endif
561         pci_dev_enable_resources(dev);
562 }
563
564 static struct device_operations northbridge_operations = {
565         .read_resources   = amdk8_read_resources,
566         .set_resources    = amdk8_set_resources,
567         .enable_resources = pci_dev_enable_resources,
568         .init             = mcf0_control_init,
569         .scan_bus         = amdk8_scan_chains,
570         .enable           = 0,
571 };
572
573 static struct pci_driver mcf0_driver __pci_driver = {
574         .ops    = &northbridge_operations,
575         .vendor = PCI_VENDOR_ID_AMD,
576         .device = 0x1100,
577 };
578
579 static void enumerate(struct chip *chip)
580 {
581         chip_enumerate(chip);
582         chip->dev->ops = &northbridge_operations;
583 }
584
585 struct chip_control northbridge_amd_amdk8_control = {
586         .name   = "AMD K8 Northbridge",
587         .enumerate = enumerate,
588 };