1 #include <console/console.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>
15 #include "northbridge.h"
18 struct mem_range *sizeram(void)
20 unsigned long mmio_basek;
21 static struct mem_range mem[10];
25 #warning "FIXME handle interleaved nodes"
26 dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
28 printk_err("Cannot find PCI: 0:18.1\n");
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);
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);
42 printk_debug("mmio_base: %dKB\n", mmio_basek);
45 for (idx = i = 0; i < 8; i++) {
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))) {
53 basek = (base & 0xffff0000) >> 2;
54 limitk = ((limit + 0x00010000) & 0xffff0000) >> 2;
55 sizek = limitk - basek;
57 ((mem[idx - 1].basek + mem[idx - 1].sizek) == basek)) {
58 mem[idx -1].sizek += sizek;
60 mem[idx].basek = basek;
61 mem[idx].sizek = sizek;
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;
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) {
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;
86 if ((mem[idx - 1].basek + mem[idx - 1].sizek) <= 4*1024*1024) {
89 mem[idx - 1].basek = 4*1024*1024;
90 mem[idx - 1].sizek -= (4*1024*1024 - mmio_basek);
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);
100 while (idx < sizeof(mem)/sizeof(mem[0])) {
109 static device_t __f1_dev[F1_DEVS];
112 static void debug_f1_devs(void)
115 for (i = 0; i < F1_DEVS; i++) {
119 printk_debug("__f1_dev[%d]: %s bus: %p\n",
120 i, dev_path(dev), dev->bus);
126 static void get_f1_devs(void)
132 for (i = 0; i < F1_DEVS; i++) {
133 __f1_dev[i] = dev_find_slot(0, PCI_DEVFN(0x18 + i, 1));
136 die("Cannot find 0:0x18.1\n");
140 static uint32_t f1_read_config32(unsigned reg)
143 return pci_read_config32(__f1_dev[0], reg);
146 static void f1_write_config32(unsigned reg, uint32_t value)
150 for (i = 0; i < F1_DEVS; i++) {
154 pci_write_config32(dev, reg, value);
159 static unsigned int amdk8_nodeid(device_t dev)
161 return (dev->path.u.pci.devfn >> 3) - 0x18;
164 static unsigned int amdk8_scan_chains(device_t dev, unsigned int max)
169 nodeid = amdk8_nodeid(dev);
172 printk_debug("amdk8_scan_chains max: %d starting...\n", max);
175 for (link = 0; link < dev->links; link++) {
177 uint32_t busses, config_busses;
178 unsigned free_reg, config_reg;
179 dev->link[link].cap = 0x80 + (link *0x20);
181 link_type = pci_read_config32(dev, dev->link[link].cap + 0x18);
182 } while(link_type & ConnectionPending);
183 if (!(link_type & LinkConnected)) {
187 link_type = pci_read_config32(dev, dev->link[link].cap + 0x18);
188 } while(!(link_type & InitComplete));
189 if (!(link_type & NonCoherent)) {
192 /* See if there is an available configuration space mapping register
195 for (config_reg = 0xe0; config_reg <= 0xec; config_reg += 4) {
197 config = f1_read_config32(config_reg);
198 if (!free_reg && ((config & 3) == 0)) {
199 free_reg = config_reg;
202 if (((config & 3) == 3) &&
203 (((config >> 4) & 7) == nodeid) &&
204 (((config >> 8) & 3) == link)) {
208 if (free_reg && (config_reg > 0xec)) {
209 config_reg = free_reg;
211 /* If we can't find an available configuration space mapping
212 * register skip this bus */
213 if (config_reg > 0xec) {
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.
221 dev->link[link].secondary = ++max;
222 dev->link[link].subordinate = 0xff;
224 /* Read the existing primary/secondary/subordinate bus
225 * number configuration.
227 busses = pci_read_config32(dev, dev->link[link].cap + 0x14);
228 config_busses = f1_read_config32(config_reg);
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
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);
240 config_busses &= 0x000fc88;
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);
250 printk_debug("Hyper transport scan link: %d max: %d\n",
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);
259 printk_debug("Hyper transport scan link: %d new max: %d\n",
263 /* We know the number of busses behind this bridge. Set the
264 * subordinate bus number to it's real value
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);
271 config_busses = (config_busses & 0x00ffffff) |
272 (dev->link[link].subordinate << 24);
273 f1_write_config32(config_reg, config_busses);
275 printk_debug("Hypertransport scan link done\n");
279 printk_debug("amdk8_scan_chains max: %d done\n", max);
284 static unsigned amdk8_find_iopair(unsigned nodeid, unsigned link)
286 unsigned free_reg, reg;
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)) {
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)) {
304 /* If I didn't find an exact match return a free register */
308 /* Return an available I/O pair or 0 on failure */
312 static unsigned amdk8_find_mempair(unsigned nodeid, unsigned link)
314 unsigned free_reg, reg;
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)) {
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)) {
332 /* If I didn't find an exact match return a free register */
336 /* Return an available I/O pair or 0 on failure */
340 static void amdk8_link_read_bases(device_t dev, unsigned nodeid, unsigned link)
342 unsigned int reg = dev->resources;
345 /* Initialize the io space constraints on the current bus */
346 index = amdk8_find_iopair(nodeid, link);
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);
360 /* Initialize the memory constraints on the current bus */
361 index = amdk8_find_mempair(nodeid, link);
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);
374 dev->resources = reg;
377 static void amdk8_read_resources(device_t dev)
379 unsigned nodeid, link;
380 nodeid = amdk8_nodeid(dev);
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);
390 static void amdk8_set_resource(device_t dev, struct resource *resource,
393 unsigned long rbase, rlimit;
396 /* Make certain the resource has actually been set */
397 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
401 /* If I have already stored this resource don't worry about it */
402 if (resource->flags & IORESOURCE_STORED) {
406 /* Only handle PCI memory and IO resources */
407 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
410 /* Get the base address */
411 rbase = resource->base;
413 /* Get the limit (rounded up) */
414 rlimit = rbase + ((resource->size + resource->align - 1UL) &
415 ~(resource->align -1)) - 1UL;
417 /* Get the register and link */
418 reg = resource->index & ~3;
419 link = resource->index & 3;
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);
428 base |= rbase & 0x01fff000;
431 limit |= rlimit & 0x01fff000;
432 limit |= (link & 3) << 4;
433 limit |= (nodeid & 7);
435 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
436 base |= PCI_IO_BASE_VGA_EN;
438 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
439 base |= PCI_IO_BASE_NO_ISA;
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);
451 base |= (rbase & 0xffff0000) >> 8;
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);
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");
466 static void amdk8_set_resources(device_t dev)
468 unsigned nodeid, link;
471 /* Find the nodeid */
472 nodeid = amdk8_nodeid(dev);
474 /* Set each resource we have found */
475 for (i = 0; i < dev->resources; i++) {
476 amdk8_set_resource(dev, &dev->resource[i], nodeid);
479 for (link = 0; link < dev->links; link++) {
481 bus = &dev->link[link];
483 assign_resources(bus);
489 * @brief Scan root bus for AMD K8 systems
491 * @param root the root device structure
492 * @max the current bus number scanned so far, usually 0x00
494 * The root device in a AMD K8 system is not at Bus 0, Device 0, Fun 0
495 * as other PCI based systems. The northbridge is at Bus 0, Device 0x18,
496 * Fun 0. We have to call the pci_scan_bus() with PCI_DEVFN(0x18,0) as
497 * the starting device instead of PCI_DEVFN(0x0, 0) as in the default
498 * root_dev_scan_pci_bus().
500 * This function is set up as the default scan_bus() method for mainboards'
501 * device_operations for AMD K8 mainboards in mainboard.c
503 * @see device_operation()
504 * @see root_dev_scan_pci_bus()
506 unsigned int amdk8_scan_root_bus(device_t root, unsigned int max)
510 printk_debug("amdk8_scan_root_bus\n");
512 /* Unmap all of the HT chains */
513 for (reg = 0xe0; reg <= 0xec; reg += 4) {
514 f1_write_config32(reg, 0);
517 printk_debug("amdk8_scan_root_bus: start scaning pci bus\n");
518 max = pci_scan_bus(&root->link[0], PCI_DEVFN(0x18, 0), 0xff, max);
520 printk_debug("amdk8_scan_root_bus: done\n");
524 static void mcf0_control_init(struct device *dev)
529 printk_debug("NB: Function 0 Misc Control.. ");
530 /* improve latency and bandwith on HT */
531 cmd = pci_read_config32(dev, 0x68);
534 pci_write_config32(dev, 0x68, cmd );
538 /* over drive the ht port to 1000 Mhz */
539 cmd = pci_read_config32(dev, 0xa8);
542 pci_write_config32(dev, 0xdc, cmd );
544 printk_debug("done.\n");
548 static void amdk8_enable_resources(struct device *dev)
552 unsigned int vgalink = -1;
554 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
555 ctrl |= dev->link[0].bridge_ctrl;
556 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
557 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
560 /* let's see what link VGA is on */
561 for (link = 0; link < dev->links; link++) {
563 printk_err("Kid %d of k8: bridge ctrl says: 0x%x\n",
564 link, dev->link[link].bridge_ctrl);
565 if (dev->link[link].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
570 /* now find the IOPAIR that goes to vgalink and set the vga
571 * enable in the base part (0x30) */
572 /* now allocate an MMIOPAIR and point it to the CPU0,
574 /* now set IORR1 so it has a hole for the 0xa0000-0xcffff
578 pci_dev_enable_resources(dev);
581 static struct device_operations northbridge_operations = {
582 .read_resources = amdk8_read_resources,
583 .set_resources = amdk8_set_resources,
584 .enable_resources = pci_dev_enable_resources,
585 .init = mcf0_control_init,
586 .scan_bus = amdk8_scan_chains,
590 static struct pci_driver mcf0_driver __pci_driver = {
591 .ops = &northbridge_operations,
592 .vendor = PCI_VENDOR_ID_AMD,
596 static void enumerate(struct chip *chip)
598 chip_enumerate(chip);
599 chip->dev->ops = &northbridge_operations;
602 struct chip_control northbridge_amd_amdk8_control = {
603 .name = "AMD K8 Northbridge",
604 .enumerate = enumerate,