Benjamin Herrenschmidt | 5738ec6 | 2007-12-21 15:39:22 +1100 | [diff] [blame^] | 1 | /* |
| 2 | * PCI / PCI-X / PCI-Express support for 4xx parts |
| 3 | * |
| 4 | * Copyright 2007 Ben. Herrenschmidt <benh@kernel.crashing.org>, IBM Corp. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/pci.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/of.h> |
| 12 | |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/pci-bridge.h> |
| 15 | #include <asm/machdep.h> |
| 16 | |
| 17 | #include "ppc4xx_pci.h" |
| 18 | |
| 19 | static int dma_offset_set; |
| 20 | |
| 21 | /* Move that to a useable header */ |
| 22 | extern unsigned long total_memory; |
| 23 | |
| 24 | static int __init ppc4xx_parse_dma_ranges(struct pci_controller *hose, |
| 25 | void __iomem *reg, |
| 26 | struct resource *res) |
| 27 | { |
| 28 | u64 size; |
| 29 | const u32 *ranges; |
| 30 | int rlen; |
| 31 | int pna = of_n_addr_cells(hose->dn); |
| 32 | int np = pna + 5; |
| 33 | |
| 34 | /* Default */ |
| 35 | res->start = 0; |
| 36 | res->end = size = 0x80000000; |
| 37 | res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH; |
| 38 | |
| 39 | /* Get dma-ranges property */ |
| 40 | ranges = of_get_property(hose->dn, "dma-ranges", &rlen); |
| 41 | if (ranges == NULL) |
| 42 | goto out; |
| 43 | |
| 44 | /* Walk it */ |
| 45 | while ((rlen -= np * 4) >= 0) { |
| 46 | u32 pci_space = ranges[0]; |
| 47 | u64 pci_addr = of_read_number(ranges + 1, 2); |
| 48 | u64 cpu_addr = of_translate_dma_address(hose->dn, ranges + 3); |
| 49 | size = of_read_number(ranges + pna + 3, 2); |
| 50 | ranges += np; |
| 51 | if (cpu_addr == OF_BAD_ADDR || size == 0) |
| 52 | continue; |
| 53 | |
| 54 | /* We only care about memory */ |
| 55 | if ((pci_space & 0x03000000) != 0x02000000) |
| 56 | continue; |
| 57 | |
| 58 | /* We currently only support memory at 0, and pci_addr |
| 59 | * within 32 bits space |
| 60 | */ |
| 61 | if (cpu_addr != 0 || pci_addr > 0xffffffff) { |
| 62 | printk(KERN_WARNING "%s: Ignored unsupported dma range" |
| 63 | " 0x%016llx...0x%016llx -> 0x%016llx\n", |
| 64 | hose->dn->full_name, |
| 65 | pci_addr, pci_addr + size - 1, cpu_addr); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | /* Check if not prefetchable */ |
| 70 | if (!(pci_space & 0x40000000)) |
| 71 | res->flags &= ~IORESOURCE_PREFETCH; |
| 72 | |
| 73 | |
| 74 | /* Use that */ |
| 75 | res->start = pci_addr; |
| 76 | #ifndef CONFIG_RESOURCES_64BIT |
| 77 | /* Beware of 32 bits resources */ |
| 78 | if ((pci_addr + size) > 0x100000000ull) |
| 79 | res->end = 0xffffffff; |
| 80 | else |
| 81 | #endif |
| 82 | res->end = res->start + size - 1; |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | /* We only support one global DMA offset */ |
| 87 | if (dma_offset_set && pci_dram_offset != res->start) { |
| 88 | printk(KERN_ERR "%s: dma-ranges(s) mismatch\n", |
| 89 | hose->dn->full_name); |
| 90 | return -ENXIO; |
| 91 | } |
| 92 | |
| 93 | /* Check that we can fit all of memory as we don't support |
| 94 | * DMA bounce buffers |
| 95 | */ |
| 96 | if (size < total_memory) { |
| 97 | printk(KERN_ERR "%s: dma-ranges too small " |
| 98 | "(size=%llx total_memory=%lx)\n", |
| 99 | hose->dn->full_name, size, total_memory); |
| 100 | return -ENXIO; |
| 101 | } |
| 102 | |
| 103 | /* Check we are a power of 2 size and that base is a multiple of size*/ |
| 104 | if (!is_power_of_2(size) || |
| 105 | (res->start & (size - 1)) != 0) { |
| 106 | printk(KERN_ERR "%s: dma-ranges unaligned\n", |
| 107 | hose->dn->full_name); |
| 108 | return -ENXIO; |
| 109 | } |
| 110 | |
| 111 | /* Check that we are fully contained within 32 bits space */ |
| 112 | if (res->end > 0xffffffff) { |
| 113 | printk(KERN_ERR "%s: dma-ranges outside of 32 bits space\n", |
| 114 | hose->dn->full_name); |
| 115 | return -ENXIO; |
| 116 | } |
| 117 | out: |
| 118 | dma_offset_set = 1; |
| 119 | pci_dram_offset = res->start; |
| 120 | |
| 121 | printk(KERN_INFO "4xx PCI DMA offset set to 0x%08lx\n", |
| 122 | pci_dram_offset); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * 4xx PCI 2.x part |
| 128 | */ |
| 129 | static void __init ppc4xx_probe_pci_bridge(struct device_node *np) |
| 130 | { |
| 131 | /* NYI */ |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * 4xx PCI-X part |
| 136 | */ |
| 137 | |
| 138 | static void __init ppc4xx_configure_pcix_POMs(struct pci_controller *hose, |
| 139 | void __iomem *reg) |
| 140 | { |
| 141 | u32 lah, lal, pciah, pcial, sa; |
| 142 | int i, j; |
| 143 | |
| 144 | /* Setup outbound memory windows */ |
| 145 | for (i = j = 0; i < 3; i++) { |
| 146 | struct resource *res = &hose->mem_resources[i]; |
| 147 | |
| 148 | /* we only care about memory windows */ |
| 149 | if (!(res->flags & IORESOURCE_MEM)) |
| 150 | continue; |
| 151 | if (j > 1) { |
| 152 | printk(KERN_WARNING "%s: Too many ranges\n", |
| 153 | hose->dn->full_name); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | /* Calculate register values */ |
| 158 | #ifdef CONFIG_PTE_64BIT |
| 159 | lah = res->start >> 32; |
| 160 | lal = res->start & 0xffffffffu; |
| 161 | pciah = (res->start - hose->pci_mem_offset) >> 32; |
| 162 | pcial = (res->start - hose->pci_mem_offset) & 0xffffffffu; |
| 163 | #else |
| 164 | lah = pciah = 0; |
| 165 | lal = res->start; |
| 166 | pcial = res->start - hose->pci_mem_offset; |
| 167 | #endif |
| 168 | sa = res->end + 1 - res->start; |
| 169 | if (!is_power_of_2(sa) || sa < 0x100000 || |
| 170 | sa > 0xffffffffu) { |
| 171 | printk(KERN_WARNING "%s: Resource out of range\n", |
| 172 | hose->dn->full_name); |
| 173 | continue; |
| 174 | } |
| 175 | sa = (0xffffffffu << ilog2(sa)) | 0x1; |
| 176 | |
| 177 | /* Program register values */ |
| 178 | if (j == 0) { |
| 179 | writel(lah, reg + PCIX0_POM0LAH); |
| 180 | writel(lal, reg + PCIX0_POM0LAL); |
| 181 | writel(pciah, reg + PCIX0_POM0PCIAH); |
| 182 | writel(pcial, reg + PCIX0_POM0PCIAL); |
| 183 | writel(sa, reg + PCIX0_POM0SA); |
| 184 | } else { |
| 185 | writel(lah, reg + PCIX0_POM1LAH); |
| 186 | writel(lal, reg + PCIX0_POM1LAL); |
| 187 | writel(pciah, reg + PCIX0_POM1PCIAH); |
| 188 | writel(pcial, reg + PCIX0_POM1PCIAL); |
| 189 | writel(sa, reg + PCIX0_POM1SA); |
| 190 | } |
| 191 | j++; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static void __init ppc4xx_configure_pcix_PIMs(struct pci_controller *hose, |
| 196 | void __iomem *reg, |
| 197 | const struct resource *res, |
| 198 | int big_pim, |
| 199 | int enable_msi_hole) |
| 200 | { |
| 201 | resource_size_t size = res->end - res->start + 1; |
| 202 | u32 sa; |
| 203 | |
| 204 | /* RAM is always at 0 */ |
| 205 | writel(0x00000000, reg + PCIX0_PIM0LAH); |
| 206 | writel(0x00000000, reg + PCIX0_PIM0LAL); |
| 207 | |
| 208 | /* Calculate window size */ |
| 209 | sa = (0xffffffffu << ilog2(size)) | 1; |
| 210 | sa |= 0x1; |
| 211 | if (res->flags & IORESOURCE_PREFETCH) |
| 212 | sa |= 0x2; |
| 213 | if (enable_msi_hole) |
| 214 | sa |= 0x4; |
| 215 | writel(sa, reg + PCIX0_PIM0SA); |
| 216 | if (big_pim) |
| 217 | writel(0xffffffff, reg + PCIX0_PIM0SAH); |
| 218 | |
| 219 | /* Map on PCI side */ |
| 220 | writel(0x00000000, reg + PCIX0_BAR0H); |
| 221 | writel(res->start, reg + PCIX0_BAR0L); |
| 222 | writew(0x0006, reg + PCIX0_COMMAND); |
| 223 | } |
| 224 | |
| 225 | static void __init ppc4xx_probe_pcix_bridge(struct device_node *np) |
| 226 | { |
| 227 | struct resource rsrc_cfg; |
| 228 | struct resource rsrc_reg; |
| 229 | struct resource dma_window; |
| 230 | struct pci_controller *hose = NULL; |
| 231 | void __iomem *reg = NULL; |
| 232 | const int *bus_range; |
| 233 | int big_pim = 0, msi = 0, primary = 0; |
| 234 | |
| 235 | /* Fetch config space registers address */ |
| 236 | if (of_address_to_resource(np, 0, &rsrc_cfg)) { |
| 237 | printk(KERN_ERR "%s:Can't get PCI-X config register base !", |
| 238 | np->full_name); |
| 239 | return; |
| 240 | } |
| 241 | /* Fetch host bridge internal registers address */ |
| 242 | if (of_address_to_resource(np, 3, &rsrc_reg)) { |
| 243 | printk(KERN_ERR "%s: Can't get PCI-X internal register base !", |
| 244 | np->full_name); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | /* Check if it supports large PIMs (440GX) */ |
| 249 | if (of_get_property(np, "large-inbound-windows", NULL)) |
| 250 | big_pim = 1; |
| 251 | |
| 252 | /* Check if we should enable MSIs inbound hole */ |
| 253 | if (of_get_property(np, "enable-msi-hole", NULL)) |
| 254 | msi = 1; |
| 255 | |
| 256 | /* Check if primary bridge */ |
| 257 | if (of_get_property(np, "primary", NULL)) |
| 258 | primary = 1; |
| 259 | |
| 260 | /* Get bus range if any */ |
| 261 | bus_range = of_get_property(np, "bus-range", NULL); |
| 262 | |
| 263 | /* Map registers */ |
| 264 | reg = ioremap(rsrc_reg.start, rsrc_reg.end + 1 - rsrc_reg.start); |
| 265 | if (reg == NULL) { |
| 266 | printk(KERN_ERR "%s: Can't map registers !", np->full_name); |
| 267 | goto fail; |
| 268 | } |
| 269 | |
| 270 | /* Allocate the host controller data structure */ |
| 271 | hose = pcibios_alloc_controller(np); |
| 272 | if (!hose) |
| 273 | goto fail; |
| 274 | |
| 275 | hose->first_busno = bus_range ? bus_range[0] : 0x0; |
| 276 | hose->last_busno = bus_range ? bus_range[1] : 0xff; |
| 277 | |
| 278 | /* Setup config space */ |
| 279 | setup_indirect_pci(hose, rsrc_cfg.start, rsrc_cfg.start + 0x4, 0); |
| 280 | |
| 281 | /* Disable all windows */ |
| 282 | writel(0, reg + PCIX0_POM0SA); |
| 283 | writel(0, reg + PCIX0_POM1SA); |
| 284 | writel(0, reg + PCIX0_POM2SA); |
| 285 | writel(0, reg + PCIX0_PIM0SA); |
| 286 | writel(0, reg + PCIX0_PIM1SA); |
| 287 | writel(0, reg + PCIX0_PIM2SA); |
| 288 | if (big_pim) { |
| 289 | writel(0, reg + PCIX0_PIM0SAH); |
| 290 | writel(0, reg + PCIX0_PIM2SAH); |
| 291 | } |
| 292 | |
| 293 | /* Parse outbound mapping resources */ |
| 294 | pci_process_bridge_OF_ranges(hose, np, primary); |
| 295 | |
| 296 | /* Parse inbound mapping resources */ |
| 297 | if (ppc4xx_parse_dma_ranges(hose, reg, &dma_window) != 0) |
| 298 | goto fail; |
| 299 | |
| 300 | /* Configure outbound ranges POMs */ |
| 301 | ppc4xx_configure_pcix_POMs(hose, reg); |
| 302 | |
| 303 | /* Configure inbound ranges PIMs */ |
| 304 | ppc4xx_configure_pcix_PIMs(hose, reg, &dma_window, big_pim, msi); |
| 305 | |
| 306 | /* We don't need the registers anymore */ |
| 307 | iounmap(reg); |
| 308 | return; |
| 309 | |
| 310 | fail: |
| 311 | if (hose) |
| 312 | pcibios_free_controller(hose); |
| 313 | if (reg) |
| 314 | iounmap(reg); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * 4xx PCI-Express part |
| 319 | */ |
| 320 | static void __init ppc4xx_probe_pciex_bridge(struct device_node *np) |
| 321 | { |
| 322 | /* NYI */ |
| 323 | } |
| 324 | |
| 325 | static int __init ppc4xx_pci_find_bridges(void) |
| 326 | { |
| 327 | struct device_node *np; |
| 328 | |
| 329 | for_each_compatible_node(np, NULL, "ibm,plb-pciex") |
| 330 | ppc4xx_probe_pciex_bridge(np); |
| 331 | for_each_compatible_node(np, NULL, "ibm,plb-pcix") |
| 332 | ppc4xx_probe_pcix_bridge(np); |
| 333 | for_each_compatible_node(np, NULL, "ibm,plb-pci") |
| 334 | ppc4xx_probe_pci_bridge(np); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | arch_initcall(ppc4xx_pci_find_bridges); |
| 339 | |