Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 4 | * Andreas Heppel <aheppel@sysgo.de> |
| 5 | * |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 6 | * (C) Copyright 2002, 2003 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
Simon Glass | 0ba553a | 2015-11-29 13:17:46 -0700 | [diff] [blame] | 11 | * Old PCI routines |
| 12 | * |
| 13 | * Do not change this file. Instead, convert your board to use CONFIG_DM_PCI |
| 14 | * and change pci-uclass.c. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <common.h> |
Simon Glass | 18afe10 | 2019-11-14 12:57:47 -0700 | [diff] [blame] | 18 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 20 | #include <asm/global_data.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 24 | #include <env.h> |
Simon Glass | 62034ff | 2015-01-27 22:13:27 -0700 | [diff] [blame] | 25 | #include <errno.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 26 | #include <asm/processor.h> |
| 27 | #include <asm/io.h> |
| 28 | #include <pci.h> |
| 29 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 32 | #define PCI_HOSE_OP(rw, size, type) \ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 33 | int pci_hose_##rw##_config_##size(struct pci_controller *hose, \ |
| 34 | pci_dev_t dev, \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 35 | int offset, type value) \ |
| 36 | { \ |
| 37 | return hose->rw##_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | PCI_HOSE_OP(read, byte, u8 *) |
| 41 | PCI_HOSE_OP(read, word, u16 *) |
| 42 | PCI_HOSE_OP(read, dword, u32 *) |
| 43 | PCI_HOSE_OP(write, byte, u8) |
| 44 | PCI_HOSE_OP(write, word, u16) |
| 45 | PCI_HOSE_OP(write, dword, u32) |
| 46 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 47 | #define PCI_OP(rw, size, type, error_code) \ |
| 48 | int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \ |
| 49 | { \ |
| 50 | struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \ |
| 51 | \ |
| 52 | if (!hose) \ |
| 53 | { \ |
| 54 | error_code; \ |
| 55 | return -1; \ |
| 56 | } \ |
| 57 | \ |
| 58 | return pci_hose_##rw##_config_##size(hose, dev, offset, value); \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | PCI_OP(read, byte, u8 *, *value = 0xff) |
| 62 | PCI_OP(read, word, u16 *, *value = 0xffff) |
| 63 | PCI_OP(read, dword, u32 *, *value = 0xffffffff) |
| 64 | PCI_OP(write, byte, u8, ) |
| 65 | PCI_OP(write, word, u16, ) |
| 66 | PCI_OP(write, dword, u32, ) |
| 67 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 68 | #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \ |
| 69 | int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 70 | pci_dev_t dev, \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 71 | int offset, type val) \ |
| 72 | { \ |
| 73 | u32 val32; \ |
| 74 | \ |
Shinya Kuribayashi | f19da9d | 2007-08-17 12:43:44 +0900 | [diff] [blame] | 75 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \ |
| 76 | *val = -1; \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 77 | return -1; \ |
Shinya Kuribayashi | f19da9d | 2007-08-17 12:43:44 +0900 | [diff] [blame] | 78 | } \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 79 | \ |
| 80 | *val = (val32 >> ((offset & (int)off_mask) * 8)); \ |
| 81 | \ |
| 82 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | } |
| 84 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 85 | #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \ |
| 86 | int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 87 | pci_dev_t dev, \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 88 | int offset, type val) \ |
| 89 | { \ |
wdenk | 19c8fb7 | 2004-04-18 22:26:17 +0000 | [diff] [blame] | 90 | u32 val32, mask, ldata, shift; \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 91 | \ |
| 92 | if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\ |
| 93 | return -1; \ |
| 94 | \ |
wdenk | 19c8fb7 | 2004-04-18 22:26:17 +0000 | [diff] [blame] | 95 | shift = ((offset & (int)off_mask) * 8); \ |
| 96 | ldata = (((unsigned long)val) & val_mask) << shift; \ |
| 97 | mask = val_mask << shift; \ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 98 | val32 = (val32 & ~mask) | ldata; \ |
| 99 | \ |
| 100 | if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\ |
| 101 | return -1; \ |
| 102 | \ |
| 103 | return 0; \ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03) |
| 107 | PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02) |
| 108 | PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff) |
| 109 | PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff) |
| 110 | |
| 111 | /* |
| 112 | * |
| 113 | */ |
| 114 | |
John Schmoller | 60e877f | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 115 | static struct pci_controller* hose_head; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 117 | struct pci_controller *pci_get_hose_head(void) |
| 118 | { |
| 119 | if (gd->hose) |
| 120 | return gd->hose; |
| 121 | |
| 122 | return hose_head; |
| 123 | } |
| 124 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 125 | void pci_register_hose(struct pci_controller* hose) |
| 126 | { |
| 127 | struct pci_controller **phose = &hose_head; |
| 128 | |
| 129 | while(*phose) |
| 130 | phose = &(*phose)->next; |
| 131 | |
| 132 | hose->next = NULL; |
| 133 | |
| 134 | *phose = hose; |
| 135 | } |
| 136 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 137 | struct pci_controller *pci_bus_to_hose(int bus) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 138 | { |
| 139 | struct pci_controller *hose; |
| 140 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 141 | for (hose = pci_get_hose_head(); hose; hose = hose->next) { |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 142 | if (bus >= hose->first_busno && bus <= hose->last_busno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 143 | return hose; |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 144 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 145 | |
Rafal Jaworowski | 384da5e | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 146 | printf("pci_bus_to_hose() failed\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | return NULL; |
| 148 | } |
| 149 | |
Kumar Gala | db943ed | 2010-12-17 05:57:25 -0600 | [diff] [blame] | 150 | struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr) |
| 151 | { |
| 152 | struct pci_controller *hose; |
| 153 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 154 | for (hose = pci_get_hose_head(); hose; hose = hose->next) { |
Kumar Gala | db943ed | 2010-12-17 05:57:25 -0600 | [diff] [blame] | 155 | if (hose->cfg_addr == cfg_addr) |
| 156 | return hose; |
| 157 | } |
| 158 | |
| 159 | return NULL; |
| 160 | } |
| 161 | |
Anton Vorontsov | 597b8c4 | 2009-02-19 18:20:41 +0300 | [diff] [blame] | 162 | int pci_last_busno(void) |
| 163 | { |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 164 | struct pci_controller *hose = pci_get_hose_head(); |
Anton Vorontsov | 597b8c4 | 2009-02-19 18:20:41 +0300 | [diff] [blame] | 165 | |
| 166 | if (!hose) |
| 167 | return -1; |
| 168 | |
| 169 | while (hose->next) |
| 170 | hose = hose->next; |
| 171 | |
| 172 | return hose->last_busno; |
| 173 | } |
| 174 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 175 | pci_dev_t pci_find_devices(struct pci_device_id *ids, int index) |
| 176 | { |
| 177 | struct pci_controller * hose; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 178 | pci_dev_t bdf; |
Simon Glass | 75532d8 | 2015-03-05 12:25:24 -0700 | [diff] [blame] | 179 | int bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 180 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 181 | for (hose = pci_get_hose_head(); hose; hose = hose->next) { |
Simon Glass | 75532d8 | 2015-03-05 12:25:24 -0700 | [diff] [blame] | 182 | for (bus = hose->first_busno; bus <= hose->last_busno; bus++) { |
Simon Glass | 75532d8 | 2015-03-05 12:25:24 -0700 | [diff] [blame] | 183 | bdf = pci_hose_find_devices(hose, bus, ids, &index); |
| 184 | if (bdf != -1) |
Simon Glass | 62034ff | 2015-01-27 22:13:27 -0700 | [diff] [blame] | 185 | return bdf; |
Simon Glass | 62034ff | 2015-01-27 22:13:27 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Simon Glass | 75532d8 | 2015-03-05 12:25:24 -0700 | [diff] [blame] | 189 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Simon Glass | a593455 | 2019-02-16 20:24:40 -0700 | [diff] [blame] | 192 | static int pci_hose_config_device(struct pci_controller *hose, pci_dev_t dev, |
| 193 | ulong io, pci_addr_t mem, ulong command) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 194 | { |
Kumar Gala | 1873d5c | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 195 | u32 bar_response; |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 196 | unsigned int old_command; |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 197 | pci_addr_t bar_value; |
| 198 | pci_size_t bar_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 199 | unsigned char pin; |
| 200 | int bar, found_mem64; |
| 201 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 202 | debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io, |
| 203 | (u64)mem, command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 205 | pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | |
Wolfgang Denk | 03bebef | 2010-03-09 14:27:25 +0100 | [diff] [blame] | 207 | 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] | 208 | pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); |
| 209 | pci_hose_read_config_dword(hose, dev, bar, &bar_response); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 210 | |
| 211 | if (!bar_response) |
| 212 | continue; |
| 213 | |
| 214 | found_mem64 = 0; |
| 215 | |
| 216 | /* Check the BAR type and set our address mask */ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 217 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 218 | bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 219 | /* round up region base address to a multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | io = ((io - 1) | (bar_size - 1)) + 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 221 | bar_value = io; |
| 222 | /* compute new region base address */ |
| 223 | io = io + bar_size; |
| 224 | } else { |
| 225 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 226 | PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 227 | u32 bar_response_upper; |
| 228 | u64 bar64; |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 229 | pci_hose_write_config_dword(hose, dev, bar + 4, |
| 230 | 0xffffffff); |
| 231 | pci_hose_read_config_dword(hose, dev, bar + 4, |
| 232 | &bar_response_upper); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 234 | bar64 = ((u64)bar_response_upper << 32) | bar_response; |
| 235 | |
| 236 | bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
| 237 | found_mem64 = 1; |
| 238 | } else { |
| 239 | bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1); |
| 240 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 242 | /* round up region base address to multiple of size */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 243 | mem = ((mem - 1) | (bar_size - 1)) + 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 244 | bar_value = mem; |
| 245 | /* compute new region base address */ |
| 246 | mem = mem + bar_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* Write it out and update our limit */ |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 250 | pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 251 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 252 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | bar += 4; |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 254 | #ifdef CONFIG_SYS_PCI_64BIT |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 255 | pci_hose_write_config_dword(hose, dev, bar, |
| 256 | (u32)(bar_value >> 32)); |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 257 | #else |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 258 | pci_hose_write_config_dword(hose, dev, bar, 0x00000000); |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 259 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | /* Configure Cache Line Size Register */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 264 | pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 265 | |
| 266 | /* Configure Latency Timer */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 267 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | |
| 269 | /* Disable interrupt line, if device says it wants to use interrupts */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 270 | pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin); |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 271 | if (pin != 0) { |
Simon Glass | 84f5733 | 2015-07-27 15:47:17 -0600 | [diff] [blame] | 272 | pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, |
| 273 | PCI_INTERRUPT_LINE_DISABLE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 276 | pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command); |
| 277 | pci_hose_write_config_dword(hose, dev, PCI_COMMAND, |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 278 | (old_command & 0xffff0000) | command); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * |
| 285 | */ |
| 286 | |
| 287 | struct pci_config_table *pci_find_config(struct pci_controller *hose, |
| 288 | unsigned short class, |
| 289 | unsigned int vendor, |
| 290 | unsigned int device, |
| 291 | unsigned int bus, |
| 292 | unsigned int dev, |
| 293 | unsigned int func) |
| 294 | { |
| 295 | struct pci_config_table *table; |
| 296 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 297 | for (table = hose->config_table; table && table->vendor; table++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 298 | if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) && |
| 299 | (table->device == PCI_ANY_ID || table->device == device) && |
| 300 | (table->class == PCI_ANY_ID || table->class == class) && |
| 301 | (table->bus == PCI_ANY_ID || table->bus == bus) && |
| 302 | (table->dev == PCI_ANY_ID || table->dev == dev) && |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 303 | (table->func == PCI_ANY_ID || table->func == func)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 304 | return table; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | void pci_cfgfunc_config_device(struct pci_controller *hose, |
| 312 | pci_dev_t dev, |
| 313 | struct pci_config_table *entry) |
| 314 | { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 315 | pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], |
| 316 | entry->priv[2]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void pci_cfgfunc_do_nothing(struct pci_controller *hose, |
| 320 | pci_dev_t dev, struct pci_config_table *entry) |
| 321 | { |
| 322 | } |
| 323 | |
| 324 | /* |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 325 | * HJF: Changed this to return int. I think this is required |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 326 | * to get the correct result when scanning bridges |
| 327 | */ |
| 328 | extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 329 | |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 330 | #ifdef CONFIG_PCI_SCAN_SHOW |
Jeroen Hofstee | ab1bca5 | 2014-10-08 22:57:27 +0200 | [diff] [blame] | 331 | __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] | 332 | { |
| 333 | if (dev == PCI_BDF(hose->first_busno, 0, 0)) |
| 334 | return 0; |
| 335 | |
| 336 | return 1; |
| 337 | } |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 338 | #endif /* CONFIG_PCI_SCAN_SHOW */ |
| 339 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | int pci_hose_scan_bus(struct pci_controller *hose, int bus) |
| 341 | { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 342 | unsigned int sub_bus, found_multi = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 343 | unsigned short vendor, device, class; |
| 344 | unsigned char header_type; |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 345 | #ifndef CONFIG_PCI_PNP |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 346 | struct pci_config_table *cfg; |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 347 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 348 | pci_dev_t dev; |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 349 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 350 | static int indent = 0; |
| 351 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 352 | |
| 353 | sub_bus = bus; |
| 354 | |
| 355 | for (dev = PCI_BDF(bus,0,0); |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 356 | dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1, |
| 357 | PCI_MAX_PCI_FUNCTIONS - 1); |
| 358 | dev += PCI_BDF(0, 0, 1)) { |
Stefan Roese | 41e846f | 2008-07-08 12:01:47 +0200 | [diff] [blame] | 359 | |
| 360 | if (pci_skip_dev(hose, dev)) |
| 361 | continue; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 362 | |
| 363 | if (PCI_FUNC(dev) && !found_multi) |
| 364 | continue; |
| 365 | |
| 366 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type); |
| 367 | |
| 368 | pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor); |
| 369 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 370 | if (vendor == 0xffff || vendor == 0x0000) |
| 371 | continue; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 372 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 373 | if (!PCI_FUNC(dev)) |
| 374 | found_multi = header_type & 0x80; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 375 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 376 | debug("PCI Scan: Found Bus %d, Device %d, Function %d\n", |
| 377 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 378 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 379 | pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device); |
| 380 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 381 | |
Tim Harvey | 231c076 | 2014-08-07 22:49:56 -0700 | [diff] [blame] | 382 | #ifdef CONFIG_PCI_FIXUP_DEV |
| 383 | board_pci_fixup_dev(hose, dev, vendor, device, class); |
| 384 | #endif |
| 385 | |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 386 | #ifdef CONFIG_PCI_SCAN_SHOW |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 387 | indent++; |
| 388 | |
| 389 | /* Print leading space, including bus indentation */ |
| 390 | printf("%*c", indent + 1, ' '); |
| 391 | |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 392 | if (pci_print_dev(hose, dev)) { |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 393 | printf("%02x:%02x.%-*x - %04x:%04x - %s\n", |
| 394 | PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev), |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 395 | vendor, device, pci_class_str(class >> 8)); |
| 396 | } |
| 397 | #endif |
| 398 | |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 399 | #ifdef CONFIG_PCI_PNP |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 400 | sub_bus = max((unsigned int)pciauto_config_device(hose, dev), |
| 401 | sub_bus); |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 402 | #else |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 403 | cfg = pci_find_config(hose, class, vendor, device, |
| 404 | PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev)); |
| 405 | if (cfg) { |
| 406 | cfg->config_device(hose, dev, cfg); |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 407 | sub_bus = max(sub_bus, |
| 408 | (unsigned int)hose->current_busno); |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 409 | } |
Andrew Sharp | 0e2dc47 | 2012-08-29 14:16:30 +0000 | [diff] [blame] | 410 | #endif |
Peter Tyser | d40cdc4 | 2010-10-29 17:59:28 -0500 | [diff] [blame] | 411 | |
Peter Tyser | b46513d | 2010-10-29 17:59:29 -0500 | [diff] [blame] | 412 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 413 | indent--; |
| 414 | #endif |
| 415 | |
Peter Tyser | 22ccb7f | 2010-10-29 17:59:27 -0500 | [diff] [blame] | 416 | if (hose->fixup_irq) |
| 417 | hose->fixup_irq(hose, dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return sub_bus; |
| 421 | } |
| 422 | |
| 423 | int pci_hose_scan(struct pci_controller *hose) |
| 424 | { |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 425 | #if defined(CONFIG_PCI_BOOTDELAY) |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 426 | char *s; |
| 427 | int i; |
| 428 | |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 429 | if (!gd->pcidelay_done) { |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 430 | /* wait "pcidelay" ms (if defined)... */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 431 | s = env_get("pcidelay"); |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 432 | if (s) { |
| 433 | int val = simple_strtoul(s, NULL, 10); |
| 434 | for (i = 0; i < val; i++) |
| 435 | udelay(1000); |
| 436 | } |
Bin Meng | f1b81fc | 2014-12-30 22:53:21 +0800 | [diff] [blame] | 437 | gd->pcidelay_done = 1; |
Anatolij Gustschin | 2bd2cb1 | 2011-10-11 22:44:30 +0000 | [diff] [blame] | 438 | } |
| 439 | #endif /* CONFIG_PCI_BOOTDELAY */ |
| 440 | |
Tim Harvey | fc409be | 2015-05-08 15:16:07 -0700 | [diff] [blame] | 441 | #ifdef CONFIG_PCI_SCAN_SHOW |
| 442 | puts("PCI:\n"); |
| 443 | #endif |
| 444 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 445 | /* |
| 446 | * Start scan at current_busno. |
Ed Swarthout | f0d4347 | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 447 | * PCIe will start scan at first_busno+1. |
| 448 | */ |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 449 | /* For legacy support, ensure current >= first */ |
Ed Swarthout | f0d4347 | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 450 | if (hose->first_busno > hose->current_busno) |
| 451 | hose->current_busno = hose->first_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 452 | #ifdef CONFIG_PCI_PNP |
| 453 | pciauto_config_init(hose); |
| 454 | #endif |
Ed Swarthout | f0d4347 | 2007-07-11 14:51:35 -0500 | [diff] [blame] | 455 | return pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Ovidiu Panait | e353edb | 2020-11-28 10:43:12 +0200 | [diff] [blame] | 458 | int pci_init(void) |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 459 | { |
John Schmoller | 60e877f | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 460 | hose_head = NULL; |
| 461 | |
Tim Harvey | 341fcb1 | 2016-06-17 06:20:25 -0700 | [diff] [blame] | 462 | /* allow env to disable pci init/enum */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 463 | if (env_get("pcidisable") != NULL) |
Ovidiu Panait | e353edb | 2020-11-28 10:43:12 +0200 | [diff] [blame] | 464 | return 0; |
Tim Harvey | 341fcb1 | 2016-06-17 06:20:25 -0700 | [diff] [blame] | 465 | |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 466 | /* now call board specific pci_init()... */ |
| 467 | pci_init_board(); |
Ovidiu Panait | e353edb | 2020-11-28 10:43:12 +0200 | [diff] [blame] | 468 | |
| 469 | return 0; |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 470 | } |
Zhao Qiang | 5d39f74 | 2013-10-12 13:46:33 +0800 | [diff] [blame] | 471 | |
| 472 | /* Returns the address of the requested capability structure within the |
| 473 | * device's PCI configuration space or 0 in case the device does not |
| 474 | * support it. |
| 475 | * */ |
| 476 | int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev, |
| 477 | int cap) |
| 478 | { |
| 479 | int pos; |
| 480 | u8 hdr_type; |
| 481 | |
| 482 | pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type); |
| 483 | |
| 484 | pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F); |
| 485 | |
| 486 | if (pos) |
| 487 | pos = pci_find_cap(hose, dev, pos, cap); |
| 488 | |
| 489 | return pos; |
| 490 | } |
| 491 | |
| 492 | /* Find the header pointer to the Capabilities*/ |
| 493 | int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev, |
| 494 | u8 hdr_type) |
| 495 | { |
| 496 | u16 status; |
| 497 | |
| 498 | pci_hose_read_config_word(hose, dev, PCI_STATUS, &status); |
| 499 | |
| 500 | if (!(status & PCI_STATUS_CAP_LIST)) |
| 501 | return 0; |
| 502 | |
| 503 | switch (hdr_type) { |
| 504 | case PCI_HEADER_TYPE_NORMAL: |
| 505 | case PCI_HEADER_TYPE_BRIDGE: |
| 506 | return PCI_CAPABILITY_LIST; |
| 507 | case PCI_HEADER_TYPE_CARDBUS: |
| 508 | return PCI_CB_CAPABILITY_LIST; |
| 509 | default: |
| 510 | return 0; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap) |
| 515 | { |
| 516 | int ttl = PCI_FIND_CAP_TTL; |
| 517 | u8 id; |
| 518 | u8 next_pos; |
| 519 | |
| 520 | while (ttl--) { |
| 521 | pci_hose_read_config_byte(hose, dev, pos, &next_pos); |
| 522 | if (next_pos < CAP_START_POS) |
| 523 | break; |
| 524 | next_pos &= ~3; |
| 525 | pos = (int) next_pos; |
| 526 | pci_hose_read_config_byte(hose, dev, |
| 527 | pos + PCI_CAP_LIST_ID, &id); |
| 528 | if (id == 0xff) |
| 529 | break; |
| 530 | if (id == cap) |
| 531 | return pos; |
| 532 | pos += PCI_CAP_LIST_NEXT; |
| 533 | } |
| 534 | return 0; |
| 535 | } |
Minghuan Lian | c5bc6aa | 2015-07-10 11:35:08 +0800 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * pci_find_next_ext_capability - Find an extended capability |
| 539 | * |
| 540 | * Returns the address of the next matching extended capability structure |
| 541 | * within the device's PCI configuration space or 0 if the device does |
| 542 | * not support it. Some capabilities can occur several times, e.g., the |
| 543 | * vendor-specific capability, and this provides a way to find them all. |
| 544 | */ |
| 545 | int pci_find_next_ext_capability(struct pci_controller *hose, pci_dev_t dev, |
| 546 | int start, int cap) |
| 547 | { |
| 548 | u32 header; |
| 549 | int ttl, pos = PCI_CFG_SPACE_SIZE; |
| 550 | |
| 551 | /* minimum 8 bytes per capability */ |
| 552 | ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; |
| 553 | |
| 554 | if (start) |
| 555 | pos = start; |
| 556 | |
| 557 | pci_hose_read_config_dword(hose, dev, pos, &header); |
| 558 | if (header == 0xffffffff || header == 0) |
| 559 | return 0; |
| 560 | |
| 561 | while (ttl-- > 0) { |
| 562 | if (PCI_EXT_CAP_ID(header) == cap && pos != start) |
| 563 | return pos; |
| 564 | |
| 565 | pos = PCI_EXT_CAP_NEXT(header); |
| 566 | if (pos < PCI_CFG_SPACE_SIZE) |
| 567 | break; |
| 568 | |
| 569 | pci_hose_read_config_dword(hose, dev, pos, &header); |
| 570 | if (header == 0xffffffff || header == 0) |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * pci_hose_find_ext_capability - Find an extended capability |
| 579 | * |
| 580 | * Returns the address of the requested extended capability structure |
| 581 | * within the device's PCI configuration space or 0 if the device does |
| 582 | * not support it. |
| 583 | */ |
| 584 | int pci_hose_find_ext_capability(struct pci_controller *hose, pci_dev_t dev, |
| 585 | int cap) |
| 586 | { |
| 587 | return pci_find_next_ext_capability(hose, dev, 0, cap); |
| 588 | } |