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