wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 3 | * Andreas Heppel <aheppel@sysgo.de> |
| 4 | * |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 5 | * (C) Copyright 2002, 2003 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | * PCI routines |
| 29 | */ |
| 30 | |
| 31 | #include <common.h> |
| 32 | |
| 33 | #ifdef CONFIG_PCI |
| 34 | |
| 35 | #include <command.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | #include <asm/processor.h> |
| 37 | #include <asm/io.h> |
| 38 | #include <pci.h> |
| 39 | |
| 40 | #ifdef DEBUG |
| 41 | #define DEBUGF(x...) printf(x) |
| 42 | #else |
| 43 | #define DEBUGF(x...) |
| 44 | #endif /* DEBUG */ |
| 45 | |
| 46 | /* |
| 47 | * |
| 48 | */ |
| 49 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 50 | #define PCI_HOSE_OP(rw, size, type) \ |
| 51 | int pci_hose_##rw##_config_##size(struct pci_controller *hose, \ |
| 52 | pci_dev_t dev, \ |
| 53 | int offset, type value) \ |
| 54 | { \ |
| 55 | return hose->rw##_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | PCI_HOSE_OP(read, byte, u8 *) |
| 59 | PCI_HOSE_OP(read, word, u16 *) |
| 60 | PCI_HOSE_OP(read, dword, u32 *) |
| 61 | PCI_HOSE_OP(write, byte, u8) |
| 62 | PCI_HOSE_OP(write, word, u16) |
| 63 | PCI_HOSE_OP(write, dword, u32) |
| 64 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 65 | #define PCI_OP(rw, size, type, error_code) \ |
| 66 | int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \ |
| 67 | { \ |
| 68 | struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \ |
| 69 | \ |
| 70 | if (!hose) \ |
| 71 | { \ |
| 72 | error_code; \ |
| 73 | return -1; \ |
| 74 | } \ |
| 75 | \ |
| 76 | return pci_hose_##rw##_config_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | PCI_OP(read, byte, u8 *, *value = 0xff) |
| 80 | PCI_OP(read, word, u16 *, *value = 0xffff) |
| 81 | PCI_OP(read, dword, u32 *, *value = 0xffffffff) |
| 82 | PCI_OP(write, byte, u8, ) |
| 83 | PCI_OP(write, word, u16, ) |
| 84 | PCI_OP(write, dword, u32, ) |
| 85 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 86 | #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \ |
| 87 | int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\ |
| 88 | pci_dev_t dev, \ |
| 89 | int offset, type val) \ |
| 90 | { \ |
| 91 | u32 val32; \ |
| 92 | \ |
| 93 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\ |
| 94 | return -1; \ |
| 95 | \ |
| 96 | *val = (val32 >> ((offset & (int)off_mask) * 8)); \ |
| 97 | \ |
| 98 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 101 | #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \ |
| 102 | int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\ |
| 103 | pci_dev_t dev, \ |
| 104 | int offset, type val) \ |
| 105 | { \ |
| 106 | u32 val32, mask, ldata; \ |
| 107 | \ |
| 108 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\ |
| 109 | return -1; \ |
| 110 | \ |
| 111 | mask = val_mask; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 112 | ldata = (((unsigned long)val) & mask) << ((offset & (int)off_mask) * 8);\ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 113 | mask <<= ((mask & (int)off_mask) * 8); \ |
| 114 | val32 = (val32 & ~mask) | ldata; \ |
| 115 | \ |
| 116 | if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\ |
| 117 | return -1; \ |
| 118 | \ |
| 119 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03) |
| 123 | PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02) |
| 124 | PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff) |
| 125 | PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff) |
| 126 | |
| 127 | /* |
| 128 | * |
| 129 | */ |
| 130 | |
| 131 | static struct pci_controller* hose_head = NULL; |
| 132 | |
| 133 | void pci_register_hose(struct pci_controller* hose) |
| 134 | { |
| 135 | struct pci_controller **phose = &hose_head; |
| 136 | |
| 137 | while(*phose) |
| 138 | phose = &(*phose)->next; |
| 139 | |
| 140 | hose->next = NULL; |
| 141 | |
| 142 | *phose = hose; |
| 143 | } |
| 144 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 145 | struct pci_controller *pci_bus_to_hose (int bus) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 146 | { |
| 147 | struct pci_controller *hose; |
| 148 | |
| 149 | for (hose = hose_head; hose; hose = hose->next) |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 150 | if (bus >= hose->first_busno && bus <= hose->last_busno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 151 | return hose; |
| 152 | |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | pci_dev_t pci_find_devices(struct pci_device_id *ids, int index) |
| 157 | { |
| 158 | struct pci_controller * hose; |
| 159 | u16 vendor, device; |
| 160 | u8 header_type; |
| 161 | pci_dev_t bdf; |
| 162 | int i, bus, found_multi = 0; |
| 163 | |
| 164 | for (hose = hose_head; hose; hose = hose->next) |
| 165 | { |
| 166 | #if CFG_SCSI_SCAN_BUS_REVERSE |
| 167 | for (bus = hose->last_busno; bus >= hose->first_busno; bus--) |
| 168 | #else |
| 169 | for (bus = hose->first_busno; bus <= hose->last_busno; bus++) |
| 170 | #endif |
| 171 | for (bdf = PCI_BDF(bus,0,0); |
| 172 | #ifdef CONFIG_ELPPC |
| 173 | bdf < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1); |
| 174 | #else |
| 175 | bdf < PCI_BDF(bus+1,0,0); |
| 176 | #endif |
| 177 | bdf += PCI_BDF(0,0,1)) |
| 178 | { |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 179 | if (!PCI_FUNC(bdf)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 180 | pci_read_config_byte(bdf, |
| 181 | PCI_HEADER_TYPE, |
| 182 | &header_type); |
| 183 | |
| 184 | found_multi = header_type & 0x80; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 185 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 186 | if (!found_multi) |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | pci_read_config_word(bdf, |
| 191 | PCI_VENDOR_ID, |
| 192 | &vendor); |
| 193 | pci_read_config_word(bdf, |
| 194 | PCI_DEVICE_ID, |
| 195 | &device); |
| 196 | |
| 197 | for (i=0; ids[i].vendor != 0; i++) |
| 198 | if (vendor == ids[i].vendor && |
| 199 | device == ids[i].device) |
| 200 | { |
| 201 | if (index <= 0) |
| 202 | return bdf; |
| 203 | |
| 204 | index--; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return (-1); |
| 210 | } |
| 211 | |
| 212 | pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index) |
| 213 | { |
| 214 | static struct pci_device_id ids[2] = {{}, {0, 0}}; |
| 215 | |
| 216 | ids[0].vendor = vendor; |
| 217 | ids[0].device = device; |
| 218 | |
| 219 | return pci_find_devices(ids, index); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * |
| 224 | */ |
| 225 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 226 | unsigned long pci_hose_phys_to_bus (struct pci_controller *hose, |
| 227 | unsigned long phys_addr, |
| 228 | unsigned long flags) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 229 | { |
| 230 | struct pci_region *res; |
| 231 | unsigned long bus_addr; |
| 232 | int i; |
| 233 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 234 | if (!hose) { |
| 235 | printf ("pci_hose_phys_to_bus: %s\n", "invalid hose"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | goto Done; |
| 237 | } |
| 238 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 239 | for (i = 0; i < hose->region_count; i++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 240 | res = &hose->regions[i]; |
| 241 | |
| 242 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 243 | continue; |
| 244 | |
| 245 | bus_addr = phys_addr - res->phys_start + res->bus_start; |
| 246 | |
| 247 | if (bus_addr >= res->bus_start && |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 248 | bus_addr < res->bus_start + res->size) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | return bus_addr; |
| 250 | } |
| 251 | } |
| 252 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 253 | printf ("pci_hose_phys_to_bus: %s\n", "invalid physical address"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 254 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 255 | Done: |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | unsigned long pci_hose_bus_to_phys(struct pci_controller* hose, |
| 260 | unsigned long bus_addr, |
| 261 | unsigned long flags) |
| 262 | { |
| 263 | struct pci_region *res; |
| 264 | int i; |
| 265 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 266 | if (!hose) { |
| 267 | printf ("pci_hose_bus_to_phys: %s\n", "invalid hose"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | goto Done; |
| 269 | } |
| 270 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 271 | for (i = 0; i < hose->region_count; i++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | res = &hose->regions[i]; |
| 273 | |
| 274 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 275 | continue; |
| 276 | |
| 277 | if (bus_addr >= res->bus_start && |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 278 | bus_addr < res->bus_start + res->size) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 279 | return bus_addr - res->bus_start + res->phys_start; |
| 280 | } |
| 281 | } |
| 282 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 283 | printf ("pci_hose_bus_to_phys: %s\n", "invalid physical address"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 284 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 285 | Done: |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * |
| 291 | */ |
| 292 | |
| 293 | int pci_hose_config_device(struct pci_controller *hose, |
| 294 | pci_dev_t dev, |
| 295 | unsigned long io, |
| 296 | unsigned long mem, |
| 297 | unsigned long command) |
| 298 | { |
| 299 | unsigned int bar_response, bar_size, bar_value, old_command; |
| 300 | unsigned char pin; |
| 301 | int bar, found_mem64; |
| 302 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 303 | DEBUGF ("PCI Config: I/O=0x%lx, Memory=0x%lx, Command=0x%lx\n", |
| 304 | io, mem, command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 305 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 306 | pci_hose_write_config_dword (hose, dev, PCI_COMMAND, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 307 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 308 | for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_5; bar += 4) { |
| 309 | pci_hose_write_config_dword (hose, dev, bar, 0xffffffff); |
| 310 | pci_hose_read_config_dword (hose, dev, bar, &bar_response); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 311 | |
| 312 | if (!bar_response) |
| 313 | continue; |
| 314 | |
| 315 | found_mem64 = 0; |
| 316 | |
| 317 | /* Check the BAR type and set our address mask */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 318 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 319 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 320 | /* round up region base address to a multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 321 | io = ((io - 1) | (bar_size - 1)) + 1; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 322 | bar_value = io; |
| 323 | /* compute new region base address */ |
| 324 | io = io + bar_size; |
| 325 | } else { |
| 326 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
| 327 | PCI_BASE_ADDRESS_MEM_TYPE_64) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 328 | found_mem64 = 1; |
| 329 | |
| 330 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 331 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 332 | /* round up region base address to multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 333 | mem = ((mem - 1) | (bar_size - 1)) + 1; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 334 | bar_value = mem; |
| 335 | /* compute new region base address */ |
| 336 | mem = mem + bar_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Write it out and update our limit */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 340 | pci_hose_write_config_dword (hose, dev, bar, bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 341 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 342 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 343 | bar += 4; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 344 | pci_hose_write_config_dword (hose, dev, bar, 0x00000000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | /* Configure Cache Line Size Register */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 349 | pci_hose_write_config_byte (hose, dev, PCI_CACHE_LINE_SIZE, 0x08); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 350 | |
| 351 | /* Configure Latency Timer */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 352 | pci_hose_write_config_byte (hose, dev, PCI_LATENCY_TIMER, 0x80); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 353 | |
| 354 | /* Disable interrupt line, if device says it wants to use interrupts */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 355 | pci_hose_read_config_byte (hose, dev, PCI_INTERRUPT_PIN, &pin); |
| 356 | if (pin != 0) { |
| 357 | pci_hose_write_config_byte (hose, dev, PCI_INTERRUPT_LINE, 0xff); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 358 | } |
| 359 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 360 | pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &old_command); |
| 361 | pci_hose_write_config_dword (hose, dev, PCI_COMMAND, |
| 362 | (old_command & 0xffff0000) | command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * |
| 369 | */ |
| 370 | |
| 371 | struct pci_config_table *pci_find_config(struct pci_controller *hose, |
| 372 | unsigned short class, |
| 373 | unsigned int vendor, |
| 374 | unsigned int device, |
| 375 | unsigned int bus, |
| 376 | unsigned int dev, |
| 377 | unsigned int func) |
| 378 | { |
| 379 | struct pci_config_table *table; |
| 380 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 381 | for (table = hose->config_table; table && table->vendor; table++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 382 | if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) && |
| 383 | (table->device == PCI_ANY_ID || table->device == device) && |
| 384 | (table->class == PCI_ANY_ID || table->class == class) && |
| 385 | (table->bus == PCI_ANY_ID || table->bus == bus) && |
| 386 | (table->dev == PCI_ANY_ID || table->dev == dev) && |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 387 | (table->func == PCI_ANY_ID || table->func == func)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 388 | return table; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | void pci_cfgfunc_config_device(struct pci_controller *hose, |
| 396 | pci_dev_t dev, |
| 397 | struct pci_config_table *entry) |
| 398 | { |
| 399 | pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], entry->priv[2]); |
| 400 | } |
| 401 | |
| 402 | void pci_cfgfunc_do_nothing(struct pci_controller *hose, |
| 403 | pci_dev_t dev, struct pci_config_table *entry) |
| 404 | { |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * |
| 409 | */ |
| 410 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 411 | /* HJF: Changed this to return int. I think this is required |
| 412 | * to get the correct result when scanning bridges |
| 413 | */ |
| 414 | extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 415 | extern void pciauto_config_init(struct pci_controller *hose); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 416 | |
| 417 | int pci_hose_scan_bus(struct pci_controller *hose, int bus) |
| 418 | { |
| 419 | unsigned int sub_bus, found_multi=0; |
| 420 | unsigned short vendor, device, class; |
| 421 | unsigned char header_type; |
| 422 | struct pci_config_table *cfg; |
| 423 | pci_dev_t dev; |
| 424 | |
| 425 | sub_bus = bus; |
| 426 | |
| 427 | for (dev = PCI_BDF(bus,0,0); |
| 428 | dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1); |
| 429 | dev += PCI_BDF(0,0,1)) |
| 430 | { |
| 431 | #ifndef CONFIG_405GP /* don't skip host bridge on ppc405gp */ |
| 432 | /* Skip our host bridge */ |
| 433 | if ( dev == PCI_BDF(hose->first_busno,0,0) ) |
| 434 | continue; |
| 435 | #endif |
| 436 | |
| 437 | if (PCI_FUNC(dev) && !found_multi) |
| 438 | continue; |
| 439 | |
| 440 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type); |
| 441 | |
| 442 | pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor); |
| 443 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 444 | if (vendor != 0xffff && vendor != 0x0000) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 445 | |
| 446 | if (!PCI_FUNC(dev)) |
| 447 | found_multi = header_type & 0x80; |
| 448 | |
| 449 | DEBUGF("PCI Scan: Found Bus %d, Device %d, Function %d\n", |
| 450 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev) ); |
| 451 | |
| 452 | pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device); |
| 453 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 454 | |
| 455 | cfg = pci_find_config(hose, class, vendor, device, |
| 456 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 457 | if (cfg) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 458 | cfg->config_device(hose, dev, cfg); |
| 459 | #ifdef CONFIG_PCI_PNP |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 460 | } else { |
| 461 | int n = pciauto_config_device(hose, dev); |
| 462 | |
| 463 | sub_bus = max(sub_bus, n); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 464 | #endif |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 465 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 466 | if (hose->fixup_irq) |
| 467 | hose->fixup_irq(hose, dev); |
| 468 | |
| 469 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 470 | /* Skip our host bridge */ |
| 471 | if ( dev != PCI_BDF(hose->first_busno,0,0) ) { |
| 472 | unsigned char int_line; |
| 473 | |
| 474 | pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_LINE, |
| 475 | &int_line); |
| 476 | printf(" %02x %02x %04x %04x %04x %02x\n", |
| 477 | PCI_BUS(dev), PCI_DEV(dev), vendor, device, class, |
| 478 | int_line); |
| 479 | } |
| 480 | #endif |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | return sub_bus; |
| 485 | } |
| 486 | |
| 487 | int pci_hose_scan(struct pci_controller *hose) |
| 488 | { |
| 489 | #ifdef CONFIG_PCI_PNP |
| 490 | pciauto_config_init(hose); |
| 491 | #endif |
| 492 | return pci_hose_scan_bus(hose, hose->first_busno); |
| 493 | } |
| 494 | |
stroese | ad10dd9 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 495 | void pci_init(void) |
| 496 | { |
| 497 | #if defined(CONFIG_PCI_BOOTDELAY) |
| 498 | char *s; |
| 499 | int i; |
| 500 | |
| 501 | /* wait "pcidelay" ms (if defined)... */ |
| 502 | s = getenv ("pcidelay"); |
| 503 | if (s) { |
| 504 | int val = simple_strtoul (s, NULL, 10); |
| 505 | for (i=0; i<val; i++) |
| 506 | udelay (1000); |
| 507 | } |
| 508 | #endif /* CONFIG_PCI_BOOTDELAY */ |
| 509 | |
| 510 | /* now call board specific pci_init()... */ |
| 511 | pci_init_board(); |
| 512 | } |
| 513 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 514 | #endif /* CONFIG_PCI */ |