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 | f4688a2 | 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 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * PCI routines |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 17 | #include <command.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 18 | #include <asm/processor.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <pci.h> |
| 21 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 24 | #define PCI_HOSE_OP(rw, size, type) \ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 25 | int pci_hose_##rw##_config_##size(struct pci_controller *hose, \ |
| 26 | pci_dev_t dev, \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 27 | int offset, type value) \ |
| 28 | { \ |
| 29 | return hose->rw##_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | PCI_HOSE_OP(read, byte, u8 *) |
| 33 | PCI_HOSE_OP(read, word, u16 *) |
| 34 | PCI_HOSE_OP(read, dword, u32 *) |
| 35 | PCI_HOSE_OP(write, byte, u8) |
| 36 | PCI_HOSE_OP(write, word, u16) |
| 37 | PCI_HOSE_OP(write, dword, u32) |
| 38 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 39 | #define PCI_OP(rw, size, type, error_code) \ |
| 40 | int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \ |
| 41 | { \ |
| 42 | struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \ |
| 43 | \ |
| 44 | if (!hose) \ |
| 45 | { \ |
| 46 | error_code; \ |
| 47 | return -1; \ |
| 48 | } \ |
| 49 | \ |
| 50 | return pci_hose_##rw##_config_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | PCI_OP(read, byte, u8 *, *value = 0xff) |
| 54 | PCI_OP(read, word, u16 *, *value = 0xffff) |
| 55 | PCI_OP(read, dword, u32 *, *value = 0xffffffff) |
| 56 | PCI_OP(write, byte, u8, ) |
| 57 | PCI_OP(write, word, u16, ) |
| 58 | PCI_OP(write, dword, u32, ) |
| 59 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 60 | #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \ |
| 61 | int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 62 | pci_dev_t dev, \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 63 | int offset, type val) \ |
| 64 | { \ |
| 65 | u32 val32; \ |
| 66 | \ |
Shinya Kuribayashi | f19da9d | 2007-08-17 12:43:44 +0900 | [diff] [blame] | 67 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \ |
| 68 | *val = -1; \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 69 | return -1; \ |
Shinya Kuribayashi | f19da9d | 2007-08-17 12:43:44 +0900 | [diff] [blame] | 70 | } \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 71 | \ |
| 72 | *val = (val32 >> ((offset & (int)off_mask) * 8)); \ |
| 73 | \ |
| 74 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 77 | #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \ |
| 78 | int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 79 | pci_dev_t dev, \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 80 | int offset, type val) \ |
| 81 | { \ |
wdenk | 19c8fb7 | 2004-04-18 22:26:17 +0000 | [diff] [blame] | 82 | u32 val32, mask, ldata, shift; \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 83 | \ |
| 84 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\ |
| 85 | return -1; \ |
| 86 | \ |
wdenk | 19c8fb7 | 2004-04-18 22:26:17 +0000 | [diff] [blame] | 87 | shift = ((offset & (int)off_mask) * 8); \ |
| 88 | ldata = (((unsigned long)val) & val_mask) << shift; \ |
| 89 | mask = val_mask << shift; \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 90 | val32 = (val32 & ~mask) | ldata; \ |
| 91 | \ |
| 92 | if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\ |
| 93 | return -1; \ |
| 94 | \ |
| 95 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03) |
| 99 | PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02) |
| 100 | PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff) |
| 101 | PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff) |
| 102 | |
Becky Bruce | 0709bfc | 2009-02-03 18:10:50 -0600 | [diff] [blame] | 103 | /* Get a virtual address associated with a BAR region */ |
| 104 | void *pci_map_bar(pci_dev_t pdev, int bar, int flags) |
| 105 | { |
| 106 | pci_addr_t pci_bus_addr; |
Kumar Gala | 1873d5c | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 107 | u32 bar_response; |
Becky Bruce | 0709bfc | 2009-02-03 18:10:50 -0600 | [diff] [blame] | 108 | |
| 109 | /* read BAR address */ |
| 110 | pci_read_config_dword(pdev, bar, &bar_response); |
Kumar Gala | 1873d5c | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 111 | pci_bus_addr = (pci_addr_t)(bar_response & ~0xf); |
Becky Bruce | 0709bfc | 2009-02-03 18:10:50 -0600 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * Pass "0" as the length argument to pci_bus_to_virt. The arg |
| 115 | * isn't actualy used on any platform because u-boot assumes a static |
| 116 | * linear mapping. In the future, this could read the BAR size |
| 117 | * and pass that as the size if needed. |
| 118 | */ |
| 119 | return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE); |
| 120 | } |
| 121 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 122 | /* |
| 123 | * |
| 124 | */ |
| 125 | |
John Schmoller | 60e877f | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 126 | static struct pci_controller* hose_head; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 128 | struct pci_controller *pci_get_hose_head(void) |
| 129 | { |
| 130 | if (gd->hose) |
| 131 | return gd->hose; |
| 132 | |
| 133 | return hose_head; |
| 134 | } |
| 135 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 136 | void pci_register_hose(struct pci_controller* hose) |
| 137 | { |
| 138 | struct pci_controller **phose = &hose_head; |
| 139 | |
| 140 | while(*phose) |
| 141 | phose = &(*phose)->next; |
| 142 | |
| 143 | hose->next = NULL; |
| 144 | |
| 145 | *phose = hose; |
| 146 | } |
| 147 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 148 | struct pci_controller *pci_bus_to_hose(int bus) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 149 | { |
| 150 | struct pci_controller *hose; |
| 151 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 152 | for (hose = pci_get_hose_head(); hose; hose = hose->next) { |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 153 | if (bus >= hose->first_busno && bus <= hose->last_busno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 154 | return hose; |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 155 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 156 | |
Rafal Jaworowski | 384da5e | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 157 | printf("pci_bus_to_hose() failed\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
| 160 | |
Kumar Gala | db943ed | 2010-12-17 05:57:25 -0600 | [diff] [blame] | 161 | struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr) |
| 162 | { |
| 163 | struct pci_controller *hose; |
| 164 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 165 | for (hose = pci_get_hose_head(); hose; hose = hose->next) { |
Kumar Gala | db943ed | 2010-12-17 05:57:25 -0600 | [diff] [blame] | 166 | if (hose->cfg_addr == cfg_addr) |
| 167 | return hose; |
| 168 | } |
| 169 | |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Anton Vorontsov | 597b8c4 | 2009-02-19 18:20:41 +0300 | [diff] [blame] | 173 | int pci_last_busno(void) |
| 174 | { |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 175 | struct pci_controller *hose = pci_get_hose_head(); |
Anton Vorontsov | 597b8c4 | 2009-02-19 18:20:41 +0300 | [diff] [blame] | 176 | |
| 177 | if (!hose) |
| 178 | return -1; |
| 179 | |
| 180 | while (hose->next) |
| 181 | hose = hose->next; |
| 182 | |
| 183 | return hose->last_busno; |
| 184 | } |
| 185 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 186 | pci_dev_t pci_find_devices(struct pci_device_id *ids, int index) |
| 187 | { |
| 188 | struct pci_controller * hose; |
| 189 | u16 vendor, device; |
| 190 | u8 header_type; |
| 191 | pci_dev_t bdf; |
| 192 | int i, bus, found_multi = 0; |
| 193 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 194 | for (hose = pci_get_hose_head(); hose; hose = hose->next) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 195 | #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 196 | for (bus = hose->last_busno; bus >= hose->first_busno; bus--) |
| 197 | #else |
| 198 | for (bus = hose->first_busno; bus <= hose->last_busno; bus++) |
| 199 | #endif |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 200 | for (bdf = PCI_BDF(bus, 0, 0); |
Heiko Schocher | d7f77fb | 2006-06-19 11:02:41 +0200 | [diff] [blame] | 201 | #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX) |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 202 | bdf < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1, |
| 203 | PCI_MAX_PCI_FUNCTIONS - 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | #else |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 205 | bdf < PCI_BDF(bus + 1, 0, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | #endif |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 207 | bdf += PCI_BDF(0, 0, 1)) { |
Thierry Reding | 699b6d3 | 2014-11-12 18:26:49 -0700 | [diff] [blame] | 208 | if (pci_skip_dev(hose, bdf)) |
| 209 | continue; |
| 210 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 211 | if (!PCI_FUNC(bdf)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 212 | pci_read_config_byte(bdf, |
| 213 | PCI_HEADER_TYPE, |
| 214 | &header_type); |
| 215 | |
| 216 | found_multi = header_type & 0x80; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 217 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 218 | if (!found_multi) |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | pci_read_config_word(bdf, |
| 223 | PCI_VENDOR_ID, |
| 224 | &vendor); |
| 225 | pci_read_config_word(bdf, |
| 226 | PCI_DEVICE_ID, |
| 227 | &device); |
| 228 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 229 | for (i = 0; ids[i].vendor != 0; i++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 230 | if (vendor == ids[i].vendor && |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 231 | device == ids[i].device) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 232 | if (index <= 0) |
| 233 | return bdf; |
| 234 | |
| 235 | index--; |
| 236 | } |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 237 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 241 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index) |
| 245 | { |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 246 | struct pci_device_id ids[2] = { {}, {0, 0} }; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 247 | |
| 248 | ids[0].vendor = vendor; |
| 249 | ids[0].device = device; |
| 250 | |
| 251 | return pci_find_devices(ids, index); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * |
| 256 | */ |
| 257 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 258 | int __pci_hose_phys_to_bus(struct pci_controller *hose, |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 259 | phys_addr_t phys_addr, |
| 260 | unsigned long flags, |
| 261 | unsigned long skip_mask, |
| 262 | pci_addr_t *ba) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 263 | { |
| 264 | struct pci_region *res; |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 265 | pci_addr_t bus_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 266 | int i; |
| 267 | |
wdenk | f4688a2 | 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 | |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 274 | if (res->flags & skip_mask) |
| 275 | continue; |
| 276 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 277 | bus_addr = phys_addr - res->phys_start + res->bus_start; |
| 278 | |
| 279 | if (bus_addr >= res->bus_start && |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 280 | bus_addr < res->bus_start + res->size) { |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 281 | *ba = bus_addr; |
| 282 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 286 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 289 | pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose, |
| 290 | phys_addr_t phys_addr, |
| 291 | unsigned long flags) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 292 | { |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 293 | pci_addr_t bus_addr = 0; |
| 294 | int ret; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 295 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 296 | if (!hose) { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 297 | puts("pci_hose_phys_to_bus: invalid hose\n"); |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 298 | return bus_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 301 | /* |
| 302 | * if PCI_REGION_MEM is set we do a two pass search with preference |
| 303 | * on matches that don't have PCI_REGION_SYS_MEMORY set |
| 304 | */ |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 305 | if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) { |
| 306 | ret = __pci_hose_phys_to_bus(hose, phys_addr, |
| 307 | flags, PCI_REGION_SYS_MEMORY, &bus_addr); |
| 308 | if (!ret) |
| 309 | return bus_addr; |
| 310 | } |
| 311 | |
| 312 | ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr); |
| 313 | |
| 314 | if (ret) |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 315 | puts("pci_hose_phys_to_bus: invalid physical address\n"); |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 316 | |
| 317 | return bus_addr; |
| 318 | } |
| 319 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 320 | int __pci_hose_bus_to_phys(struct pci_controller *hose, |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 321 | pci_addr_t bus_addr, |
| 322 | unsigned long flags, |
| 323 | unsigned long skip_mask, |
| 324 | phys_addr_t *pa) |
| 325 | { |
| 326 | struct pci_region *res; |
| 327 | int i; |
| 328 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 329 | for (i = 0; i < hose->region_count; i++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 330 | res = &hose->regions[i]; |
| 331 | |
| 332 | if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0) |
| 333 | continue; |
| 334 | |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 335 | if (res->flags & skip_mask) |
| 336 | continue; |
| 337 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 338 | if (bus_addr >= res->bus_start && |
Stephen Warren | 739a14c | 2014-08-11 16:09:28 -0600 | [diff] [blame] | 339 | (bus_addr - res->bus_start) < res->size) { |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 340 | *pa = (bus_addr - res->bus_start + res->phys_start); |
| 341 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 345 | return 1; |
| 346 | } |
| 347 | |
| 348 | phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose, |
| 349 | pci_addr_t bus_addr, |
| 350 | unsigned long flags) |
| 351 | { |
| 352 | phys_addr_t phys_addr = 0; |
| 353 | int ret; |
| 354 | |
| 355 | if (!hose) { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 356 | puts("pci_hose_bus_to_phys: invalid hose\n"); |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 357 | return phys_addr; |
| 358 | } |
| 359 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 360 | /* |
| 361 | * if PCI_REGION_MEM is set we do a two pass search with preference |
| 362 | * on matches that don't have PCI_REGION_SYS_MEMORY set |
| 363 | */ |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 364 | if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) { |
| 365 | ret = __pci_hose_bus_to_phys(hose, bus_addr, |
| 366 | flags, PCI_REGION_SYS_MEMORY, &phys_addr); |
| 367 | if (!ret) |
| 368 | return phys_addr; |
| 369 | } |
| 370 | |
| 371 | ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr); |
| 372 | |
| 373 | if (ret) |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 374 | puts("pci_hose_bus_to_phys: invalid physical address\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 375 | |
Kumar Gala | deb55f2 | 2009-02-06 09:49:32 -0600 | [diff] [blame] | 376 | return phys_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Simon Glass | 6ac5af4 | 2014-11-14 18:18:30 -0700 | [diff] [blame] | 379 | void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum, |
| 380 | u32 addr_and_ctrl) |
| 381 | { |
| 382 | int bar; |
| 383 | |
| 384 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 385 | pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl); |
| 386 | } |
| 387 | |
| 388 | u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum) |
| 389 | { |
| 390 | u32 addr; |
| 391 | int bar; |
| 392 | |
| 393 | bar = PCI_BASE_ADDRESS_0 + barnum * 4; |
| 394 | pci_hose_read_config_dword(hose, dev, bar, &addr); |
| 395 | if (addr & PCI_BASE_ADDRESS_SPACE_IO) |
| 396 | return addr & PCI_BASE_ADDRESS_IO_MASK; |
| 397 | else |
| 398 | return addr & PCI_BASE_ADDRESS_MEM_MASK; |
| 399 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 400 | |
| 401 | int pci_hose_config_device(struct pci_controller *hose, |
| 402 | pci_dev_t dev, |
| 403 | unsigned long io, |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 404 | pci_addr_t mem, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 405 | unsigned long command) |
| 406 | { |
Kumar Gala | 1873d5c | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 407 | u32 bar_response; |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 408 | unsigned int old_command; |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 409 | pci_addr_t bar_value; |
| 410 | pci_size_t bar_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 411 | unsigned char pin; |
| 412 | int bar, found_mem64; |
| 413 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 414 | debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io, |
| 415 | (u64)mem, command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 416 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 417 | pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 418 | |
Wolfgang Denk | 03bebef | 2010-03-09 14:27:25 +0100 | [diff] [blame] | 419 | for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 420 | pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); |
| 421 | pci_hose_read_config_dword(hose, dev, bar, &bar_response); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 422 | |
| 423 | if (!bar_response) |
| 424 | continue; |
| 425 | |
| 426 | found_mem64 = 0; |
| 427 | |
| 428 | /* Check the BAR type and set our address mask */ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 429 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 430 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 431 | /* round up region base address to a multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 432 | io = ((io - 1) | (bar_size - 1)) + 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 433 | bar_value = io; |
| 434 | /* compute new region base address */ |
| 435 | io = io + bar_size; |
| 436 | } else { |
| 437 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 438 | PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 439 | u32 bar_response_upper; |
| 440 | u64 bar64; |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 441 | pci_hose_write_config_dword(hose, dev, bar + 4, |
| 442 | 0xffffffff); |
| 443 | pci_hose_read_config_dword(hose, dev, bar + 4, |
| 444 | &bar_response_upper); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 445 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 446 | bar64 = ((u64)bar_response_upper << 32) | bar_response; |
| 447 | |
| 448 | bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
| 449 | found_mem64 = 1; |
| 450 | } else { |
| 451 | bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1); |
| 452 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 453 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 454 | /* round up region base address to multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 455 | mem = ((mem - 1) | (bar_size - 1)) + 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 456 | bar_value = mem; |
| 457 | /* compute new region base address */ |
| 458 | mem = mem + bar_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Write it out and update our limit */ |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 462 | pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 463 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 464 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 465 | bar += 4; |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 466 | #ifdef CONFIG_SYS_PCI_64BIT |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 467 | pci_hose_write_config_dword(hose, dev, bar, |
| 468 | (u32)(bar_value >> 32)); |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 469 | #else |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 470 | pci_hose_write_config_dword(hose, dev, bar, 0x00000000); |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 471 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | /* Configure Cache Line Size Register */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 476 | pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 477 | |
| 478 | /* Configure Latency Timer */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 479 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 480 | |
| 481 | /* Disable interrupt line, if device says it wants to use interrupts */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 482 | pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin); |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 483 | if (pin != 0) { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 484 | pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 487 | pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command); |
| 488 | pci_hose_write_config_dword(hose, dev, PCI_COMMAND, |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 489 | (old_command & 0xffff0000) | command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * |
| 496 | */ |
| 497 | |
| 498 | struct pci_config_table *pci_find_config(struct pci_controller *hose, |
| 499 | unsigned short class, |
| 500 | unsigned int vendor, |
| 501 | unsigned int device, |
| 502 | unsigned int bus, |
| 503 | unsigned int dev, |
| 504 | unsigned int func) |
| 505 | { |
| 506 | struct pci_config_table *table; |
| 507 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 508 | for (table = hose->config_table; table && table->vendor; table++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 509 | if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) && |
| 510 | (table->device == PCI_ANY_ID || table->device == device) && |
| 511 | (table->class == PCI_ANY_ID || table->class == class) && |
| 512 | (table->bus == PCI_ANY_ID || table->bus == bus) && |
| 513 | (table->dev == PCI_ANY_ID || table->dev == dev) && |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 514 | (table->func == PCI_ANY_ID || table->func == func)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 515 | return table; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | return NULL; |
| 520 | } |
| 521 | |
| 522 | void pci_cfgfunc_config_device(struct pci_controller *hose, |
| 523 | pci_dev_t dev, |
| 524 | struct pci_config_table *entry) |
| 525 | { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 526 | pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], |
| 527 | entry->priv[2]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void pci_cfgfunc_do_nothing(struct pci_controller *hose, |
| 531 | pci_dev_t dev, struct pci_config_table *entry) |
| 532 | { |
| 533 | } |
| 534 | |
| 535 | /* |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 536 | * HJF: Changed this to return int. I think this is required |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 537 | * to get the correct result when scanning bridges |
| 538 | */ |
| 539 | extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 540 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 541 | #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI_SCAN_SHOW) |
| 542 | const char * pci_class_str(u8 class) |
| 543 | { |
| 544 | switch (class) { |
| 545 | case PCI_CLASS_NOT_DEFINED: |
| 546 | return "Build before PCI Rev2.0"; |
| 547 | break; |
| 548 | case PCI_BASE_CLASS_STORAGE: |
| 549 | return "Mass storage controller"; |
| 550 | break; |
| 551 | case PCI_BASE_CLASS_NETWORK: |
| 552 | return "Network controller"; |
| 553 | break; |
| 554 | case PCI_BASE_CLASS_DISPLAY: |
| 555 | return "Display controller"; |
| 556 | break; |
| 557 | case PCI_BASE_CLASS_MULTIMEDIA: |
| 558 | return "Multimedia device"; |
| 559 | break; |
| 560 | case PCI_BASE_CLASS_MEMORY: |
| 561 | return "Memory controller"; |
| 562 | break; |
| 563 | case PCI_BASE_CLASS_BRIDGE: |
| 564 | return "Bridge device"; |
| 565 | break; |
| 566 | case PCI_BASE_CLASS_COMMUNICATION: |
| 567 | return "Simple comm. controller"; |
| 568 | break; |
| 569 | case PCI_BASE_CLASS_SYSTEM: |
| 570 | return "Base system peripheral"; |
| 571 | break; |
| 572 | case PCI_BASE_CLASS_INPUT: |
| 573 | return "Input device"; |
| 574 | break; |
| 575 | case PCI_BASE_CLASS_DOCKING: |
| 576 | return "Docking station"; |
| 577 | break; |
| 578 | case PCI_BASE_CLASS_PROCESSOR: |
| 579 | return "Processor"; |
| 580 | break; |
| 581 | case PCI_BASE_CLASS_SERIAL: |
| 582 | return "Serial bus controller"; |
| 583 | break; |
| 584 | case PCI_BASE_CLASS_INTELLIGENT: |
| 585 | return "Intelligent controller"; |
| 586 | break; |
| 587 | case PCI_BASE_CLASS_SATELLITE: |
| 588 | return "Satellite controller"; |
| 589 | break; |
| 590 | case PCI_BASE_CLASS_CRYPT: |
| 591 | return "Cryptographic device"; |
| 592 | break; |
| 593 | case PCI_BASE_CLASS_SIGNAL_PROCESSING: |
| 594 | return "DSP"; |
| 595 | break; |
| 596 | case PCI_CLASS_OTHERS: |
| 597 | return "Does not fit any class"; |
| 598 | break; |
| 599 | default: |
| 600 | return "???"; |
| 601 | break; |
| 602 | }; |
| 603 | } |
| 604 | #endif /* CONFIG_CMD_PCI || CONFIG_PCI_SCAN_SHOW */ |
| 605 | |
Jeroen Hofstee | ab1bca5 | 2014-10-08 22:57:27 +0200 | [diff] [blame] | 606 | __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev) |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 607 | { |
| 608 | /* |
| 609 | * Check if pci device should be skipped in configuration |
| 610 | */ |
| 611 | if (dev == PCI_BDF(hose->first_busno, 0, 0)) { |
| 612 | #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */ |
| 613 | /* |
| 614 | * Only skip configuration if "pciconfighost" is not set |
| 615 | */ |
| 616 | if (getenv("pciconfighost") == NULL) |
| 617 | return 1; |
| 618 | #else |
| 619 | return 1; |
| 620 | #endif |
| 621 | } |
| 622 | |
| 623 | return 0; |
| 624 | } |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 625 | |
| 626 | #ifdef CONFIG_PCI_SCAN_SHOW |
Jeroen Hofstee | ab1bca5 | 2014-10-08 22:57:27 +0200 | [diff] [blame] | 627 | __weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev) |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 628 | { |
| 629 | if (dev == PCI_BDF(hose->first_busno, 0, 0)) |
| 630 | return 0; |
| 631 | |
| 632 | return 1; |
| 633 | } |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 634 | #endif /* CONFIG_PCI_SCAN_SHOW */ |
| 635 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 636 | int pci_hose_scan_bus(struct pci_controller *hose, int bus) |
| 637 | { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 638 | unsigned int sub_bus, found_multi = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 639 | unsigned short vendor, device, class; |
| 640 | unsigned char header_type; |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 641 | #ifndef CONFIG_PCI_PNP |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 642 | struct pci_config_table *cfg; |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 643 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 644 | pci_dev_t dev; |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 645 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 646 | static int indent = 0; |
| 647 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 648 | |
| 649 | sub_bus = bus; |
| 650 | |
| 651 | for (dev = PCI_BDF(bus,0,0); |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 652 | dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1, |
| 653 | PCI_MAX_PCI_FUNCTIONS - 1); |
| 654 | dev += PCI_BDF(0, 0, 1)) { |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 655 | |
| 656 | if (pci_skip_dev(hose, dev)) |
| 657 | continue; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 658 | |
| 659 | if (PCI_FUNC(dev) && !found_multi) |
| 660 | continue; |
| 661 | |
| 662 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type); |
| 663 | |
| 664 | pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor); |
| 665 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 666 | if (vendor == 0xffff || vendor == 0x0000) |
| 667 | continue; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 668 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 669 | if (!PCI_FUNC(dev)) |
| 670 | found_multi = header_type & 0x80; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 671 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 672 | debug("PCI Scan: Found Bus %d, Device %d, Function %d\n", |
| 673 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 674 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 675 | pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device); |
| 676 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 677 | |
Tim Harvey | 231c076 | 2014-08-07 22:49:56 -0700 | [diff] [blame] | 678 | #ifdef CONFIG_PCI_FIXUP_DEV |
| 679 | board_pci_fixup_dev(hose, dev, vendor, device, class); |
| 680 | #endif |
| 681 | |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 682 | #ifdef CONFIG_PCI_SCAN_SHOW |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 683 | indent++; |
| 684 | |
| 685 | /* Print leading space, including bus indentation */ |
| 686 | printf("%*c", indent + 1, ' '); |
| 687 | |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 688 | if (pci_print_dev(hose, dev)) { |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 689 | printf("%02x:%02x.%-*x - %04x:%04x - %s\n", |
| 690 | PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev), |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 691 | vendor, device, pci_class_str(class >> 8)); |
| 692 | } |
| 693 | #endif |
| 694 | |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 695 | #ifdef CONFIG_PCI_PNP |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 696 | sub_bus = max((unsigned int)pciauto_config_device(hose, dev), |
| 697 | sub_bus); |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 698 | #else |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 699 | cfg = pci_find_config(hose, class, vendor, device, |
| 700 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); |
| 701 | if (cfg) { |
| 702 | cfg->config_device(hose, dev, cfg); |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 703 | sub_bus = max(sub_bus, |
| 704 | (unsigned int)hose->current_busno); |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 705 | } |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 706 | #endif |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 707 | |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 708 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 709 | indent--; |
| 710 | #endif |
| 711 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 712 | if (hose->fixup_irq) |
| 713 | hose->fixup_irq(hose, dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | return sub_bus; |
| 717 | } |
| 718 | |
| 719 | int pci_hose_scan(struct pci_controller *hose) |
| 720 | { |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 721 | #if defined(CONFIG_PCI_BOOTDELAY) |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 722 | char *s; |
| 723 | int i; |
| 724 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 725 | if (!gd->pcidelay_done) { |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 726 | /* wait "pcidelay" ms (if defined)... */ |
| 727 | s = getenv("pcidelay"); |
| 728 | if (s) { |
| 729 | int val = simple_strtoul(s, NULL, 10); |
| 730 | for (i = 0; i < val; i++) |
| 731 | udelay(1000); |
| 732 | } |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 733 | gd->pcidelay_done = 1; |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 734 | } |
| 735 | #endif /* CONFIG_PCI_BOOTDELAY */ |
| 736 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 737 | /* |
| 738 | * Start scan at current_busno. |
Ed Swarthout | f0d4347 | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 739 | * PCIe will start scan at first_busno+1. |
| 740 | */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 741 | /* For legacy support, ensure current >= first */ |
Ed Swarthout | f0d4347 | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 742 | if (hose->first_busno > hose->current_busno) |
| 743 | hose->current_busno = hose->first_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 744 | #ifdef CONFIG_PCI_PNP |
| 745 | pciauto_config_init(hose); |
| 746 | #endif |
Ed Swarthout | f0d4347 | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 747 | return pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 748 | } |
| 749 | |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 750 | void pci_init(void) |
| 751 | { |
John Schmoller | 60e877f | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 752 | hose_head = NULL; |
| 753 | |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 754 | /* now call board specific pci_init()... */ |
| 755 | pci_init_board(); |
| 756 | } |
Zhao Qiang | 5d39f74 | 2013-10-12 13:46:33 +0800 | [diff] [blame] | 757 | |
| 758 | /* Returns the address of the requested capability structure within the |
| 759 | * device's PCI configuration space or 0 in case the device does not |
| 760 | * support it. |
| 761 | * */ |
| 762 | int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev, |
| 763 | int cap) |
| 764 | { |
| 765 | int pos; |
| 766 | u8 hdr_type; |
| 767 | |
| 768 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type); |
| 769 | |
| 770 | pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F); |
| 771 | |
| 772 | if (pos) |
| 773 | pos = pci_find_cap(hose, dev, pos, cap); |
| 774 | |
| 775 | return pos; |
| 776 | } |
| 777 | |
| 778 | /* Find the header pointer to the Capabilities*/ |
| 779 | int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev, |
| 780 | u8 hdr_type) |
| 781 | { |
| 782 | u16 status; |
| 783 | |
| 784 | pci_hose_read_config_word(hose, dev, PCI_STATUS, &status); |
| 785 | |
| 786 | if (!(status & PCI_STATUS_CAP_LIST)) |
| 787 | return 0; |
| 788 | |
| 789 | switch (hdr_type) { |
| 790 | case PCI_HEADER_TYPE_NORMAL: |
| 791 | case PCI_HEADER_TYPE_BRIDGE: |
| 792 | return PCI_CAPABILITY_LIST; |
| 793 | case PCI_HEADER_TYPE_CARDBUS: |
| 794 | return PCI_CB_CAPABILITY_LIST; |
| 795 | default: |
| 796 | return 0; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap) |
| 801 | { |
| 802 | int ttl = PCI_FIND_CAP_TTL; |
| 803 | u8 id; |
| 804 | u8 next_pos; |
| 805 | |
| 806 | while (ttl--) { |
| 807 | pci_hose_read_config_byte(hose, dev, pos, &next_pos); |
| 808 | if (next_pos < CAP_START_POS) |
| 809 | break; |
| 810 | next_pos &= ~3; |
| 811 | pos = (int) next_pos; |
| 812 | pci_hose_read_config_byte(hose, dev, |
| 813 | pos + PCI_CAP_LIST_ID, &id); |
| 814 | if (id == 0xff) |
| 815 | break; |
| 816 | if (id == cap) |
| 817 | return pos; |
| 818 | pos += PCI_CAP_LIST_NEXT; |
| 819 | } |
| 820 | return 0; |
| 821 | } |