Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 1 | #include <console/console.h> |
| 2 | #include <arch/io.h> |
| 3 | #include <stdint.h> |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 4 | #include <device/device.h> |
| 5 | #include <device/pci.h> |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <bitops.h> |
| 9 | #include "chip.h" |
| 10 | #include "northbridge.h" |
| 11 | |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 12 | #define BRIDGE_IO_MASK (IORESOURCE_IO | IORESOURCE_MEM) |
| 13 | |
| 14 | static void pci_domain_read_resources(device_t dev) |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 15 | { |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 16 | struct resource *resource; |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 17 | |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 18 | /* Initialize the system wide io space constraints */ |
| 19 | resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0,0)); |
| 20 | resource->limit = 0xffffUL; |
| 21 | resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 22 | |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 23 | /* Initialize the system wide memory resources constraints */ |
| 24 | resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1,0)); |
| 25 | resource->limit = 0xffffffffULL; |
| 26 | resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE | IORESOURCE_ASSIGNED; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Stefan Reinauer | e4932dc | 2004-11-02 20:33:12 +0000 | [diff] [blame] | 29 | static void ram_resource(device_t dev, unsigned long index, |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 30 | unsigned long basek, unsigned long sizek) |
Stefan Reinauer | e4932dc | 2004-11-02 20:33:12 +0000 | [diff] [blame] | 31 | { |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 32 | struct resource *resource; |
Stefan Reinauer | e4932dc | 2004-11-02 20:33:12 +0000 | [diff] [blame] | 33 | |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 34 | if (!sizek) { |
| 35 | return; |
| 36 | } |
| 37 | resource = new_resource(dev, index); |
| 38 | resource->base = ((resource_t)basek) << 10; |
| 39 | resource->size = ((resource_t)sizek) << 10; |
| 40 | resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \ |
| 41 | IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED; |
Stefan Reinauer | e4932dc | 2004-11-02 20:33:12 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 44 | static void tolm_test(void *gp, struct device *dev, struct resource *new) |
| 45 | { |
| 46 | struct resource **best_p = gp; |
| 47 | struct resource *best; |
| 48 | best = *best_p; |
| 49 | if (!best || (best->base > new->base)) { |
| 50 | best = new; |
| 51 | } |
| 52 | *best_p = best; |
| 53 | } |
| 54 | |
| 55 | static uint32_t find_pci_tolm(struct bus *bus) |
| 56 | { |
| 57 | struct resource *min; |
| 58 | uint32_t tolm; |
| 59 | min = 0; |
| 60 | search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min); |
| 61 | tolm = 0xffffffffUL; |
| 62 | if (min && tolm > min->base) { |
| 63 | tolm = min->base; |
| 64 | } |
| 65 | return tolm; |
| 66 | } |
Stefan Reinauer | e4932dc | 2004-11-02 20:33:12 +0000 | [diff] [blame] | 67 | |
Myles Watson | 0520d55 | 2009-05-11 22:44:14 +0000 | [diff] [blame^] | 68 | #if HAVE_HIGH_TABLES==1 |
| 69 | #define HIGH_TABLES_SIZE 64 // maximum size of high tables in KB |
| 70 | extern uint64_t high_tables_base, high_tables_size; |
| 71 | #endif |
| 72 | |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 73 | static void pci_domain_set_resources(device_t dev) |
| 74 | { |
Ronald G. Minnich | 9cf642b | 2006-09-13 04:12:35 +0000 | [diff] [blame] | 75 | static const uint8_t ramregs[] = { |
| 76 | 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x56, 0x57 |
| 77 | }; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 78 | device_t mc_dev; |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 79 | uint32_t pci_tolm; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 80 | |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 81 | pci_tolm = find_pci_tolm(&dev->link[0]); |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 82 | mc_dev = dev->link[0].children; |
| 83 | if (mc_dev) { |
| 84 | unsigned long tomk, tolmk; |
Ronald G. Minnich | 9cf642b | 2006-09-13 04:12:35 +0000 | [diff] [blame] | 85 | unsigned char rambits; |
| 86 | int i, idx; |
| 87 | |
Carl-Daniel Hailfinger | 2ee6779 | 2008-10-01 12:52:52 +0000 | [diff] [blame] | 88 | for(rambits = 0, i = 0; i < ARRAY_SIZE(ramregs); i++) { |
Ronald G. Minnich | 9cf642b | 2006-09-13 04:12:35 +0000 | [diff] [blame] | 89 | unsigned char reg; |
| 90 | reg = pci_read_config8(mc_dev, ramregs[i]); |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 91 | /* these are ENDING addresses, not sizes. |
Ronald G. Minnich | 9cf642b | 2006-09-13 04:12:35 +0000 | [diff] [blame] | 92 | * if there is memory in this slot, then reg will be > rambits. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 93 | * So we just take the max, that gives us total. |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 94 | * We take the highest one to cover for once and future coreboot |
Ronald G. Minnich | 9cf642b | 2006-09-13 04:12:35 +0000 | [diff] [blame] | 95 | * bugs. We warn about bugs. |
| 96 | */ |
| 97 | if (reg > rambits) |
| 98 | rambits = reg; |
| 99 | if (reg < rambits) |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 100 | printk_err("ERROR! register 0x%x is not set!\n", |
Ronald G. Minnich | 9cf642b | 2006-09-13 04:12:35 +0000 | [diff] [blame] | 101 | ramregs[i]); |
| 102 | } |
| 103 | if (rambits == 0) { |
| 104 | printk_err("RAM size config registers are empty; defaulting to 64 MBytes\n"); |
| 105 | rambits = 8; |
| 106 | } |
| 107 | printk_debug("I would set ram size to 0x%x Kbytes\n", (rambits)*8*1024); |
| 108 | tomk = rambits*8*1024; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 109 | /* Compute the top of Low memory */ |
| 110 | tolmk = pci_tolm >> 10; |
| 111 | if (tolmk >= tomk) { |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 112 | /* The PCI hole does not overlap the memory. */ |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 113 | tolmk = tomk; |
| 114 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 115 | |
| 116 | /* Report the memory regions. */ |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 117 | idx = 10; |
Myles Watson | 0520d55 | 2009-05-11 22:44:14 +0000 | [diff] [blame^] | 118 | ram_resource(dev, idx++, 0, 640); |
| 119 | ram_resource(dev, idx++, 768, tolmk - 768); |
| 120 | |
| 121 | #if HAVE_HIGH_TABLES==1 |
| 122 | /* Leave some space for ACPI, PIRQ and MP tables */ |
| 123 | high_tables_base = (tomk - HIGH_TABLES_SIZE) * 1024; |
| 124 | high_tables_size = HIGH_TABLES_SIZE * 1024; |
| 125 | #endif |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 126 | } |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 127 | assign_resources(&dev->link[0]); |
| 128 | } |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 129 | |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 130 | static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max) |
| 131 | { |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 132 | max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max); |
| 133 | return max; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static struct device_operations pci_domain_ops = { |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 137 | .read_resources = pci_domain_read_resources, |
| 138 | .set_resources = pci_domain_set_resources, |
| 139 | .enable_resources = enable_childrens_resources, |
| 140 | .init = 0, |
| 141 | .scan_bus = pci_domain_scan_bus, |
| 142 | }; |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 143 | |
| 144 | static void enable_dev(struct device *dev) |
| 145 | { |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 146 | /* Set the operations if it is a special bus type */ |
| 147 | if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) { |
| 148 | dev->ops = &pci_domain_ops; |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 149 | pci_set_method(dev); |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 150 | } |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Myles Watson | b8c2aa2 | 2008-02-07 20:37:37 +0000 | [diff] [blame] | 153 | struct chip_operations cpu_emulation_qemu_x86_ops = { |
Eric Biederman | 018d8dd | 2004-11-04 11:04:33 +0000 | [diff] [blame] | 154 | CHIP_NAME("QEMU Northbridge") |
Eric Biederman | 6e53f50 | 2004-10-27 08:53:57 +0000 | [diff] [blame] | 155 | .enable_dev = enable_dev, |
Stefan Reinauer | e2b53e1 | 2004-06-28 11:59:45 +0000 | [diff] [blame] | 156 | }; |
Stefan Reinauer | 246ae21 | 2005-09-08 17:17:25 +0000 | [diff] [blame] | 157 | |
| 158 | void udelay(int usecs) |
| 159 | { |
| 160 | int i; |
| 161 | for(i = 0; i < usecs; i++) |
Stefan Reinauer | 8e65adb | 2008-08-13 12:16:15 +0000 | [diff] [blame] | 162 | inb(0x80); |
Stefan Reinauer | 246ae21 | 2005-09-08 17:17:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |