wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * arch/ppc/kernel/pci_auto.c |
| 3 | * |
| 4 | * PCI autoconfiguration library |
| 5 | * |
| 6 | * Author: Matt Porter <mporter@mvista.com> |
| 7 | * |
| 8 | * Copyright 2000 MontaVista Software Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | |
| 18 | #ifdef CONFIG_PCI |
| 19 | |
| 20 | #include <pci.h> |
| 21 | |
| 22 | #undef DEBUG |
| 23 | #ifdef DEBUG |
| 24 | #define DEBUGF(x...) printf(x) |
| 25 | #else |
| 26 | #define DEBUGF(x...) |
| 27 | #endif /* DEBUG */ |
| 28 | |
| 29 | #define PCIAUTO_IDE_MODE_MASK 0x05 |
| 30 | |
| 31 | /* |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | void pciauto_region_init(struct pci_region* res) |
| 36 | { |
| 37 | res->bus_lower = res->bus_start; |
| 38 | } |
| 39 | |
| 40 | void pciauto_region_align(struct pci_region *res, unsigned long size) |
| 41 | { |
| 42 | res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 43 | } |
| 44 | |
| 45 | int pciauto_region_allocate(struct pci_region* res, unsigned int size, unsigned int *bar) |
| 46 | { |
| 47 | unsigned long addr; |
| 48 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 49 | if (!res) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | DEBUGF("No resource"); |
| 51 | goto error; |
| 52 | } |
| 53 | |
| 54 | addr = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 55 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 56 | if (addr - res->bus_start + size > res->size) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 57 | DEBUGF("No room in resource"); |
| 58 | goto error; |
| 59 | } |
| 60 | |
| 61 | res->bus_lower = addr + size; |
| 62 | |
| 63 | DEBUGF("address=0x%lx", addr); |
| 64 | |
| 65 | *bar = addr; |
| 66 | return 0; |
| 67 | |
| 68 | error: |
| 69 | *bar = 0xffffffff; |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * |
| 75 | */ |
| 76 | |
| 77 | void pciauto_setup_device(struct pci_controller *hose, |
| 78 | pci_dev_t dev, int bars_num, |
| 79 | struct pci_region *mem, |
| 80 | struct pci_region *io) |
| 81 | { |
| 82 | unsigned int bar_value, bar_response, bar_size; |
| 83 | unsigned int cmdstat = 0; |
| 84 | struct pci_region *bar_res; |
| 85 | int bar, bar_nr = 0; |
| 86 | int found_mem64 = 0; |
| 87 | |
| 88 | pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat); |
| 89 | cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER; |
| 90 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 91 | for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_0 + (bars_num*4); bar += 4) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | /* Tickle the BAR and get the response */ |
| 93 | pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); |
| 94 | pci_hose_read_config_dword(hose, dev, bar, &bar_response); |
| 95 | |
| 96 | /* If BAR is not implemented go to the next BAR */ |
| 97 | if (!bar_response) |
| 98 | continue; |
| 99 | |
| 100 | found_mem64 = 0; |
| 101 | |
| 102 | /* Check the BAR type and set our address mask */ |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 103 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 104 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1; |
| 105 | bar_res = io; |
| 106 | |
| 107 | DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%x, ", bar_nr, bar_size); |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 108 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 109 | if ( (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
| 110 | PCI_BASE_ADDRESS_MEM_TYPE_64) |
| 111 | found_mem64 = 1; |
| 112 | |
| 113 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
| 114 | bar_res = mem; |
| 115 | |
| 116 | DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%x, ", bar_nr, bar_size); |
| 117 | } |
| 118 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 119 | if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | /* Write it out and update our limit */ |
| 121 | pci_hose_write_config_dword(hose, dev, bar, bar_value); |
| 122 | |
| 123 | /* |
| 124 | * If we are a 64-bit decoder then increment to the |
| 125 | * upper 32 bits of the bar and force it to locate |
| 126 | * in the lower 4GB of memory. |
| 127 | */ |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 128 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | bar += 4; |
| 130 | pci_hose_write_config_dword(hose, dev, bar, 0x00000000); |
| 131 | } |
| 132 | |
| 133 | cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ? |
| 134 | PCI_COMMAND_IO : PCI_COMMAND_MEMORY; |
| 135 | } |
| 136 | |
| 137 | DEBUGF("\n"); |
| 138 | |
| 139 | bar_nr++; |
| 140 | } |
| 141 | |
| 142 | pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat); |
| 143 | pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08); |
| 144 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
| 145 | } |
| 146 | |
| 147 | static void pciauto_prescan_setup_bridge(struct pci_controller *hose, |
| 148 | pci_dev_t dev, int sub_bus) |
| 149 | { |
| 150 | struct pci_region *pci_mem = hose->pci_mem; |
| 151 | struct pci_region *pci_io = hose->pci_io; |
| 152 | unsigned int cmdstat; |
| 153 | |
| 154 | pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat); |
| 155 | |
| 156 | /* Configure bus number registers */ |
| 157 | pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, PCI_BUS(dev)); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 158 | pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, sub_bus); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 159 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff); |
| 160 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 161 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 162 | /* Round memory allocator to 1MB boundary */ |
| 163 | pciauto_region_align(pci_mem, 0x100000); |
| 164 | |
| 165 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 166 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE, |
| 167 | (pci_mem->bus_lower & 0xfff00000) >> 16); |
| 168 | |
| 169 | cmdstat |= PCI_COMMAND_MEMORY; |
| 170 | } |
| 171 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 172 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 173 | /* Round I/O allocator to 4KB boundary */ |
| 174 | pciauto_region_align(pci_io, 0x1000); |
| 175 | |
| 176 | pci_hose_write_config_byte(hose, dev, PCI_IO_BASE, |
| 177 | (pci_io->bus_lower & 0x0000f000) >> 8); |
| 178 | pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16, |
| 179 | (pci_io->bus_lower & 0xffff0000) >> 16); |
| 180 | |
| 181 | cmdstat |= PCI_COMMAND_IO; |
| 182 | } |
| 183 | |
| 184 | /* We don't support prefetchable memory for now, so disable */ |
| 185 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000); |
| 186 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x1000); |
| 187 | |
| 188 | /* Enable memory and I/O accesses, enable bus master */ |
| 189 | pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER); |
| 190 | } |
| 191 | |
| 192 | static void pciauto_postscan_setup_bridge(struct pci_controller *hose, |
| 193 | pci_dev_t dev, int sub_bus) |
| 194 | { |
| 195 | struct pci_region *pci_mem = hose->pci_mem; |
| 196 | struct pci_region *pci_io = hose->pci_io; |
| 197 | |
| 198 | /* Configure bus number registers */ |
| 199 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, sub_bus); |
| 200 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 201 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 202 | /* Round memory allocator to 1MB boundary */ |
| 203 | pciauto_region_align(pci_mem, 0x100000); |
| 204 | |
| 205 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT, |
| 206 | (pci_mem->bus_lower-1) >> 16); |
| 207 | } |
| 208 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 209 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 210 | /* Round I/O allocator to 4KB boundary */ |
| 211 | pciauto_region_align(pci_io, 0x1000); |
| 212 | |
| 213 | pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT, |
| 214 | ((pci_io->bus_lower-1) & 0x0000f000) >> 8); |
| 215 | pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16, |
| 216 | ((pci_io->bus_lower-1) & 0xffff0000) >> 16); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * |
| 222 | */ |
| 223 | |
| 224 | void pciauto_config_init(struct pci_controller *hose) |
| 225 | { |
| 226 | int i; |
| 227 | |
| 228 | hose->pci_io = hose->pci_mem = NULL; |
| 229 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 230 | for (i=0; i<hose->region_count; i++) { |
| 231 | switch(hose->regions[i].flags) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 232 | case PCI_REGION_IO: |
| 233 | if (!hose->pci_io || |
| 234 | hose->pci_io->size < hose->regions[i].size) |
| 235 | hose->pci_io = hose->regions + i; |
| 236 | break; |
| 237 | case PCI_REGION_MEM: |
| 238 | if (!hose->pci_mem || |
| 239 | hose->pci_mem->size < hose->regions[i].size) |
| 240 | hose->pci_mem = hose->regions + i; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 246 | if (hose->pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 247 | pciauto_region_init(hose->pci_mem); |
| 248 | |
| 249 | DEBUGF("PCI Autoconfig: Memory region: [%lx-%lx]\n", |
| 250 | hose->pci_mem->bus_start, |
| 251 | hose->pci_mem->bus_start + hose->pci_mem->size - 1); |
| 252 | } |
| 253 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 254 | if (hose->pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 255 | pciauto_region_init(hose->pci_io); |
| 256 | |
| 257 | DEBUGF("PCI Autoconfig: I/O region: [%lx-%lx]\n", |
| 258 | hose->pci_io->bus_start, |
| 259 | hose->pci_io->bus_start + hose->pci_io->size - 1); |
| 260 | } |
| 261 | } |
| 262 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 263 | /* HJF: Changed this to return int. I think this is required |
| 264 | * to get the correct result when scanning bridges |
| 265 | */ |
| 266 | int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 267 | { |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 268 | unsigned int sub_bus = PCI_BUS(dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 269 | unsigned short class; |
| 270 | unsigned char prg_iface; |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 271 | int n; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | |
| 273 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 274 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 275 | switch(class) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | case PCI_CLASS_BRIDGE_PCI: |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 277 | hose->current_busno++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 278 | pciauto_setup_device(hose, dev, 2, hose->pci_mem, hose->pci_io); |
| 279 | |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 280 | DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev)); |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 281 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 282 | /* Passing in current_busno allows for sibling P2P bridges */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 283 | pciauto_prescan_setup_bridge(hose, dev, hose->current_busno); |
wdenk | cd37d9e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 284 | /* |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 285 | * need to figure out if this is a subordinate bridge on the bus |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 286 | * to be able to properly set the pri/sec/sub bridge registers. |
| 287 | */ |
| 288 | n = pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 289 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 290 | /* figure out the deepest we've gone for this leg */ |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 291 | sub_bus = max(n, sub_bus); |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 292 | pciauto_postscan_setup_bridge(hose, dev, sub_bus); |
wdenk | 5653fc3 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 293 | |
wdenk | db2f721f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 294 | sub_bus = hose->current_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 295 | break; |
| 296 | |
| 297 | case PCI_CLASS_STORAGE_IDE: |
| 298 | pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface); |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 299 | if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) { |
| 300 | DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n"); |
| 301 | return sub_bus; |
| 302 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 303 | |
| 304 | pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_io); |
| 305 | break; |
| 306 | |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 307 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 308 | /* just do a minimal setup of the bridge, let the OS take care of the rest */ |
| 309 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, hose->pci_io); |
| 310 | |
wdenk | 3c74e32 | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 311 | DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n", PCI_DEV(dev)); |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 312 | |
| 313 | hose->current_busno++; |
| 314 | break; |
| 315 | |
wdenk | e0ac62d | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 316 | #ifdef CONFIG_MPC5200 |
| 317 | case PCI_CLASS_BRIDGE_OTHER: |
| 318 | DEBUGF("PCI Autoconfig: Skipping bridge device %d\n", |
| 319 | PCI_DEV(dev)); |
| 320 | break; |
| 321 | #endif |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 322 | #ifdef CONFIG_MPC834X |
| 323 | case PCI_CLASS_BRIDGE_OTHER: |
| 324 | /* |
| 325 | * The host/PCI bridge 1 seems broken in 8349 - it presents |
| 326 | * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_ |
| 327 | * device claiming resources io/mem/irq.. we only allow for |
| 328 | * the PIMMR window to be allocated (BAR0 - 1MB size) |
| 329 | */ |
| 330 | DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n"); |
| 331 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, hose->pci_io); |
| 332 | break; |
| 333 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 334 | default: |
| 335 | pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_io); |
| 336 | break; |
| 337 | } |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 338 | |
| 339 | return sub_bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | #endif /* CONFIG_PCI */ |