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 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 40 | #define PCI_HOSE_OP(rw, size, type) \ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 41 | int pci_hose_##rw##_config_##size(struct pci_controller *hose, \ |
| 42 | pci_dev_t dev, \ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 43 | int offset, type value) \ |
| 44 | { \ |
| 45 | return hose->rw##_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | PCI_HOSE_OP(read, byte, u8 *) |
| 49 | PCI_HOSE_OP(read, word, u16 *) |
| 50 | PCI_HOSE_OP(read, dword, u32 *) |
| 51 | PCI_HOSE_OP(write, byte, u8) |
| 52 | PCI_HOSE_OP(write, word, u16) |
| 53 | PCI_HOSE_OP(write, dword, u32) |
| 54 | |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 55 | #ifndef CONFIG_IXP425 |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 56 | #define PCI_OP(rw, size, type, error_code) \ |
| 57 | int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \ |
| 58 | { \ |
| 59 | struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \ |
| 60 | \ |
| 61 | if (!hose) \ |
| 62 | { \ |
| 63 | error_code; \ |
| 64 | return -1; \ |
| 65 | } \ |
| 66 | \ |
| 67 | return pci_hose_##rw##_config_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | PCI_OP(read, byte, u8 *, *value = 0xff) |
| 71 | PCI_OP(read, word, u16 *, *value = 0xffff) |
| 72 | PCI_OP(read, dword, u32 *, *value = 0xffffffff) |
| 73 | PCI_OP(write, byte, u8, ) |
| 74 | PCI_OP(write, word, u16, ) |
| 75 | PCI_OP(write, dword, u32, ) |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 76 | #endif /* CONFIG_IXP425 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 77 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 78 | #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \ |
| 79 | int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 80 | pci_dev_t dev, \ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 81 | int offset, type val) \ |
| 82 | { \ |
| 83 | u32 val32; \ |
| 84 | \ |
Shinya Kuribayashi | 815b5bd | 2007-08-17 12:43:44 +0900 | [diff] [blame] | 85 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \ |
| 86 | *val = -1; \ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 87 | return -1; \ |
Shinya Kuribayashi | 815b5bd | 2007-08-17 12:43:44 +0900 | [diff] [blame] | 88 | } \ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 89 | \ |
| 90 | *val = (val32 >> ((offset & (int)off_mask) * 8)); \ |
| 91 | \ |
| 92 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 93 | } |
| 94 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 95 | #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \ |
| 96 | int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 97 | pci_dev_t dev, \ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 98 | int offset, type val) \ |
| 99 | { \ |
wdenk | 498b8db | 2004-04-18 22:26:17 +0000 | [diff] [blame] | 100 | u32 val32, mask, ldata, shift; \ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 101 | \ |
| 102 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\ |
| 103 | return -1; \ |
| 104 | \ |
wdenk | 498b8db | 2004-04-18 22:26:17 +0000 | [diff] [blame] | 105 | shift = ((offset & (int)off_mask) * 8); \ |
| 106 | ldata = (((unsigned long)val) & val_mask) << shift; \ |
| 107 | mask = val_mask << shift; \ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 108 | val32 = (val32 & ~mask) | ldata; \ |
| 109 | \ |
| 110 | if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\ |
| 111 | return -1; \ |
| 112 | \ |
| 113 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03) |
| 117 | PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02) |
| 118 | PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff) |
| 119 | PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff) |
| 120 | |
| 121 | /* |
| 122 | * |
| 123 | */ |
| 124 | |
| 125 | static struct pci_controller* hose_head = NULL; |
| 126 | |
| 127 | void pci_register_hose(struct pci_controller* hose) |
| 128 | { |
| 129 | struct pci_controller **phose = &hose_head; |
| 130 | |
| 131 | while(*phose) |
| 132 | phose = &(*phose)->next; |
| 133 | |
| 134 | hose->next = NULL; |
| 135 | |
| 136 | *phose = hose; |
| 137 | } |
| 138 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 139 | struct pci_controller *pci_bus_to_hose (int bus) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 140 | { |
| 141 | struct pci_controller *hose; |
| 142 | |
| 143 | for (hose = hose_head; hose; hose = hose->next) |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 144 | if (bus >= hose->first_busno && bus <= hose->last_busno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 145 | return hose; |
| 146 | |
Rafal Jaworowski | 6902df5 | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 147 | printf("pci_bus_to_hose() failed\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 148 | return NULL; |
| 149 | } |
| 150 | |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 151 | #ifndef CONFIG_IXP425 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 152 | pci_dev_t pci_find_devices(struct pci_device_id *ids, int index) |
| 153 | { |
| 154 | struct pci_controller * hose; |
| 155 | u16 vendor, device; |
| 156 | u8 header_type; |
| 157 | pci_dev_t bdf; |
| 158 | int i, bus, found_multi = 0; |
| 159 | |
| 160 | for (hose = hose_head; hose; hose = hose->next) |
| 161 | { |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 162 | #ifdef CFG_SCSI_SCAN_BUS_REVERSE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 163 | for (bus = hose->last_busno; bus >= hose->first_busno; bus--) |
| 164 | #else |
| 165 | for (bus = hose->first_busno; bus <= hose->last_busno; bus++) |
| 166 | #endif |
| 167 | for (bdf = PCI_BDF(bus,0,0); |
Heiko Schocher | f5e0d03 | 2006-06-19 11:02:41 +0200 | [diff] [blame] | 168 | #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 169 | bdf < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1); |
| 170 | #else |
| 171 | bdf < PCI_BDF(bus+1,0,0); |
| 172 | #endif |
| 173 | bdf += PCI_BDF(0,0,1)) |
| 174 | { |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 175 | if (!PCI_FUNC(bdf)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 176 | pci_read_config_byte(bdf, |
| 177 | PCI_HEADER_TYPE, |
| 178 | &header_type); |
| 179 | |
| 180 | found_multi = header_type & 0x80; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 181 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 182 | if (!found_multi) |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | pci_read_config_word(bdf, |
| 187 | PCI_VENDOR_ID, |
| 188 | &vendor); |
| 189 | pci_read_config_word(bdf, |
| 190 | PCI_DEVICE_ID, |
| 191 | &device); |
| 192 | |
| 193 | for (i=0; ids[i].vendor != 0; i++) |
| 194 | if (vendor == ids[i].vendor && |
| 195 | device == ids[i].device) |
| 196 | { |
| 197 | if (index <= 0) |
| 198 | return bdf; |
| 199 | |
| 200 | index--; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return (-1); |
| 206 | } |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 207 | #endif /* CONFIG_IXP425 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 208 | |
| 209 | pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index) |
| 210 | { |
| 211 | static struct pci_device_id ids[2] = {{}, {0, 0}}; |
| 212 | |
| 213 | ids[0].vendor = vendor; |
| 214 | ids[0].device = device; |
| 215 | |
| 216 | return pci_find_devices(ids, index); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * |
| 221 | */ |
| 222 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 223 | unsigned long pci_hose_phys_to_bus (struct pci_controller *hose, |
Becky Bruce | 36f3267 | 2008-05-07 13:24:57 -0500 | [diff] [blame] | 224 | phys_addr_t phys_addr, |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 225 | unsigned long flags) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 226 | { |
| 227 | struct pci_region *res; |
| 228 | unsigned long bus_addr; |
| 229 | int i; |
| 230 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 231 | if (!hose) { |
| 232 | printf ("pci_hose_phys_to_bus: %s\n", "invalid hose"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | goto Done; |
| 234 | } |
| 235 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 236 | for (i = 0; i < hose->region_count; i++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 237 | res = &hose->regions[i]; |
| 238 | |
| 239 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 240 | continue; |
| 241 | |
| 242 | bus_addr = phys_addr - res->phys_start + res->bus_start; |
| 243 | |
| 244 | if (bus_addr >= res->bus_start && |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 245 | bus_addr < res->bus_start + res->size) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 246 | return bus_addr; |
| 247 | } |
| 248 | } |
| 249 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 250 | printf ("pci_hose_phys_to_bus: %s\n", "invalid physical address"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 251 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 252 | Done: |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
Becky Bruce | 36f3267 | 2008-05-07 13:24:57 -0500 | [diff] [blame] | 256 | phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose, |
| 257 | unsigned long bus_addr, |
| 258 | unsigned long flags) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 259 | { |
| 260 | struct pci_region *res; |
| 261 | int i; |
| 262 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 263 | if (!hose) { |
| 264 | printf ("pci_hose_bus_to_phys: %s\n", "invalid hose"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 265 | goto Done; |
| 266 | } |
| 267 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 268 | for (i = 0; i < hose->region_count; i++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 269 | res = &hose->regions[i]; |
| 270 | |
| 271 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 272 | continue; |
| 273 | |
| 274 | if (bus_addr >= res->bus_start && |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 275 | bus_addr < res->bus_start + res->size) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | return bus_addr - res->bus_start + res->phys_start; |
| 277 | } |
| 278 | } |
| 279 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 280 | printf ("pci_hose_bus_to_phys: %s\n", "invalid physical address"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 281 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 282 | Done: |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * |
| 288 | */ |
| 289 | |
| 290 | int pci_hose_config_device(struct pci_controller *hose, |
| 291 | pci_dev_t dev, |
| 292 | unsigned long io, |
| 293 | unsigned long mem, |
| 294 | unsigned long command) |
| 295 | { |
| 296 | unsigned int bar_response, bar_size, bar_value, old_command; |
| 297 | unsigned char pin; |
| 298 | int bar, found_mem64; |
| 299 | |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 300 | debug ("PCI Config: I/O=0x%lx, Memory=0x%lx, Command=0x%lx\n", |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 301 | io, mem, command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 302 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 303 | pci_hose_write_config_dword (hose, dev, PCI_COMMAND, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 304 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 305 | for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_5; bar += 4) { |
| 306 | pci_hose_write_config_dword (hose, dev, bar, 0xffffffff); |
| 307 | pci_hose_read_config_dword (hose, dev, bar, &bar_response); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 308 | |
| 309 | if (!bar_response) |
| 310 | continue; |
| 311 | |
| 312 | found_mem64 = 0; |
| 313 | |
| 314 | /* Check the BAR type and set our address mask */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 315 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 316 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 317 | /* round up region base address to a multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 318 | io = ((io - 1) | (bar_size - 1)) + 1; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 319 | bar_value = io; |
| 320 | /* compute new region base address */ |
| 321 | io = io + bar_size; |
| 322 | } else { |
| 323 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
| 324 | PCI_BASE_ADDRESS_MEM_TYPE_64) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 325 | found_mem64 = 1; |
| 326 | |
| 327 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 328 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 329 | /* round up region base address to multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 330 | mem = ((mem - 1) | (bar_size - 1)) + 1; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 331 | bar_value = mem; |
| 332 | /* compute new region base address */ |
| 333 | mem = mem + bar_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Write it out and update our limit */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 337 | pci_hose_write_config_dword (hose, dev, bar, bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 338 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 339 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | bar += 4; |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 341 | pci_hose_write_config_dword (hose, dev, bar, 0x00000000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | /* Configure Cache Line Size Register */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 346 | pci_hose_write_config_byte (hose, dev, PCI_CACHE_LINE_SIZE, 0x08); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 347 | |
| 348 | /* Configure Latency Timer */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 349 | pci_hose_write_config_byte (hose, dev, PCI_LATENCY_TIMER, 0x80); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 350 | |
| 351 | /* Disable interrupt line, if device says it wants to use interrupts */ |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 352 | pci_hose_read_config_byte (hose, dev, PCI_INTERRUPT_PIN, &pin); |
| 353 | if (pin != 0) { |
| 354 | pci_hose_write_config_byte (hose, dev, PCI_INTERRUPT_LINE, 0xff); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 355 | } |
| 356 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 357 | pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &old_command); |
| 358 | pci_hose_write_config_dword (hose, dev, PCI_COMMAND, |
| 359 | (old_command & 0xffff0000) | command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * |
| 366 | */ |
| 367 | |
| 368 | struct pci_config_table *pci_find_config(struct pci_controller *hose, |
| 369 | unsigned short class, |
| 370 | unsigned int vendor, |
| 371 | unsigned int device, |
| 372 | unsigned int bus, |
| 373 | unsigned int dev, |
| 374 | unsigned int func) |
| 375 | { |
| 376 | struct pci_config_table *table; |
| 377 | |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 378 | for (table = hose->config_table; table && table->vendor; table++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 379 | if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) && |
| 380 | (table->device == PCI_ANY_ID || table->device == device) && |
| 381 | (table->class == PCI_ANY_ID || table->class == class) && |
| 382 | (table->bus == PCI_ANY_ID || table->bus == bus) && |
| 383 | (table->dev == PCI_ANY_ID || table->dev == dev) && |
wdenk | f07771c | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 384 | (table->func == PCI_ANY_ID || table->func == func)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 385 | return table; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | void pci_cfgfunc_config_device(struct pci_controller *hose, |
| 393 | pci_dev_t dev, |
| 394 | struct pci_config_table *entry) |
| 395 | { |
| 396 | pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], entry->priv[2]); |
| 397 | } |
| 398 | |
| 399 | void pci_cfgfunc_do_nothing(struct pci_controller *hose, |
| 400 | pci_dev_t dev, struct pci_config_table *entry) |
| 401 | { |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * |
| 406 | */ |
| 407 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 408 | /* HJF: Changed this to return int. I think this is required |
| 409 | * to get the correct result when scanning bridges |
| 410 | */ |
| 411 | extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 412 | extern void pciauto_config_init(struct pci_controller *hose); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 413 | |
Stefan Roese | dc1da42 | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 414 | int __pci_skip_dev(struct pci_controller *hose, pci_dev_t dev) |
| 415 | { |
| 416 | /* |
| 417 | * Check if pci device should be skipped in configuration |
| 418 | */ |
| 419 | if (dev == PCI_BDF(hose->first_busno, 0, 0)) { |
| 420 | #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */ |
| 421 | /* |
| 422 | * Only skip configuration if "pciconfighost" is not set |
| 423 | */ |
| 424 | if (getenv("pciconfighost") == NULL) |
| 425 | return 1; |
| 426 | #else |
| 427 | return 1; |
| 428 | #endif |
| 429 | } |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev) |
| 434 | __attribute__((weak, alias("__pci_skip_dev"))); |
| 435 | |
| 436 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 437 | int __pci_print_dev(struct pci_controller *hose, pci_dev_t dev) |
| 438 | { |
| 439 | if (dev == PCI_BDF(hose->first_busno, 0, 0)) |
| 440 | return 0; |
| 441 | |
| 442 | return 1; |
| 443 | } |
| 444 | int pci_print_dev(struct pci_controller *hose, pci_dev_t dev) |
| 445 | __attribute__((weak, alias("__pci_print_dev"))); |
| 446 | #endif /* CONFIG_PCI_SCAN_SHOW */ |
| 447 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 448 | int pci_hose_scan_bus(struct pci_controller *hose, int bus) |
| 449 | { |
| 450 | unsigned int sub_bus, found_multi=0; |
| 451 | unsigned short vendor, device, class; |
| 452 | unsigned char header_type; |
| 453 | struct pci_config_table *cfg; |
| 454 | pci_dev_t dev; |
| 455 | |
| 456 | sub_bus = bus; |
| 457 | |
| 458 | for (dev = PCI_BDF(bus,0,0); |
| 459 | dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1); |
Stefan Roese | dc1da42 | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 460 | dev += PCI_BDF(0,0,1)) { |
| 461 | |
| 462 | if (pci_skip_dev(hose, dev)) |
| 463 | continue; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 464 | |
| 465 | if (PCI_FUNC(dev) && !found_multi) |
| 466 | continue; |
| 467 | |
| 468 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type); |
| 469 | |
| 470 | pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor); |
| 471 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 472 | if (vendor != 0xffff && vendor != 0x0000) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 473 | |
| 474 | if (!PCI_FUNC(dev)) |
| 475 | found_multi = header_type & 0x80; |
| 476 | |
wdenk | a119190 | 2005-01-09 17:12:27 +0000 | [diff] [blame] | 477 | debug ("PCI Scan: Found Bus %d, Device %d, Function %d\n", |
| 478 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev) ); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 479 | |
| 480 | pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device); |
| 481 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 482 | |
| 483 | cfg = pci_find_config(hose, class, vendor, device, |
| 484 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 485 | if (cfg) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 486 | cfg->config_device(hose, dev, cfg); |
Kumar Gala | 3411d11 | 2006-01-11 13:27:19 -0600 | [diff] [blame] | 487 | sub_bus = max(sub_bus, hose->current_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 488 | #ifdef CONFIG_PCI_PNP |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 489 | } else { |
| 490 | int n = pciauto_config_device(hose, dev); |
| 491 | |
| 492 | sub_bus = max(sub_bus, n); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 493 | #endif |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 494 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 495 | if (hose->fixup_irq) |
| 496 | hose->fixup_irq(hose, dev); |
| 497 | |
| 498 | #ifdef CONFIG_PCI_SCAN_SHOW |
Stefan Roese | dc1da42 | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 499 | if (pci_print_dev(hose, dev)) { |
| 500 | unsigned char int_line; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 501 | |
Stefan Roese | dc1da42 | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 502 | pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_LINE, |
| 503 | &int_line); |
| 504 | printf(" %02x %02x %04x %04x %04x %02x\n", |
| 505 | PCI_BUS(dev), PCI_DEV(dev), vendor, device, class, |
| 506 | int_line); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 507 | } |
| 508 | #endif |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | return sub_bus; |
| 513 | } |
| 514 | |
| 515 | int pci_hose_scan(struct pci_controller *hose) |
| 516 | { |
Ed Swarthout | 40e81ad | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 517 | /* Start scan at current_busno. |
| 518 | * PCIe will start scan at first_busno+1. |
| 519 | */ |
| 520 | /* For legacy support, ensure current>=first */ |
| 521 | if (hose->first_busno > hose->current_busno) |
| 522 | hose->current_busno = hose->first_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 523 | #ifdef CONFIG_PCI_PNP |
| 524 | pciauto_config_init(hose); |
| 525 | #endif |
Ed Swarthout | 40e81ad | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 526 | return pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 527 | } |
| 528 | |
stroese | ad10dd9 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 529 | void pci_init(void) |
| 530 | { |
| 531 | #if defined(CONFIG_PCI_BOOTDELAY) |
| 532 | char *s; |
| 533 | int i; |
| 534 | |
| 535 | /* wait "pcidelay" ms (if defined)... */ |
| 536 | s = getenv ("pcidelay"); |
| 537 | if (s) { |
| 538 | int val = simple_strtoul (s, NULL, 10); |
| 539 | for (i=0; i<val; i++) |
| 540 | udelay (1000); |
| 541 | } |
| 542 | #endif /* CONFIG_PCI_BOOTDELAY */ |
| 543 | |
| 544 | /* now call board specific pci_init()... */ |
| 545 | pci_init_board(); |
| 546 | } |
| 547 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 548 | #endif /* CONFIG_PCI */ |