wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | 88fbf93 | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 2 | * arch/powerpc/kernel/pci_auto.c |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * |
| 4 | * PCI autoconfiguration library |
| 5 | * |
| 6 | * Author: Matt Porter <mporter@mvista.com> |
| 7 | * |
| 8 | * Copyright 2000 MontaVista Software Inc. |
| 9 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 15 | #include <pci.h> |
| 16 | |
| 17 | #undef DEBUG |
| 18 | #ifdef DEBUG |
| 19 | #define DEBUGF(x...) printf(x) |
| 20 | #else |
| 21 | #define DEBUGF(x...) |
| 22 | #endif /* DEBUG */ |
| 23 | |
| 24 | #define PCIAUTO_IDE_MODE_MASK 0x05 |
| 25 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 26 | /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */ |
| 27 | #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE |
| 28 | #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8 |
Gary Jennejohn | 9a1263f | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 29 | #endif |
| 30 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 31 | /* |
| 32 | * |
| 33 | */ |
| 34 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 35 | void pciauto_region_init(struct pci_region *res) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | { |
Sergei Shtylyov | 9679f4d | 2007-04-23 15:30:39 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Avoid allocating PCI resources from address 0 -- this is illegal |
| 39 | * according to PCI 2.1 and moreover, this is known to cause Linux IDE |
| 40 | * drivers to fail. Use a reasonable starting value of 0x1000 instead. |
| 41 | */ |
| 42 | res->bus_lower = res->bus_start ? res->bus_start : 0x1000; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 45 | void pciauto_region_align(struct pci_region *res, pci_size_t size) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | { |
| 47 | res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 48 | } |
| 49 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 50 | int pciauto_region_allocate(struct pci_region *res, pci_size_t size, |
| 51 | pci_addr_t *bar) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | { |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 53 | pci_addr_t addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 55 | if (!res) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | DEBUGF("No resource"); |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | addr = ((res->bus_lower - 1) | (size - 1)) + 1; |
| 61 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 62 | if (addr - res->bus_start + size > res->size) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | DEBUGF("No room in resource"); |
| 64 | goto error; |
| 65 | } |
| 66 | |
| 67 | res->bus_lower = addr + size; |
| 68 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 69 | DEBUGF("address=0x%llx bus_lower=0x%llx", (u64)addr, (u64)res->bus_lower); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 70 | |
| 71 | *bar = addr; |
| 72 | return 0; |
| 73 | |
| 74 | error: |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 75 | *bar = (pci_addr_t)-1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * |
| 81 | */ |
| 82 | |
| 83 | void pciauto_setup_device(struct pci_controller *hose, |
| 84 | pci_dev_t dev, int bars_num, |
| 85 | struct pci_region *mem, |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 86 | struct pci_region *prefetch, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 87 | struct pci_region *io) |
| 88 | { |
Kumar Gala | 1873d5c | 2012-09-19 04:47:36 +0000 | [diff] [blame] | 89 | u32 bar_response; |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 90 | pci_size_t bar_size; |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 91 | u16 cmdstat = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | int bar, bar_nr = 0; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 93 | #ifndef CONFIG_PCI_ENUM_ONLY |
| 94 | pci_addr_t bar_value; |
| 95 | struct pci_region *bar_res; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | int found_mem64 = 0; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 97 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 99 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER; |
| 101 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 102 | for (bar = PCI_BASE_ADDRESS_0; |
| 103 | bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 104 | /* Tickle the BAR and get the response */ |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 105 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | pci_hose_write_config_dword(hose, dev, bar, 0xffffffff); |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 107 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | pci_hose_read_config_dword(hose, dev, bar, &bar_response); |
| 109 | |
| 110 | /* If BAR is not implemented go to the next BAR */ |
| 111 | if (!bar_response) |
| 112 | continue; |
| 113 | |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 114 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 115 | found_mem64 = 0; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 116 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | |
| 118 | /* Check the BAR type and set our address mask */ |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 119 | if (bar_response & PCI_BASE_ADDRESS_SPACE) { |
Jin Zhengxiong-R64188 | f4ff3e8 | 2006-06-27 18:12:02 +0800 | [diff] [blame] | 120 | bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK)) |
| 121 | & 0xffff) + 1; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 122 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | bar_res = io; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 124 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 125 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 126 | DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ", bar_nr, (u64)bar_size); |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 127 | } else { |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 128 | if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 129 | PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 130 | u32 bar_response_upper; |
| 131 | u64 bar64; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 132 | |
| 133 | #ifndef CONFIG_PCI_ENUM_ONLY |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 134 | pci_hose_write_config_dword(hose, dev, bar + 4, |
| 135 | 0xffffffff); |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 136 | #endif |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 137 | pci_hose_read_config_dword(hose, dev, bar + 4, |
| 138 | &bar_response_upper); |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 139 | |
| 140 | bar64 = ((u64)bar_response_upper << 32) | bar_response; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 141 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 142 | bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 143 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 144 | found_mem64 = 1; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 145 | #endif |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 146 | } else { |
| 147 | bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1); |
| 148 | } |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 149 | #ifndef CONFIG_PCI_ENUM_ONLY |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 150 | if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH)) |
| 151 | bar_res = prefetch; |
| 152 | else |
| 153 | bar_res = mem; |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 154 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 156 | DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%llx, ", bar_nr, (u64)bar_size); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 159 | #ifndef CONFIG_PCI_ENUM_ONLY |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 160 | if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | /* Write it out and update our limit */ |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 162 | pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 163 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 164 | if (found_mem64) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 165 | bar += 4; |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 166 | #ifdef CONFIG_SYS_PCI_64BIT |
| 167 | pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32)); |
| 168 | #else |
| 169 | /* |
| 170 | * If we are a 64-bit decoder then increment to the |
| 171 | * upper 32 bits of the bar and force it to locate |
| 172 | * in the lower 4GB of memory. |
| 173 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 174 | pci_hose_write_config_dword(hose, dev, bar, 0x00000000); |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 175 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 176 | } |
| 177 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 178 | } |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 179 | #endif |
| 180 | cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ? |
| 181 | PCI_COMMAND_IO : PCI_COMMAND_MEMORY; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 182 | |
| 183 | DEBUGF("\n"); |
| 184 | |
| 185 | bar_nr++; |
| 186 | } |
| 187 | |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 188 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat); |
Gary Jennejohn | 9a1263f | 2007-08-31 15:21:46 +0200 | [diff] [blame] | 189 | pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 190 | CONFIG_SYS_PCI_CACHE_LINE_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 191 | pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80); |
| 192 | } |
| 193 | |
Ed Swarthout | a523296 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 194 | void pciauto_prescan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 195 | pci_dev_t dev, int sub_bus) |
| 196 | { |
| 197 | struct pci_region *pci_mem = hose->pci_mem; |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 198 | struct pci_region *pci_prefetch = hose->pci_prefetch; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 199 | struct pci_region *pci_io = hose->pci_io; |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 200 | u16 cmdstat; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 201 | |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 202 | pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 203 | |
| 204 | /* Configure bus number registers */ |
Ed Swarthout | 4aeb55a | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 205 | pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, |
| 206 | PCI_BUS(dev) - hose->first_busno); |
| 207 | pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, |
| 208 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 209 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff); |
| 210 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 211 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 212 | /* Round memory allocator to 1MB boundary */ |
| 213 | pciauto_region_align(pci_mem, 0x100000); |
| 214 | |
| 215 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 216 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE, |
| 217 | (pci_mem->bus_lower & 0xfff00000) >> 16); |
| 218 | |
| 219 | cmdstat |= PCI_COMMAND_MEMORY; |
| 220 | } |
| 221 | |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 222 | if (pci_prefetch) { |
| 223 | /* Round memory allocator to 1MB boundary */ |
| 224 | pciauto_region_align(pci_prefetch, 0x100000); |
| 225 | |
| 226 | /* Set up memory and I/O filter limits, assume 32-bit I/O space */ |
| 227 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, |
| 228 | (pci_prefetch->bus_lower & 0xfff00000) >> 16); |
| 229 | |
| 230 | cmdstat |= PCI_COMMAND_MEMORY; |
| 231 | } else { |
| 232 | /* We don't support prefetchable memory for now, so disable */ |
| 233 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000); |
Matthew McClintock | 2f43f33 | 2006-06-28 10:44:23 -0500 | [diff] [blame] | 234 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0); |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 235 | } |
| 236 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 237 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | /* Round I/O allocator to 4KB boundary */ |
| 239 | pciauto_region_align(pci_io, 0x1000); |
| 240 | |
| 241 | pci_hose_write_config_byte(hose, dev, PCI_IO_BASE, |
| 242 | (pci_io->bus_lower & 0x0000f000) >> 8); |
| 243 | pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16, |
| 244 | (pci_io->bus_lower & 0xffff0000) >> 16); |
| 245 | |
| 246 | cmdstat |= PCI_COMMAND_IO; |
| 247 | } |
| 248 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | /* Enable memory and I/O accesses, enable bus master */ |
Andrew Sharp | f4f2482 | 2012-08-01 12:27:16 +0000 | [diff] [blame] | 250 | pci_hose_write_config_word(hose, dev, PCI_COMMAND, |
| 251 | cmdstat | PCI_COMMAND_MASTER); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Ed Swarthout | a523296 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 254 | void pciauto_postscan_setup_bridge(struct pci_controller *hose, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 255 | pci_dev_t dev, int sub_bus) |
| 256 | { |
| 257 | struct pci_region *pci_mem = hose->pci_mem; |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 258 | struct pci_region *pci_prefetch = hose->pci_prefetch; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 259 | struct pci_region *pci_io = hose->pci_io; |
| 260 | |
| 261 | /* Configure bus number registers */ |
Ed Swarthout | 4aeb55a | 2007-07-11 14:52:08 -0500 | [diff] [blame] | 262 | pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, |
| 263 | sub_bus - hose->first_busno); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 264 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 265 | if (pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 266 | /* Round memory allocator to 1MB boundary */ |
| 267 | pciauto_region_align(pci_mem, 0x100000); |
| 268 | |
| 269 | pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT, |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 270 | (pci_mem->bus_lower - 1) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 273 | if (pci_prefetch) { |
| 274 | /* Round memory allocator to 1MB boundary */ |
| 275 | pciauto_region_align(pci_prefetch, 0x100000); |
| 276 | |
| 277 | pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 278 | (pci_prefetch->bus_lower - 1) >> 16); |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 279 | } |
| 280 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 281 | if (pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 282 | /* Round I/O allocator to 4KB boundary */ |
| 283 | pciauto_region_align(pci_io, 0x1000); |
| 284 | |
| 285 | pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT, |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 286 | ((pci_io->bus_lower - 1) & 0x0000f000) >> 8); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 287 | pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16, |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 288 | ((pci_io->bus_lower - 1) & 0xffff0000) >> 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * |
| 294 | */ |
| 295 | |
| 296 | void pciauto_config_init(struct pci_controller *hose) |
| 297 | { |
| 298 | int i; |
| 299 | |
Thierry Reding | a3d5df3 | 2013-09-20 15:50:50 +0200 | [diff] [blame] | 300 | hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 301 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 302 | for (i = 0; i < hose->region_count; i++) { |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 303 | switch(hose->regions[i].flags) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 304 | case PCI_REGION_IO: |
| 305 | if (!hose->pci_io || |
| 306 | hose->pci_io->size < hose->regions[i].size) |
| 307 | hose->pci_io = hose->regions + i; |
| 308 | break; |
| 309 | case PCI_REGION_MEM: |
| 310 | if (!hose->pci_mem || |
| 311 | hose->pci_mem->size < hose->regions[i].size) |
| 312 | hose->pci_mem = hose->regions + i; |
| 313 | break; |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 314 | case (PCI_REGION_MEM | PCI_REGION_PREFETCH): |
| 315 | if (!hose->pci_prefetch || |
| 316 | hose->pci_prefetch->size < hose->regions[i].size) |
| 317 | hose->pci_prefetch = hose->regions + i; |
| 318 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 323 | if (hose->pci_mem) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 324 | pciauto_region_init(hose->pci_mem); |
| 325 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 326 | DEBUGF("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n" |
| 327 | "\t\tPhysical Memory [%llx-%llxx]\n", |
| 328 | (u64)hose->pci_mem->bus_start, |
| 329 | (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1), |
| 330 | (u64)hose->pci_mem->phys_start, |
| 331 | (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 334 | if (hose->pci_prefetch) { |
| 335 | pciauto_region_init(hose->pci_prefetch); |
| 336 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 337 | DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n" |
| 338 | "\t\tPhysical Memory [%llx-%llx]\n", |
| 339 | (u64)hose->pci_prefetch->bus_start, |
| 340 | (u64)(hose->pci_prefetch->bus_start + |
| 341 | hose->pci_prefetch->size - 1), |
| 342 | (u64)hose->pci_prefetch->phys_start, |
| 343 | (u64)(hose->pci_prefetch->phys_start + |
| 344 | hose->pci_prefetch->size - 1)); |
Kumar Gala | e5ce420 | 2006-01-11 13:24:15 -0600 | [diff] [blame] | 345 | } |
| 346 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 347 | if (hose->pci_io) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 348 | pciauto_region_init(hose->pci_io); |
| 349 | |
Kumar Gala | ad714f5 | 2008-10-21 08:36:08 -0500 | [diff] [blame] | 350 | DEBUGF("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n" |
| 351 | "\t\tPhysical Memory: [%llx-%llx]\n", |
| 352 | (u64)hose->pci_io->bus_start, |
| 353 | (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1), |
| 354 | (u64)hose->pci_io->phys_start, |
| 355 | (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1)); |
Ed Swarthout | a523296 | 2007-07-11 14:51:48 -0500 | [diff] [blame] | 356 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 360 | /* |
| 361 | * HJF: Changed this to return int. I think this is required |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 362 | * to get the correct result when scanning bridges |
| 363 | */ |
| 364 | int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 365 | { |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 366 | unsigned int sub_bus = PCI_BUS(dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 367 | unsigned short class; |
| 368 | unsigned char prg_iface; |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 369 | int n; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 370 | |
| 371 | pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class); |
| 372 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 373 | switch (class) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 374 | case PCI_CLASS_BRIDGE_PCI: |
wdenk | b666c8f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 375 | hose->current_busno++; |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 376 | pciauto_setup_device(hose, dev, 2, hose->pci_mem, |
| 377 | hose->pci_prefetch, hose->pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 378 | |
wdenk | b666c8f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 379 | DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev)); |
wdenk | 6cfa84e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 380 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 381 | /* Passing in current_busno allows for sibling P2P bridges */ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 382 | pciauto_prescan_setup_bridge(hose, dev, hose->current_busno); |
wdenk | 6cfa84e | 2004-02-10 00:03:41 +0000 | [diff] [blame] | 383 | /* |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 384 | * need to figure out if this is a subordinate bridge on the bus |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 385 | * to be able to properly set the pri/sec/sub bridge registers. |
| 386 | */ |
| 387 | n = pci_hose_scan_bus(hose, hose->current_busno); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 388 | |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 389 | /* figure out the deepest we've gone for this leg */ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 390 | sub_bus = max(n, sub_bus); |
wdenk | b666c8f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 391 | pciauto_postscan_setup_bridge(hose, dev, sub_bus); |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 392 | |
wdenk | b666c8f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 393 | sub_bus = hose->current_busno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case PCI_CLASS_STORAGE_IDE: |
| 397 | pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface); |
wdenk | 56ed43e | 2004-02-22 23:46:08 +0000 | [diff] [blame] | 398 | if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) { |
| 399 | DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n"); |
| 400 | return sub_bus; |
| 401 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 402 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 403 | pciauto_setup_device(hose, dev, 6, hose->pci_mem, |
| 404 | hose->pci_prefetch, hose->pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 405 | break; |
| 406 | |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 407 | case PCI_CLASS_BRIDGE_CARDBUS: |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 408 | /* |
| 409 | * just do a minimal setup of the bridge, |
| 410 | * let the OS take care of the rest |
| 411 | */ |
| 412 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, |
| 413 | hose->pci_prefetch, hose->pci_io); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 414 | |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 415 | DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n", |
| 416 | PCI_DEV(dev)); |
wdenk | 1fe2c70 | 2003-03-06 21:55:29 +0000 | [diff] [blame] | 417 | |
| 418 | hose->current_busno++; |
| 419 | break; |
| 420 | |
TsiChung Liew | 521f97b | 2008-03-30 01:19:06 -0500 | [diff] [blame] | 421 | #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE) |
wdenk | 5d84173 | 2003-08-17 18:55:18 +0000 | [diff] [blame] | 422 | case PCI_CLASS_BRIDGE_OTHER: |
| 423 | DEBUGF("PCI Autoconfig: Skipping bridge device %d\n", |
| 424 | PCI_DEV(dev)); |
| 425 | break; |
| 426 | #endif |
Reinhard Arlt | 4691179 | 2009-07-25 06:19:12 +0200 | [diff] [blame] | 427 | #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349) |
Rafal Jaworowski | 384da5e | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 428 | case PCI_CLASS_BRIDGE_OTHER: |
| 429 | /* |
| 430 | * The host/PCI bridge 1 seems broken in 8349 - it presents |
| 431 | * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_ |
| 432 | * device claiming resources io/mem/irq.. we only allow for |
| 433 | * the PIMMR window to be allocated (BAR0 - 1MB size) |
| 434 | */ |
| 435 | DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n"); |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 436 | pciauto_setup_device(hose, dev, 0, hose->pci_mem, |
| 437 | hose->pci_prefetch, hose->pci_io); |
Rafal Jaworowski | 384da5e | 2005-10-17 02:39:53 +0200 | [diff] [blame] | 438 | break; |
| 439 | #endif |
Andrew Sharp | 61d47ca | 2012-08-29 14:16:32 +0000 | [diff] [blame] | 440 | |
| 441 | case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */ |
| 442 | DEBUGF("PCI AutoConfig: Found PowerPC device\n"); |
| 443 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 444 | default: |
Andrew Sharp | 6870513 | 2012-08-29 14:16:29 +0000 | [diff] [blame] | 445 | pciauto_setup_device(hose, dev, 6, hose->pci_mem, |
| 446 | hose->pci_prefetch, hose->pci_io); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 447 | break; |
| 448 | } |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 449 | |
| 450 | return sub_bus; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 451 | } |