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